@declaro/data 2.0.0-beta.137 → 2.0.0-beta.139

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.
@@ -7,7 +7,14 @@ import type {
7
7
  InferSummary,
8
8
  } from '../../shared/utils/schema-inference'
9
9
  import { ModelMutationAction, ModelQueryEvent } from '../events/event-types'
10
- import { MutationEvent } from '../events/mutation-event'
10
+ import {
11
+ MutationEvent,
12
+ type ICreateEventMeta,
13
+ type IUpdateEventMeta,
14
+ type IRemoveEventMeta,
15
+ type IRestoreEventMeta,
16
+ type IDuplicateEventMeta,
17
+ } from '../events/mutation-event'
11
18
  import type { IModelServiceArgs } from './model-service-args'
12
19
  import { ReadOnlyModelService, type ILoadOptions } from './read-only-model-service'
13
20
  import type { IActionOptions } from './base-model-service'
@@ -111,8 +118,36 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
111
118
  // Merge optional overrides
112
119
  const finalInput = overrides ? Object.assign({}, input, overrides) : input
113
120
 
114
- // Create the new record
115
- return this.create(finalInput as InferInput<TSchema>, options)
121
+ // Emit the before duplicate event
122
+ if (!options?.doNotDispatchEvents) {
123
+ const beforeDuplicateEvent = new MutationEvent<
124
+ InferDetail<TSchema>,
125
+ InferInput<TSchema>,
126
+ IDuplicateEventMeta<InferDetail<TSchema>, InferLookup<TSchema>, InferInput<TSchema>>
127
+ >(
128
+ this.getDescriptor(ModelMutationAction.BeforeDuplicate),
129
+ finalInput as InferInput<TSchema>,
130
+ ).setMeta({ existing, args: { lookup, overrides, options: options as Record<string, unknown> } })
131
+ await this.emitter.emitAsync(beforeDuplicateEvent)
132
+ }
133
+
134
+ // Create the new record (also emits beforeCreate/afterCreate)
135
+ const result = await this.create(finalInput as InferInput<TSchema>, options)
136
+
137
+ // Emit the after duplicate event
138
+ if (!options?.doNotDispatchEvents) {
139
+ const afterDuplicateEvent = new MutationEvent<
140
+ InferDetail<TSchema>,
141
+ InferInput<TSchema>,
142
+ IDuplicateEventMeta<InferDetail<TSchema>, InferLookup<TSchema>, InferInput<TSchema>>
143
+ >(
144
+ this.getDescriptor(ModelMutationAction.AfterDuplicate),
145
+ finalInput as InferInput<TSchema>,
146
+ ).setMeta({ existing, args: { lookup, overrides, options: options as Record<string, unknown> } }).setResult(result)
147
+ await this.emitter.emitAsync(afterDuplicateEvent)
148
+ }
149
+
150
+ return result
116
151
  }
117
152
 
118
153
  /**
@@ -122,20 +157,28 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
122
157
  */
123
158
  async remove(lookup: InferLookup<TSchema>, options?: ILoadOptions): Promise<InferSummary<TSchema>> {
124
159
  // Emit the before remove event
125
- const beforeRemoveEvent = new MutationEvent<InferSummary<TSchema>, InferLookup<TSchema>>(
160
+ const beforeRemoveEvent = new MutationEvent<
161
+ InferSummary<TSchema>,
162
+ InferLookup<TSchema>,
163
+ IRemoveEventMeta<InferSummary<TSchema>, InferLookup<TSchema>>
164
+ >(
126
165
  this.getDescriptor(ModelMutationAction.BeforeRemove),
127
166
  lookup,
128
- )
167
+ ).setMeta({ args: { lookup, options: options as Record<string, unknown> } })
129
168
  await this.emitter.emitAsync(beforeRemoveEvent)
130
169
 
131
170
  // Perform the removal
132
171
  const result = await this.repository.remove(lookup, options)
133
172
 
134
173
  // Emit the after remove event
135
- const afterRemoveEvent = new MutationEvent<InferSummary<TSchema>, InferLookup<TSchema>>(
174
+ const afterRemoveEvent = new MutationEvent<
175
+ InferSummary<TSchema>,
176
+ InferLookup<TSchema>,
177
+ IRemoveEventMeta<InferSummary<TSchema>, InferLookup<TSchema>>
178
+ >(
136
179
  this.getDescriptor(ModelMutationAction.AfterRemove),
137
180
  lookup,
138
- ).setResult(result)
181
+ ).setMeta({ args: { lookup, options: options as Record<string, unknown> } }).setResult(result)
139
182
  await this.emitter.emitAsync(afterRemoveEvent)
140
183
 
141
184
  // Return the results of the removal
@@ -150,20 +193,28 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
150
193
  */
151
194
  async restore(lookup: InferLookup<TSchema>, options?: ILoadOptions): Promise<InferSummary<TSchema>> {
152
195
  // Emit the before restore event
153
- const beforeRestoreEvent = new MutationEvent<InferSummary<TSchema>, InferLookup<TSchema>>(
196
+ const beforeRestoreEvent = new MutationEvent<
197
+ InferSummary<TSchema>,
198
+ InferLookup<TSchema>,
199
+ IRestoreEventMeta<InferSummary<TSchema>, InferLookup<TSchema>>
200
+ >(
154
201
  this.getDescriptor(ModelMutationAction.BeforeRestore),
155
202
  lookup,
156
- )
203
+ ).setMeta({ args: { lookup, options: options as Record<string, unknown> } })
157
204
  await this.emitter.emitAsync(beforeRestoreEvent)
158
205
 
159
206
  // Perform the restore operation
160
207
  const result = await this.repository.restore(lookup, options)
161
208
 
162
209
  // Emit the after restore event
163
- const afterRestoreEvent = new MutationEvent<InferSummary<TSchema>, InferLookup<TSchema>>(
210
+ const afterRestoreEvent = new MutationEvent<
211
+ InferSummary<TSchema>,
212
+ InferLookup<TSchema>,
213
+ IRestoreEventMeta<InferSummary<TSchema>, InferLookup<TSchema>>
214
+ >(
164
215
  this.getDescriptor(ModelMutationAction.AfterRestore),
165
216
  lookup,
166
- ).setResult(result)
217
+ ).setMeta({ args: { lookup, options: options as Record<string, unknown> } }).setResult(result)
167
218
  await this.emitter.emitAsync(afterRestoreEvent)
168
219
 
169
220
  // Return the results of the restore operation
@@ -178,10 +229,14 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
178
229
 
179
230
  // Emit the before create event
180
231
  if (!options?.doNotDispatchEvents) {
181
- const beforeCreateEvent = new MutationEvent<InferDetail<TSchema>, InferInput<TSchema>>(
232
+ const beforeCreateEvent = new MutationEvent<
233
+ InferDetail<TSchema>,
234
+ InferInput<TSchema>,
235
+ ICreateEventMeta<InferDetail<TSchema>, InferInput<TSchema>>
236
+ >(
182
237
  this.getDescriptor(ModelMutationAction.BeforeCreate),
183
238
  normalizedInput,
184
- )
239
+ ).setMeta({ args: { input: normalizedInput, options: options as Record<string, unknown> } })
185
240
  await this.emitter.emitAsync(beforeCreateEvent)
186
241
  }
187
242
 
@@ -190,10 +245,14 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
190
245
 
191
246
  // Emit the after create event
192
247
  if (!options?.doNotDispatchEvents) {
193
- const afterCreateEvent = new MutationEvent<InferDetail<TSchema>, InferInput<TSchema>>(
248
+ const afterCreateEvent = new MutationEvent<
249
+ InferDetail<TSchema>,
250
+ InferInput<TSchema>,
251
+ ICreateEventMeta<InferDetail<TSchema>, InferInput<TSchema>>
252
+ >(
194
253
  this.getDescriptor(ModelMutationAction.AfterCreate),
195
254
  normalizedInput,
196
- ).setResult(result)
255
+ ).setMeta({ args: { input: normalizedInput, options: options as Record<string, unknown> } }).setResult(result)
197
256
  await this.emitter.emitAsync(afterCreateEvent)
198
257
  }
199
258
 
@@ -215,10 +274,14 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
215
274
 
216
275
  // Emit the before update event
217
276
  if (!options?.doNotDispatchEvents) {
218
- const beforeUpdateEvent = new MutationEvent<InferDetail<TSchema>, InferInput<TSchema>>(
277
+ const beforeUpdateEvent = new MutationEvent<
278
+ InferDetail<TSchema>,
279
+ InferInput<TSchema>,
280
+ IUpdateEventMeta<InferDetail<TSchema>, InferLookup<TSchema>, InferInput<TSchema>>
281
+ >(
219
282
  this.getDescriptor(ModelMutationAction.BeforeUpdate),
220
283
  normalizedInput,
221
- )
284
+ ).setMeta({ existing, args: { lookup, input, options: options as Record<string, unknown> } })
222
285
  await this.emitter.emitAsync(beforeUpdateEvent)
223
286
  }
224
287
 
@@ -227,10 +290,14 @@ export class ModelService<TSchema extends AnyModelSchema> extends ReadOnlyModelS
227
290
 
228
291
  // Emit the after update event
229
292
  if (!options?.doNotDispatchEvents) {
230
- const afterUpdateEvent = new MutationEvent<InferDetail<TSchema>, InferInput<TSchema>>(
293
+ const afterUpdateEvent = new MutationEvent<
294
+ InferDetail<TSchema>,
295
+ InferInput<TSchema>,
296
+ IUpdateEventMeta<InferDetail<TSchema>, InferLookup<TSchema>, InferInput<TSchema>>
297
+ >(
231
298
  this.getDescriptor(ModelMutationAction.AfterUpdate),
232
299
  normalizedInput,
233
- ).setResult(result)
300
+ ).setMeta({ existing, args: { lookup, input, options: options as Record<string, unknown> } }).setResult(result)
234
301
  await this.emitter.emitAsync(afterUpdateEvent)
235
302
  }
236
303
 
@@ -4,7 +4,13 @@ import { MockMemoryRepository } from '../../test/mock/repositories/mock-memory-r
4
4
  import { MockBookSchema, type MockBookDetail, type MockBookLookup } from '../../test/mock/models/mock-book-models'
5
5
  import { EventManager } from '@declaro/core'
6
6
  import type { QueryEvent } from '../events/query-event'
7
- import type { InferDetail, InferFilters, InferLookup, InferSearchResults } from '../../shared/utils/schema-inference'
7
+ import type {
8
+ InferDetail,
9
+ InferFilters,
10
+ InferLookup,
11
+ InferSearchResults,
12
+ InferSummary,
13
+ } from '../../shared/utils/schema-inference'
8
14
 
9
15
  describe('ReadOnlyModelService', () => {
10
16
  const namespace = 'books'
@@ -348,7 +354,7 @@ describe('ReadOnlyModelService', () => {
348
354
  return detail
349
355
  }
350
356
 
351
- async normalizeSummary(summary: InferDetail<typeof mockSchema>): Promise<InferDetail<typeof mockSchema>> {
357
+ async normalizeSummary(summary: InferSummary<typeof mockSchema>): Promise<InferSummary<typeof mockSchema>> {
352
358
  // Handle null case (e.g., when load returns null)
353
359
  if (!summary) return summary
354
360
 
@@ -5,6 +5,7 @@ import type {
5
5
  InferLookup,
6
6
  InferSearchResults,
7
7
  InferSort,
8
+ InferSummary,
8
9
  } from '../../shared/utils/schema-inference'
9
10
  import { ModelQueryEvent } from '../events/event-types'
10
11
  import { QueryEvent } from '../events/query-event'
@@ -66,7 +67,7 @@ export class ReadOnlyModelService<TSchema extends AnyModelSchema> extends BaseMo
66
67
  * @param summary The summary data to normalize.
67
68
  * @returns The normalized summary data.
68
69
  */
69
- async normalizeSummary(summary: InferDetail<TSchema>): Promise<InferDetail<TSchema>> {
70
+ async normalizeSummary(summary: InferSummary<TSchema>): Promise<InferSummary<TSchema>> {
70
71
  return summary
71
72
  }
72
73
 
@@ -166,7 +167,7 @@ export class ReadOnlyModelService<TSchema extends AnyModelSchema> extends BaseMo
166
167
  // Return the search results
167
168
  return {
168
169
  ...results,
169
- results: await Promise.all(results.results.map((detail) => this.normalizeSummary(detail))),
170
+ results: await Promise.all(results.results.map((summary) => this.normalizeSummary(summary))),
170
171
  }
171
172
  }
172
173