@declaro/data 2.0.0-beta.98 → 2.0.0-beta.99

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 (33) hide show
  1. package/dist/browser/index.js +7 -7
  2. package/dist/browser/index.js.map +3 -3
  3. package/dist/node/index.cjs +59 -17
  4. package/dist/node/index.cjs.map +3 -3
  5. package/dist/node/index.js +59 -17
  6. package/dist/node/index.js.map +3 -3
  7. package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts +2 -0
  8. package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts.map +1 -0
  9. package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts +2 -0
  10. package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts.map +1 -0
  11. package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts +2 -0
  12. package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts.map +1 -0
  13. package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts +2 -0
  14. package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts.map +1 -0
  15. package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts +1 -0
  16. package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts.map +1 -0
  17. package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts.map +1 -1
  18. package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts +2 -0
  19. package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts.map +1 -0
  20. package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts +2 -0
  21. package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts.map +1 -0
  22. package/package.json +5 -5
  23. package/src/test/mock/repositories/mock-memory-repository.assign.test.ts +215 -0
  24. package/src/test/mock/repositories/mock-memory-repository.basic.test.ts +129 -0
  25. package/src/test/mock/repositories/mock-memory-repository.bulk-upsert.test.ts +159 -0
  26. package/src/test/mock/repositories/mock-memory-repository.count.test.ts +98 -0
  27. package/src/test/mock/repositories/mock-memory-repository.custom-lookup.test.ts +0 -0
  28. package/src/test/mock/repositories/mock-memory-repository.search.test.ts +265 -0
  29. package/src/test/mock/repositories/mock-memory-repository.ts +67 -16
  30. package/src/test/mock/repositories/mock-memory-repository.upsert.test.ts +108 -0
  31. package/dist/ts/test/mock/repositories/mock-memory-repository.test.d.ts +0 -2
  32. package/dist/ts/test/mock/repositories/mock-memory-repository.test.d.ts.map +0 -1
  33. package/src/test/mock/repositories/mock-memory-repository.test.ts +0 -919
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mock-memory-repository.assign.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.assign.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.assign.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mock-memory-repository.basic.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.basic.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.basic.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mock-memory-repository.bulk-upsert.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.bulk-upsert.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.bulk-upsert.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mock-memory-repository.count.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.count.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.count.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=mock-memory-repository.custom-lookup.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.custom-lookup.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.custom-lookup.test.ts"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"mock-memory-repository.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAqB,MAAM,eAAe,CAAA;AAC5F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uCAAuC,CAAA;AAExE,OAAO,KAAK,EACR,WAAW,EACX,YAAY,EACZ,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACf,MAAM,wCAAwC,CAAA;AAE/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kDAAkD,CAAA;AACtF,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAA;AAE5F,MAAM,WAAW,yBAAyB,CAAC,OAAO,SAAS,cAAc;IACrE,MAAM,EAAE,OAAO,CAAA;IACf,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,KAAK,OAAO,CAAA;IAC9E,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,OAAO,CAAA;IACjF,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,KAAK,WAAW,CAAC,OAAO,CAAC,CAAA;CAC5F;AAED,qBAAa,oBAAoB,CAAC,OAAO,SAAS,cAAc,CAAE,YAAW,WAAW,CAAC,O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,OAAO,CAAC,MAAM,CAAC;IAKpG,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAkB5G,UAAU,CACZ,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,EAC7B,OAAO,CAAC,EAAE,cAAc,GAAG,cAAc,GAC1C,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;IAIlC;;;;OAIG;IACH,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,EAAE;IAW5E;;;;;OAKG;IACH,SAAS,CAAC,WAAW,CAAC,YAAY,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC;cAS3F,kBAAkB;CAcrC"}
1
+ {"version":3,"file":"mock-memory-repository.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAqB,MAAM,eAAe,CAAA;AAC5F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uCAAuC,CAAA;AAExE,OAAO,KAAK,EACR,WAAW,EACX,YAAY,EACZ,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACf,MAAM,wCAAwC,CAAA;AAE/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kDAAkD,CAAA;AACtF,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAA;AAE5F,MAAM,WAAW,yBAAyB,CAAC,OAAO,SAAS,cAAc;IACrE,MAAM,EAAE,OAAO,CAAA;IACf,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,OA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C,GAAG,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IA2BrE,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAsBjE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAgC/F,KAAK,CAAC,MAAM,EAAE,YAAY,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,cAAc,CAAC,OAAO,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAKpG,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAkB5G,UAAU,CACZ,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,EAAE,EAC7B,OAAO,CAAC,EAAE,cAAc,GAAG,cAAc,GAC1C,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;IAIlC;;;;OAIG;IACH,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,EAAE;IAW5E;;;;;OAKG;IACH,SAAS,CAAC,WAAW,CAAC,YAAY,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC;cAS3F,kBAAkB;CAcrC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mock-memory-repository.search.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.search.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.search.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mock-memory-repository.upsert.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-memory-repository.upsert.test.d.ts","sourceRoot":"","sources":["../../../../../src/test/mock/repositories/mock-memory-repository.upsert.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@declaro/data",
3
- "version": "2.0.0-beta.98",
3
+ "version": "2.0.0-beta.99",
4
4
  "description": "A data-mapper framework for managing application data across integrated systems.",
5
5
  "main": "dist/node/index.cjs",
6
6
  "module": "dist/node/index.js",
@@ -22,9 +22,9 @@
22
22
  "@declaro/zod": "^2.0.0-beta.51"
23
23
  },
24
24
  "devDependencies": {
25
- "@declaro/auth": "^2.0.0-beta.98",
26
- "@declaro/core": "^2.0.0-beta.98",
27
- "@declaro/zod": "^2.0.0-beta.98",
25
+ "@declaro/auth": "^2.0.0-beta.99",
26
+ "@declaro/core": "^2.0.0-beta.99",
27
+ "@declaro/zod": "^2.0.0-beta.99",
28
28
  "crypto-browserify": "^3.12.1",
29
29
  "typescript": "^5.8.3",
30
30
  "uuid": "^11.1.0",
@@ -43,5 +43,5 @@
43
43
  "require": "./dist/node/index.cjs",
44
44
  "browser": "./dist/browser/index.js"
45
45
  },
46
- "gitHead": "5f998cfb45e04482853d7511c04348d668c292da"
46
+ "gitHead": "0277b5d86fbbb5b1c1f77e367005bb11551471e7"
47
47
  }
@@ -0,0 +1,215 @@
1
+ import { beforeEach, describe, expect, it, mock } from 'bun:test'
2
+ import { MockBookSchema } from '../models/mock-book-models'
3
+ import { MockMemoryRepository } from './mock-memory-repository'
4
+
5
+ describe('MockMemoryRepository - Assign Functionality', () => {
6
+ const mockSchema = MockBookSchema
7
+
8
+ let repository: MockMemoryRepository<typeof mockSchema>
9
+
10
+ beforeEach(() => {
11
+ repository = new MockMemoryRepository({ schema: mockSchema })
12
+ })
13
+
14
+ it('should use default Object.assign for create when no custom assign function is provided', async () => {
15
+ const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
16
+ const createdItem = await repository.create(input)
17
+
18
+ expect(createdItem).toMatchObject(input)
19
+ expect(createdItem.id).toBeDefined()
20
+ })
21
+
22
+ it('should use default Object.assign for update when no custom assign function is provided', async () => {
23
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
24
+ const createdItem = await repository.create(input)
25
+
26
+ const updateInput = { title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
27
+ const updatedItem = await repository.update({ id: createdItem.id }, updateInput)
28
+
29
+ expect(updatedItem).toEqual({
30
+ id: createdItem.id,
31
+ title: 'Updated Book',
32
+ author: 'Updated Author',
33
+ publishedDate: updateInput.publishedDate,
34
+ })
35
+ })
36
+
37
+ it('should use default Object.assign for upsert when no custom assign function is provided', async () => {
38
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
39
+ const upsertedItem = await repository.upsert(input)
40
+
41
+ expect(upsertedItem).toEqual(input)
42
+
43
+ const updateInput = { id: 42, title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
44
+ const updatedItem = await repository.upsert(updateInput)
45
+
46
+ expect(updatedItem).toEqual(updateInput)
47
+ })
48
+
49
+ it('should use custom assign function for create operation', async () => {
50
+ const customAssignMock = mock((existing: any, input: any) => ({
51
+ ...existing,
52
+ ...input,
53
+ customField: 'custom_create_value',
54
+ }))
55
+
56
+ const customRepository = new MockMemoryRepository({
57
+ schema: mockSchema,
58
+ assign: customAssignMock,
59
+ })
60
+
61
+ const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
62
+ const createdItem = await customRepository.create(input)
63
+
64
+ expect(customAssignMock).toHaveBeenCalledWith({}, input)
65
+ expect((createdItem as any).customField).toBe('custom_create_value')
66
+ expect(createdItem).toMatchObject(input)
67
+ })
68
+
69
+ it('should use custom assign function for update operation', async () => {
70
+ const customAssignMock = mock((existing: any, input: any) => ({
71
+ ...existing,
72
+ ...input,
73
+ customField: 'custom_update_value',
74
+ lastModified: new Date('2023-01-01'),
75
+ }))
76
+
77
+ const customRepository = new MockMemoryRepository({
78
+ schema: mockSchema,
79
+ assign: customAssignMock,
80
+ })
81
+
82
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
83
+ const createdItem = await customRepository.create(input)
84
+
85
+ const updateInput = { title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
86
+ const updatedItem = await customRepository.update({ id: createdItem.id }, updateInput)
87
+
88
+ expect(customAssignMock).toHaveBeenCalledWith(createdItem, updateInput)
89
+ expect((updatedItem as any).customField).toBe('custom_update_value')
90
+ expect((updatedItem as any).lastModified).toEqual(new Date('2023-01-01'))
91
+ expect(updatedItem.title).toBe('Updated Book')
92
+ })
93
+
94
+ it('should use custom assign function for upsert operation on existing item', async () => {
95
+ const customAssignMock = mock((existing: any, input: any) => ({
96
+ ...existing,
97
+ ...input,
98
+ mergeTimestamp: new Date('2023-01-01'),
99
+ isUpserted: true,
100
+ }))
101
+
102
+ const customRepository = new MockMemoryRepository({
103
+ schema: mockSchema,
104
+ assign: customAssignMock,
105
+ })
106
+
107
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
108
+ const createdItem = await customRepository.create(input)
109
+
110
+ const upsertInput = { id: 42, title: 'Upserted Book', author: 'Upserted Author', publishedDate: new Date() }
111
+ const upsertedItem = await customRepository.upsert(upsertInput)
112
+
113
+ // Should have been called twice: once for create, once for upsert
114
+ expect(customAssignMock).toHaveBeenCalledTimes(2)
115
+ expect(customAssignMock).toHaveBeenLastCalledWith(createdItem, upsertInput)
116
+ expect((upsertedItem as any).mergeTimestamp).toEqual(new Date('2023-01-01'))
117
+ expect((upsertedItem as any).isUpserted).toBe(true)
118
+ expect(upsertedItem.title).toBe('Upserted Book')
119
+ })
120
+
121
+ it('should use custom assign function for upsert operation on new item', async () => {
122
+ const customAssignMock = mock((existing: any, input: any) => ({
123
+ ...existing,
124
+ ...input,
125
+ createdViaUpsert: true,
126
+ }))
127
+
128
+ const customRepository = new MockMemoryRepository({
129
+ schema: mockSchema,
130
+ assign: customAssignMock,
131
+ })
132
+
133
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
134
+ const upsertedItem = await customRepository.upsert(input)
135
+
136
+ expect(customAssignMock).toHaveBeenCalledWith({}, input)
137
+ expect((upsertedItem as any).createdViaUpsert).toBe(true)
138
+ expect(upsertedItem).toMatchObject(input)
139
+ })
140
+
141
+ it('should use custom assign function for bulkUpsert operation', async () => {
142
+ const customAssignMock = mock((existing: any, input: any) => ({
143
+ ...existing,
144
+ ...input,
145
+ bulkProcessed: true,
146
+ }))
147
+
148
+ const customRepository = new MockMemoryRepository({
149
+ schema: mockSchema,
150
+ assign: customAssignMock,
151
+ })
152
+
153
+ const inputs = [
154
+ { id: 1, title: 'Book 1', author: 'Author 1', publishedDate: new Date() },
155
+ { id: 2, title: 'Book 2', author: 'Author 2', publishedDate: new Date() },
156
+ ]
157
+
158
+ const results = await customRepository.bulkUpsert(inputs)
159
+
160
+ expect(customAssignMock).toHaveBeenCalledTimes(2)
161
+ expect((results[0] as any).bulkProcessed).toBe(true)
162
+ expect((results[1] as any).bulkProcessed).toBe(true)
163
+ expect(results[0]).toMatchObject(inputs[0])
164
+ expect(results[1]).toMatchObject(inputs[1])
165
+ })
166
+
167
+ it('should handle complex custom assign logic with conditional merging', async () => {
168
+ const customAssign = (existing: any, input: any) => {
169
+ const result = { ...existing, ...input }
170
+
171
+ // Custom logic: preserve original author if input doesn't have one
172
+ if (!input.author && existing.author) {
173
+ result.author = existing.author
174
+ }
175
+
176
+ // Custom logic: track modification count
177
+ result.modificationCount = (existing.modificationCount || 0) + 1
178
+
179
+ return result
180
+ }
181
+
182
+ const customRepository = new MockMemoryRepository({
183
+ schema: mockSchema,
184
+ assign: customAssign,
185
+ })
186
+
187
+ const input = { id: 42, title: 'Test Book', author: 'Original Author', publishedDate: new Date() }
188
+ const createdItem = await customRepository.create(input)
189
+
190
+ expect((createdItem as any).modificationCount).toBe(1)
191
+
192
+ // Update with partial data - use existing values for required fields
193
+ const updateInput = {
194
+ title: 'Updated Book',
195
+ author: createdItem.author,
196
+ publishedDate: createdItem.publishedDate,
197
+ }
198
+ const updatedItem = await customRepository.update({ id: createdItem.id }, updateInput)
199
+
200
+ expect(updatedItem.author).toBe('Original Author')
201
+ expect(updatedItem.title).toBe('Updated Book')
202
+ expect((updatedItem as any).modificationCount).toBe(2)
203
+
204
+ // Update with new author
205
+ const updateWithAuthor = {
206
+ title: updatedItem.title,
207
+ author: 'New Author',
208
+ publishedDate: updatedItem.publishedDate,
209
+ }
210
+ const finalItem = await customRepository.update({ id: createdItem.id }, updateWithAuthor)
211
+
212
+ expect(finalItem.author).toBe('New Author')
213
+ expect((finalItem as any).modificationCount).toBe(3)
214
+ })
215
+ })
@@ -0,0 +1,129 @@
1
+ import { beforeEach, describe, expect, it } from 'bun:test'
2
+ import { MockBookSchema } from '../models/mock-book-models'
3
+ import { MockMemoryRepository } from './mock-memory-repository'
4
+ import { z } from 'zod/v4'
5
+ import { ZodModel } from '@declaro/zod'
6
+
7
+ describe('MockMemoryRepository - Basic Operations', () => {
8
+ const mockSchema = MockBookSchema
9
+
10
+ let repository: MockMemoryRepository<typeof mockSchema>
11
+
12
+ beforeEach(() => {
13
+ repository = new MockMemoryRepository({ schema: mockSchema })
14
+ })
15
+
16
+ it('should create an item', async () => {
17
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
18
+ const createdItem = await repository.create(input)
19
+
20
+ expect(createdItem).toEqual(input)
21
+ expect(await repository.load({ id: createdItem.id })).toEqual(createdItem)
22
+ })
23
+
24
+ it('should throw an error when creating an item with duplicate primary key', async () => {
25
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
26
+ const createdItem = await repository.create(input)
27
+
28
+ await expect(repository.create({ ...input, id: createdItem.id })).rejects.toThrow(
29
+ 'Item with the same primary key already exists',
30
+ )
31
+ })
32
+
33
+ it('should update an existing item', async () => {
34
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
35
+ const createdItem = await repository.create(input)
36
+
37
+ const updatedInput = { title: 'Updated Book', author: 'Updated Author', publishedDate: new Date() }
38
+ const updatedItem = await repository.update({ id: createdItem.id }, updatedInput)
39
+
40
+ expect(updatedItem).toEqual({ id: createdItem.id, ...updatedInput })
41
+ expect(await repository.load({ id: createdItem.id })).toEqual({ id: createdItem.id, ...updatedInput })
42
+ })
43
+
44
+ it('should throw an error when updating a non-existent item', async () => {
45
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
46
+
47
+ await expect(repository.update({ id: 999 }, input)).rejects.toThrow('Item not found')
48
+ })
49
+
50
+ it('should remove an item', async () => {
51
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
52
+ const createdItem = await repository.create(input)
53
+
54
+ const removedItem = await repository.remove({ id: createdItem.id })
55
+
56
+ expect(removedItem).toEqual(input)
57
+ const loadedItem = await repository.load({ id: createdItem.id })
58
+ expect(loadedItem).toBeNull()
59
+ })
60
+
61
+ it('should restore a removed item', async () => {
62
+ const input = { id: 42, title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
63
+ const createdItem = await repository.create(input)
64
+ await repository.remove({ id: createdItem.id })
65
+
66
+ const restoredItem = await repository.restore({ id: createdItem.id })
67
+
68
+ expect(restoredItem).toEqual(input)
69
+ expect(await repository.load({ id: createdItem.id })).toEqual(input)
70
+ })
71
+
72
+ it('should throw an error when restoring a non-existent item', async () => {
73
+ await expect(repository.restore({ id: 999 })).rejects.toThrow('Item not found in trash')
74
+ })
75
+
76
+ it('should allow me to create an item without a primary key', async () => {
77
+ const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
78
+ const createdItem = await repository.create(input)
79
+
80
+ expect(createdItem.id).toBeDefined()
81
+ expect(createdItem.title).toBe(input.title)
82
+ expect(createdItem.author).toBe(input.author)
83
+ expect(await repository.load({ id: createdItem.id })).toEqual(createdItem)
84
+ })
85
+
86
+ it('should increment ids when creating items without a primary key', async () => {
87
+ const input1 = { title: 'Test Book 1', author: 'Author Name 1', publishedDate: new Date() }
88
+ const input2 = { title: 'Test Book 2', author: 'Author Name 2', publishedDate: new Date() }
89
+
90
+ const createdItem1 = await repository.create(input1)
91
+ const createdItem2 = await repository.create(input2)
92
+
93
+ expect(createdItem1.id).toBe(1)
94
+ expect(createdItem2.id).toBe(2)
95
+ expect(await repository.load({ id: createdItem1.id })).toEqual(createdItem1)
96
+ expect(await repository.load({ id: createdItem2.id })).toEqual(createdItem2)
97
+ })
98
+
99
+ it('should return null when loading a non-existent item', async () => {
100
+ const result = await repository.load({ id: 999 })
101
+ expect(result).toBeNull()
102
+ })
103
+
104
+ it('should be able to load items from a custom filter', async () => {
105
+ // Creating a hypothetical schema with a custom filter, and a title lookup attribute
106
+ const repository = new MockMemoryRepository({
107
+ schema: mockSchema.custom({
108
+ lookup: (h) =>
109
+ new ZodModel(
110
+ h.name,
111
+ z.object({
112
+ id: z.number().optional(),
113
+ title: z.string().optional(),
114
+ }),
115
+ ),
116
+ }),
117
+ lookup: (data, lookup) =>
118
+ data.id === lookup.id || data.title?.toLowerCase() === lookup.title?.toLowerCase(),
119
+ })
120
+
121
+ const input = { title: 'Test Book', author: 'Author Name', publishedDate: new Date() }
122
+ const createdItem = await repository.create(input)
123
+
124
+ const loadedItem = await repository.load({ id: createdItem.id })
125
+ const titleLoadedItem = await repository.load({ title: createdItem.title })
126
+ expect(loadedItem).toEqual(createdItem)
127
+ expect(titleLoadedItem).toEqual(createdItem)
128
+ })
129
+ })
@@ -0,0 +1,159 @@
1
+ import { beforeEach, describe, expect, it } from 'bun:test'
2
+ import { MockBookSchema } from '../models/mock-book-models'
3
+ import { MockMemoryRepository } from './mock-memory-repository'
4
+
5
+ describe('MockMemoryRepository - Bulk Upsert Functionality', () => {
6
+ const mockSchema = MockBookSchema
7
+
8
+ let repository: MockMemoryRepository<typeof mockSchema>
9
+
10
+ beforeEach(() => {
11
+ repository = new MockMemoryRepository({ schema: mockSchema })
12
+ })
13
+
14
+ it('should upsert multiple new items', async () => {
15
+ const inputs = [
16
+ { id: 1, title: 'Book 1', author: 'Author 1', publishedDate: new Date('2023-01-01') },
17
+ { id: 2, title: 'Book 2', author: 'Author 2', publishedDate: new Date('2023-02-01') },
18
+ { id: 3, title: 'Book 3', author: 'Author 3', publishedDate: new Date('2023-03-01') },
19
+ ]
20
+
21
+ const upsertedItems = await repository.bulkUpsert(inputs)
22
+
23
+ expect(upsertedItems).toHaveLength(3)
24
+ expect(upsertedItems).toEqual(inputs)
25
+
26
+ // Verify all items were created
27
+ for (const input of inputs) {
28
+ expect(await repository.load({ id: input.id })).toEqual(input)
29
+ }
30
+ })
31
+
32
+ it('should update existing items and create new ones in same operation', async () => {
33
+ // Create some initial items
34
+ const existing1 = {
35
+ id: 1,
36
+ title: 'Original Book 1',
37
+ author: 'Original Author 1',
38
+ publishedDate: new Date('2023-01-01'),
39
+ }
40
+ const existing2 = {
41
+ id: 2,
42
+ title: 'Original Book 2',
43
+ author: 'Original Author 2',
44
+ publishedDate: new Date('2023-02-01'),
45
+ }
46
+ await repository.create(existing1)
47
+ await repository.create(existing2)
48
+
49
+ // Bulk upsert with mix of updates and new items
50
+ const upsertInputs = [
51
+ { id: 1, title: 'Updated Book 1', author: 'Updated Author 1', publishedDate: new Date('2023-06-01') }, // Update
52
+ { id: 2, title: 'Updated Book 2', author: 'Updated Author 2', publishedDate: new Date('2023-07-01') }, // Update
53
+ { id: 3, title: 'New Book 3', author: 'New Author 3', publishedDate: new Date('2023-08-01') }, // Create
54
+ { id: 4, title: 'New Book 4', author: 'New Author 4', publishedDate: new Date('2023-09-01') }, // Create
55
+ ]
56
+
57
+ const upsertedItems = await repository.bulkUpsert(upsertInputs)
58
+
59
+ expect(upsertedItems).toHaveLength(4)
60
+ expect(upsertedItems).toEqual(upsertInputs)
61
+
62
+ // Verify all items have the updated/new values
63
+ for (const input of upsertInputs) {
64
+ expect(await repository.load({ id: input.id })).toEqual(input)
65
+ }
66
+ })
67
+
68
+ it('should handle bulk upsert with items without primary keys', async () => {
69
+ const inputs = [
70
+ { title: 'Book Without ID 1', author: 'Author 1', publishedDate: new Date() },
71
+ { title: 'Book Without ID 2', author: 'Author 2', publishedDate: new Date() },
72
+ { title: 'Book Without ID 3', author: 'Author 3', publishedDate: new Date() },
73
+ ]
74
+
75
+ const upsertedItems = await repository.bulkUpsert(inputs)
76
+
77
+ expect(upsertedItems).toHaveLength(3)
78
+
79
+ // All items should have generated IDs
80
+ expect(upsertedItems[0].id).toBe(1)
81
+ expect(upsertedItems[1].id).toBe(2)
82
+ expect(upsertedItems[2].id).toBe(3)
83
+
84
+ // Verify content is preserved
85
+ for (let i = 0; i < inputs.length; i++) {
86
+ expect(upsertedItems[i].title).toBe(inputs[i].title)
87
+ expect(upsertedItems[i].author).toBe(inputs[i].author)
88
+ expect(await repository.load({ id: upsertedItems[i].id })).toEqual(upsertedItems[i])
89
+ }
90
+ })
91
+
92
+ it('should handle empty bulk upsert', async () => {
93
+ const upsertedItems = await repository.bulkUpsert([])
94
+
95
+ expect(upsertedItems).toEqual([])
96
+ })
97
+
98
+ it('should handle bulk upsert with partial updates', async () => {
99
+ // Create initial items
100
+ const initial1 = {
101
+ id: 1,
102
+ title: 'Original Book 1',
103
+ author: 'Original Author 1',
104
+ publishedDate: new Date('2023-01-01'),
105
+ }
106
+ const initial2 = {
107
+ id: 2,
108
+ title: 'Original Book 2',
109
+ author: 'Original Author 2',
110
+ publishedDate: new Date('2023-02-01'),
111
+ }
112
+ await repository.create(initial1)
113
+ await repository.create(initial2)
114
+
115
+ // Updates with all required fields but only changing title
116
+ const partialUpdates = [
117
+ { id: 1, title: 'Updated Title 1', author: 'Original Author 1', publishedDate: new Date('2023-01-01') },
118
+ { id: 2, title: 'Updated Title 2', author: 'Original Author 2', publishedDate: new Date('2023-02-01') },
119
+ ]
120
+
121
+ const upsertedItems = await repository.bulkUpsert(partialUpdates)
122
+
123
+ expect(upsertedItems).toHaveLength(2)
124
+
125
+ // Should have updated titles but kept other properties
126
+ expect(upsertedItems[0].title).toBe('Updated Title 1')
127
+ expect(upsertedItems[0].author).toBe('Original Author 1')
128
+ expect(upsertedItems[0].publishedDate).toEqual(initial1.publishedDate)
129
+
130
+ expect(upsertedItems[1].title).toBe('Updated Title 2')
131
+ expect(upsertedItems[1].author).toBe('Original Author 2')
132
+ expect(upsertedItems[1].publishedDate).toEqual(initial2.publishedDate)
133
+ })
134
+
135
+ it('should handle large bulk operations efficiently', async () => {
136
+ // Create a large number of items to test performance
137
+ const inputs: Array<{ id: number; title: string; author: string; publishedDate: Date }> = []
138
+ for (let i = 1; i <= 100; i++) {
139
+ inputs.push({
140
+ id: i,
141
+ title: `Book ${i}`,
142
+ author: `Author ${i}`,
143
+ publishedDate: new Date(`2023-${String((i % 12) + 1).padStart(2, '0')}-01`),
144
+ })
145
+ }
146
+
147
+ const startTime = Date.now()
148
+ const upsertedItems = await repository.bulkUpsert(inputs)
149
+ const endTime = Date.now()
150
+
151
+ expect(upsertedItems).toHaveLength(100)
152
+ expect(endTime - startTime).toBeLessThan(1000) // Should complete in under 1 second
153
+
154
+ // Verify a few random items
155
+ expect(await repository.load({ id: 1 })).toEqual(inputs[0])
156
+ expect(await repository.load({ id: 50 })).toEqual(inputs[49])
157
+ expect(await repository.load({ id: 100 })).toEqual(inputs[99])
158
+ })
159
+ })