lakutata
Version:
An IoC-based universal application framework.
158 lines (141 loc) • 4.56 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module"
});
const t = require("events");
const e = require("util");
const s = require("./Package.65.cjs");
const i = require("sqlite3");
require("buffer");
const a = t => t && t.__esModule ? t : {
default: t
};
const n = a(t);
const r = a(i);
var o = t => String(t).search(/^[a-zA-Z]+$/) < 0 ? "_" + t : t;
var c = class extends n.default {
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 r.default.Database(s.db, (a => {
if (a) {
e(a);
} else {
if (s.busyTimeout) {
i.configure("busyTimeout", s.busyTimeout);
}
t(i);
}
}));
})).then((t => ({
query: e.promisify(t.all).bind(t),
close: e.promisify(t.close).bind(t)
})));
this.opts = {
table: "keyv",
keySize: 255,
...s
};
this.opts.table = o(this.opts.table);
const i = `CREATE TABLE IF NOT EXISTS ${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`;
const a = this.opts.connect().then((async t => t.query(i).then((() => t)))).catch((t => this.emit("error", t)));
this.query = async (t, ...e) => a.then((async s => s.query(t, ...e)));
this.close = async () => a.then((t => t.close()));
}
async get(t) {
const e = `SELECT * FROM ${this.opts.table} WHERE key = ?`;
const s = await this.query(e, t);
const i = s[0];
if (i === void 0) {
return void 0;
}
return i.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 i = await this.query(e, t);
const a = i[0];
if (a === 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(i, a, n) {
const r = `SELECT * FROM ${a.table} WHERE key LIKE ? LIMIT ? OFFSET ?`;
const o = await n(r, [ `${t ? t + ":" : ""}%`, e, i ]);
const c = [ ...o ];
if (c.length === 0) {
return;
}
for (const t of c) {
i += 1;
yield [ t.key, t.value ];
}
yield* s(i, a, 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 u = t => new s.index_default({
store: new c(t)
});
var l = c;
exports.KeyvSqlite = c;
exports.createKeyv = u;
exports.default = l;