diff --git a/web/database/queries/db-queries.js b/web/database/queries/db-queries.js --- a/web/database/queries/db-queries.js +++ b/web/database/queries/db-queries.js @@ -27,6 +27,11 @@ name TEXT UNIQUE PRIMARY KEY NOT NULL, data TEXT NOT NULL ); + + CREATE TABLE IF NOT EXISTS persist_storage ( + key TEXT UNIQUE PRIMARY KEY NOT NULL, + item TEXT NOT NULL + ); `); } diff --git a/web/database/queries/storage-engine-queries.js b/web/database/queries/storage-engine-queries.js new file mode 100644 --- /dev/null +++ b/web/database/queries/storage-engine-queries.js @@ -0,0 +1,59 @@ +// @flow + +import { SqliteDatabase } from 'sql.js'; + +import { parseMultiStatementSQLiteResult } from '../utils/db-utils.js'; + +type Entry = { + key: string, + item: string, +}; + +function setPersistStorageItem(db: SqliteDatabase, key: string, item: string) { + const query = ` + INSERT OR REPLACE INTO persist_storage (key, item) + VALUES ($key, $item) + `; + const params = { + $key: key, + $item: item, + }; + + db.exec(query, params); +} + +function getPersistStorageItem(db: SqliteDatabase, key: string): string { + const query = ` + SELECT * FROM persist_storage + WHERE key = $key + `; + const params = { + $key: key, + }; + + const rawResult = db.exec(query, params); + const result = parseMultiStatementSQLiteResult(rawResult); + if (result.length === 0 || result[0].length === 0) { + return ''; + } + const [entry] = result[0]; + return entry.item; +} + +function removePersistStorageItem(db: SqliteDatabase, key: string) { + const query = ` + DELETE FROM persist_storage + WHERE key = $key + `; + const params = { + $key: key, + }; + + db.exec(query, params); +} + +export { + setPersistStorageItem, + getPersistStorageItem, + removePersistStorageItem, +};