lakutata
Version:
An IoC-based universal application framework.
140 lines (130 loc) • 4.32 kB
JavaScript
import t from "events";
import { promisify as e } from "util";
import { i as s } from "./Package.65.mjs";
import a from "sqlite3";
import "buffer";
var i = t => String(t).search(/^[a-zA-Z]+$/) < 0 ? "_" + t : t;
var n = class extends t {
ttlSupport;
opts;
namespace;
close;
query;
constructor(t) {
super();
this.ttlSupport = false;
let s = {
dialect: "sqlite",
uri: "sqlite://:memory:"
};
if (typeof t === "string") {
s.uri = t;
} else {
s = {
...s,
...t
};
}
s.db = s.uri.replace(/^sqlite:\/\//, "");
s.connect = async () => new Promise(((t, e) => {
const i = new a.Database(s.db, (a => {
if (a) {
e(a);
} else {
if (s.busyTimeout) {
i.configure("busyTimeout", s.busyTimeout);
}
t(i);
}
}));
})).then((t => ({
query: e(t.all).bind(t),
close: e(t.close).bind(t)
})));
this.opts = {
table: "keyv",
keySize: 255,
...s
};
this.opts.table = i(this.opts.table);
const n = `CREATE TABLE IF NOT EXISTS ${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`;
const o = this.opts.connect().then((async t => t.query(n).then((() => t)))).catch((t => this.emit("error", t)));
this.query = async (t, ...e) => o.then((async s => s.query(t, ...e)));
this.close = async () => o.then((t => t.close()));
}
async get(t) {
const e = `SELECT * FROM ${this.opts.table} WHERE key = ?`;
const s = await this.query(e, t);
const a = s[0];
if (a === void 0) {
return void 0;
}
return a.value;
}
async getMany(t) {
const e = `SELECT * FROM ${this.opts.table} WHERE key IN (SELECT value FROM json_each(?))`;
const s = await this.query(e, JSON.stringify(t));
return t.map((t => {
const e = s.find((e => e.key === t));
return e ? e.value : void 0;
}));
}
async set(t, e) {
const s = `INSERT INTO ${this.opts.table} (key, value)\n\t\t\tVALUES(?, ?) \n\t\t\tON CONFLICT(key) \n\t\t\tDO UPDATE SET value=excluded.value;`;
return this.query(s, t, e);
}
async delete(t) {
const e = `SELECT * FROM ${this.opts.table} WHERE key = ?`;
const s = `DELETE FROM ${this.opts.table} WHERE key = ?`;
const a = await this.query(e, t);
const i = a[0];
if (i === void 0) {
return false;
}
await this.query(s, t);
return true;
}
async deleteMany(t) {
const e = `DELETE FROM ${this.opts.table} WHERE key IN (SELECT value FROM json_each(?))`;
const s = await this.getMany(t);
if (s.every((t => t === void 0))) {
return false;
}
await this.query(e, JSON.stringify(t));
return true;
}
async clear() {
const t = `DELETE FROM ${this.opts.table} WHERE key LIKE ?`;
await this.query(t, this.namespace ? `${this.namespace}:%` : "%");
}
async* iterator(t) {
const e = Number.parseInt(this.opts.iterationLimit, 10) || 10;
async function* s(a, i, n) {
const o = `SELECT * FROM ${i.table} WHERE key LIKE ? LIMIT ? OFFSET ?`;
const r = await n(o, [ `${t ? t + ":" : ""}%`, e, a ]);
const c = [ ...r ];
if (c.length === 0) {
return;
}
for (const t of c) {
a += 1;
yield [ t.key, t.value ];
}
yield* s(a, i, n);
}
yield* s(0, this.opts, this.query);
}
async has(t) {
const e = `SELECT EXISTS ( SELECT * FROM ${this.opts.table} WHERE key = ? )`;
const s = await this.query(e, t);
return Object.values(s[0])[0] === 1;
}
async disconnect() {
await this.close();
}
};
var o = t => new s({
store: new n(t)
});
var r = n;
export { n as KeyvSqlite, o as createKeyv, r as default };