sqlitekv
Version:
SQLiteKV is a key-value store built on top of SQLite3. This project allows you to perform basic key-value operations with the additional power of SQLite3, making it suitable for applications requiring persistent, lightweight, and reliable storage.
109 lines (108 loc) • 3.39 kB
JavaScript
import { SQLiteDatabase } from "./database";
export class SQLiteKV {
db;
initPromise;
constructor(dbFilename = "database.sqlite", configOrTableName) {
let config = {};
if (typeof configOrTableName === "string") {
config.tableName = configOrTableName;
}
else if (typeof configOrTableName === "object") {
config = configOrTableName;
}
const tableName = config.tableName ?? "kv_store";
const autoCommit = config.autoCommit ?? true;
const journalMode = config.journalMode ?? "WAL";
const sqliteMode = config.sqliteMode ?? "disk";
const logQueries = config.logQueries ?? false;
this.db = new SQLiteDatabase(dbFilename, tableName, autoCommit, journalMode, sqliteMode, logQueries);
this.initPromise = this.init();
}
getAll() {
throw new Error("Method not implemented.");
}
async convertToJson(jsonFilePath) {
await this.ensureInitialized();
return this.db.convertToJson(jsonFilePath);
}
async init() {
await this.db.init();
}
async ensureInitialized() {
await this.initPromise;
}
async set(key, value, oneTime = false) {
await this.ensureInitialized();
return this.db.set(key, value, oneTime);
}
async setex(key, seconds, value, oneTime = false) {
await this.ensureInitialized();
return this.db.setex(key, seconds, value, oneTime);
}
async get(key) {
await this.ensureInitialized();
return this.db.get(key);
}
async delete(key) {
await this.ensureInitialized();
return this.db.delete(key);
}
async exists(key) {
await this.ensureInitialized();
return this.db.exists(key);
}
async keys(pattern) {
await this.ensureInitialized();
return this.db.keys(pattern);
}
async close() {
await this.ensureInitialized();
await this.db.close();
return new Uint8Array(0);
}
async clear() {
await this.ensureInitialized();
await this.db.clear();
return true;
}
async size() {
await this.ensureInitialized();
const keys = await this.keys();
return keys.length;
}
async increment(key, amount = 1) {
await this.ensureInitialized();
const value = await this.get(key);
if (typeof value === "number") {
const newValue = value + amount;
await this.set(key, newValue);
return newValue;
}
return null;
}
async mget(...keys) {
await this.ensureInitialized();
const values = await Promise.all(keys.map((key) => this.get(key)));
return values.filter((value) => value !== null);
}
async beginTransaction() {
await this.ensureInitialized();
await this.db.beginTransaction();
}
async commitTransaction() {
await this.ensureInitialized();
await this.db.commitTransaction();
}
async ttl(key) {
await this.ensureInitialized();
return this.db.ttl(key);
}
async setJournalMode(mode) {
await this.ensureInitialized();
await this.db.setJournalMode(mode);
}
async getJournalMode() {
await this.ensureInitialized();
return this.db.getJournalMode();
}
}