@haverstack/record-adapter-sqlite 0.21.0 → 0.23.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.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,115 @@
1
- import { TokenSession, StackRecord, RecordChanges, RecordVersion, ActorOptions, TypeId, StackQuery, QueryResult, FileId, StackType, Association } from '@haverstack/core';
1
+ import { StackRecord, RecordChanges, RecordVersion, ActorOptions, TypeId, StackQuery, QueryResult, FileId, StackType, Association, TokenSession } from '@haverstack/core';
2
2
  import { StackRecordAdapter, AdapterCapabilities } from '@haverstack/core/adapter';
3
3
  import { StackTokenStore, TokenInfo } from '@haverstack/core/wire';
4
4
 
5
+ /**
6
+ * Normalizes a SQLite binding's call convention (array-bind-then-step,
7
+ * spread-args run/get/all, cursor-returning exec) behind one interface,
8
+ * so the actual CRUD/query/version/type/association/token logic can live
9
+ * once in this package instead of being re-typed per engine. Each adapter
10
+ * package provides a thin SqlExecutor implementation over its own
11
+ * database handle.
12
+ *
13
+ * Deliberately synchronous: every binding this targets executes queries
14
+ * in-process without yielding, and an async interface would force the
15
+ * shared logic's transaction sequences to interleave.
16
+ */
17
+ interface SqlExecutor {
18
+ /** Run DDL, pragmas, or any statement(s) with no bound params and no results needed. */
19
+ exec(sql: string): void;
20
+ /** Run a single parameterized statement. Returns the number of rows affected (INSERT/UPDATE/DELETE). */
21
+ run(sql: string, params?: readonly unknown[]): number;
22
+ /** Run a parameterized statement and return the first result row, or undefined. */
23
+ get<T = Record<string, unknown>>(sql: string, params?: readonly unknown[]): T | undefined;
24
+ /** Run a parameterized statement and return all result rows. */
25
+ all<T = Record<string, unknown>>(sql: string, params?: readonly unknown[]): T[];
26
+ /**
27
+ * Run `fn` atomically: every statement it issues commits together, or
28
+ * none do if it throws. `fn` must be synchronous and call back into
29
+ * this executor only — not every engine's transaction primitive can
30
+ * straddle a suspended call. A binding with a real multi-statement
31
+ * transaction (BEGIN/COMMIT/ROLLBACK) implements this with one; a
32
+ * binding whose transaction primitive is itself a callback wrapper
33
+ * (e.g. a Durable Object's storage.transactionSync) can pass `fn`
34
+ * straight through to it.
35
+ */
36
+ transaction<T>(fn: () => T): T;
37
+ }
38
+
39
+ /** timezone is optional passthrough app metadata — no default. */
40
+ type StackConfig = {
41
+ entityId: string;
42
+ timezone: string | undefined;
43
+ };
44
+
45
+ /**
46
+ * The engine-independent half of a SQLite-backed StackRecordAdapter: the
47
+ * capability declaration and the delegation of all seventeen
48
+ * StackRecordAdapter methods onto SharedSqlRecordLogic. Every method here
49
+ * is a forward with no engine-specific content, so an adapter that owns a
50
+ * SqlExecutor already knows how to answer all of them.
51
+ *
52
+ * What a concrete adapter is left to supply is exactly what differs
53
+ * between bindings: constructing the database handle, applying the
54
+ * schema, and its own lifecycle (flush/close) — hence those two being the
55
+ * only abstract members.
56
+ *
57
+ * Lives in record.js's barrel rather than index.js's, so an adapter that
58
+ * imports it never pulls in token-logic.ts's `node:crypto` — see the note
59
+ * at the top of record.ts.
60
+ */
61
+
62
+ declare abstract class SharedSqlRecordAdapter implements StackRecordAdapter {
63
+ readonly capabilities: AdapterCapabilities;
64
+ readonly ownerEntityId: string;
65
+ readonly timezone: string | undefined;
66
+ private readonly record;
67
+ protected constructor(exec: SqlExecutor, config: StackConfig);
68
+ createRecord(record: StackRecord): Promise<StackRecord>;
69
+ getRecord(id: string): Promise<StackRecord | null>;
70
+ mutateRecord(id: string, changes: RecordChanges, opts?: {
71
+ expectedVersion?: number;
72
+ snapshot?: RecordVersion;
73
+ } & ActorOptions): Promise<StackRecord>;
74
+ deleteRecord(id: string, opts?: {
75
+ hard?: boolean;
76
+ expectedVersion?: number;
77
+ snapshot?: RecordVersion;
78
+ } & ActorOptions): Promise<StackRecord | null>;
79
+ undeleteRecord(id: string, opts?: {
80
+ expectedVersion?: number;
81
+ snapshot?: RecordVersion;
82
+ } & ActorOptions): Promise<StackRecord>;
83
+ restoreVersion(id: string, version: number, opts?: {
84
+ expectedVersion?: number;
85
+ snapshot?: RecordVersion;
86
+ } & {
87
+ restoreAssociations?: boolean;
88
+ } & ActorOptions): Promise<StackRecord>;
89
+ commitMigration(id: string, toTypeId: TypeId, content: Record<string, unknown>, opts?: {
90
+ expectedVersion?: number;
91
+ snapshot?: RecordVersion;
92
+ } & ActorOptions): Promise<StackRecord>;
93
+ queryRecords(query: StackQuery): Promise<QueryResult>;
94
+ deleteUnreferencedAttachmentRecords(fileId: FileId, metadataTypeIds: TypeId[]): Promise<StackRecord[]>;
95
+ getVersions(id: string): Promise<RecordVersion[]>;
96
+ getVersion(id: string, version: number): Promise<RecordVersion | null>;
97
+ saveVersion(id: string, version: RecordVersion): Promise<void>;
98
+ saveType(type: StackType): Promise<void>;
99
+ getType(id: TypeId): Promise<StackType | null>;
100
+ listTypes(): Promise<StackType[]>;
101
+ associate(recordId: string, association: Association, opts?: {
102
+ expectedVersion?: number;
103
+ snapshot?: RecordVersion;
104
+ } & ActorOptions): Promise<StackRecord>;
105
+ dissociate(recordId: string, association: Association, opts?: {
106
+ expectedVersion?: number;
107
+ snapshot?: RecordVersion;
108
+ } & ActorOptions): Promise<StackRecord>;
109
+ abstract flush(): Promise<void>;
110
+ abstract close(): Promise<void>;
111
+ }
112
+
5
113
  /**
6
114
  * Bearer-token storage backing @haverstack/core's StackTokenStore,
7
115
  * implemented against its own file — deliberately separate from
@@ -64,12 +172,12 @@ declare class NativeTokenStore implements StackTokenStore {
64
172
  * package, which implements @haverstack/core's StackTokenStore against
65
173
  * its own file rather than this adapter's records database.
66
174
  *
67
- * This class itself is a thin node:sqlite binding: schema setup,
68
- * pragmas, and the storage-ownership lock are genuinely engine-specific
69
- * and live here. The actual StackRecordAdapter logic lives in
70
- * @haverstack/sqlite-shared's SharedSqlRecordLogic, reached through the
71
- * SqlExecutor interface (NativeSqliteExecutor here normalizes
72
- * node:sqlite's spread-args run/get/all calls to it).
175
+ * This class itself is a thin node:sqlite binding: opening the file, the
176
+ * storage-ownership lock, and the WAL checkpoint are what's genuinely
177
+ * engine-specific and live here. Everything above storage comes from
178
+ * @haverstack/sqlite-shared's SharedSqlRecordAdapter, reached through the
179
+ * SqlExecutor interface (NativeSqliteExecutor here normalizes node:sqlite's
180
+ * spread-args run/get/all calls to it).
73
181
  */
74
182
 
75
183
  type NativeRecordInitializeOptions = {
@@ -93,15 +201,17 @@ type NativeRecordOpenOptions = {
93
201
  */
94
202
  force?: boolean;
95
203
  };
96
- declare class NativeSQLiteRecordAdapter implements StackRecordAdapter {
204
+ declare class NativeSQLiteRecordAdapter extends SharedSqlRecordAdapter {
97
205
  private readonly path;
98
- readonly capabilities: AdapterCapabilities;
99
- ownerEntityId: string;
100
- timezone: string | undefined;
101
- private db;
102
- private record;
206
+ private readonly db;
103
207
  private constructor();
104
- private wire;
208
+ /**
209
+ * Takes the storage-ownership lock, opens the file, and brings the
210
+ * schema up. Shared by initialize() and open(), which differ only in
211
+ * whether the file may already exist and in where the config record
212
+ * comes from.
213
+ */
214
+ private static attach;
105
215
  /**
106
216
  * Initialize a new stack database. Fails if the file already exists —
107
217
  * use open() for existing databases.
@@ -112,47 +222,6 @@ declare class NativeSQLiteRecordAdapter implements StackRecordAdapter {
112
222
  * use initialize() for new databases.
113
223
  */
114
224
  static open(opts: NativeRecordOpenOptions): Promise<NativeSQLiteRecordAdapter>;
115
- createRecord(record: StackRecord): Promise<StackRecord>;
116
- getRecord(id: string): Promise<StackRecord | null>;
117
- mutateRecord(id: string, changes: RecordChanges, opts?: {
118
- expectedVersion?: number;
119
- snapshot?: RecordVersion;
120
- } & ActorOptions): Promise<StackRecord>;
121
- deleteRecord(id: string, opts?: {
122
- hard?: boolean;
123
- expectedVersion?: number;
124
- snapshot?: RecordVersion;
125
- } & ActorOptions): Promise<StackRecord | null>;
126
- undeleteRecord(id: string, opts?: {
127
- expectedVersion?: number;
128
- snapshot?: RecordVersion;
129
- } & ActorOptions): Promise<StackRecord>;
130
- restoreVersion(id: string, version: number, opts?: {
131
- expectedVersion?: number;
132
- snapshot?: RecordVersion;
133
- } & {
134
- restoreAssociations?: boolean;
135
- } & ActorOptions): Promise<StackRecord>;
136
- commitMigration(id: string, toTypeId: TypeId, content: Record<string, unknown>, opts?: {
137
- expectedVersion?: number;
138
- snapshot?: RecordVersion;
139
- } & ActorOptions): Promise<StackRecord>;
140
- queryRecords(query: StackQuery): Promise<QueryResult>;
141
- deleteUnreferencedAttachmentRecords(fileId: FileId, metadataTypeIds: TypeId[]): Promise<StackRecord[]>;
142
- getVersions(id: string): Promise<RecordVersion[]>;
143
- getVersion(id: string, version: number): Promise<RecordVersion | null>;
144
- saveVersion(id: string, version: RecordVersion): Promise<void>;
145
- saveType(type: StackType): Promise<void>;
146
- getType(id: TypeId): Promise<StackType | null>;
147
- listTypes(): Promise<StackType[]>;
148
- associate(recordId: string, association: Association, opts?: {
149
- expectedVersion?: number;
150
- snapshot?: RecordVersion;
151
- } & ActorOptions): Promise<StackRecord>;
152
- dissociate(recordId: string, association: Association, opts?: {
153
- expectedVersion?: number;
154
- snapshot?: RecordVersion;
155
- } & ActorOptions): Promise<StackRecord>;
156
225
  /** Folds the WAL back into the main file — useful before copying/backing up the database. */
157
226
  flush(): Promise<void>;
158
227
  close(): Promise<void>;