@@ -27,23 +27,25 @@ export interface Collection<T extends Record<string, unknown>> {
2727 * - The previous document state (if trackPrevious is enabled)
2828 * - SQLite bookmark for PITR
2929 *
30+ * @param noun - Singular entity type name (e.g. 'contact', 'deal', 'invoice')
31+ *
3032 * @example
3133 * ```typescript
32- * const users = new CDCCollection(
33- * this.collection<User >('users '),
34+ * const contacts = new CDCCollection(
35+ * this.collection<Contact >('contacts '),
3436 * this.events,
35- * 'users'
37+ * 'contact' // singular noun — events become contact.created, contact.updated, etc.
3638 * )
3739 *
38- * // This emits a CDC event
39- * users .put('user -123', { name: 'Alice', active: true })
40+ * // This emits a CDC event: { type: 'cdc', event: 'contact.created', data: { type: 'contact', id, ... } }
41+ * contacts .put('contact -123', { name: 'Alice', active: true })
4042 * ```
4143 */
4244export class CDCCollection < T extends Record < string , unknown > > {
4345 constructor (
4446 private collection : Collection < T > ,
4547 private emitter : EventEmitter ,
46- private name : string
48+ private noun : string
4749 ) { }
4850
4951 /**
@@ -61,9 +63,9 @@ export class CDCCollection<T extends Record<string, unknown>> {
6163 this . collection . put ( id , doc )
6264
6365 if ( prev ) {
64- this . emitter . emitChange ( 'updated' , this . name , id , doc , prev )
66+ this . emitter . emitChange ( 'updated' , this . noun , id , doc , prev )
6567 } else {
66- this . emitter . emitChange ( 'created' , this . name , id , doc )
68+ this . emitter . emitChange ( 'created' , this . noun , id , doc )
6769 }
6870 }
6971
@@ -75,7 +77,7 @@ export class CDCCollection<T extends Record<string, unknown>> {
7577 const deleted = this . collection . delete ( id )
7678
7779 if ( deleted && prev ) {
78- this . emitter . emitChange ( 'deleted' , this . name , id , undefined , prev )
80+ this . emitter . emitChange ( 'deleted' , this . noun , id , undefined , prev )
7981 }
8082
8183 return deleted
@@ -125,7 +127,7 @@ export class CDCCollection<T extends Record<string, unknown>> {
125127
126128 // Emit delete for each document
127129 for ( const id of keys ) {
128- this . emitter . emitChange ( 'deleted' , this . name , id )
130+ this . emitter . emitChange ( 'deleted' , this . noun , id )
129131 }
130132
131133 return count
0 commit comments