UNPKG

lakutata

Version:

An IoC-based universal application framework.

169 lines (156 loc) 5.12 kB
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */ "use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const t = require("events"); require("buffer"); const s = require("pg"); const e = t => t && t.__esModule ? t : { default: t }; const a = e(t); const i = e(s); var o; var n; var c = (t, s = {}) => { if (n !== t) { o = void 0; n = t; } o ??= new i.default.Pool({ connectionString: t, ...s }); return o; }; var r = async () => { await (o?.end()); n = void 0; }; exports.KeyvPostgres = class KeyvPostgres extends a.default { ttlSupport; opts; query; namespace; constructor(t) { super(); this.ttlSupport = false; if (typeof t === "string") { const s = t; t = { dialect: "postgres", uri: s }; } else { t = { dialect: "postgres", uri: "postgresql://localhost:5432", ...t }; } this.opts = { table: "keyv", schema: "public", keySize: 255, ...t }; let s = `CREATE${this.opts.useUnloggedTable ? " UNLOGGED " : " "}TABLE IF NOT EXISTS ${this.opts.schema}.${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`; if (this.opts.schema !== "public") { s = `CREATE SCHEMA IF NOT EXISTS ${this.opts.schema}; ${s}`; } const e = this.connect().then(async t => { try { await t(s); } catch (s) { if (s.code !== "23505") { this.emit("error", s); } return t; } return t; }).catch(t => this.emit("error", t)); this.query = async (t, s) => e.then(e => e(t, s)); } async get(t) { const s = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; const e = await this.query(s, [ t ]); const a = e[0]; return a === void 0 ? void 0 : a.value; } async getMany(t) { const s = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; const e = await this.query(s, [ t ]); const a = new Map(e.map(t => [ t.key, t ])); return t.map(t => a.get(t)?.value); } async set(t, s) { const e = `INSERT INTO ${this.opts.schema}.${this.opts.table} (key, value)\n VALUES($1, $2) \n ON CONFLICT(key) \n DO UPDATE SET value=excluded.value;`; await this.query(e, [ t, s ]); } async setMany(t) { const s = []; const e = []; for (const {key: a, value: i} of t) { s.push(a); e.push(i); } const a = `INSERT INTO ${this.opts.schema}.${this.opts.table} (key, value)\n SELECT * FROM UNNEST($1::text[], $2::text[])\n ON CONFLICT(key)\n DO UPDATE SET value=excluded.value;`; await this.query(a, [ s, e ]); } async delete(t) { const s = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; const e = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; const a = await this.query(s, [ t ]); if (a[0] === void 0) { return false; } await this.query(e, [ t ]); return true; } async deleteMany(t) { const s = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; const e = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; const a = await this.query(s, [ t ]); if (a[0] === void 0) { return false; } await this.query(e, [ t ]); return true; } async clear() { const t = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key LIKE $1`; await this.query(t, [ this.namespace ? `${this.namespace}:%` : "%" ]); } async* iterator(t) { const s = Number.parseInt(String(this.opts.iterationLimit), 10) || 10; async function* e(a, i, o) { const n = `SELECT * FROM ${i.schema}.${i.table} WHERE key LIKE $1 LIMIT $2 OFFSET $3`; const c = await o(n, [ `${t ? t + ":" : ""}%`, s, a ]); if (c.length === 0) { return; } for (const t of c) { a += 1; yield [ t.key, t.value ]; } yield* e(a, i, o); } yield* e(0, this.opts, this.query); } async has(t) { const s = `SELECT EXISTS ( SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1 )`; const e = await this.query(s, [ t ]); return e[0].exists; } async connect() { const t = c(this.opts.uri, this.opts); return async (s, e) => { const a = await t.query(s, e); return a.rows; }; } async disconnect() { await r(); } };