lakutata
Version:
An IoC-based universal application framework.
176 lines (157 loc) • 4.89 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module"
});
const t = require("events");
const e = require("./Package.65.cjs");
const s = require("pg");
require("buffer");
const a = t => t && t.__esModule ? t : {
default: t
};
const i = a(t);
const o = a(s);
var r;
var n;
var c = (t, e = {}) => {
if (n !== t) {
r = void 0;
n = t;
}
r ??= new o.default.Pool({
connectionString: t,
...e
});
return r;
};
var h = async () => {
await r.end();
n = void 0;
};
var u = class extends i.default {
ttlSupport;
opts;
query;
namespace;
constructor(t) {
super();
this.ttlSupport = false;
if (typeof t === "string") {
const e = t;
t = {
dialect: "postgres",
uri: e
};
} else {
t = {
dialect: "postgres",
uri: "postgresql://localhost:5432",
...t
};
}
const e = async () => {
const e = c(t.uri, t);
return async (t, s) => {
const a = await e.query(t, s);
return a.rows;
};
};
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 a = e().then((async t => {
try {
await t(s);
} catch (e) {
if (e.code !== "23505") {
this.emit("error", e);
}
return t;
}
return t;
})).catch((t => this.emit("error", t)));
this.query = async (t, e) => a.then((s => s(t, e)));
}
async get(t) {
const e = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`;
const s = await this.query(e, [ t ]);
const a = s[0];
return a === void 0 ? void 0 : a.value;
}
async getMany(t) {
const e = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`;
const s = await this.query(e, [ t ]);
const a = [];
for (const e of t) {
const t = s?.findIndex((t => t.key === e));
a.push(t > -1 ? s[t].value : void 0);
}
return a;
}
async set(t, e) {
const s = `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(s, [ t, e ]);
}
async delete(t) {
const e = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`;
const s = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`;
const a = await this.query(e, [ t ]);
if (a[0] === void 0) {
return false;
}
await this.query(s, [ t ]);
return true;
}
async deleteMany(t) {
const e = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`;
const s = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`;
const a = await this.query(e, [ t ]);
if (a[0] === void 0) {
return false;
}
await this.query(s, [ 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 e = Number.parseInt(String(this.opts.iterationLimit), 10) || 10;
async function* s(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 + ":" : ""}%`, e, a ]);
if (n.length === 0) {
return;
}
for (const t of n) {
a += 1;
yield [ t.key, t.value ];
}
yield* s(a, i, o);
}
yield* s(0, this.opts, this.query);
}
async has(t) {
const e = `SELECT EXISTS ( SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1 )`;
const s = await this.query(e, [ t ]);
return s[0].exists;
}
async disconnect() {
await h();
}
};
var E = t => new e.index_default({
store: new u(t)
});
var y = u;
exports.KeyvPostgres = u;
exports.createKeyv = E;
exports.default = y;