@declaro/data 2.0.0-beta.14 → 2.0.0-beta.140

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.
Files changed (142) hide show
  1. package/{LICENSE → LICENSE.md} +1 -1
  2. package/README.md +0 -0
  3. package/dist/browser/index.js +26 -0
  4. package/dist/browser/index.js.map +93 -0
  5. package/dist/node/index.cjs +13372 -0
  6. package/dist/node/index.cjs.map +93 -0
  7. package/dist/node/index.js +13351 -0
  8. package/dist/node/index.js.map +93 -0
  9. package/dist/ts/application/model-controller.d.ts +60 -0
  10. package/dist/ts/application/model-controller.d.ts.map +1 -0
  11. package/dist/ts/application/model-controller.test.d.ts +2 -0
  12. package/dist/ts/application/model-controller.test.d.ts.map +1 -0
  13. package/dist/ts/application/read-only-model-controller.d.ts +24 -0
  14. package/dist/ts/application/read-only-model-controller.d.ts.map +1 -0
  15. package/dist/ts/application/read-only-model-controller.test.d.ts +2 -0
  16. package/dist/ts/application/read-only-model-controller.test.d.ts.map +1 -0
  17. package/dist/ts/domain/events/domain-event.d.ts +41 -0
  18. package/dist/ts/domain/events/domain-event.d.ts.map +1 -0
  19. package/dist/ts/domain/events/domain-event.test.d.ts +2 -0
  20. package/dist/ts/domain/events/domain-event.test.d.ts.map +1 -0
  21. package/dist/ts/domain/events/event-types.d.ts +37 -0
  22. package/dist/ts/domain/events/event-types.d.ts.map +1 -0
  23. package/dist/ts/domain/events/mutation-event.d.ts +41 -0
  24. package/dist/ts/domain/events/mutation-event.d.ts.map +1 -0
  25. package/dist/ts/domain/events/mutation-event.test.d.ts +2 -0
  26. package/dist/ts/domain/events/mutation-event.test.d.ts.map +1 -0
  27. package/dist/ts/domain/events/query-event.d.ts +8 -0
  28. package/dist/ts/domain/events/query-event.d.ts.map +1 -0
  29. package/dist/ts/domain/events/query-event.test.d.ts +2 -0
  30. package/dist/ts/domain/events/query-event.test.d.ts.map +1 -0
  31. package/dist/ts/domain/events/request-event.d.ts +26 -0
  32. package/dist/ts/domain/events/request-event.d.ts.map +1 -0
  33. package/dist/ts/domain/events/request-event.test.d.ts +2 -0
  34. package/dist/ts/domain/events/request-event.test.d.ts.map +1 -0
  35. package/dist/ts/domain/interfaces/repository.d.ts +110 -0
  36. package/dist/ts/domain/interfaces/repository.d.ts.map +1 -0
  37. package/dist/ts/domain/models/pagination.d.ts +28 -0
  38. package/dist/ts/domain/models/pagination.d.ts.map +1 -0
  39. package/dist/ts/domain/services/base-model-service.d.ts +23 -0
  40. package/dist/ts/domain/services/base-model-service.d.ts.map +1 -0
  41. package/dist/ts/domain/services/model-service-args.d.ts +9 -0
  42. package/dist/ts/domain/services/model-service-args.d.ts.map +1 -0
  43. package/dist/ts/domain/services/model-service.d.ts +99 -0
  44. package/dist/ts/domain/services/model-service.d.ts.map +1 -0
  45. package/dist/ts/domain/services/model-service.normalization.test.d.ts +2 -0
  46. package/dist/ts/domain/services/model-service.normalization.test.d.ts.map +1 -0
  47. package/dist/ts/domain/services/model-service.test.d.ts +2 -0
  48. package/dist/ts/domain/services/model-service.test.d.ts.map +1 -0
  49. package/dist/ts/domain/services/read-only-model-service.d.ts +90 -0
  50. package/dist/ts/domain/services/read-only-model-service.d.ts.map +1 -0
  51. package/dist/ts/domain/services/read-only-model-service.test.d.ts +2 -0
  52. package/dist/ts/domain/services/read-only-model-service.test.d.ts.map +1 -0
  53. package/dist/ts/index.d.ts +18 -0
  54. package/dist/ts/index.d.ts.map +1 -0
  55. package/dist/ts/shared/utils/schema-inference.d.ts +23 -0
  56. package/dist/ts/shared/utils/schema-inference.d.ts.map +1 -0
  57. package/dist/ts/shared/utils/schema-inheritance.d.ts +24 -0
  58. package/dist/ts/shared/utils/schema-inheritance.d.ts.map +1 -0
  59. package/dist/ts/shared/utils/schema-inheritance.test.d.ts +2 -0
  60. package/dist/ts/shared/utils/schema-inheritance.test.d.ts.map +1 -0
  61. package/dist/ts/shared/utils/test/animal-schema.d.ts +57 -0
  62. package/dist/ts/shared/utils/test/animal-schema.d.ts.map +1 -0
  63. package/dist/ts/shared/utils/test/animal-trait-schema.d.ts +55 -0
  64. package/dist/ts/shared/utils/test/animal-trait-schema.d.ts.map +1 -0
  65. package/dist/ts/shared/utils/test/elephant-schema.d.ts +30 -0
  66. package/dist/ts/shared/utils/test/elephant-schema.d.ts.map +1 -0
  67. package/dist/ts/shared/utils/test/elephant-trait-schema.d.ts +26 -0
  68. package/dist/ts/shared/utils/test/elephant-trait-schema.d.ts.map +1 -0
  69. package/dist/ts/test/mock/models/mock-book-models.d.ts +42 -0
  70. package/dist/ts/test/mock/models/mock-book-models.d.ts.map +1 -0
  71. package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts +2 -0
  72. package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts.map +1 -0
  73. package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts +2 -0
  74. package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts.map +1 -0
  75. package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts +2 -0
  76. package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts.map +1 -0
  77. package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts +2 -0
  78. package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts.map +1 -0
  79. package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts +62 -0
  80. package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts.map +1 -0
  81. package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts +2 -0
  82. package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts.map +1 -0
  83. package/dist/ts/test/mock/repositories/mock-memory-repository.trash.test.d.ts +2 -0
  84. package/dist/ts/test/mock/repositories/mock-memory-repository.trash.test.d.ts.map +1 -0
  85. package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts +2 -0
  86. package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts.map +1 -0
  87. package/package.json +46 -42
  88. package/src/application/model-controller.test.ts +694 -0
  89. package/src/application/model-controller.ts +186 -0
  90. package/src/application/read-only-model-controller.test.ts +335 -0
  91. package/src/application/read-only-model-controller.ts +79 -0
  92. package/src/domain/events/domain-event.test.ts +82 -0
  93. package/src/domain/events/domain-event.ts +69 -0
  94. package/src/domain/events/event-types.ts +37 -0
  95. package/src/domain/events/mutation-event.test.ts +390 -0
  96. package/src/domain/events/mutation-event.ts +53 -0
  97. package/src/domain/events/query-event.test.ts +228 -0
  98. package/src/domain/events/query-event.ts +14 -0
  99. package/src/domain/events/request-event.test.ts +38 -0
  100. package/src/domain/events/request-event.ts +47 -0
  101. package/src/domain/interfaces/repository.ts +136 -0
  102. package/src/domain/models/pagination.ts +28 -0
  103. package/src/domain/services/base-model-service.ts +54 -0
  104. package/src/domain/services/model-service-args.ts +9 -0
  105. package/src/domain/services/model-service.normalization.test.ts +704 -0
  106. package/src/domain/services/model-service.test.ts +1616 -0
  107. package/src/domain/services/model-service.ts +597 -0
  108. package/src/domain/services/read-only-model-service.test.ts +1130 -0
  109. package/src/domain/services/read-only-model-service.ts +211 -0
  110. package/src/index.ts +17 -4
  111. package/src/shared/utils/schema-inference.ts +26 -0
  112. package/src/shared/utils/schema-inheritance.test.ts +295 -0
  113. package/src/shared/utils/schema-inheritance.ts +28 -0
  114. package/src/shared/utils/test/animal-schema.ts +46 -0
  115. package/src/shared/utils/test/animal-trait-schema.ts +45 -0
  116. package/src/shared/utils/test/elephant-schema.ts +58 -0
  117. package/src/shared/utils/test/elephant-trait-schema.ts +53 -0
  118. package/src/test/mock/models/mock-book-models.ts +78 -0
  119. package/src/test/mock/repositories/mock-memory-repository.assign.test.ts +215 -0
  120. package/src/test/mock/repositories/mock-memory-repository.basic.test.ts +129 -0
  121. package/src/test/mock/repositories/mock-memory-repository.bulk-upsert.test.ts +159 -0
  122. package/src/test/mock/repositories/mock-memory-repository.count.test.ts +98 -0
  123. package/src/test/mock/repositories/mock-memory-repository.search.test.ts +265 -0
  124. package/src/test/mock/repositories/mock-memory-repository.trash.test.ts +736 -0
  125. package/src/test/mock/repositories/mock-memory-repository.ts +401 -0
  126. package/src/test/mock/repositories/mock-memory-repository.upsert.test.ts +108 -0
  127. package/dist/databaseConnection.d.ts +0 -24
  128. package/dist/datastoreAbstract.d.ts +0 -37
  129. package/dist/declaro-data.cjs +0 -1
  130. package/dist/declaro-data.mjs +0 -250
  131. package/dist/hydrateEntity.d.ts +0 -8
  132. package/dist/index.d.ts +0 -4
  133. package/dist/serverConnection.d.ts +0 -15
  134. package/dist/trackedStatus.d.ts +0 -15
  135. package/src/databaseConnection.ts +0 -137
  136. package/src/datastoreAbstract.ts +0 -190
  137. package/src/hydrateEntity.ts +0 -36
  138. package/src/placeholder.test.ts +0 -7
  139. package/src/serverConnection.ts +0 -74
  140. package/src/trackedStatus.ts +0 -35
  141. package/tsconfig.json +0 -10
  142. package/vite.config.ts +0 -23
@@ -0,0 +1,228 @@
1
+ import { describe, it, expect } from 'bun:test'
2
+ import { QueryEvent } from './query-event'
3
+ import { ActionDescriptor, type IActionDescriptorInput } from '@declaro/core'
4
+
5
+ interface IBookResult {
6
+ id: string
7
+ title: string
8
+ description: string
9
+ author: string
10
+ year: number
11
+ }
12
+
13
+ interface ILoadParams {
14
+ id: string
15
+ }
16
+
17
+ interface ISearchParams {
18
+ text?: string
19
+ author?: string
20
+ year?: number
21
+ }
22
+
23
+ interface ICustomMeta {
24
+ customField?: string
25
+ anotherField?: number
26
+ }
27
+
28
+ describe('QueryEvent', () => {
29
+ it('should create a query event with params and descriptor', () => {
30
+ const params: ILoadParams = { id: '1' }
31
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'load' }
32
+ const event = new QueryEvent<IBookResult, ILoadParams>(descriptor, params)
33
+
34
+ expect(event.input).toEqual(params)
35
+ expect(event.descriptor.toString()).toEqual(new ActionDescriptor(descriptor).toString())
36
+ expect(event.meta).toEqual({})
37
+ expect(event.data).toBeUndefined()
38
+ })
39
+
40
+ it('should set result correctly', () => {
41
+ const params: ILoadParams = { id: '1' }
42
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'load' }
43
+ const event = new QueryEvent<IBookResult, ILoadParams>(descriptor, params)
44
+
45
+ expect(event.data).toBeUndefined()
46
+
47
+ const result: IBookResult = {
48
+ id: '1',
49
+ title: 'Test Book',
50
+ description: 'A book for testing',
51
+ author: 'Author Name',
52
+ year: 2024,
53
+ }
54
+ event.setResult(result)
55
+
56
+ expect(event.data).toEqual(result)
57
+ })
58
+
59
+ it('should update meta correctly', () => {
60
+ const params: ISearchParams = { text: '1984' }
61
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'search' }
62
+ const event = new QueryEvent<IBookResult[], ISearchParams, ICustomMeta>(descriptor, params)
63
+
64
+ event.setMeta({ customField: 'custom value' })
65
+
66
+ expect(event.meta.customField).toBe('custom value')
67
+ })
68
+
69
+ it('should serialize to JSON with eventId and timestamp', () => {
70
+ const params: ILoadParams = { id: '1' }
71
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'load' }
72
+ const event = new QueryEvent<IBookResult, ILoadParams>(descriptor, params)
73
+
74
+ const result: IBookResult = {
75
+ id: '1',
76
+ title: 'Test Book',
77
+ description: 'A book for testing',
78
+ author: 'Author Name',
79
+ year: 2024,
80
+ }
81
+ event.setResult(result)
82
+
83
+ const json = event.toJSON()
84
+
85
+ expect(json.eventId).toBeDefined()
86
+ expect(json.timestamp).toBeDefined()
87
+ expect(json.type).toBe('books::book.load')
88
+ expect(json.input).toEqual(params)
89
+ expect(json.data).toEqual(result)
90
+ })
91
+
92
+ it('should handle chaining of setter methods', () => {
93
+ const params: ILoadParams = { id: '1' }
94
+ const result: IBookResult = {
95
+ id: '1',
96
+ title: 'Test Book',
97
+ description: 'A book for testing',
98
+ author: 'Author Name',
99
+ year: 2024,
100
+ }
101
+
102
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'load' }
103
+ const event = new QueryEvent<IBookResult, ILoadParams, ICustomMeta>(descriptor, params)
104
+ .setMeta({ customField: 'value' })
105
+ .setResult(result)
106
+
107
+ expect(event.input).toEqual(params)
108
+ expect(event.meta.customField).toBe('value')
109
+ expect(event.data).toEqual(result)
110
+ })
111
+
112
+ it('should update input after initialization', () => {
113
+ const params: ISearchParams = { text: '1984' }
114
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'search' }
115
+ const event = new QueryEvent<IBookResult[], ISearchParams>(descriptor, params)
116
+
117
+ const newParams: ISearchParams = {
118
+ text: 'Brave New World',
119
+ author: 'Aldous Huxley',
120
+ }
121
+ event.setInput(newParams)
122
+
123
+ expect(event.input).toEqual(newParams)
124
+ })
125
+
126
+ it('should maintain type throughout lifecycle', () => {
127
+ const params: ILoadParams = { id: '1' }
128
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'load' }
129
+ const event = new QueryEvent<IBookResult, ILoadParams>(descriptor, params)
130
+
131
+ expect(event.type).toBe('books::book.load')
132
+
133
+ event.setInput({ id: '2' })
134
+ expect(event.type).toBe('books::book.load')
135
+
136
+ const result: IBookResult = {
137
+ id: '2',
138
+ title: 'Another Book',
139
+ description: 'Another description',
140
+ author: 'Another Author',
141
+ year: 2025,
142
+ }
143
+ event.setResult(result)
144
+ expect(event.type).toBe('books::book.load')
145
+ })
146
+
147
+ it('should handle search action with multiple filters', () => {
148
+ const params: ISearchParams = {
149
+ text: 'science fiction',
150
+ author: 'Isaac Asimov',
151
+ year: 1950,
152
+ }
153
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'search' }
154
+ const event = new QueryEvent<IBookResult[], ISearchParams>(descriptor, params)
155
+
156
+ expect(event.descriptor.action).toBe('search')
157
+ expect(event.input).toEqual(params)
158
+ })
159
+
160
+ it('should handle search results as array', () => {
161
+ const params: ISearchParams = { text: 'Foundation' }
162
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'search' }
163
+ const event = new QueryEvent<IBookResult[], ISearchParams>(descriptor, params)
164
+
165
+ const results: IBookResult[] = [
166
+ {
167
+ id: '1',
168
+ title: 'Foundation',
169
+ description: 'First book',
170
+ author: 'Isaac Asimov',
171
+ year: 1951,
172
+ },
173
+ {
174
+ id: '2',
175
+ title: 'Foundation and Empire',
176
+ description: 'Second book',
177
+ author: 'Isaac Asimov',
178
+ year: 1952,
179
+ },
180
+ ]
181
+ event.setResult(results)
182
+
183
+ expect(event.data).toEqual(results)
184
+ expect(event.data?.length).toBe(2)
185
+ })
186
+
187
+ it('should handle load action with single result', () => {
188
+ const params: ILoadParams = { id: '42' }
189
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'load' }
190
+ const event = new QueryEvent<IBookResult, ILoadParams>(descriptor, params)
191
+
192
+ const result: IBookResult = {
193
+ id: '42',
194
+ title: "The Hitchhiker's Guide to the Galaxy",
195
+ description: "Don't Panic",
196
+ author: 'Douglas Adams',
197
+ year: 1979,
198
+ }
199
+ event.setResult(result)
200
+
201
+ expect(event.data).toEqual(result)
202
+ expect(event.descriptor.action).toBe('load')
203
+ })
204
+
205
+ it('should serialize meta to JSON', () => {
206
+ const params: ISearchParams = { text: 'test' }
207
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'search' }
208
+ const event = new QueryEvent<IBookResult[], ISearchParams, ICustomMeta>(descriptor, params, {
209
+ customField: 'custom value',
210
+ anotherField: 123,
211
+ })
212
+
213
+ const json = event.toJSON()
214
+
215
+ expect(json.meta.customField).toBe('custom value')
216
+ expect(json.meta.anotherField).toBe(123)
217
+ expect(json.input).toEqual(params)
218
+ })
219
+
220
+ it('should handle empty search params', () => {
221
+ const params: ISearchParams = {}
222
+ const descriptor: IActionDescriptorInput = { namespace: 'books', resource: 'book', action: 'search' }
223
+ const event = new QueryEvent<IBookResult[], ISearchParams>(descriptor, params)
224
+
225
+ expect(event.input).toEqual({})
226
+ expect(event.descriptor.action).toBe('search')
227
+ })
228
+ })
@@ -0,0 +1,14 @@
1
+ import type { IActionDescriptorInput } from '@declaro/core'
2
+ import { RequestEvent, type IRequestEventMeta } from './request-event'
3
+
4
+ export interface IQueryEventMeta<TResult> extends IRequestEventMeta {}
5
+
6
+ export class QueryEvent<
7
+ TResult,
8
+ TParams,
9
+ TMeta extends IQueryEventMeta<TResult> = IQueryEventMeta<TResult>,
10
+ > extends RequestEvent<TResult, TParams, TMeta> {
11
+ constructor(descriptor: IActionDescriptorInput, params: TParams, meta: TMeta = {} as TMeta) {
12
+ super(descriptor, params, meta)
13
+ }
14
+ }
@@ -0,0 +1,38 @@
1
+ import { describe, it, expect } from 'bun:test'
2
+ import { RequestEvent } from './request-event'
3
+
4
+ describe('RequestEvent', () => {
5
+ it('should initialize meta correctly', () => {
6
+ const input = { key: 'value' }
7
+ const descriptor = { namespace: 'test', action: 'create' }
8
+ const event = new RequestEvent(descriptor, input, {
9
+ foo: 'bar',
10
+ })
11
+
12
+ expect(event.meta.foo).toBe('bar')
13
+ expect(event.input?.key).toBe('value')
14
+ })
15
+
16
+ it('should update meta correctly', () => {
17
+ const input = { key: 'value' }
18
+ const descriptor = { namespace: 'test', action: 'create' }
19
+ const event = new RequestEvent(descriptor, input, {
20
+ foo: 'bar',
21
+ })
22
+
23
+ event.setMeta({ foo: 'test' })
24
+ expect(event.meta.foo).toBe('test')
25
+ })
26
+
27
+ it('should set result correctly', () => {
28
+ const input = { key: 'value' }
29
+ const descriptor = { namespace: 'test', action: 'create' }
30
+ const event = new RequestEvent(descriptor, input, {
31
+ foo: 'bar',
32
+ })
33
+
34
+ const result = { success: true }
35
+ event.setResult(result)
36
+ expect(event.data).toEqual(result)
37
+ })
38
+ })
@@ -0,0 +1,47 @@
1
+ import type { IActionDescriptorInput, Simplify } from '@declaro/core'
2
+ import { DomainEvent } from './domain-event'
3
+
4
+ export interface IRequestEventMeta {}
5
+
6
+ export interface IRequestEventJSON<TInput, TMeta = IRequestEventMeta> extends Simplify<DomainEvent<TInput, TMeta>> {
7
+ input: TInput
8
+ }
9
+
10
+ export class RequestEvent<TResult, TInput, TMeta extends IRequestEventMeta = IRequestEventMeta> extends DomainEvent<
11
+ TResult,
12
+ TMeta
13
+ > {
14
+ input: TInput
15
+
16
+ constructor(descriptor: IActionDescriptorInput, input: TInput, meta: TMeta = {} as TMeta) {
17
+ super({
18
+ meta,
19
+ descriptor,
20
+ })
21
+
22
+ this.input = input
23
+ }
24
+
25
+ setInput(input: TInput): this {
26
+ this.input = input
27
+ return this
28
+ }
29
+
30
+ setMeta(meta: Partial<TMeta>): this {
31
+ this.meta = { ...this.meta, ...meta }
32
+ return this
33
+ }
34
+
35
+ setResult(result: TResult): this {
36
+ this.data = result
37
+
38
+ return this
39
+ }
40
+
41
+ toJSON() {
42
+ return {
43
+ ...super.toJSON(),
44
+ input: this.input,
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,136 @@
1
+ import type { AnyModelSchema } from '@declaro/core'
2
+ import type {
3
+ InferDetail,
4
+ InferFilters,
5
+ InferInput,
6
+ InferLookup,
7
+ InferSearchResults,
8
+ InferSummary,
9
+ } from '../../shared/utils/schema-inference'
10
+ import type { ICreateOptions, IUpdateOptions } from '../services/model-service'
11
+ import type { ILoadOptions, ISearchOptions } from '../services/read-only-model-service'
12
+
13
+ export interface IRepository<TSchema extends AnyModelSchema> {
14
+ /**
15
+ * Loads a single element based on the provided lookup.
16
+ *
17
+ * @param input - The lookup criteria for the element.
18
+ * @returns A promise resolving to the detailed element or null if not found.
19
+ */
20
+ load(input: InferLookup<TSchema>, options?: ILoadOptions): Promise<InferDetail<TSchema> | null>
21
+
22
+ /**
23
+ * Loads multiple elements based on the provided lookups.
24
+ *
25
+ * @param inputs - The lookup criteria for the elements.
26
+ * @returns A promise resolving to an array of detailed elements.
27
+ */
28
+ loadMany(inputs: InferLookup<TSchema>[], options?: ILoadOptions): Promise<InferDetail<TSchema>[]>
29
+
30
+ /**
31
+ * Searches for elements based on filters and optional pagination.
32
+ *
33
+ * @param input - The filters to apply to the search.
34
+ * @param pagination - Optional pagination criteria.
35
+ * @returns A promise resolving to the search results.
36
+ */
37
+ search(input: InferFilters<TSchema>, options?: ISearchOptions<TSchema>): Promise<InferSearchResults<TSchema>>
38
+
39
+ /**
40
+ * Deletes elements based on the provided lookups.
41
+ *
42
+ * @param lookup - The lookup criteria for the element to delete.
43
+ * @returns A promise resolving to the deleted list item.
44
+ */
45
+ remove(lookup: InferLookup<TSchema>, options?: ILoadOptions): Promise<InferSummary<TSchema>>
46
+
47
+ /**
48
+ * Restores an element based on the provided lookups, if a soft-deleted copy exists.
49
+ *
50
+ * @param lookup - The lookup criteria for the element to restore.
51
+ * @returns A promise resolving to the restored list item.
52
+ */
53
+ restore(lookup: InferLookup<TSchema>, options?: ILoadOptions): Promise<InferSummary<TSchema>>
54
+
55
+ /**
56
+ * Creates a new element based on the provided input.
57
+ *
58
+ * @param input - The input data for the new element.
59
+ * @returns A promise resolving to the detailed created element.
60
+ */
61
+ create(input: InferInput<TSchema>, options?: ICreateOptions): Promise<InferDetail<TSchema>>
62
+
63
+ /**
64
+ * Updates elements based on filters.
65
+ *
66
+ * @param filters - The filters to apply to the update.
67
+ * @returns A promise resolving to the detailed updated element.
68
+ */
69
+ update(
70
+ lookup: InferLookup<TSchema>,
71
+ input: InferInput<TSchema>,
72
+ options?: IUpdateOptions,
73
+ ): Promise<InferDetail<TSchema>>
74
+
75
+ /**
76
+ * Upserts an element based on the provided input.
77
+ * If the element exists, it will be updated; otherwise, a new element will be created.
78
+ *
79
+ * A load operation will be performed to check if the element exists. create/update related events will be dispatched accordingly.
80
+ *
81
+ * @param input - The input data for the new or existing element.
82
+ * @returns A promise resolving to the detailed created or updated element.
83
+ */
84
+ upsert(input: InferInput<TSchema>, options?: ICreateOptions | IUpdateOptions): Promise<InferDetail<TSchema>>
85
+
86
+ /**
87
+ * Upserts multiple elements based on the provided inputs.
88
+ * If an element exists, it will be updated; otherwise, a new element will be created.
89
+ *
90
+ * A loadMany operation will be performed to check if the elements exist. create/update related events will be dispatched accordingly.
91
+ *
92
+ * @param inputs - The input data for the upsert operation.
93
+ * @returns A promise resolving to an array of detailed upserted elements.
94
+ */
95
+ bulkUpsert(
96
+ inputs: InferInput<TSchema>[],
97
+ options?: ICreateOptions | IUpdateOptions,
98
+ ): Promise<InferDetail<TSchema>[]>
99
+
100
+ /**
101
+ * Counts the number of elements matching the provided search criteria.
102
+ *
103
+ * @param search - The search criteria to apply.
104
+ * @returns A promise resolving to the count of matching elements.
105
+ */
106
+ count(search: InferFilters<TSchema>, options?: ISearchOptions<TSchema>): Promise<number>
107
+
108
+ /**
109
+ * Permanently deletes all items from trash, optionally filtered by the provided criteria.
110
+ * Items deleted via this method cannot be restored.
111
+ *
112
+ * @param filters - Optional filters to apply when selecting items to delete from trash.
113
+ * @returns A promise resolving to the count of permanently deleted items.
114
+ */
115
+ emptyTrash(filters?: InferFilters<TSchema>): Promise<number>
116
+
117
+ /**
118
+ * Permanently deletes a specific item from trash based on the provided lookup.
119
+ * The item must exist in trash (previously removed). Items deleted via this method cannot be restored.
120
+ *
121
+ * @param lookup - The lookup criteria for the item to permanently delete from trash.
122
+ * @returns A promise resolving to the permanently deleted item summary.
123
+ * @throws Error if the item is not found in trash.
124
+ */
125
+ permanentlyDeleteFromTrash(lookup: InferLookup<TSchema>): Promise<InferSummary<TSchema>>
126
+
127
+ /**
128
+ * Permanently deletes an item based on the provided lookup, regardless of whether it is active or in trash.
129
+ * Items deleted via this method cannot be restored.
130
+ *
131
+ * @param lookup - The lookup criteria for the item to permanently delete.
132
+ * @returns A promise resolving to the permanently deleted item summary.
133
+ * @throws Error if the item is not found in either active data or trash.
134
+ */
135
+ permanentlyDelete(lookup: InferLookup<TSchema>): Promise<InferSummary<TSchema>>
136
+ }
@@ -0,0 +1,28 @@
1
+ import z from 'zod/v4'
2
+ import { ZodModel } from '@declaro/zod'
3
+ import { ModelSchema } from '@declaro/core'
4
+
5
+ export const PaginationInput = new ZodModel(
6
+ 'PaginationInput' as const,
7
+ z.object({
8
+ page: z.number().int().min(1).default(1).nullish(),
9
+ pageSize: z.number().int().min(1).default(25).nullish(),
10
+ }),
11
+ )
12
+ export type IPaginationInput = z.infer<typeof PaginationInput.schema>
13
+
14
+ export const PaginationOutput = new ZodModel(
15
+ 'PaginationOutput' as const,
16
+ z.object({
17
+ page: z.number().int().min(1).default(1),
18
+ pageSize: z.number().int().min(1).default(25),
19
+ total: z.number().int().min(0).default(0),
20
+ totalPages: z.number().int().min(1).default(1),
21
+ }),
22
+ )
23
+ export type IPagination = z.infer<typeof PaginationOutput.schema>
24
+
25
+ export const PaginationSchema = ModelSchema.create('Pagination').custom({
26
+ input: () => PaginationInput,
27
+ output: () => PaginationOutput,
28
+ })
@@ -0,0 +1,54 @@
1
+ import { ActionDescriptor, type AnyModelSchema, type ModelSchema } from '@declaro/core'
2
+ import type { IModelServiceArgs } from './model-service-args'
3
+ import type { IRepository } from '../interfaces/repository'
4
+ import type {
5
+ InferEntityMetadata,
6
+ InferInput,
7
+ InferLookup,
8
+ InferPrimaryKeyType,
9
+ } from '../../shared/utils/schema-inference'
10
+
11
+ export interface IActionOptions {
12
+ /**
13
+ * The scope of the load operation, e.g., 'detail', 'list', etc.
14
+ */
15
+ scope?: string
16
+ }
17
+
18
+ export class BaseModelService<TSchema extends AnyModelSchema> {
19
+ protected readonly namespace: string
20
+ protected readonly schema: TSchema
21
+ protected readonly entityMetadata: InferEntityMetadata<TSchema>
22
+ protected readonly emitter: IModelServiceArgs<TSchema>['emitter']
23
+ protected readonly repository: IRepository<TSchema>
24
+
25
+ constructor(args: IModelServiceArgs<TSchema>) {
26
+ this.schema = args.schema
27
+ this.namespace = args.namespace ?? 'global'
28
+ this.emitter = args.emitter
29
+ this.repository = args.repository
30
+ this.entityMetadata = this.schema.getEntityMetadata()
31
+ }
32
+
33
+ getDescriptor(action: string, scope?: string) {
34
+ return ActionDescriptor.fromJSON({
35
+ namespace: this.namespace,
36
+ resource: this.schema.name,
37
+ action,
38
+ scope,
39
+ })
40
+ }
41
+
42
+ getPrimaryKeyValue(input: InferLookup<TSchema>): InferPrimaryKeyType<TSchema>
43
+ getPrimaryKeyValue(input: InferInput<TSchema>): InferPrimaryKeyType<TSchema>
44
+ getPrimaryKeyValue(input: any = {}): InferPrimaryKeyType<TSchema> {
45
+ if (!this.entityMetadata?.primaryKey) {
46
+ return undefined
47
+ }
48
+ return input[this.entityMetadata.primaryKey]
49
+ }
50
+
51
+ public getSchema() {
52
+ return this.schema
53
+ }
54
+ }
@@ -0,0 +1,9 @@
1
+ import type { AnyModelSchema, EventManager } from '@declaro/core'
2
+ import type { IRepository } from '../interfaces/repository'
3
+
4
+ export interface IModelServiceArgs<TSchema extends AnyModelSchema> {
5
+ schema: TSchema
6
+ namespace?: string
7
+ emitter: EventManager
8
+ repository: IRepository<TSchema>
9
+ }