@aphexcms/sqlite-adapter 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ import { and, eq, desc, count } from 'drizzle-orm';
2
+ const toRecord = (r) => ({
3
+ id: r.id,
4
+ organizationId: r.organizationId,
5
+ plugin: r.plugin,
6
+ collection: r.collection,
7
+ data: r.data,
8
+ createdAt: r.createdAt
9
+ });
10
+ /**
11
+ * Generic plugin storage on SQLite (libsql). Org isolation is WHERE-based (no RLS): every read
12
+ * filters by organization_id and every write carries it. Runs on whichever `db` it holds — the
13
+ * pooled db, or the transaction handle when the parent rebinds it inside `withTransaction`.
14
+ */
15
+ export class SQLitePluginStorageAdapter {
16
+ db;
17
+ tables;
18
+ constructor(db, tables) {
19
+ this.db = db;
20
+ this.tables = tables;
21
+ }
22
+ async createPluginRecord(input) {
23
+ const [row] = await this.db
24
+ .insert(this.tables.pluginStorage)
25
+ .values({
26
+ id: input.id,
27
+ organizationId: input.organizationId,
28
+ plugin: input.plugin,
29
+ collection: input.collection,
30
+ data: input.data
31
+ })
32
+ .returning();
33
+ if (!row)
34
+ throw new Error('createPluginRecord: insert returned no row');
35
+ return toRecord(row);
36
+ }
37
+ async getPluginRecord(organizationId, id) {
38
+ const [row] = await this.db
39
+ .select()
40
+ .from(this.tables.pluginStorage)
41
+ .where(and(eq(this.tables.pluginStorage.id, id), eq(this.tables.pluginStorage.organizationId, organizationId)))
42
+ .limit(1);
43
+ return row ? toRecord(row) : null;
44
+ }
45
+ async listPluginRecords(options) {
46
+ const { pluginStorage } = this.tables;
47
+ const limit = options.limit ?? 50;
48
+ const offset = options.offset ?? 0;
49
+ const conds = [
50
+ eq(pluginStorage.organizationId, options.organizationId),
51
+ eq(pluginStorage.plugin, options.plugin)
52
+ ];
53
+ if (options.collection)
54
+ conds.push(eq(pluginStorage.collection, options.collection));
55
+ const where = and(...conds);
56
+ const rows = await this.db
57
+ .select()
58
+ .from(pluginStorage)
59
+ .where(where)
60
+ .orderBy(desc(pluginStorage.createdAt))
61
+ .limit(limit)
62
+ .offset(offset);
63
+ const totals = await this.db.select({ value: count() }).from(pluginStorage).where(where);
64
+ return { items: rows.map(toRecord), total: Number(totals[0]?.value ?? 0), limit, offset };
65
+ }
66
+ }
67
+ //# sourceMappingURL=plugin-storage-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin-storage-adapter.js","sourceRoot":"","sources":["../src/plugin-storage-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAcnD,MAAM,QAAQ,GAAG,CAAC,CAAmB,EAAuB,EAAE,CAAC,CAAC;IAC/D,EAAE,EAAE,CAAC,CAAC,EAAE;IACR,cAAc,EAAE,CAAC,CAAC,cAAc;IAChC,MAAM,EAAE,CAAC,CAAC,MAAM;IAChB,UAAU,EAAE,CAAC,CAAC,UAAU;IACxB,IAAI,EAAE,CAAC,CAAC,IAAI;IACZ,SAAS,EAAE,CAAC,CAAC,SAAS;CACtB,CAAC,CAAC;AAEH;;;;GAIG;AACH,MAAM,OAAO,0BAA0B;IAE7B;IACA;IAFT,YACS,EAAM,EACN,MAAc;QADd,OAAE,GAAF,EAAE,CAAI;QACN,WAAM,GAAN,MAAM,CAAQ;IACpB,CAAC;IAEJ,KAAK,CAAC,kBAAkB,CAAC,KAA8B;QACtD,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,CAAC,EAAE;aACzB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC;aACjC,MAAM,CAAC;YACP,EAAE,EAAE,KAAK,CAAC,EAAE;YACZ,cAAc,EAAE,KAAK,CAAC,cAAc;YACpC,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,UAAU,EAAE,KAAK,CAAC,UAAU;YAC5B,IAAI,EAAE,KAAK,CAAC,IAAI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