@roc-db/test-utils 0.2.0-pre.6 → 0.2.0-pre.61

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