UNPKG

lakutata

Version:

An IoC-based universal application framework.

181 lines (171 loc) 5.43 kB
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */ import t from "events"; import e from "mysql2"; var s; var o; var a = 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 (o !== t) { s = void 0; o = t; } const n = a(t); const r = { ...n, ...i }; s ??= e.createPool(r); return s.promise(); }; var n = () => { if (s) { s.end(); } o = 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 o = 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 a = `CREATE TABLE IF NOT EXISTS ${this.opts.table}(id VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT)`; const n = o().then(async t => { await t(a); 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 o = e.format(s, [ t ]); const a = await this.query(o); const i = a[0]; return i?.value; } async getMany(t) { const s = `SELECT * FROM ${this.opts.table} WHERE id IN (?)`; const o = e.format(s, [ t ]); const a = await this.query(o); const i = []; for (const e of t) { const t = a.findIndex(t => t.id === e); i.push(t === -1 ? void 0 : a[t].value); } return i; } async set(t, s) { const o = `INSERT INTO ${this.opts.table} (id, value)\n\t\t\tVALUES(?, ?)\n\t\t\tON DUPLICATE KEY UPDATE value=?;`; const a = [ t, s, s ]; const i = e.format(o, a); return this.query(i); } async delete(t) { const s = `SELECT * FROM ${this.opts.table} WHERE id = ?`; const o = e.format(s, [ t ]); const a = `DELETE FROM ${this.opts.table} WHERE id = ?`; const i = e.format(a, [ t ]); const n = await this.query(o); 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 o = e.format(s, [ t ]); const a = await this.query(o); return a.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* o(a, i, n) { const r = `SELECT * FROM ${i.table} WHERE id LIKE ? LIMIT ? OFFSET ?`; const c = e.format(r, [ `${t ? t + ":" : ""}%`, s, a ]); const E = await n(c); if (E.length === 0) { return; } for (const t of E) { a += 1; yield [ t.id, t.value ]; } yield* o(a, i, n); } yield* o(0, this.opts, this.query); } async has(t) { const s = `SELECT EXISTS ( SELECT * FROM ${this.opts.table} WHERE id = ? )`; const o = e.format(s, [ t ]); const a = await this.query(o); return Object.values(a[0])[0] === 1; } async disconnect() { n(); } }; export { c as KeyvMysql };