@rocicorp/zero 0.25.13-canary.1 → 0.25.13-canary.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/out/zero/package.json.js +1 -1
- package/out/zero/src/zero.js +2 -0
- package/out/zero/src/zero.js.map +1 -1
- package/out/zero-client/src/client/context.js +1 -0
- package/out/zero-client/src/client/context.js.map +1 -1
- package/out/zero-client/src/client/crud-impl.d.ts +4 -4
- package/out/zero-client/src/client/crud-impl.d.ts.map +1 -1
- package/out/zero-client/src/client/crud-impl.js +32 -23
- package/out/zero-client/src/client/crud-impl.js.map +1 -1
- package/out/zero-client/src/client/crud.d.ts +1 -1
- package/out/zero-client/src/client/crud.d.ts.map +1 -1
- package/out/zero-client/src/client/crud.js +9 -3
- 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 +4 -4
- package/out/zero-client/src/client/custom.js.map +1 -1
- package/out/zero-client/src/client/ivm-branch.d.ts +3 -1
- package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
- package/out/zero-client/src/client/ivm-branch.js +27 -17
- package/out/zero-client/src/client/ivm-branch.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 +7 -5
- 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/mod.d.ts +1 -0
- package/out/zero-client/src/mod.d.ts.map +1 -1
- package/out/zero-react/src/zero-provider.js +3 -0
- package/out/zero-react/src/zero-provider.js.map +1 -1
- package/out/zero-solid/src/solid-view.js +3 -2
- package/out/zero-solid/src/solid-view.js.map +1 -1
- package/out/zero-solid/src/use-query.js +2 -1
- package/out/zero-solid/src/use-query.js.map +1 -1
- package/out/zero-solid/src/use-zero.js +3 -0
- package/out/zero-solid/src/use-zero.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts +24 -3
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +114 -14
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/source.d.ts +10 -2
- package/out/zql/src/ivm/source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.d.ts +3 -3
- package/out/zqlite/src/table-source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.js +2 -2
- package/out/zqlite/src/table-source.js.map +1 -1
- package/package.json +1 -1
package/out/zero/package.json.js
CHANGED
package/out/zero/src/zero.js
CHANGED
|
@@ -9,6 +9,7 @@ import { relationships } from "../../zero-schema/src/builder/relationship-builde
|
|
|
9
9
|
import { createSchema } from "../../zero-schema/src/builder/schema-builder.js";
|
|
10
10
|
import { boolean, enumeration, json, number, string, table } from "../../zero-schema/src/builder/table-builder.js";
|
|
11
11
|
import { ANYONE_CAN, ANYONE_CAN_DO_ANYTHING, NOBODY_CAN, definePermissions } from "../../zero-schema/src/permissions.js";
|
|
12
|
+
import { debugMemorySource } from "../../zql/src/ivm/memory-source.js";
|
|
12
13
|
import { defineMutators, defineMutatorsWithType, getMutator, isMutatorRegistry, mustGetMutator } from "../../zql/src/mutate/mutator-registry.js";
|
|
13
14
|
import { defineMutator, defineMutatorWithType, isMutator, isMutatorDefinition } from "../../zql/src/mutate/mutator.js";
|
|
14
15
|
import { createBuilder } from "../../zql/src/query/create-builder.js";
|
|
@@ -32,6 +33,7 @@ export {
|
|
|
32
33
|
boolean,
|
|
33
34
|
createBuilder,
|
|
34
35
|
createSchema,
|
|
36
|
+
debugMemorySource,
|
|
35
37
|
defineMutator,
|
|
36
38
|
defineMutatorWithType,
|
|
37
39
|
defineMutators,
|
package/out/zero/src/zero.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zero.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"zero.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -6,6 +6,7 @@ import "../../../shared/src/config.js";
|
|
|
6
6
|
import "../../../replicache/src/hash.js";
|
|
7
7
|
import "../../../replicache/src/btree/node.js";
|
|
8
8
|
import "compare-utf8";
|
|
9
|
+
import "../../../shared/src/bigint-json.js";
|
|
9
10
|
import "../../../shared/src/btree-set.js";
|
|
10
11
|
import "../../../zero-protocol/src/ast.js";
|
|
11
12
|
import "js-xxhash";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sources":["../../../../../zero-client/src/client/context.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {NoIndexDiff} from '../../../replicache/src/btree/node.ts';\nimport type {Hash} from '../../../replicache/src/hash.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport {ErrorKind} from '../../../zero-protocol/src/error-kind.ts';\nimport type {DebugDelegate} from '../../../zql/src/builder/debug-delegate.ts';\nimport type {Input} from '../../../zql/src/ivm/operator.ts';\nimport type {Source, SourceInput} from '../../../zql/src/ivm/source.ts';\nimport {MeasurePushOperator} from '../../../zql/src/query/measure-push-operator.ts';\nimport type {MetricsDelegate} from '../../../zql/src/query/metrics-delegate.ts';\nimport {QueryDelegateBase} from '../../../zql/src/query/query-delegate-base.ts';\nimport type {CommitListener} from '../../../zql/src/query/query-delegate.ts';\nimport type {RunOptions} from '../../../zql/src/query/query.ts';\nimport {type IVMSourceBranch} from './ivm-branch.ts';\nimport type {QueryManager} from './query-manager.ts';\n\nexport type AddQuery = QueryManager['addLegacy'];\nexport type AddCustomQuery = QueryManager['addCustom'];\n\nexport type UpdateQuery = QueryManager['updateLegacy'];\nexport type UpdateCustomQuery = QueryManager['updateCustom'];\nexport type FlushQueryChanges = QueryManager['flushBatch'];\n\n/**\n * ZeroContext glues together zql and Replicache. It listens to changes in\n * Replicache data and pushes them into IVM and on tells the server about new\n * queries.\n */\nexport class ZeroContext extends QueryDelegateBase {\n readonly #lc: LogContext;\n\n // It is a bummer to have to maintain separate MemorySources here and copy the\n // data in from the Replicache db. But we want the data to be accessible via\n // pipelines *synchronously* and the core Replicache infra is all async. So\n // that needs to be fixed.\n readonly #mainSources: IVMSourceBranch;\n\n readonly addServerQuery: AddQuery;\n readonly addCustomQuery: AddCustomQuery;\n readonly updateServerQuery: UpdateQuery;\n readonly updateCustomQuery: UpdateCustomQuery;\n readonly flushQueryChanges: () => void;\n readonly #batchViewUpdates: (applyViewUpdates: () => void) => void;\n readonly #commitListeners: Set<CommitListener> = new Set();\n\n readonly assertValidRunOptions: (options?: RunOptions) => void;\n\n /**\n * Client-side queries start out as \"unknown\" and are then updated to\n * \"complete\" once the server has sent back the query result.\n */\n readonly defaultQueryComplete = false;\n\n readonly addMetric: MetricsDelegate['addMetric'];\n\n constructor(\n lc: LogContext,\n mainSources: IVMSourceBranch,\n addQuery: AddQuery,\n addCustomQuery: AddCustomQuery,\n updateQuery: UpdateQuery,\n updateCustomQuery: UpdateCustomQuery,\n flushQueryChanges: () => void,\n batchViewUpdates: (applyViewUpdates: () => void) => void,\n addMetric: MetricsDelegate['addMetric'],\n assertValidRunOptions: (options?: RunOptions) => void,\n ) {\n super();\n this.#lc = lc;\n this.#mainSources = mainSources;\n this.addServerQuery = addQuery;\n this.updateServerQuery = updateQuery;\n this.updateCustomQuery = updateCustomQuery;\n this.#batchViewUpdates = batchViewUpdates;\n this.assertValidRunOptions = assertValidRunOptions;\n this.addCustomQuery = addCustomQuery;\n this.flushQueryChanges = flushQueryChanges;\n this.addMetric = addMetric;\n }\n\n applyFiltersAnyway?: boolean | undefined;\n\n debug?: DebugDelegate | undefined;\n\n getSource(name: string): Source | undefined {\n return this.#mainSources.getSource(name);\n }\n\n mapAst(ast: AST): AST {\n return ast;\n }\n\n override decorateSourceInput(input: SourceInput, queryID: string): Input {\n return new MeasurePushOperator(input, queryID, this, 'query-update-client');\n }\n\n onTransactionCommit(cb: CommitListener): () => void {\n this.#commitListeners.add(cb);\n return () => {\n this.#commitListeners.delete(cb);\n };\n }\n\n override batchViewUpdates<T>(applyViewUpdates: () => T) {\n let result: T | undefined;\n let viewChangesPerformed = false;\n this.#batchViewUpdates(() => {\n result = applyViewUpdates();\n viewChangesPerformed = true;\n });\n assert(\n viewChangesPerformed,\n 'batchViewUpdates must call applyViewUpdates synchronously.',\n );\n return result as T;\n }\n\n processChanges(\n expectedHead: Hash | undefined,\n newHead: Hash,\n changes: NoIndexDiff,\n ) {\n this.batchViewUpdates(() => {\n try {\n this.#mainSources.advance(expectedHead, newHead, changes);\n } finally {\n this.#endTransaction();\n }\n });\n }\n\n #endTransaction() {\n for (const listener of this.#commitListeners) {\n try {\n listener();\n } catch (e) {\n // We should not fatal the inner-workings of Zero due to the user's application\n // code throwing an error.\n // Hence we wrap notifications in a try-catch block.\n this.#lc.error?.(\n ErrorKind.Internal,\n 'Failed notifying a commit listener of IVM updates',\n e,\n );\n }\n }\n }\n}\n"],"names":["ErrorKind.Internal"],"mappings":"
|
|
1
|
+
{"version":3,"file":"context.js","sources":["../../../../../zero-client/src/client/context.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport type {NoIndexDiff} from '../../../replicache/src/btree/node.ts';\nimport type {Hash} from '../../../replicache/src/hash.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport {ErrorKind} from '../../../zero-protocol/src/error-kind.ts';\nimport type {DebugDelegate} from '../../../zql/src/builder/debug-delegate.ts';\nimport type {Input} from '../../../zql/src/ivm/operator.ts';\nimport type {Source, SourceInput} from '../../../zql/src/ivm/source.ts';\nimport {MeasurePushOperator} from '../../../zql/src/query/measure-push-operator.ts';\nimport type {MetricsDelegate} from '../../../zql/src/query/metrics-delegate.ts';\nimport {QueryDelegateBase} from '../../../zql/src/query/query-delegate-base.ts';\nimport type {CommitListener} from '../../../zql/src/query/query-delegate.ts';\nimport type {RunOptions} from '../../../zql/src/query/query.ts';\nimport {type IVMSourceBranch} from './ivm-branch.ts';\nimport type {QueryManager} from './query-manager.ts';\n\nexport type AddQuery = QueryManager['addLegacy'];\nexport type AddCustomQuery = QueryManager['addCustom'];\n\nexport type UpdateQuery = QueryManager['updateLegacy'];\nexport type UpdateCustomQuery = QueryManager['updateCustom'];\nexport type FlushQueryChanges = QueryManager['flushBatch'];\n\n/**\n * ZeroContext glues together zql and Replicache. It listens to changes in\n * Replicache data and pushes them into IVM and on tells the server about new\n * queries.\n */\nexport class ZeroContext extends QueryDelegateBase {\n readonly #lc: LogContext;\n\n // It is a bummer to have to maintain separate MemorySources here and copy the\n // data in from the Replicache db. But we want the data to be accessible via\n // pipelines *synchronously* and the core Replicache infra is all async. So\n // that needs to be fixed.\n readonly #mainSources: IVMSourceBranch;\n\n readonly addServerQuery: AddQuery;\n readonly addCustomQuery: AddCustomQuery;\n readonly updateServerQuery: UpdateQuery;\n readonly updateCustomQuery: UpdateCustomQuery;\n readonly flushQueryChanges: () => void;\n readonly #batchViewUpdates: (applyViewUpdates: () => void) => void;\n readonly #commitListeners: Set<CommitListener> = new Set();\n\n readonly assertValidRunOptions: (options?: RunOptions) => void;\n\n /**\n * Client-side queries start out as \"unknown\" and are then updated to\n * \"complete\" once the server has sent back the query result.\n */\n readonly defaultQueryComplete = false;\n\n readonly addMetric: MetricsDelegate['addMetric'];\n\n constructor(\n lc: LogContext,\n mainSources: IVMSourceBranch,\n addQuery: AddQuery,\n addCustomQuery: AddCustomQuery,\n updateQuery: UpdateQuery,\n updateCustomQuery: UpdateCustomQuery,\n flushQueryChanges: () => void,\n batchViewUpdates: (applyViewUpdates: () => void) => void,\n addMetric: MetricsDelegate['addMetric'],\n assertValidRunOptions: (options?: RunOptions) => void,\n ) {\n super();\n this.#lc = lc;\n this.#mainSources = mainSources;\n this.addServerQuery = addQuery;\n this.updateServerQuery = updateQuery;\n this.updateCustomQuery = updateCustomQuery;\n this.#batchViewUpdates = batchViewUpdates;\n this.assertValidRunOptions = assertValidRunOptions;\n this.addCustomQuery = addCustomQuery;\n this.flushQueryChanges = flushQueryChanges;\n this.addMetric = addMetric;\n }\n\n applyFiltersAnyway?: boolean | undefined;\n\n debug?: DebugDelegate | undefined;\n\n getSource(name: string): Source | undefined {\n return this.#mainSources.getSource(name);\n }\n\n mapAst(ast: AST): AST {\n return ast;\n }\n\n override decorateSourceInput(input: SourceInput, queryID: string): Input {\n return new MeasurePushOperator(input, queryID, this, 'query-update-client');\n }\n\n onTransactionCommit(cb: CommitListener): () => void {\n this.#commitListeners.add(cb);\n return () => {\n this.#commitListeners.delete(cb);\n };\n }\n\n override batchViewUpdates<T>(applyViewUpdates: () => T) {\n let result: T | undefined;\n let viewChangesPerformed = false;\n this.#batchViewUpdates(() => {\n result = applyViewUpdates();\n viewChangesPerformed = true;\n });\n assert(\n viewChangesPerformed,\n 'batchViewUpdates must call applyViewUpdates synchronously.',\n );\n return result as T;\n }\n\n processChanges(\n expectedHead: Hash | undefined,\n newHead: Hash,\n changes: NoIndexDiff,\n ) {\n this.batchViewUpdates(() => {\n try {\n this.#mainSources.advance(expectedHead, newHead, changes);\n } finally {\n this.#endTransaction();\n }\n });\n }\n\n #endTransaction() {\n for (const listener of this.#commitListeners) {\n try {\n listener();\n } catch (e) {\n // We should not fatal the inner-workings of Zero due to the user's application\n // code throwing an error.\n // Hence we wrap notifications in a try-catch block.\n this.#lc.error?.(\n ErrorKind.Internal,\n 'Failed notifying a commit listener of IVM updates',\n e,\n );\n }\n }\n }\n}\n"],"names":["ErrorKind.Internal"],"mappings":";;;;;;;;;;;;;;AA6BO,MAAM,oBAAoB,kBAAkB;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,uCAA4C,IAAA;AAAA,EAE5C;AAAA;AAAA;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;AAAAA,UACA;AAAA,UACA;AAAA,QAAA;AAAA,MAEJ;AAAA,IACF;AAAA,EACF;AACF;"}
|
|
@@ -3,9 +3,9 @@ import type { Schema } from '../../../zero-types/src/schema.ts';
|
|
|
3
3
|
import type { IVMSourceBranch } from './ivm-branch.ts';
|
|
4
4
|
import type { WriteTransaction } from './replicache-types.ts';
|
|
5
5
|
export type { TableMutator } from '../../../zql/src/mutate/crud.ts';
|
|
6
|
-
export declare function insert(tx: WriteTransaction, arg: InsertOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
7
|
-
export declare function upsert(tx: WriteTransaction, arg: InsertOp | UpsertOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
8
|
-
export declare function update(tx: WriteTransaction, arg: UpdateOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
9
|
-
declare function deleteImpl(tx: WriteTransaction, arg: DeleteOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined): Promise<void>;
|
|
6
|
+
export declare function insert(tx: WriteTransaction, arg: InsertOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined, mutationName: string): Promise<void>;
|
|
7
|
+
export declare function upsert(tx: WriteTransaction, arg: InsertOp | UpsertOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined, mutationName: string): Promise<void>;
|
|
8
|
+
export declare function update(tx: WriteTransaction, arg: UpdateOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined, mutationName: string): Promise<void>;
|
|
9
|
+
declare function deleteImpl(tx: WriteTransaction, arg: DeleteOp, schema: Schema, ivmBranch: IVMSourceBranch | undefined, mutationName: string): Promise<void>;
|
|
10
10
|
export { deleteImpl as delete };
|
|
11
11
|
//# sourceMappingURL=crud-impl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crud-impl.d.ts","sourceRoot":"","sources":["../../../../../zero-client/src/client/crud-impl.ts"],"names":[],"mappings":"AAGA,OAAO,E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Y,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC,CAWf;AAED,wBAAsB,MAAM,CAC1B,EAAE,EAAE,gBAAgB,EACpB,GAAG,EAAE,QAAQ,EACb,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,eAAe,GAAG,SAAS,EACtC,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC,CA8Bf;AAED,iBAAe,UAAU,CACvB,EAAE,EAAE,gBAAgB,EACpB,GAAG,EAAE,QAAQ,EACb,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,eAAe,GAAG,SAAS,EACtC,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC,CAsBf;AAED,OAAO,EAAC,UAAU,IAAI,MAAM,EAAC,CAAC"}
|
|
@@ -11,7 +11,7 @@ function defaultOptionalFieldsToNull(schema, value) {
|
|
|
11
11
|
}
|
|
12
12
|
return rv;
|
|
13
13
|
}
|
|
14
|
-
async function insert(tx, arg, schema, ivmBranch) {
|
|
14
|
+
async function insert(tx, arg, schema, ivmBranch, mutationName) {
|
|
15
15
|
const key = toPrimaryKeyString(
|
|
16
16
|
arg.tableName,
|
|
17
17
|
schema.tables[arg.tableName].primaryKey,
|
|
@@ -25,27 +25,30 @@ async function insert(tx, arg, schema, ivmBranch) {
|
|
|
25
25
|
await tx.set(key, val);
|
|
26
26
|
if (ivmBranch) {
|
|
27
27
|
consume(
|
|
28
|
-
must(ivmBranch.getSource(arg.tableName)).push(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
must(ivmBranch.getSource(arg.tableName)).push(
|
|
29
|
+
{
|
|
30
|
+
type: "add",
|
|
31
|
+
row: arg.value
|
|
32
|
+
},
|
|
33
|
+
{ reason: { type: "mutation", name: mutationName } }
|
|
34
|
+
)
|
|
32
35
|
);
|
|
33
36
|
}
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
|
-
async function upsert(tx, arg, schema, ivmBranch) {
|
|
39
|
+
async function upsert(tx, arg, schema, ivmBranch, mutationName) {
|
|
37
40
|
const key = toPrimaryKeyString(
|
|
38
41
|
arg.tableName,
|
|
39
42
|
schema.tables[arg.tableName].primaryKey,
|
|
40
43
|
arg.value
|
|
41
44
|
);
|
|
42
45
|
if (await tx.has(key)) {
|
|
43
|
-
await update(tx, { ...arg }, schema, ivmBranch);
|
|
46
|
+
await update(tx, { ...arg }, schema, ivmBranch, mutationName);
|
|
44
47
|
} else {
|
|
45
|
-
await insert(tx, { ...arg }, schema, ivmBranch);
|
|
48
|
+
await insert(tx, { ...arg }, schema, ivmBranch, mutationName);
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
|
-
async function update(tx, arg, schema, ivmBranch) {
|
|
51
|
+
async function update(tx, arg, schema, ivmBranch, mutationName) {
|
|
49
52
|
const key = toPrimaryKeyString(
|
|
50
53
|
arg.tableName,
|
|
51
54
|
schema.tables[arg.tableName].primaryKey,
|
|
@@ -55,25 +58,28 @@ async function update(tx, arg, schema, ivmBranch) {
|
|
|
55
58
|
if (prev === void 0) {
|
|
56
59
|
return;
|
|
57
60
|
}
|
|
58
|
-
const
|
|
61
|
+
const updateVal = arg.value;
|
|
59
62
|
const next = { ...prev };
|
|
60
|
-
for (const k in
|
|
61
|
-
if (
|
|
62
|
-
next[k] =
|
|
63
|
+
for (const k in updateVal) {
|
|
64
|
+
if (updateVal[k] !== void 0) {
|
|
65
|
+
next[k] = updateVal[k];
|
|
63
66
|
}
|
|
64
67
|
}
|
|
65
68
|
await tx.set(key, next);
|
|
66
69
|
if (ivmBranch) {
|
|
67
70
|
consume(
|
|
68
|
-
must(ivmBranch.getSource(arg.tableName)).push(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
must(ivmBranch.getSource(arg.tableName)).push(
|
|
72
|
+
{
|
|
73
|
+
type: "edit",
|
|
74
|
+
oldRow: prev,
|
|
75
|
+
row: next
|
|
76
|
+
},
|
|
77
|
+
{ reason: { type: "mutation", name: mutationName } }
|
|
78
|
+
)
|
|
73
79
|
);
|
|
74
80
|
}
|
|
75
81
|
}
|
|
76
|
-
async function deleteImpl(tx, arg, schema, ivmBranch) {
|
|
82
|
+
async function deleteImpl(tx, arg, schema, ivmBranch, mutationName) {
|
|
77
83
|
const key = toPrimaryKeyString(
|
|
78
84
|
arg.tableName,
|
|
79
85
|
schema.tables[arg.tableName].primaryKey,
|
|
@@ -86,10 +92,13 @@ async function deleteImpl(tx, arg, schema, ivmBranch) {
|
|
|
86
92
|
await tx.del(key);
|
|
87
93
|
if (ivmBranch) {
|
|
88
94
|
consume(
|
|
89
|
-
must(ivmBranch.getSource(arg.tableName)).push(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
must(ivmBranch.getSource(arg.tableName)).push(
|
|
96
|
+
{
|
|
97
|
+
type: "remove",
|
|
98
|
+
row: prev
|
|
99
|
+
},
|
|
100
|
+
{ reason: { type: "mutation", name: mutationName } }
|
|
101
|
+
)
|
|
93
102
|
);
|
|
94
103
|
}
|
|
95
104
|
}
|
|
@@ -1 +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
|
|
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 mutationName: string,\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 {\n type: 'add',\n row: arg.value,\n },\n {reason: {type: 'mutation', name: mutationName}},\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 mutationName: string,\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, mutationName);\n } else {\n await insert(tx, {...arg, op: 'insert'}, schema, ivmBranch, mutationName);\n }\n}\n\nexport async function update(\n tx: WriteTransaction,\n arg: UpdateOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n mutationName: string,\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 updateVal = arg.value;\n const next = {...(prev as ReadonlyJSONObject)};\n for (const k in updateVal) {\n if (updateVal[k] !== undefined) {\n next[k] = updateVal[k];\n }\n }\n await tx.set(key, next);\n if (ivmBranch) {\n consume(\n must(ivmBranch.getSource(arg.tableName)).push(\n {\n type: 'edit',\n oldRow: prev as Row,\n row: next,\n },\n {reason: {type: 'mutation', name: mutationName}},\n ),\n );\n }\n}\n\nasync function deleteImpl(\n tx: WriteTransaction,\n arg: DeleteOp,\n schema: Schema,\n ivmBranch: IVMSourceBranch | undefined,\n mutationName: string,\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 {\n type: 'remove',\n row: prev as Row,\n },\n {reason: {type: 'mutation', name: mutationName}},\n ),\n );\n }\n}\n\nexport {deleteImpl as delete};\n"],"names":[],"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,WACA,cACe;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;AAAA,UACvC;AAAA,YACE,MAAM;AAAA,YACN,KAAK,IAAI;AAAA,UAAA;AAAA,UAEX,EAAC,QAAQ,EAAC,MAAM,YAAY,MAAM,eAAY;AAAA,QAAC;AAAA,MACjD;AAAA,IAEJ;AAAA,EACF;AACF;AAEA,eAAsB,OACpB,IACA,KACA,QACA,WACA,cACe;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,WAAW,YAAY;AAAA,EAC1E,OAAO;AACL,UAAM,OAAO,IAAI,EAAC,GAAG,IAAiB,GAAG,QA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
@@ -34,7 +34,7 @@ export type WithCRUD<MD extends MutatorDefs> = MD & {
|
|
|
34
34
|
};
|
|
35
35
|
export type CRUDMutate = (crudArg: CRUDMutationArg) => Promise<void>;
|
|
36
36
|
export type CRUDMutator = (tx: WriteTransaction, crudArg: CRUDMutationArg) => Promise<void>;
|
|
37
|
-
export declare function makeCRUDExecutor(tx: WriteTransaction, schema: Schema, ivmBranch: IVMSourceBranch | undefined): CRUDExecutor;
|
|
37
|
+
export declare function makeCRUDExecutor(tx: WriteTransaction, schema: Schema, ivmBranch: IVMSourceBranch | undefined, mutationName: string): CRUDExecutor;
|
|
38
38
|
export declare function makeCRUDMutator(schema: Schema): CRUDMutator;
|
|
39
39
|
export {};
|
|
40
40
|
//# sourceMappingURL=crud.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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,EACV,YAAY,EAGZ,YAAY,EAGb,MAAM,iCAAiC,CAAC;AAEzC,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,QAA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,CAAC,KAAK,CAAC,CAAC,SAAS,MAAM,EACxD,MAAM,EAAE,CAAC,EACT,SAAS,EAAE,cAAc,GACxB,YAAY,CAAC,CAAC,CAAC,CAoBjB;AAED,wBAAgB,sBAAsB,CAAC,OAAO,SAAS,MAAM,EAC3D,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,cAAc,GACxB,IAAI,CASN;AAmDD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,WAAW,EACvD,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EAAE,GACZ,YAAY,CAAC,CAAC,CAAC,CA4CjB;AAED,MAAM,MAAM,QAAQ,CAAC,EAAE,SAAS,WAAW,IAAI,EAAE,GAAG;IAClD,CAAC,kBAAkB,CAAC,EAAE,WAAW,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAErE,MAAM,MAAM,WAAW,GAAG,CACxB,EAAE,EAAE,gBAAgB,EACpB,OAAO,EAAE,eAAe,KACrB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,wBAAgB,gBAAgB,CAC9B,EAAE,EAAE,gBAAgB,EACpB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,eAAe,GAAG,SAAS,EACtC,YAAY,EAAE,MAAM,GACnB,YAAY,CAYd;AAMD,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAe3D"}
|
|
@@ -113,7 +113,7 @@ function makeBatchCRUDMutate(tableName, schema, ops) {
|
|
|
113
113
|
}
|
|
114
114
|
};
|
|
115
115
|
}
|
|
116
|
-
function makeCRUDExecutor(tx, schema, ivmBranch) {
|
|
116
|
+
function makeCRUDExecutor(tx, schema, ivmBranch, mutationName) {
|
|
117
117
|
return (tableName, kind, value) => {
|
|
118
118
|
const { primaryKey } = schema.tables[tableName];
|
|
119
119
|
return crudImpl[kind](
|
|
@@ -121,13 +121,19 @@ function makeCRUDExecutor(tx, schema, ivmBranch) {
|
|
|
121
121
|
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
|
122
122
|
{ op: kind, tableName, primaryKey, value },
|
|
123
123
|
schema,
|
|
124
|
-
ivmBranch
|
|
124
|
+
ivmBranch,
|
|
125
|
+
mutationName
|
|
125
126
|
);
|
|
126
127
|
};
|
|
127
128
|
}
|
|
128
129
|
function makeCRUDMutator(schema) {
|
|
129
130
|
return async (tx, crudArg) => {
|
|
130
|
-
const executor = makeCRUDExecutor(
|
|
131
|
+
const executor = makeCRUDExecutor(
|
|
132
|
+
tx,
|
|
133
|
+
schema,
|
|
134
|
+
void 0,
|
|
135
|
+
CRUD_MUTATION_NAME
|
|
136
|
+
);
|
|
131
137
|
for (const op of crudArg.ops) {
|
|
132
138
|
await executor(op.tableName, op.op, op.value);
|
|
133
139
|
}
|
|
@@ -1 +1 @@
|
|
|
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 {\n CRUDExecutor,\n DeleteID,\n InsertValue,\n TableMutator,\n UpdateValue,\n UpsertValue,\n} from '../../../zql/src/mutate/crud.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.mutateBatch function 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 makeCRUDMutateBatch<const S extends Schema>(\n schema: S,\n repMutate: ZeroCRUDMutate,\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 return mutateBatch as BatchMutator<S>;\n}\n\nexport function addTableCRUDProperties<TSchema extends Schema>(\n schema: TSchema,\n mutate: object,\n repMutate: ZeroCRUDMutate,\n): void {\n const {[CRUD_MUTATION_NAME]: zeroCRUD} = repMutate;\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}\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
|
|
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 {\n CRUDExecutor,\n DeleteID,\n InsertValue,\n TableMutator,\n UpdateValue,\n UpsertValue,\n} from '../../../zql/src/mutate/crud.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.mutateBatch function 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 makeCRUDMutateBatch<const S extends Schema>(\n schema: S,\n repMutate: ZeroCRUDMutate,\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 return mutateBatch as BatchMutator<S>;\n}\n\nexport function addTableCRUDProperties<TSchema extends Schema>(\n schema: TSchema,\n mutate: object,\n repMutate: ZeroCRUDMutate,\n): void {\n const {[CRUD_MUTATION_NAME]: zeroCRUD} = repMutate;\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}\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 mutationName: string,\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 mutationName,\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(\n tx,\n schema,\n undefined,\n CRUD_MUTATION_NAME,\n );\n for (const op of crudArg.ops) {\n await executor(op.tableName, op.op, op.value);\n }\n };\n}\n"],"names":[],"mappings":";;;AAkDO,SAAS,oBACd,QACA,WACiB;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,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,SAAO;AACT;AAEO,SAAS,uBACd,QACA,QACA,WACM;AACN,QAAM,EAAC,CAAC,kBAAkB,GAAG,aAAY;AACzC,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;AACF;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,KAAe;AAA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AAAA,EAAA;AAEJ;AAaO,SAAS,iBACd,IACA,QACA,WACA,cACc;AACd,SAAO,CAAC,WAAW,MAAM,UAAU;AACjC,UAAM,EAAC,WAAA,IAAc,OAAO,OAAO,SAAS;AAC5C,WAAO,SAAS,IAAI;AAAA,MAClB;AAAA;AAAA,MAEA,EAAC,IAAI,MAAM,WAAW,YAAY,MAAA;AAAA,MAClC;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AACF;AAMO,SAAS,gBAAgB,QAA6B;AAC3D,SAAO,OACL,IACA,YACkB;AAClB,UAAM,WAAW;AAAA,MACf;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAEF,eAAW,MAAM,QAAQ,KAAK;AAC5B,YAAM,SAAS,GAAG,WAAW,GAAG,IAAI,GAAG,KAAK;AAAA,IAC9C;AAAA,EACF;AACF;"}
|
|
@@ -60,7 +60,7 @@ export declare class TransactionImpl<TSchema extends Schema = DefaultSchema> imp
|
|
|
60
60
|
* @deprecated Use {@linkcode createBuilder} with `tx.run(zql.table.where(...))` instead.
|
|
61
61
|
*/
|
|
62
62
|
readonly query: ConditionalSchemaQuery<TSchema>;
|
|
63
|
-
constructor(lc: LogContext, repTx: WriteTransaction, schema: TSchema);
|
|
63
|
+
constructor(lc: LogContext, repTx: WriteTransaction, schema: TSchema, mutationName: string);
|
|
64
64
|
get clientID(): ClientID;
|
|
65
65
|
get mutationID(): number;
|
|
66
66
|
get reason(): 'optimistic' | 'rebase';
|
|
@@ -68,5 +68,5 @@ export declare class TransactionImpl<TSchema extends Schema = DefaultSchema> imp
|
|
|
68
68
|
run<TTable extends keyof TSchema['tables'] & string, TReturn>(query: Query<TTable, TSchema, TReturn>, options?: RunOptions): Promise<HumanReadable<TReturn>>;
|
|
69
69
|
}
|
|
70
70
|
export declare function getZeroTxData(repTx: WriteTransaction): ZeroTxData;
|
|
71
|
-
export declare function makeReplicacheMutator<S extends Schema, TWrappedTransaction, Context>(lc: LogContext, mutator: CustomMutatorImpl<S, TWrappedTransaction>, schema: S, context: Context): (repTx: WriteTransaction, args: ReadonlyJSONValue) => Promise<void>;
|
|
71
|
+
export declare function makeReplicacheMutator<S extends Schema, TWrappedTransaction, Context>(lc: LogContext, mutator: CustomMutatorImpl<S, TWrappedTransaction>, schema: S, context: Context, mutationName: string): (repTx: WriteTransaction, args: ReadonlyJSONValue) => Promise<void>;
|
|
72
72
|
//# sourceMappingURL=custom.d.ts.map
|
|
@@ -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;AAGnE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,0CAA0C,CAAC;AAC5E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EACV,iBAAiB,EACjB,WAAW,EACZ,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,KAAK,EACV,KAAK,UAAU,EAChB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAC,sBAAsB,EAAC,MAAM,wCAAwC,CAAC;AACnF,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,0BAA0B,CAAC;AAIvD,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAE5D;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAE9B,CAAC,cAAc,EAAE,MAAM,GAAG,iBAAiB,CAAC,GAAG,CAAC,GAAG,iBAAiB,CAAC;CACtE,CAAC;AAEF,M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x, schema) {
|
|
19
|
+
constructor(lc, repTx, schema, mutationName) {
|
|
20
20
|
must(repTx.reason === "initial" || repTx.reason === "rebase");
|
|
21
21
|
const txData = getZeroTxData(repTx);
|
|
22
22
|
const ivmBranch = txData.ivmSources;
|
|
23
23
|
this.#repTx = repTx;
|
|
24
|
-
const executor = makeCRUDExecutor(repTx, schema, ivmBranch);
|
|
24
|
+
const executor = makeCRUDExecutor(repTx, schema, ivmBranch, mutationName);
|
|
25
25
|
this.mutate = makeTransactionMutate(schema, executor);
|
|
26
26
|
const zeroContext = newZeroContext(
|
|
27
27
|
lc,
|
|
@@ -53,9 +53,9 @@ function getZeroTxData(repTx) {
|
|
|
53
53
|
);
|
|
54
54
|
return txData;
|
|
55
55
|
}
|
|
56
|
-
function makeReplicacheMutator(lc, mutator, schema, context) {
|
|
56
|
+
function makeReplicacheMutator(lc, mutator, schema, context, mutationName) {
|
|
57
57
|
return async (repTx, args) => {
|
|
58
|
-
const tx = new TransactionImpl(lc, repTx, schema);
|
|
58
|
+
const tx = new TransactionImpl(lc, repTx, schema, mutationName);
|
|
59
59
|
await mutator(tx, args, context);
|
|
60
60
|
};
|
|
61
61
|
}
|
|
@@ -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 {DefaultSchema} from '../../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport {\n makeTransactionMutate,\n type TransactionMutate,\n} from '../../../zql/src/mutate/crud.ts';\nimport type {\n ClientTransaction,\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 {ConditionalSchemaQuery} 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: TransactionMutate<TSchema>;\n /**\n * @deprecated Use {@linkcode createBuilder} with `tx.run(zql.table.where(...))` instead.\n */\n readonly query: ConditionalSchemaQuery<TSchema>;\n\n readonly #repTx: WriteTransaction;\n readonly #zeroContext: ZeroContext;\n\n constructor(lc: LogContext
|
|
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 {\n makeTransactionMutate,\n type TransactionMutate,\n} from '../../../zql/src/mutate/crud.ts';\nimport type {\n ClientTransaction,\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 {ConditionalSchemaQuery} 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: TransactionMutate<TSchema>;\n /**\n * @deprecated Use {@linkcode createBuilder} with `tx.run(zql.table.where(...))` instead.\n */\n readonly query: ConditionalSchemaQuery<TSchema>;\n\n readonly #repTx: WriteTransaction;\n readonly #zeroContext: ZeroContext;\n\n constructor(\n lc: LogContext,\n repTx: WriteTransaction,\n schema: TSchema,\n mutationName: string,\n ) {\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, mutationName);\n this.mutate = makeTransactionMutate(schema, executor);\n\n const zeroContext = newZeroContext(\n lc,\n txData.ivmSources as IVMSourceBranch,\n );\n\n this.query = createRunnableBuilder(zeroContext, schema);\n\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 mutationName: string,\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, mutationName);\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":";;;;;;;;;AAoHO,MAAM,gBAEb;AAAA,EACW,WAAW;AAAA,EACX;AAAA;AAAA;AAAA;AAAA,EAIA;AAAA,EAEA;AAAA,EACA;AAAA,EAET,YACE,IACA,OACA,QACA,cACA;AACA,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,WAAW,YAAY;AACxE,SAAK,SAAS,sBAAsB,QAAQ,QAAQ;AAEpD,UAAM,cAAc;AAAA,MAClB;AAAA,MACA,OAAO;AAAA,IAAA;AAGT,SAAK,QAAQ,sBAAsB,aAAa,MAAM;AAEtD,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,SACA,cACqE;AACrE,SAAO,OACL,OACA,SACkB;AAClB,UAAM,KAAK,IAAI,gBAAgB,IAAI,OAAO,QAAQ,YAAY;AAC9D,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;"}
|
|
@@ -31,7 +31,7 @@ export declare class IVMSourceBranch {
|
|
|
31
31
|
* Mutates the current branch, advancing it to the new head
|
|
32
32
|
* by applying the given diffs.
|
|
33
33
|
*/
|
|
34
|
-
advance(expectedHead: Hash | undefined, newHead: Hash, diffs: NoIndexDiff): void;
|
|
34
|
+
advance(expectedHead: Hash | undefined, newHead: Hash, diffs: NoIndexDiff, reason?: ApplyDiffsReason): void;
|
|
35
35
|
/**
|
|
36
36
|
* Fork the branch and patch it up to match the desired head.
|
|
37
37
|
*/
|
|
@@ -47,4 +47,6 @@ export declare class IVMSourceBranch {
|
|
|
47
47
|
fork(): IVMSourceBranch;
|
|
48
48
|
}
|
|
49
49
|
export declare function initFromStore(branch: IVMSourceBranch, hash: Hash, store: Store): Promise<void>;
|
|
50
|
+
type ApplyDiffsReason = 'poke' | 'rebase' | 'init' | 'refresh';
|
|
51
|
+
export {};
|
|
50
52
|
//# sourceMappingURL=ivm-branch.d.ts.map
|
|
@@ -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;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,SAA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M,EAAE,eAAe,EACvB,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,KAAK,iBAkBb;AAoDD,KAAK,gBAAgB,GAAG,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAC"}
|
|
@@ -36,12 +36,12 @@ class IVMSourceBranch {
|
|
|
36
36
|
* Mutates the current branch, advancing it to the new head
|
|
37
37
|
* by applying the given diffs.
|
|
38
38
|
*/
|
|
39
|
-
advance(expectedHead, newHead, diffs) {
|
|
39
|
+
advance(expectedHead, newHead, diffs, reason = "poke") {
|
|
40
40
|
assert(
|
|
41
41
|
this.hash === expectedHead,
|
|
42
42
|
() => `Expected head must match the main head. Got: ${this.hash}, expected: ${expectedHead}`
|
|
43
43
|
);
|
|
44
|
-
applyDiffs(diffs, this);
|
|
44
|
+
applyDiffs(diffs, this, reason);
|
|
45
45
|
this.hash = newHead;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
@@ -87,7 +87,7 @@ async function patchBranch(desiredHead, store, fork, readOptions) {
|
|
|
87
87
|
if (!diffs) {
|
|
88
88
|
return;
|
|
89
89
|
}
|
|
90
|
-
applyDiffs(diffs, fork);
|
|
90
|
+
applyDiffs(diffs, fork, "rebase");
|
|
91
91
|
}
|
|
92
92
|
async function computeDiffs(startHash, endHash, store, readOptions) {
|
|
93
93
|
const readFn = (dagRead) => diff(
|
|
@@ -112,7 +112,7 @@ async function computeDiffs(startHash, endHash, store, readOptions) {
|
|
|
112
112
|
}
|
|
113
113
|
return diffs.get("");
|
|
114
114
|
}
|
|
115
|
-
function applyDiffs(diffs, branch) {
|
|
115
|
+
function applyDiffs(diffs, branch, reason) {
|
|
116
116
|
for (let i = diffBinarySearch(diffs, ENTITIES_KEY_PREFIX, (diff2) => diff2.key); i < diffs.length; i++) {
|
|
117
117
|
const diff2 = diffs[i];
|
|
118
118
|
const { key } = diff2;
|
|
@@ -121,30 +121,40 @@ function applyDiffs(diffs, branch) {
|
|
|
121
121
|
}
|
|
122
122
|
const name = sourceNameFromKey(key);
|
|
123
123
|
const source = must(branch.getSource(name));
|
|
124
|
+
const options = { reason };
|
|
124
125
|
switch (diff2.op) {
|
|
125
126
|
case "del":
|
|
126
127
|
consume(
|
|
127
|
-
source.push(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
128
|
+
source.push(
|
|
129
|
+
{
|
|
130
|
+
type: "remove",
|
|
131
|
+
row: diff2.oldValue
|
|
132
|
+
},
|
|
133
|
+
options
|
|
134
|
+
)
|
|
131
135
|
);
|
|
132
136
|
break;
|
|
133
137
|
case "add":
|
|
134
138
|
consume(
|
|
135
|
-
source.push(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
source.push(
|
|
140
|
+
{
|
|
141
|
+
type: "add",
|
|
142
|
+
row: diff2.newValue
|
|
143
|
+
},
|
|
144
|
+
options
|
|
145
|
+
)
|
|
139
146
|
);
|
|
140
147
|
break;
|
|
141
148
|
case "change":
|
|
142
149
|
consume(
|
|
143
|
-
source.push(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
150
|
+
source.push(
|
|
151
|
+
{
|
|
152
|
+
type: "edit",
|
|
153
|
+
row: diff2.newValue,
|
|
154
|
+
oldRow: diff2.oldValue
|
|
155
|
+
},
|
|
156
|
+
options
|
|
157
|
+
)
|
|
148
158
|
);
|
|
149
159
|
break;
|
|
150
160
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ivm-branch.js","sources":["../../../../../zero-client/src/client/ivm-branch.ts"],"sourcesContent":["import type {\n InternalDiff,\n InternalDiffOperation,\n NoIndexDiff,\n} from '../../../replicache/src/btree/node.ts';\nimport type {LazyStore} from '../../../replicache/src/dag/lazy-store.ts';\nimport {type Read, type Store} from '../../../replicache/src/dag/store.ts';\nimport {readFromHash} from '../../../replicache/src/db/read.ts';\nimport * as FormatVersion from '../../../replicache/src/format-version-enum.ts';\nimport type {Hash} from '../../../replicache/src/hash.ts';\nimport type {ZeroReadOptions} from '../../../replicache/src/replicache-options.ts';\nimport {diffBinarySearch} from '../../../replicache/src/subscriptions.ts';\nimport type {DiffsMap} from '../../../replicache/src/sync/diff.ts';\nimport {diff} from '../../../replicache/src/sync/diff.ts';\nimport {using, withRead} from '../../../replicache/src/with-transactions.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {wrapIterable} from '../../../shared/src/iterables.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {TableSchema} from '../../../zero-schema/src/table-schema.ts';\nimport {MemorySource} from '../../../zql/src/ivm/memory-source.ts';\nimport {consume} from '../../../zql/src/ivm/stream.ts';\nimport {ENTITIES_KEY_PREFIX, sourceNameFromKey} from './keys.ts';\n\n/**\n * Replicache needs to rebase mutations onto different\n * commits of it's b-tree. These mutations can have reads\n * in them and those reads must be run against the IVM sources.\n *\n * To ensure the reads get the correct state, the IVM\n * sources need to reflect the state of the commit\n * being rebased onto. `IVMSourceBranch` allows us to:\n * 1. fork the IVM sources\n * 2. patch them up to match the desired head\n * 3. run the reads against the forked sources\n *\n * (2) is expected to be a cheap operation as there should only\n * ever be a few outstanding diffs to apply given Zero is meant\n * to be run in a connected state.\n */\nexport class IVMSourceBranch {\n readonly #sources: Map<string, MemorySource | undefined>;\n readonly #tables: Record<string, TableSchema>;\n hash: Hash | undefined;\n\n constructor(\n tables: Record<string, TableSchema>,\n hash?: Hash,\n sources: Map<string, MemorySource | undefined> = new Map(),\n ) {\n this.#tables = tables;\n this.#sources = sources;\n this.hash = hash;\n }\n\n getSource(name: string): MemorySource | undefined {\n if (this.#sources.has(name)) {\n return this.#sources.get(name);\n }\n\n const schema = this.#tables[name];\n const source = schema\n ? new MemorySource(name, schema.columns, schema.primaryKey)\n : undefined;\n this.#sources.set(name, source);\n return source;\n }\n\n clear() {\n this.#sources.clear();\n }\n\n /**\n * Mutates the current branch, advancing it to the new head\n * by applying the given diffs.\n */\n advance(expectedHead: Hash | undefined, newHead: Hash, diffs: NoIndexDiff) {\n assert(\n this.hash === expectedHead,\n () =>\n `Expected head must match the main head. Got: ${this.hash}, expected: ${expectedHead}`,\n );\n\n applyDiffs(diffs, this);\n this.hash = newHead;\n }\n\n /**\n * Fork the branch and patch it up to match the desired head.\n */\n async forkToHead(\n store: LazyStore,\n desiredHead: Hash,\n readOptions?: ZeroReadOptions,\n ): Promise<IVMSourceBranch> {\n const fork = this.fork();\n\n if (fork.hash === desiredHead) {\n return fork;\n }\n\n await patchBranch(desiredHead, store, fork, readOptions);\n fork.hash = desiredHead;\n return fork;\n }\n\n /**\n * Creates a new IVMSourceBranch that is a copy of the current one.\n * This is a cheap operation since the b-trees are shared until a write is performed\n * and then only the modified nodes are copied.\n *\n * IVM branches are forked when we need to rebase mutations.\n * The mutations modify the fork rather than original branch.\n */\n fork() {\n return new IVMSourceBranch(\n this.#tables,\n this.hash,\n new Map(\n wrapIterable(this.#sources.entries()).map(([name, source]) => [\n name,\n source?.fork(),\n ]),\n ),\n );\n }\n}\n\nexport async function initFromStore(\n branch: IVMSourceBranch,\n hash: Hash,\n store: Store,\n) {\n const diffs: InternalDiffOperation[] = [];\n await withRead(store, async dagRead => {\n const read = await readFromHash(hash, dagRead, FormatVersion.Latest);\n for await (const entry of read.map.scan(ENTITIES_KEY_PREFIX)) {\n if (!entry[0].startsWith(ENTITIES_KEY_PREFIX)) {\n break;\n }\n diffs.push({\n op: 'add',\n key: entry[0],\n newValue: entry[1],\n });\n }\n });\n\n branch.advance(undefined, hash, diffs);\n}\n\nasync function patchBranch(\n desiredHead: Hash,\n store: LazyStore,\n fork: IVMSourceBranch,\n readOptions: ZeroReadOptions | undefined,\n) {\n const diffs = await computeDiffs(\n must(fork.hash),\n desiredHead,\n store,\n readOptions,\n );\n if (!diffs) {\n return;\n }\n applyDiffs(diffs, fork);\n}\n\nasync function computeDiffs(\n startHash: Hash,\n endHash: Hash,\n store: LazyStore,\n readOptions: ZeroReadOptions | undefined,\n): Promise<InternalDiff | undefined> {\n const readFn = (dagRead: Read) =>\n diff(\n startHash,\n endHash,\n dagRead,\n {\n shouldComputeDiffs: () => true,\n shouldComputeDiffsForIndex(_name) {\n return false;\n },\n },\n FormatVersion.Latest,\n );\n\n let diffs: DiffsMap;\n if (readOptions?.openLazySourceRead) {\n diffs = await using(store.read(readOptions.openLazySourceRead), readFn);\n } else if (readOptions?.openLazyRead) {\n diffs = await readFn(readOptions.openLazyRead);\n } else {\n diffs = await withRead(store, readFn);\n }\n\n return diffs.get('');\n}\n\nfunction applyDiffs(diffs: NoIndexDiff, branch: IVMSourceBranch) {\n for (\n let i = diffBinarySearch(diffs, ENTITIES_KEY_PREFIX, diff => diff.key);\n i < diffs.length;\n i++\n ) {\n const diff = diffs[i];\n const {key} = diff;\n if (!key.startsWith(ENTITIES_KEY_PREFIX)) {\n break;\n }\n const name = sourceNameFromKey(key);\n const source = must(branch.getSource(name));\n switch (diff.op) {\n case 'del':\n consume(\n source.push({\n type: 'remove',\n row: diff.oldValue as Row,\n }),\n );\n break;\n case 'add':\n consume(\n source.push({\n type: 'add',\n row: diff.newValue as Row,\n }),\n );\n break;\n case 'change':\n consume(\n source.push({\n type: 'edit',\n row: diff.newValue as Row,\n oldRow: diff.oldValue as Row,\n }),\n );\n break;\n }\n }\n}\n"],"names":["FormatVersion.Latest","diff"],"mappings":";;;;;;;;;;;;;AAwCO,MAAM,gBAAgB;AAAA,EAClB;AAAA,EACA;AAAA,EACT;AAAA,EAEA,YACE,QACA,MACA,UAAiD,oBAAI,OACrD;AACA,SAAK,UAAU;AACf,SAAK,WAAW;AAChB,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,UAAU,MAAwC;AAChD,QAAI,KAAK,SAAS,IAAI,IAAI,GAAG;AAC3B,aAAO,KAAK,SAAS,IAAI,IAAI;AAAA,IAC/B;AAEA,UAAM,SAAS,KAAK,QAAQ,IAAI;AAChC,UAAM,SAAS,SACX,IAAI,aAAa,MAAM,OAAO,SAAS,OAAO,UAAU,I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AAChC,eAAO;AAAA,MACT;AAAA,IAAA;AAAA,IAEFA;AAAAA,EAAc;AAGlB,MAAI;AACJ,MAAI,aAAa,oBAAoB;AACnC,YAAQ,MAAM,MAAM,MAAM,KAAK,YAAY,kBAAkB,GAAG,MAAM;AAAA,EACxE,WAAW,aAAa,cAAc;AACpC,YAAQ,MAAM,OAAO,YAAY,YAAY;AAAA,EAC/C,OAAO;AACL,YAAQ,MAAM,SAAS,OAAO,MAAM;AAAA,EACtC;AAEA,SAAO,MAAM,IAAI,EAAE;AACrB;AAEA,SAAS,WAAW,OAAoB,QAAyB;AAC/D,WACM,IAAI,iBAAiB,OAAO,qBAAqB,CAAAC,UAAQA,MAAK,GAAG,GACrE,IAAI,MAAM,QACV,KACA;AACA,UAAMA,QAAO,MAAM,CAAC;AACpB,UAAM,EAAC,QAAOA;AACd,QAAI,CAAC,IAAI,WAAW,mBAAmB,GAAG;AACxC;AAAA,IACF;AACA,UAAM,OAAO,kBAAkB,GAAG;AAClC,UAAM,SAAS,KAAK,OAAO,UAAU,IAAI,CAAC;AAC1C,YAAQA,MAAK,IAAA;AAAA,MACX,KAAK;AACH;AAAA,UACE,OAAO,KAAK;AAAA,YACV,MAAM;AAAA,YACN,KAAKA,MAAK;AAAA,UAAA,CACX;AAAA,QAAA;AAEH;AAAA,MACF,KAAK;AACH;AAAA,UACE,OAAO,KAAK;AAAA,YACV,MAAM;AAAA,YACN,KAAKA,MAAK;AAAA,UAAA,CACX;AAAA,QAAA;AAEH;AAAA,MACF,KAAK;AACH;AAAA,UACE,OAAO,KAAK;AAAA,YACV,MAAM;AAAA,YACN,KAAKA,MAAK;AAAA,YACV,QAAQA,MAAK;AAAA,UAAA,CACd;AAAA,QAAA;AAEH;AAAA,IAAA;AAAA,EAEN;AACF;"}
|
|
1
|
+
{"version":3,"file":"ivm-branch.js","sources":["../../../../../zero-client/src/client/ivm-branch.ts"],"sourcesContent":["import type {\n InternalDiff,\n InternalDiffOperation,\n NoIndexDiff,\n} from '../../../replicache/src/btree/node.ts';\nimport type {LazyStore} from '../../../replicache/src/dag/lazy-store.ts';\nimport {type Read, type Store} from '../../../replicache/src/dag/store.ts';\nimport {readFromHash} from '../../../replicache/src/db/read.ts';\nimport * as FormatVersion from '../../../replicache/src/format-version-enum.ts';\nimport type {Hash} from '../../../replicache/src/hash.ts';\nimport type {ZeroReadOptions} from '../../../replicache/src/replicache-options.ts';\nimport {diffBinarySearch} from '../../../replicache/src/subscriptions.ts';\nimport type {DiffsMap} from '../../../replicache/src/sync/diff.ts';\nimport {diff} from '../../../replicache/src/sync/diff.ts';\nimport {using, withRead} from '../../../replicache/src/with-transactions.ts';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {wrapIterable} from '../../../shared/src/iterables.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {TableSchema} from '../../../zero-schema/src/table-schema.ts';\nimport {MemorySource} from '../../../zql/src/ivm/memory-source.ts';\nimport {consume} from '../../../zql/src/ivm/stream.ts';\nimport {ENTITIES_KEY_PREFIX, sourceNameFromKey} from './keys.ts';\n\n/**\n * Replicache needs to rebase mutations onto different\n * commits of it's b-tree. These mutations can have reads\n * in them and those reads must be run against the IVM sources.\n *\n * To ensure the reads get the correct state, the IVM\n * sources need to reflect the state of the commit\n * being rebased onto. `IVMSourceBranch` allows us to:\n * 1. fork the IVM sources\n * 2. patch them up to match the desired head\n * 3. run the reads against the forked sources\n *\n * (2) is expected to be a cheap operation as there should only\n * ever be a few outstanding diffs to apply given Zero is meant\n * to be run in a connected state.\n */\nexport class IVMSourceBranch {\n readonly #sources: Map<string, MemorySource | undefined>;\n readonly #tables: Record<string, TableSchema>;\n hash: Hash | undefined;\n\n constructor(\n tables: Record<string, TableSchema>,\n hash?: Hash,\n sources: Map<string, MemorySource | undefined> = new Map(),\n ) {\n this.#tables = tables;\n this.#sources = sources;\n this.hash = hash;\n }\n\n getSource(name: string): MemorySource | undefined {\n if (this.#sources.has(name)) {\n return this.#sources.get(name);\n }\n\n const schema = this.#tables[name];\n const source = schema\n ? new MemorySource(name, schema.columns, schema.primaryKey)\n : undefined;\n this.#sources.set(name, source);\n return source;\n }\n\n clear() {\n this.#sources.clear();\n }\n\n /**\n * Mutates the current branch, advancing it to the new head\n * by applying the given diffs.\n */\n advance(\n expectedHead: Hash | undefined,\n newHead: Hash,\n diffs: NoIndexDiff,\n reason: ApplyDiffsReason = 'poke',\n ) {\n assert(\n this.hash === expectedHead,\n () =>\n `Expected head must match the main head. Got: ${this.hash}, expected: ${expectedHead}`,\n );\n\n applyDiffs(diffs, this, reason);\n this.hash = newHead;\n }\n\n /**\n * Fork the branch and patch it up to match the desired head.\n */\n async forkToHead(\n store: LazyStore,\n desiredHead: Hash,\n readOptions?: ZeroReadOptions,\n ): Promise<IVMSourceBranch> {\n const fork = this.fork();\n\n if (fork.hash === desiredHead) {\n return fork;\n }\n\n await patchBranch(desiredHead, store, fork, readOptions);\n fork.hash = desiredHead;\n return fork;\n }\n\n /**\n * Creates a new IVMSourceBranch that is a copy of the current one.\n * This is a cheap operation since the b-trees are shared until a write is performed\n * and then only the modified nodes are copied.\n *\n * IVM branches are forked when we need to rebase mutations.\n * The mutations modify the fork rather than original branch.\n */\n fork() {\n return new IVMSourceBranch(\n this.#tables,\n this.hash,\n new Map(\n wrapIterable(this.#sources.entries()).map(([name, source]) => [\n name,\n source?.fork(),\n ]),\n ),\n );\n }\n}\n\nexport async function initFromStore(\n branch: IVMSourceBranch,\n hash: Hash,\n store: Store,\n) {\n const diffs: InternalDiffOperation[] = [];\n await withRead(store, async dagRead => {\n const read = await readFromHash(hash, dagRead, FormatVersion.Latest);\n for await (const entry of read.map.scan(ENTITIES_KEY_PREFIX)) {\n if (!entry[0].startsWith(ENTITIES_KEY_PREFIX)) {\n break;\n }\n diffs.push({\n op: 'add',\n key: entry[0],\n newValue: entry[1],\n });\n }\n });\n\n branch.advance(undefined, hash, diffs, 'init');\n}\n\nasync function patchBranch(\n desiredHead: Hash,\n store: LazyStore,\n fork: IVMSourceBranch,\n readOptions: ZeroReadOptions | undefined,\n) {\n const diffs = await computeDiffs(\n must(fork.hash),\n desiredHead,\n store,\n readOptions,\n );\n if (!diffs) {\n return;\n }\n applyDiffs(diffs, fork, 'rebase');\n}\n\nasync function computeDiffs(\n startHash: Hash,\n endHash: Hash,\n store: LazyStore,\n readOptions: ZeroReadOptions | undefined,\n): Promise<InternalDiff | undefined> {\n const readFn = (dagRead: Read) =>\n diff(\n startHash,\n endHash,\n dagRead,\n {\n shouldComputeDiffs: () => true,\n shouldComputeDiffsForIndex(_name) {\n return false;\n },\n },\n FormatVersion.Latest,\n );\n\n let diffs: DiffsMap;\n if (readOptions?.openLazySourceRead) {\n diffs = await using(store.read(readOptions.openLazySourceRead), readFn);\n } else if (readOptions?.openLazyRead) {\n diffs = await readFn(readOptions.openLazyRead);\n } else {\n diffs = await withRead(store, readFn);\n }\n\n return diffs.get('');\n}\n\ntype ApplyDiffsReason = 'poke' | 'rebase' | 'init' | 'refresh';\n\nfunction applyDiffs(\n diffs: NoIndexDiff,\n branch: IVMSourceBranch,\n reason: ApplyDiffsReason,\n) {\n for (\n let i = diffBinarySearch(diffs, ENTITIES_KEY_PREFIX, diff => diff.key);\n i < diffs.length;\n i++\n ) {\n const diff = diffs[i];\n const {key} = diff;\n if (!key.startsWith(ENTITIES_KEY_PREFIX)) {\n break;\n }\n const name = sourceNameFromKey(key);\n const source = must(branch.getSource(name));\n const options = {reason};\n switch (diff.op) {\n case 'del':\n consume(\n source.push(\n {\n type: 'remove',\n row: diff.oldValue as Row,\n },\n options,\n ),\n );\n break;\n case 'add':\n consume(\n source.push(\n {\n type: 'add',\n row: diff.newValue as Row,\n },\n options,\n ),\n );\n break;\n case 'change':\n consume(\n source.push(\n {\n type: 'edit',\n row: diff.newValue as Row,\n oldRow: diff.oldValue as Row,\n },\n options,\n ),\n );\n break;\n }\n }\n}\n"],"names":["FormatVersion.Latest","diff"],"mappings":";;;;;;;;;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