sqlite-zod-orm 3.6.0 → 3.6.1
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.js +21 -21
- package/package.json +1 -1
- package/src/database.ts +36 -38
- package/src/index.ts +1 -1
- package/src/types.ts +10 -12
package/dist/index.js
CHANGED
|
@@ -4717,9 +4717,9 @@ class _Database {
|
|
|
4717
4717
|
on: (event, callback, options2) => {
|
|
4718
4718
|
if (event === "insert")
|
|
4719
4719
|
return this._createOnStream(entityName, callback, options2?.interval);
|
|
4720
|
-
if (event === "
|
|
4721
|
-
return this._createChangeStream(entityName, callback, options2?.interval);
|
|
4722
|
-
throw new Error(`Unknown event type: '${event}'. Supported: 'insert', '
|
|
4720
|
+
if (event === "update" || event === "delete")
|
|
4721
|
+
return this._createChangeStream(entityName, event, callback, options2?.interval);
|
|
4722
|
+
throw new Error(`Unknown event type: '${event}'. Supported: 'insert', 'update', 'delete'`);
|
|
4723
4723
|
},
|
|
4724
4724
|
_tableName: entityName
|
|
4725
4725
|
};
|
|
@@ -4799,7 +4799,7 @@ class _Database {
|
|
|
4799
4799
|
stopped = true;
|
|
4800
4800
|
};
|
|
4801
4801
|
}
|
|
4802
|
-
_createChangeStream(entityName, callback, intervalOverride) {
|
|
4802
|
+
_createChangeStream(entityName, eventType, callback, intervalOverride) {
|
|
4803
4803
|
const interval = intervalOverride ?? this.pollInterval;
|
|
4804
4804
|
const allRows = this.db.query(`SELECT * FROM "${entityName}" ORDER BY id ASC`).all();
|
|
4805
4805
|
let snapshot = new Map;
|
|
@@ -4823,24 +4823,24 @@ class _Database {
|
|
|
4823
4823
|
currentMap.set(row.id, JSON.stringify(row));
|
|
4824
4824
|
currentEntities.set(row.id, row);
|
|
4825
4825
|
}
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
|
|
4832
|
-
|
|
4833
|
-
|
|
4834
|
-
|
|
4835
|
-
await callback({ type: "update", row: entity, oldRow: oldEntity });
|
|
4826
|
+
if (eventType === "update") {
|
|
4827
|
+
for (const [id, json] of currentMap) {
|
|
4828
|
+
if (stopped)
|
|
4829
|
+
return;
|
|
4830
|
+
if (snapshot.has(id) && snapshot.get(id) !== json) {
|
|
4831
|
+
const entity = this._attachMethods(entityName, transformFromStorage(currentEntities.get(id), this.schemas[entityName]));
|
|
4832
|
+
const oldEntity = this._attachMethods(entityName, transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]));
|
|
4833
|
+
await callback(entity, oldEntity);
|
|
4834
|
+
}
|
|
4836
4835
|
}
|
|
4837
|
-
}
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4836
|
+
} else if (eventType === "delete") {
|
|
4837
|
+
for (const [id] of snapshot) {
|
|
4838
|
+
if (stopped)
|
|
4839
|
+
return;
|
|
4840
|
+
if (!currentMap.has(id)) {
|
|
4841
|
+
const oldEntity = this._attachMethods(entityName, transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]));
|
|
4842
|
+
await callback(oldEntity);
|
|
4843
|
+
}
|
|
4844
4844
|
}
|
|
4845
4845
|
}
|
|
4846
4846
|
snapshot = currentMap;
|
package/package.json
CHANGED
package/src/database.ts
CHANGED
|
@@ -59,10 +59,10 @@ class _Database<Schemas extends SchemaMap> {
|
|
|
59
59
|
upsert: (conditions, data) => this.upsert(entityName, data, conditions),
|
|
60
60
|
delete: (id) => this.delete(entityName, id),
|
|
61
61
|
select: (...cols: string[]) => this._createQueryBuilder(entityName, cols),
|
|
62
|
-
on: (event: string, callback: (
|
|
62
|
+
on: (event: string, callback: (...args: any[]) => void | Promise<void>, options?: { interval?: number }) => {
|
|
63
63
|
if (event === 'insert') return this._createOnStream(entityName, callback, options?.interval);
|
|
64
|
-
if (event === '
|
|
65
|
-
throw new Error(`Unknown event type: '${event}'. Supported: 'insert', '
|
|
64
|
+
if (event === 'update' || event === 'delete') return this._createChangeStream(entityName, event, callback, options?.interval);
|
|
65
|
+
throw new Error(`Unknown event type: '${event}'. Supported: 'insert', 'update', 'delete'`);
|
|
66
66
|
},
|
|
67
67
|
_tableName: entityName,
|
|
68
68
|
};
|
|
@@ -205,14 +205,18 @@ class _Database<Schemas extends SchemaMap> {
|
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
|
-
* Stream
|
|
208
|
+
* Stream specific mutations (update or delete) with natural callback signatures.
|
|
209
209
|
*
|
|
210
210
|
* Maintains a full snapshot and diffs on each poll.
|
|
211
|
-
*
|
|
211
|
+
* Only fires for the subscribed event type.
|
|
212
|
+
*
|
|
213
|
+
* - 'update': callback(row, oldRow)
|
|
214
|
+
* - 'delete': callback(row)
|
|
212
215
|
*/
|
|
213
216
|
public _createChangeStream(
|
|
214
217
|
entityName: string,
|
|
215
|
-
|
|
218
|
+
eventType: 'update' | 'delete',
|
|
219
|
+
callback: (...args: any[]) => void | Promise<void>,
|
|
216
220
|
intervalOverride?: number,
|
|
217
221
|
): () => void {
|
|
218
222
|
const interval = intervalOverride ?? this.pollInterval;
|
|
@@ -244,39 +248,33 @@ class _Database<Schemas extends SchemaMap> {
|
|
|
244
248
|
currentEntities.set(row.id, row);
|
|
245
249
|
}
|
|
246
250
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
);
|
|
263
|
-
const oldEntity = this._attachMethods(
|
|
264
|
-
entityName,
|
|
265
|
-
transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]!)
|
|
266
|
-
);
|
|
267
|
-
await callback({ type: 'update', row: entity, oldRow: oldEntity });
|
|
251
|
+
if (eventType === 'update') {
|
|
252
|
+
// Detect updates: existing rows whose JSON changed
|
|
253
|
+
for (const [id, json] of currentMap) {
|
|
254
|
+
if (stopped) return;
|
|
255
|
+
if (snapshot.has(id) && snapshot.get(id) !== json) {
|
|
256
|
+
const entity = this._attachMethods(
|
|
257
|
+
entityName,
|
|
258
|
+
transformFromStorage(currentEntities.get(id), this.schemas[entityName]!)
|
|
259
|
+
);
|
|
260
|
+
const oldEntity = this._attachMethods(
|
|
261
|
+
entityName,
|
|
262
|
+
transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]!)
|
|
263
|
+
);
|
|
264
|
+
await callback(entity, oldEntity);
|
|
265
|
+
}
|
|
268
266
|
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
267
|
+
} else if (eventType === 'delete') {
|
|
268
|
+
// Detect deletes: rows in snapshot but not in current
|
|
269
|
+
for (const [id] of snapshot) {
|
|
270
|
+
if (stopped) return;
|
|
271
|
+
if (!currentMap.has(id)) {
|
|
272
|
+
const oldEntity = this._attachMethods(
|
|
273
|
+
entityName,
|
|
274
|
+
transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]!)
|
|
275
|
+
);
|
|
276
|
+
await callback(oldEntity);
|
|
277
|
+
}
|
|
280
278
|
}
|
|
281
279
|
}
|
|
282
280
|
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { Database } from './database';
|
|
|
7
7
|
export type { DatabaseType } from './database';
|
|
8
8
|
|
|
9
9
|
export type {
|
|
10
|
-
SchemaMap, DatabaseOptions, Relationship,
|
|
10
|
+
SchemaMap, DatabaseOptions, Relationship,
|
|
11
11
|
EntityAccessor, TypedAccessors, AugmentedEntity, UpdateBuilder,
|
|
12
12
|
InferSchema, EntityData, IndexDef,
|
|
13
13
|
ProxyColumns, ColumnRef,
|
package/src/types.ts
CHANGED
|
@@ -45,12 +45,6 @@ export type Relationship = {
|
|
|
45
45
|
foreignKey: string;
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
/** Change event emitted by .on('change', callback) */
|
|
49
|
-
export type ChangeEvent<T = any> = {
|
|
50
|
-
type: 'insert' | 'update' | 'delete';
|
|
51
|
-
row: T;
|
|
52
|
-
oldRow?: T; // present on 'update' and 'delete'
|
|
53
|
-
};
|
|
54
48
|
|
|
55
49
|
// =============================================================================
|
|
56
50
|
// Type Helpers
|
|
@@ -158,15 +152,17 @@ export type NavEntityAccessor<
|
|
|
158
152
|
/**
|
|
159
153
|
* Listen for table events.
|
|
160
154
|
*
|
|
161
|
-
* `'insert'` — streams new rows one at a time
|
|
162
|
-
* `'
|
|
155
|
+
* `'insert'` — streams new rows, one at a time.
|
|
156
|
+
* `'update'` — fires on row changes with (newRow, oldRow).
|
|
157
|
+
* `'delete'` — fires when a row is removed.
|
|
163
158
|
*
|
|
164
159
|
* Callbacks are awaited — strict ordering guaranteed even with async handlers.
|
|
165
160
|
* @returns Unsubscribe function.
|
|
166
161
|
*/
|
|
167
162
|
on: {
|
|
168
163
|
(event: 'insert', callback: (row: NavEntity<S, R, Table>) => void | Promise<void>, options?: { interval?: number }): () => void;
|
|
169
|
-
(event: '
|
|
164
|
+
(event: 'update', callback: (row: NavEntity<S, R, Table>, oldRow: NavEntity<S, R, Table>) => void | Promise<void>, options?: { interval?: number }): () => void;
|
|
165
|
+
(event: 'delete', callback: (row: NavEntity<S, R, Table>) => void | Promise<void>, options?: { interval?: number }): () => void;
|
|
170
166
|
};
|
|
171
167
|
_tableName: string;
|
|
172
168
|
readonly _schema?: S[Table & keyof S];
|
|
@@ -196,15 +192,17 @@ export type EntityAccessor<S extends z.ZodType<any>> = {
|
|
|
196
192
|
/**
|
|
197
193
|
* Listen for table events.
|
|
198
194
|
*
|
|
199
|
-
* `'insert'` — streams new rows one at a time
|
|
200
|
-
* `'
|
|
195
|
+
* `'insert'` — streams new rows, one at a time.
|
|
196
|
+
* `'update'` — fires on row changes with (newRow, oldRow).
|
|
197
|
+
* `'delete'` — fires when a row is removed.
|
|
201
198
|
*
|
|
202
199
|
* Callbacks are awaited — strict ordering guaranteed even with async handlers.
|
|
203
200
|
* @returns Unsubscribe function.
|
|
204
201
|
*/
|
|
205
202
|
on: {
|
|
206
203
|
(event: 'insert', callback: (row: AugmentedEntity<S>) => void | Promise<void>, options?: { interval?: number }): () => void;
|
|
207
|
-
(event: '
|
|
204
|
+
(event: 'update', callback: (row: AugmentedEntity<S>, oldRow: AugmentedEntity<S>) => void | Promise<void>, options?: { interval?: number }): () => void;
|
|
205
|
+
(event: 'delete', callback: (row: AugmentedEntity<S>) => void | Promise<void>, options?: { interval?: number }): () => void;
|
|
208
206
|
};
|
|
209
207
|
_tableName: string;
|
|
210
208
|
readonly _schema?: S;
|