lakutata
Version:
An IoC-based universal application framework.
181 lines (170 loc) • 5.36 kB
JavaScript
import t from "events";
import e from "mysql2";
var s;
var a;
var o = t => {
t = t.replace(/#/g, "%23");
const e = new URL(t);
const s = {
user: decodeURIComponent(e.username),
password: decodeURIComponent(e.password) || void 0,
host: e.hostname,
port: e.port ? Number.parseInt(e.port, 10) : void 0,
database: decodeURIComponent(e.pathname.slice(1))
};
for (const t of Object.keys(s)) {
if (s[t] === void 0) {
delete s[t];
}
}
return s;
};
var i = (t, i = {}) => {
if (a !== t) {
s = void 0;
a = t;
}
const n = o(t);
const r = {
...n,
...i
};
s ??= e.createPool(r);
return s.promise();
};
var n = () => {
if (s) {
s.end();
}
a = void 0;
};
var r = new Set([ "adapter", "compression", "connect", "dialect", "keySize", "table", "ttl", "uri" ]);
var c = class extends t {
ttlSupport=false;
opts;
namespace;
query;
constructor(t) {
super();
let e = {
dialect: "mysql",
uri: "mysql://localhost"
};
if (typeof t === "string") {
e.uri = t;
} else {
e = {
...e,
...t
};
}
if (e.intervalExpiration !== void 0 && e.intervalExpiration > 0) {
this.ttlSupport = true;
}
const s = Object.fromEntries(Object.entries(e).filter((([t]) => !r.has(t))));
delete s.namespace;
delete s.serialize;
delete s.deserialize;
const a = async () => {
const t = i(e.uri, s);
return async e => {
const s = await t.query(e);
return s[0];
};
};
this.opts = {
table: "keyv",
keySize: 255,
...e
};
const o = `CREATE TABLE IF NOT EXISTS ${this.opts.table}(id VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT)`;
const n = a().then((async t => {
await t(o);
if (this.opts.intervalExpiration !== void 0 && this.opts.intervalExpiration > 0) {
await t("SET GLOBAL event_scheduler = ON;");
await t("DROP EVENT IF EXISTS keyv_delete_expired_keys;");
await t(`CREATE EVENT IF NOT EXISTS keyv_delete_expired_keys ON SCHEDULE EVERY ${this.opts.intervalExpiration} SECOND\n\t\t\t\t\tDO DELETE FROM ${this.opts.table}\n\t\t\t\t\tWHERE CAST(value->'$.expires' AS UNSIGNED) BETWEEN 1 AND UNIX_TIMESTAMP(NOW(3)) * 1000;`);
}
return t;
})).catch((t => this.emit("error", t)));
this.query = async t => {
const e = await n;
return e(t);
};
}
async get(t) {
const s = `SELECT * FROM ${this.opts.table} WHERE id = ?`;
const a = e.format(s, [ t ]);
const o = await this.query(a);
const i = o[0];
return i?.value;
}
async getMany(t) {
const s = `SELECT * FROM ${this.opts.table} WHERE id IN (?)`;
const a = e.format(s, [ t ]);
const o = await this.query(a);
const i = [];
for (const e of t) {
const t = o.findIndex((t => t.id === e));
i.push(t === -1 ? void 0 : o[t].value);
}
return i;
}
async set(t, s) {
const a = `INSERT INTO ${this.opts.table} (id, value)\n\t\t\tVALUES(?, ?) \n\t\t\tON DUPLICATE KEY UPDATE value=?;`;
const o = [ t, s, s ];
const i = e.format(a, o);
return this.query(i);
}
async delete(t) {
const s = `SELECT * FROM ${this.opts.table} WHERE id = ?`;
const a = e.format(s, [ t ]);
const o = `DELETE FROM ${this.opts.table} WHERE id = ?`;
const i = e.format(o, [ t ]);
const n = await this.query(a);
const r = n[0];
if (r === void 0) {
return false;
}
await this.query(i);
return true;
}
async deleteMany(t) {
const s = `DELETE FROM ${this.opts.table} WHERE id IN (?)`;
const a = e.format(s, [ t ]);
const o = await this.query(a);
return o.affectedRows !== 0;
}
async clear() {
const t = `DELETE FROM ${this.opts.table} WHERE id LIKE ?`;
const s = e.format(t, [ this.namespace ? `${this.namespace}:%` : "%" ]);
await this.query(s);
}
async* iterator(t) {
const s = Number.parseInt(this.opts.iterationLimit, 10) || 10;
async function* a(o, i, n) {
const r = `SELECT * FROM ${i.table} WHERE id LIKE ? LIMIT ? OFFSET ?`;
const c = e.format(r, [ `${t ? t + ":" : ""}%`, s, o ]);
const E = await n(c);
if (E.length === 0) {
return;
}
for (const t of E) {
o += 1;
yield [ t.id, t.value ];
}
yield* a(o, i, n);
}
yield* a(0, this.opts, this.query);
}
async has(t) {
const e = `SELECT EXISTS ( SELECT * FROM ${this.opts.table} WHERE id = '${t}' )`;
const s = await this.query(e);
return Object.values(s[0])[0] === 1;
}
async disconnect() {
n();
}
};
var E = c;
export { c as KeyvMysql, E as default };