Changeset View
Changeset View
Standalone View
Standalone View
web/database/queries/draft-queries.js
// @flow | // @flow | ||||
import type { SqliteDatabase } from 'sql.js'; | import { SqliteDatabase } from 'sql.js'; | ||||
import type { ClientDBDraftInfo } from 'lib/types/draft-types.js'; | |||||
import { parseMultiStatementSQLiteResult } from '../utils/db-utils.js'; | |||||
function removeAllDrafts(db: SqliteDatabase) { | function removeAllDrafts(db: SqliteDatabase) { | ||||
db.exec(`DELETE FROM drafts`); | db.exec(`DELETE FROM drafts`); | ||||
} | } | ||||
function updateDraft(db: SqliteDatabase, key: string, text: string) { | function updateDraft(db: SqliteDatabase, key: string, text: string) { | ||||
const query = ` | const query = ` | ||||
INSERT OR REPLACE INTO drafts (key, text) | INSERT OR REPLACE INTO drafts (key, text) | ||||
Show All 16 Lines | function moveDraft(db: SqliteDatabase, oldKey: string, newKey: string) { | ||||
const params = { | const params = { | ||||
$newKey: newKey, | $newKey: newKey, | ||||
$oldKey: oldKey, | $oldKey: oldKey, | ||||
}; | }; | ||||
db.exec(query, params); | db.exec(query, params); | ||||
} | } | ||||
export { removeAllDrafts, updateDraft, moveDraft }; | function getAllDrafts(db: SqliteDatabase): ClientDBDraftInfo[] { | ||||
const rawDBResult = db.exec(`SELECT * FROM drafts`); | |||||
const dbResult = | |||||
parseMultiStatementSQLiteResult<ClientDBDraftInfo>(rawDBResult); | |||||
if (dbResult.length === 0) { | |||||
return []; | |||||
} | |||||
return dbResult[0]; | |||||
} | |||||
export { removeAllDrafts, updateDraft, moveDraft, getAllDrafts }; |