@rocicorp/zero 0.25.0-canary.14 → 0.25.0-canary.17
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/out/shared/src/deep-merge.d.ts +6 -4
- package/out/shared/src/deep-merge.d.ts.map +1 -1
- package/out/shared/src/deep-merge.js +2 -1
- package/out/shared/src/deep-merge.js.map +1 -1
- package/out/shared/src/record-proxy.d.ts +13 -0
- package/out/shared/src/record-proxy.d.ts.map +1 -0
- package/out/shared/src/record-proxy.js +59 -0
- package/out/shared/src/record-proxy.js.map +1 -0
- package/out/z2s/src/compiler.d.ts.map +1 -1
- package/out/z2s/src/compiler.js +4 -2
- package/out/z2s/src/compiler.js.map +1 -1
- package/out/zero/package.json.js +1 -1
- package/out/zero/src/pg.js +4 -3
- package/out/zero/src/server.js +4 -3
- package/out/zero/src/zero.js +11 -3
- package/out/zero/src/zero.js.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.d.ts.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.js +20 -13
- package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts +16 -0
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +28 -0
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/server/otel-diag-logger.d.ts.map +1 -1
- package/out/zero-cache/src/server/otel-diag-logger.js +1 -22
- package/out/zero-cache/src/server/otel-diag-logger.js.map +1 -1
- package/out/zero-cache/src/server/otel-start.d.ts.map +1 -1
- package/out/zero-cache/src/server/otel-start.js +1 -5
- package/out/zero-cache/src/server/otel-start.js.map +1 -1
- package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/server/syncer.js +6 -1
- package/out/zero-cache/src/server/syncer.js.map +1 -1
- package/out/zero-cache/src/services/litestream/commands.js +3 -2
- package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +8 -9
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +17 -11
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +2 -2
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.js +19 -4
- package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +1 -7
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-client/src/client/crud-impl.d.ts +11 -0
- package/out/zero-client/src/client/crud-impl.d.ts.map +1 -0
- package/out/zero-client/src/client/crud-impl.js +102 -0
- package/out/zero-client/src/client/crud-impl.js.map +1 -0
- package/out/zero-client/src/client/crud.d.ts +7 -40
- package/out/zero-client/src/client/crud.d.ts.map +1 -1
- package/out/zero-client/src/client/crud.js +21 -107
- package/out/zero-client/src/client/crud.js.map +1 -1
- package/out/zero-client/src/client/custom.d.ts +2 -2
- package/out/zero-client/src/client/custom.d.ts.map +1 -1
- package/out/zero-client/src/client/custom.js +5 -50
- package/out/zero-client/src/client/custom.js.map +1 -1
- package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
- package/out/zero-client/src/client/ivm-branch.js +20 -13
- package/out/zero-client/src/client/ivm-branch.js.map +1 -1
- package/out/zero-client/src/client/make-mutate-property.d.ts +1 -1
- package/out/zero-client/src/client/make-mutate-property.d.ts.map +1 -1
- package/out/zero-client/src/client/make-mutate-property.js.map +1 -1
- package/out/zero-client/src/client/make-replicache-mutators.d.ts.map +1 -1
- package/out/zero-client/src/client/make-replicache-mutators.js +14 -7
- package/out/zero-client/src/client/make-replicache-mutators.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero.d.ts +9 -7
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +11 -4
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-client/src/mod.d.ts +7 -6
- package/out/zero-client/src/mod.d.ts.map +1 -1
- package/out/zero-react/src/use-query.d.ts +6 -4
- package/out/zero-react/src/use-query.d.ts.map +1 -1
- package/out/zero-react/src/use-query.js +2 -1
- package/out/zero-react/src/use-query.js.map +1 -1
- package/out/zero-server/src/custom.d.ts +44 -5
- package/out/zero-server/src/custom.d.ts.map +1 -1
- package/out/zero-server/src/custom.js +98 -35
- package/out/zero-server/src/custom.js.map +1 -1
- package/out/zero-server/src/zql-database.d.ts +1 -1
- package/out/zero-server/src/zql-database.d.ts.map +1 -1
- package/out/zero-server/src/zql-database.js +5 -13
- package/out/zero-server/src/zql-database.js.map +1 -1
- package/out/zero-solid/src/solid-view.d.ts +7 -2
- package/out/zero-solid/src/solid-view.d.ts.map +1 -1
- package/out/zero-solid/src/solid-view.js +3 -32
- package/out/zero-solid/src/solid-view.js.map +1 -1
- package/out/zero-solid/src/use-query.d.ts +5 -3
- package/out/zero-solid/src/use-query.d.ts.map +1 -1
- package/out/zero-solid/src/use-query.js +5 -1
- package/out/zero-solid/src/use-query.js.map +1 -1
- package/out/zero-types/src/schema.d.ts +4 -4
- package/out/zql/src/builder/builder.d.ts.map +1 -1
- package/out/zql/src/builder/builder.js +1 -11
- package/out/zql/src/builder/builder.js.map +1 -1
- package/out/zql/src/error.js +1 -10
- package/out/zql/src/error.js.map +1 -1
- package/out/zql/src/ivm/array-view.d.ts +1 -1
- package/out/zql/src/ivm/array-view.d.ts.map +1 -1
- package/out/zql/src/ivm/array-view.js +2 -0
- package/out/zql/src/ivm/array-view.js.map +1 -1
- package/out/zql/src/ivm/exists.d.ts +3 -2
- package/out/zql/src/ivm/exists.d.ts.map +1 -1
- package/out/zql/src/ivm/exists.js +25 -23
- package/out/zql/src/ivm/exists.js.map +1 -1
- package/out/zql/src/ivm/fan-in.d.ts +3 -3
- package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
- package/out/zql/src/ivm/fan-in.js +6 -5
- package/out/zql/src/ivm/fan-in.js.map +1 -1
- package/out/zql/src/ivm/fan-out.d.ts +2 -2
- package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
- package/out/zql/src/ivm/fan-out.js +5 -5
- package/out/zql/src/ivm/fan-out.js.map +1 -1
- package/out/zql/src/ivm/filter-operators.d.ts +5 -5
- package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
- package/out/zql/src/ivm/filter-operators.js +8 -8
- package/out/zql/src/ivm/filter-operators.js.map +1 -1
- package/out/zql/src/ivm/filter-push.d.ts +2 -1
- package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
- package/out/zql/src/ivm/filter-push.js +5 -5
- package/out/zql/src/ivm/filter-push.js.map +1 -1
- package/out/zql/src/ivm/filter.d.ts +2 -2
- package/out/zql/src/ivm/filter.d.ts.map +1 -1
- package/out/zql/src/ivm/filter.js +4 -4
- package/out/zql/src/ivm/filter.js.map +1 -1
- package/out/zql/src/ivm/flipped-join.d.ts.map +1 -1
- package/out/zql/src/ivm/flipped-join.js +100 -83
- package/out/zql/src/ivm/flipped-join.js.map +1 -1
- package/out/zql/src/ivm/join.d.ts.map +1 -1
- package/out/zql/src/ivm/join.js +52 -50
- package/out/zql/src/ivm/join.js.map +1 -1
- package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts +1 -1
- package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts.map +1 -1
- package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +4 -4
- package/out/zql/src/ivm/maybe-split-and-push-edit-change.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts +3 -3
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +7 -4
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/operator.d.ts +10 -3
- package/out/zql/src/ivm/operator.d.ts.map +1 -1
- package/out/zql/src/ivm/operator.js.map +1 -1
- package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
- package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
- package/out/zql/src/ivm/push-accumulated.js +8 -8
- package/out/zql/src/ivm/push-accumulated.js.map +1 -1
- package/out/zql/src/ivm/skip.d.ts +1 -1
- package/out/zql/src/ivm/skip.d.ts.map +1 -1
- package/out/zql/src/ivm/skip.js +8 -3
- package/out/zql/src/ivm/skip.js.map +1 -1
- package/out/zql/src/ivm/source.d.ts +15 -7
- package/out/zql/src/ivm/source.d.ts.map +1 -1
- package/out/zql/src/ivm/stream.d.ts +2 -0
- package/out/zql/src/ivm/stream.d.ts.map +1 -1
- package/out/zql/src/ivm/stream.js +5 -14
- package/out/zql/src/ivm/stream.js.map +1 -1
- package/out/zql/src/ivm/take.d.ts +1 -1
- package/out/zql/src/ivm/take.d.ts.map +1 -1
- package/out/zql/src/ivm/take.js +164 -147
- package/out/zql/src/ivm/take.js.map +1 -1
- package/out/zql/src/ivm/union-fan-in.d.ts +2 -2
- package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
- package/out/zql/src/ivm/union-fan-in.js +7 -7
- package/out/zql/src/ivm/union-fan-in.js.map +1 -1
- package/out/zql/src/ivm/union-fan-out.d.ts +1 -1
- package/out/zql/src/ivm/union-fan-out.d.ts.map +1 -1
- package/out/zql/src/ivm/union-fan-out.js +3 -3
- package/out/zql/src/ivm/union-fan-out.js.map +1 -1
- package/out/zql/src/mutate/crud.d.ts +139 -0
- package/out/zql/src/mutate/crud.d.ts.map +1 -0
- package/out/zql/src/mutate/crud.js +53 -0
- package/out/zql/src/mutate/crud.js.map +1 -0
- package/out/zql/src/mutate/custom.d.ts +12 -53
- package/out/zql/src/mutate/custom.d.ts.map +1 -1
- package/out/zql/src/mutate/custom.js +1 -5
- package/out/zql/src/mutate/custom.js.map +1 -1
- package/out/zql/src/mutate/mutator-registry.d.ts +33 -32
- package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
- package/out/zql/src/mutate/mutator-registry.js +26 -25
- package/out/zql/src/mutate/mutator-registry.js.map +1 -1
- package/out/zql/src/mutate/mutator.d.ts +48 -58
- package/out/zql/src/mutate/mutator.d.ts.map +1 -1
- package/out/zql/src/mutate/mutator.js +12 -8
- package/out/zql/src/mutate/mutator.js.map +1 -1
- package/out/zql/src/planner/planner-builder.d.ts +2 -1
- package/out/zql/src/planner/planner-builder.d.ts.map +1 -1
- package/out/zql/src/planner/planner-builder.js +5 -5
- package/out/zql/src/planner/planner-builder.js.map +1 -1
- package/out/zql/src/planner/planner-graph.d.ts +3 -1
- package/out/zql/src/planner/planner-graph.d.ts.map +1 -1
- package/out/zql/src/planner/planner-graph.js +5 -5
- package/out/zql/src/planner/planner-graph.js.map +1 -1
- package/out/zql/src/query/abstract-query.d.ts +2 -3
- package/out/zql/src/query/abstract-query.d.ts.map +1 -1
- package/out/zql/src/query/abstract-query.js +0 -3
- package/out/zql/src/query/abstract-query.js.map +1 -1
- package/out/zql/src/query/create-builder.d.ts.map +1 -1
- package/out/zql/src/query/create-builder.js +7 -36
- package/out/zql/src/query/create-builder.js.map +1 -1
- package/out/zql/src/query/measure-push-operator.d.ts +1 -1
- package/out/zql/src/query/measure-push-operator.d.ts.map +1 -1
- package/out/zql/src/query/measure-push-operator.js +2 -2
- package/out/zql/src/query/measure-push-operator.js.map +1 -1
- package/out/zql/src/query/query-impl.d.ts +2 -2
- package/out/zql/src/query/query-impl.d.ts.map +1 -1
- package/out/zql/src/query/query-impl.js.map +1 -1
- package/out/zql/src/query/query-registry.d.ts +87 -79
- package/out/zql/src/query/query-registry.d.ts.map +1 -1
- package/out/zql/src/query/query-registry.js +44 -38
- package/out/zql/src/query/query-registry.js.map +1 -1
- package/out/zql/src/query/query.d.ts +3 -18
- package/out/zql/src/query/query.d.ts.map +1 -1
- package/out/zql/src/query/runnable-query-impl.d.ts +2 -2
- package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
- package/out/zql/src/query/runnable-query-impl.js.map +1 -1
- package/out/zql/src/query/static-query.d.ts +1 -0
- package/out/zql/src/query/static-query.d.ts.map +1 -1
- package/out/zql/src/query/static-query.js +2 -2
- package/out/zql/src/query/static-query.js.map +1 -1
- package/out/zqlite/src/internal/sql-inline.d.ts +13 -0
- package/out/zqlite/src/internal/sql-inline.d.ts.map +1 -0
- package/out/zqlite/src/internal/sql-inline.js +45 -0
- package/out/zqlite/src/internal/sql-inline.js.map +1 -0
- package/out/zqlite/src/sqlite-cost-model.d.ts.map +1 -1
- package/out/zqlite/src/sqlite-cost-model.js +2 -2
- package/out/zqlite/src/sqlite-cost-model.js.map +1 -1
- package/out/zqlite/src/table-source.d.ts +3 -2
- package/out/zqlite/src/table-source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.js +5 -2
- package/out/zqlite/src/table-source.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { must } from "../../../shared/src/must.js";
|
|
2
|
+
import "../../../zero-protocol/src/push.js";
|
|
3
|
+
import { consume } from "../../../zql/src/ivm/stream.js";
|
|
4
|
+
import { toPrimaryKeyString } from "./keys.js";
|
|
5
|
+
function defaultOptionalFieldsToNull(schema, value) {
|
|
6
|
+
let rv = value;
|
|
7
|
+
for (const name in schema.columns) {
|
|
8
|
+
if (rv[name] === void 0) {
|
|
9
|
+
rv = { ...rv, [name]: null };
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return rv;
|
|
13
|
+
}
|
|
14
|
+
async function insert(tx, arg, schema, ivmBranch) {
|
|
15
|
+
const key = toPrimaryKeyString(
|
|
16
|
+
arg.tableName,
|
|
17
|
+
schema.tables[arg.tableName].primaryKey,
|
|
18
|
+
arg.value
|
|
19
|
+
);
|
|
20
|
+
if (!await tx.has(key)) {
|
|
21
|
+
const val = defaultOptionalFieldsToNull(
|
|
22
|
+
schema.tables[arg.tableName],
|
|
23
|
+
arg.value
|
|
24
|
+
);
|
|
25
|
+
await tx.set(key, val);
|
|
26
|
+
if (ivmBranch) {
|
|
27
|
+
consume(
|
|
28
|
+
must(ivmBranch.getSource(arg.tableName)).push({
|
|
29
|
+
type: "add",
|
|
30
|
+
row: arg.value
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function upsert(tx, arg, schema, ivmBranch) {
|
|
37
|
+
const key = toPrimaryKeyString(
|
|
38
|
+
arg.tableName,
|
|
39
|
+
schema.tables[arg.tableName].primaryKey,
|
|
40
|
+
arg.value
|
|
41
|
+
);
|
|
42
|
+
if (await tx.has(key)) {
|
|
43
|
+
await update(tx, { ...arg }, schema, ivmBranch);
|
|
44
|
+
} else {
|
|
45
|
+
await insert(tx, { ...arg }, schema, ivmBranch);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function update(tx, arg, schema, ivmBranch) {
|
|
49
|
+
const key = toPrimaryKeyString(
|
|
50
|
+
arg.tableName,
|
|
51
|
+
schema.tables[arg.tableName].primaryKey,
|
|
52
|
+
arg.value
|
|
53
|
+
);
|
|
54
|
+
const prev = await tx.get(key);
|
|
55
|
+
if (prev === void 0) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const update2 = arg.value;
|
|
59
|
+
const next = { ...prev };
|
|
60
|
+
for (const k in update2) {
|
|
61
|
+
if (update2[k] !== void 0) {
|
|
62
|
+
next[k] = update2[k];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
await tx.set(key, next);
|
|
66
|
+
if (ivmBranch) {
|
|
67
|
+
consume(
|
|
68
|
+
must(ivmBranch.getSource(arg.tableName)).push({
|
|
69
|
+
type: "edit",
|
|
70
|
+
oldRow: prev,
|
|
71
|
+
row: next
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function deleteImpl(tx, arg, schema, ivmBranch) {
|
|
77
|
+
const key = toPrimaryKeyString(
|
|
78
|
+
arg.tableName,
|
|
79
|
+
schema.tables[arg.tableName].primaryKey,
|
|
80
|
+
arg.value
|
|
81
|
+
);
|
|
82
|
+
const prev = await tx.get(key);
|
|
83
|
+
if (prev === void 0) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
await tx.del(key);
|
|
87
|
+
if (ivmBranch) {
|
|
88
|
+
consume(
|
|
89
|
+
must(ivmBranch.getSource(arg.tableName)).push({
|
|
90
|
+
type: "remove",
|
|
91
|
+
row: prev
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
deleteImpl as delete,
|
|
98
|
+
insert,
|
|
99
|
+
update,
|
|
100
|
+
upsert
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=crud-impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crud-impl.js","sources":["../../../../../zero-client/src/client/crud-impl.ts"],"sourcesContent":["import type {ReadonlyJSONObject} from '../../../shared/src/json.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {\n type DeleteOp,\n type InsertOp,\n type UpdateOp,\n type UpsertOp,\n} from '../../../zero-protocol/src/push.ts';\nimport type {TableSchema} from '../../../zero-schema/src/table-schema.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport {consume} from '../../../zql/src/ivm/stream.ts';\nimport type {IVMSourceBranch} from './ivm-branch.ts';\nimport {toPrimaryKeyString} from './keys.ts';\nimport type {WriteTransaction} from './replicache-types.ts';\nexport type {TableMutator} from '../../../zql/src/mutate/crud.ts';\n\nfunction defaultOptionalFieldsToNull(\n schema: TableSchema,\n value: ReadonlyJSONObject,\n): ReadonlyJSONObject {\n let rv = value;\n for (const name in schema.columns) {\n if (rv[name] === undefined) {\n rv = {...rv, [name]: null};\n }\n }\n return rv;\n}\n\nexport async function insert(\n tx: WriteTransaction,\n arg: InsertOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n if (!(await tx.has(key))) {\n const val = defaultOptionalFieldsToNull(\n schema.tables[arg.tableName],\n arg.value,\n );\n await tx.set(key, val);\n if (ivmBranch) {\n consume(\n must(ivmBranch.getSource(arg.tableName)).push({\n type: 'add',\n row: arg.value,\n }),\n );\n }\n }\n}\n\nexport async function upsert(\n tx: WriteTransaction,\n arg: InsertOp | UpsertOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n if (await tx.has(key)) {\n await update(tx, {...arg, op: 'update'}, schema, ivmBranch);\n } else {\n await insert(tx, {...arg, op: 'insert'}, schema, ivmBranch);\n }\n}\n\nexport async function update(\n tx: WriteTransaction,\n arg: UpdateOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n const prev = await tx.get(key);\n if (prev === undefined) {\n return;\n }\n const update = arg.value;\n const next = {...(prev as ReadonlyJSONObject)};\n for (const k in update) {\n if (update[k] !== undefined) {\n next[k] = update[k];\n }\n }\n await tx.set(key, next);\n if (ivmBranch) {\n consume(\n must(ivmBranch.getSource(arg.tableName)).push({\n type: 'edit',\n oldRow: prev as Row,\n row: next,\n }),\n );\n }\n}\n\nasync function deleteImpl(\n tx: WriteTransaction,\n arg: DeleteOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n const prev = await tx.get(key);\n if (prev === undefined) {\n return;\n }\n await tx.del(key);\n if (ivmBranch) {\n consume(\n must(ivmBranch.getSource(arg.tableName)).push({\n type: 'remove',\n row: prev as Row,\n }),\n );\n }\n}\n\nexport {deleteImpl as delete};\n"],"names":["update"],"mappings":";;;;AAiBA,SAAS,4BACP,QACA,OACoB;AACpB,MAAI,KAAK;AACT,aAAW,QAAQ,OAAO,SAAS;AACjC,QAAI,GAAG,IAAI,MAAM,QAAW;AAC1B,WAAK,EAAC,GAAG,IAAI,CAAC,IAAI,GAAG,KAAA;AAAA,IACvB;AAAA,EACF;AACA,SAAO;AACT;AAEA,eAAsB,OACpB,IACA,KACA,QACA,WACe;AACf,QAAM,MAAM;AAAA,IACV,IAAI;AAAA,IACJ,OAAO,OAAO,IAAI,SAAS,EAAE;AAAA,IAC7B,IAAI;AAAA,EAAA;AAEN,MAAI,CAAE,MAAM,GAAG,IAAI,GAAG,GAAI;AACxB,UAAM,MAAM;AAAA,MACV,OAAO,OAAO,IAAI,SAAS;AAAA,MAC3B,IAAI;AAAA,IAAA;AAEN,UAAM,GAAG,IAAI,KAAK,GAAG;AACrB,QAAI,WAAW;AACb;AAAA,QACE,KAAK,UAAU,UAAU,IAAI,SAAS,CAAC,EAAE,KAAK;AAAA,UAC5C,MAAM;AAAA,UACN,KAAK,IAAI;AAAA,QAAA,CACV;AAAA,MAAA;AAAA,IAEL;AAAA,EACF;AACF;AAEA,eAAsB,OACpB,IACA,KACA,QACA,WACe;AACf,QAAM,MAAM;AAAA,IACV,IAAI;AAAA,IACJ,OAAO,OAAO,IAAI,SAAS,EAAE;AAAA,IAC7B,IAAI;AAAA,EAAA;AAEN,MAAI,MAAM,GAAG,IAAI,GAAG,GAAG;AACrB,UAAM,OAAO,IAAI,EAAC,GAAG,IAAiB,GAAG,QAAQ,SAAS;AAAA,EAC5D,OAAO;AACL,UAAM,OAAO,IAAI,EAAC,GAAG,IAAiB,GAAG,QAAQ,SAAS;AAAA,EAC5D;AACF;AAEA,eAAsB,OACpB,IACA,KACA,QACA,WACe;AACf,QAAM,MAAM;AAAA,IACV,IAAI;AAAA,IACJ,OAAO,OAAO,IAAI,SAAS,EAAE;AAAA,IAC7B,IAAI;AAAA,EAAA;AAEN,QAAM,OAAO,MAAM,GAAG,IAAI,GAAG;AAC7B,MAAI,SAAS,QAAW;AACtB;AAAA,EACF;AACA,QAAMA,UAAS,IAAI;AACnB,QAAM,OAAO,EAAC,GAAI,KAAA;AAClB,aAAW,KAAKA,SAAQ;AACtB,QAAIA,QAAO,CAAC,MAAM,QAAW;AAC3B,WAAK,CAAC,IAAIA,QAAO,CAAC;AAAA,IACpB;AAAA,EACF;AACA,QAAM,GAAG,IAAI,KAAK,IAAI;AACtB,MAAI,WAAW;AACb;AAAA,MACE,KAAK,UAAU,UAAU,IAAI,SAAS,CAAC,EAAE,KAAK;AAAA,QAC5C,MAAM;AAAA,QACN,QAAQ;AAAA,QACR,KAAK;AAAA,MAAA,CACN;AAAA,IAAA;AAAA,EAEL;AACF;AAEA,eAAe,WACb,IACA,KACA,QACA,WACe;AACf,QAAM,MAAM;AAAA,IACV,IAAI;AAAA,IACJ,OAAO,OAAO,IAAI,SAAS,EAAE;AAAA,IAC7B,IAAI;AAAA,EAAA;AAEN,QAAM,OAAO,MAAM,GAAG,IAAI,GAAG;AAC7B,MAAI,SAAS,QAAW;AACtB;AAAA,EACF;AACA,QAAM,GAAG,IAAI,GAAG;AAChB,MAAI,WAAW;AACb;AAAA,MACE,KAAK,UAAU,UAAU,IAAI,SAAS,CAAC,EAAE,KAAK;AAAA,QAC5C,MAAM;AAAA,QACN,KAAK;AAAA,MAAA,CACN;AAAA,IAAA;AAAA,EAEL;AACF;"}
|
|
@@ -1,44 +1,14 @@
|
|
|
1
1
|
import type { MaybePromise } from '../../../shared/src/types.ts';
|
|
2
|
-
import { CRUD_MUTATION_NAME, type CRUDMutationArg, type CRUDOp
|
|
2
|
+
import { CRUD_MUTATION_NAME, type CRUDMutationArg, type CRUDOp } from '../../../zero-protocol/src/push.ts';
|
|
3
3
|
import type { TableSchema } from '../../../zero-schema/src/table-schema.ts';
|
|
4
4
|
import type { Schema } from '../../../zero-types/src/schema.ts';
|
|
5
|
-
import type {
|
|
5
|
+
import type { CRUDExecutor, TableMutator } from '../../../zql/src/mutate/crud.ts';
|
|
6
6
|
import type { IVMSourceBranch } from './ivm-branch.ts';
|
|
7
7
|
import type { MutatorDefs, WriteTransaction } from './replicache-types.ts';
|
|
8
|
-
|
|
9
|
-
* This is the type of the generated mutate.<name>.<verb> function.
|
|
10
|
-
*/
|
|
11
|
-
export type TableMutator<S extends TableSchema> = {
|
|
12
|
-
/**
|
|
13
|
-
* Writes a row if a row with the same primary key doesn't already exists.
|
|
14
|
-
* Non-primary-key fields that are 'optional' can be omitted or set to
|
|
15
|
-
* `undefined`. Such fields will be assigned the value `null` optimistically
|
|
16
|
-
* and then the default value as defined by the server.
|
|
17
|
-
*/
|
|
18
|
-
insert: (value: InsertValue<S>) => Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* Writes a row unconditionally, overwriting any existing row with the same
|
|
21
|
-
* primary key. Non-primary-key fields that are 'optional' can be omitted or
|
|
22
|
-
* set to `undefined`. Such fields will be assigned the value `null`
|
|
23
|
-
* optimistically and then the default value as defined by the server.
|
|
24
|
-
*/
|
|
25
|
-
upsert: (value: UpsertValue<S>) => Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* Updates a row with the same primary key. If no such row exists, this
|
|
28
|
-
* function does nothing. All non-primary-key fields can be omitted or set to
|
|
29
|
-
* `undefined`. Such fields will be left unchanged from previous value.
|
|
30
|
-
*/
|
|
31
|
-
update: (value: UpdateValue<S>) => Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
* Deletes the row with the specified primary key. If no such row exists, this
|
|
34
|
-
* function does nothing.
|
|
35
|
-
*/
|
|
36
|
-
delete: (id: DeleteID<S>) => Promise<void>;
|
|
37
|
-
};
|
|
38
|
-
export type DBMutator<S extends Schema> = S['enableLegacyMutators'] extends false ? {} : {
|
|
8
|
+
export type DBMutator<S extends Schema> = S['enableLegacyMutators'] extends true ? {
|
|
39
9
|
[K in keyof S['tables']]: TableMutator<S['tables'][K]>;
|
|
40
|
-
};
|
|
41
|
-
export type BatchMutator<S extends Schema> = <R>(body: (m: DBMutator<S>) => MaybePromise<R>) => Promise<R
|
|
10
|
+
} : {};
|
|
11
|
+
export type BatchMutator<S extends Schema> = S['enableLegacyMutators'] extends true ? <R>(body: (m: DBMutator<S>) => MaybePromise<R>) => Promise<R> : undefined;
|
|
42
12
|
type ZeroCRUDMutate = {
|
|
43
13
|
[CRUD_MUTATION_NAME]: CRUDMutate;
|
|
44
14
|
};
|
|
@@ -54,7 +24,7 @@ type ZeroCRUDMutate = {
|
|
|
54
24
|
*/
|
|
55
25
|
export declare function makeCRUDMutate<const S extends Schema>(schema: S, repMutate: ZeroCRUDMutate, mutate: object): BatchMutator<S>;
|
|
56
26
|
/**
|
|
57
|
-
* Creates the `{
|
|
27
|
+
* Creates the `{insert, upsert, update, delete}` object for use inside a
|
|
58
28
|
* batch.
|
|
59
29
|
*/
|
|
60
30
|
export declare function makeBatchCRUDMutate<S extends TableSchema>(tableName: string, schema: Schema, ops: CRUDOp[]): TableMutator<S>;
|
|
@@ -63,10 +33,7 @@ export type WithCRUD<MD extends MutatorDefs> = MD & {
|
|
|
63
33
|
};
|
|
64
34
|
export type CRUDMutate = (crudArg: CRUDMutationArg) => Promise<void>;
|
|
65
35
|
export type CRUDMutator = (tx: WriteTransaction, crudArg: CRUDMutationArg) => Promise<void>;
|
|
36
|
+
export declare function makeCRUDExecutor(tx: WriteTransaction, schema: Schema, ivmBranch: IVMSourceBranch | undefined): CRUDExecutor;
|
|
66
37
|
export declare function makeCRUDMutator(schema: Schema): CRUDMutator;
|
|
67
|
-
export declare function insertImpl(tx: WriteTransaction, arg: InsertOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
68
|
-
export declare function upsertImpl(tx: WriteTransaction, arg: InsertOp | UpsertOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
69
|
-
export declare function updateImpl(tx: WriteTransaction, arg: UpdateOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
70
|
-
export declare function deleteImpl(tx: WriteTransaction, arg: DeleteOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
71
38
|
export {};
|
|
72
39
|
//# sourceMappingURL=crud.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/crud.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/crud.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EACL,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,MAAM,EAKZ,MAAM,oCAAoC,CAAC;AAC5C,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,0CAA0C,CAAC;AAC1E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,YAAY,EAAE,YAAY,EAAC,MAAM,iCAAiC,CAAC;AAQhF,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAC,WAAW,EAAE,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAEzE,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,sBAAsB,CAAC,SAAS,IAAI,GAC5E;KACG,CAAC,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;CACvD,GACD,EAAE,CAAC;AAEP,MAAM,MAAM,YAAY,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import { must } from "../../../shared/src/must.js";
|
|
2
1
|
import { promiseVoid } from "../../../shared/src/resolved-promises.js";
|
|
3
2
|
import { CRUD_MUTATION_NAME } from "../../../zero-protocol/src/push.js";
|
|
4
|
-
import
|
|
3
|
+
import * as crudImpl from "./crud-impl.js";
|
|
5
4
|
function makeCRUDMutate(schema, repMutate, mutate) {
|
|
5
|
+
if (schema.enableLegacyMutators !== true) {
|
|
6
|
+
return void 0;
|
|
7
|
+
}
|
|
6
8
|
const { [CRUD_MUTATION_NAME]: zeroCRUD } = repMutate;
|
|
7
9
|
const mutateBatch = async (body) => {
|
|
8
10
|
const ops = [];
|
|
@@ -108,118 +110,30 @@ function makeBatchCRUDMutate(tableName, schema, ops) {
|
|
|
108
110
|
}
|
|
109
111
|
};
|
|
110
112
|
}
|
|
113
|
+
function makeCRUDExecutor(tx, schema, ivmBranch) {
|
|
114
|
+
return (tableName, kind, value) => {
|
|
115
|
+
const { primaryKey } = schema.tables[tableName];
|
|
116
|
+
return crudImpl[kind](
|
|
117
|
+
tx,
|
|
118
|
+
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
|
119
|
+
{ op: kind, tableName, primaryKey, value },
|
|
120
|
+
schema,
|
|
121
|
+
ivmBranch
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
111
125
|
function makeCRUDMutator(schema) {
|
|
112
|
-
return async
|
|
126
|
+
return async (tx, crudArg) => {
|
|
127
|
+
const executor = makeCRUDExecutor(tx, schema, void 0);
|
|
113
128
|
for (const op of crudArg.ops) {
|
|
114
|
-
|
|
115
|
-
case "insert":
|
|
116
|
-
await insertImpl(tx, op, schema, void 0);
|
|
117
|
-
break;
|
|
118
|
-
case "upsert":
|
|
119
|
-
await upsertImpl(tx, op, schema, void 0);
|
|
120
|
-
break;
|
|
121
|
-
case "update":
|
|
122
|
-
await updateImpl(tx, op, schema, void 0);
|
|
123
|
-
break;
|
|
124
|
-
case "delete":
|
|
125
|
-
await deleteImpl(tx, op, schema, void 0);
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
129
|
+
await executor(op.tableName, op.op, op.value);
|
|
128
130
|
}
|
|
129
131
|
};
|
|
130
132
|
}
|
|
131
|
-
function defaultOptionalFieldsToNull(schema, value) {
|
|
132
|
-
let rv = value;
|
|
133
|
-
for (const name in schema.columns) {
|
|
134
|
-
if (rv[name] === void 0) {
|
|
135
|
-
rv = { ...rv, [name]: null };
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return rv;
|
|
139
|
-
}
|
|
140
|
-
async function insertImpl(tx, arg, schema, ivmBranch) {
|
|
141
|
-
const key = toPrimaryKeyString(
|
|
142
|
-
arg.tableName,
|
|
143
|
-
schema.tables[arg.tableName].primaryKey,
|
|
144
|
-
arg.value
|
|
145
|
-
);
|
|
146
|
-
if (!await tx.has(key)) {
|
|
147
|
-
const val = defaultOptionalFieldsToNull(
|
|
148
|
-
schema.tables[arg.tableName],
|
|
149
|
-
arg.value
|
|
150
|
-
);
|
|
151
|
-
await tx.set(key, val);
|
|
152
|
-
if (ivmBranch) {
|
|
153
|
-
must(ivmBranch.getSource(arg.tableName)).push({
|
|
154
|
-
type: "add",
|
|
155
|
-
row: arg.value
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
async function upsertImpl(tx, arg, schema, ivmBranch) {
|
|
161
|
-
const key = toPrimaryKeyString(
|
|
162
|
-
arg.tableName,
|
|
163
|
-
schema.tables[arg.tableName].primaryKey,
|
|
164
|
-
arg.value
|
|
165
|
-
);
|
|
166
|
-
if (await tx.has(key)) {
|
|
167
|
-
await updateImpl(tx, { ...arg }, schema, ivmBranch);
|
|
168
|
-
} else {
|
|
169
|
-
await insertImpl(tx, { ...arg }, schema, ivmBranch);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async function updateImpl(tx, arg, schema, ivmBranch) {
|
|
173
|
-
const key = toPrimaryKeyString(
|
|
174
|
-
arg.tableName,
|
|
175
|
-
schema.tables[arg.tableName].primaryKey,
|
|
176
|
-
arg.value
|
|
177
|
-
);
|
|
178
|
-
const prev = await tx.get(key);
|
|
179
|
-
if (prev === void 0) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
const update = arg.value;
|
|
183
|
-
const next = { ...prev };
|
|
184
|
-
for (const k in update) {
|
|
185
|
-
if (update[k] !== void 0) {
|
|
186
|
-
next[k] = update[k];
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
await tx.set(key, next);
|
|
190
|
-
if (ivmBranch) {
|
|
191
|
-
must(ivmBranch.getSource(arg.tableName)).push({
|
|
192
|
-
type: "edit",
|
|
193
|
-
oldRow: prev,
|
|
194
|
-
row: next
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
async function deleteImpl(tx, arg, schema, ivmBranch) {
|
|
199
|
-
const key = toPrimaryKeyString(
|
|
200
|
-
arg.tableName,
|
|
201
|
-
schema.tables[arg.tableName].primaryKey,
|
|
202
|
-
arg.value
|
|
203
|
-
);
|
|
204
|
-
const prev = await tx.get(key);
|
|
205
|
-
if (prev === void 0) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
await tx.del(key);
|
|
209
|
-
if (ivmBranch) {
|
|
210
|
-
must(ivmBranch.getSource(arg.tableName)).push({
|
|
211
|
-
type: "remove",
|
|
212
|
-
row: prev
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
133
|
export {
|
|
217
|
-
deleteImpl,
|
|
218
|
-
insertImpl,
|
|
219
134
|
makeBatchCRUDMutate,
|
|
135
|
+
makeCRUDExecutor,
|
|
220
136
|
makeCRUDMutate,
|
|
221
|
-
makeCRUDMutator
|
|
222
|
-
updateImpl,
|
|
223
|
-
upsertImpl
|
|
137
|
+
makeCRUDMutator
|
|
224
138
|
};
|
|
225
139
|
//# sourceMappingURL=crud.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crud.js","sources":["../../../../../zero-client/src/client/crud.ts"],"sourcesContent":["import type {ReadonlyJSONObject} from '../../../shared/src/json.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {promiseVoid} from '../../../shared/src/resolved-promises.ts';\nimport type {MaybePromise} from '../../../shared/src/types.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport {\n CRUD_MUTATION_NAME,\n type CRUDMutationArg,\n type CRUDOp,\n type DeleteOp,\n type InsertOp,\n type UpdateOp,\n type UpsertOp,\n} from '../../../zero-protocol/src/push.ts';\nimport type {TableSchema} from '../../../zero-schema/src/table-schema.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {\n DeleteID,\n InsertValue,\n UpdateValue,\n UpsertValue,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {IVMSourceBranch} from './ivm-branch.ts';\nimport {toPrimaryKeyString} from './keys.ts';\nimport type {MutatorDefs, WriteTransaction} from './replicache-types.ts';\n\n/**\n * This is the type of the generated mutate.<name>.<verb> function.\n */\nexport type TableMutator<S extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exists.\n * Non-primary-key fields that are 'optional' can be omitted or set to\n * `undefined`. Such fields will be assigned the value `null` optimistically\n * and then the default value as defined by the server.\n */\n insert: (value: InsertValue<S>) => Promise<void>;\n\n /**\n * Writes a row unconditionally, overwriting any existing row with the same\n * primary key. Non-primary-key fields that are 'optional' can be omitted or\n * set to `undefined`. Such fields will be assigned the value `null`\n * optimistically and then the default value as defined by the server.\n */\n upsert: (value: UpsertValue<S>) => Promise<void>;\n\n /**\n * Updates a row with the same primary key. If no such row exists, this\n * function does nothing. All non-primary-key fields can be omitted or set to\n * `undefined`. Such fields will be left unchanged from previous value.\n */\n update: (value: UpdateValue<S>) => Promise<void>;\n\n /**\n * Deletes the row with the specified primary key. If no such row exists, this\n * function does nothing.\n */\n delete: (id: DeleteID<S>) => Promise<void>;\n};\n\nexport type DBMutator<S extends Schema> =\n S['enableLegacyMutators'] extends false\n ? {} // {} is needed here for intersection type identity\n : {\n [K in keyof S['tables']]: TableMutator<S['tables'][K]>;\n };\n\nexport type BatchMutator<S extends Schema> = <R>(\n body: (m: DBMutator<S>) => MaybePromise<R>,\n) => Promise<R>;\n\ntype ZeroCRUDMutate = {\n [CRUD_MUTATION_NAME]: CRUDMutate;\n};\n\n/**\n * This is the zero.mutate object part representing the CRUD operations. If the\n * tables are `issue` and `label`, then this object will have `issue` and\n * `label` properties.\n *\n * @param schema - The schema defining the tables\n * @param repMutate - The replicache mutate object with the CRUD mutation\n * @param mutate - The object to use as the mutate object. Properties for each\n * table will be assigned to this object.\n */\nexport function makeCRUDMutate<const S extends Schema>(\n schema: S,\n repMutate: ZeroCRUDMutate,\n mutate: object,\n): BatchMutator<S> {\n const {[CRUD_MUTATION_NAME]: zeroCRUD} = repMutate;\n\n const mutateBatch = async <R>(body: (m: DBMutator<S>) => R): Promise<R> => {\n const ops: CRUDOp[] = [];\n const m = {} as Record<string, unknown>;\n for (const name of Object.keys(schema.tables)) {\n m[name] = makeBatchCRUDMutate(name, schema, ops);\n }\n\n const rv = await body(m as DBMutator<S>);\n await zeroCRUD({ops});\n return rv;\n };\n\n for (const [name, tableSchema] of Object.entries(schema.tables)) {\n (mutate as Record<string, unknown>)[name] = makeEntityCRUDMutate(\n name,\n tableSchema.primaryKey,\n zeroCRUD,\n );\n }\n return mutateBatch as BatchMutator<S>;\n}\n\n/**\n * Creates the `{insert, upsert, update, delete}` object for use outside a\n * batch.\n */\nfunction makeEntityCRUDMutate<S extends TableSchema>(\n tableName: string,\n primaryKey: S['primaryKey'],\n zeroCRUD: CRUDMutate,\n): TableMutator<S> {\n return {\n insert: (value: InsertValue<S>) => {\n const op: InsertOp = {\n op: 'insert',\n tableName,\n primaryKey,\n value,\n };\n return zeroCRUD({ops: [op]});\n },\n upsert: (value: UpsertValue<S>) => {\n const op: UpsertOp = {\n op: 'upsert',\n tableName,\n primaryKey,\n value,\n };\n return zeroCRUD({ops: [op]});\n },\n update: (value: UpdateValue<S>) => {\n const op: UpdateOp = {\n op: 'update',\n tableName,\n primaryKey,\n value,\n };\n return zeroCRUD({ops: [op]});\n },\n delete: (id: DeleteID<S>) => {\n const op: DeleteOp = {\n op: 'delete',\n tableName,\n primaryKey,\n value: id,\n };\n return zeroCRUD({ops: [op]});\n },\n };\n}\n\n/**\n * Creates the `{inesrt, upsert, update, delete}` object for use inside a\n * batch.\n */\nexport function makeBatchCRUDMutate<S extends TableSchema>(\n tableName: string,\n schema: Schema,\n ops: CRUDOp[],\n): TableMutator<S> {\n const {primaryKey} = schema.tables[tableName];\n return {\n insert: (value: InsertValue<S>) => {\n const op: InsertOp = {\n op: 'insert',\n tableName,\n primaryKey,\n value,\n };\n ops.push(op);\n return promiseVoid;\n },\n upsert: (value: UpsertValue<S>) => {\n const op: UpsertOp = {\n op: 'upsert',\n tableName,\n primaryKey,\n value,\n };\n ops.push(op);\n return promiseVoid;\n },\n update: (value: UpdateValue<S>) => {\n const op: UpdateOp = {\n op: 'update',\n tableName,\n primaryKey,\n value,\n };\n ops.push(op);\n return promiseVoid;\n },\n delete: (id: DeleteID<S>) => {\n const op: DeleteOp = {\n op: 'delete',\n tableName,\n primaryKey,\n value: id,\n };\n ops.push(op);\n return promiseVoid;\n },\n };\n}\n\nexport type WithCRUD<MD extends MutatorDefs> = MD & {\n [CRUD_MUTATION_NAME]: CRUDMutator;\n};\n\nexport type CRUDMutate = (crudArg: CRUDMutationArg) => Promise<void>;\n\nexport type CRUDMutator = (\n tx: WriteTransaction,\n crudArg: CRUDMutationArg,\n) => Promise<void>;\n\n// Zero crud mutators cannot function at the same\n// time as custom mutators as the rebase of crud mutators will not\n// update the IVM branch. That's ok, we're removing crud mutators\n// in favor of custom mutators.\nexport function makeCRUDMutator(schema: Schema): CRUDMutator {\n return async function zeroCRUDMutator(\n tx: WriteTransaction,\n crudArg: CRUDMutationArg,\n ): Promise<void> {\n for (const op of crudArg.ops) {\n switch (op.op) {\n case 'insert':\n await insertImpl(tx, op, schema, undefined);\n break;\n case 'upsert':\n await upsertImpl(tx, op, schema, undefined);\n break;\n case 'update':\n await updateImpl(tx, op, schema, undefined);\n break;\n case 'delete':\n await deleteImpl(tx, op, schema, undefined);\n break;\n }\n }\n };\n}\n\nfunction defaultOptionalFieldsToNull(\n schema: TableSchema,\n value: ReadonlyJSONObject,\n): ReadonlyJSONObject {\n let rv = value;\n for (const name in schema.columns) {\n if (rv[name] === undefined) {\n rv = {...rv, [name]: null};\n }\n }\n return rv;\n}\n\nexport async function insertImpl(\n tx: WriteTransaction,\n arg: InsertOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n if (!(await tx.has(key))) {\n const val = defaultOptionalFieldsToNull(\n schema.tables[arg.tableName],\n arg.value,\n );\n await tx.set(key, val);\n if (ivmBranch) {\n must(ivmBranch.getSource(arg.tableName)).push({\n type: 'add',\n row: arg.value,\n });\n }\n }\n}\n\nexport async function upsertImpl(\n tx: WriteTransaction,\n arg: InsertOp | UpsertOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n if (await tx.has(key)) {\n await updateImpl(tx, {...arg, op: 'update'}, schema, ivmBranch);\n } else {\n await insertImpl(tx, {...arg, op: 'insert'}, schema, ivmBranch);\n }\n}\n\nexport async function updateImpl(\n tx: WriteTransaction,\n arg: UpdateOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n const prev = await tx.get(key);\n if (prev === undefined) {\n return;\n }\n const update = arg.value;\n const next = {...(prev as ReadonlyJSONObject)};\n for (const k in update) {\n if (update[k] !== undefined) {\n next[k] = update[k];\n }\n }\n await tx.set(key, next);\n if (ivmBranch) {\n must(ivmBranch.getSource(arg.tableName)).push({\n type: 'edit',\n oldRow: prev as Row,\n row: next,\n });\n }\n}\n\nexport async function deleteImpl(\n tx: WriteTransaction,\n arg: DeleteOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): Promise<void> {\n const key = toPrimaryKeyString(\n arg.tableName,\n schema.tables[arg.tableName].primaryKey,\n arg.value,\n );\n const prev = await tx.get(key);\n if (prev === undefined) {\n return;\n }\n await tx.del(key);\n if (ivmBranch) {\n must(ivmBranch.getSource(arg.tableName)).push({\n type: 'remove',\n row: prev as Row,\n });\n }\n}\n"],"names":[],"mappings":";;;;AAqFO,SAAS,eACd,QACA,WACA,QACiB;AACjB,QAAM,EAAC,CAAC,kBAAkB,GAAG,aAAY;AAEzC,QAAM,cAAc,OAAU,SAA6C;AACzE,UAAM,MAAgB,CAAA;AACtB,UAAM,IAAI,CAAA;AACV,eAAW,QAAQ,OAAO,KAAK,OAAO,MAAM,GAAG;AAC7C,QAAE,IAAI,IAAI,oBAAoB,MAAM,QAAQ,GAAG;AAAA,IACjD;AAEA,UAAM,KAAK,MAAM,KAAK,CAAiB;AACvC,UAAM,SAAS,EAAC,KAAI;AACpB,WAAO;AAAA,EACT;AAEA,aAAW,CAAC,MAAM,WAAW,KAAK,OAAO,QAAQ,OAAO,MAAM,GAAG;AAC9D,WAAmC,IAAI,IAAI;AAAA,MAC1C;AAAA,MACA,YAAY;AAAA,MACZ;AAAA,IAAA;AAAA,EAEJ;AACA,SAAO;AACT;AAMA,SAAS,qBACP,WACA,YACA,UACiB;AACjB,SAAO;AAAA,IACL,QAAQ,CAAC,UAA0B;AACjC,YAAM,KAAe;AAAA,QACnB,IAAI;AAAA,QACJ;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAEF,aAAO,SAAS,EAAC,KAAK,CAAC,EAAE,GAAE;AAAA,IAC7B;AAAA,IACA,QAAQ,CAAC,UAA0B;AACjC,YAAM,K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ACN;AAAA,EACH;AACF;"}
|
|
1
|
+
{"version":3,"file":"crud.js","sources":["../../../../../zero-client/src/client/crud.ts"],"sourcesContent":["import {promiseVoid} from '../../../shared/src/resolved-promises.ts';\nimport type {MaybePromise} from '../../../shared/src/types.ts';\nimport {\n CRUD_MUTATION_NAME,\n type CRUDMutationArg,\n type CRUDOp,\n type DeleteOp,\n type InsertOp,\n type UpdateOp,\n type UpsertOp,\n} from '../../../zero-protocol/src/push.ts';\nimport type {TableSchema} from '../../../zero-schema/src/table-schema.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {CRUDExecutor, TableMutator} from '../../../zql/src/mutate/crud.ts';\nimport type {\n DeleteID,\n InsertValue,\n UpdateValue,\n UpsertValue,\n} from '../../../zql/src/mutate/custom.ts';\nimport * as crudImpl from './crud-impl.ts';\nimport type {IVMSourceBranch} from './ivm-branch.ts';\nimport type {MutatorDefs, WriteTransaction} from './replicache-types.ts';\n\nexport type DBMutator<S extends Schema> = S['enableLegacyMutators'] extends true\n ? {\n [K in keyof S['tables']]: TableMutator<S['tables'][K]>;\n }\n : {}; // {} is needed here for intersection type identity\n\nexport type BatchMutator<S extends Schema> =\n S['enableLegacyMutators'] extends true\n ? <R>(body: (m: DBMutator<S>) => MaybePromise<R>) => Promise<R>\n : undefined;\n\ntype ZeroCRUDMutate = {\n [CRUD_MUTATION_NAME]: CRUDMutate;\n};\n\n/**\n * This is the zero.mutate object part representing the CRUD operations. If the\n * tables are `issue` and `label`, then this object will have `issue` and\n * `label` properties.\n *\n * @param schema - The schema defining the tables\n * @param repMutate - The replicache mutate object with the CRUD mutation\n * @param mutate - The object to use as the mutate object. Properties for each\n * table will be assigned to this object.\n */\nexport function makeCRUDMutate<const S extends Schema>(\n schema: S,\n repMutate: ZeroCRUDMutate,\n mutate: object,\n): BatchMutator<S> {\n if (schema.enableLegacyMutators !== true) {\n return undefined as BatchMutator<S>;\n }\n\n const {[CRUD_MUTATION_NAME]: zeroCRUD} = repMutate;\n\n const mutateBatch = async <R>(body: (m: DBMutator<S>) => R): Promise<R> => {\n const ops: CRUDOp[] = [];\n const m = {} as Record<string, unknown>;\n for (const name of Object.keys(schema.tables)) {\n m[name] = makeBatchCRUDMutate(name, schema, ops);\n }\n\n const rv = await body(m as DBMutator<S>);\n await zeroCRUD({ops});\n return rv;\n };\n\n for (const [name, tableSchema] of Object.entries(schema.tables)) {\n (mutate as Record<string, unknown>)[name] = makeEntityCRUDMutate(\n name,\n tableSchema.primaryKey,\n zeroCRUD,\n );\n }\n return mutateBatch as BatchMutator<S>;\n}\n\n/**\n * Creates the `{insert, upsert, update, delete}` object for use outside a\n * batch.\n */\nfunction makeEntityCRUDMutate<S extends TableSchema>(\n tableName: string,\n primaryKey: S['primaryKey'],\n zeroCRUD: CRUDMutate,\n): TableMutator<S> {\n return {\n insert: (value: InsertValue<S>) => {\n const op: InsertOp = {\n op: 'insert',\n tableName,\n primaryKey,\n value,\n };\n return zeroCRUD({ops: [op]});\n },\n upsert: (value: UpsertValue<S>) => {\n const op: UpsertOp = {\n op: 'upsert',\n tableName,\n primaryKey,\n value,\n };\n return zeroCRUD({ops: [op]});\n },\n update: (value: UpdateValue<S>) => {\n const op: UpdateOp = {\n op: 'update',\n tableName,\n primaryKey,\n value,\n };\n return zeroCRUD({ops: [op]});\n },\n delete: (id: DeleteID<S>) => {\n const op: DeleteOp = {\n op: 'delete',\n tableName,\n primaryKey,\n value: id,\n };\n return zeroCRUD({ops: [op]});\n },\n };\n}\n\n/**\n * Creates the `{insert, upsert, update, delete}` object for use inside a\n * batch.\n */\nexport function makeBatchCRUDMutate<S extends TableSchema>(\n tableName: string,\n schema: Schema,\n ops: CRUDOp[],\n): TableMutator<S> {\n const {primaryKey} = schema.tables[tableName];\n return {\n insert: (value: InsertValue<S>) => {\n const op: InsertOp = {\n op: 'insert',\n tableName,\n primaryKey,\n value,\n };\n ops.push(op);\n return promiseVoid;\n },\n upsert: (value: UpsertValue<S>) => {\n const op: UpsertOp = {\n op: 'upsert',\n tableName,\n primaryKey,\n value,\n };\n ops.push(op);\n return promiseVoid;\n },\n update: (value: UpdateValue<S>) => {\n const op: UpdateOp = {\n op: 'update',\n tableName,\n primaryKey,\n value,\n };\n ops.push(op);\n return promiseVoid;\n },\n delete: (id: DeleteID<S>) => {\n const op: DeleteOp = {\n op: 'delete',\n tableName,\n primaryKey,\n value: id,\n };\n ops.push(op);\n return promiseVoid;\n },\n };\n}\n\nexport type WithCRUD<MD extends MutatorDefs> = MD & {\n [CRUD_MUTATION_NAME]: CRUDMutator;\n};\n\nexport type CRUDMutate = (crudArg: CRUDMutationArg) => Promise<void>;\n\nexport type CRUDMutator = (\n tx: WriteTransaction,\n crudArg: CRUDMutationArg,\n) => Promise<void>;\n\nexport function makeCRUDExecutor(\n tx: WriteTransaction,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n): CRUDExecutor {\n return (tableName, kind, value) => {\n const {primaryKey} = schema.tables[tableName];\n return crudImpl[kind](\n tx,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n {op: kind, tableName, primaryKey, value} as any,\n schema,\n ivmBranch,\n );\n };\n}\n\n// Zero crud mutators cannot function at the same\n// time as custom mutators as the rebase of crud mutators will not\n// update the IVM branch. That's ok, we're removing crud mutators\n// in favor of custom mutators.\nexport function makeCRUDMutator(schema: Schema): CRUDMutator {\n return async (\n tx: WriteTransaction,\n crudArg: CRUDMutationArg,\n ): Promise<void> => {\n const executor = makeCRUDExecutor(tx, schema, undefined);\n for (const op of crudArg.ops) {\n await executor(op.tableName, op.op, op.value);\n }\n };\n}\n"],"names":[],"mappings":";;;AAiDO,SAAS,eACd,QACA,WACA,QACiB;AACjB,MAAI,OAAO,yBAAyB,MAAM;AACxC,WAAO;AAAA,EACT;AAEA,QAAM,EAAC,CAAC,kBAAkB,GAAG,aAAY;AAEzC,QAAM,cAAc,OAAU,SAA6C;AACzE,UAAM,MAAgB,CAAA;AACtB,UAAM,IAAI,CAAA;AACV,eAAW,QAA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ue } from '../../../shared/src/json.ts';
|
|
4
4
|
import type { DefaultSchema } from '../../../zero-types/src/default-types.ts';
|
|
5
5
|
import type { Schema } from '../../../zero-types/src/schema.ts';
|
|
6
|
-
import type { ClientTransaction,
|
|
6
|
+
import type { ClientTransaction, MutateCRUD, Transaction } from '../../../zql/src/mutate/custom.ts';
|
|
7
7
|
import { type HumanReadable, type Query, type RunOptions } from '../../../zql/src/query/query.ts';
|
|
8
8
|
import type { SchemaQuery } from '../../../zql/src/query/schema-query.ts';
|
|
9
9
|
import type { ClientID } from '../types/client-state.ts';
|
|
@@ -54,7 +54,7 @@ export type MakeCustomMutatorInterface<TSchema extends Schema, F> = F extends (t
|
|
|
54
54
|
export declare class TransactionImpl<TSchema extends Schema = DefaultSchema> implements ClientTransaction<TSchema> {
|
|
55
55
|
#private;
|
|
56
56
|
readonly location = "client";
|
|
57
|
-
readonly mutate:
|
|
57
|
+
readonly mutate: MutateCRUD<TSchema>;
|
|
58
58
|
readonly query: SchemaQuery<TSchema>;
|
|
59
59
|
constructor(lc: LogContext, repTx: WriteTransaction, schema: TSchema);
|
|
60
60
|
get clientID(): ClientID;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/custom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,+CAA+C,CAAC;AAI9E,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/custom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,+CAA+C,CAAC;AAI9E,OAAO,KAAK,EAAC,iBAAiB,EAAC,MA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CAAC;AAEV,qBAAa,eAAe,CAAC,OAAO,SAAS,MAAM,GAAG,aAAa,CACjE,YAAW,iBAAiB,CAAC,OAAO,CAAC;;IAErC,QAAQ,CAAC,QAAQ,YAAY;IAC7B,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IACrC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;gBAIzB,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,OAAO;IAmBpE,IAAI,QAAQ,IAAI,QAAQ,CAEvB;IAED,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,IAAI,MAAM,IAAI,YAAY,GAAG,QAAQ,CAEpC;IAED,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;IAED,GAAG,CAAC,MAAM,SAAS,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,MAAM,EAAE,OAAO,EAC1D,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,EACtC,OAAO,CAAC,EAAE,UAAU,GACnB,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;CAGnC;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,gBAAgB,GAAG,UAAU,CAMjE;AAED,wBAAgB,qBAAqB,CACnC,CAAC,SAAS,MAAM,EAChB,mBAAmB,EACnB,OAAO,EAEP,EAAE,EAAE,UAAU,EACd,OAAO,EAAE,iBAAiB,CAAC,CAAC,EAAE,mBAAmB,CAAC,EAClD,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,OAAO,GACf,CAAC,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,iBAAiB,KAAK,OAAO,CAAC,IAAI,CAAC,CAQrE"}
|
|
@@ -2,10 +2,11 @@ import { zeroData } from "../../../replicache/src/transactions.js";
|
|
|
2
2
|
import { assert } from "../../../shared/src/asserts.js";
|
|
3
3
|
import { must } from "../../../shared/src/must.js";
|
|
4
4
|
import { emptyFunction } from "../../../shared/src/sentinels.js";
|
|
5
|
+
import { makeMutateCRUDFunction } from "../../../zql/src/mutate/crud.js";
|
|
5
6
|
import { createRunnableBuilder } from "../../../zql/src/query/create-builder.js";
|
|
6
7
|
import "../../../zero-protocol/src/ast.js";
|
|
7
8
|
import { ZeroContext } from "./context.js";
|
|
8
|
-
import {
|
|
9
|
+
import { makeCRUDExecutor } from "./crud.js";
|
|
9
10
|
class TransactionImpl {
|
|
10
11
|
location = "client";
|
|
11
12
|
mutate;
|
|
@@ -15,12 +16,10 @@ class TransactionImpl {
|
|
|
15
16
|
constructor(lc, repTx, schema) {
|
|
16
17
|
must(repTx.reason === "initial" || repTx.reason === "rebase");
|
|
17
18
|
const txData = getZeroTxData(repTx);
|
|
19
|
+
const ivmBranch = txData.ivmSources;
|
|
18
20
|
this.#repTx = repTx;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
repTx,
|
|
22
|
-
txData.ivmSources
|
|
23
|
-
);
|
|
21
|
+
const executor = makeCRUDExecutor(repTx, schema, ivmBranch);
|
|
22
|
+
this.mutate = makeMutateCRUDFunction(schema, executor);
|
|
24
23
|
const zeroContext = newZeroContext(
|
|
25
24
|
lc,
|
|
26
25
|
txData.ivmSources
|
|
@@ -57,20 +56,6 @@ function makeReplicacheMutator(lc, mutator, schema, context) {
|
|
|
57
56
|
await mutator(tx, args, context);
|
|
58
57
|
};
|
|
59
58
|
}
|
|
60
|
-
function makeSchemaCRUD(schema, tx, ivmBranch) {
|
|
61
|
-
return new Proxy(
|
|
62
|
-
{},
|
|
63
|
-
{
|
|
64
|
-
get(target, prop) {
|
|
65
|
-
if (prop in target) {
|
|
66
|
-
return target[prop];
|
|
67
|
-
}
|
|
68
|
-
target[prop] = makeTableCRUD(schema, prop, tx, ivmBranch);
|
|
69
|
-
return target[prop];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
59
|
function assertValidRunOptions(options) {
|
|
75
60
|
assert(
|
|
76
61
|
options?.type !== "complete",
|
|
@@ -91,36 +76,6 @@ function newZeroContext(lc, ivmBranch) {
|
|
|
91
76
|
assertValidRunOptions
|
|
92
77
|
);
|
|
93
78
|
}
|
|
94
|
-
function makeTableCRUD(schema, tableName, tx, ivmBranch) {
|
|
95
|
-
const table = must(schema.tables[tableName]);
|
|
96
|
-
const { primaryKey } = table;
|
|
97
|
-
return {
|
|
98
|
-
insert: (value) => insertImpl(
|
|
99
|
-
tx,
|
|
100
|
-
{ tableName, value },
|
|
101
|
-
schema,
|
|
102
|
-
ivmBranch
|
|
103
|
-
),
|
|
104
|
-
upsert: (value) => upsertImpl(
|
|
105
|
-
tx,
|
|
106
|
-
{ op: "upsert", tableName, primaryKey, value },
|
|
107
|
-
schema,
|
|
108
|
-
ivmBranch
|
|
109
|
-
),
|
|
110
|
-
update: (value) => updateImpl(
|
|
111
|
-
tx,
|
|
112
|
-
{ tableName, value },
|
|
113
|
-
schema,
|
|
114
|
-
ivmBranch
|
|
115
|
-
),
|
|
116
|
-
delete: (id) => deleteImpl(
|
|
117
|
-
tx,
|
|
118
|
-
{ tableName, value: id },
|
|
119
|
-
schema,
|
|
120
|
-
ivmBranch
|
|
121
|
-
)
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
79
|
export {
|
|
125
80
|
TransactionImpl,
|
|
126
81
|
getZeroTxData,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.js","sources":["../../../../../zero-client/src/client/custom.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {ZeroTxData} from '../../../replicache/src/replicache-options.ts';\nimport type {WriteTransactionImpl} from '../../../replicache/src/transactions.ts';\nimport {zeroData} from '../../../replicache/src/transactions.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport type {ReadonlyJSONValue} from '../../../shared/src/json.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {emptyFunction} from '../../../shared/src/sentinels.ts';\nimport type {TableSchema} from '../../../zero-schema/src/table-schema.ts';\nimport type {DefaultSchema} from '../../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {\n ClientTransaction,\n DeleteID,\n InsertValue,\n SchemaCRUD,\n TableCRUD,\n Transaction,\n UpdateValue,\n UpsertValue,\n} from '../../../zql/src/mutate/custom.ts';\nimport {createRunnableBuilder} from '../../../zql/src/query/create-builder.ts';\nimport {\n type HumanReadable,\n type Query,\n type RunOptions,\n} from '../../../zql/src/query/query.ts';\nimport type {SchemaQuery} from '../../../zql/src/query/schema-query.ts';\nimport type {ClientID} from '../types/client-state.ts';\nimport {ZeroContext} from './context.ts';\nimport {deleteImpl, insertImpl, updateImpl, upsertImpl} from './crud.ts';\nimport type {IVMSourceBranch} from './ivm-branch.ts';\nimport type {WriteTransaction} from './replicache-types.ts';\n\n/**\n * The shape which a user's custom mutator definitions must conform to.\n * Supports arbitrary depth nesting of namespaces.\n */\nexport type CustomMutatorDefs = {\n // oxlint-disable-next-line no-explicit-any\n [namespaceOrKey: string]: CustomMutatorImpl<any> | CustomMutatorDefs;\n};\n\nexport type MutatorResultDetails =\n | {\n readonly type: 'success';\n }\n | {\n readonly type: 'error';\n readonly error:\n | {\n readonly type: 'app';\n readonly message: string;\n readonly details: ReadonlyJSONValue | undefined;\n }\n | {\n readonly type: 'zero';\n readonly message: string;\n };\n };\n\nexport type MutatorResultSuccessDetails = Extract<\n MutatorResultDetails,\n {type: 'success'}\n>;\nexport type MutatorResultErrorDetails = Extract<\n MutatorResultDetails,\n {type: 'error'}\n>;\n\nexport type MutatorResult = {\n client: Promise<MutatorResultDetails & {}>;\n server: Promise<MutatorResultDetails & {}>;\n} & {};\n\nexport type CustomMutatorImpl<\n S extends Schema,\n TWrappedTransaction = unknown,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n TArgs = any,\n Context = unknown,\n> = (\n tx: Transaction<S, TWrappedTransaction>,\n // TODO: many args. See commit: 52657c2f934b4a458d628ea77e56ce92b61eb3c6 which did have many args.\n // The issue being that it will be a protocol change to support varargs.\n args: TArgs,\n ctx: Context,\n) => Promise<void>;\n\n/**\n * The shape exposed on the `Zero.mutate` instance.\n * The signature of a custom mutator takes a `transaction` as its first arg\n * but the user does not provide this arg when calling the mutator.\n *\n * This utility strips the `tx` arg from the user's custom mutator signatures.\n * Supports arbitrary depth nesting of namespaces.\n */\nexport type MakeCustomMutatorInterfaces<\n S extends Schema,\n MD extends CustomMutatorDefs,\n TContext,\n> = {\n readonly [NamespaceOrName in keyof MD]: MD[NamespaceOrName] extends (\n tx: Transaction<S>,\n ...args: infer Args\n ) => Promise<void>\n ? (...args: Args) => MutatorResult\n : MD[NamespaceOrName] extends CustomMutatorDefs\n ? MakeCustomMutatorInterfaces<S, MD[NamespaceOrName], TContext>\n : never;\n};\n\nexport type MakeCustomMutatorInterface<TSchema extends Schema, F> = F extends (\n tx: ClientTransaction<TSchema>,\n ...args: infer Args\n) => Promise<void>\n ? (...args: Args) => MutatorResult\n : never;\n\nexport class TransactionImpl<TSchema extends Schema = DefaultSchema>\n implements ClientTransaction<TSchema>\n{\n readonly location = 'client';\n readonly mutate: SchemaCRUD<TSchema>;\n readonly query: SchemaQuery<TSchema>;\n readonly #repTx: WriteTransaction;\n readonly #zeroContext: ZeroContext;\n\n constructor(lc: LogContext, repTx: WriteTransaction, schema: TSchema) {\n must(repTx.reason === 'initial' || repTx.reason === 'rebase');\n const txData = getZeroTxData(repTx);\n\n this.#repTx = repTx;\n this.mutate = makeSchemaCRUD(\n schema,\n repTx,\n txData.ivmSources as IVMSourceBranch,\n );\n\n const zeroContext = newZeroContext(\n lc,\n txData.ivmSources as IVMSourceBranch,\n );\n\n this.query = createRunnableBuilder(zeroContext, schema);\n this.#zeroContext = zeroContext;\n }\n\n get clientID(): ClientID {\n return this.#repTx.clientID;\n }\n\n get mutationID(): number {\n return this.#repTx.mutationID;\n }\n\n get reason(): 'optimistic' | 'rebase' {\n return this.#repTx.reason === 'initial' ? 'optimistic' : 'rebase';\n }\n\n get token(): string | undefined {\n return (this.#repTx as WriteTransactionImpl)[zeroData]?.token;\n }\n\n run<TTable extends keyof TSchema['tables'] & string, TReturn>(\n query: Query<TTable, TSchema, TReturn>,\n options?: RunOptions,\n ): Promise<HumanReadable<TReturn>> {\n return this.#zeroContext.run(query, options);\n }\n}\n\nexport function getZeroTxData(repTx: WriteTransaction): ZeroTxData {\n const txData = must(\n (repTx as WriteTransactionImpl)[zeroData],\n 'zero was not set on replicache internal options!',\n );\n return txData as ZeroTxData;\n}\n\nexport function makeReplicacheMutator<\n S extends Schema,\n TWrappedTransaction,\n Context,\n>(\n lc: LogContext,\n mutator: CustomMutatorImpl<S, TWrappedTransaction>,\n schema: S,\n context: Context,\n): (repTx: WriteTransaction, args: ReadonlyJSONValue) => Promise<void> {\n return async (\n repTx: WriteTransaction,\n args: ReadonlyJSONValue,\n ): Promise<void> => {\n const tx = new TransactionImpl(lc, repTx, schema);\n await mutator(tx, args, context);\n };\n}\n\nfunction makeSchemaCRUD<S extends Schema>(\n schema: S,\n tx: WriteTransaction,\n ivmBranch: IVMSourceBranch,\n) {\n // Only creates the CRUD mutators on demand\n // rather than creating them all up-front for each mutation.\n return new Proxy(\n {},\n {\n get(target: Record<string, TableCRUD<TableSchema>>, prop: string) {\n if (prop in target) {\n return target[prop];\n }\n\n target[prop] = makeTableCRUD(schema, prop, tx, ivmBranch);\n return target[prop];\n },\n },\n ) as SchemaCRUD<S>;\n}\n\nfunction assertValidRunOptions(options: RunOptions | undefined): void {\n // TODO(arv): We should enforce this with the type system too.\n assert(\n options?.type !== 'complete',\n 'Cannot wait for complete results in custom mutations',\n );\n}\n\nfunction newZeroContext(lc: LogContext, ivmBranch: IVMSourceBranch) {\n return new ZeroContext(\n lc,\n ivmBranch,\n () => emptyFunction,\n () => emptyFunction,\n emptyFunction,\n emptyFunction,\n emptyFunction,\n applyViewUpdates => applyViewUpdates(),\n emptyFunction,\n assertValidRunOptions,\n );\n}\n\nfunction makeTableCRUD(\n schema: Schema,\n tableName: string,\n tx: WriteTransaction,\n ivmBranch: IVMSourceBranch,\n) {\n const table = must(schema.tables[tableName]);\n const {primaryKey} = table;\n return {\n insert: (value: InsertValue<TableSchema>) =>\n insertImpl(\n tx,\n {op: 'insert', tableName, primaryKey, value},\n schema,\n ivmBranch,\n ),\n upsert: (value: UpsertValue<TableSchema>) =>\n upsertImpl(\n tx,\n {op: 'upsert', tableName, primaryKey, value},\n schema,\n ivmBranch,\n ),\n update: (value: UpdateValue<TableSchema>) =>\n updateImpl(\n tx,\n {op: 'update', tableName, primaryKey, value},\n schema,\n ivmBranch,\n ),\n delete: (id: DeleteID<TableSchema>) =>\n deleteImpl(\n tx,\n {op: 'delete', tableName, primaryKey, value: id},\n schema,\n ivmBranch,\n ),\n };\n}\n"],"names":[],"mappings":";;;;;;;;AAuHO,MAAM,gBAEb;AAAA,EACW,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,IAAgB,OAAyB,QAAiB;AACpE,SAAK,MAAM,WAAW,aAAa,MAAM,WAAW,QAAQ;AAC5D,UAAM,SAAS,cAAc,KAAK;AAElC,SAAK,SAAS;AACd,SAAK,SAAS;AAAA,MACZ;AAAA,MACA;AAAA,MACA,OAAO;AAAA,IAAA;AAGT,UAAM,cAAc;AAAA,MAClB;AAAA,MACA,OAAO;AAAA,IAAA;AAGT,SAAK,QAAQ,sBAAsB,aAAa,MAAM;AACtD,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,IAAI,WAAqB;AACvB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,IAAI,aAAqB;AACvB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,IAAI,SAAkC;AACpC,WAAO,KAAK,OAAO,WAAW,YAAY,eAAe;AAAA,EAC3D;AAAA,EAEA,IAAI,QAA4B;AAC9B,WAAQ,KAAK,OAAgC,QAAQ,GAAG;AAAA,EAC1D;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,MACA,EAAC,IAAI,UAAU,WAAW,YAAY,MAAA;AAAA,MACtC;AAAA,MACA;AAAA,IAAA;AAAA,IAEJ,QAAQ,CAAC,UACP;AAAA,MACE;AAAA,MACA,EAAe,WAAuB,MAAA;AAAA,MACtC;AAAA,MACA;AAAA,IAAA;AAAA,IAEJ,QAAQ,CAAC,OACP;AAAA,MACE;AAAA,MACA,EAAe,WAAuB,OAAO,GAAA;AAAA,MAC7C;AAAA,MACA;AAAA,IAAA;AAAA,EACF;AAEN;"}
|
|
1
|
+
{"version":3,"file":"custom.js","sources":["../../../../../zero-client/src/client/custom.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {ZeroTxData} from '../../../replicache/src/replicache-options.ts';\nimport type {WriteTransactionImpl} from '../../../replicache/src/transactions.ts';\nimport {zeroData} from '../../../replicache/src/transactions.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport type {ReadonlyJSONValue} from '../../../shared/src/json.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {emptyFunction} from '../../../shared/src/sentinels.ts';\nimport type {DefaultSchema} from '../../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport {makeMutateCRUDFunction} from '../../../zql/src/mutate/crud.ts';\nimport type {\n ClientTransaction,\n MutateCRUD,\n Transaction,\n} from '../../../zql/src/mutate/custom.ts';\nimport {createRunnableBuilder} from '../../../zql/src/query/create-builder.ts';\nimport {\n type HumanReadable,\n type Query,\n type RunOptions,\n} from '../../../zql/src/query/query.ts';\nimport type {SchemaQuery} from '../../../zql/src/query/schema-query.ts';\nimport type {ClientID} from '../types/client-state.ts';\nimport {ZeroContext} from './context.ts';\nimport {makeCRUDExecutor} from './crud.ts';\nimport type {IVMSourceBranch} from './ivm-branch.ts';\nimport type {WriteTransaction} from './replicache-types.ts';\n\n/**\n * The shape which a user's custom mutator definitions must conform to.\n * Supports arbitrary depth nesting of namespaces.\n */\nexport type CustomMutatorDefs = {\n // oxlint-disable-next-line no-explicit-any\n [namespaceOrKey: string]: CustomMutatorImpl<any> | CustomMutatorDefs;\n};\n\nexport type MutatorResultDetails =\n | {\n readonly type: 'success';\n }\n | {\n readonly type: 'error';\n readonly error:\n | {\n readonly type: 'app';\n readonly message: string;\n readonly details: ReadonlyJSONValue | undefined;\n }\n | {\n readonly type: 'zero';\n readonly message: string;\n };\n };\n\nexport type MutatorResultSuccessDetails = Extract<\n MutatorResultDetails,\n {type: 'success'}\n>;\nexport type MutatorResultErrorDetails = Extract<\n MutatorResultDetails,\n {type: 'error'}\n>;\n\nexport type MutatorResult = {\n client: Promise<MutatorResultDetails & {}>;\n server: Promise<MutatorResultDetails & {}>;\n} & {};\n\nexport type CustomMutatorImpl<\n S extends Schema,\n TWrappedTransaction = unknown,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n TArgs = any,\n Context = unknown,\n> = (\n tx: Transaction<S, TWrappedTransaction>,\n // TODO: many args. See commit: 52657c2f934b4a458d628ea77e56ce92b61eb3c6 which did have many args.\n // The issue being that it will be a protocol change to support varargs.\n args: TArgs,\n ctx: Context,\n) => Promise<void>;\n\n/**\n * The shape exposed on the `Zero.mutate` instance.\n * The signature of a custom mutator takes a `transaction` as its first arg\n * but the user does not provide this arg when calling the mutator.\n *\n * This utility strips the `tx` arg from the user's custom mutator signatures.\n * Supports arbitrary depth nesting of namespaces.\n */\nexport type MakeCustomMutatorInterfaces<\n S extends Schema,\n MD extends CustomMutatorDefs,\n TContext,\n> = {\n readonly [NamespaceOrName in keyof MD]: MD[NamespaceOrName] extends (\n tx: Transaction<S>,\n ...args: infer Args\n ) => Promise<void>\n ? (...args: Args) => MutatorResult\n : MD[NamespaceOrName] extends CustomMutatorDefs\n ? MakeCustomMutatorInterfaces<S, MD[NamespaceOrName], TContext>\n : never;\n};\n\nexport type MakeCustomMutatorInterface<TSchema extends Schema, F> = F extends (\n tx: ClientTransaction<TSchema>,\n ...args: infer Args\n) => Promise<void>\n ? (...args: Args) => MutatorResult\n : never;\n\nexport class TransactionImpl<TSchema extends Schema = DefaultSchema>\n implements ClientTransaction<TSchema>\n{\n readonly location = 'client';\n readonly mutate: MutateCRUD<TSchema>;\n readonly query: SchemaQuery<TSchema>;\n readonly #repTx: WriteTransaction;\n readonly #zeroContext: ZeroContext;\n\n constructor(lc: LogContext, repTx: WriteTransaction, schema: TSchema) {\n must(repTx.reason === 'initial' || repTx.reason === 'rebase');\n const txData = getZeroTxData(repTx);\n const ivmBranch = txData.ivmSources as IVMSourceBranch;\n\n this.#repTx = repTx;\n\n const executor = makeCRUDExecutor(repTx, schema, ivmBranch);\n this.mutate = makeMutateCRUDFunction(schema, executor);\n\n const zeroContext = newZeroContext(\n lc,\n txData.ivmSources as IVMSourceBranch,\n );\n\n this.query = createRunnableBuilder(zeroContext, schema);\n this.#zeroContext = zeroContext;\n }\n\n get clientID(): ClientID {\n return this.#repTx.clientID;\n }\n\n get mutationID(): number {\n return this.#repTx.mutationID;\n }\n\n get reason(): 'optimistic' | 'rebase' {\n return this.#repTx.reason === 'initial' ? 'optimistic' : 'rebase';\n }\n\n get token(): string | undefined {\n return (this.#repTx as WriteTransactionImpl)[zeroData]?.token;\n }\n\n run<TTable extends keyof TSchema['tables'] & string, TReturn>(\n query: Query<TTable, TSchema, TReturn>,\n options?: RunOptions,\n ): Promise<HumanReadable<TReturn>> {\n return this.#zeroContext.run(query, options);\n }\n}\n\nexport function getZeroTxData(repTx: WriteTransaction): ZeroTxData {\n const txData = must(\n (repTx as WriteTransactionImpl)[zeroData],\n 'zero was not set on replicache internal options!',\n );\n return txData as ZeroTxData;\n}\n\nexport function makeReplicacheMutator<\n S extends Schema,\n TWrappedTransaction,\n Context,\n>(\n lc: LogContext,\n mutator: CustomMutatorImpl<S, TWrappedTransaction>,\n schema: S,\n context: Context,\n): (repTx: WriteTransaction, args: ReadonlyJSONValue) => Promise<void> {\n return async (\n repTx: WriteTransaction,\n args: ReadonlyJSONValue,\n ): Promise<void> => {\n const tx = new TransactionImpl(lc, repTx, schema);\n await mutator(tx, args, context);\n };\n}\n\nfunction assertValidRunOptions(options: RunOptions | undefined): void {\n // TODO(arv): We should enforce this with the type system too.\n assert(\n options?.type !== 'complete',\n 'Cannot wait for complete results in custom mutations',\n );\n}\n\nfunction newZeroContext(lc: LogContext, ivmBranch: IVMSourceBranch) {\n return new ZeroContext(\n lc,\n ivmBranch,\n () => emptyFunction,\n () => emptyFunction,\n emptyFunction,\n emptyFunction,\n emptyFunction,\n applyViewUpdates => applyViewUpdates(),\n emptyFunction,\n assertValidRunOptions,\n );\n}\n"],"names":[],"mappings":";;;;;;;;;AAkHO,MAAM,gBAEb;AAAA,EACW,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,IAAgB,OAAyB,QAAiB;AACpE,SAAK,MAAM,WAAW,aAAa,MAAM,WAAW,QAAQ;AAC5D,UAAM,SAAS,cAAc,KAAK;AAClC,UAAM,YAAY,OAAO;AAEzB,SAAK,SAAS;AAEd,UAAM,WAAW,iBAAiB,OAAO,QAAQ,SAAS;AAC1D,SAAK,SAAS,uBAAuB,QAAQ,QAAQ;AAErD,UAAM,cAAc;AAAA,MAClB;AAAA,MACA,OAAO;AAAA,IAAA;AAGT,SAAK,QAAQ,sBAAsB,aAAa,MAAM;AACtD,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,IAAI,WAAqB;AACvB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,IAAI,aAAqB;AACvB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,IAAI,SAAkC;AACpC,WAAO,KAAK,OAAO,WAAW,YAAY,eAAe;AAAA,EAC3D;AAAA,EAEA,IAAI,QAA4B;AAC9B,WAAQ,KAAK,OAAgC,QAAQ,GAAG;AAAA,EAC1D;AAAA,EAEA,IACE,OACA,SACiC;AACjC,WAAO,KAAK,aAAa,IAAI,OAAO,OAAO;AAAA,EAC7C;AACF;AAEO,SAAS,cAAc,OAAqC;AACjE,QAAM,SAAS;AAAA,IACZ,MAA+B,QAAQ;AAAA,IACxC;AAAA,EAAA;AAEF,SAAO;AACT;AAEO,SAAS,sBAKd,IACA,SACA,QACA,SACqE;AACrE,SAAO,OACL,OACA,SACkB;AAClB,UAAM,KAAK,IAAI,gBAAgB,IAAI,OAAO,MAAM;AAChD,UAAM,QAAQ,IAAI,MAAM,OAAO;AAAA,EACjC;AACF;AAEA,SAAS,sBAAsB,SAAuC;AAEpE;AAAA,IACE,SAAS,SAAS;AAAA,IAClB;AAAA,EAAA;AAEJ;AAEA,SAAS,eAAe,IAAgB,WAA4B;AAClE,SAAO,IAAI;AAAA,IACT;AAAA,IACA;AAAA,IACA,MAAM;AAAA,IACN,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA,sBAAoB,iBAAA;AAAA,IACpB;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ivm-branch.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/ivm-branch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,WAAW,EACZ,MAAM,uCAAuC,CAAC;AAC/C,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,2CAA2C,CAAC;AACzE,OAAO,EAAY,KAAK,KAAK,EAAC,MAAM,sCAAsC,CAAC;AAG3E,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,iCAAiC,CAAC;AAC1D,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,+CAA+C,CAAC;AASnF,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,0CAA0C,CAAC;AAC1E,OAAO,EAAC,YAAY,EAAC,MAAM,uCAAuC,CAAC;
|
|
1
|
+
{"version":3,"file":"ivm-branch.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/ivm-branch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,WAAW,EACZ,MAAM,uCAAuC,CAAC;AAC/C,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,2CAA2C,CAAC;AACzE,OAAO,EAAY,KAAK,KAAK,EAAC,MAAM,sCAAsC,CAAC;AAG3E,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,iCAAiC,CAAC;AAC1D,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,+CAA+C,CAAC;AASnF,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,0CAA0C,CAAC;AAC1E,OAAO,EAAC,YAAY,EAAC,MAAM,uCAAuC,CAAC;AAInE;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,eAAe;;IAG1B,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;gBAGrB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EACnC,IAAI,CAAC,EAAE,IAAI,EACX,OAAO,GAAE,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS,CAAa;IAO5D,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAajD,KAAK;IAIL;;;OAGG;IACH,OAAO,CAAC,YAAY,EAAE,IAAI,GAAG,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW;IAWzE;;OAEG;IACG,UAAU,CACd,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,IAAI,EACjB,WAAW,CAAC,EAAE,eAAe,GAC5B,OAAO,CAAC,eAAe,CAAC;IAY3B;;;;;;;OAOG;IACH,IAAI;CAYL;AAED,wBAAsB,aAAa,CACjC,MAAM,EAAE,eAAe,EACvB,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,KAAK,iBAkBb"}
|
|
@@ -9,6 +9,7 @@ import { using, withRead } from "../../../replicache/src/with-transactions.js";
|
|
|
9
9
|
import { wrapIterable } from "../../../shared/src/iterables.js";
|
|
10
10
|
import { must } from "../../../shared/src/must.js";
|
|
11
11
|
import { MemorySource } from "../../../zql/src/ivm/memory-source.js";
|
|
12
|
+
import { consume } from "../../../zql/src/ivm/stream.js";
|
|
12
13
|
import { ENTITIES_KEY_PREFIX, sourceNameFromKey } from "./keys.js";
|
|
13
14
|
class IVMSourceBranch {
|
|
14
15
|
#sources;
|
|
@@ -122,23 +123,29 @@ function applyDiffs(diffs, branch) {
|
|
|
122
123
|
const source = must(branch.getSource(name));
|
|
123
124
|
switch (diff2.op) {
|
|
124
125
|
case "del":
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
126
|
+
consume(
|
|
127
|
+
source.push({
|
|
128
|
+
type: "remove",
|
|
129
|
+
row: diff2.oldValue
|
|
130
|
+
})
|
|
131
|
+
);
|
|
129
132
|
break;
|
|
130
133
|
case "add":
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
134
|
+
consume(
|
|
135
|
+
source.push({
|
|
136
|
+
type: "add",
|
|
137
|
+
row: diff2.newValue
|
|
138
|
+
})
|
|
139
|
+
);
|
|
135
140
|
break;
|
|
136
141
|
case "change":
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
+
consume(
|
|
143
|
+
source.push({
|
|
144
|
+
type: "edit",
|
|
145
|
+
row: diff2.newValue,
|
|
146
|
+
oldRow: diff2.oldValue
|
|
147
|
+
})
|
|
148
|
+
);
|
|
142
149
|
break;
|
|
143
150
|
}
|
|
144
151
|
}
|