UNPKG

lakutata

Version:

An IoC-based universal application framework.

158 lines (146 loc) 4.65 kB
import t from "events"; import { i as s } from "./Package.65.mjs"; import e from "pg"; import "buffer"; var a; var i; var o = (t, s = {}) => { if (i !== t) { a = void 0; i = t; } a ??= new e.Pool({ connectionString: t, ...s }); return a; }; var r = async () => { await a.end(); i = void 0; }; var n = class extends t { 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 }; } const s = async () => { const s = o(t.uri, t); return async (t, e) => { const a = await s.query(t, e); return a.rows; }; }; this.opts = { table: "keyv", schema: "public", keySize: 255, ...t }; let e = `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") { e = `CREATE SCHEMA IF NOT EXISTS ${this.opts.schema}; ${e}`; } const a = s().then((async t => { try { await t(e); } 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) => a.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 = []; for (const s of t) { const t = e?.findIndex((t => t.key === s)); a.push(t > -1 ? e[t].value : void 0); } return a; } 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 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 r = `SELECT * FROM ${i.schema}.${i.table} WHERE key LIKE $1 LIMIT $2 OFFSET $3`; const n = await o(r, [ `${t ? t + ":" : ""}%`, s, a ]); if (n.length === 0) { return; } for (const t of n) { 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 disconnect() { await r(); } }; var c = t => new s({ store: new n(t) }); var h = n; export { n as KeyvPostgres, c as createKeyv, h as default };