UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

84 lines 3.06 kB
/** * @module Lock */ import { TimeSpan, } from "../../../../utilities/_module-exports.js"; import { Kysely } from "kysely"; import { KyselyTableNameTransformerPlugin } from "../../../../utilities/_module-exports.js"; import { LibsqlDialect } from "@libsql/kysely-libsql"; import { KyselyLockAdapter } from "../../../../lock/implementations/adapters/kysely-lock-adapter/_module.js"; /** * To utilize the `LibsqlLockAdapter`, you must install the `"@libsql/client"` package. * * Note in order to use `LibsqlLockAdapter` correctly, ensure you use a single, consistent database across all server instances. * This means you can't use libsql embedded replicas. * * IMPORT_PATH: `"@daiso-tech/core/lock/adapters"` * @group Adapters */ export class LibsqlLockAdapter { databaseLockAdapter; /*** * @example * ```ts * import { LibsqlLockAdapter } from "@daiso-tech/core/lock/adapters"; * import { createClient } from "@libsql/client"; * * const database = createClient({ url: "file:local.db" }); * const lockAdapter = new LibsqlLockAdapter({ * database, * }); * // You need initialize the adapter once before using it. * await lockAdapter.init(); * ``` */ constructor(settings) { const { database, tableName = "lock", expiredKeysRemovalInterval = TimeSpan.fromMinutes(1), shouldRemoveExpiredKeys = true, } = settings; this.databaseLockAdapter = new KyselyLockAdapter({ database: new Kysely({ dialect: new LibsqlDialect({ client: database, }), plugins: [ new KyselyTableNameTransformerPlugin({ lock: tableName, }), ], }), expiredKeysRemovalInterval, shouldRemoveExpiredKeys, }); } async removeExpiredKeys() { await this.databaseLockAdapter.removeExpiredKeys(); } /** * Removes the table where the lock keys are stored and removes the table indexes. * Note all lock data will be removed. */ async deInit() { await this.databaseLockAdapter.deInit(); } /** * Creates the table where the lock keys are stored and it's related indexes. * Note the `init` method needs to be called before using the adapter. */ async init() { await this.databaseLockAdapter.init(); } async insert(key, owner, expiration) { await this.databaseLockAdapter.insert(key, owner, expiration); } async update(key, owner, expiration) { return await this.databaseLockAdapter.update(key, owner, expiration); } async remove(key, owner) { await this.databaseLockAdapter.remove(key, owner); } async refresh(key, owner, expiration) { return await this.databaseLockAdapter.refresh(key, owner, expiration); } async find(key) { return await this.databaseLockAdapter.find(key); } } //# sourceMappingURL=libsql-lock-adapter.js.map