import{eq as o}from"drizzle-orm";import{isTtlExpired as l}from"../../../utils/is-ttl-expired.js";import{logger as n}from"../../../tools/notifiers/logger.js";import{LocalDatabaseInitializationStrategy as s}from"../../../providers/database/database-initialization-strategy.js";import{PLUGIN_STORAGE_LOCAL_DATABASE_FOLDER as c,PLUGIN_STORAGE_DATABASE_MIGRATIONS_FOLDER as u,PLUGIN_STORAGE_LOCAL_DATABASE_NAME as d}from"../../../providers/database/constants.js";import{pluginStorageTable as r}from"../../../providers/database/databases/plugin-storage-sqlite/schemas/plugin-storage-table.js";import{createPluginStorageDbRecord as g}from"../mappers/create-plugin-storage-db-record.js";import{createPluginStorageReadModel as m}from"../mappers/create-plugin-storage-read-model.js";class i{static#e;#t;constructor(e){this.#t=e.client}static async getInstance(e){if(!i.#e){const a=await new s().initialize({...e,databaseName:d,additionalFolder:c,migrationsFolder:u});i.#e=new i(a)}return i.#e}async get(e){const t=await this.#t.client.select().from(r).where(o(r.key,e)).get();if(!t)return null;const a=m(t);return l(a.ttlInSeconds,a.createdAt)?(await this.delete(e),null):a}async set(e){try{const t=g(e);return await this.#t.client.insert(r).values(t).onConflictDoUpdate({target:[r.key],set:{value:t.value,ttl:t.ttl,updatedAt:t.updatedAt}}),t.key}catch(t){return n.error("Error setting storage in plugin storage",t),null}}async delete(e){try{return await this.#t.client.delete(r).where(o(r.key,e)),!0}catch(t){return n.error("Error deleting storage from plugin storage",t),!1}}async clearAll(){try{return await this.#t.client.delete(r),!0}catch(e){return n.error("Error clearing all storage from plugin storage",e),!1}}}export{i as PluginStorageLocalRepository};
