on-zero 0.7.2-1784530701923 → 0.7.2
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/cjs/asyncTasks.cjs +51 -0
- package/dist/cjs/asyncTasks.native.js +84 -0
- package/dist/cjs/asyncTasks.native.js.map +1 -0
- package/dist/cjs/createZeroDirectServer.cjs +269 -0
- package/dist/cjs/createZeroDirectServer.native.js +287 -0
- package/dist/cjs/createZeroDirectServer.native.js.map +1 -0
- package/dist/cjs/createZeroSQLiteServer.cjs +50 -0
- package/dist/cjs/createZeroSQLiteServer.native.js +58 -0
- package/dist/cjs/createZeroSQLiteServer.native.js.map +1 -0
- package/dist/cjs/createZeroSQLiteServer.test.cjs +595 -0
- package/dist/cjs/createZeroSQLiteServer.test.native.js +647 -0
- package/dist/cjs/createZeroSQLiteServer.test.native.js.map +1 -0
- package/dist/cjs/createZeroServer.cjs +82 -0
- package/dist/cjs/createZeroServer.native.js +93 -0
- package/dist/cjs/createZeroServer.native.js.map +1 -0
- package/dist/cjs/createZeroServer.test.cjs +378 -0
- package/dist/cjs/createZeroServer.test.native.js +448 -0
- package/dist/cjs/createZeroServer.test.native.js.map +1 -0
- package/dist/cjs/createZeroServerCore.cjs +211 -0
- package/dist/cjs/createZeroServerCore.native.js +355 -0
- package/dist/cjs/createZeroServerCore.native.js.map +1 -0
- package/dist/cjs/helpers/createMutators.cjs +3 -4
- package/dist/cjs/helpers/createMutators.native.js +3 -6
- package/dist/cjs/helpers/createMutators.native.js.map +1 -1
- package/dist/cjs/httpPull/auth.test.cjs +197 -0
- package/dist/cjs/httpPull/auth.test.native.js +279 -0
- package/dist/cjs/httpPull/auth.test.native.js.map +1 -0
- package/dist/cjs/httpPull/churn.test.cjs +132 -0
- package/dist/cjs/httpPull/churn.test.native.js +155 -0
- package/dist/cjs/httpPull/churn.test.native.js.map +1 -0
- package/dist/cjs/httpPull/completeAck.test.cjs +69 -0
- package/dist/cjs/httpPull/completeAck.test.native.js +76 -0
- package/dist/cjs/httpPull/completeAck.test.native.js.map +1 -0
- package/dist/cjs/httpPull/fixtureServer.cjs +340 -0
- package/dist/cjs/httpPull/fixtureServer.native.js +534 -0
- package/dist/cjs/httpPull/fixtureServer.native.js.map +1 -0
- package/dist/cjs/httpPull/integration.test.cjs +53 -0
- package/dist/cjs/httpPull/integration.test.native.js +60 -0
- package/dist/cjs/httpPull/integration.test.native.js.map +1 -0
- package/dist/cjs/httpPull/rebase.test.cjs +360 -0
- package/dist/cjs/httpPull/rebase.test.native.js +420 -0
- package/dist/cjs/httpPull/rebase.test.native.js.map +1 -0
- package/dist/cjs/httpPull/relations.test.cjs +107 -0
- package/dist/cjs/httpPull/relations.test.native.js +119 -0
- package/dist/cjs/httpPull/relations.test.native.js.map +1 -0
- package/dist/cjs/httpPull/testHarness.cjs +100 -0
- package/dist/cjs/httpPull/testHarness.native.js +112 -0
- package/dist/cjs/httpPull/testHarness.native.js.map +1 -0
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/index.native.js +1 -0
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/run.cjs +1 -1
- package/dist/cjs/run.native.js +1 -1
- package/dist/cjs/server.cjs +22 -0
- package/dist/cjs/server.native.js +4 -0
- package/dist/cjs/server.native.js.map +1 -0
- package/dist/cjs/sqliteRuntime.cjs +318 -0
- package/dist/cjs/sqliteRuntime.native.js +438 -0
- package/dist/cjs/sqliteRuntime.native.js.map +1 -0
- package/dist/cjs/state.cjs +1 -1
- package/dist/cjs/state.native.js +1 -1
- package/dist/cjs/syncHost.cjs +179 -0
- package/dist/cjs/syncHost.native.js +388 -0
- package/dist/cjs/syncHost.native.js.map +1 -0
- package/dist/cjs/syncHost.test.cjs +240 -0
- package/dist/cjs/syncHost.test.native.js +290 -0
- package/dist/cjs/syncHost.test.native.js.map +1 -0
- package/dist/cjs/zeroRunner.cjs +1 -1
- package/dist/cjs/zeroRunner.native.js +1 -1
- package/dist/esm/asyncTasks.mjs +26 -0
- package/dist/esm/asyncTasks.mjs.map +1 -0
- package/dist/esm/asyncTasks.native.js +56 -0
- package/dist/esm/asyncTasks.native.js.map +1 -0
- package/dist/esm/createZeroDirectServer.mjs +241 -0
- package/dist/esm/createZeroDirectServer.mjs.map +1 -0
- package/dist/esm/createZeroDirectServer.native.js +256 -0
- package/dist/esm/createZeroDirectServer.native.js.map +1 -0
- package/dist/esm/createZeroSQLiteServer.mjs +24 -0
- package/dist/esm/createZeroSQLiteServer.mjs.map +1 -0
- package/dist/esm/createZeroSQLiteServer.native.js +29 -0
- package/dist/esm/createZeroSQLiteServer.native.js.map +1 -0
- package/dist/esm/createZeroSQLiteServer.test.mjs +596 -0
- package/dist/esm/createZeroSQLiteServer.test.mjs.map +1 -0
- package/dist/esm/createZeroSQLiteServer.test.native.js +645 -0
- package/dist/esm/createZeroSQLiteServer.test.native.js.map +1 -0
- package/dist/esm/createZeroServer.mjs +57 -0
- package/dist/esm/createZeroServer.mjs.map +1 -0
- package/dist/esm/createZeroServer.native.js +65 -0
- package/dist/esm/createZeroServer.native.js.map +1 -0
- package/dist/esm/createZeroServer.test.mjs +379 -0
- package/dist/esm/createZeroServer.test.mjs.map +1 -0
- package/dist/esm/createZeroServer.test.native.js +446 -0
- package/dist/esm/createZeroServer.test.native.js.map +1 -0
- package/dist/esm/createZeroServerCore.mjs +186 -0
- package/dist/esm/createZeroServerCore.mjs.map +1 -0
- package/dist/esm/createZeroServerCore.native.js +327 -0
- package/dist/esm/createZeroServerCore.native.js.map +1 -0
- package/dist/esm/helpers/createMutators.mjs +3 -4
- package/dist/esm/helpers/createMutators.mjs.map +1 -1
- package/dist/esm/helpers/createMutators.native.js +3 -6
- package/dist/esm/helpers/createMutators.native.js.map +1 -1
- package/dist/esm/httpPull/auth.test.mjs +198 -0
- package/dist/esm/httpPull/auth.test.mjs.map +1 -0
- package/dist/esm/httpPull/auth.test.native.js +277 -0
- package/dist/esm/httpPull/auth.test.native.js.map +1 -0
- package/dist/esm/httpPull/churn.test.mjs +133 -0
- package/dist/esm/httpPull/churn.test.mjs.map +1 -0
- package/dist/esm/httpPull/churn.test.native.js +153 -0
- package/dist/esm/httpPull/churn.test.native.js.map +1 -0
- package/dist/esm/httpPull/completeAck.test.mjs +70 -0
- package/dist/esm/httpPull/completeAck.test.mjs.map +1 -0
- package/dist/esm/httpPull/completeAck.test.native.js +74 -0
- package/dist/esm/httpPull/completeAck.test.native.js.map +1 -0
- package/dist/esm/httpPull/fixtureServer.mjs +315 -0
- package/dist/esm/httpPull/fixtureServer.mjs.map +1 -0
- package/dist/esm/httpPull/fixtureServer.native.js +506 -0
- package/dist/esm/httpPull/fixtureServer.native.js.map +1 -0
- package/dist/esm/httpPull/integration.test.mjs +54 -0
- package/dist/esm/httpPull/integration.test.mjs.map +1 -0
- package/dist/esm/httpPull/integration.test.native.js +58 -0
- package/dist/esm/httpPull/integration.test.native.js.map +1 -0
- package/dist/esm/httpPull/rebase.test.mjs +361 -0
- package/dist/esm/httpPull/rebase.test.mjs.map +1 -0
- package/dist/esm/httpPull/rebase.test.native.js +418 -0
- package/dist/esm/httpPull/rebase.test.native.js.map +1 -0
- package/dist/esm/httpPull/relations.test.mjs +108 -0
- package/dist/esm/httpPull/relations.test.mjs.map +1 -0
- package/dist/esm/httpPull/relations.test.native.js +117 -0
- package/dist/esm/httpPull/relations.test.native.js.map +1 -0
- package/dist/esm/httpPull/testHarness.mjs +72 -0
- package/dist/esm/httpPull/testHarness.mjs.map +1 -0
- package/dist/esm/httpPull/testHarness.native.js +81 -0
- package/dist/esm/httpPull/testHarness.native.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +1 -0
- package/dist/esm/index.native.js.map +1 -1
- package/dist/esm/run.mjs +1 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/run.native.js +1 -1
- package/dist/esm/server.mjs +4 -0
- package/dist/esm/server.mjs.map +1 -0
- package/dist/esm/server.native.js +2 -0
- package/dist/esm/server.native.js.map +1 -0
- package/dist/esm/sqliteRuntime.mjs +289 -0
- package/dist/esm/sqliteRuntime.mjs.map +1 -0
- package/dist/esm/sqliteRuntime.native.js +406 -0
- package/dist/esm/sqliteRuntime.native.js.map +1 -0
- package/dist/esm/state.mjs +1 -1
- package/dist/esm/state.native.js +1 -1
- package/dist/esm/syncHost.mjs +153 -0
- package/dist/esm/syncHost.mjs.map +1 -0
- package/dist/esm/syncHost.native.js +359 -0
- package/dist/esm/syncHost.native.js.map +1 -0
- package/dist/esm/syncHost.test.mjs +217 -0
- package/dist/esm/syncHost.test.mjs.map +1 -0
- package/dist/esm/syncHost.test.native.js +264 -0
- package/dist/esm/syncHost.test.native.js.map +1 -0
- package/dist/esm/zeroRunner.mjs +1 -1
- package/dist/esm/zeroRunner.mjs.map +1 -1
- package/dist/esm/zeroRunner.native.js +1 -1
- package/package.json +23 -8
- package/readme.md +65 -40
- package/src/asyncTasks.ts +36 -0
- package/src/createZeroClient.tsx +1 -1
- package/src/createZeroDirectServer.ts +411 -0
- package/src/createZeroSQLiteServer.test.ts +614 -0
- package/src/createZeroSQLiteServer.ts +56 -0
- package/src/createZeroServer.test.ts +421 -0
- package/src/createZeroServer.ts +105 -0
- package/src/createZeroServerCore.ts +296 -0
- package/src/helpers/createMutators.ts +2 -6
- package/src/httpPull/auth.test.ts +208 -0
- package/src/httpPull/churn.test.ts +147 -0
- package/src/httpPull/completeAck.test.ts +80 -0
- package/src/httpPull/fixtureServer.ts +391 -0
- package/src/httpPull/integration.test.ts +57 -0
- package/src/httpPull/rebase.test.ts +368 -0
- package/src/httpPull/relations.test.ts +135 -0
- package/src/httpPull/testHarness.ts +95 -0
- package/src/index.ts +1 -0
- package/src/run.ts +1 -1
- package/src/server.native.ts +3 -0
- package/src/server.ts +4 -0
- package/src/sqliteRuntime.ts +512 -0
- package/src/state.ts +1 -1
- package/src/syncHost.test.ts +223 -0
- package/src/syncHost.ts +294 -0
- package/src/types.ts +5 -0
- package/src/zeroRunner.ts +1 -1
- package/test-fixtures/server-return-types.ts +58 -21
- package/types/asyncTasks.d.ts +11 -0
- package/types/asyncTasks.d.ts.map +1 -0
- package/types/createZeroDirectServer.d.ts +80 -0
- package/types/createZeroDirectServer.d.ts.map +1 -0
- package/types/createZeroSQLiteServer.d.ts +12 -0
- package/types/createZeroSQLiteServer.d.ts.map +1 -0
- package/types/createZeroSQLiteServer.test.d.ts +2 -0
- package/types/createZeroSQLiteServer.test.d.ts.map +1 -0
- package/types/createZeroServer.d.ts +18 -0
- package/types/createZeroServer.d.ts.map +1 -0
- package/types/createZeroServer.test.d.ts +2 -0
- package/types/createZeroServer.test.d.ts.map +1 -0
- package/types/createZeroServerCore.d.ts +256 -0
- package/types/createZeroServerCore.d.ts.map +1 -0
- package/types/helpers/createMutators.d.ts +1 -2
- package/types/helpers/createMutators.d.ts.map +1 -1
- package/types/httpPull/auth.test.d.ts +2 -0
- package/types/httpPull/auth.test.d.ts.map +1 -0
- package/types/httpPull/churn.test.d.ts +2 -0
- package/types/httpPull/churn.test.d.ts.map +1 -0
- package/types/httpPull/completeAck.test.d.ts +2 -0
- package/types/httpPull/completeAck.test.d.ts.map +1 -0
- package/types/httpPull/fixtureSchema.d.ts +1 -1
- package/types/httpPull/fixtureServer.d.ts +14 -0
- package/types/httpPull/fixtureServer.d.ts.map +1 -0
- package/types/httpPull/integration.test.d.ts +2 -0
- package/types/httpPull/integration.test.d.ts.map +1 -0
- package/types/httpPull/rebase.test.d.ts +2 -0
- package/types/httpPull/rebase.test.d.ts.map +1 -0
- package/types/httpPull/relations.test.d.ts +2 -0
- package/types/httpPull/relations.test.d.ts.map +1 -0
- package/types/httpPull/testHarness.d.ts +32 -0
- package/types/httpPull/testHarness.d.ts.map +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/server.d.ts +5 -0
- package/types/server.d.ts.map +1 -0
- package/types/server.native.d.ts +1 -0
- package/types/server.native.d.ts.map +1 -0
- package/types/sqliteRuntime.d.ts +71 -0
- package/types/sqliteRuntime.d.ts.map +1 -0
- package/types/syncHost.d.ts +42 -0
- package/types/syncHost.d.ts.map +1 -0
- package/types/syncHost.test.d.ts +2 -0
- package/types/syncHost.test.d.ts.map +1 -0
- package/types/types.d.ts +4 -0
- package/types/types.d.ts.map +1 -1
- package/dist/cjs/orez.cjs +0 -227
- package/dist/cjs/orez.native.js +0 -314
- package/dist/cjs/orez.native.js.map +0 -1
- package/dist/cjs/orez.test.cjs +0 -211
- package/dist/cjs/orez.test.native.js +0 -226
- package/dist/cjs/orez.test.native.js.map +0 -1
- package/dist/esm/orez.mjs +0 -202
- package/dist/esm/orez.mjs.map +0 -1
- package/dist/esm/orez.native.js +0 -286
- package/dist/esm/orez.native.js.map +0 -1
- package/dist/esm/orez.test.mjs +0 -188
- package/dist/esm/orez.test.mjs.map +0 -1
- package/dist/esm/orez.test.native.js +0 -200
- package/dist/esm/orez.test.native.js.map +0 -1
- package/src/orez.test.ts +0 -195
- package/src/orez.ts +0 -331
- package/types/orez.d.ts +0 -53
- package/types/orez.d.ts.map +0 -1
- package/types/orez.test.d.ts +0 -2
- package/types/orez.test.d.ts.map +0 -1
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
var import_node_sqlite = require("node:sqlite");
|
|
2
|
+
var import_zero = require("@rocicorp/zero");
|
|
3
|
+
var import_bindings = require("@rocicorp/zero/bindings");
|
|
4
|
+
var import_vitest = require("vitest");
|
|
5
|
+
var import_createZeroSQLiteServer = require("./createZeroSQLiteServer.cjs");
|
|
6
|
+
var import_fixtureSchema = require("./httpPull/fixtureSchema.cjs");
|
|
7
|
+
var import_mutations = require("./mutations.cjs");
|
|
8
|
+
var import_serverWhere = require("./serverWhere.cjs");
|
|
9
|
+
const note = (0, import_zero.table)("note").from("note_records").columns({
|
|
10
|
+
id: (0, import_zero.string)().from("note_id"),
|
|
11
|
+
done: (0, import_zero.boolean)(),
|
|
12
|
+
data: (0, import_zero.json)().optional()
|
|
13
|
+
}).primaryKey("id");
|
|
14
|
+
const sqliteTypesSchema = (0, import_zero.createSchema)({
|
|
15
|
+
tables: [note],
|
|
16
|
+
relationships: [],
|
|
17
|
+
enableLegacyQueries: true
|
|
18
|
+
});
|
|
19
|
+
const person = (0, import_zero.table)("person").columns({
|
|
20
|
+
id: (0, import_zero.string)()
|
|
21
|
+
}).primaryKey("id");
|
|
22
|
+
const membership = (0, import_zero.table)("membership").columns({
|
|
23
|
+
personId: (0, import_zero.string)(),
|
|
24
|
+
teamId: (0, import_zero.string)()
|
|
25
|
+
}).primaryKey("personId", "teamId");
|
|
26
|
+
const team = (0, import_zero.table)("team").from("team_records").columns({
|
|
27
|
+
id: (0, import_zero.string)().from("team_id"),
|
|
28
|
+
active: (0, import_zero.boolean)().from("is_active"),
|
|
29
|
+
metadata: (0, import_zero.json)().from("metadata_json")
|
|
30
|
+
}).primaryKey("id");
|
|
31
|
+
const twoHopSchema = (0, import_zero.createSchema)({
|
|
32
|
+
tables: [person, membership, team],
|
|
33
|
+
relationships: [(0, import_zero.relationships)(person, ({
|
|
34
|
+
many
|
|
35
|
+
}) => ({
|
|
36
|
+
teams: many({
|
|
37
|
+
sourceField: ["id"],
|
|
38
|
+
destSchema: membership,
|
|
39
|
+
destField: ["personId"]
|
|
40
|
+
}, {
|
|
41
|
+
sourceField: ["teamId"],
|
|
42
|
+
destSchema: team,
|
|
43
|
+
destField: ["id"]
|
|
44
|
+
})
|
|
45
|
+
}))]
|
|
46
|
+
});
|
|
47
|
+
function pushRequest(id, name, args) {
|
|
48
|
+
return new Request("http://localhost/push?schema=zero_0&appID=zero", {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"content-type": "application/json"
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
clientGroupID: "cg1",
|
|
55
|
+
mutations: [{
|
|
56
|
+
type: "custom",
|
|
57
|
+
id,
|
|
58
|
+
clientID: "client-a",
|
|
59
|
+
name,
|
|
60
|
+
args: [args],
|
|
61
|
+
timestamp: 1
|
|
62
|
+
}],
|
|
63
|
+
pushVersion: 1,
|
|
64
|
+
timestamp: 1,
|
|
65
|
+
requestID: "req-" + id
|
|
66
|
+
})
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function cleanupRequest() {
|
|
70
|
+
return new Request("http://localhost/push?schema=zero_0&appID=zero", {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: {
|
|
73
|
+
"content-type": "application/json"
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
clientGroupID: "cg1",
|
|
77
|
+
mutations: [{
|
|
78
|
+
type: "custom",
|
|
79
|
+
id: 2,
|
|
80
|
+
clientID: "client-a",
|
|
81
|
+
name: "_zero_cleanupResults",
|
|
82
|
+
args: [{
|
|
83
|
+
clientGroupID: "cg1",
|
|
84
|
+
clientID: "client-a",
|
|
85
|
+
upToMutationID: 1
|
|
86
|
+
}],
|
|
87
|
+
timestamp: 1
|
|
88
|
+
}],
|
|
89
|
+
pushVersion: 1,
|
|
90
|
+
timestamp: 1,
|
|
91
|
+
requestID: "cleanup"
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
(0, import_vitest.describe)("createZeroSQLiteServer", () => {
|
|
96
|
+
let db;
|
|
97
|
+
let retryNextTransaction;
|
|
98
|
+
let transactionProvider;
|
|
99
|
+
let statements;
|
|
100
|
+
(0, import_vitest.beforeEach)(() => {
|
|
101
|
+
db = new import_node_sqlite.DatabaseSync(":memory:");
|
|
102
|
+
db.exec(["CREATE TABLE project (id TEXT PRIMARY KEY, ownerId TEXT NOT NULL, name TEXT NOT NULL);", "CREATE TABLE note_records (note_id TEXT PRIMARY KEY, done INTEGER NOT NULL, data TEXT);"].join("\n"));
|
|
103
|
+
retryNextTransaction = false;
|
|
104
|
+
statements = [];
|
|
105
|
+
const executor = {
|
|
106
|
+
async exec(sql, params = [], metadata) {
|
|
107
|
+
if (metadata) statements.push(metadata);
|
|
108
|
+
const result = db.prepare(sql).run(...params);
|
|
109
|
+
return {
|
|
110
|
+
changes: Number(result.changes)
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
async query(sql, params = []) {
|
|
114
|
+
return db.prepare(sql).all(...params);
|
|
115
|
+
},
|
|
116
|
+
async queryAst(ast, format) {
|
|
117
|
+
let rows;
|
|
118
|
+
if (ast.table === "project") {
|
|
119
|
+
rows = db.prepare("SELECT * FROM project ORDER BY id").all();
|
|
120
|
+
} else if (ast.table === "note") {
|
|
121
|
+
rows = db.prepare("SELECT note_id, done, data FROM note_records ORDER BY note_id").all().map(row => ({
|
|
122
|
+
id: row.note_id,
|
|
123
|
+
done: row.done !== 0,
|
|
124
|
+
data: typeof row.data === "string" ? JSON.parse(row.data) : row.data
|
|
125
|
+
}));
|
|
126
|
+
} else if (ast.table === "person") {
|
|
127
|
+
rows = [{
|
|
128
|
+
id: "p1",
|
|
129
|
+
teams: [{
|
|
130
|
+
id: "t1",
|
|
131
|
+
active: true,
|
|
132
|
+
metadata: {
|
|
133
|
+
label: "core"
|
|
134
|
+
}
|
|
135
|
+
}]
|
|
136
|
+
}];
|
|
137
|
+
} else {
|
|
138
|
+
throw new Error("unexpected table " + ast.table);
|
|
139
|
+
}
|
|
140
|
+
return format.singular ? rows[0] : rows;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const run = async callback => {
|
|
144
|
+
db.exec("BEGIN");
|
|
145
|
+
try {
|
|
146
|
+
const value = await callback(executor);
|
|
147
|
+
db.exec("COMMIT");
|
|
148
|
+
return value;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
db.exec("ROLLBACK");
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
transactionProvider = async callback => {
|
|
155
|
+
if (retryNextTransaction) {
|
|
156
|
+
retryNextTransaction = false;
|
|
157
|
+
db.exec("BEGIN");
|
|
158
|
+
await callback(executor);
|
|
159
|
+
db.exec("ROLLBACK");
|
|
160
|
+
}
|
|
161
|
+
return run(callback);
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
(0, import_vitest.afterEach)(() => {
|
|
165
|
+
db.close();
|
|
166
|
+
});
|
|
167
|
+
(0, import_vitest.test)("commits CRUD, the mutation ledger, and one retry-safe async task", async () => {
|
|
168
|
+
let effectRuns = 0;
|
|
169
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
170
|
+
schema: import_fixtureSchema.zeroHttpFixtureSchema,
|
|
171
|
+
models: {
|
|
172
|
+
project: {
|
|
173
|
+
mutate: {
|
|
174
|
+
create: async ({
|
|
175
|
+
tx,
|
|
176
|
+
server: server2
|
|
177
|
+
}, value) => {
|
|
178
|
+
await tx.mutate.project.insert(value);
|
|
179
|
+
server2?.enqueueTask(async () => {
|
|
180
|
+
effectRuns++;
|
|
181
|
+
}, {
|
|
182
|
+
barrier: true
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
createServerActions: () => ({}),
|
|
189
|
+
transactionProvider
|
|
190
|
+
});
|
|
191
|
+
retryNextTransaction = true;
|
|
192
|
+
const result = await server.handleMutationRequest({
|
|
193
|
+
authData: null,
|
|
194
|
+
request: pushRequest(1, "project.create", {
|
|
195
|
+
id: "p1",
|
|
196
|
+
ownerId: "u1",
|
|
197
|
+
name: "first"
|
|
198
|
+
}),
|
|
199
|
+
skipAsyncTasks: true
|
|
200
|
+
});
|
|
201
|
+
(0, import_vitest.expect)(result.response.mutations[0].result).toEqual({});
|
|
202
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM project").all()).toEqual([{
|
|
203
|
+
id: "p1",
|
|
204
|
+
ownerId: "u1",
|
|
205
|
+
name: "first"
|
|
206
|
+
}]);
|
|
207
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM zero_0_clients").all()).toEqual([{
|
|
208
|
+
clientGroupID: "cg1",
|
|
209
|
+
clientID: "client-a",
|
|
210
|
+
lastMutationID: 1
|
|
211
|
+
}]);
|
|
212
|
+
(0, import_vitest.expect)(result.asyncTasks).toHaveLength(1);
|
|
213
|
+
(0, import_vitest.expect)(result.asyncTasks[0].barrier).toBe(true);
|
|
214
|
+
await result.asyncTasks[0].task();
|
|
215
|
+
(0, import_vitest.expect)(effectRuns).toBe(1);
|
|
216
|
+
});
|
|
217
|
+
(0, import_vitest.test)("keeps later writes after inserting an existing primary key", async () => {
|
|
218
|
+
db.prepare("INSERT INTO project (id, ownerId, name) VALUES (?, ?, ?)").run("p1", "u1", "original");
|
|
219
|
+
db.prepare("INSERT INTO project (id, ownerId, name) VALUES (?, ?, ?)").run("p2", "u1", "before");
|
|
220
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
221
|
+
schema: import_fixtureSchema.zeroHttpFixtureSchema,
|
|
222
|
+
models: {
|
|
223
|
+
project: {
|
|
224
|
+
mutate: {
|
|
225
|
+
converge: async ({
|
|
226
|
+
tx
|
|
227
|
+
}) => {
|
|
228
|
+
await tx.mutate.project.insert({
|
|
229
|
+
id: "p1",
|
|
230
|
+
ownerId: "u2",
|
|
231
|
+
name: "replacement"
|
|
232
|
+
});
|
|
233
|
+
await tx.mutate.project.update({
|
|
234
|
+
id: "p2",
|
|
235
|
+
name: "after"
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
createServerActions: () => ({}),
|
|
242
|
+
transactionProvider
|
|
243
|
+
});
|
|
244
|
+
const result = await server.handleMutationRequest({
|
|
245
|
+
authData: null,
|
|
246
|
+
request: pushRequest(1, "project.converge", {})
|
|
247
|
+
});
|
|
248
|
+
(0, import_vitest.expect)(result.response.mutations[0].result).toEqual({});
|
|
249
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM project ORDER BY id").all()).toEqual([{
|
|
250
|
+
id: "p1",
|
|
251
|
+
ownerId: "u1",
|
|
252
|
+
name: "original"
|
|
253
|
+
}, {
|
|
254
|
+
id: "p2",
|
|
255
|
+
ownerId: "u1",
|
|
256
|
+
name: "after"
|
|
257
|
+
}]);
|
|
258
|
+
});
|
|
259
|
+
(0, import_vitest.test)("rolls back row writes and persists an application error with its LMID", async () => {
|
|
260
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
261
|
+
schema: import_fixtureSchema.zeroHttpFixtureSchema,
|
|
262
|
+
models: {
|
|
263
|
+
project: {
|
|
264
|
+
mutate: {
|
|
265
|
+
fail: async ({
|
|
266
|
+
tx
|
|
267
|
+
}, value) => {
|
|
268
|
+
await tx.mutate.project.insert(value);
|
|
269
|
+
throw new Error("rejected");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
createServerActions: () => ({}),
|
|
275
|
+
transactionProvider
|
|
276
|
+
});
|
|
277
|
+
const result = await server.handleMutationRequest({
|
|
278
|
+
authData: null,
|
|
279
|
+
request: pushRequest(1, "project.fail", {
|
|
280
|
+
id: "p1",
|
|
281
|
+
ownerId: "u1",
|
|
282
|
+
name: "first"
|
|
283
|
+
})
|
|
284
|
+
});
|
|
285
|
+
(0, import_vitest.expect)(result.response.mutations[0].result).toMatchObject({
|
|
286
|
+
error: "app",
|
|
287
|
+
message: "rejected"
|
|
288
|
+
});
|
|
289
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM project").all()).toEqual([]);
|
|
290
|
+
(0, import_vitest.expect)(db.prepare("SELECT lastMutationID FROM zero_0_clients").get()).toEqual({
|
|
291
|
+
lastMutationID: 1
|
|
292
|
+
});
|
|
293
|
+
const stored = db.prepare("SELECT result FROM zero_0_mutations").get();
|
|
294
|
+
(0, import_vitest.expect)(JSON.parse(stored.result)).toMatchObject({
|
|
295
|
+
error: "app",
|
|
296
|
+
message: "rejected"
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
(0, import_vitest.test)("allows a custom CRUD mutation to no-op without an automatic permission check", async () => {
|
|
300
|
+
const projectPermission = (0, import_serverWhere.serverWhere)("project", q => q.cmp("ownerId", "u1"));
|
|
301
|
+
const projectMutations = (0, import_mutations.mutations)("project", projectPermission, {
|
|
302
|
+
insert: async () => {}
|
|
303
|
+
});
|
|
304
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
305
|
+
schema: import_fixtureSchema.zeroHttpFixtureSchema,
|
|
306
|
+
models: {
|
|
307
|
+
project: {
|
|
308
|
+
mutate: projectMutations
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
createServerActions: () => ({}),
|
|
312
|
+
transactionProvider
|
|
313
|
+
});
|
|
314
|
+
const result = await server.handleMutationRequest({
|
|
315
|
+
authData: {
|
|
316
|
+
id: "u1",
|
|
317
|
+
role: "user"
|
|
318
|
+
},
|
|
319
|
+
request: pushRequest(1, "project.insert", {
|
|
320
|
+
id: "p1",
|
|
321
|
+
ownerId: "u1",
|
|
322
|
+
name: "skip"
|
|
323
|
+
})
|
|
324
|
+
});
|
|
325
|
+
(0, import_vitest.expect)(result.response.mutations[0].result).toEqual({});
|
|
326
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM project").all()).toEqual([]);
|
|
327
|
+
});
|
|
328
|
+
(0, import_vitest.test)("rolls back when a custom CRUD mutation throws", async () => {
|
|
329
|
+
const projectPermission = (0, import_serverWhere.serverWhere)("project", q => q.cmp("ownerId", "u1"));
|
|
330
|
+
const projectMutations = (0, import_mutations.mutations)("project", projectPermission, {
|
|
331
|
+
insert: async ({
|
|
332
|
+
tx
|
|
333
|
+
}, value) => {
|
|
334
|
+
await tx.mutate.project.insert(value);
|
|
335
|
+
throw new Error("denied by custom validation");
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
339
|
+
schema: import_fixtureSchema.zeroHttpFixtureSchema,
|
|
340
|
+
models: {
|
|
341
|
+
project: {
|
|
342
|
+
mutate: projectMutations
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
createServerActions: () => ({}),
|
|
346
|
+
transactionProvider
|
|
347
|
+
});
|
|
348
|
+
const result = await server.handleMutationRequest({
|
|
349
|
+
authData: {
|
|
350
|
+
id: "u2",
|
|
351
|
+
role: "user"
|
|
352
|
+
},
|
|
353
|
+
request: pushRequest(1, "project.insert", {
|
|
354
|
+
id: "p2",
|
|
355
|
+
ownerId: "u2",
|
|
356
|
+
name: "unauthorized"
|
|
357
|
+
})
|
|
358
|
+
});
|
|
359
|
+
(0, import_vitest.expect)(result.response.mutations[0].result).toMatchObject({
|
|
360
|
+
error: "app",
|
|
361
|
+
message: "denied by custom validation"
|
|
362
|
+
});
|
|
363
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM project").all()).toEqual([]);
|
|
364
|
+
});
|
|
365
|
+
(0, import_vitest.test)("permission-checks generated CRUD mutations", async () => {
|
|
366
|
+
let inserted;
|
|
367
|
+
let permissionChecks = 0;
|
|
368
|
+
const projectPermission = (0, import_serverWhere.serverWhere)("project", q => q.cmp("ownerId", "u1"));
|
|
369
|
+
const projectMutations = (0, import_mutations.mutations)("project", projectPermission);
|
|
370
|
+
const value = {
|
|
371
|
+
id: "p3",
|
|
372
|
+
ownerId: "u2",
|
|
373
|
+
name: "unauthorized"
|
|
374
|
+
};
|
|
375
|
+
await (0, import_vitest.expect)(projectMutations.insert({
|
|
376
|
+
tx: {
|
|
377
|
+
mutate: {
|
|
378
|
+
project: {
|
|
379
|
+
insert: async row => {
|
|
380
|
+
inserted = row;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
can: async (_where, row) => {
|
|
386
|
+
permissionChecks++;
|
|
387
|
+
(0, import_vitest.expect)(row).toEqual(value);
|
|
388
|
+
throw new Error("denied");
|
|
389
|
+
}
|
|
390
|
+
}, value)).rejects.toThrow("denied");
|
|
391
|
+
(0, import_vitest.expect)(inserted).toEqual(value);
|
|
392
|
+
(0, import_vitest.expect)(permissionChecks).toBe(1);
|
|
393
|
+
});
|
|
394
|
+
(0, import_vitest.test)("recovers duplicate results and deletes them through the cleanup mutation", async () => {
|
|
395
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
396
|
+
schema: import_fixtureSchema.zeroHttpFixtureSchema,
|
|
397
|
+
models: {
|
|
398
|
+
project: {
|
|
399
|
+
mutate: {
|
|
400
|
+
create: async ({
|
|
401
|
+
tx
|
|
402
|
+
}, value2) => {
|
|
403
|
+
await tx.mutate.project.insert(value2);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
createServerActions: () => ({}),
|
|
409
|
+
transactionProvider
|
|
410
|
+
});
|
|
411
|
+
const value = {
|
|
412
|
+
id: "p1",
|
|
413
|
+
ownerId: "u1",
|
|
414
|
+
name: "first"
|
|
415
|
+
};
|
|
416
|
+
await server.handleMutationRequest({
|
|
417
|
+
authData: null,
|
|
418
|
+
request: pushRequest(1, "project.create", value)
|
|
419
|
+
});
|
|
420
|
+
const duplicate = await server.handleMutationRequest({
|
|
421
|
+
authData: null,
|
|
422
|
+
request: pushRequest(1, "project.create", value)
|
|
423
|
+
});
|
|
424
|
+
(0, import_vitest.expect)(duplicate.response.mutations[0].result).toMatchObject({
|
|
425
|
+
error: "alreadyProcessed"
|
|
426
|
+
});
|
|
427
|
+
(0, import_vitest.expect)(db.prepare("SELECT lastMutationID FROM zero_0_clients").get()).toEqual({
|
|
428
|
+
lastMutationID: 1
|
|
429
|
+
});
|
|
430
|
+
(0, import_vitest.expect)(db.prepare("SELECT COUNT(*) AS count FROM zero_0_mutations").get()).toEqual({
|
|
431
|
+
count: 1
|
|
432
|
+
});
|
|
433
|
+
await server.handleMutationRequest({
|
|
434
|
+
authData: null,
|
|
435
|
+
request: cleanupRequest()
|
|
436
|
+
});
|
|
437
|
+
(0, import_vitest.expect)(db.prepare("SELECT COUNT(*) AS count FROM zero_0_mutations").get()).toEqual({
|
|
438
|
+
count: 0
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
(0, import_vitest.test)("supports direct CRUD, server names, value codecs, and query AST execution", async () => {
|
|
442
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
443
|
+
schema: sqliteTypesSchema,
|
|
444
|
+
models: {
|
|
445
|
+
note: {
|
|
446
|
+
mutate: {
|
|
447
|
+
insert: async ({
|
|
448
|
+
tx
|
|
449
|
+
}, value) => {
|
|
450
|
+
await tx.mutate.note.insert(value);
|
|
451
|
+
},
|
|
452
|
+
upsert: async ({
|
|
453
|
+
tx
|
|
454
|
+
}, value) => {
|
|
455
|
+
await tx.mutate.note.upsert(value);
|
|
456
|
+
},
|
|
457
|
+
update: async ({
|
|
458
|
+
tx
|
|
459
|
+
}, value) => {
|
|
460
|
+
await tx.mutate.note.update(value);
|
|
461
|
+
},
|
|
462
|
+
delete: async ({
|
|
463
|
+
tx
|
|
464
|
+
}, value) => {
|
|
465
|
+
await tx.mutate.note.delete(value);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
createServerActions: () => ({}),
|
|
471
|
+
transactionProvider
|
|
472
|
+
});
|
|
473
|
+
await server.mutate.note.insert({
|
|
474
|
+
id: "n1",
|
|
475
|
+
done: false,
|
|
476
|
+
data: {
|
|
477
|
+
label: "first"
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
await server.mutate.note.upsert({
|
|
481
|
+
id: "n1",
|
|
482
|
+
done: true,
|
|
483
|
+
data: {
|
|
484
|
+
label: "second"
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
await server.mutate.note.update({
|
|
488
|
+
id: "n1",
|
|
489
|
+
done: false
|
|
490
|
+
});
|
|
491
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM note_records").get()).toEqual({
|
|
492
|
+
note_id: "n1",
|
|
493
|
+
done: 0,
|
|
494
|
+
data: JSON.stringify({
|
|
495
|
+
label: "second"
|
|
496
|
+
})
|
|
497
|
+
});
|
|
498
|
+
await (0, import_vitest.expect)(server.query(q => q.note)).resolves.toEqual([{
|
|
499
|
+
id: "n1",
|
|
500
|
+
done: false,
|
|
501
|
+
data: {
|
|
502
|
+
label: "second"
|
|
503
|
+
}
|
|
504
|
+
}]);
|
|
505
|
+
await server.transaction(async tx => {
|
|
506
|
+
if (tx.location !== "server") throw new Error("expected server transaction");
|
|
507
|
+
await (0, import_vitest.expect)(tx.query.note.one().run()).resolves.toEqual({
|
|
508
|
+
id: "n1",
|
|
509
|
+
done: false,
|
|
510
|
+
data: {
|
|
511
|
+
label: "second"
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
await (0, import_vitest.expect)(tx.dbTransaction.query("SELECT * FROM note_records", [])).resolves.toEqual([{
|
|
515
|
+
note_id: "n1",
|
|
516
|
+
done: 0,
|
|
517
|
+
data: JSON.stringify({
|
|
518
|
+
label: "second"
|
|
519
|
+
})
|
|
520
|
+
}]);
|
|
521
|
+
});
|
|
522
|
+
await server.mutate.note.delete({
|
|
523
|
+
id: "n1"
|
|
524
|
+
});
|
|
525
|
+
(0, import_vitest.expect)(db.prepare("SELECT * FROM note_records").all()).toEqual([]);
|
|
526
|
+
(0, import_vitest.expect)(statements).toEqual([{
|
|
527
|
+
table: "note_records",
|
|
528
|
+
publicTable: "note",
|
|
529
|
+
kind: "insert"
|
|
530
|
+
}, {
|
|
531
|
+
table: "note_records",
|
|
532
|
+
publicTable: "note",
|
|
533
|
+
kind: "upsert"
|
|
534
|
+
}, {
|
|
535
|
+
table: "note_records",
|
|
536
|
+
publicTable: "note",
|
|
537
|
+
kind: "update"
|
|
538
|
+
}, {
|
|
539
|
+
table: "note_records",
|
|
540
|
+
publicTable: "note",
|
|
541
|
+
kind: "delete"
|
|
542
|
+
}]);
|
|
543
|
+
});
|
|
544
|
+
(0, import_vitest.test)("decodes destination rows through a hidden two-hop junction", async () => {
|
|
545
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
546
|
+
schema: twoHopSchema,
|
|
547
|
+
models: {},
|
|
548
|
+
createServerActions: () => ({}),
|
|
549
|
+
transactionProvider
|
|
550
|
+
});
|
|
551
|
+
await (0, import_vitest.expect)(server.query(q => q.person.related("teams"))).resolves.toEqual([{
|
|
552
|
+
id: "p1",
|
|
553
|
+
teams: [{
|
|
554
|
+
id: "t1",
|
|
555
|
+
active: true,
|
|
556
|
+
metadata: {
|
|
557
|
+
label: "core"
|
|
558
|
+
}
|
|
559
|
+
}]
|
|
560
|
+
}]);
|
|
561
|
+
});
|
|
562
|
+
(0, import_vitest.test)("accepts a hydrated singular query result from the transaction executor", async () => {
|
|
563
|
+
const hydrated = {
|
|
564
|
+
id: "n1",
|
|
565
|
+
done: false,
|
|
566
|
+
data: {
|
|
567
|
+
label: "already decoded"
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
let receivedQueryName;
|
|
571
|
+
const hydratedExecutor = {
|
|
572
|
+
async exec() {
|
|
573
|
+
return {
|
|
574
|
+
changes: 0
|
|
575
|
+
};
|
|
576
|
+
},
|
|
577
|
+
async query() {
|
|
578
|
+
return [];
|
|
579
|
+
},
|
|
580
|
+
async queryAst(_ast, _format, queryName) {
|
|
581
|
+
receivedQueryName = queryName;
|
|
582
|
+
return hydrated;
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
const hydratedProvider = async callback => callback(hydratedExecutor);
|
|
586
|
+
const server = (0, import_createZeroSQLiteServer.createZeroSQLiteServer)({
|
|
587
|
+
schema: sqliteTypesSchema,
|
|
588
|
+
models: {},
|
|
589
|
+
createServerActions: () => ({}),
|
|
590
|
+
transactionProvider: hydratedProvider
|
|
591
|
+
});
|
|
592
|
+
await (0, import_vitest.expect)(server.query(q => (0, import_bindings.asQueryInternals)(q.note.one()).nameAndArgs("noteByID", ["n1"]))).resolves.toEqual(hydrated);
|
|
593
|
+
(0, import_vitest.expect)(receivedQueryName).toBe("noteByID");
|
|
594
|
+
});
|
|
595
|
+
});
|