@roc-db/test-utils 0.2.0-pre.4 → 0.2.0-pre.41
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.
- package/dist/index.js +474 -138
- package/dist/types/inMemoryAdapter.d.ts +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/operations/applyDraft.d.ts +5 -0
- package/dist/types/operations/createBlockImage.d.ts +7 -0
- package/dist/types/operations/createBlockParagraph.d.ts +7 -0
- package/dist/types/operations/createBlockRow.d.ts +7 -0
- package/dist/types/operations/createBlockTitle.d.ts +7 -0
- package/dist/types/operations/createDraft.d.ts +7 -0
- package/dist/types/operations/createPost.d.ts +7 -0
- package/dist/types/operations/deleteBlocks.d.ts +7 -0
- package/dist/types/operations/deletePost.d.ts +5 -0
- package/dist/types/operations/index.d.ts +83 -0
- package/dist/types/operations/moveBlocks.d.ts +5 -0
- package/dist/types/operations/readEntity.d.ts +1 -0
- package/dist/types/operations/readPost.d.ts +1 -0
- package/dist/types/operations/testTransactionalEdits.d.ts +7 -0
- package/dist/types/operations/updateBlockParagraph.d.ts +5 -0
- package/dist/types/operations/updatePost.d.ts +7 -0
- package/dist/types/operations/updatePostDescription.d.ts +7 -0
- package/dist/types/operations/updatePostTitle.d.ts +7 -0
- package/dist/types/schemas/ApplyDraftMutationSchema.d.ts +64 -0
- package/dist/types/schemas/BlockImageRefSchema.d.ts +1 -0
- package/dist/types/schemas/BlockImageSchema.d.ts +32 -0
- package/dist/types/schemas/BlockParagraphRefSchema.d.ts +1 -0
- package/dist/types/schemas/BlockParagraphSchema.d.ts +33 -0
- package/dist/types/schemas/BlockRefSchema.d.ts +2 -0
- package/dist/types/schemas/BlockRowRefSchema.d.ts +1 -0
- package/dist/types/schemas/BlockRowSchema.d.ts +33 -0
- package/dist/types/schemas/BlockTitleRefSchema.d.ts +1 -0
- package/dist/types/schemas/BlockTitleSchema.d.ts +32 -0
- package/dist/types/schemas/CreateBlockImageMutationSchema.d.ts +64 -0
- package/dist/types/schemas/CreateBlockParagraphMutationSchema.d.ts +64 -0
- package/dist/types/schemas/CreateBlockRowMutationSchema.d.ts +64 -0
- package/dist/types/schemas/CreateBlockTitleMutationSchema.d.ts +64 -0
- package/dist/types/schemas/CreateDraftMutationSchema.d.ts +64 -0
- package/dist/types/schemas/CreatePostMutationSchema.d.ts +74 -0
- package/dist/types/schemas/CreateUserMutationSchema.d.ts +64 -0
- package/dist/types/schemas/DeleteBlocksMutationSchema.d.ts +54 -0
- package/dist/types/schemas/DeletePostMutationSchema.d.ts +53 -0
- package/dist/types/schemas/DraftRefSchema.d.ts +1 -0
- package/dist/types/schemas/DraftSchema.d.ts +32 -0
- package/dist/types/schemas/MoveBlocksMutationSchema.d.ts +69 -0
- package/dist/types/schemas/PostRefSchema.d.ts +1 -0
- package/dist/types/schemas/PostSchema.d.ts +33 -0
- package/dist/types/schemas/TestTransactionalEditsMutationSchema.d.ts +74 -0
- package/dist/types/schemas/UpdateBlockParagraphMutationSchema.d.ts +69 -0
- package/dist/types/schemas/UpdatePostDescriptionMutationSchema.d.ts +69 -0
- package/dist/types/schemas/UpdatePostMutationSchema.d.ts +79 -0
- package/dist/types/schemas/UpdatePostTitleMutationSchema.d.ts +69 -0
- package/dist/types/schemas/UserRefSchema.d.ts +1 -0
- package/dist/types/schemas/index.d.ts +44 -0
- package/dist/types/testAdapterImplementation.d.ts +1 -0
- package/package.json +10 -17
- package/dist/tarball/roc-db-test-utils-0.2.0-pre.4.tgz +0 -0
package/dist/index.js
CHANGED
|
@@ -114,7 +114,7 @@ var UpdatePostMutationSchema = mutationSchemaGenerator3("updatePost", z8.object(
|
|
|
114
114
|
ref: PostRefSchema,
|
|
115
115
|
title: z8.string().optional(),
|
|
116
116
|
tags: z8.array(z8.string()).optional().default([]),
|
|
117
|
-
authorRef: UserRefSchema.optional()
|
|
117
|
+
authorRef: UserRefSchema.optional()
|
|
118
118
|
}));
|
|
119
119
|
// src/schemas/UpdatePostTitleMutationSchema.ts
|
|
120
120
|
import { mutationSchemaGenerator as mutationSchemaGenerator4 } from "roc-db";
|
|
@@ -128,95 +128,115 @@ var entities = [
|
|
|
128
128
|
DraftSchema,
|
|
129
129
|
PostSchema
|
|
130
130
|
];
|
|
131
|
-
// src/operations/
|
|
132
|
-
import { Query, writeOperation } from "roc-db";
|
|
131
|
+
// src/operations/applyDraft.ts
|
|
132
|
+
import { Query, QueryChain, writeOperation } from "roc-db";
|
|
133
133
|
|
|
134
|
-
// src/schemas/
|
|
134
|
+
// src/schemas/ApplyDraftMutationSchema.ts
|
|
135
135
|
import { mutationSchemaGenerator as mutationSchemaGenerator5 } from "roc-db";
|
|
136
136
|
import { z as z10 } from "zod";
|
|
137
|
-
var
|
|
137
|
+
var ApplyDraftMutationSchema = mutationSchemaGenerator5("applyDraft", z10.object({ ref: DraftRefSchema }).strict());
|
|
138
|
+
|
|
139
|
+
// src/operations/applyDraft.ts
|
|
140
|
+
var applyDraft = writeOperation(ApplyDraftMutationSchema, undefined, (txn) => {
|
|
141
|
+
const { ref } = txn.payload;
|
|
142
|
+
const versionRef = txn.createRef("PostVersion");
|
|
143
|
+
return QueryChain(Query(() => txn.applyChangeSet(ref)), Query(() => txn.patchEntity(ref, {
|
|
144
|
+
data: { appliedAt: txn.timestamp }
|
|
145
|
+
})), Query((draft) => txn.childRefsOf(draft.parents.post, true)), Query((childRefs, draft) => txn.batchReadEntities([draft.parents.post, ...childRefs])), Query((childEntities) => txn.createEntity(versionRef, {
|
|
146
|
+
data: {
|
|
147
|
+
snapshot: childEntities
|
|
148
|
+
}
|
|
149
|
+
})));
|
|
150
|
+
});
|
|
138
151
|
|
|
139
152
|
// src/operations/createBlockImage.ts
|
|
140
|
-
|
|
153
|
+
import { Query as Query2, writeOperation as writeOperation2 } from "roc-db";
|
|
154
|
+
|
|
155
|
+
// src/schemas/CreateBlockImageMutationSchema.ts
|
|
156
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator6 } from "roc-db";
|
|
157
|
+
import { z as z11 } from "zod";
|
|
158
|
+
var CreateBlockImageMutationSchema = mutationSchemaGenerator6("createBlockImage", z11.object({ postRef: PostRefSchema }).strict(), undefined, true);
|
|
159
|
+
|
|
160
|
+
// src/operations/createBlockImage.ts
|
|
161
|
+
var createBlockImage = writeOperation2(CreateBlockImageMutationSchema, BlockImageSchema, (txn) => {
|
|
141
162
|
const ref = txn.createRef("BlockImage");
|
|
142
163
|
const { postRef } = txn.payload;
|
|
143
|
-
return
|
|
164
|
+
return Query2(() => txn.createEntity(ref, { parents: { post: postRef } }));
|
|
144
165
|
}, { changeSetOnly: true });
|
|
145
166
|
|
|
146
167
|
// src/operations/createBlockParagraph.ts
|
|
147
|
-
import { Query as
|
|
168
|
+
import { Query as Query3, QueryChain as QueryChain2, writeOperation as writeOperation3 } from "roc-db";
|
|
148
169
|
|
|
149
170
|
// src/schemas/CreateBlockParagraphMutationSchema.ts
|
|
150
|
-
import { mutationSchemaGenerator as
|
|
151
|
-
import { z as
|
|
171
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator7 } from "roc-db";
|
|
172
|
+
import { z as z12 } from "zod";
|
|
152
173
|
|
|
153
174
|
// src/schemas/BlockRowRefSchema.ts
|
|
154
175
|
import { refSchemaGenerator as refSchemaGenerator7 } from "roc-db";
|
|
155
176
|
var BlockRowRefSchema = refSchemaGenerator7("BlockRow");
|
|
156
177
|
|
|
157
178
|
// src/schemas/CreateBlockParagraphMutationSchema.ts
|
|
158
|
-
var CreateBlockParagraphMutationSchema =
|
|
179
|
+
var CreateBlockParagraphMutationSchema = mutationSchemaGenerator7("createBlockParagraph", z12.object({ parentRef: z12.union([PostRefSchema, BlockRowRefSchema]) }).strict(), undefined, true);
|
|
159
180
|
|
|
160
181
|
// src/operations/createBlockParagraph.ts
|
|
161
|
-
var createBlockParagrah =
|
|
182
|
+
var createBlockParagrah = writeOperation3(CreateBlockParagraphMutationSchema, BlockParagraphSchema, (txn) => {
|
|
162
183
|
const ref = txn.createRef("BlockParagraph");
|
|
163
184
|
const { parentRef } = txn.payload;
|
|
164
|
-
return
|
|
185
|
+
return QueryChain2(Query3(() => txn.readEntity(parentRef, true)), Query3((parent) => txn.patchEntity(parentRef, {
|
|
165
186
|
children: { blocks: [...parent.children.blocks, ref] }
|
|
166
|
-
})),
|
|
187
|
+
})), Query3(() => txn.createEntity(ref, { parents: { parent: parentRef } })), Query3((block, updatedParent) => ({ block, updatedParent })));
|
|
167
188
|
}, { changeSetOnly: true });
|
|
168
189
|
|
|
169
190
|
// src/operations/createBlockRow.ts
|
|
170
|
-
import { Query as
|
|
191
|
+
import { Query as Query4, QueryChain as QueryChain3, writeOperation as writeOperation4 } from "roc-db";
|
|
171
192
|
|
|
172
193
|
// src/schemas/CreateBlockRowMutationSchema.ts
|
|
173
|
-
import { mutationSchemaGenerator as
|
|
174
|
-
import { z as
|
|
175
|
-
var CreateBlockRowMutationSchema =
|
|
194
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator8 } from "roc-db";
|
|
195
|
+
import { z as z13 } from "zod";
|
|
196
|
+
var CreateBlockRowMutationSchema = mutationSchemaGenerator8("createBlockRow", z13.object({ parentRef: z13.union([PostRefSchema, BlockRowRefSchema]) }).strict(), undefined, true);
|
|
176
197
|
|
|
177
198
|
// src/operations/createBlockRow.ts
|
|
178
|
-
var createBlockRow =
|
|
199
|
+
var createBlockRow = writeOperation4(CreateBlockRowMutationSchema, BlockParagraphSchema, (txn) => {
|
|
179
200
|
const ref = txn.createRef("BlockRow");
|
|
180
201
|
const { parentRef } = txn.payload;
|
|
181
|
-
return
|
|
182
|
-
console.log("asdfasfd", parent);
|
|
202
|
+
return QueryChain3(Query4(() => txn.readEntity(parentRef)), Query4((parent) => {
|
|
183
203
|
return txn.patchEntity(parentRef, {
|
|
184
204
|
children: { blocks: [...parent.children.blocks, ref] }
|
|
185
205
|
});
|
|
186
|
-
}),
|
|
206
|
+
}), Query4(() => txn.createEntity(ref, {
|
|
187
207
|
children: { blocks: [] },
|
|
188
208
|
parents: { parent: parentRef }
|
|
189
|
-
})),
|
|
209
|
+
})), Query4((block, updatedParent) => ({ block, updatedParent })));
|
|
190
210
|
}, { changeSetOnly: true });
|
|
191
211
|
|
|
192
212
|
// src/operations/createBlockTitle.ts
|
|
193
|
-
import { Query as
|
|
213
|
+
import { Query as Query5, writeOperation as writeOperation5 } from "roc-db";
|
|
194
214
|
|
|
195
215
|
// src/schemas/CreateBlockTitleMutationSchema.ts
|
|
196
|
-
import { mutationSchemaGenerator as
|
|
197
|
-
import { z as
|
|
198
|
-
var CreateBlockTitleMutationSchema =
|
|
216
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator9 } from "roc-db";
|
|
217
|
+
import { z as z14 } from "zod";
|
|
218
|
+
var CreateBlockTitleMutationSchema = mutationSchemaGenerator9("createBlockTitle", z14.object({ parentRef: z14.union([PostRefSchema, BlockRowRefSchema]) }).strict(), undefined, true);
|
|
199
219
|
|
|
200
220
|
// src/operations/createBlockTitle.ts
|
|
201
|
-
var createBlockTitle =
|
|
221
|
+
var createBlockTitle = writeOperation5(CreateBlockTitleMutationSchema, BlockTitleSchema, (txn) => {
|
|
202
222
|
const ref = txn.createRef("BlockTitle");
|
|
203
223
|
const { postRef } = txn.payload;
|
|
204
|
-
return
|
|
224
|
+
return Query5(() => txn.createEntity(ref, { parents: { post: postRef } }));
|
|
205
225
|
}, { changeSetOnly: true });
|
|
206
226
|
|
|
207
227
|
// src/operations/createDraft.ts
|
|
208
|
-
import { Query as
|
|
228
|
+
import { Query as Query6, writeOperation as writeOperation6 } from "roc-db";
|
|
209
229
|
|
|
210
230
|
// src/schemas/CreateDraftMutationSchema.ts
|
|
211
|
-
import { mutationSchemaGenerator as
|
|
212
|
-
import { z as
|
|
213
|
-
var CreateDraftMutationSchema =
|
|
231
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator10 } from "roc-db";
|
|
232
|
+
import { z as z15 } from "zod";
|
|
233
|
+
var CreateDraftMutationSchema = mutationSchemaGenerator10("createDraft", z15.object({ postRef: PostRefSchema }).strict());
|
|
214
234
|
|
|
215
235
|
// src/operations/createDraft.ts
|
|
216
|
-
var createDraft =
|
|
236
|
+
var createDraft = writeOperation6(CreateDraftMutationSchema, DraftSchema, (txn) => {
|
|
217
237
|
const ref = txn.createRef("Draft");
|
|
218
238
|
const { postRef } = txn.payload;
|
|
219
|
-
return
|
|
239
|
+
return Query6(() => txn.createEntity(ref, {
|
|
220
240
|
parents: {
|
|
221
241
|
post: postRef
|
|
222
242
|
}
|
|
@@ -224,57 +244,83 @@ var createDraft = writeOperation5(CreateDraftMutationSchema, DraftSchema, (txn)
|
|
|
224
244
|
});
|
|
225
245
|
|
|
226
246
|
// src/operations/createPost.ts
|
|
227
|
-
import { Query as
|
|
228
|
-
var createPost =
|
|
247
|
+
import { Query as Query7, writeOperation as writeOperation7 } from "roc-db";
|
|
248
|
+
var createPost = writeOperation7(CreatePostMutationSchema, PostSchema, (txn) => {
|
|
229
249
|
const ref = txn.createRef("Post");
|
|
230
250
|
const { title, slug, tags } = txn.payload;
|
|
231
|
-
return
|
|
251
|
+
return Query7(() => txn.createEntity(ref, {
|
|
232
252
|
data: { title, tags, slug },
|
|
233
253
|
children: { blocks: [] }
|
|
234
254
|
}));
|
|
235
255
|
});
|
|
236
256
|
|
|
237
|
-
// src/operations/
|
|
238
|
-
import { Query as
|
|
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]));
|
|
257
|
+
// src/operations/deleteBlocks.ts
|
|
258
|
+
import { Query as Query8, QueryChain as QueryChain4, writeOperation as writeOperation8 } from "roc-db";
|
|
244
259
|
|
|
245
|
-
// src/
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
260
|
+
// src/schemas/DeleteBlocksMutationSchema.ts
|
|
261
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator11 } from "roc-db";
|
|
262
|
+
import { z as z16 } from "zod";
|
|
263
|
+
var DeleteBlocksMutationSchema = mutationSchemaGenerator11("deleteBlocks", z16.array(z16.union([
|
|
264
|
+
BlockParagraphRefSchema,
|
|
265
|
+
BlockImageRefSchema,
|
|
266
|
+
BlockRowRefSchema
|
|
267
|
+
])));
|
|
268
|
+
|
|
269
|
+
// src/operations/deleteBlocks.ts
|
|
270
|
+
var DeleteBlockQuery = (txn, blockRef) => {
|
|
271
|
+
return QueryChain4(Query8(() => txn.readEntity(blockRef)), Query8((block) => {
|
|
272
|
+
return txn.readEntity(block.parents.parent, true);
|
|
273
|
+
}), Query8((post) => {
|
|
249
274
|
return txn.patchEntity(post.ref, {
|
|
250
|
-
|
|
251
|
-
|
|
275
|
+
children: {
|
|
276
|
+
blocks: post.children.blocks.filter((ref) => ref !== blockRef)
|
|
252
277
|
}
|
|
253
278
|
});
|
|
254
|
-
}),
|
|
255
|
-
|
|
256
|
-
|
|
279
|
+
}), Query8(() => txn.deleteEntity(blockRef)));
|
|
280
|
+
};
|
|
281
|
+
var deleteBlocks = writeOperation8(DeleteBlocksMutationSchema, PostSchema, (txn) => {
|
|
282
|
+
const refs = txn.payload;
|
|
283
|
+
return QueryChain4(...refs.map((ref) => DeleteBlockQuery(txn, ref)));
|
|
257
284
|
}, { changeSetOnly: true });
|
|
258
285
|
|
|
259
286
|
// src/operations/deletePost.ts
|
|
260
|
-
import { Query as
|
|
287
|
+
import { Query as Query9, writeOperation as writeOperation9 } from "roc-db";
|
|
261
288
|
|
|
262
289
|
// src/schemas/DeletePostMutationSchema.ts
|
|
263
|
-
import { mutationSchemaGenerator as
|
|
264
|
-
var DeletePostMutationSchema =
|
|
290
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator12 } from "roc-db";
|
|
291
|
+
var DeletePostMutationSchema = mutationSchemaGenerator12("deletePost", PostRefSchema);
|
|
265
292
|
|
|
266
293
|
// src/operations/deletePost.ts
|
|
267
|
-
var deletePost =
|
|
294
|
+
var deletePost = writeOperation9(DeletePostMutationSchema, undefined, (txn) => {
|
|
268
295
|
const ref = txn.payload;
|
|
269
|
-
return
|
|
296
|
+
return Query9(() => txn.deleteEntity(ref, true));
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// src/operations/moveBlocks.ts
|
|
300
|
+
import { Query as Query10, QueryChain as QueryChain5, writeOperation as writeOperation10 } from "roc-db";
|
|
301
|
+
|
|
302
|
+
// src/schemas/MoveBlocksMutationSchema.ts
|
|
303
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator13 } from "roc-db";
|
|
304
|
+
import { z as z17 } from "zod";
|
|
305
|
+
var MoveBlocksMutationSchema = mutationSchemaGenerator13("moveBlocks", z17.object({
|
|
306
|
+
refs: z17.array(z17.union([BlockParagraphRefSchema, BlockRowRefSchema])),
|
|
307
|
+
targetPosition: z17.number().min(0)
|
|
308
|
+
}).strict(), undefined, true);
|
|
309
|
+
|
|
310
|
+
// src/operations/moveBlocks.ts
|
|
311
|
+
var moveBlocks = writeOperation10(MoveBlocksMutationSchema, undefined, (txn) => {
|
|
312
|
+
const { refs } = txn.payload;
|
|
313
|
+
return QueryChain5(Query10(() => txn.batchReadEntities(refs)), Query10((items) => {
|
|
314
|
+
return "todo";
|
|
315
|
+
}));
|
|
270
316
|
});
|
|
271
317
|
|
|
272
318
|
// src/operations/readEntity.ts
|
|
273
|
-
import { NotFoundError, Query as
|
|
274
|
-
import { z as
|
|
275
|
-
var readEntity = readOperation("readEntity",
|
|
319
|
+
import { NotFoundError, Query as Query11, QueryChain as QueryChain6, readOperation } from "roc-db";
|
|
320
|
+
import { z as z18 } from "zod";
|
|
321
|
+
var readEntity = readOperation("readEntity", z18.string(), z18.union([PostSchema, BlockImageSchema, BlockParagraphSchema]), (txn) => {
|
|
276
322
|
const ref = txn.payload;
|
|
277
|
-
return
|
|
323
|
+
return QueryChain6(Query11(() => txn.readEntity(ref)), Query11((entity) => {
|
|
278
324
|
if (!entity) {
|
|
279
325
|
throw new NotFoundError(ref);
|
|
280
326
|
}
|
|
@@ -283,158 +329,448 @@ var readEntity = readOperation("readEntity", z16.string(), z16.union([PostSchema
|
|
|
283
329
|
});
|
|
284
330
|
|
|
285
331
|
// src/operations/readPost.ts
|
|
286
|
-
import { Query as
|
|
332
|
+
import { Query as Query12, readOperation as readOperation2 } from "roc-db";
|
|
287
333
|
var readPost = readOperation2("readPost", PostRefSchema, PostSchema, (txn) => {
|
|
288
334
|
const ref = txn.payload;
|
|
289
|
-
return
|
|
335
|
+
return Query12(() => txn.readEntity(ref, true));
|
|
290
336
|
});
|
|
291
337
|
|
|
338
|
+
// src/operations/testTransactionalEdits.ts
|
|
339
|
+
import { Query as Query13, QueryChain as QueryChain7, writeOperation as writeOperation11 } from "roc-db";
|
|
340
|
+
|
|
341
|
+
// src/schemas/TestTransactionalEditsMutationSchema.ts
|
|
342
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator14 } from "roc-db";
|
|
343
|
+
import { z as z19 } from "zod";
|
|
344
|
+
var TestTransactionalEditsMutationSchema = mutationSchemaGenerator14("testTransactionalEdits", z19.object({
|
|
345
|
+
name: z19.string().optional(),
|
|
346
|
+
post1ref: PostRefSchema.optional(),
|
|
347
|
+
post2ref: PostRefSchema.optional()
|
|
348
|
+
}).strict());
|
|
349
|
+
|
|
350
|
+
// src/operations/testTransactionalEdits.ts
|
|
351
|
+
import { expect } from "bun:test";
|
|
352
|
+
var TestCreatePatchDeleteInSameTransaction = (txn, name) => {
|
|
353
|
+
const postRef = txn.createRef("Post");
|
|
354
|
+
QueryChain7(Query13(() => txn.createEntity(postRef, { data: { name } })), Query13(() => txn.patchEntity(postRef, { data: { foo: "bar" } })), Query13((post) => {
|
|
355
|
+
expect(post.data.name).toBe(name);
|
|
356
|
+
expect(post.data.foo).toBe("bar");
|
|
357
|
+
}), Query13(() => txn.patchEntity(postRef, { data: { foo: null, bar: "foo" } })), Query13((post) => {
|
|
358
|
+
expect(post.data.name).toBe(name);
|
|
359
|
+
expect(post.data.foo).toBeUndefined();
|
|
360
|
+
expect(post.data.bar).toBe("foo");
|
|
361
|
+
expect(txn.log).toHaveLength(1);
|
|
362
|
+
expect(txn.log.get(postRef)[0]).toBe("create");
|
|
363
|
+
}), Query13((post) => txn.deleteEntity(postRef)), Query13((res) => {
|
|
364
|
+
expect(res).toBe(1);
|
|
365
|
+
expect(txn.log).toHaveLength(0);
|
|
366
|
+
}));
|
|
367
|
+
};
|
|
368
|
+
var TestUpdateAndDeleteInSameTransaction = (txn, ref) => {
|
|
369
|
+
return QueryChain7(Query13(() => txn.patchEntity(ref, { data: { foo: "bar" } })), Query13((post) => {
|
|
370
|
+
expect(post.data.foo).toBe("bar");
|
|
371
|
+
}), Query13(() => txn.patchEntity(ref, { data: { foo: null, bar: "foo" } })), Query13((post) => {
|
|
372
|
+
expect(post.data.foo).toBeUndefined();
|
|
373
|
+
expect(post.data.bar).toBe("foo");
|
|
374
|
+
expect(txn.log).toHaveLength(1);
|
|
375
|
+
expect(txn.log.get(ref)[0]).toBe("update");
|
|
376
|
+
}), Query13((post) => txn.deleteEntity(ref)), Query13((res) => {
|
|
377
|
+
expect(res).toBe(1);
|
|
378
|
+
expect(txn.log).toHaveLength(1);
|
|
379
|
+
expect(txn.log.get(ref)[0]).toBe("delete");
|
|
380
|
+
}));
|
|
381
|
+
};
|
|
382
|
+
var TestCreateOnExistingRef = (txn) => {
|
|
383
|
+
const ref = txn.createRef("Post");
|
|
384
|
+
return QueryChain7(Query13(() => txn.createEntity(ref, { data: { foo: "bar" } })), Query13(() => txn.createEntity(ref, { data: { foo: "bar" } })));
|
|
385
|
+
};
|
|
386
|
+
var testTransactionalEdits = writeOperation11(TestTransactionalEditsMutationSchema, PostSchema, (txn) => {
|
|
387
|
+
const { name, post1ref, post2ref } = txn.payload;
|
|
388
|
+
if (name)
|
|
389
|
+
return TestCreatePatchDeleteInSameTransaction(txn, name);
|
|
390
|
+
if (post1ref)
|
|
391
|
+
return TestUpdateAndDeleteInSameTransaction(txn, post1ref);
|
|
392
|
+
if (post2ref)
|
|
393
|
+
return TestCreateOnExistingRef(txn);
|
|
394
|
+
}, {});
|
|
395
|
+
|
|
292
396
|
// src/operations/updateBlockParagraph.ts
|
|
293
|
-
import { Query as
|
|
397
|
+
import { Query as Query14, writeOperation as writeOperation12 } from "roc-db";
|
|
294
398
|
|
|
295
399
|
// src/schemas/UpdateBlockParagraphMutationSchema.ts
|
|
296
|
-
import { mutationSchemaGenerator as
|
|
297
|
-
import { z as
|
|
298
|
-
var UpdateBlockParagraphMutationSchema =
|
|
400
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator15 } from "roc-db";
|
|
401
|
+
import { z as z20 } from "zod";
|
|
402
|
+
var UpdateBlockParagraphMutationSchema = mutationSchemaGenerator15("updateBlockParagraph", z20.object({
|
|
299
403
|
ref: BlockParagraphRefSchema,
|
|
300
|
-
content:
|
|
404
|
+
content: z20.string()
|
|
301
405
|
}));
|
|
302
406
|
|
|
303
407
|
// src/operations/updateBlockParagraph.ts
|
|
304
|
-
var updateBlockParagraph =
|
|
408
|
+
var updateBlockParagraph = writeOperation12(UpdateBlockParagraphMutationSchema, BlockParagraphRefSchema, (txn) => {
|
|
305
409
|
const { ref, content } = txn.payload;
|
|
306
|
-
return
|
|
410
|
+
return Query14(() => txn.patchEntity(ref, {
|
|
307
411
|
data: { content }
|
|
308
412
|
}));
|
|
309
413
|
});
|
|
310
414
|
|
|
311
415
|
// src/operations/updatePost.ts
|
|
312
|
-
import { Query as
|
|
313
|
-
var updatePost =
|
|
416
|
+
import { Query as Query15, writeOperation as writeOperation13 } from "roc-db";
|
|
417
|
+
var updatePost = writeOperation13(UpdatePostMutationSchema, PostSchema, (txn) => {
|
|
314
418
|
const { ref, authorRef, ...data } = txn.payload;
|
|
315
|
-
return
|
|
419
|
+
return Query15(() => txn.updateEntity(ref, {
|
|
316
420
|
data,
|
|
317
421
|
parents: { author: authorRef },
|
|
318
422
|
children: { blocks: [] }
|
|
319
423
|
}));
|
|
320
424
|
});
|
|
321
425
|
|
|
426
|
+
// src/operations/updatePostDescription.ts
|
|
427
|
+
import { Query as Query16, writeOperation as writeOperation14 } from "roc-db";
|
|
428
|
+
|
|
429
|
+
// src/schemas/UpdatePostDescriptionMutationSchema.ts
|
|
430
|
+
import { mutationSchemaGenerator as mutationSchemaGenerator16 } from "roc-db";
|
|
431
|
+
import { z as z21 } from "zod";
|
|
432
|
+
var UpdatePostDescriptionMutationSchema = mutationSchemaGenerator16("updatePostDescription", z21.object({ ref: PostRefSchema, description: z21.string() }));
|
|
433
|
+
|
|
434
|
+
// src/operations/updatePostDescription.ts
|
|
435
|
+
var updatePostDescription = writeOperation14(UpdatePostDescriptionMutationSchema, PostSchema, (txn) => {
|
|
436
|
+
const { ref, description } = txn.payload;
|
|
437
|
+
return Query16(() => txn.patchEntity(ref, { data: { description } }));
|
|
438
|
+
}, { debounce: 10, changeSetOnly: true });
|
|
439
|
+
|
|
322
440
|
// src/operations/updatePostTitle.ts
|
|
323
|
-
import { Query as
|
|
324
|
-
var updatePostTitle =
|
|
441
|
+
import { Query as Query17, writeOperation as writeOperation15 } from "roc-db";
|
|
442
|
+
var updatePostTitle = writeOperation15(UpdatePostTitleMutationSchema, PostSchema, (txn) => {
|
|
325
443
|
const { ref, title } = txn.payload;
|
|
326
|
-
return
|
|
444
|
+
return Query17(() => txn.patchEntity(ref, { data: { title } }));
|
|
327
445
|
}, { debounce: 10 });
|
|
328
446
|
|
|
329
447
|
// src/operations/index.ts
|
|
330
448
|
var operations = [
|
|
449
|
+
applyDraft,
|
|
331
450
|
createBlockImage,
|
|
332
451
|
createBlockTitle,
|
|
333
452
|
createBlockParagrah,
|
|
334
453
|
createBlockRow,
|
|
335
454
|
createDraft,
|
|
336
455
|
createPost,
|
|
337
|
-
|
|
456
|
+
deleteBlocks,
|
|
338
457
|
deletePost,
|
|
458
|
+
moveBlocks,
|
|
339
459
|
readEntity,
|
|
340
460
|
readPost,
|
|
461
|
+
testTransactionalEdits,
|
|
341
462
|
updateBlockParagraph,
|
|
342
463
|
updatePost,
|
|
464
|
+
updatePostDescription,
|
|
343
465
|
updatePostTitle
|
|
344
466
|
];
|
|
345
467
|
// src/testAdapterImplementation.ts
|
|
346
|
-
import { beforeAll, describe, expect, test } from "bun:test";
|
|
347
|
-
import { NotFoundError as NotFoundError2 } from "roc-db";
|
|
348
|
-
var
|
|
349
|
-
|
|
468
|
+
import { beforeAll, describe, expect as expect2, test } from "bun:test";
|
|
469
|
+
import { BadRequestError, NotFoundError as NotFoundError2, Snowflake } from "roc-db";
|
|
470
|
+
var snowflake = new Snowflake(10, 10);
|
|
471
|
+
var prepareChangeSetTest = async (adapter, adapter2) => {
|
|
472
|
+
const [post, createPostMutation] = await adapter.createPost({
|
|
350
473
|
title: "Title 1",
|
|
351
474
|
slug: "title-1",
|
|
352
475
|
tags: ["Foo", "Bar"]
|
|
353
476
|
});
|
|
354
|
-
|
|
477
|
+
expect2(createPostMutation.log).toStrictEqual([[post.ref, "create"]]);
|
|
478
|
+
const [draft, createDraftMutation] = await adapter.createDraft({
|
|
355
479
|
postRef: post.ref
|
|
356
480
|
});
|
|
481
|
+
expect2(createDraftMutation.log).toStrictEqual([[draft.ref, "create"]]);
|
|
357
482
|
const changeSetAdapter = adapter.changeSet(draft.ref);
|
|
358
|
-
const [
|
|
483
|
+
const [
|
|
484
|
+
{ block: blockParagraph1, updatedParent: updatedPost },
|
|
485
|
+
createBlockParagraphMutation
|
|
486
|
+
] = await changeSetAdapter.createBlockParagraph({
|
|
359
487
|
parentRef: post.ref
|
|
360
488
|
});
|
|
361
|
-
|
|
489
|
+
expect2(updatedPost.children.blocks).toStrictEqual([blockParagraph1.ref]);
|
|
490
|
+
expect2(updatedPost.children.blocks).toHaveLength(1);
|
|
491
|
+
expect2(createBlockParagraphMutation.log[0][2].children.blocks).toStrictEqual([]);
|
|
492
|
+
const readPost1 = await changeSetAdapter.readEntity(post.ref);
|
|
493
|
+
expect2(readPost1.children.blocks).toHaveLength(1);
|
|
494
|
+
const [{ block: blockRow, updatedParent: updatedPost2 }, mutation] = await changeSetAdapter.createBlockRow({
|
|
362
495
|
parentRef: post.ref
|
|
363
496
|
});
|
|
364
|
-
|
|
497
|
+
expect2(updatedPost2.children.blocks).toHaveLength(2);
|
|
498
|
+
const readPost2 = await changeSetAdapter.readEntity(post.ref);
|
|
499
|
+
expect2(readPost2.children.blocks).toHaveLength(2);
|
|
500
|
+
const [{ block: blockParagraph2, updatedParent: updatedBlockRow }] = await changeSetAdapter.createBlockParagraph({
|
|
365
501
|
parentRef: blockRow.ref
|
|
366
502
|
});
|
|
503
|
+
const readPost3 = await changeSetAdapter.readEntity(post.ref);
|
|
504
|
+
expect2(readPost3.children.blocks).toHaveLength(2);
|
|
367
505
|
return {
|
|
368
506
|
postRef: post.ref,
|
|
369
507
|
draftRef: draft.ref,
|
|
370
508
|
changeSetAdapter,
|
|
371
509
|
blockParagraph1,
|
|
372
510
|
blockParagraph2,
|
|
373
|
-
blockRow,
|
|
374
|
-
|
|
511
|
+
blockRow: updatedBlockRow,
|
|
512
|
+
draft,
|
|
513
|
+
post: readPost3
|
|
375
514
|
};
|
|
376
515
|
};
|
|
377
|
-
var testAdapterImplementation = (adapterConstructor,
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
516
|
+
var testAdapterImplementation = async (adapterConstructor, generateArgs) => {
|
|
517
|
+
let createAdapter = async ({ optimistic = false } = {}) => {
|
|
518
|
+
return adapterConstructor({
|
|
519
|
+
operations,
|
|
520
|
+
entities,
|
|
521
|
+
session: { identityRef: "User/42" },
|
|
522
|
+
snowflake,
|
|
523
|
+
optimistic,
|
|
524
|
+
...await generateArgs()
|
|
525
|
+
});
|
|
526
|
+
};
|
|
527
|
+
let adapter1, adapter2;
|
|
528
|
+
beforeAll(async () => {
|
|
529
|
+
adapter1 = await createAdapter();
|
|
530
|
+
adapter2 = await createAdapter();
|
|
531
|
+
});
|
|
532
|
+
describe("Post CRUD", () => {
|
|
533
|
+
let postRef;
|
|
534
|
+
let post;
|
|
535
|
+
let createPostMutation;
|
|
536
|
+
beforeAll(async () => {
|
|
537
|
+
const createPostRes = await adapter1.createPost({
|
|
538
|
+
title: "Title 1",
|
|
539
|
+
slug: "title-1",
|
|
540
|
+
tags: ["Foo", "Bar"]
|
|
392
541
|
});
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
542
|
+
post = createPostRes[0];
|
|
543
|
+
postRef = createPostRes[0].ref;
|
|
544
|
+
createPostMutation = createPostRes[1];
|
|
545
|
+
});
|
|
546
|
+
test("create (createEntity function)", async () => {
|
|
547
|
+
expect2(post).toMatchZodSchema(PostSchema);
|
|
548
|
+
expect2(post.data.title).toBe("Title 1");
|
|
549
|
+
expect2(post.data.tags).toStrictEqual(["Foo", "Bar"]);
|
|
550
|
+
expect2(createPostMutation).toMatchZodSchema(CreatePostMutationSchema);
|
|
551
|
+
});
|
|
552
|
+
test("read (readEntity function)", async () => {
|
|
553
|
+
const readPostRes = await adapter1.readPost(postRef);
|
|
554
|
+
expect2(readPostRes.data.title).toStrictEqual("Title 1");
|
|
555
|
+
expect2(readPostRes.data.tags).toStrictEqual(["Foo", "Bar"]);
|
|
556
|
+
expect2(readPostRes).toMatchZodSchema(PostSchema);
|
|
557
|
+
});
|
|
558
|
+
test("read not found (readEntity function)", async () => {
|
|
559
|
+
expect2(() => adapter1.readPost("Post/999")).toThrow(NotFoundError2);
|
|
560
|
+
});
|
|
561
|
+
test.todo("update (updateEntity function)", async () => {
|
|
562
|
+
const [post2] = await adapter1.updatePost({
|
|
563
|
+
ref: postRef,
|
|
564
|
+
title: "Title 2",
|
|
565
|
+
tags: ["Foo"]
|
|
396
566
|
});
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
567
|
+
expect2(post2.data.tags).toStrictEqual(["Foo"]);
|
|
568
|
+
expect2(post2.data.title).toBe("Title 2");
|
|
569
|
+
expect2(post2).toMatchZodSchema(PostSchema);
|
|
570
|
+
});
|
|
571
|
+
test("updateTitle (patchEntity function + debounce)", async () => {
|
|
572
|
+
const [update1, mutation1] = await adapter1.updatePostTitle({
|
|
573
|
+
ref: postRef,
|
|
574
|
+
title: "Title 3"
|
|
400
575
|
});
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
[blockRow.ref, "delete", draftRef],
|
|
407
|
-
[blockParagraph2.ref, "delete", draftRef],
|
|
408
|
-
[postRef, "delete", draftRef]
|
|
409
|
-
]);
|
|
576
|
+
expect2(update1).toMatchZodSchema(PostSchema);
|
|
577
|
+
expect2(mutation1).toMatchZodSchema(UpdatePostTitleMutationSchema);
|
|
578
|
+
const [update2, mutation2] = await adapter1.updatePostTitle({
|
|
579
|
+
ref: postRef,
|
|
580
|
+
title: "Title 4"
|
|
410
581
|
});
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
582
|
+
expect2(update2).toMatchZodSchema(PostSchema);
|
|
583
|
+
expect2(mutation2).toMatchZodSchema(UpdatePostTitleMutationSchema);
|
|
584
|
+
expect2(mutation1.ref).toBe(mutation2.ref);
|
|
585
|
+
expect2(mutation2.timestamp >= mutation1.timestamp).toBeTrue();
|
|
586
|
+
});
|
|
587
|
+
test("updatePostDescription without changeSetAdapter fails with BadRequestError", async () => {
|
|
588
|
+
expect2(() => adapter1.updatePostDescription({
|
|
589
|
+
ref: postRef,
|
|
590
|
+
description: "Description 1"
|
|
591
|
+
})).toThrow(BadRequestError);
|
|
592
|
+
});
|
|
593
|
+
test("delete (deleteEntity function)", async () => {
|
|
594
|
+
const currentPost = await adapter1.readPost(postRef);
|
|
595
|
+
expect2(currentPost.ref).toBe(postRef);
|
|
596
|
+
const [count, mutation] = await adapter1.deletePost(postRef);
|
|
597
|
+
expect2(count).toBe(1);
|
|
598
|
+
expect2(mutation.log).toStrictEqual([
|
|
599
|
+
[postRef, "delete", currentPost]
|
|
600
|
+
]);
|
|
601
|
+
expect2(() => adapter1.readPost(postRef)).toThrow(NotFoundError2);
|
|
602
|
+
});
|
|
603
|
+
test("sync mutation", async () => {
|
|
604
|
+
const [{ persistedAt, ...mutation }] = await adapter2.persistOptimisticMutations([createPostMutation]);
|
|
605
|
+
expect2(mutation).toStrictEqual(createPostMutation);
|
|
606
|
+
});
|
|
607
|
+
test("create, patch and delete the same item within the same transaction", async () => {
|
|
608
|
+
const [post1] = await adapter1.createPost({ title: "Post 1" });
|
|
609
|
+
const [post2] = await adapter1.createPost({ title: "Post 1" });
|
|
610
|
+
const [currentPost, mutation] = await adapter1.testTransactionalEdits({
|
|
611
|
+
name: "Test"
|
|
417
612
|
});
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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);
|
|
613
|
+
expect2(mutation.log).toHaveLength(0);
|
|
614
|
+
const [, mutation2] = await adapter1.testTransactionalEdits({
|
|
615
|
+
post1ref: post1.ref
|
|
429
616
|
});
|
|
617
|
+
expect2(mutation2.log).toHaveLength(1);
|
|
618
|
+
expect2(mutation2.log[0][1]).toBe("delete");
|
|
619
|
+
expect2(() => adapter1.testTransactionalEdits({
|
|
620
|
+
post2ref: post2.ref
|
|
621
|
+
})).toThrow(BadRequestError);
|
|
430
622
|
});
|
|
431
|
-
};
|
|
623
|
+
});
|
|
624
|
+
describe("changeSet", () => {
|
|
625
|
+
let postRef, draftRef, changeSetAdapter, blockParagraph1, post, blockRow, blockParagraph2, draft;
|
|
626
|
+
beforeAll(async () => {
|
|
627
|
+
({
|
|
628
|
+
postRef,
|
|
629
|
+
draftRef,
|
|
630
|
+
changeSetAdapter,
|
|
631
|
+
blockParagraph1,
|
|
632
|
+
post,
|
|
633
|
+
blockRow,
|
|
634
|
+
blockParagraph2,
|
|
635
|
+
draft
|
|
636
|
+
} = await prepareChangeSetTest(adapter1, adapter2));
|
|
637
|
+
});
|
|
638
|
+
test("create (createEntity function w/changeSetRef)", async () => {
|
|
639
|
+
expect2(blockParagraph1).toMatchZodSchema(BlockParagraphSchema);
|
|
640
|
+
expect2(post).toMatchZodSchema(PostSchema);
|
|
641
|
+
});
|
|
642
|
+
test("read (readEntity function w/changeSetRef)", async () => {
|
|
643
|
+
const read = await changeSetAdapter.readEntity(blockParagraph1.ref);
|
|
644
|
+
expect2(read).toMatchZodSchema(BlockParagraphSchema);
|
|
645
|
+
});
|
|
646
|
+
test("update (patchEntity function w/changeSetRef)", async () => {
|
|
647
|
+
const [updatedBlock] = await changeSetAdapter.updateBlockParagraph({
|
|
648
|
+
ref: blockParagraph1.ref,
|
|
649
|
+
content: "Hello, world!"
|
|
650
|
+
});
|
|
651
|
+
expect2(updatedBlock).toMatchZodSchema(BlockParagraphSchema);
|
|
652
|
+
});
|
|
653
|
+
test("move (batchReadEntities function)", async () => {
|
|
654
|
+
const res = await changeSetAdapter.moveBlocks({
|
|
655
|
+
refs: [blockParagraph1.ref],
|
|
656
|
+
targetPosition: 1
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
test("delete (with cascade delete)", async () => {
|
|
660
|
+
const postUpdated = await changeSetAdapter.readEntity(postRef);
|
|
661
|
+
const blockParagraph1Update = await changeSetAdapter.readEntity(blockParagraph1.ref);
|
|
662
|
+
const blockRowUpdated = await changeSetAdapter.readEntity(blockRow.ref);
|
|
663
|
+
const [count, mutation] = await changeSetAdapter.deletePost(postRef);
|
|
664
|
+
expect2(count).toBe(4);
|
|
665
|
+
expect2(mutation.log).toStrictEqual([
|
|
666
|
+
[blockParagraph1.ref, "delete", blockParagraph1Update],
|
|
667
|
+
[blockParagraph2.ref, "delete", blockParagraph2],
|
|
668
|
+
[blockRow.ref, "delete", blockRowUpdated],
|
|
669
|
+
[postRef, "delete", postUpdated]
|
|
670
|
+
]);
|
|
671
|
+
await expect2(() => changeSetAdapter.readEntity(blockParagraph1.ref)).toThrow(NotFoundError2);
|
|
672
|
+
await expect2(() => changeSetAdapter.updateBlockParagraph({
|
|
673
|
+
ref: blockParagraph1.ref,
|
|
674
|
+
content: "Foo"
|
|
675
|
+
})).toThrow(NotFoundError2);
|
|
676
|
+
});
|
|
677
|
+
test("pageMutations", async () => {
|
|
678
|
+
const res = await changeSetAdapter.pageMutations({
|
|
679
|
+
changeSetRef: draftRef
|
|
680
|
+
});
|
|
681
|
+
expect2(res.length).toBeGreaterThan(0);
|
|
682
|
+
expect2(res.every((m) => m.changeSetRef === draftRef)).toBeTrue();
|
|
683
|
+
});
|
|
684
|
+
test("pageMutations run again", async () => {
|
|
685
|
+
const res = await changeSetAdapter.pageMutations({
|
|
686
|
+
changeSetRef: draftRef
|
|
687
|
+
});
|
|
688
|
+
expect2(res.length).toBeGreaterThan(0);
|
|
689
|
+
expect2(res.every((m) => m.changeSetRef === draftRef)).toBeTrue();
|
|
690
|
+
});
|
|
691
|
+
test("invalid changeSetRef throws error", async () => {
|
|
692
|
+
const changeSetAdapter2 = adapter1.changeSet("Draft/123");
|
|
693
|
+
expect2(() => changeSetAdapter2.createPost({ title: "Title 1" })).toThrow(BadRequestError);
|
|
694
|
+
});
|
|
695
|
+
test("deleteBlocks (patch running 2 times removing on array element every time)", async () => {
|
|
696
|
+
const [post2, createPostMutation] = await adapter1.createPost({
|
|
697
|
+
title: "Title 1",
|
|
698
|
+
slug: "title-1",
|
|
699
|
+
tags: ["Foo", "Bar"]
|
|
700
|
+
});
|
|
701
|
+
const [draft2, createDraftMutation] = await adapter1.createDraft({
|
|
702
|
+
postRef: post2.ref
|
|
703
|
+
});
|
|
704
|
+
const changeSetAdapter2 = adapter1.changeSet(draft2.ref);
|
|
705
|
+
const [{ block: blockParagraph12 }, createBlockParagraph1Mutation] = await changeSetAdapter2.createBlockParagraph({
|
|
706
|
+
parentRef: post2.ref
|
|
707
|
+
});
|
|
708
|
+
const [{ block: blockParagraph22 }, createBlockParagraph2Mutation] = await changeSetAdapter2.createBlockParagraph({
|
|
709
|
+
parentRef: post2.ref
|
|
710
|
+
});
|
|
711
|
+
const readPost2 = await changeSetAdapter2.readEntity(post2.ref);
|
|
712
|
+
expect2(readPost2.children.blocks).toEqual([
|
|
713
|
+
blockParagraph12.ref,
|
|
714
|
+
blockParagraph22.ref
|
|
715
|
+
]);
|
|
716
|
+
const [, deleteMutation] = await changeSetAdapter2.deleteBlocks([
|
|
717
|
+
blockParagraph12.ref,
|
|
718
|
+
blockParagraph22.ref
|
|
719
|
+
]);
|
|
720
|
+
expect2(deleteMutation.log[0][2].children.blocks).toEqual([
|
|
721
|
+
blockParagraph12.ref,
|
|
722
|
+
blockParagraph22.ref
|
|
723
|
+
]);
|
|
724
|
+
expect2(deleteMutation.log[0][2].updated.mutationRef).toBe(createBlockParagraph2Mutation.ref);
|
|
725
|
+
const readPost22 = await changeSetAdapter2.readEntity(post2.ref);
|
|
726
|
+
expect2(readPost22.updated.mutationRef).toEqual(deleteMutation.ref);
|
|
727
|
+
});
|
|
728
|
+
test("applyDraft", async () => {
|
|
729
|
+
const { draftRef: draftRef2 } = await prepareChangeSetTest(adapter1);
|
|
730
|
+
const [version, mutation] = await adapter1.applyDraft({
|
|
731
|
+
ref: draftRef2
|
|
732
|
+
});
|
|
733
|
+
});
|
|
734
|
+
test("init adapter2", async () => {
|
|
735
|
+
const adapterA = await createAdapter({ optimistic: true });
|
|
736
|
+
const adapterB = await createAdapter({ optimistic: true });
|
|
737
|
+
const { draftRef: draftRef2 } = await prepareChangeSetTest(adapterA, adapterB);
|
|
738
|
+
const mutations = await adapterA.pageMutations({
|
|
739
|
+
changeSetRef: draftRef2
|
|
740
|
+
});
|
|
741
|
+
const allMutationsAdapter1 = await adapterA.pageMutations({});
|
|
742
|
+
const allMutationsAdapter2 = await adapterB.pageMutations({});
|
|
743
|
+
expect2(allMutationsAdapter1).not.toBeEmpty();
|
|
744
|
+
expect2(allMutationsAdapter2).toBeEmpty();
|
|
745
|
+
const res = await adapterB.loadMutations(allMutationsAdapter1);
|
|
746
|
+
const allMutationsAdapter2after = await adapterB.pageMutations({});
|
|
747
|
+
expect2(allMutationsAdapter2after.length).toBe(allMutationsAdapter1.length);
|
|
748
|
+
const res2 = await adapterB.loadMutations(allMutationsAdapter1);
|
|
749
|
+
});
|
|
750
|
+
test("persistOptimisticMutations", async () => {
|
|
751
|
+
const persistedAdapter = await createAdapter({ optimistic: false });
|
|
752
|
+
const optimisticAdapter = await createAdapter({ optimistic: true });
|
|
753
|
+
expect2(persistedAdapter.persistOptimisticMutations).toBeDefined();
|
|
754
|
+
expect2(optimisticAdapter.loadMutations).toBeDefined();
|
|
755
|
+
const [, createPostMutation] = await optimisticAdapter.createPost({
|
|
756
|
+
title: "Foo",
|
|
757
|
+
tags: []
|
|
758
|
+
});
|
|
759
|
+
const [persistCratePostMutation] = await persistedAdapter.persistOptimisticMutations([
|
|
760
|
+
createPostMutation
|
|
761
|
+
]);
|
|
762
|
+
expect2(persistCratePostMutation.persistedAt >= createPostMutation.timestamp).toBeTrue();
|
|
763
|
+
});
|
|
764
|
+
});
|
|
432
765
|
};
|
|
433
766
|
// src/inMemoryAdapter.ts
|
|
434
767
|
import { createInMemoryAdapter } from "@roc-db/in-memory";
|
|
435
768
|
var inMemoryAdapter = createInMemoryAdapter({
|
|
436
769
|
operations,
|
|
437
|
-
entities
|
|
770
|
+
entities,
|
|
771
|
+
session: {
|
|
772
|
+
identityRef: "User/42"
|
|
773
|
+
}
|
|
438
774
|
});
|
|
439
775
|
export {
|
|
440
776
|
testAdapterImplementation,
|