on-zero 0.9.7 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/cjs/generate-helpers.test.cjs +0 -38
  2. package/dist/cjs/generate-helpers.test.native.js +0 -38
  3. package/dist/cjs/generate-helpers.test.native.js.map +1 -1
  4. package/dist/cjs/generate-lite.test.cjs +0 -522
  5. package/dist/cjs/generate-lite.test.native.js +0 -525
  6. package/dist/cjs/generate-lite.test.native.js.map +1 -1
  7. package/dist/cjs/generate.test.cjs +0 -691
  8. package/dist/cjs/generate.test.native.js +0 -691
  9. package/dist/cjs/generate.test.native.js.map +1 -1
  10. package/dist/cjs/mutations.test.cjs +0 -11
  11. package/dist/cjs/mutations.test.native.js +0 -18
  12. package/dist/cjs/mutations.test.native.js.map +1 -1
  13. package/dist/cjs/server.test.cjs +0 -93
  14. package/dist/cjs/server.test.native.js +0 -101
  15. package/dist/cjs/server.test.native.js.map +1 -1
  16. package/dist/cjs/serverWhere.test.cjs +0 -45
  17. package/dist/cjs/serverWhere.test.native.js +0 -51
  18. package/dist/cjs/serverWhere.test.native.js.map +1 -1
  19. package/dist/esm/generate-helpers.test.mjs +1 -39
  20. package/dist/esm/generate-helpers.test.mjs.map +1 -1
  21. package/dist/esm/generate-helpers.test.native.js +1 -39
  22. package/dist/esm/generate-helpers.test.native.js.map +1 -1
  23. package/dist/esm/generate-lite.test.mjs +0 -522
  24. package/dist/esm/generate-lite.test.mjs.map +1 -1
  25. package/dist/esm/generate-lite.test.native.js +0 -525
  26. package/dist/esm/generate-lite.test.native.js.map +1 -1
  27. package/dist/esm/generate.test.mjs +2 -668
  28. package/dist/esm/generate.test.mjs.map +1 -1
  29. package/dist/esm/generate.test.native.js +2 -668
  30. package/dist/esm/generate.test.native.js.map +1 -1
  31. package/dist/esm/mutations.test.mjs +0 -11
  32. package/dist/esm/mutations.test.mjs.map +1 -1
  33. package/dist/esm/mutations.test.native.js +0 -18
  34. package/dist/esm/mutations.test.native.js.map +1 -1
  35. package/dist/esm/server.test.mjs +0 -93
  36. package/dist/esm/server.test.mjs.map +1 -1
  37. package/dist/esm/server.test.native.js +0 -101
  38. package/dist/esm/server.test.native.js.map +1 -1
  39. package/dist/esm/serverWhere.test.mjs +0 -45
  40. package/dist/esm/serverWhere.test.mjs.map +1 -1
  41. package/dist/esm/serverWhere.test.native.js +0 -51
  42. package/dist/esm/serverWhere.test.native.js.map +1 -1
  43. package/package.json +1 -1
  44. package/src/generate-helpers.test.ts +0 -40
  45. package/src/generate-lite.test.ts +0 -586
  46. package/src/generate.test.ts +0 -844
  47. package/src/mutations.test.ts +0 -21
  48. package/src/server.test.ts +0 -75
  49. package/src/serverWhere.test.ts +0 -42
@@ -1,37 +1,11 @@
1
1
  "use strict";
2
2
 
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: () => from[key],
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
19
- // If the importer is in node compatibility mode or this is not an ESM
20
- // file that has been converted to a CommonJS file using a Babel-
21
- // compatible transform (i.e. "__esModule" has not been set), then set
22
- // "default" to the CommonJS "module.exports" for node compatibility.
23
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
- value: mod,
25
- enumerable: true
26
- }) : target, mod));
27
3
  var import_fs = require("fs");
28
4
  var import_os = require("os");
29
5
  var import_path = require("path");
30
6
  var import_vm = require("vm");
31
- var zero = __toESM(require("@rocicorp/zero"), 1);
32
7
  var import_vitest = require("vitest");
33
8
  var import_generate = require("./generate.native.js");
34
- const import_meta = {};
35
9
  var testDir = (0, import_path.join)((0, import_os.tmpdir)(), "on-zero-test-" + Date.now());
36
10
  function writeFileSync(path, content) {
37
11
  (0, import_fs.mkdirSync)((0, import_path.dirname)(path), {
@@ -52,105 +26,6 @@ function writeFileSync(path, content) {
52
26
  });
53
27
  });
54
28
  (0, import_vitest.describe)("generate", function () {
55
- (0, import_vitest.test)("generates models.ts, types.ts, tables.ts from model files", async function () {
56
- writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `
57
- import { table, string, boolean } from 'on-zero'
58
-
59
- export const schema = table('post', {
60
- id: string(),
61
- title: string(),
62
- published: boolean(),
63
- })
64
- `);
65
- writeFileSync((0, import_path.join)(testDir, "comment/mutations.ts"), `
66
- import { table, string } from 'on-zero'
67
-
68
- export const schema = table('comment', {
69
- id: string(),
70
- postId: string(),
71
- body: string(),
72
- })
73
- `);
74
- writeFileSync((0, import_path.join)(testDir, "post/post.test.ts"), `throw new Error('test files must not be generated as models')`);
75
- writeFileSync((0, import_path.join)(testDir, "comment/comment.spec.ts"), `throw new Error('spec files must not be generated as queries')`);
76
- var result = await (0, import_generate.generate)({
77
- dir: testDir,
78
- silent: true
79
- });
80
- (0, import_vitest.expect)(result.modelCount).toBe(2);
81
- (0, import_vitest.expect)(result.schemaCount).toBe(2);
82
- (0, import_vitest.expect)(result.filesChanged).toBeGreaterThan(0);
83
- (0, import_vitest.expect)((0, import_fs.existsSync)((0, import_path.join)(testDir, "generated/models.ts"))).toBe(true);
84
- (0, import_vitest.expect)((0, import_fs.existsSync)((0, import_path.join)(testDir, "generated/types.ts"))).toBe(true);
85
- (0, import_vitest.expect)((0, import_fs.existsSync)((0, import_path.join)(testDir, "generated/tables.ts"))).toBe(true);
86
- var modelsContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/models.ts"), "utf-8");
87
- (0, import_vitest.expect)(modelsContent).toContain("import * as comment from '../comment/mutations'");
88
- (0, import_vitest.expect)(modelsContent).toContain("import * as post from '../post/mutations'");
89
- (0, import_vitest.expect)(modelsContent).not.toContain("post.test");
90
- (0, import_vitest.expect)(modelsContent).toContain("export const models = {");
91
- var typesContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/types.ts"), "utf-8");
92
- (0, import_vitest.expect)(typesContent).toContain("export type Post = TableInsertRow<typeof schema.post>");
93
- (0, import_vitest.expect)(typesContent).toContain("export type Comment = TableInsertRow<typeof schema.comment>");
94
- var tablesContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/tables.ts"), "utf-8");
95
- (0, import_vitest.expect)(tablesContent).toContain("export { schema as post } from '../post/mutations'");
96
- (0, import_vitest.expect)(tablesContent).toContain("export { schema as comment } from '../comment/mutations'");
97
- });
98
- (0, import_vitest.test)("generates query validators from query files", async function () {
99
- writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `export const schema = table('post', { id: string() })`);
100
- writeFileSync((0, import_path.join)(testDir, "post/queries.ts"), `
101
- import { zero } from '../zero'
102
-
103
- export const allPosts = () => zero.query.post
104
-
105
- export const postById = ({ id }: { id: string }) => zero.query.post.where('id', id)
106
-
107
- export const postsByAuthor = ({ authorId, limit }: { authorId: string; limit?: number }) =>
108
- zero.query.post.where('authorId', authorId).limit(limit ?? 10)
109
- `);
110
- var result = await (0, import_generate.generate)({
111
- dir: testDir,
112
- silent: true
113
- });
114
- (0, import_vitest.expect)(result.queryCount).toBe(3);
115
- (0, import_vitest.expect)((0, import_fs.existsSync)((0, import_path.join)(testDir, "generated/groupedQueries.ts"))).toBe(true);
116
- (0, import_vitest.expect)((0, import_fs.existsSync)((0, import_path.join)(testDir, "generated/syncedQueries.ts"))).toBe(true);
117
- var groupedContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/groupedQueries.ts"), "utf-8");
118
- (0, import_vitest.expect)(groupedContent).toContain("import * as postSource from '../post/queries'");
119
- (0, import_vitest.expect)(groupedContent).toContain("postById: postSource.postById");
120
- var syncedContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedQueries.ts"), "utf-8");
121
- (0, import_vitest.expect)(syncedContent).toContain("allPosts: defineQuery");
122
- (0, import_vitest.expect)(syncedContent).toContain("postById: defineQuery");
123
- (0, import_vitest.expect)(syncedContent).toContain("postsByAuthor: defineQuery");
124
- (0, import_vitest.expect)(syncedContent).toContain("v.object");
125
- });
126
- (0, import_vitest.test)("skips permission exports in queries", async function () {
127
- writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `export const schema = table('post', { id: string() })`);
128
- writeFileSync((0, import_path.join)(testDir, "post/queries.ts"), `
129
- export const permission = () => ({ canRead: true })
130
- export const allPosts = () => zero.query.post
131
- `);
132
- var result = await (0, import_generate.generate)({
133
- dir: testDir,
134
- silent: true
135
- });
136
- (0, import_vitest.expect)(result.queryCount).toBe(1);
137
- var syncedContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedQueries.ts"), "utf-8");
138
- (0, import_vitest.expect)(syncedContent).toContain("allPosts");
139
- (0, import_vitest.expect)(syncedContent).not.toContain("permission:");
140
- });
141
- (0, import_vitest.test)("aliases user import without changing the model key", async function () {
142
- writeFileSync((0, import_path.join)(testDir, "user/mutations.ts"), `export const schema = table('user', { id: string(), name: string() })`);
143
- await (0, import_generate.generate)({
144
- dir: testDir,
145
- silent: true
146
- });
147
- var modelsContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/models.ts"), "utf-8");
148
- (0, import_vitest.expect)(modelsContent).toContain("import * as userPublic from '../user/mutations'");
149
- (0, import_vitest.expect)(modelsContent).toContain("user: userPublic,");
150
- (0, import_vitest.expect)(modelsContent).not.toContain("\n userPublic,");
151
- var typesContent = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/types.ts"), "utf-8");
152
- (0, import_vitest.expect)(typesContent).toContain("typeof schema.userPublic");
153
- });
154
29
  (0, import_vitest.test)("runs after command when files change", async function () {
155
30
  writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `export const schema = table('post', { id: string() })`);
156
31
  var markerFile = (0, import_path.join)(testDir, "after-ran");
@@ -196,51 +71,6 @@ export const allPosts = () => zero.query.post
196
71
  var dataDir = function dataDir2() {
197
72
  return (0, import_path.join)(testDir, "src/data");
198
73
  };
199
- (0, import_vitest.test)("discovers file and folder namespaces and emits related sync closure", async function () {
200
- writeFileSync((0, import_path.join)(dataDir(), "control/reaction.ts"), `export const allReactions = () => zql.reaction`);
201
- writeFileSync((0, import_path.join)(dataDir(), "on-zero.config.ts"), `export default defineConfig({ instances: { control: {}, project: { scope: 'projectId' } } })`);
202
- writeFileSync((0, import_path.join)(dataDir(), "project/message/queries.ts"), `
203
- const unused = () => zql.message.related('notARealRelation')
204
- const withComments = () => zql.message.related('comments', (q) => q.related('author'))
205
- export const messages = () => withComments()
206
- `);
207
- writeFileSync((0, import_path.join)(dataDir(), "project/message/mutations.ts"), `export const schema = table('message').columns({ id: string(), projectId: string() })`);
208
- writeFileSync((0, import_path.join)(dataDir(), "project/message/helpers.ts"), `export const privateHelper = () => 'ignored'`);
209
- writeFileSync((0, import_path.join)(testDir, "src/database/relations.ts"), `
210
- export const relations = defineRelations(schema, (r) => ({
211
- message: { comments: r.many.comment({}) },
212
- comment: { author: r.one.userPublic({}) },
213
- }))
214
- `);
215
- writeFileSync((0, import_path.join)(testDir, "src/database/schema.ts"), `
216
- export const comment = sqliteTable('comment', { id: text(), projectId: text() })
217
- export const userPublic = sqliteTable('user_public', { id: text(), projectId: text() })
218
- `);
219
- var membership = await (0, import_generate.deriveDataMembership)({
220
- dir: dataDir()
221
- });
222
- (0, import_vitest.expect)(membership.allTables).toEqual(["comment", "message", "reaction", "userPublic"]);
223
- (0, import_vitest.expect)(membership.instances.project).toEqual({
224
- tables: ["message"],
225
- syncTables: ["comment", "message", "userPublic"],
226
- supportTables: [],
227
- scope: "projectId"
228
- });
229
- await (0, import_generate.generate)({
230
- dir: dataDir(),
231
- silent: true
232
- });
233
- var grouped = (0, import_fs.readFileSync)((0, import_path.join)(dataDir(), "generated/groupedQueries.ts"), "utf8");
234
- (0, import_vitest.expect)(grouped).toContain("from '../control/reaction'");
235
- (0, import_vitest.expect)(grouped).toContain("from '../project/message/queries'");
236
- (0, import_vitest.expect)(grouped).not.toContain("privateHelper");
237
- var manifest = (0, import_fs.readFileSync)((0, import_path.join)(dataDir(), "generated/instances.ts"), "utf8");
238
- (0, import_vitest.expect)(manifest).toContain("control: {");
239
- (0, import_vitest.expect)(manifest).toContain("project: {");
240
- (0, import_vitest.expect)(manifest).toContain('tables: ["message"]');
241
- (0, import_vitest.expect)(manifest).toContain('syncTables: ["comment","message","userPublic"]');
242
- (0, import_vitest.expect)(manifest).toContain(`defaultVisibility: (value: string) => ({ column: "projectId", value })`);
243
- });
244
74
  (0, import_vitest.test)("derives query membership from the returned query root", async function () {
245
75
  writeFileSync((0, import_path.join)(dataDir(), "category/mutations.ts"), `export const schema = table('category').columns({ id: string() })`);
246
76
  writeFileSync((0, import_path.join)(dataDir(), "dashboard/queries.ts"), `export const monthSummary = () => zql.category.related('expenses')`);
@@ -459,42 +289,6 @@ export const messages = () => zql.message.related('author')
459
289
  silent: true
460
290
  })).rejects.toThrow(/namespace 'message'.*instances 'control' and 'project'/);
461
291
  });
462
- (0, import_vitest.test)("resolves configured instance directories relative to the config file", async function () {
463
- writeFileSync((0, import_path.join)(dataDir(), "on-zero.config.ts"), `export default defineConfig({ instances: { control: { dir: './planes/control-data', supportTables: ['audit'] }, project: { dir: '../project-data', scope: 'projectId' } } })`);
464
- writeFileSync((0, import_path.join)(dataDir(), "planes/control-data/account.ts"), `export const accounts = () => zql.account`);
465
- writeFileSync((0, import_path.join)(testDir, "src/project-data/message.ts"), `export const schema = table('message').columns({ id: string(), projectId: string() })`);
466
- await (0, import_vitest.expect)((0, import_generate.deriveDataMembership)({
467
- dir: dataDir(),
468
- config: (0, import_path.join)(dataDir(), "on-zero.config.ts")
469
- })).resolves.toEqual({
470
- instances: {
471
- control: {
472
- tables: ["account"],
473
- syncTables: ["account"],
474
- supportTables: ["audit"],
475
- scope: null
476
- },
477
- project: {
478
- tables: ["message"],
479
- syncTables: ["message"],
480
- supportTables: [],
481
- scope: "projectId"
482
- }
483
- },
484
- allTables: ["account", "audit", "message"]
485
- });
486
- await (0, import_vitest.expect)((0, import_generate.generate)({
487
- dir: dataDir(),
488
- config: (0, import_path.join)(dataDir(), "on-zero.config.ts"),
489
- silent: true
490
- })).resolves.toMatchObject({
491
- modelCount: 1,
492
- schemaCount: 1
493
- });
494
- var models = (0, import_fs.readFileSync)((0, import_path.join)(dataDir(), "generated/models.ts"), "utf8");
495
- (0, import_vitest.expect)(models).toContain("from '../../project-data/message'");
496
- (0, import_vitest.expect)(models).not.toContain("account");
497
- });
498
292
  (0, import_vitest.test)("rejects missing configured instance directories", async function () {
499
293
  writeFileSync((0, import_path.join)(dataDir(), "on-zero.config.ts"), `export default defineConfig({ instances: { project: { dir: './missing' } } })`);
500
294
  await (0, import_vitest.expect)((0, import_generate.deriveDataMembership)({
@@ -539,253 +333,6 @@ export const messages = () => zql.message.related('author')
539
333
  });
540
334
  });
541
335
  (0, import_vitest.describe)("mutations", function () {
542
- (0, import_vitest.test)("generates validators for inline mutation param types", async function () {
543
- writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `
544
- import { table, string } from 'on-zero'
545
- import { mutations, serverWhere } from 'on-zero'
546
-
547
- export const schema = table('post').columns({
548
- id: string(),
549
- title: string(),
550
- }).primaryKey('id')
551
-
552
- const perm = serverWhere('post', () => true)
553
-
554
- export const mutate = mutations(schema, perm, {
555
- archive: async ({ tx }, { id, reason }: { id: string; reason: string }) => {
556
- await tx.mutate.post.update({ id, archived: true })
557
- },
558
- })
559
- `);
560
- var result = await (0, import_generate.generate)({
561
- dir: testDir,
562
- silent: true
563
- });
564
- (0, import_vitest.expect)(result.mutationCount).toBeGreaterThan(0);
565
- (0, import_vitest.expect)((0, import_fs.existsSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"))).toBe(true);
566
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
567
- (0, import_vitest.expect)(content).toContain("archive");
568
- (0, import_vitest.expect)(content).toContain("v.object");
569
- (0, import_vitest.expect)(content).toContain("v.string()");
570
- });
571
- (0, import_vitest.test)("generates CRUD validators from schema columns", async function () {
572
- writeFileSync((0, import_path.join)(testDir, "task/mutations.ts"), `
573
- import { table, string, number, boolean } from 'on-zero'
574
- import { mutations, serverWhere } from 'on-zero'
575
-
576
- export const schema = table('task').columns({
577
- id: string(),
578
- title: string(),
579
- priority: number(),
580
- done: boolean(),
581
- note: string().optional(),
582
- }).primaryKey('id')
583
-
584
- const perm = serverWhere('task', () => true)
585
-
586
- export const mutate = mutations(schema, perm)
587
- `);
588
- var result = await (0, import_generate.generate)({
589
- dir: testDir,
590
- silent: true
591
- });
592
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
593
- (0, import_vitest.expect)(content).toContain("insert:");
594
- (0, import_vitest.expect)(content).toMatch(/insert:.*v\.object/);
595
- (0, import_vitest.expect)(content).toContain("update:");
596
- (0, import_vitest.expect)(content).toContain("delete:");
597
- });
598
- (0, import_vitest.test)("treats models without export const mutate as empty mutations", async function () {
599
- writeFileSync((0, import_path.join)(testDir, "readonly/mutations.ts"), `
600
- import { table, string } from 'on-zero'
601
-
602
- export const schema = table('readonly').columns({
603
- id: string(),
604
- name: string(),
605
- }).primaryKey('id')
606
- `);
607
- var result = await (0, import_generate.generate)({
608
- dir: testDir,
609
- silent: true
610
- });
611
- (0, import_vitest.expect)(result.mutationCount).toBe(0);
612
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
613
- (0, import_vitest.expect)(content).toContain("readonly: {");
614
- });
615
- (0, import_vitest.test)("extracts custom mutations from bare mutations({})", async function () {
616
- writeFileSync((0, import_path.join)(testDir, "admin/mutations.ts"), `
617
- import { mutations } from 'on-zero'
618
-
619
- export const mutate = mutations({
620
- reset: async ({ tx }, { targetId }: { targetId: string }) => {
621
- await tx.mutate.admin.delete({ id: targetId })
622
- },
623
- })
624
- `);
625
- var result = await (0, import_generate.generate)({
626
- dir: testDir,
627
- silent: true
628
- });
629
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
630
- (0, import_vitest.expect)(content).toContain("reset");
631
- (0, import_vitest.expect)(content).toContain("v.string()");
632
- });
633
- (0, import_vitest.test)("generates validators for string-model multiline mutation params", async function () {
634
- writeFileSync((0, import_path.join)(testDir, "agentEvent/mutations.ts"), `
635
- import { mutations, serverWhere } from 'on-zero'
636
-
637
- const perm = serverWhere('agentEvent', () => true)
638
-
639
- export const mutate = mutations('agentEvent', perm, {
640
- claimReviewLease: async (
641
- { tx },
642
- props: {
643
- id: string
644
- projectId: string
645
- agentId: string
646
- userId: string
647
- taskId: string
648
- reviewKey: string
649
- runnerId: string
650
- createdAt: number
651
- },
652
- ) => {
653
- await tx.mutate.agentEvent.insert(props)
654
- },
655
- })
656
- `);
657
- await (0, import_generate.generate)({
658
- dir: testDir,
659
- silent: true
660
- });
661
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
662
- (0, import_vitest.expect)(content).toContain("claimReviewLease");
663
- (0, import_vitest.expect)(content).toContain("projectId: v.string()");
664
- (0, import_vitest.expect)(content).toContain("createdAt: v.number()");
665
- (0, import_vitest.expect)(content).not.toContain("claimReviewLease: v.object({})");
666
- });
667
- (0, import_vitest.test)("generates validators for object intersections", async function () {
668
- writeFileSync((0, import_path.join)(testDir, "agent/mutations.ts"), `
669
- import { mutations, serverWhere } from 'on-zero'
670
-
671
- const perm = serverWhere('agent', () => true)
672
-
673
- export const mutate = mutations('agent', perm, {
674
- update: async (
675
- { tx },
676
- props: {
677
- id: string
678
- } & {
679
- status?: string
680
- currentTaskId?: string
681
- [key: string]: unknown
682
- },
683
- ) => {
684
- await tx.mutate.agent.update(props)
685
- },
686
- })
687
- `);
688
- await (0, import_generate.generate)({
689
- dir: testDir,
690
- silent: true
691
- });
692
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
693
- (0, import_vitest.expect)(content).toContain("update");
694
- (0, import_vitest.expect)(content).toContain("id: v.string()");
695
- (0, import_vitest.expect)(content).toContain("status: v.optional(v.string())");
696
- (0, import_vitest.expect)(content).toContain("currentTaskId: v.optional(v.string())");
697
- (0, import_vitest.expect)(content).not.toContain("update: v.object({})");
698
- (0, import_vitest.expect)(content).not.toContain("[key");
699
- });
700
- (0, import_vitest.test)("uses v.unknown for untyped mutation payloads", async function () {
701
- writeFileSync((0, import_path.join)(testDir, "project/mutations.ts"), `
702
- import { mutations, serverWhere } from 'on-zero'
703
-
704
- const perm = serverWhere('project', () => true)
705
-
706
- export const mutate = mutations('project', perm, {
707
- insert: async ({ tx }, project) => {
708
- await tx.mutate.project.insert(project)
709
- },
710
- })
711
- `);
712
- await (0, import_generate.generate)({
713
- dir: testDir,
714
- silent: true
715
- });
716
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
717
- (0, import_vitest.expect)(content).toContain("insert: v.unknown()");
718
- (0, import_vitest.expect)(content).not.toContain("insert: v.void_()");
719
- });
720
- (0, import_vitest.test)("handles mutations with only context param (void)", async function () {
721
- writeFileSync((0, import_path.join)(testDir, "user/mutations.ts"), `
722
- import { table, string } from 'on-zero'
723
- import { mutations, serverWhere } from 'on-zero'
724
-
725
- export const schema = table('user').columns({
726
- id: string(),
727
- name: string(),
728
- }).primaryKey('id')
729
-
730
- const perm = serverWhere('user', () => true)
731
-
732
- export const mutate = mutations(schema, perm, {
733
- finishOnboarding: async ({ tx }) => {
734
- // no second param
735
- },
736
- })
737
- `);
738
- var result = await (0, import_generate.generate)({
739
- dir: testDir,
740
- silent: true
741
- });
742
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
743
- (0, import_vitest.expect)(content).toContain("finishOnboarding");
744
- });
745
- (0, import_vitest.test)("handles primitive param type", async function () {
746
- writeFileSync((0, import_path.join)(testDir, "user/mutations.ts"), `
747
- import { table, string } from 'on-zero'
748
- import { mutations, serverWhere } from 'on-zero'
749
-
750
- export const schema = table('user').columns({
751
- id: string(),
752
- name: string(),
753
- }).primaryKey('id')
754
-
755
- const perm = serverWhere('user', () => true)
756
-
757
- export const mutate = mutations(schema, perm, {
758
- completeSignup: async ({ tx }, userId: string) => {
759
- await tx.mutate.user.update({ id: userId })
760
- },
761
- })
762
- `);
763
- var result = await (0, import_generate.generate)({
764
- dir: testDir,
765
- silent: true
766
- });
767
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
768
- (0, import_vitest.expect)(content).toContain("completeSignup");
769
- (0, import_vitest.expect)(content).toContain("v.string()");
770
- });
771
- (0, import_vitest.test)("handles array param type", async function () {
772
- writeFileSync((0, import_path.join)(testDir, "batch/mutations.ts"), `
773
- import { mutations } from 'on-zero'
774
-
775
- export const mutate = mutations({
776
- bulkDelete: async ({ tx }, ids: Array<{ id: string }>) => {
777
- for (const { id } of ids) await tx.mutate.batch.delete({ id })
778
- },
779
- })
780
- `);
781
- var result = await (0, import_generate.generate)({
782
- dir: testDir,
783
- silent: true
784
- });
785
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
786
- (0, import_vitest.expect)(content).toContain("bulkDelete");
787
- (0, import_vitest.expect)(content).toContain("v.array");
788
- });
789
336
  (0, import_vitest.test)("populates mutationCount and caching works", async function () {
790
337
  writeFileSync((0, import_path.join)(testDir, "item/mutations.ts"), `
791
338
  import { table, string } from 'on-zero'
@@ -817,242 +364,4 @@ export const mutate = mutations(schema, perm, {
817
364
  (0, import_vitest.expect)(second.mutationCount).toBe(first.mutationCount);
818
365
  });
819
366
  });
820
- (0, import_vitest.describe)("type resolution", function () {
821
- (0, import_vitest.test)("resolves imported type references for mutations", async function () {
822
- writeFileSync((0, import_path.join)(testDir, "post/types.ts"), `
823
- export type ArchiveParams = {
824
- id: string
825
- reason: string
826
- archived: boolean
827
- }
828
- `);
829
- writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `
830
- import { table, string, boolean } from 'on-zero'
831
- import { mutations } from 'on-zero'
832
- import type { ArchiveParams } from './types'
833
-
834
- export const mutate = mutations({
835
- archive: async ({ tx }, params: ArchiveParams) => {
836
- await tx.mutate.post.update(params)
837
- },
838
- })
839
- `);
840
- var result = await (0, import_generate.generate)({
841
- dir: testDir,
842
- silent: true
843
- });
844
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
845
- (0, import_vitest.expect)(content).toContain("v.string()");
846
- (0, import_vitest.expect)(content).toContain("v.boolean()");
847
- (0, import_vitest.expect)(content).toContain("reason");
848
- (0, import_vitest.expect)(content).toContain("archived");
849
- });
850
- (0, import_vitest.test)("resolves utility types like Pick", async function () {
851
- writeFileSync((0, import_path.join)(testDir, "item/types.ts"), `
852
- export type Item = {
853
- id: string
854
- name: string
855
- description: string
856
- count: number
857
- }
858
- `);
859
- writeFileSync((0, import_path.join)(testDir, "item/mutations.ts"), `
860
- import { table, string, number } from 'on-zero'
861
- import { mutations, serverWhere } from 'on-zero'
862
- import type { Item } from './types'
863
-
864
- export const schema = table('item').columns({
865
- id: string(),
866
- name: string(),
867
- description: string(),
868
- count: number(),
869
- }).primaryKey('id')
870
-
871
- const perm = serverWhere('item', () => true)
872
-
873
- export const mutate = mutations(schema, perm, {
874
- rename: async ({ tx }, updates: Pick<Item, 'id' | 'name'>) => {
875
- await tx.mutate.item.update(updates)
876
- },
877
- })
878
- `);
879
- var result = await (0, import_generate.generate)({
880
- dir: testDir,
881
- silent: true
882
- });
883
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
884
- (0, import_vitest.expect)(content).toContain("id");
885
- (0, import_vitest.expect)(content).toContain("name");
886
- (0, import_vitest.expect)(content).not.toMatch(/rename:[\s\S]*description/);
887
- (0, import_vitest.expect)(content).not.toMatch(/rename:[\s\S]*count/);
888
- });
889
- (0, import_vitest.test)("resolves instantiated Zero row fields through Partial and Omit", async function () {
890
- (0, import_fs.symlinkSync)((0, import_path.join)(import_meta.dirname, "../node_modules"), (0, import_path.join)(testDir, "node_modules"), "dir");
891
- writeFileSync((0, import_path.join)(testDir, "item/types.ts"), `
892
- import type { Row } from '@rocicorp/zero'
893
- import { drizzleZeroConfig } from 'drizzle-zero-sqlite'
894
- import { sqliteTable, text } from 'drizzle-orm/sqlite-core'
895
-
896
- const item = sqliteTable('item', {
897
- id: text().primaryKey(),
898
- name: text().notNull(),
899
- description: text().notNull(),
900
- })
901
- const schema = drizzleZeroConfig({ item })
902
-
903
- export type Item = Row<(typeof schema)['tables']['item']>
904
- `);
905
- writeFileSync((0, import_path.join)(testDir, "item/mutations.ts"), `
906
- import { mutations } from 'on-zero'
907
- import type { Item } from './types'
908
-
909
- type ItemUpdate = { id: string } & Partial<Omit<Item, 'id'>>
910
-
911
- export const mutate = mutations('item', {
912
- update: async ({ tx }, props: ItemUpdate) => {
913
- await tx.mutate.item.update(props)
914
- },
915
- })
916
- `);
917
- await (0, import_generate.generate)({
918
- dir: testDir,
919
- silent: true
920
- });
921
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
922
- (0, import_vitest.expect)(content).toContain("name: v.optional(v.string())");
923
- (0, import_vitest.expect)(content).toContain("description: v.optional(v.string())");
924
- (0, import_vitest.expect)(content).not.toContain("name: v.optional(v.unknown())");
925
- (0, import_vitest.expect)(content).not.toContain("description: v.optional(v.unknown())");
926
- });
927
- (0, import_vitest.test)("skips symbol-keyed properties when resolving imported mutation param types", async function () {
928
- writeFileSync((0, import_path.join)(testDir, "item/types.ts"), `
929
- export type WeirdParams = {
930
- id: string
931
- [Symbol.iterator]?: () => Iterator<string>
932
- }
933
- `);
934
- writeFileSync((0, import_path.join)(testDir, "item/mutations.ts"), `
935
- import { mutations } from 'on-zero'
936
- import type { WeirdParams } from './types'
937
-
938
- export const mutate = mutations({
939
- run: async ({ tx }, params: WeirdParams) => {
940
- await tx.mutate.item.delete({ id: params.id })
941
- },
942
- })
943
- `);
944
- await (0, import_generate.generate)({
945
- dir: testDir,
946
- silent: true
947
- });
948
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedMutations.ts"), "utf-8");
949
- (0, import_vitest.expect)(content).toContain("run");
950
- (0, import_vitest.expect)(content).toContain("id: v.string()");
951
- (0, import_vitest.expect)(content).not.toContain("__@iterator");
952
- });
953
- (0, import_vitest.test)("resolves imported types in query params", async function () {
954
- writeFileSync((0, import_path.join)(testDir, "post/mutations.ts"), `export const schema = table('post', { id: string() })`);
955
- writeFileSync((0, import_path.join)(testDir, "post/types.ts"), `
956
- export type PostFilter = {
957
- authorId: string
958
- published: boolean
959
- }
960
- `);
961
- writeFileSync((0, import_path.join)(testDir, "post/queries.ts"), `
962
- import type { PostFilter } from './types'
963
-
964
- export const filteredPosts = (filter: PostFilter) => zero.query.post
965
- `);
966
- var result = await (0, import_generate.generate)({
967
- dir: testDir,
968
- silent: true
969
- });
970
- var content = (0, import_fs.readFileSync)((0, import_path.join)(testDir, "generated/syncedQueries.ts"), "utf-8");
971
- (0, import_vitest.expect)(content).toContain("filteredPosts");
972
- (0, import_vitest.expect)(content).toContain("v.object");
973
- (0, import_vitest.expect)(content).toContain("authorId");
974
- (0, import_vitest.expect)(content).toContain("v.boolean()");
975
- });
976
- });
977
- (0, import_vitest.describe)("generateDrizzleSchemaFile", function () {
978
- (0, import_vitest.test)("preserves table server names and executable two-hop relationships", function () {
979
- var schema = {
980
- tables: {
981
- users: {
982
- name: "users",
983
- serverName: "user_records",
984
- primaryKey: ["id"],
985
- columns: {
986
- id: {
987
- type: "string",
988
- optional: false,
989
- customType: null
990
- }
991
- }
992
- },
993
- groups: {
994
- name: "groups",
995
- primaryKey: ["id"],
996
- columns: {
997
- id: {
998
- type: "string",
999
- optional: false,
1000
- customType: null
1001
- }
1002
- }
1003
- },
1004
- memberships: {
1005
- name: "memberships",
1006
- primaryKey: ["userId", "groupId"],
1007
- columns: {
1008
- userId: {
1009
- type: "string",
1010
- optional: false,
1011
- customType: null
1012
- },
1013
- groupId: {
1014
- type: "string",
1015
- optional: false,
1016
- customType: null
1017
- }
1018
- }
1019
- }
1020
- },
1021
- relationships: {
1022
- users: {
1023
- groups: [{
1024
- sourceField: ["id"],
1025
- destField: ["userId"],
1026
- destSchema: "memberships",
1027
- cardinality: "many"
1028
- }, {
1029
- sourceField: ["groupId"],
1030
- destField: ["id"],
1031
- destSchema: "groups",
1032
- cardinality: "many"
1033
- }]
1034
- }
1035
- }
1036
- };
1037
- var source = (0, import_generate.generateDrizzleSchemaFile)(schema);
1038
- var executableSource = source.replace("import { boolean, createSchema, json, number, relationships, string, table } from '@rocicorp/zero'", "").replace("export const schema =", "globalThis.generatedSchema =");
1039
- var context = {
1040
- ...zero
1041
- };
1042
- (0, import_vm.runInNewContext)(executableSource, context);
1043
- (0, import_vitest.expect)(source).toContain('const usersTable = table("users").from("user_records")');
1044
- (0, import_vitest.expect)(context.generatedSchema).toMatchObject({
1045
- tables: {
1046
- users: {
1047
- serverName: "user_records"
1048
- }
1049
- },
1050
- relationships: {
1051
- users: {
1052
- groups: schema.relationships.users.groups
1053
- }
1054
- }
1055
- });
1056
- });
1057
- });
1058
367
  //# sourceMappingURL=generate.test.native.js.map