Skip to content

Commit

Permalink
fix delete function (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Dec 9, 2023
1 parent 6a5d526 commit 7e216f7
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/middlelayers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ export async function selectFromDatabaseWithSql<T extends object>(sql: string, v
export async function deleteFromDatabase<T extends object>(table: string, where: Partial<T>, allowFullDelete = false) {
const db = await getDatabase()

const whereKeys = _(where).filter(v => !_(v).isUndefined()).keys().value()
const filteredWhere = _(where).omitBy(v => _(v).isUndefined()).value()
const whereKeys = _(filteredWhere).keys().value()
if (!allowFullDelete && whereKeys.length === 0) {
throw new Error("Delete without where is not allowed")
}

const whereStr = _(whereKeys).map(k => `${k}=?`).join(' AND ')
const values = _(whereKeys).map(k => _(where).get(k)).value()
const values = _(filteredWhere).map(v => v).value()


const sql = `DELETE FROM ${table} WHERE ${whereStr}`

return db.execute(sql, values)
}

0 comments on commit 7e216f7

Please sign in to comment.