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 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 === "change")
4721
- return this._createChangeStream(entityName, callback, options2?.interval);
4722
- throw new Error(`Unknown event type: '${event}'. Supported: 'insert', 'change'`);
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
- for (const [id, json] of currentMap) {
4827
- if (stopped)
4828
- return;
4829
- if (!snapshot.has(id)) {
4830
- const entity = this._attachMethods(entityName, transformFromStorage(currentEntities.get(id), this.schemas[entityName]));
4831
- await callback({ type: "insert", row: entity });
4832
- } else if (snapshot.get(id) !== json) {
4833
- const entity = this._attachMethods(entityName, transformFromStorage(currentEntities.get(id), this.schemas[entityName]));
4834
- const oldEntity = this._attachMethods(entityName, transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]));
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
- for (const [id] of snapshot) {
4839
- if (stopped)
4840
- return;
4841
- if (!currentMap.has(id)) {
4842
- const oldEntity = this._attachMethods(entityName, transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]));
4843
- await callback({ type: "delete", row: oldEntity, oldRow: oldEntity });
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqlite-zod-orm",
3
- "version": "3.6.0",
3
+ "version": "3.6.1",
4
4
  "description": "Type-safe SQLite ORM for Bun — Zod schemas, fluent queries, auto relationships, zero SQL",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
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: (row: any) => void | Promise<void>, options?: { interval?: number }) => {
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 === 'change') return this._createChangeStream(entityName, callback, options?.interval);
65
- throw new Error(`Unknown event type: '${event}'. Supported: 'insert', 'change'`);
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 all mutations (insert / update / delete) as ChangeEvent objects.
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
- * Heavier than _createOnStream (which only tracks watermark), but catches all changes.
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
- callback: (event: any) => void | Promise<void>,
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
- // Detect inserts and updates
248
- for (const [id, json] of currentMap) {
249
- if (stopped) return;
250
- if (!snapshot.has(id)) {
251
- // INSERT
252
- const entity = this._attachMethods(
253
- entityName,
254
- transformFromStorage(currentEntities.get(id), this.schemas[entityName]!)
255
- );
256
- await callback({ type: 'insert', row: entity });
257
- } else if (snapshot.get(id) !== json) {
258
- // UPDATE
259
- const entity = this._attachMethods(
260
- entityName,
261
- transformFromStorage(currentEntities.get(id), this.schemas[entityName]!)
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
- // Detect deletes
272
- for (const [id] of snapshot) {
273
- if (stopped) return;
274
- if (!currentMap.has(id)) {
275
- const oldEntity = this._attachMethods(
276
- entityName,
277
- transformFromStorage(snapshotEntities.get(id), this.schemas[entityName]!)
278
- );
279
- await callback({ type: 'delete', row: oldEntity, oldRow: oldEntity });
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, ChangeEvent,
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, in insertion order.
162
- * `'change'` — streams all mutations: { type: 'insert'|'update'|'delete', row, oldRow? }.
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: 'change', callback: (event: ChangeEvent<NavEntity<S, R, Table>>) => void | Promise<void>, options?: { interval?: number }): () => void;
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, in insertion order.
200
- * `'change'` — streams all mutations: { type: 'insert'|'update'|'delete', row, oldRow? }.
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: 'change', callback: (event: ChangeEvent<AugmentedEntity<S>>) => void | Promise<void>, options?: { interval?: number }): () => void;
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;