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