@roc-db/test-utils 0.2.0-pre.8 → 0.2.0-pre.82

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 (68) hide show
  1. package/dist/index.js +933 -287
  2. package/dist/types/entities/BlockImageEntity.d.ts +7 -0
  3. package/dist/types/entities/BlockParagraph.d.ts +7 -0
  4. package/dist/types/entities/BlockRow.d.ts +7 -0
  5. package/dist/types/entities/Draft.d.ts +7 -0
  6. package/dist/types/entities/PostEntity.d.ts +12 -0
  7. package/dist/types/entities/PostVersion.d.ts +8 -0
  8. package/dist/types/entities/UserEntity.d.ts +5 -0
  9. package/dist/types/entities/index.d.ts +33 -0
  10. package/dist/types/index.d.ts +2 -1
  11. package/dist/types/operations/applyDraft.d.ts +1 -0
  12. package/dist/types/operations/createBlockImage.d.ts +5 -15
  13. package/dist/types/operations/createBlockParagraph.d.ts +5 -15
  14. package/dist/types/operations/createBlockRow.d.ts +4 -15
  15. package/dist/types/operations/createBlockTitle.d.ts +4 -15
  16. package/dist/types/operations/createDraft.d.ts +4 -15
  17. package/dist/types/operations/createPost.d.ts +6 -19
  18. package/dist/types/operations/createUser.d.ts +4 -0
  19. package/dist/types/operations/crudBySlug.d.ts +5 -0
  20. package/dist/types/operations/deleteBlocks.d.ts +2 -0
  21. package/dist/types/operations/deleteDraft.d.ts +1 -0
  22. package/dist/types/operations/deletePost.d.ts +1 -9
  23. package/dist/types/operations/index.d.ts +1 -157
  24. package/dist/types/operations/moveBlocks.d.ts +5 -0
  25. package/dist/types/operations/pageBlocks.d.ts +1 -0
  26. package/dist/types/operations/pageEmptyEntitiesArray.d.ts +1 -0
  27. package/dist/types/operations/pagePosts.d.ts +1 -0
  28. package/dist/types/operations/pagePostsByTag.d.ts +1 -0
  29. package/dist/types/operations/pageSplat.d.ts +1 -0
  30. package/dist/types/operations/readMutation.d.ts +1 -0
  31. package/dist/types/operations/readPostBySlug.d.ts +1 -0
  32. package/dist/types/operations/testTransactionalEdits.d.ts +6 -0
  33. package/dist/types/operations/updateBlockParagraph.d.ts +5 -15
  34. package/dist/types/operations/updatePost.d.ts +7 -21
  35. package/dist/types/operations/updatePostDescription.d.ts +5 -0
  36. package/dist/types/operations/updatePostSlug.d.ts +5 -0
  37. package/dist/types/operations/updatePostTags.d.ts +5 -0
  38. package/dist/types/operations/updatePostTitle.d.ts +5 -17
  39. package/dist/types/schemas/BlockImageRefSchema.d.ts +1 -1
  40. package/dist/types/schemas/BlockImageSchema.d.ts +20 -31
  41. package/dist/types/schemas/BlockParagraphRefSchema.d.ts +1 -1
  42. package/dist/types/schemas/BlockParagraphSchema.d.ts +20 -31
  43. package/dist/types/schemas/BlockRefSchema.d.ts +1 -1
  44. package/dist/types/schemas/BlockRowRefSchema.d.ts +1 -1
  45. package/dist/types/schemas/BlockRowSchema.d.ts +21 -31
  46. package/dist/types/schemas/BlockTitleRefSchema.d.ts +1 -1
  47. package/dist/types/schemas/BlockTitleSchema.d.ts +20 -31
  48. package/dist/types/schemas/CreatePostMutationSchema.d.ts +14 -44
  49. package/dist/types/schemas/CreateUserMutationSchema.d.ts +13 -35
  50. package/dist/types/schemas/DraftRefSchema.d.ts +1 -1
  51. package/dist/types/schemas/DraftSchema.d.ts +18 -31
  52. package/dist/types/schemas/PostRefSchema.d.ts +1 -1
  53. package/dist/types/schemas/PostSchema.d.ts +25 -31
  54. package/dist/types/schemas/UpdatePostMutationSchema.d.ts +16 -50
  55. package/dist/types/schemas/UpdatePostTitleMutationSchema.d.ts +14 -40
  56. package/dist/types/schemas/UserRefSchema.d.ts +1 -1
  57. package/dist/types/schemas/index.d.ts +0 -31
  58. package/dist/types/testAdapterImplementation.d.ts +1 -1
  59. package/package.json +3 -3
  60. package/dist/types/operations/deleteBlock.d.ts +0 -11
  61. package/dist/types/schemas/CreateBlockImageMutationSchema.d.ts +0 -40
  62. package/dist/types/schemas/CreateBlockParagraphMutationSchema.d.ts +0 -40
  63. package/dist/types/schemas/CreateBlockRowMutationSchema.d.ts +0 -40
  64. package/dist/types/schemas/CreateBlockTitleMutationSchema.d.ts +0 -40
  65. package/dist/types/schemas/CreateDraftMutationSchema.d.ts +0 -40
  66. package/dist/types/schemas/DeleteBlockMutationSchema.d.ts +0 -30
  67. package/dist/types/schemas/DeletePostMutationSchema.d.ts +0 -29
  68. package/dist/types/schemas/UpdateBlockParagraphMutationSchema.d.ts +0 -45
package/dist/index.js CHANGED
@@ -61,448 +61,1094 @@ var BlockTitleSchema = entitySchemaGenerator3("BlockTitle", {
61
61
 
62
62
  // src/schemas/DraftSchema.ts
63
63
  import { entitySchemaGenerator as entitySchemaGenerator4 } from "roc-db";
64
+ import { z as z5 } from "zod";
65
+
66
+ // src/entities/Draft.ts
67
+ import { Entity } from "roc-db";
64
68
  import { z as z4 } from "zod";
69
+ var Draft = new Entity("Draft", {
70
+ data: z4.object({
71
+ appliedAt: z4.string().optional()
72
+ }),
73
+ parents: z4.object({
74
+ post: PostRefSchema
75
+ })
76
+ });
65
77
 
66
78
  // src/schemas/DraftRefSchema.ts
67
- import { refSchemaGenerator as refSchemaGenerator5 } from "roc-db";
68
- var DraftRefSchema = refSchemaGenerator5("Draft");
79
+ var DraftRefSchema = Draft.refSchema;
69
80
 
70
81
  // src/schemas/DraftSchema.ts
71
82
  var DraftSchema = entitySchemaGenerator4("Draft", {
72
83
  ref: DraftRefSchema,
73
- data: z4.object({
74
- postRef: PostRefSchema
84
+ parents: z5.object({
85
+ post: PostRefSchema
75
86
  })
76
87
  });
77
88
 
78
89
  // src/schemas/PostSchema.ts
79
90
  import { entitySchemaGenerator as entitySchemaGenerator5 } from "roc-db";
80
- import { z as z5 } from "zod";
91
+ import { z as z6 } from "zod";
81
92
 
82
93
  // src/schemas/UserRefSchema.ts
83
- import { refSchemaGenerator as refSchemaGenerator6 } from "roc-db";
84
- var UserRefSchema = refSchemaGenerator6("User");
94
+ import { refSchemaGenerator as refSchemaGenerator5 } from "roc-db";
95
+ var UserRefSchema = refSchemaGenerator5("User");
85
96
 
86
97
  // src/schemas/PostSchema.ts
87
98
  var PostSchema = entitySchemaGenerator5("Post", {
88
99
  ref: PostRefSchema,
89
- data: z5.object({
90
- title: z5.string(),
91
- text: z5.string().optional(),
92
- tags: z5.array(z5.string()),
93
- slug: z5.string().optional()
100
+ data: z6.object({
101
+ title: z6.string(),
102
+ text: z6.string().optional(),
103
+ tags: z6.array(z6.string()),
104
+ slug: z6.string().optional()
94
105
  }),
95
- parents: z5.object({ author: UserRefSchema.optional() }),
96
- children: z5.object({ blocks: z5.array(z5.string()) })
106
+ parents: z6.object({ author: UserRefSchema.optional() }),
107
+ children: z6.object({ blocks: z6.array(z6.string()) })
97
108
  });
98
109
  // src/schemas/CreatePostMutationSchema.ts
99
110
  import { mutationSchemaGenerator } from "roc-db";
100
- import { z as z6 } from "zod";
101
- var CreatePostMutationSchema = mutationSchemaGenerator("createPost", z6.object({
102
- title: z6.string(),
103
- slug: z6.string().optional(),
104
- tags: z6.array(z6.string()).default([])
111
+ import { z as z7 } from "zod";
112
+ var CreatePostMutationSchema = mutationSchemaGenerator("createPost", z7.object({
113
+ title: z7.string(),
114
+ slug: z7.string().optional(),
115
+ tags: z7.array(z7.string()).default([])
105
116
  }).strict());
106
117
  // src/schemas/CreateUserMutationSchema.ts
107
118
  import { mutationSchemaGenerator as mutationSchemaGenerator2 } from "roc-db";
108
- import { z as z7 } from "zod";
109
- var CreateUserMutationSchema = mutationSchemaGenerator2("createUser", z7.object({ email: z7.string() }));
119
+ import { z as z8 } from "zod";
120
+ var CreateUserMutationSchema = mutationSchemaGenerator2("createUser", z8.object({ email: z8.string() }));
110
121
  // src/schemas/UpdatePostMutationSchema.ts
111
122
  import { mutationSchemaGenerator as mutationSchemaGenerator3 } from "roc-db";
112
- import { z as z8 } from "zod";
113
- var UpdatePostMutationSchema = mutationSchemaGenerator3("updatePost", z8.object({
123
+ import { z as z9 } from "zod";
124
+ var UpdatePostMutationSchema = mutationSchemaGenerator3("updatePost", z9.object({
114
125
  ref: PostRefSchema,
115
- title: z8.string().optional(),
116
- tags: z8.array(z8.string()).optional().default([]),
117
- authorRef: UserRefSchema.optional().optional()
126
+ title: z9.string().optional(),
127
+ tags: z9.array(z9.string()).optional().default([]),
128
+ authorRef: UserRefSchema.optional()
118
129
  }));
119
130
  // src/schemas/UpdatePostTitleMutationSchema.ts
120
131
  import { mutationSchemaGenerator as mutationSchemaGenerator4 } from "roc-db";
121
- import { z as z9 } from "zod";
122
- var UpdatePostTitleMutationSchema = mutationSchemaGenerator4("updatePostTitle", z9.object({ ref: PostRefSchema, title: z9.string() }));
123
- // src/schemas/index.ts
124
- var entities = [
125
- BlockImageSchema,
126
- BlockParagraphSchema,
127
- BlockTitleSchema,
128
- DraftSchema,
129
- PostSchema
130
- ];
131
- // src/operations/createBlockImage.ts
132
- import { Query, writeOperation } from "roc-db";
133
-
134
- // src/schemas/CreateBlockImageMutationSchema.ts
135
- import { mutationSchemaGenerator as mutationSchemaGenerator5 } from "roc-db";
136
132
  import { z as z10 } from "zod";
137
- var CreateBlockImageMutationSchema = mutationSchemaGenerator5("createBlockImage", z10.object({ postRef: PostRefSchema }).strict(), undefined, true);
133
+ var UpdatePostTitleMutationSchema = mutationSchemaGenerator4("updatePostTitle", z10.object({ ref: PostRefSchema, title: z10.string() }));
134
+ // src/entities/BlockImageEntity.ts
135
+ import { Entity as Entity2 } from "roc-db";
136
+ import { z as z11 } from "zod";
137
+ var BlockImageEntity = new Entity2("BlockImage", {
138
+ data: z11.object({
139
+ url: z11.string().url()
140
+ }),
141
+ parents: z11.object({
142
+ parent: PostRefSchema
143
+ })
144
+ });
138
145
 
139
- // src/operations/createBlockImage.ts
140
- var createBlockImage = writeOperation(CreateBlockImageMutationSchema, BlockImageSchema, (txn) => {
141
- const ref = txn.createRef("BlockImage");
142
- const { postRef } = txn.payload;
143
- return Query(() => txn.createEntity(ref, { parents: { post: postRef } }));
144
- }, { changeSetOnly: true });
146
+ // src/entities/BlockParagraph.ts
147
+ import { Entity as Entity4 } from "roc-db";
148
+ import { z as z14 } from "zod";
145
149
 
146
- // src/operations/createBlockParagraph.ts
147
- import { Query as Query2, QueryChain, writeOperation as writeOperation2 } from "roc-db";
150
+ // src/entities/PostEntity.ts
151
+ import { Entity as Entity3 } from "roc-db";
152
+ import { z as z12 } from "zod";
153
+ var PostEntity = new Entity3("Post", {
154
+ data: z12.object({
155
+ title: z12.string(),
156
+ text: z12.string().optional(),
157
+ tags: z12.array(z12.string()),
158
+ slug: z12.string().optional()
159
+ }),
160
+ parents: z12.object({ author: UserRefSchema.optional() }),
161
+ children: z12.object({ blocks: z12.array(z12.string()) }),
162
+ indexedDataKeys: ["tags", "slug"],
163
+ uniqueDataKeys: ["slug"]
164
+ });
148
165
 
149
- // src/schemas/CreateBlockParagraphMutationSchema.ts
150
- import { mutationSchemaGenerator as mutationSchemaGenerator6 } from "roc-db";
151
- import { z as z11 } from "zod";
166
+ // src/schemas/BlockRefSchema.ts
167
+ import { z as z13 } from "zod";
152
168
 
153
169
  // src/schemas/BlockRowRefSchema.ts
154
- import { refSchemaGenerator as refSchemaGenerator7 } from "roc-db";
155
- var BlockRowRefSchema = refSchemaGenerator7("BlockRow");
170
+ import { refSchemaGenerator as refSchemaGenerator6 } from "roc-db";
171
+ var BlockRowRefSchema = refSchemaGenerator6("BlockRow");
156
172
 
157
- // src/schemas/CreateBlockParagraphMutationSchema.ts
158
- var CreateBlockParagraphMutationSchema = mutationSchemaGenerator6("createBlockParagraph", z11.object({ parentRef: z11.union([PostRefSchema, BlockRowRefSchema]) }).strict(), undefined, true);
173
+ // src/schemas/BlockRefSchema.ts
174
+ var BlockRefSchema = z13.union([
175
+ BlockParagraphRefSchema,
176
+ BlockRowRefSchema
177
+ ]);
159
178
 
160
- // src/operations/createBlockParagraph.ts
161
- var createBlockParagrah = writeOperation2(CreateBlockParagraphMutationSchema, BlockParagraphSchema, (txn) => {
162
- const ref = txn.createRef("BlockParagraph");
163
- const { parentRef } = txn.payload;
164
- return QueryChain(Query2(() => txn.readEntity(parentRef, true)), Query2((parent) => txn.patchEntity(parentRef, {
165
- children: { blocks: [...parent.children.blocks, ref] }
166
- })), Query2(() => txn.createEntity(ref, { parents: { parent: parentRef } })), Query2((block, updatedParent) => ({ block, updatedParent })));
179
+ // src/entities/BlockParagraph.ts
180
+ var BlockParagraph = new Entity4("BlockParagraph", {
181
+ data: z14.object({
182
+ content: z14.string().optional()
183
+ }),
184
+ parents: z14.object({
185
+ parent: z14.union([PostEntity.refSchema, BlockRefSchema])
186
+ })
187
+ });
188
+
189
+ // src/entities/BlockRow.ts
190
+ import { Entity as Entity5 } from "roc-db";
191
+ import { z as z15 } from "zod";
192
+ var BlockRow = new Entity5("BlockRow", {
193
+ children: z15.object({
194
+ blocks: z15.array(BlockRefSchema)
195
+ }),
196
+ parents: z15.object({
197
+ parent: z15.union([PostEntity.refSchema, BlockRefSchema])
198
+ })
199
+ });
200
+
201
+ // src/entities/PostVersion.ts
202
+ import { Entity as Entity6 } from "roc-db";
203
+ import { z as z16 } from "zod";
204
+ var PostVersion = new Entity6("PostVersion", {
205
+ data: z16.object({
206
+ version: z16.number(),
207
+ snapshot: z16.array(z16.any())
208
+ }),
209
+ parents: z16.object({ post: PostRefSchema }),
210
+ indexedDataKeys: ["version"],
211
+ uniqueDataKeys: ["slug"]
212
+ });
213
+
214
+ // src/entities/UserEntity.ts
215
+ import { Entity as Entity7 } from "roc-db";
216
+ import { z as z17 } from "zod";
217
+ var UserEntity = new Entity7("User", {
218
+ data: z17.object({
219
+ email: z17.string().email()
220
+ }),
221
+ uniqueDataKeys: ["email"]
222
+ });
223
+
224
+ // src/entities/index.ts
225
+ var entities = [
226
+ BlockImageEntity,
227
+ BlockParagraph,
228
+ BlockRow,
229
+ Draft,
230
+ PostEntity,
231
+ PostVersion,
232
+ UserEntity
233
+ ];
234
+ // src/operations/applyDraft.ts
235
+ import { Query, QueryChain, writeOperation } from "roc-db";
236
+ var applyDraft = writeOperation("applyDraft", DraftRefSchema, (txn) => {
237
+ const ref = txn.payload;
238
+ const versionRef = txn.createRef("PostVersion");
239
+ return QueryChain(Query(() => txn.applyChangeSet(ref)), Query(() => txn.patchEntity(ref, {
240
+ data: { appliedAt: txn.timestamp }
241
+ })), Query((draft) => txn.childRefsOf(draft.parents.post, true)), Query((childRefs, draft) => txn.batchReadEntities([draft.parents.post, ...childRefs])), Query((childEntities, _, draft) => txn.createEntity(versionRef, {
242
+ data: {
243
+ version: 1,
244
+ snapshot: childEntities
245
+ },
246
+ parents: {
247
+ post: draft.parents.post
248
+ }
249
+ })));
250
+ });
251
+
252
+ // src/operations/createBlockImage.ts
253
+ import { Query as Query2, writeOperation as writeOperation2 } from "roc-db";
254
+ import { z as z18 } from "zod";
255
+ var createBlockImage = writeOperation2("createBlockImage", z18.object({ parentRef: PostRefSchema, url: z18.string().url() }).strict(), (txn) => {
256
+ const ref = txn.createRef("BlockImage");
257
+ const { parentRef, url } = txn.payload;
258
+ return Query2(() => txn.createEntity(ref, {
259
+ data: { url },
260
+ parents: { parent: parentRef }
261
+ }));
167
262
  }, { changeSetOnly: true });
168
263
 
169
- // src/operations/createBlockRow.ts
264
+ // src/operations/createBlockParagraph.ts
170
265
  import { Query as Query3, QueryChain as QueryChain2, writeOperation as writeOperation3 } from "roc-db";
171
-
172
- // src/schemas/CreateBlockRowMutationSchema.ts
173
- import { mutationSchemaGenerator as mutationSchemaGenerator7 } from "roc-db";
174
- import { z as z12 } from "zod";
175
- var CreateBlockRowMutationSchema = mutationSchemaGenerator7("createBlockRow", z12.object({ parentRef: z12.union([PostRefSchema, BlockRowRefSchema]) }).strict(), undefined, true);
266
+ import { z as z19 } from "zod";
267
+ var createBlockParagrah = writeOperation3("createBlockParagraph", z19.object({
268
+ parentRef: z19.union([PostRefSchema, BlockRowRefSchema]),
269
+ content: z19.string().optional()
270
+ }).strict(), (txn) => {
271
+ const ref = txn.createRef("BlockParagraph");
272
+ const { parentRef, content } = txn.payload;
273
+ return QueryChain2(Query3(() => txn.readEntity(parentRef, true)), Query3((parent) => txn.patchEntity(parentRef, {
274
+ children: { blocks: [...parent.children.blocks, ref] }
275
+ })), Query3(() => txn.createEntity(ref, {
276
+ parents: { parent: parentRef },
277
+ data: { content }
278
+ })), Query3((block, updatedParent) => ({ block, updatedParent })));
279
+ }, {
280
+ changeSetOnly: true,
281
+ outputSchema: z19.object({
282
+ block: z19.any(),
283
+ updatedParent: z19.any()
284
+ })
285
+ });
176
286
 
177
287
  // src/operations/createBlockRow.ts
178
- var createBlockRow = writeOperation3(CreateBlockRowMutationSchema, BlockParagraphSchema, (txn) => {
288
+ import { Query as Query4, QueryChain as QueryChain3, writeOperation as writeOperation4 } from "roc-db";
289
+ import { z as z20 } from "zod";
290
+ var createBlockRow = writeOperation4("createBlockRow", z20.object({ parentRef: z20.union([PostRefSchema, BlockRowRefSchema]) }).strict(), (txn) => {
179
291
  const ref = txn.createRef("BlockRow");
180
292
  const { parentRef } = txn.payload;
181
- return QueryChain2(Query3(() => txn.readEntity(parentRef)), Query3((parent) => {
293
+ return QueryChain3(Query4(() => txn.readEntity(parentRef)), Query4((parent) => {
182
294
  return txn.patchEntity(parentRef, {
183
295
  children: { blocks: [...parent.children.blocks, ref] }
184
296
  });
185
- }), Query3(() => txn.createEntity(ref, {
297
+ }), Query4(() => txn.createEntity(ref, {
186
298
  children: { blocks: [] },
187
299
  parents: { parent: parentRef }
188
- })), Query3((block, updatedParent) => ({ block, updatedParent })));
189
- }, { changeSetOnly: true });
190
-
191
- // src/operations/createBlockTitle.ts
192
- import { Query as Query4, writeOperation as writeOperation4 } from "roc-db";
193
-
194
- // src/schemas/CreateBlockTitleMutationSchema.ts
195
- import { mutationSchemaGenerator as mutationSchemaGenerator8 } from "roc-db";
196
- import { z as z13 } from "zod";
197
- var CreateBlockTitleMutationSchema = mutationSchemaGenerator8("createBlockTitle", z13.object({ parentRef: z13.union([PostRefSchema, BlockRowRefSchema]) }).strict(), undefined, true);
300
+ })), Query4((block, updatedParent) => ({ block, updatedParent })));
301
+ }, {
302
+ changeSetOnly: true,
303
+ outputSchema: z20.object({
304
+ block: z20.any(),
305
+ updatedParent: z20.any()
306
+ })
307
+ });
198
308
 
199
309
  // src/operations/createBlockTitle.ts
200
- var createBlockTitle = writeOperation4(CreateBlockTitleMutationSchema, BlockTitleSchema, (txn) => {
310
+ import { Query as Query5, writeOperation as writeOperation5 } from "roc-db";
311
+ import { z as z21 } from "zod";
312
+ var PayloadSchema = z21.object({ parentRef: z21.union([PostRefSchema, BlockRowRefSchema]) }).strict();
313
+ var createBlockTitle = writeOperation5("createBlockTitle", PayloadSchema, (txn) => {
201
314
  const ref = txn.createRef("BlockTitle");
202
315
  const { postRef } = txn.payload;
203
- return Query4(() => txn.createEntity(ref, { parents: { post: postRef } }));
204
- }, { changeSetOnly: true });
316
+ return Query5(() => txn.createEntity(ref, { parents: { post: postRef } }));
317
+ }, { changeSetOnly: true, outputSchema: BlockTitleSchema });
205
318
 
206
319
  // src/operations/createDraft.ts
207
- import { Query as Query5, writeOperation as writeOperation5 } from "roc-db";
208
-
209
- // src/schemas/CreateDraftMutationSchema.ts
210
- import { mutationSchemaGenerator as mutationSchemaGenerator9 } from "roc-db";
211
- import { z as z14 } from "zod";
212
- var CreateDraftMutationSchema = mutationSchemaGenerator9("createDraft", z14.object({ postRef: PostRefSchema }).strict());
213
-
214
- // src/operations/createDraft.ts
215
- var createDraft = writeOperation5(CreateDraftMutationSchema, DraftSchema, (txn) => {
320
+ import { Query as Query6, writeOperation as writeOperation6 } from "roc-db";
321
+ import { z as z22 } from "zod";
322
+ var createDraft = writeOperation6("createDraft", z22.object({ postRef: PostRefSchema }).strict(), (txn) => {
216
323
  const ref = txn.createRef("Draft");
217
324
  const { postRef } = txn.payload;
218
- return Query5(() => txn.createEntity(ref, {
325
+ return Query6(() => txn.createEntity(ref, {
219
326
  parents: {
220
327
  post: postRef
221
328
  }
222
329
  }));
223
- });
330
+ }, { outputSchema: DraftSchema });
224
331
 
225
332
  // src/operations/createPost.ts
226
- import { Query as Query6, writeOperation as writeOperation6 } from "roc-db";
227
- var createPost = writeOperation6(CreatePostMutationSchema, PostSchema, (txn) => {
333
+ import { Query as Query7, writeOperation as writeOperation7 } from "roc-db";
334
+ import { z as z23 } from "zod";
335
+ var PayloadSchema2 = z23.object({
336
+ title: z23.string(),
337
+ slug: z23.string().optional(),
338
+ tags: z23.array(z23.string()).default([])
339
+ }).strict();
340
+ var createPost = writeOperation7("createPost", PayloadSchema2, (txn) => {
228
341
  const ref = txn.createRef("Post");
229
342
  const { title, slug, tags } = txn.payload;
230
- return Query6(() => txn.createEntity(ref, {
343
+ return Query7(() => txn.createEntity(ref, {
231
344
  data: { title, tags, slug },
232
345
  children: { blocks: [] }
233
346
  }));
234
347
  });
235
348
 
236
- // src/operations/deleteBlock.ts
237
- import { Query as Query7, QueryChain as QueryChain3, writeOperation as writeOperation7 } from "roc-db";
238
-
239
- // src/schemas/DeleteBlockMutationSchema.ts
240
- import { mutationSchemaGenerator as mutationSchemaGenerator10 } from "roc-db";
241
- import { z as z15 } from "zod";
242
- var DeleteBlockMutationSchema = mutationSchemaGenerator10("deleteBlock", z15.union([BlockParagraphRefSchema, BlockImageRefSchema]));
349
+ // src/operations/createUser.ts
350
+ import { Query as Query8, writeOperation as writeOperation8 } from "roc-db";
351
+ import { z as z24 } from "zod";
352
+ var createUser = writeOperation8("createUser", z24.object({ email: z24.string() }), (txn) => {
353
+ const ref = txn.createRef("User");
354
+ const { email } = txn.payload;
355
+ return Query8(() => txn.createEntity(ref, { data: { email } }));
356
+ });
243
357
 
244
- // src/operations/deleteBlock.ts
245
- var deleteBlock = writeOperation7(DeleteBlockMutationSchema, PostSchema, (txn) => {
246
- const blockRef = txn.payload;
247
- return QueryChain3(Query7(() => txn.readEntity(blockRef, true)), Query7((block) => txn.readEntity(block.parents.post, true)), Query7((post) => {
358
+ // src/operations/deleteBlocks.ts
359
+ import { Query as Query9, QueryChain as QueryChain4, writeOperation as writeOperation9 } from "roc-db";
360
+ import { z as z25 } from "zod";
361
+ var DeleteBlockQuery = (txn, blockRef) => {
362
+ return QueryChain4(Query9(() => txn.readEntity(blockRef)), Query9((block) => {
363
+ return txn.readEntity(block.parents.parent, true);
364
+ }), Query9((post) => {
248
365
  return txn.patchEntity(post.ref, {
249
- data: {
250
- blockRefs: post.data.blockRefs.filter((ref) => ref !== blockRef)
366
+ children: {
367
+ blocks: post.children.blocks.filter((ref) => ref !== blockRef)
251
368
  }
252
369
  });
253
- }), Query7(() => txn.deleteEntity(blockRef)), Query7((_, updatedPost) => {
254
- return updatedPost;
255
- }));
370
+ }), Query9(() => txn.deleteEntity(blockRef)));
371
+ };
372
+ var deleteBlocks = writeOperation9("deleteBlocks", z25.array(z25.union([
373
+ BlockParagraphRefSchema,
374
+ BlockImageRefSchema,
375
+ BlockRowRefSchema
376
+ ])), (txn) => {
377
+ const refs = txn.payload;
378
+ return QueryChain4(...refs.map((ref) => DeleteBlockQuery(txn, ref)));
256
379
  }, { changeSetOnly: true });
257
380
 
258
- // src/operations/deletePost.ts
259
- import { Query as Query8, writeOperation as writeOperation8 } from "roc-db";
260
-
261
- // src/schemas/DeletePostMutationSchema.ts
262
- import { mutationSchemaGenerator as mutationSchemaGenerator11 } from "roc-db";
263
- var DeletePostMutationSchema = mutationSchemaGenerator11("deletePost", PostRefSchema);
381
+ // src/operations/deleteDraft.ts
382
+ import { Query as Query10, QueryChain as QueryChain5, writeOperation as writeOperation10 } from "roc-db";
383
+ var deleteDraft = writeOperation10("deleteDraft", DraftRefSchema, (txn) => {
384
+ const ref = txn.payload;
385
+ return QueryChain5(Query10(() => txn.deleteEntity(ref, true)), Query10(() => txn.deleteChangeSet(ref)));
386
+ });
264
387
 
265
388
  // src/operations/deletePost.ts
266
- var deletePost = writeOperation8(DeletePostMutationSchema, undefined, (txn) => {
389
+ import { Query as Query11, writeOperation as writeOperation11 } from "roc-db";
390
+ var deletePost = writeOperation11("deletePost", PostRefSchema, (txn) => {
267
391
  const ref = txn.payload;
268
- return Query8(() => txn.deleteEntity(ref, true));
392
+ return Query11(() => txn.deleteEntity(ref, true));
393
+ });
394
+
395
+ // src/operations/moveBlocks.ts
396
+ import { Query as Query12, QueryChain as QueryChain6, writeOperation as writeOperation12 } from "roc-db";
397
+ import { z as z26 } from "zod";
398
+ var PayloadSchema3 = z26.object({
399
+ refs: z26.array(z26.union([BlockParagraphRefSchema, BlockRowRefSchema])),
400
+ targetPosition: z26.number().min(0)
401
+ }).strict();
402
+ var moveBlocks = writeOperation12("moveBlocks", PayloadSchema3, (txn) => {
403
+ const { refs } = txn.payload;
404
+ return QueryChain6(Query12(() => txn.batchReadEntities(refs)), Query12((items) => {
405
+ return "todo";
406
+ }));
407
+ });
408
+
409
+ // src/operations/pageBlocks.ts
410
+ import { Query as Query13, readOperation } from "roc-db";
411
+ import { z as z27 } from "zod";
412
+ var pageBlocks = readOperation("pageBlocks", z27.object({
413
+ size: z27.number().default(100),
414
+ parentRef: z27.string().optional()
415
+ }), (txn) => {
416
+ const { size, parentRef } = txn.payload;
417
+ return Query13(() => txn.pageEntities({
418
+ entities: ["BlockImage", "BlockParagraph"],
419
+ size,
420
+ childrenOf: parentRef ? [parentRef] : []
421
+ }));
422
+ });
423
+
424
+ // src/operations/pageEmptyEntitiesArray.ts
425
+ import { Query as Query14, readOperation as readOperation2 } from "roc-db";
426
+ import { z as z28 } from "zod";
427
+ var pageEmptyEntitiesArray = readOperation2("pageEmptyEntitiesArray", z28.object({ size: z28.number().default(100) }), (txn) => {
428
+ const { size } = txn.payload;
429
+ return Query14(() => txn.pageEntities({ entities: [], size }));
430
+ });
431
+
432
+ // src/operations/pagePosts.ts
433
+ import { Query as Query15, readOperation as readOperation3 } from "roc-db";
434
+ import { z as z29 } from "zod";
435
+ var pagePosts = readOperation3("pagePosts", z29.object({ size: z29.number().default(100) }).default({}), (txn) => {
436
+ const { size } = txn.payload;
437
+ return Query15(() => txn.pageEntities({ entities: ["Post"], size }));
438
+ });
439
+
440
+ // src/operations/pageSplat.ts
441
+ import { Query as Query16, readOperation as readOperation4 } from "roc-db";
442
+ import { z as z30 } from "zod";
443
+ var pageSplat = readOperation4("pageSplat", z30.object({ size: z30.number().default(100) }), (txn) => {
444
+ const { size } = txn.payload;
445
+ return Query16(() => txn.pageEntities({ entities: "*", size }));
446
+ });
447
+
448
+ // src/operations/pagePostsByTag.ts
449
+ import { Query as Query17, readOperation as readOperation5 } from "roc-db";
450
+ import { z as z31 } from "zod";
451
+ var pagePostsByTag = readOperation5("pagePostsByTag", z31.string(), (txn) => {
452
+ const tag = txn.payload;
453
+ return Query17(() => txn.pageEntitiesByIndex("Post", "tags", tag));
269
454
  });
270
455
 
271
456
  // src/operations/readEntity.ts
272
- import { NotFoundError, Query as Query9, QueryChain as QueryChain4, readOperation } from "roc-db";
273
- import { z as z16 } from "zod";
274
- var readEntity = readOperation("readEntity", z16.string(), z16.union([PostSchema, BlockImageSchema, BlockParagraphSchema]), (txn) => {
457
+ import { NotFoundError, Query as Query18, QueryChain as QueryChain7, readOperation as readOperation6 } from "roc-db";
458
+ import { z as z32 } from "zod";
459
+ var readEntity = readOperation6("readEntity", z32.string(), (txn) => {
275
460
  const ref = txn.payload;
276
- return QueryChain4(Query9(() => txn.readEntity(ref)), Query9((entity) => {
461
+ return QueryChain7(Query18(() => txn.readEntity(ref)), Query18((entity) => {
277
462
  if (!entity) {
278
463
  throw new NotFoundError(ref);
279
464
  }
280
465
  return entity;
281
466
  }));
467
+ }, {
468
+ outputSchema: z32.union([
469
+ PostSchema,
470
+ BlockImageSchema,
471
+ BlockParagraphSchema
472
+ ])
282
473
  });
283
474
 
284
- // src/operations/readPost.ts
285
- import { Query as Query10, readOperation as readOperation2 } from "roc-db";
286
- var readPost = readOperation2("readPost", PostRefSchema, PostSchema, (txn) => {
475
+ // src/operations/readMutation.ts
476
+ import { NotFoundError as NotFoundError2, Query as Query19, QueryChain as QueryChain8, readOperation as readOperation7 } from "roc-db";
477
+ import { z as z33 } from "zod";
478
+ var readMutation = readOperation7("readMutation", z33.string(), (txn) => {
287
479
  const ref = txn.payload;
288
- return Query10(() => txn.readEntity(ref));
480
+ return QueryChain8(Query19(() => txn.readMutation(ref)), Query19((entity) => {
481
+ if (!entity) {
482
+ throw new NotFoundError2(ref);
483
+ }
484
+ return entity;
485
+ }));
289
486
  });
290
487
 
291
- // src/operations/updateBlockParagraph.ts
292
- import { Query as Query11, writeOperation as writeOperation9 } from "roc-db";
488
+ // src/operations/readPost.ts
489
+ import { Query as Query20, readOperation as readOperation8 } from "roc-db";
490
+ var readPost = readOperation8("readPost", PostRefSchema, (txn) => {
491
+ const ref = txn.payload;
492
+ return Query20(() => txn.readEntity(ref, true));
493
+ }, { outputSchema: PostSchema });
293
494
 
294
- // src/schemas/UpdateBlockParagraphMutationSchema.ts
295
- import { mutationSchemaGenerator as mutationSchemaGenerator12 } from "roc-db";
296
- import { z as z17 } from "zod";
297
- var UpdateBlockParagraphMutationSchema = mutationSchemaGenerator12("updateBlockParagraph", z17.object({
298
- ref: BlockParagraphRefSchema,
299
- content: z17.string()
300
- }));
495
+ // src/operations/readPostBySlug.ts
496
+ import { Query as Query21, readOperation as readOperation9 } from "roc-db";
497
+ import { z as z34 } from "zod";
498
+ var readPostBySlug = readOperation9("readPostBySlug", z34.string(), (txn) => {
499
+ const slug = txn.payload;
500
+ return Query21(() => txn.readEntityByUniqueField("Post", "slug", slug));
501
+ }, { outputSchema: PostSchema });
502
+
503
+ // src/operations/testTransactionalEdits.ts
504
+ import { expect } from "bun:test";
505
+ import { Query as Query22, QueryChain as QueryChain9, writeOperation as writeOperation13 } from "roc-db";
506
+ import { z as z35 } from "zod";
507
+ var TestCreatePatchDeleteInSameTransaction = (txn, title) => {
508
+ const postRef = txn.createRef("Post");
509
+ return QueryChain9(Query22(() => txn.createEntity(postRef, {
510
+ data: { title, tags: [] },
511
+ children: { blocks: [] }
512
+ })), Query22(() => txn.patchEntity(postRef, { data: { foo: "bar" } })), Query22((post) => {
513
+ expect(post.data.title).toBe(title);
514
+ expect(post.data.foo).toBe("bar");
515
+ }), Query22(() => txn.patchEntity(postRef, { data: { foo: null, bar: "foo" } })), Query22((post) => {
516
+ expect(post.data.title).toBe(title);
517
+ expect(post.data.foo).toBeUndefined();
518
+ expect(post.data.bar).toBe("foo");
519
+ expect(txn.log).toHaveLength(1);
520
+ expect(txn.log.get(postRef)[0]).toBe("create");
521
+ }), Query22((post) => txn.deleteEntity(postRef)), Query22((res) => {
522
+ expect(res).toBe(1);
523
+ expect(txn.log).toHaveLength(0);
524
+ }));
525
+ };
526
+ var TestUpdateAndDeleteInSameTransaction = (txn, ref) => {
527
+ return QueryChain9(Query22(() => txn.patchEntity(ref, { data: { foo: "bar" } })), Query22((post) => {
528
+ expect(post.data.foo).toBe("bar");
529
+ }), Query22(() => txn.patchEntity(ref, { data: { foo: null, bar: "foo" } })), Query22((post) => {
530
+ expect(post.data.foo).toBeUndefined();
531
+ expect(post.data.bar).toBe("foo");
532
+ expect(txn.log).toHaveLength(1);
533
+ expect(txn.log.get(ref)[0]).toBe("update");
534
+ }), Query22((post) => txn.deleteEntity(ref)), Query22((res) => {
535
+ expect(res).toBe(1);
536
+ expect(txn.log).toHaveLength(1);
537
+ expect(txn.log.get(ref)[0]).toBe("delete");
538
+ }));
539
+ };
540
+ var TestCreateOnExistingRef = (txn) => {
541
+ const ref = txn.createRef("Post");
542
+ return QueryChain9(Query22(() => txn.createEntity(ref, { data: { foo: "bar" } })), Query22(() => txn.createEntity(ref, { data: { foo: "bar" } })));
543
+ };
544
+ var PayloadSchema4 = z35.object({
545
+ name: z35.string().optional(),
546
+ post1ref: PostRefSchema.optional(),
547
+ post2ref: PostRefSchema.optional()
548
+ }).strict();
549
+ var testTransactionalEdits = writeOperation13("testTransactionalEdits", PayloadSchema4, (txn) => {
550
+ const { name, post1ref, post2ref } = txn.payload;
551
+ if (name)
552
+ return TestCreatePatchDeleteInSameTransaction(txn, name);
553
+ if (post1ref)
554
+ return TestUpdateAndDeleteInSameTransaction(txn, post1ref);
555
+ if (post2ref)
556
+ return TestCreateOnExistingRef(txn);
557
+ }, {});
301
558
 
302
559
  // src/operations/updateBlockParagraph.ts
303
- var updateBlockParagraph = writeOperation9(UpdateBlockParagraphMutationSchema, BlockParagraphRefSchema, (txn) => {
560
+ import { Query as Query23, writeOperation as writeOperation14 } from "roc-db";
561
+ import { z as z36 } from "zod";
562
+ var updateBlockParagraph = writeOperation14("updateBlockParagraph", z36.object({
563
+ ref: BlockParagraphRefSchema,
564
+ content: z36.string()
565
+ }), (txn) => {
304
566
  const { ref, content } = txn.payload;
305
- return Query11(() => txn.patchEntity(ref, {
567
+ return Query23(() => txn.patchEntity(ref, {
306
568
  data: { content }
307
569
  }));
308
- });
570
+ }, { outputSchema: BlockParagraphSchema });
309
571
 
310
572
  // src/operations/updatePost.ts
311
- import { Query as Query12, writeOperation as writeOperation10 } from "roc-db";
312
- var updatePost = writeOperation10(UpdatePostMutationSchema, PostSchema, (txn) => {
573
+ import { Query as Query24, writeOperation as writeOperation15 } from "roc-db";
574
+ import { z as z37 } from "zod";
575
+ var PayloadSchema5 = z37.object({
576
+ ref: PostRefSchema,
577
+ title: z37.string().optional(),
578
+ tags: z37.array(z37.string()).optional().default([]),
579
+ authorRef: UserRefSchema.optional()
580
+ });
581
+ var updatePost = writeOperation15("updatePost", PayloadSchema5, (txn) => {
313
582
  const { ref, authorRef, ...data } = txn.payload;
314
- return Query12(() => txn.updateEntity(ref, {
583
+ return Query24(() => txn.updateEntity(ref, {
315
584
  data,
316
585
  parents: { author: authorRef },
317
586
  children: { blocks: [] }
318
587
  }));
319
- });
588
+ }, { outputSchema: PostSchema });
589
+
590
+ // src/operations/updatePostDescription.ts
591
+ import { Query as Query25, writeOperation as writeOperation16 } from "roc-db";
592
+ import { z as z38 } from "zod";
593
+ var updatePostDescription = writeOperation16("updatePostDescription", z38.object({ ref: PostRefSchema, description: z38.string() }), (txn) => {
594
+ const { ref, description } = txn.payload;
595
+ return Query25(() => txn.patchEntity(ref, { data: { description } }));
596
+ }, { debounce: 10, changeSetOnly: true, outputSchema: PostSchema });
597
+
598
+ // src/operations/updatePostSlug.ts
599
+ import { Query as Query26, writeOperation as writeOperation17 } from "roc-db";
600
+ import { z as z39 } from "zod";
601
+ var updatePostSlug = writeOperation17("updatePostSlug", z39.object({ ref: PostRefSchema, slug: z39.string() }), (txn) => {
602
+ const { ref, slug } = txn.payload;
603
+ return Query26(() => txn.patchEntity(ref, { data: { slug } }));
604
+ }, { debounce: 10, outputSchema: PostSchema });
605
+
606
+ // src/operations/updatePostTags.ts
607
+ import { Query as Query27, writeOperation as writeOperation18 } from "roc-db";
608
+ import { z as z40 } from "zod";
609
+ var updatePostTags = writeOperation18("updatePostTags", z40.object({ ref: PostRefSchema, tags: z40.array(z40.string()) }), (txn) => {
610
+ const { ref, tags } = txn.payload;
611
+ return Query27(() => txn.patchEntity(ref, { data: { tags } }));
612
+ }, { debounce: 10, outputSchema: PostSchema });
320
613
 
321
614
  // src/operations/updatePostTitle.ts
322
- import { Query as Query13, writeOperation as writeOperation11 } from "roc-db";
323
- var updatePostTitle = writeOperation11(UpdatePostTitleMutationSchema, PostSchema, (txn) => {
615
+ import { Query as Query28, writeOperation as writeOperation19 } from "roc-db";
616
+ import { z as z41 } from "zod";
617
+ var updatePostTitle = writeOperation19("updatePostTitle", z41.object({ ref: PostRefSchema, title: z41.string() }), (txn) => {
324
618
  const { ref, title } = txn.payload;
325
- return Query13(() => txn.patchEntity(ref, { data: { title } }));
326
- }, { debounce: 10 });
619
+ return Query28(() => txn.patchEntity(ref, { data: { title } }));
620
+ }, { debounce: 10, outputSchema: PostSchema });
621
+
622
+ // src/operations/crudBySlug.ts
623
+ import { Query as Query29, QueryChain as QueryChain10, writeOperation as writeOperation20 } from "roc-db";
624
+ import { z as z42 } from "zod";
625
+ var crudBySlug = writeOperation20("crudBySlug", z42.object({ slug: z42.string(), newSlug: z42.string() }), (txn) => {
626
+ const { slug, newSlug } = txn.payload;
627
+ return QueryChain10(Query29(() => txn.readEntityByUniqueField("Post", "slug", slug)), Query29((post) => txn.patchEntity(post.ref, { data: { slug: newSlug } })), Query29(() => txn.readEntityByUniqueField("Post", "slug", newSlug)), Query29((post) => txn.deleteEntity(post.ref)), Query29(() => {
628
+ return [
629
+ txn.readEntityByUniqueField("Post", "slug", slug, false),
630
+ txn.readEntityByUniqueField("Post", "slug", newSlug, false)
631
+ ];
632
+ }));
633
+ });
327
634
 
328
635
  // src/operations/index.ts
329
636
  var operations = [
637
+ applyDraft,
330
638
  createBlockImage,
331
639
  createBlockTitle,
332
640
  createBlockParagrah,
333
641
  createBlockRow,
334
642
  createDraft,
335
643
  createPost,
336
- deleteBlock,
644
+ createUser,
645
+ crudBySlug,
646
+ deleteBlocks,
647
+ deleteDraft,
337
648
  deletePost,
649
+ readPostBySlug,
650
+ moveBlocks,
651
+ pagePostsByTag,
652
+ pageBlocks,
338
653
  readEntity,
654
+ readMutation,
339
655
  readPost,
656
+ pageEmptyEntitiesArray,
657
+ pagePosts,
658
+ pageSplat,
659
+ testTransactionalEdits,
340
660
  updateBlockParagraph,
341
661
  updatePost,
662
+ updatePostDescription,
663
+ updatePostSlug,
664
+ updatePostTags,
342
665
  updatePostTitle
343
666
  ];
344
667
  // src/testAdapterImplementation.ts
345
- import { beforeAll, describe, expect, test } from "bun:test";
346
- import { BadRequestError, NotFoundError as NotFoundError2 } from "roc-db";
347
- var prepareChangeSetTest = async (adapter) => {
348
- const [post] = await adapter.createPost({
668
+ import { beforeAll, describe, expect as expect2, test } from "bun:test";
669
+ import {
670
+ BadRequestError,
671
+ ConflictError,
672
+ NotFoundError as NotFoundError3,
673
+ Snowflake
674
+ } from "roc-db";
675
+ import { faker } from "@faker-js/faker";
676
+ var snowflake = new Snowflake(10, 10);
677
+ var prepareChangeSetTest = async (adapter, adapter2) => {
678
+ const [post, createPostMutation] = await adapter.createPost({
349
679
  title: "Title 1",
350
- slug: "title-1",
680
+ slug: faker.lorem.slug(5),
351
681
  tags: ["Foo", "Bar"]
352
682
  });
353
- const [draft] = await adapter.createDraft({
683
+ expect2(createPostMutation.log).toStrictEqual([[post.ref, "create"]]);
684
+ const [draft, createDraftMutation] = await adapter.createDraft({
354
685
  postRef: post.ref
355
686
  });
687
+ expect2(createDraftMutation.log).toStrictEqual([[draft.ref, "create"]]);
356
688
  const changeSetAdapter = adapter.changeSet(draft.ref);
357
- const [{ block: blockParagraph1 }] = await changeSetAdapter.createBlockParagraph({
689
+ const [
690
+ { block: blockParagraph1, updatedParent: updatedPost },
691
+ createBlockParagraphMutation
692
+ ] = await changeSetAdapter.createBlockParagraph({
358
693
  parentRef: post.ref
359
694
  });
360
- const [{ block: blockRow, updatedParent }] = await changeSetAdapter.createBlockRow({
695
+ expect2(updatedPost.children.blocks).toStrictEqual([blockParagraph1.ref]);
696
+ expect2(updatedPost.children.blocks).toHaveLength(1);
697
+ expect2(createBlockParagraphMutation.log[1][2].children.blocks).toStrictEqual([]);
698
+ const readPost1 = await changeSetAdapter.readEntity(post.ref);
699
+ expect2(readPost1.children.blocks).toHaveLength(1);
700
+ const [{ block: blockRow, updatedParent: updatedPost2 }, mutation] = await changeSetAdapter.createBlockRow({
361
701
  parentRef: post.ref
362
702
  });
363
- const [{ block: blockParagraph2 }] = await changeSetAdapter.createBlockParagraph({
703
+ expect2(updatedPost2.children.blocks).toHaveLength(2);
704
+ const readPost2 = await changeSetAdapter.readEntity(post.ref);
705
+ expect2(readPost2.children.blocks).toHaveLength(2);
706
+ const [{ block: blockParagraph2, updatedParent: updatedBlockRow }] = await changeSetAdapter.createBlockParagraph({
364
707
  parentRef: blockRow.ref
365
708
  });
709
+ const readPost3 = await changeSetAdapter.readEntity(post.ref);
710
+ expect2(readPost3.children.blocks).toHaveLength(2);
366
711
  return {
367
712
  postRef: post.ref,
368
713
  draftRef: draft.ref,
369
714
  changeSetAdapter,
370
715
  blockParagraph1,
371
716
  blockParagraph2,
372
- blockRow,
373
- post: updatedParent
717
+ blockRow: updatedBlockRow,
718
+ draft,
719
+ post: readPost3
374
720
  };
375
721
  };
376
- var testAdapterImplementation = (adapterConstructor, engineArgsAdapter1 = {}, engineArgsAdapter2 = {}) => {
377
- return async () => {
378
- const adapter1 = adapterConstructor({ operations, entities, ...engineArgsAdapter1 });
379
- const adapter2 = adapterConstructor({ operations, entities, ...engineArgsAdapter2 });
380
- describe("Post CRUD", async () => {
381
- let postRef;
382
- let post;
383
- let createPostMutation;
384
- beforeAll(async () => {
385
- const createPostRes = await adapter1.createPost({
386
- title: "Title 1",
387
- slug: "title-1",
388
- tags: ["Foo", "Bar"]
389
- });
390
- post = createPostRes[0];
391
- postRef = createPostRes[0].ref;
392
- createPostMutation = createPostRes[1];
393
- });
394
- test("create (createEntity function)", async () => {
395
- expect(post).toMatchZodSchema(PostSchema);
396
- expect(post.data.title).toBe("Title 1");
397
- expect(post.data.tags).toStrictEqual(["Foo", "Bar"]);
398
- });
399
- test("read (readEntity function)", async () => {
400
- const readPostRes = await adapter1.readPost(postRef);
401
- expect(readPostRes.data.title).toStrictEqual("Title 1");
402
- expect(readPostRes.data.tags).toStrictEqual(["Foo", "Bar"]);
403
- expect(readPostRes).toMatchZodSchema(PostSchema);
404
- });
405
- test("update (updateEntity function)", async () => {
406
- const [post2] = await adapter1.updatePost({
407
- ref: postRef,
408
- title: "Title 2",
409
- tags: ["Foo"]
410
- });
411
- expect(post2.data.tags).toStrictEqual(["Foo"]);
412
- expect(post2.data.title).toBe("Title 2");
413
- expect(post2).toMatchZodSchema(PostSchema);
414
- });
415
- test("updateTitle (patchEntity function + debounce)", async () => {
416
- const updatePostTitleRes1 = await adapter1.updatePostTitle({
417
- ref: postRef,
418
- title: "Title 3"
419
- });
420
- expect(updatePostTitleRes1[0]).toMatchZodSchema(PostSchema);
421
- const updatePostTitleRes2 = await adapter1.updatePostTitle({
422
- ref: postRef,
423
- title: "Title 4"
424
- });
425
- expect(updatePostTitleRes2[0]).toMatchZodSchema(PostSchema);
426
- expect(updatePostTitleRes2[1].ref).toBe(updatePostTitleRes1[1].ref);
427
- expect(updatePostTitleRes2[1].timestamp >= updatePostTitleRes1[1].timestamp).toBeTrue();
428
- });
429
- test("delete (deleteEntity function)", async () => {
430
- const [count, mutation] = await adapter1.deletePost(postRef);
431
- expect(count).toBe(1);
432
- expect(mutation.log).toStrictEqual([[postRef, "delete", null]]);
433
- });
434
- test("sync mutation", async () => {
435
- const res = await adapter2.syncOptimisticMutation(createPostMutation);
436
- expect(res[0]).toStrictEqual(post);
437
- expect(res[1]).toStrictEqual(createPostMutation);
438
- expect(() => adapter2.syncOptimisticMutation(createPostMutation)).toThrowError(BadRequestError);
439
- });
440
- });
441
- describe("changeSet", async () => {
442
- let postRef, draftRef, changeSetAdapter, blockParagraph1, post, blockRow, blockParagraph2;
443
- beforeAll(async () => {
444
- ({
445
- postRef,
446
- draftRef,
447
- changeSetAdapter,
448
- blockParagraph1,
449
- post,
450
- blockRow,
451
- blockParagraph2
452
- } = await prepareChangeSetTest(adapter1));
453
- });
454
- test("create (createEntity function w/changeSetRef)", async () => {
455
- expect(blockParagraph1).toMatchZodSchema(BlockParagraphSchema);
456
- expect(post).toMatchZodSchema(PostSchema);
457
- });
458
- test("read (readEntity function w/changeSetRef)", async () => {
459
- const read = await changeSetAdapter.readEntity(blockParagraph1.ref);
460
- expect(read).toMatchZodSchema(BlockParagraphSchema);
461
- });
462
- test("update (patchEntity function w/changeSetRef)", async () => {
463
- const [updatedBlock] = await changeSetAdapter.updateBlockParagraph({
464
- ref: blockParagraph1.ref,
465
- content: "Hello, world!"
466
- });
467
- expect(updatedBlock).toMatchZodSchema(BlockParagraphSchema);
468
- });
469
- test("delete (with cascade delete)", async () => {
470
- const [count, mutation] = await changeSetAdapter.deletePost(postRef);
471
- expect(count).toBe(4);
472
- expect(mutation.log).toStrictEqual([
473
- [blockParagraph1.ref, "delete", draftRef],
474
- [blockRow.ref, "delete", draftRef],
475
- [blockParagraph2.ref, "delete", draftRef],
476
- [postRef, "delete", draftRef]
477
- ]);
478
- await expect(() => changeSetAdapter.readEntity(blockParagraph1.ref)).toThrow(NotFoundError2);
479
- await expect(() => changeSetAdapter.updateBlockParagraph({
480
- ref: blockParagraph1.ref,
481
- content: "Foo"
482
- })).toThrow(NotFoundError2);
483
- });
484
- test("pageMutations", async () => {
485
- const res = await changeSetAdapter.pageMutations({
486
- changeSetRef: draftRef
487
- });
488
- expect(res.length).toBeGreaterThan(0);
489
- expect(res.every((m) => m.changeSetRef === draftRef)).toBeTrue();
490
- });
491
- test("pageMutations2", async () => {
492
- const res = await changeSetAdapter.pageMutations({
493
- changeSetRef: draftRef
494
- });
495
- expect(res.length).toBeGreaterThan(0);
496
- expect(res.every((m) => m.changeSetRef === draftRef)).toBeTrue();
497
- });
722
+ var testAdapterImplementation = async (adapterConstructor, generateArgs) => {
723
+ let createAdapter = async ({ optimistic = false } = {}) => {
724
+ return adapterConstructor({
725
+ operations,
726
+ entities,
727
+ session: { identityRef: "User/42" },
728
+ snowflake,
729
+ optimistic,
730
+ ...await generateArgs()
498
731
  });
499
732
  };
733
+ let adapter1, adapter2;
734
+ beforeAll(async () => {
735
+ adapter1 = await createAdapter();
736
+ adapter2 = await createAdapter();
737
+ });
738
+ describe("Post CRUD", () => {
739
+ let postRef;
740
+ let post;
741
+ let createPostMutation;
742
+ beforeAll(async () => {
743
+ const createPostRes = await adapter1.createPost({
744
+ title: "Title 1",
745
+ slug: faker.lorem.slug(5),
746
+ tags: ["Foo", "Bar"]
747
+ });
748
+ post = createPostRes[0];
749
+ postRef = createPostRes[0].ref;
750
+ createPostMutation = createPostRes[1];
751
+ });
752
+ test("create (createEntity function)", async () => {
753
+ expect2(post).toMatchZodSchema(PostEntity.schema);
754
+ expect2(post.data.title).toBe("Title 1");
755
+ expect2(post.data.tags).toStrictEqual(["Foo", "Bar"]);
756
+ expect2(createPostMutation).toMatchZodSchema(CreatePostMutationSchema);
757
+ });
758
+ test("read (readEntity function)", async () => {
759
+ const readPostRes = await adapter1.readPost(postRef);
760
+ expect2(readPostRes.data.title).toStrictEqual("Title 1");
761
+ expect2(readPostRes.data.tags).toStrictEqual(["Foo", "Bar"]);
762
+ expect2(readPostRes).toMatchZodSchema(PostEntity.schema);
763
+ });
764
+ test("read by unique", async () => {
765
+ const readPostRes = await adapter1.readPostBySlug(post.data.slug);
766
+ expect2(readPostRes.data.title).toStrictEqual("Title 1");
767
+ expect2(readPostRes.data.tags).toStrictEqual(["Foo", "Bar"]);
768
+ expect2(readPostRes.data.slug).toStrictEqual(post.data.slug);
769
+ });
770
+ test("create with same slug", async () => {
771
+ expect2(() => adapter1.createPost({
772
+ title: "Title 2",
773
+ slug: post.data.slug,
774
+ tags: ["Foo", "Bar"]
775
+ })).toThrow(ConflictError);
776
+ });
777
+ test("update unique slug", async () => {
778
+ const slug1 = faker.lorem.slug(5);
779
+ const slug2 = faker.lorem.slug(5);
780
+ const [post2] = await adapter1.createPost({
781
+ title: "Title 1",
782
+ slug: slug1
783
+ });
784
+ const readPostRes1 = await adapter1.readPostBySlug(slug1);
785
+ expect2(readPostRes1.data.slug).toStrictEqual(slug1);
786
+ const [updateRes] = await adapter1.updatePostSlug({
787
+ ref: post2.ref,
788
+ slug: slug2
789
+ });
790
+ expect2(updateRes.data.slug).toStrictEqual(slug2);
791
+ expect2(() => adapter1.readPostBySlug(slug1)).toThrow(NotFoundError3);
792
+ const readPostRes2 = await adapter1.readPostBySlug(slug2);
793
+ expect2(readPostRes2.data.slug).toStrictEqual(slug2);
794
+ });
795
+ test("update and delete post by slug", async () => {
796
+ const slug1 = faker.lorem.slug(5);
797
+ const slug2 = faker.lorem.slug(5);
798
+ const [post2] = await adapter1.createPost({
799
+ title: "Title 1",
800
+ slug: slug1
801
+ });
802
+ const [res] = await adapter1.crudBySlug({
803
+ slug: slug1,
804
+ newSlug: slug2
805
+ });
806
+ expect2(res).toStrictEqual([null, null]);
807
+ });
808
+ test("update index entries", async () => {
809
+ const [post2] = await adapter1.createPost({
810
+ title: "Title 1",
811
+ tags: ["foo"]
812
+ });
813
+ const pageTagFoo1 = await adapter1.pagePostsByTag("foo");
814
+ expect2(pageTagFoo1.length).toBe(1);
815
+ expect2(pageTagFoo1[0].ref).toBe(post2.ref);
816
+ const [updatedRes] = await adapter1.updatePostTags({
817
+ ref: post2.ref,
818
+ tags: ["bar"]
819
+ });
820
+ const pageTagFoo2 = await adapter1.pagePostsByTag("foo");
821
+ expect2(pageTagFoo2.length).toBe(0);
822
+ const pageTagBar1 = await adapter1.pagePostsByTag("bar");
823
+ expect2(pageTagBar1.length).toBe(1);
824
+ await adapter1.deletePost(post2.ref);
825
+ const pageTagBar2 = await adapter1.pagePostsByTag("bar");
826
+ expect2(pageTagBar2.length).toBe(0);
827
+ });
828
+ test("read not found (readEntity function)", async () => {
829
+ expect2(() => adapter1.readPost("Post/999")).toThrow(NotFoundError3);
830
+ });
831
+ test.todo("update (updateEntity function)", async () => {
832
+ const [post2] = await adapter1.updatePost({
833
+ ref: postRef,
834
+ title: "Title 2",
835
+ tags: ["Foo"]
836
+ });
837
+ expect2(post2.data.tags).toStrictEqual(["Foo"]);
838
+ expect2(post2.data.title).toBe("Title 2");
839
+ expect2(post2).toMatchZodSchema(PostEntity.schema);
840
+ });
841
+ test("updateTitle (patchEntity function + debounce)", async () => {
842
+ const [update1, mutation1] = await adapter1.updatePostTitle({
843
+ ref: postRef,
844
+ title: "Title 3"
845
+ });
846
+ expect2(update1).toMatchZodSchema(PostEntity.schema);
847
+ expect2(mutation1).toMatchZodSchema(UpdatePostTitleMutationSchema);
848
+ const [update2, mutation2] = await adapter1.updatePostTitle({
849
+ ref: postRef,
850
+ title: "Title 4"
851
+ });
852
+ expect2(update2).toMatchZodSchema(PostEntity.schema);
853
+ expect2(mutation2).toMatchZodSchema(UpdatePostTitleMutationSchema);
854
+ expect2(mutation1.ref).toBe(mutation2.ref);
855
+ expect2(mutation2.timestamp >= mutation1.timestamp).toBeTrue();
856
+ });
857
+ test("updatePostDescription without changeSetAdapter fails with BadRequestError", async () => {
858
+ expect2(() => adapter1.updatePostDescription({
859
+ ref: postRef,
860
+ description: "Description 1"
861
+ })).toThrow(BadRequestError);
862
+ });
863
+ test("delete (deleteEntity function)", async () => {
864
+ const currentPost = await adapter1.readPost(postRef);
865
+ expect2(currentPost.ref).toBe(postRef);
866
+ const [count, mutation] = await adapter1.deletePost(postRef);
867
+ expect2(count).toBe(1);
868
+ expect2(mutation.log).toStrictEqual([
869
+ [postRef, "delete", currentPost]
870
+ ]);
871
+ expect2(() => adapter1.readPost(postRef)).toThrow(NotFoundError3);
872
+ });
873
+ test("sync mutation", async () => {
874
+ const [{ persistedAt, ...mutation }] = await adapter2.persistOptimisticMutations([createPostMutation]);
875
+ expect2(mutation).toStrictEqual(createPostMutation);
876
+ });
877
+ test("sync mutation with debounce > 0", async () => {
878
+ const [post2, createPostMutation2] = await adapter1.createPost({
879
+ title: "Foo"
880
+ });
881
+ const [{ persistedAt, ...mutation }] = await adapter2.persistOptimisticMutations([createPostMutation2]);
882
+ expect2(mutation).toStrictEqual(createPostMutation2);
883
+ const res = await adapter1.updatePostTitle({
884
+ ref: post2.ref,
885
+ title: "A"
886
+ });
887
+ const [, debounceMutation] = await adapter1.updatePostTitle({
888
+ ref: post2.ref,
889
+ title: "A"
890
+ });
891
+ expect2(debounceMutation.debounceCount).toBe(1);
892
+ const [debounceMutationPersist] = await adapter2.persistOptimisticMutations([debounceMutation]);
893
+ expect2(debounceMutationPersist.debounceCount).toBe(1);
894
+ expect2(debounceMutationPersist.persistedAt).toBeDefined();
895
+ });
896
+ test("create, patch and delete the same item within the same transaction", async () => {
897
+ const [post1] = await adapter1.createPost({ title: "Post 1" });
898
+ const [post2] = await adapter1.createPost({ title: "Post 1" });
899
+ const [currentPost, mutation] = await adapter1.testTransactionalEdits({
900
+ name: "Test"
901
+ });
902
+ expect2(mutation.log).toHaveLength(0);
903
+ const [, mutation2] = await adapter1.testTransactionalEdits({
904
+ post1ref: post1.ref
905
+ });
906
+ expect2(mutation2.log).toHaveLength(1);
907
+ expect2(mutation2.log[0][1]).toBe("delete");
908
+ expect2(() => adapter1.testTransactionalEdits({
909
+ post2ref: post2.ref
910
+ })).toThrow(BadRequestError);
911
+ });
912
+ });
913
+ describe("paging", () => {
914
+ let adapter;
915
+ let post1ref;
916
+ beforeAll(async () => {
917
+ adapter = await createAdapter();
918
+ const [post1] = await adapter.createPost({ title: "Post 1" });
919
+ post1ref = post1.ref;
920
+ const [user] = await adapter.createUser({ email: "foo@bar.com" });
921
+ const [draft1] = await adapter.createDraft({
922
+ postRef: post1.ref
923
+ });
924
+ const [post2] = await adapter.createPost({ title: "Post 2" });
925
+ const [draft2] = await adapter.createDraft({
926
+ postRef: post2.ref
927
+ });
928
+ const draft1adapter = adapter.changeSet(draft1.ref);
929
+ await draft1adapter.createBlockImage({
930
+ parentRef: post1.ref,
931
+ url: "https://example.com/image.png"
932
+ });
933
+ await draft1adapter.createBlockParagraph({
934
+ parentRef: post1.ref
935
+ });
936
+ await adapter.applyDraft(draft1.ref);
937
+ const draft2adapter = adapter.changeSet(draft2.ref);
938
+ await draft2adapter.createBlockImage({
939
+ parentRef: post2.ref,
940
+ url: "https://example.com/image.png"
941
+ });
942
+ await draft2adapter.createBlockParagraph({
943
+ parentRef: post2.ref
944
+ });
945
+ await adapter.applyDraft(draft2.ref);
946
+ });
947
+ test("limited to given collection", async () => {
948
+ const res = await adapter.pagePosts();
949
+ expect2(res).toHaveLength(2);
950
+ });
951
+ test("limit", async () => {
952
+ const res = await adapter.pagePosts({ size: 1 });
953
+ expect2(res).toHaveLength(1);
954
+ });
955
+ test("pageSplat", async () => {
956
+ const res = await adapter.pageSplat({});
957
+ expect2(res).toHaveLength(11);
958
+ });
959
+ test("pageEmptyEntitiesArray", async () => {
960
+ const res = await adapter.pageEmptyEntitiesArray({});
961
+ expect2(res).toHaveLength(0);
962
+ });
963
+ test("pageBlocks", async () => {
964
+ const res = await adapter.pageBlocks({});
965
+ expect2(res).toHaveLength(4);
966
+ });
967
+ test("pageBlocks with parent ref", async () => {
968
+ const res = await adapter.pageBlocks({ parentRef: post1ref });
969
+ expect2(res).toHaveLength(2);
970
+ expect2(res[0].parents.parent).toBe(post1ref);
971
+ expect2(res[1].parents.parent).toBe(post1ref);
972
+ });
973
+ });
974
+ describe("changeSet", () => {
975
+ let postRef, draftRef, changeSetAdapter, blockParagraph1, post, blockRow, blockParagraph2, draft;
976
+ beforeAll(async () => {
977
+ ({
978
+ postRef,
979
+ draftRef,
980
+ changeSetAdapter,
981
+ blockParagraph1,
982
+ post,
983
+ blockRow,
984
+ blockParagraph2,
985
+ draft
986
+ } = await prepareChangeSetTest(adapter1, adapter2));
987
+ });
988
+ test("create (createEntity function w/changeSetRef)", async () => {
989
+ expect2(blockParagraph1).toMatchZodSchema(BlockParagraph.schema);
990
+ expect2(post).toMatchZodSchema(PostEntity.schema);
991
+ });
992
+ test("read (readEntity function w/changeSetRef)", async () => {
993
+ const read = await changeSetAdapter.readEntity(blockParagraph1.ref);
994
+ expect2(read).toMatchZodSchema(BlockParagraph.schema);
995
+ });
996
+ test("update (patchEntity function w/changeSetRef)", async () => {
997
+ const [updatedBlock] = await changeSetAdapter.updateBlockParagraph({
998
+ ref: blockParagraph1.ref,
999
+ content: "Hello, world!"
1000
+ });
1001
+ expect2(updatedBlock).toMatchZodSchema(BlockParagraph.schema);
1002
+ });
1003
+ test("move (batchReadEntities function)", async () => {
1004
+ const res = await changeSetAdapter.moveBlocks({
1005
+ refs: [blockParagraph1.ref],
1006
+ targetPosition: 1
1007
+ });
1008
+ });
1009
+ test("delete (with cascade delete)", async () => {
1010
+ const postUpdated = await changeSetAdapter.readEntity(postRef);
1011
+ const blockParagraph1Update = await changeSetAdapter.readEntity(blockParagraph1.ref);
1012
+ const blockRowUpdated = await changeSetAdapter.readEntity(blockRow.ref);
1013
+ const [count, mutation] = await changeSetAdapter.deletePost(postRef);
1014
+ expect2(count).toBe(4);
1015
+ expect2(mutation.log).toStrictEqual([
1016
+ [blockParagraph1.ref, "delete", blockParagraph1Update],
1017
+ [blockParagraph2.ref, "delete", blockParagraph2],
1018
+ [blockRow.ref, "delete", blockRowUpdated],
1019
+ [postRef, "delete", postUpdated]
1020
+ ]);
1021
+ await expect2(() => changeSetAdapter.readEntity(blockParagraph1.ref)).toThrow(NotFoundError3);
1022
+ await expect2(() => changeSetAdapter.updateBlockParagraph({
1023
+ ref: blockParagraph1.ref,
1024
+ content: "Foo"
1025
+ })).toThrow(NotFoundError3);
1026
+ });
1027
+ test("pageMutations", async () => {
1028
+ const res = await changeSetAdapter.pageMutations({
1029
+ changeSetRef: draftRef
1030
+ });
1031
+ expect2(res.length).toBeGreaterThan(0);
1032
+ expect2(res.every((m) => m.changeSetRef === draftRef)).toBeTrue();
1033
+ });
1034
+ test("pageMutations run again", async () => {
1035
+ const res = await changeSetAdapter.pageMutations({
1036
+ changeSetRef: draftRef
1037
+ });
1038
+ expect2(res.length).toBeGreaterThan(0);
1039
+ expect2(res.every((m) => m.changeSetRef === draftRef)).toBeTrue();
1040
+ });
1041
+ test("invalid changeSetRef throws error", async () => {
1042
+ const changeSetAdapter2 = adapter1.changeSet("Draft/123");
1043
+ expect2(() => changeSetAdapter2.createPost({ title: "Title 1" })).toThrow(BadRequestError);
1044
+ });
1045
+ test("deleteBlocks (patch running 2 times removing on array element every time)", async () => {
1046
+ const [post2, createPostMutation] = await adapter1.createPost({
1047
+ title: "Title 1",
1048
+ slug: faker.lorem.slug(5),
1049
+ tags: ["Foo", "Bar"]
1050
+ });
1051
+ const [draft2, createDraftMutation] = await adapter1.createDraft({
1052
+ postRef: post2.ref
1053
+ });
1054
+ const changeSetAdapter2 = adapter1.changeSet(draft2.ref);
1055
+ const [{ block: blockParagraph12 }, createBlockParagraph1Mutation] = await changeSetAdapter2.createBlockParagraph({
1056
+ parentRef: post2.ref
1057
+ });
1058
+ const [{ block: blockParagraph22 }, createBlockParagraph2Mutation] = await changeSetAdapter2.createBlockParagraph({
1059
+ parentRef: post2.ref
1060
+ });
1061
+ const readPost2 = await changeSetAdapter2.readEntity(post2.ref);
1062
+ expect2(readPost2.children.blocks).toEqual([
1063
+ blockParagraph12.ref,
1064
+ blockParagraph22.ref
1065
+ ]);
1066
+ const [, deleteMutation] = await changeSetAdapter2.deleteBlocks([
1067
+ blockParagraph12.ref,
1068
+ blockParagraph22.ref
1069
+ ]);
1070
+ expect2(deleteMutation.log[0][2].children.blocks).toEqual([
1071
+ blockParagraph12.ref,
1072
+ blockParagraph22.ref
1073
+ ]);
1074
+ expect2(deleteMutation.log[0][2].updated.mutationRef).toBe(createBlockParagraph2Mutation.ref);
1075
+ const readPost22 = await changeSetAdapter2.readEntity(post2.ref);
1076
+ expect2(readPost22.updated.mutationRef).toEqual(deleteMutation.ref);
1077
+ });
1078
+ test("applyDraft", async () => {
1079
+ const { draftRef: draftRef2, post: post2, changeSetAdapter: changeSetAdapter2 } = await prepareChangeSetTest(adapter1);
1080
+ const [version, mutation] = await adapter1.applyDraft(draftRef2);
1081
+ expect2(() => changeSetAdapter2.createBlockImage({
1082
+ parentRef: post2.ref,
1083
+ url: "https://example.com/image.png"
1084
+ })).toThrowError("The provided changeSetRef has already been applied");
1085
+ });
1086
+ test("deleteDraft", async () => {
1087
+ const { draftRef: draftRef2 } = await prepareChangeSetTest(adapter1);
1088
+ const mutationsBefore = await adapter1.pageMutations({
1089
+ changeSetRef: draftRef2
1090
+ });
1091
+ const [, mutation] = await adapter1.deleteDraft(draftRef2);
1092
+ expect2(mutation.log).toHaveLength(mutationsBefore.length + 1);
1093
+ const mutationsAfter = await adapter1.pageMutations({
1094
+ changeSetRef: draftRef2
1095
+ });
1096
+ expect2(mutationsAfter).toHaveLength(0);
1097
+ });
1098
+ test("init adapter2", async () => {
1099
+ const adapterA = await createAdapter({ optimistic: true });
1100
+ const adapterB = await createAdapter({ optimistic: true });
1101
+ const { draftRef: draftRef2 } = await prepareChangeSetTest(adapterA, adapterB);
1102
+ const mutations = await adapterA.pageMutations({
1103
+ changeSetRef: draftRef2
1104
+ });
1105
+ const allMutationsAdapter1 = await adapterA.pageMutations({});
1106
+ const allMutationsAdapter2 = await adapterB.pageMutations({});
1107
+ expect2(allMutationsAdapter1).not.toBeEmpty();
1108
+ expect2(allMutationsAdapter2).toBeEmpty();
1109
+ const res = await adapterB.loadMutations(allMutationsAdapter1);
1110
+ const allMutationsAdapter2after = await adapterB.pageMutations({});
1111
+ expect2(allMutationsAdapter2after.length).toBe(allMutationsAdapter1.length);
1112
+ const res2 = await adapterB.loadMutations(allMutationsAdapter1);
1113
+ });
1114
+ test("persistOptimisticMutations", async () => {
1115
+ const persistedAdapter = await createAdapter({ optimistic: false });
1116
+ const optimisticAdapter = await createAdapter({ optimistic: true });
1117
+ expect2(persistedAdapter.persistOptimisticMutations).toBeDefined();
1118
+ expect2(optimisticAdapter.loadMutations).toBeDefined();
1119
+ const [post2, createPostMutation] = await optimisticAdapter.createPost({
1120
+ title: "Foo",
1121
+ slug: faker.lorem.slug(5),
1122
+ tags: []
1123
+ });
1124
+ const [persistCratePostMutation] = await persistedAdapter.persistOptimisticMutations([
1125
+ createPostMutation
1126
+ ]);
1127
+ expect2(persistCratePostMutation.persistedAt >= createPostMutation.timestamp).toBeTrue();
1128
+ const readMutation2 = await persistedAdapter.readMutation(persistCratePostMutation.ref);
1129
+ expect2(readMutation2).toStrictEqual(persistCratePostMutation);
1130
+ const [draft2, createDraftMutation] = await optimisticAdapter.createDraft({
1131
+ postRef: post2.ref
1132
+ });
1133
+ const [{ persistedAt, ...persistedCreateDraftMutation }] = await persistedAdapter.persistOptimisticMutations([
1134
+ createDraftMutation
1135
+ ]);
1136
+ expect2(persistedCreateDraftMutation).toStrictEqual(createDraftMutation);
1137
+ const changeSetAdapter2 = optimisticAdapter.changeSet(draft2.ref);
1138
+ const [res] = await changeSetAdapter2.createBlockParagraph({
1139
+ parentRef: post2.ref
1140
+ });
1141
+ });
1142
+ });
500
1143
  };
501
1144
  // src/inMemoryAdapter.ts
502
1145
  import { createInMemoryAdapter } from "@roc-db/in-memory";
503
1146
  var inMemoryAdapter = createInMemoryAdapter({
504
1147
  operations,
505
- entities
1148
+ entities,
1149
+ session: {
1150
+ identityRef: "User/42"
1151
+ }
506
1152
  });
507
1153
  export {
508
1154
  testAdapterImplementation,