Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo Taylor committed Sep 9, 2022
1 parent 1c74e3e commit 32c20fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
28 changes: 14 additions & 14 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ const Item = object({
})

type Item = InferType<typeof Item>

const itemsTable = initializeDatabase().table("items", Item)

export default function App() {
const [text, setText] = useState("")
const [items, setItems] = useState<Item[]>([])
const add = async () => {
try {
let result = await itemsTable.insert<Item>({ value: text, done: false, id: 0 })
if (result.status != "success") return
result = await itemsTable.select()
if (result.status != "success") return
setItems(result.data as Item[])
let insert = await itemsTable.insert<Item>({ value: text, done: false, id: 0 })
if (insert.status != "success") return
let select = await itemsTable.select()
if (select.status != "success") return
setItems(select.data as Item[])
} catch (e) {
console.log("error", e)
}
Expand All @@ -80,21 +80,21 @@ export default function App() {
try {
let update = await itemsTable.update(item.id, item)
if (update.status != "success") return
let result = await itemsTable.select()
if (result.status != "success") return
setItems(result.data as Item[])
let select = await itemsTable.select()
if (select.status != "success") return
setItems(select.data as Item[])
} catch (e) {
console.log("error", e)
}
}

const deleteItem = async (id: number) => {
try {
let result = await itemsTable.delete(id)
if (result.status != "success") return
result = await itemsTable.select()
if (result.status != "success") return
setItems(result.data as Item[])
let deletion = await itemsTable.delete(id)
if (deletion.status != "success") return
let select = await itemsTable.select()
if (select.status != "success") return
setItems(select.data as Item[])
} catch (e) {
console.log("error", e)
}
Expand Down
21 changes: 9 additions & 12 deletions database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import {

type Status = "success" | "failure"

type StandardResult = {
status: "success" | "failure"
data: any
}

/**
* Creates or connects the database of the given name with each schema representing a table.
*
Expand Down Expand Up @@ -48,7 +43,6 @@ const initializeDatabase = (dbName = "main") => {
command += `, ${key} ${type}`
})
command += ");"
console.log(command)

sqlDB.transaction((tx) => {
tx.executeSql(command)
Expand Down Expand Up @@ -79,7 +73,7 @@ const initializeDatabase = (dbName = "main") => {

let command = `INSERT INTO ${tableName} (${columnNames}) VALUES (${columnPlaceholders})`

return new Promise<StandardResult>((resolve) => {
return new Promise<{ status: Status, data: T | null }>((resolve) => {
sqlDB.transaction((tx) => {
tx.executeSql(
command,
Expand All @@ -98,7 +92,7 @@ const initializeDatabase = (dbName = "main") => {

const select = async <T>() => {
const command = `SELECT * FROM ${tableName};`
return new Promise<StandardResult>((resolve) => {
return new Promise<{ status: Status, data: T[] | null }>((resolve) => {
sqlDB.transaction((tx) => {
tx.executeSql(
command,
Expand All @@ -121,8 +115,7 @@ const initializeDatabase = (dbName = "main") => {
columnStr += index === 0 ? column.toString() : `, ${column.toString()}`
})
const command = `SELECT ${columnStr} FROM ${tableName};`
return new Promise<StandardResult>((resolve) => {
resolve({ data: { done: false }, status: "success" })
return new Promise<{ status: Status, data: T[] | null }>((resolve) => {
sqlDB.transaction((tx) => {
tx.executeSql(
command,
Expand Down Expand Up @@ -153,12 +146,16 @@ const initializeDatabase = (dbName = "main") => {

const remove = async (id: number) => {
const command = `DELETE FROM ${tableName} WHERE id = ?;`
return new Promise<StandardResult>((resolve) => {
return new Promise<{ status: Status, data: null }>((resolve) => {
sqlDB.transaction((tx) => {
tx.executeSql(command, [id],
() => {
resolve({ status: "success", data: null })
},)
},
() => {
resolve({ status: "failure", data: null })
return false
})
})
})
}
Expand Down

0 comments on commit 32c20fc

Please sign in to comment.