UNPKG

litemap

Version:

A TypeScript library for mapping and processing data with SQLite integration

141 lines (140 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const sqlite3_1 = require("sqlite3"); const sqlite_1 = require("sqlite"); class SQLiteDatabase { constructor(filename = "./db/litemap.db") { this.initialized = false; this.dbPromise = (0, sqlite_1.open)({ filename, driver: sqlite3_1.Database, }); this.initialize(); this.initializeItems(); } async initialize() { if (this.initialized) return; const db = await this.dbPromise; await db.exec(` CREATE TABLE IF NOT EXISTS users ( key TEXT PRIMARY KEY, value TEXT ); CREATE INDEX IF NOT EXISTS idx_key ON users(key); `); this.initialized = true; } async initializeItems() { const db = await this.dbPromise; await db.exec(` CREATE TABLE IF NOT EXISTS items ( key TEXT PRIMARY KEY, value TEXT ); CREATE INDEX IF NOT EXISTS idx_item_key ON items(key); `); this.initialized = true; } async set(key, value) { if (!key || value === undefined) throw new Error("Key and value must be provided"); const db = await this.dbPromise; const valueString = JSON.stringify(value); await db.run("INSERT OR REPLACE INTO users (key, value) VALUES (?, ?)", [ key, valueString, ]); } async batchSetUsers(records) { if (!records.length) return; const db = await this.dbPromise; await db.run("BEGIN TRANSACTION"); try { for (const { key, value } of records) { const valueString = JSON.stringify(value); await db.run("INSERT OR REPLACE INTO users (key, value) VALUES (?, ?)", [key, valueString]); } await db.run("COMMIT"); } catch (error) { await db.run("ROLLBACK"); throw error; } } // batch set for items async batchSetItems(records) { if (!records.length) return; const db = await this.dbPromise; await db.run("BEGIN TRANSACTION"); try { for (const { key, value } of records) { const valueString = JSON.stringify(value); await db.run("INSERT OR REPLACE INTO items (key, value) VALUES (?, ?)", [key, valueString]); } await db.run("COMMIT"); } catch (error) { await db.run("ROLLBACK"); throw error; } } async append(key, value) { if (!key || value === undefined) throw new Error("Key and value must be provided"); const db = await this.dbPromise; const existingRow = await db.get("SELECT value FROM users WHERE key = ?", [ key, ]); const newValue = existingRow ? { ...JSON.parse(existingRow.value), ...value } : value; const valueString = JSON.stringify(newValue); await db.run("INSERT OR REPLACE INTO users (key, value) VALUES (?, ?)", [ key, valueString, ]); } async get(key) { if (!key) throw new Error("Key must be provided"); const db = await this.dbPromise; const row = await db.get("SELECT value FROM users WHERE key = ?", [key]); return row ? JSON.parse(row.value) : undefined; } async getAllKeys(prefix = "") { const db = await this.dbPromise; const query = prefix ? "SELECT key FROM users WHERE key LIKE ?" : "SELECT key FROM users"; const rows = await db.all(query, prefix ? [`${prefix}%`] : []); return rows.map((row) => row.key); } async delete(key) { if (!key) throw new Error("Key must be provided"); const db = await this.dbPromise; const result = await db.run("DELETE FROM users WHERE key = ?", [key]); return result.changes > 0; } async getRawDataMap() { const db = await this.dbPromise; const rows = await db.all("SELECT key, value FROM users"); const dataMap = {}; for (const row of rows) { dataMap[row.key] = JSON.parse(row.value); } return dataMap; } async clear() { const db = await this.dbPromise; await db.run("DELETE FROM users"); } async close() { const db = await this.dbPromise; await db.close(); } } exports.default = SQLiteDatabase;