@rocicorp/zero 0.25.0-canary.15 → 0.25.0-canary.18
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/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/config/zero-config.d.ts +8 -0
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +16 -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/services/litestream/commands.js +3 -2
- package/out/zero-cache/src/services/litestream/commands.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 +4 -37
- package/out/zero-client/src/client/crud.d.ts.map +1 -1
- package/out/zero-client/src/client/crud.js +18 -114
- 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 -43
- package/out/zero-client/src/client/custom.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 +4 -1
- 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 +10 -3
- 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 +96 -17
- 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 +6 -1
- package/out/zero-solid/src/solid-view.d.ts.map +1 -1
- package/out/zero-solid/src/solid-view.js +1 -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/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 +25 -25
- package/out/zql/src/mutate/mutator-registry.js.map +1 -1
- package/out/zql/src/mutate/mutator.d.ts +42 -62
- 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/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/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/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zql-database.js","sources":["../../../../zero-server/src/zql-database.ts"],"sourcesContent":["import type {MaybePromise} from '../../shared/src/types.ts';\nimport {formatPg, sql} from '../../z2s/src/sql.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport type {
|
|
1
|
+
{"version":3,"file":"zql-database.js","sources":["../../../../zero-server/src/zql-database.ts"],"sourcesContent":["import type {MaybePromise} from '../../shared/src/types.ts';\nimport {formatPg, sql} from '../../z2s/src/sql.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport type {DBConnection, DBTransaction} from '../../zql/src/mutate/custom.ts';\nimport type {\n HumanReadable,\n Query,\n RunOptions,\n} from '../../zql/src/query/query.ts';\nimport {CRUDMutatorFactory, type TransactionImpl} from './custom.ts';\nimport type {\n Database,\n TransactionProviderHooks,\n TransactionProviderInput,\n} from './process-mutations.ts';\n\n/**\n * Implements a Database for use with PushProcessor that is backed by Postgres.\n *\n * This implementation also implements the same ZQL interfaces for reading and\n * writing data that the Zero client does, so that mutator functions can be\n * shared across client and server.\n */\nexport class ZQLDatabase<TSchema extends Schema, TWrappedTransaction>\n implements Database<TransactionImpl<TSchema, TWrappedTransaction>>\n{\n readonly connection: DBConnection<TWrappedTransaction>;\n readonly #crudFactory: CRUDMutatorFactory<TSchema>;\n\n constructor(connection: DBConnection<TWrappedTransaction>, schema: TSchema) {\n this.connection = connection;\n this.#crudFactory = new CRUDMutatorFactory(schema);\n }\n\n transaction<R>(\n callback: (\n tx: TransactionImpl<TSchema, TWrappedTransaction>,\n transactionHooks: TransactionProviderHooks,\n ) => MaybePromise<R>,\n transactionInput?: TransactionProviderInput,\n ): Promise<R> {\n // Icky hack. This is just here to have user not have to do this.\n // These interfaces need to be factored better.\n const {\n upstreamSchema = '',\n clientGroupID = '',\n clientID = '',\n mutationID = 0,\n } = transactionInput ?? {};\n return this.connection.transaction(async dbTx => {\n const zeroTx = await this.#makeServerTransaction(\n dbTx,\n clientID,\n mutationID,\n );\n\n return callback(zeroTx, {\n async updateClientMutationID() {\n const formatted = formatPg(\n sql`INSERT INTO ${sql.ident(upstreamSchema)}.clients \n as current (\"clientGroupID\", \"clientID\", \"lastMutationID\")\n VALUES (${clientGroupID}, ${clientID}, ${1})\n ON CONFLICT (\"clientGroupID\", \"clientID\")\n DO UPDATE SET \"lastMutationID\" = current.\"lastMutationID\" + 1\n RETURNING \"lastMutationID\"`,\n );\n\n const [{lastMutationID}] = (await dbTx.query(\n formatted.text,\n formatted.values,\n )) as {lastMutationID: bigint}[];\n\n return {lastMutationID};\n },\n\n async writeMutationResult(result) {\n const formatted = formatPg(\n sql`INSERT INTO ${sql.ident(upstreamSchema)}.mutations\n (\"clientGroupID\", \"clientID\", \"mutationID\", \"result\")\n VALUES (${clientGroupID}, ${result.id.clientID}, ${result.id.id}, ${JSON.stringify(\n result.result,\n )}::text::json)`,\n );\n await dbTx.query(formatted.text, formatted.values);\n },\n });\n });\n }\n\n #makeServerTransaction(\n dbTx: DBTransaction<TWrappedTransaction>,\n clientID: string,\n mutationID: number,\n ) {\n return this.#crudFactory.createTransaction(dbTx, clientID, mutationID);\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.transaction(tx => tx.run(query, options));\n }\n}\n"],"names":[],"mappings":";;AAuBO,MAAM,YAEb;AAAA,EACW;AAAA,EACA;AAAA,EAET,YAAY,YAA+C,QAAiB;AAC1E,SAAK,aAAa;AAClB,SAAK,eAAe,IAAI,mBAAmB,MAAM;AAAA,EACnD;AAAA,EAEA,YACE,UAIA,kBACY;AAGZ,UAAM;AAAA,MACJ,iBAAiB;AAAA,MACjB,gBAAgB;AAAA,MAChB,WAAW;AAAA,MACX,aAAa;AAAA,IAAA,IACX,oBAAoB,CAAA;AACxB,WAAO,KAAK,WAAW,YAAY,OAAM,SAAQ;AAC/C,YAAM,SAAS,MAAM,KAAK;AAAA,QACxB;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAGF,aAAO,SAAS,QAAQ;AAAA,QACtB,MAAM,yBAAyB;AAC7B,gBAAM,YAAY;AAAA,YAChB,kBAAkB,IAAI,MAAM,cAAc,CAAC;AAAA;AAAA,kCAErB,aAAa,KAAK,QAAQ,KAAK,CAAC;AAAA;AAAA;AAAA;AAAA,UAAA;AAMxD,gBAAM,CAAC,EAAC,eAAA,CAAe,IAAK,MAAM,KAAK;AAAA,YACrC,UAAU;AAAA,YACV,UAAU;AAAA,UAAA;AAGZ,iBAAO,EAAC,eAAA;AAAA,QACV;AAAA,QAEA,MAAM,oBAAoB,QAAQ;AAChC,gBAAM,YAAY;AAAA,YAChB,kBAAkB,IAAI,MAAM,cAAc,CAAC;AAAA;AAAA,0BAE7B,aAAa,KAAK,OAAO,GAAG,QAAQ,KAAK,OAAO,GAAG,EAAE,KAAK,KAAK;AAAA,cACvE,OAAO;AAAA,YAAA,CACR;AAAA,UAAA;AAEP,gBAAM,KAAK,MAAM,UAAU,MAAM,UAAU,MAAM;AAAA,QACnD;AAAA,MAAA,CACD;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,uBACE,MACA,UACA,YACA;AACA,WAAO,KAAK,aAAa,kBAAkB,MAAM,UAAU,UAAU;AAAA,EACvE;AAAA,EAEA,IACE,OACA,SACiC;AACjC,WAAO,KAAK,YAAY,CAAA,OAAM,GAAG,IAAI,OAAO,OAAO,CAAC;AAAA,EACtD;AACF;"}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { type SetStoreFunction } from 'solid-js/store';
|
|
2
|
-
import { type Change, type Entry, type Format, type Input, type Output, type Query, type Schema, type TTL } from '../../zero-client/src/mod.js';
|
|
3
2
|
import type { QueryResultDetails } from '../../zero-client/src/types/query-result.ts';
|
|
4
3
|
import type { ErroredQuery } from '../../zero-protocol/src/custom-queries.ts';
|
|
4
|
+
import type { Schema } from '../../zero-types/src/schema.ts';
|
|
5
|
+
import type { Change } from '../../zql/src/ivm/change.ts';
|
|
6
|
+
import { type Input, type Output } from '../../zql/src/ivm/operator.ts';
|
|
7
|
+
import type { Entry, Format } from '../../zql/src/ivm/view.ts';
|
|
8
|
+
import type { Query } from '../../zql/src/query/query.ts';
|
|
9
|
+
import type { TTL } from '../../zql/src/query/ttl.ts';
|
|
5
10
|
export type State = [Entry, QueryResultDetails];
|
|
6
11
|
export declare const COMPLETE: QueryResultDetails;
|
|
7
12
|
export declare const UNKNOWN: QueryResultDetails;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solid-view.d.ts","sourceRoot":"","sources":["../../../../zero-solid/src/solid-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,gBAAgB,EAAC,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"solid-view.d.ts","sourceRoot":"","sources":["../../../../zero-solid/src/solid-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,gBAAgB,EAAC,MAAM,gBAAgB,CAAC;AAEzE,OAAO,KAAK,EAEV,kBAAkB,EACnB,MAAM,6CAA6C,CAAC;AACrD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,2CAA2C,CAAC;AAC5E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,gCAAgC,CAAC;AAC3D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,6BAA6B,CAAC;AAExD,OAAO,EAEL,KAAK,KAAK,EACV,KAAK,MAAM,EACZ,MAAM,+BAA+B,CAAC;AAOvC,OAAO,KAAK,EAAiB,KAAK,EAAE,MAAM,EAAC,MAAM,2BAA2B,CAAC;AAC7E,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,8BAA8B,CAAC;AACxD,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,4BAA4B,CAAC;AAEpD,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAEhD,eAAO,MAAM,QAAQ,EAAE,kBAAsD,CAAC;AAC9E,eAAO,MAAM,OAAO,EAAE,kBAAqD,CAAC;AAE5E,qBAAa,SAAU,YAAW,MAAM;;gBAqBpC,KAAK,EAAE,KAAK,EACZ,mBAAmB,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,IAAI,EAC7C,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,IAAI,EACrB,aAAa,EAAE,IAAI,GAAG,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,EAClD,SAAS,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,EAC7B,QAAQ,EAAE,gBAAgB,CAAC,KAAK,CAAC,EACjC,KAAK,EAAE,MAAM,IAAI;IAiEnB,OAAO,IAAI,IAAI;IA2Bf,IAAI,CAAC,MAAM,EAAE,MAAM;IAqDnB,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI;CAG1B;AA8CD,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,gBAAgB,CAAC,KAAK,CAAC,EACjC,KAAK,CAAC,EAAE,MAAM,IAAI,IAGhB,MAAM,SAAS,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,MAAM,EAC/C,OAAO,SAAS,MAAM,EACtB,OAAO,UAEC,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,SAChC,KAAK,UACJ,MAAM,aACH,MAAM,IAAI,uBACA,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,IAAI,iBAC9B,IAAI,GAAG,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,aACvC,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,eAiBhC"}
|
|
@@ -1,38 +1,7 @@
|
|
|
1
1
|
import { reconcile, produce } from "solid-js/store";
|
|
2
|
-
import "../../shared/src/config.js";
|
|
3
|
-
import "@rocicorp/resolver";
|
|
4
|
-
import "../../shared/src/resolved-promises.js";
|
|
5
|
-
import "../../shared/src/sleep.js";
|
|
6
|
-
import "../../replicache/src/hash.js";
|
|
7
|
-
import "../../replicache/src/deleted-clients.js";
|
|
8
|
-
import "@rocicorp/lock";
|
|
9
|
-
import "@rocicorp/logger";
|
|
10
|
-
import "../../replicache/src/persist/client-groups.js";
|
|
11
|
-
import "../../replicache/src/persist/clients.js";
|
|
12
|
-
import "../../shared/src/json-schema.js";
|
|
13
|
-
import "../../zero-protocol/src/custom-queries.js";
|
|
14
|
-
import "js-xxhash";
|
|
15
|
-
import "../../zero-protocol/src/client-schema.js";
|
|
16
|
-
import "../../zero-schema/src/permissions.js";
|
|
17
|
-
import { idSymbol, applyChange } from "../../zql/src/ivm/view-apply-change.js";
|
|
18
|
-
import "../../zero-protocol/src/ast.js";
|
|
19
|
-
import "../../shared/src/document-visible.js";
|
|
20
|
-
import "../../replicache/src/btree/node.js";
|
|
21
|
-
import "compare-utf8";
|
|
22
|
-
import "../../replicache/src/sync/push.js";
|
|
23
2
|
import { emptyArray } from "../../shared/src/sentinels.js";
|
|
24
|
-
import "../../shared/src/valita.js";
|
|
25
|
-
import "../../zero-protocol/src/connect.js";
|
|
26
|
-
import "../../zero-protocol/src/down.js";
|
|
27
|
-
import "../../zero-protocol/src/error.js";
|
|
28
|
-
import "../../zero-protocol/src/protocol-version.js";
|
|
29
|
-
import "../../zero-protocol/src/push.js";
|
|
30
|
-
import "../../zero-protocol/src/version.js";
|
|
31
3
|
import { skipYields } from "../../zql/src/ivm/operator.js";
|
|
32
|
-
import "../../
|
|
33
|
-
import "../../zero-protocol/src/primary-key.js";
|
|
34
|
-
import "../../datadog/src/datadog-log-sink.js";
|
|
35
|
-
import "../../zero-client/src/client/reload-error-handler.js";
|
|
4
|
+
import { idSymbol, applyChange } from "../../zql/src/ivm/view-apply-change.js";
|
|
36
5
|
const COMPLETE = Object.freeze({ type: "complete" });
|
|
37
6
|
const UNKNOWN = Object.freeze({ type: "unknown" });
|
|
38
7
|
class SolidView {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solid-view.js","sources":["../../../../zero-solid/src/solid-view.ts"],"sourcesContent":["import {produce, reconcile, type SetStoreFunction} from 'solid-js/store';\nimport {\n applyChange,\n type AnyViewFactory,\n type Change,\n type Entry,\n type Format,\n type Input,\n type Node,\n type Output,\n type Query,\n type Schema,\n type Stream,\n type TTL,\n type ViewChange,\n} from '../../zero-client/src/mod.js';\nimport type {\n QueryErrorDetails,\n QueryResultDetails,\n} from '../../zero-client/src/types/query-result.ts';\nimport type {ErroredQuery} from '../../zero-protocol/src/custom-queries.ts';\nimport {idSymbol} from '../../zql/src/ivm/view-apply-change.ts';\nimport {skipYields} from '../../zql/src/ivm/operator.ts';\nimport {emptyArray} from '../../shared/src/sentinels.ts';\n\nexport type State = [Entry, QueryResultDetails];\n\nexport const COMPLETE: QueryResultDetails = Object.freeze({type: 'complete'});\nexport const UNKNOWN: QueryResultDetails = Object.freeze({type: 'unknown'});\n\nexport class SolidView implements Output {\n readonly #input: Input;\n readonly #format: Format;\n readonly #onDestroy: () => void;\n readonly #retry: () => void;\n\n #setState: SetStoreFunction<State>;\n\n // Optimization: if the store is currently empty we build up\n // the view on a plain old JS object stored at #builderRoot, and return\n // that for the new state on transaction commit. This avoids building up\n // large views from scratch via solid produce. The proxy object used by\n // solid produce is slow and in this case we don't care about solid tracking\n // the fine grained changes (everything has changed, it's all new). For a\n // test case with a view with 3000 rows, each row having 2 children, this\n // optimization reduced #applyChanges time from 743ms to 133ms.\n #builderRoot: Entry | undefined;\n #pendingChanges: ViewChange[] = [];\n readonly #updateTTL: (ttl: TTL) => void;\n\n constructor(\n input: Input,\n onTransactionCommit: (cb: () => void) => void,\n format: Format,\n onDestroy: () => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n setState: SetStoreFunction<State>,\n retry: () => void,\n ) {\n this.#input = input;\n onTransactionCommit(this.#onTransactionCommit);\n this.#format = format;\n this.#onDestroy = onDestroy;\n this.#updateTTL = updateTTL;\n this.#retry = retry;\n\n input.setOutput(this);\n\n const initialRoot = this.#createEmptyRoot();\n this.#applyChangesToRoot(\n skipYields(input.fetch({})),\n node => ({type: 'add', node}),\n initialRoot,\n );\n\n this.#setState = setState;\n this.#setState(\n reconcile(\n [\n initialRoot,\n queryComplete === true\n ? COMPLETE\n : 'error' in queryComplete\n ? this.#makeError(queryComplete)\n : UNKNOWN,\n ],\n {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n },\n ),\n );\n\n if (isEmptyRoot(initialRoot)) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n\n if (queryComplete !== true && !('error' in queryComplete)) {\n void queryComplete\n .then(() => {\n this.#setState(prev => [prev[0], COMPLETE]);\n })\n .catch((error: ErroredQuery) => {\n this.#setState(prev => [prev[0], this.#makeError(error)]);\n });\n }\n }\n\n #makeError(error: ErroredQuery): QueryErrorDetails {\n const message = error.message ?? 'An unknown error occurred';\n return {\n type: 'error',\n retry: this.#retry,\n refetch: this.#retry,\n error: {\n type: error.error,\n message,\n ...(error.details ? {details: error.details} : {}),\n },\n };\n }\n\n destroy(): void {\n this.#onDestroy();\n }\n\n #onTransactionCommit = () => {\n const builderRoot = this.#builderRoot;\n if (builderRoot) {\n if (!isEmptyRoot(builderRoot)) {\n this.#setState(\n 0,\n reconcile(builderRoot, {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n }),\n );\n this.#setState(prev => [builderRoot, prev[1]]);\n this.#builderRoot = undefined;\n }\n } else {\n try {\n this.#applyChanges(this.#pendingChanges, c => c);\n } finally {\n this.#pendingChanges = [];\n }\n }\n };\n\n push(change: Change) {\n // Delay updating the solid store state until the transaction commit\n // (because each update of the solid store is quite expensive). If\n // this.#builderRoot is defined apply the changes to it (we are building\n // from an empty root), otherwise queue the changes to be applied\n // using produce at the end of the transaction but read the relationships\n // now as they are only valid to read when the push is received.\n if (this.#builderRoot) {\n this.#applyChangeToRoot(change, this.#builderRoot);\n } else {\n this.#pendingChanges.push(materializeRelationships(change));\n }\n return emptyArray;\n }\n\n #applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => ViewChange): void {\n this.#setState(\n produce((draftState: State) => {\n this.#applyChangesToRoot<T>(changes, mapper, draftState[0]);\n if (isEmptyRoot(draftState[0])) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n }),\n );\n }\n\n #applyChangesToRoot<T>(\n changes: Iterable<T>,\n mapper: (v: T) => ViewChange,\n root: Entry,\n ) {\n for (const change of changes) {\n this.#applyChangeToRoot(mapper(change), root);\n }\n }\n\n #applyChangeToRoot(change: ViewChange, root: Entry) {\n applyChange(\n root,\n change,\n this.#input.getSchema(),\n '',\n this.#format,\n true /* withIDs */,\n );\n }\n\n #createEmptyRoot(): Entry {\n return {\n '': this.#format.singular ? undefined : [],\n };\n }\n\n updateTTL(ttl: TTL): void {\n this.#updateTTL(ttl);\n }\n}\n\nfunction materializeRelationships(change: Change): ViewChange {\n switch (change.type) {\n case 'add':\n return {type: 'add', node: materializeNodeRelationships(change.node)};\n case 'remove':\n return {type: 'remove', node: materializeNodeRelationships(change.node)};\n case 'child':\n return {\n type: 'child',\n node: {row: change.node.row},\n child: {\n relationshipName: change.child.relationshipName,\n change: materializeRelationships(change.child.change),\n },\n };\n case 'edit':\n return {\n type: 'edit',\n node: {row: change.node.row},\n oldNode: {row: change.oldNode.row},\n };\n }\n}\n\nfunction materializeNodeRelationships(node: Node): Node {\n const relationships: Record<string, () => Stream<Node>> = {};\n for (const relationship in node.relationships) {\n const materialized: Node[] = [];\n for (const n of skipYields(node.relationships[relationship]())) {\n materialized.push(materializeNodeRelationships(n));\n }\n relationships[relationship] = () => materialized;\n }\n return {\n row: node.row,\n relationships,\n };\n}\n\nfunction isEmptyRoot(entry: Entry) {\n const data = entry[''];\n return data === undefined || (Array.isArray(data) && data.length === 0);\n}\n\nexport function createSolidViewFactory(\n setState: SetStoreFunction<State>,\n retry?: () => void,\n) {\n function solidViewFactory<\n TTable extends keyof TSchema['tables'] & string,\n TSchema extends Schema,\n TReturn,\n >(\n _query: Query<TTable, TSchema, TReturn>,\n input: Input,\n format: Format,\n onDestroy: () => void,\n onTransactionCommit: (cb: () => void) => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n return new SolidView(\n input,\n onTransactionCommit,\n format,\n onDestroy,\n queryComplete,\n updateTTL,\n setState,\n retry || (() => {}),\n );\n }\n\n solidViewFactory satisfies AnyViewFactory;\n\n return solidViewFactory;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;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CAAG,GAAG;AAC9D,mBAAa,KAAK,6BAA6B,CAAC,CAAC;AAAA,IACnD;AACA,kBAAc,YAAY,IAAI,MAAM;AAAA,EACtC;AACA,SAAO;AAAA,IACL,KAAK,KAAK;AAAA,IACV;AAAA,EAAA;AAEJ;AAEA,SAAS,YAAY,OAAc;AACjC,QAAM,OAAO,MAAM,EAAE;AACrB,SAAO,SAAS,UAAc,MAAM,QAAQ,IAAI,KAAK,KAAK,WAAW;AACvE;AAEO,SAAS,uBACd,UACA,OACA;AACA,WAAS,iBAKP,QACA,OACA,QACA,WACA,qBACA,eACA,WACA;AACA,WAAO,IAAI;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,UAAU,MAAM;AAAA,MAAC;AAAA,IAAA;AAAA,EAErB;AAIA,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"solid-view.js","sources":["../../../../zero-solid/src/solid-view.ts"],"sourcesContent":["import {produce, reconcile, type SetStoreFunction} from 'solid-js/store';\nimport {emptyArray} from '../../shared/src/sentinels.ts';\nimport type {\n QueryErrorDetails,\n QueryResultDetails,\n} from '../../zero-client/src/types/query-result.ts';\nimport type {ErroredQuery} from '../../zero-protocol/src/custom-queries.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport type {Change} from '../../zql/src/ivm/change.ts';\nimport type {Node} from '../../zql/src/ivm/data.ts';\nimport {\n skipYields,\n type Input,\n type Output,\n} from '../../zql/src/ivm/operator.ts';\nimport type {Stream} from '../../zql/src/ivm/stream.ts';\nimport {\n applyChange,\n idSymbol,\n type ViewChange,\n} from '../../zql/src/ivm/view-apply-change.ts';\nimport type {AnyViewFactory, Entry, Format} from '../../zql/src/ivm/view.ts';\nimport type {Query} from '../../zql/src/query/query.ts';\nimport type {TTL} from '../../zql/src/query/ttl.ts';\n\nexport type State = [Entry, QueryResultDetails];\n\nexport const COMPLETE: QueryResultDetails = Object.freeze({type: 'complete'});\nexport const UNKNOWN: QueryResultDetails = Object.freeze({type: 'unknown'});\n\nexport class SolidView implements Output {\n readonly #input: Input;\n readonly #format: Format;\n readonly #onDestroy: () => void;\n readonly #retry: () => void;\n\n #setState: SetStoreFunction<State>;\n\n // Optimization: if the store is currently empty we build up\n // the view on a plain old JS object stored at #builderRoot, and return\n // that for the new state on transaction commit. This avoids building up\n // large views from scratch via solid produce. The proxy object used by\n // solid produce is slow and in this case we don't care about solid tracking\n // the fine grained changes (everything has changed, it's all new). For a\n // test case with a view with 3000 rows, each row having 2 children, this\n // optimization reduced #applyChanges time from 743ms to 133ms.\n #builderRoot: Entry | undefined;\n #pendingChanges: ViewChange[] = [];\n readonly #updateTTL: (ttl: TTL) => void;\n\n constructor(\n input: Input,\n onTransactionCommit: (cb: () => void) => void,\n format: Format,\n onDestroy: () => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n setState: SetStoreFunction<State>,\n retry: () => void,\n ) {\n this.#input = input;\n onTransactionCommit(this.#onTransactionCommit);\n this.#format = format;\n this.#onDestroy = onDestroy;\n this.#updateTTL = updateTTL;\n this.#retry = retry;\n\n input.setOutput(this);\n\n const initialRoot = this.#createEmptyRoot();\n this.#applyChangesToRoot(\n skipYields(input.fetch({})),\n node => ({type: 'add', node}),\n initialRoot,\n );\n\n this.#setState = setState;\n this.#setState(\n reconcile(\n [\n initialRoot,\n queryComplete === true\n ? COMPLETE\n : 'error' in queryComplete\n ? this.#makeError(queryComplete)\n : UNKNOWN,\n ],\n {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n },\n ),\n );\n\n if (isEmptyRoot(initialRoot)) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n\n if (queryComplete !== true && !('error' in queryComplete)) {\n void queryComplete\n .then(() => {\n this.#setState(prev => [prev[0], COMPLETE]);\n })\n .catch((error: ErroredQuery) => {\n this.#setState(prev => [prev[0], this.#makeError(error)]);\n });\n }\n }\n\n #makeError(error: ErroredQuery): QueryErrorDetails {\n const message = error.message ?? 'An unknown error occurred';\n return {\n type: 'error',\n retry: this.#retry,\n refetch: this.#retry,\n error: {\n type: error.error,\n message,\n ...(error.details ? {details: error.details} : {}),\n },\n };\n }\n\n destroy(): void {\n this.#onDestroy();\n }\n\n #onTransactionCommit = () => {\n const builderRoot = this.#builderRoot;\n if (builderRoot) {\n if (!isEmptyRoot(builderRoot)) {\n this.#setState(\n 0,\n reconcile(builderRoot, {\n // solidjs's types want a string, but a symbol works\n key: idSymbol as unknown as string,\n }),\n );\n this.#setState(prev => [builderRoot, prev[1]]);\n this.#builderRoot = undefined;\n }\n } else {\n try {\n this.#applyChanges(this.#pendingChanges, c => c);\n } finally {\n this.#pendingChanges = [];\n }\n }\n };\n\n push(change: Change) {\n // Delay updating the solid store state until the transaction commit\n // (because each update of the solid store is quite expensive). If\n // this.#builderRoot is defined apply the changes to it (we are building\n // from an empty root), otherwise queue the changes to be applied\n // using produce at the end of the transaction but read the relationships\n // now as they are only valid to read when the push is received.\n if (this.#builderRoot) {\n this.#applyChangeToRoot(change, this.#builderRoot);\n } else {\n this.#pendingChanges.push(materializeRelationships(change));\n }\n return emptyArray;\n }\n\n #applyChanges<T>(changes: Iterable<T>, mapper: (v: T) => ViewChange): void {\n this.#setState(\n produce((draftState: State) => {\n this.#applyChangesToRoot<T>(changes, mapper, draftState[0]);\n if (isEmptyRoot(draftState[0])) {\n this.#builderRoot = this.#createEmptyRoot();\n }\n }),\n );\n }\n\n #applyChangesToRoot<T>(\n changes: Iterable<T>,\n mapper: (v: T) => ViewChange,\n root: Entry,\n ) {\n for (const change of changes) {\n this.#applyChangeToRoot(mapper(change), root);\n }\n }\n\n #applyChangeToRoot(change: ViewChange, root: Entry) {\n applyChange(\n root,\n change,\n this.#input.getSchema(),\n '',\n this.#format,\n true /* withIDs */,\n );\n }\n\n #createEmptyRoot(): Entry {\n return {\n '': this.#format.singular ? undefined : [],\n };\n }\n\n updateTTL(ttl: TTL): void {\n this.#updateTTL(ttl);\n }\n}\n\nfunction materializeRelationships(change: Change): ViewChange {\n switch (change.type) {\n case 'add':\n return {type: 'add', node: materializeNodeRelationships(change.node)};\n case 'remove':\n return {type: 'remove', node: materializeNodeRelationships(change.node)};\n case 'child':\n return {\n type: 'child',\n node: {row: change.node.row},\n child: {\n relationshipName: change.child.relationshipName,\n change: materializeRelationships(change.child.change),\n },\n };\n case 'edit':\n return {\n type: 'edit',\n node: {row: change.node.row},\n oldNode: {row: change.oldNode.row},\n };\n }\n}\n\nfunction materializeNodeRelationships(node: Node): Node {\n const relationships: Record<string, () => Stream<Node>> = {};\n for (const relationship in node.relationships) {\n const materialized: Node[] = [];\n for (const n of skipYields(node.relationships[relationship]())) {\n materialized.push(materializeNodeRelationships(n));\n }\n relationships[relationship] = () => materialized;\n }\n return {\n row: node.row,\n relationships,\n };\n}\n\nfunction isEmptyRoot(entry: Entry) {\n const data = entry[''];\n return data === undefined || (Array.isArray(data) && data.length === 0);\n}\n\nexport function createSolidViewFactory(\n setState: SetStoreFunction<State>,\n retry?: () => void,\n) {\n function solidViewFactory<\n TTable extends keyof TSchema['tables'] & string,\n TSchema extends Schema,\n TReturn,\n >(\n _query: Query<TTable, TSchema, TReturn>,\n input: Input,\n format: Format,\n onDestroy: () => void,\n onTransactionCommit: (cb: () => void) => void,\n queryComplete: true | ErroredQuery | Promise<true>,\n updateTTL: (ttl: TTL) => void,\n ) {\n return new SolidView(\n input,\n onTransactionCommit,\n format,\n onDestroy,\n queryComplete,\n updateTTL,\n setState,\n retry || (() => {}),\n );\n }\n\n solidViewFactory satisfies AnyViewFactory;\n\n return solidViewFactory;\n}\n"],"names":[],"mappings":";;;;AA2BO,MAAM,WAA+B,OAAO,OAAO,EAAC,MAAM,YAAW;AACrE,MAAM,UAA8B,OAAO,OAAO,EAAC,MAAM,WAAU;AAEnE,MAAM,UAA4B;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA;AAAA;AAAA;A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M,OAAO,MAAM,EAAE;AACrB,SAAO,SAAS,UAAc,MAAM,QAAQ,IAAI,KAAK,KAAK,WAAW;AACvE;AAEO,SAAS,uBACd,UACA,OACA;AACA,WAAS,iBAKP,QACA,OACA,QACA,WACA,qBACA,eACA,WACA;AACA,WAAO,IAAI;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,UAAU,MAAM;AAAA,MAAC;AAAA,IAAA;AAAA,EAErB;AAIA,SAAO;AACT;"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type Accessor } from 'solid-js';
|
|
2
|
+
import type { ReadonlyJSONValue } from '../../shared/src/json.ts';
|
|
2
3
|
import type { QueryResultDetails } from '../../zero-client/src/types/query-result.ts';
|
|
3
4
|
import type { DefaultContext, DefaultSchema } from '../../zero-types/src/default-types.ts';
|
|
4
5
|
import type { Schema } from '../../zero-types/src/schema.ts';
|
|
5
|
-
import
|
|
6
|
+
import { type QueryOrQueryRequest } from '../../zql/src/query/query-registry.ts';
|
|
7
|
+
import { type HumanReadable, type PullRow } from '../../zql/src/query/query.ts';
|
|
6
8
|
import { type TTL } from '../../zql/src/query/ttl.ts';
|
|
7
9
|
export type QueryResult<TReturn> = readonly [
|
|
8
10
|
Accessor<HumanReadable<TReturn>>,
|
|
@@ -20,6 +22,6 @@ export type UseQueryOptions = {
|
|
|
20
22
|
/**
|
|
21
23
|
* @deprecated Use {@linkcode useQuery} instead.
|
|
22
24
|
*/
|
|
23
|
-
export declare function createQuery<TTable extends keyof TSchema['tables'] & string, TSchema extends Schema = DefaultSchema, TReturn = PullRow<TTable, TSchema>, TContext = DefaultContext>(querySignal: Accessor<
|
|
24
|
-
export declare function useQuery<TTable extends keyof TSchema['tables'] & string, TSchema extends Schema = DefaultSchema, TReturn = PullRow<TTable, TSchema>, TContext = DefaultContext>(querySignal: Accessor<
|
|
25
|
+
export declare function createQuery<TTable extends keyof TSchema['tables'] & string, TInput extends ReadonlyJSONValue | undefined, TOutput extends ReadonlyJSONValue | undefined, TSchema extends Schema = DefaultSchema, TReturn = PullRow<TTable, TSchema>, TContext = DefaultContext>(querySignal: Accessor<QueryOrQueryRequest<TTable, TInput, TOutput, TSchema, TReturn, TContext>>, options?: CreateQueryOptions | Accessor<CreateQueryOptions>): QueryResult<TReturn>;
|
|
26
|
+
export declare function useQuery<TTable extends keyof TSchema['tables'] & string, TInput extends ReadonlyJSONValue | undefined, TOutput extends ReadonlyJSONValue | undefined, TSchema extends Schema = DefaultSchema, TReturn = PullRow<TTable, TSchema>, TContext = DefaultContext>(querySignal: Accessor<QueryOrQueryRequest<TTable, TInput, TOutput, TSchema, TReturn, TContext>>, options?: UseQueryOptions | Accessor<UseQueryOptions>): QueryResult<TReturn>;
|
|
25
27
|
//# sourceMappingURL=use-query.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-query.d.ts","sourceRoot":"","sources":["../../../../zero-solid/src/use-query.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,QAAQ,EACd,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"use-query.d.ts","sourceRoot":"","sources":["../../../../zero-solid/src/use-query.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,QAAQ,EACd,MAAM,UAAU,CAAC;AAElB,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEhE,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,6CAA6C,CAAC;AACpF,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACd,MAAM,uCAAuC,CAAC;AAC/C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,gCAAgC,CAAC;AAC3D,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAC,KAAK,aAAa,EAAE,KAAK,OAAO,EAAC,MAAM,8BAA8B,CAAC;AAC9E,OAAO,EA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AmB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,CACzE,EACD,OAAO,CAAC,EAAE,eAAe,GAAG,QAAQ,CAAC,eAAe,CAAC,GACpD,WAAW,CAAC,OAAO,CAAC,CAsDtB"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createSignal, createMemo, untrack, onCleanup, createEffect, on } from "solid-js";
|
|
2
2
|
import { createStore } from "solid-js/store";
|
|
3
3
|
import { bindingsForZero } from "../../zero-client/src/client/bindings.js";
|
|
4
|
+
import { addContextToQuery } from "../../zql/src/query/query-registry.js";
|
|
5
|
+
import "../../zero-protocol/src/ast.js";
|
|
4
6
|
import { DEFAULT_TTL_MS } from "../../zql/src/query/ttl.js";
|
|
5
7
|
import { UNKNOWN, createSolidViewFactory } from "./solid-view.js";
|
|
6
8
|
import { useZero } from "./use-zero.js";
|
|
@@ -20,7 +22,9 @@ function useQuery(querySignal, options) {
|
|
|
20
22
|
setRefetchKey((k) => k + 1);
|
|
21
23
|
};
|
|
22
24
|
const zero = useZero();
|
|
23
|
-
const query = createMemo(
|
|
25
|
+
const query = createMemo(
|
|
26
|
+
() => addContextToQuery(querySignal(), zero().context)
|
|
27
|
+
);
|
|
24
28
|
const bindings = createMemo(() => bindingsForZero(zero()));
|
|
25
29
|
const hash = createMemo(() => bindings().hash(query()));
|
|
26
30
|
const ttl = createMemo(() => normalize(options)?.ttl ?? DEFAULT_TTL_MS);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-query.js","sources":["../../../../zero-solid/src/use-query.ts"],"sourcesContent":["import {\n createEffect,\n createMemo,\n createSignal,\n on,\n onCleanup,\n untrack,\n type Accessor,\n} from 'solid-js';\nimport {createStore} from 'solid-js/store';\nimport {bindingsForZero} from '../../zero-client/src/client/bindings.ts';\nimport type {QueryResultDetails} from '../../zero-client/src/types/query-result.ts';\nimport type {\n DefaultContext,\n DefaultSchema,\n} from '../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport
|
|
1
|
+
{"version":3,"file":"use-query.js","sources":["../../../../zero-solid/src/use-query.ts"],"sourcesContent":["import {\n createEffect,\n createMemo,\n createSignal,\n on,\n onCleanup,\n untrack,\n type Accessor,\n} from 'solid-js';\nimport {createStore} from 'solid-js/store';\nimport type {ReadonlyJSONValue} from '../../shared/src/json.ts';\nimport {bindingsForZero} from '../../zero-client/src/client/bindings.ts';\nimport type {QueryResultDetails} from '../../zero-client/src/types/query-result.ts';\nimport type {\n DefaultContext,\n DefaultSchema,\n} from '../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport {\n addContextToQuery,\n type QueryOrQueryRequest,\n} from '../../zql/src/query/query-registry.ts';\nimport {type HumanReadable, type PullRow} from '../../zql/src/query/query.ts';\nimport {DEFAULT_TTL_MS, type TTL} from '../../zql/src/query/ttl.ts';\nimport {createSolidViewFactory, UNKNOWN, type State} from './solid-view.ts';\nimport {useZero} from './use-zero.ts';\n\nexport type QueryResult<TReturn> = readonly [\n Accessor<HumanReadable<TReturn>>,\n Accessor<QueryResultDetails & {}>,\n];\n\n// Deprecated in 0.22\n/**\n * @deprecated Use {@linkcode UseQueryOptions} instead.\n */\nexport type CreateQueryOptions = {\n ttl?: TTL | undefined;\n};\n\nexport type UseQueryOptions = {\n ttl?: TTL | undefined;\n};\n\n// Deprecated in 0.22\n/**\n * @deprecated Use {@linkcode useQuery} instead.\n */\nexport function createQuery<\n TTable extends keyof TSchema['tables'] & string,\n TInput extends ReadonlyJSONValue | undefined,\n TOutput extends ReadonlyJSONValue | undefined,\n TSchema extends Schema = DefaultSchema,\n TReturn = PullRow<TTable, TSchema>,\n TContext = DefaultContext,\n>(\n querySignal: Accessor<\n QueryOrQueryRequest<TTable, TInput, TOutput, TSchema, TReturn, TContext>\n >,\n options?: CreateQueryOptions | Accessor<CreateQueryOptions>,\n): QueryResult<TReturn> {\n return useQuery(querySignal, options);\n}\n\nexport function useQuery<\n TTable extends keyof TSchema['tables'] & string,\n TInput extends ReadonlyJSONValue | undefined,\n TOutput extends ReadonlyJSONValue | undefined,\n TSchema extends Schema = DefaultSchema,\n TReturn = PullRow<TTable, TSchema>,\n TContext = DefaultContext,\n>(\n querySignal: Accessor<\n QueryOrQueryRequest<TTable, TInput, TOutput, TSchema, TReturn, TContext>\n >,\n options?: UseQueryOptions | Accessor<UseQueryOptions>,\n): QueryResult<TReturn> {\n const [state, setState] = createStore<State>([\n {\n '': undefined,\n },\n UNKNOWN,\n ]);\n const initialRefetchKey = 0;\n const [refetchKey, setRefetchKey] = createSignal(initialRefetchKey);\n\n const refetch = () => {\n setRefetchKey(k => k + 1);\n };\n\n const zero = useZero<TSchema, undefined, TContext>();\n\n const query = createMemo(() =>\n addContextToQuery(querySignal(), zero().context),\n );\n const bindings = createMemo(() => bindingsForZero(zero()));\n const hash = createMemo(() => bindings().hash(query()));\n const ttl = createMemo(() => normalize(options)?.ttl ?? DEFAULT_TTL_MS);\n\n const initialTTL = ttl();\n\n const view = createMemo(() => {\n // Depend on hash instead of query to avoid recreating the view when the\n // query object changes but the hash is the same.\n hash();\n refetchKey();\n const b = bindings();\n const q = untrack(query);\n\n const v = b.materialize(q, createSolidViewFactory(setState, refetch), {\n ttl: initialTTL,\n });\n\n onCleanup(() => v.destroy());\n\n return v;\n });\n\n // Update TTL on existing view when it changes.\n createEffect(\n on(\n ttl,\n currentTTL => {\n view().updateTTL(currentTTL);\n },\n {defer: true},\n ),\n );\n\n return [() => state[0][''] as HumanReadable<TReturn>, () => state[1]];\n}\n\nfunction normalize<T>(options?: T | Accessor<T | undefined>): T | undefined {\n return typeof options === 'function' ? (options as Accessor<T>)() : options;\n}\n"],"names":[],"mappings":";;;;;;;;AAgDO,SAAS,YAQd,aAGA,SACsB;AACtB,SAAO,SAAS,aAAa,OAAO;AACtC;AAEO,SAAS,SAQd,aAGA,SACsB;AACtB,QAAM,CAAC,OAAO,QAAQ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/schema-value.ts';
|
|
3
|
+
import type { Schema, TableSchema } from '../../../zero-types/src/schema.ts';
|
|
4
|
+
import type { MutateCRUD } from './custom.ts';
|
|
5
|
+
export type SchemaCRUD<S extends Schema> = {
|
|
6
|
+
[Table in keyof S['tables']]: TableCRUD<S['tables'][Table]>;
|
|
7
|
+
};
|
|
8
|
+
export type TableCRUD<S extends TableSchema> = {
|
|
9
|
+
/**
|
|
10
|
+
* Writes a row if a row with the same primary key doesn't already exist.
|
|
11
|
+
* Non-primary-key fields that are 'optional' can be omitted or set to
|
|
12
|
+
* `undefined`. Such fields will be assigned the value `null` optimistically
|
|
13
|
+
* and then the default value as defined by the server.
|
|
14
|
+
*/
|
|
15
|
+
insert: (value: InsertValue<S>) => Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Writes a row unconditionally, overwriting any existing row with the same
|
|
18
|
+
* primary key. Non-primary-key fields that are 'optional' can be omitted or
|
|
19
|
+
* set to `undefined`. Such fields will be assigned the value `null`
|
|
20
|
+
* optimistically and then the default value as defined by the server.
|
|
21
|
+
*/
|
|
22
|
+
upsert: (value: UpsertValue<S>) => Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Updates a row with the same primary key. If no such row exists, this
|
|
25
|
+
* function does nothing. All non-primary-key fields can be omitted or set to
|
|
26
|
+
* `undefined`. Such fields will be left unchanged from previous value.
|
|
27
|
+
*/
|
|
28
|
+
update: (value: UpdateValue<S>) => Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Deletes the row with the specified primary key. If no such row exists, this
|
|
31
|
+
* function does nothing.
|
|
32
|
+
*/
|
|
33
|
+
delete: (id: DeleteID<S>) => Promise<void>;
|
|
34
|
+
};
|
|
35
|
+
export type CRUDKind = keyof TableCRUD<TableSchema>;
|
|
36
|
+
export declare const CRUD_KINDS: readonly ["insert", "upsert", "update", "delete"];
|
|
37
|
+
export type DeleteID<S extends TableSchema> = Expand<PrimaryKeyFields<S>>;
|
|
38
|
+
type PrimaryKeyFields<S extends TableSchema> = {
|
|
39
|
+
[K in Extract<S['primaryKey'][number], keyof S['columns']>]: SchemaValueToTSType<S['columns'][K]>;
|
|
40
|
+
};
|
|
41
|
+
export type InsertValue<S extends TableSchema> = Expand<PrimaryKeyFields<S> & {
|
|
42
|
+
[K in keyof S['columns'] as S['columns'][K] extends {
|
|
43
|
+
optional: true;
|
|
44
|
+
} ? K : never]?: SchemaValueToTSType<S['columns'][K]> | undefined;
|
|
45
|
+
} & {
|
|
46
|
+
[K in keyof S['columns'] as S['columns'][K] extends {
|
|
47
|
+
optional: true;
|
|
48
|
+
} ? never : K]: SchemaValueToTSType<S['columns'][K]>;
|
|
49
|
+
}>;
|
|
50
|
+
export type UpsertValue<S extends TableSchema> = InsertValue<S>;
|
|
51
|
+
export type UpdateValue<S extends TableSchema> = Expand<PrimaryKeyFields<S> & {
|
|
52
|
+
[K in keyof S['columns']]?: SchemaValueToTSType<S['columns'][K]> | undefined;
|
|
53
|
+
}>;
|
|
54
|
+
/**
|
|
55
|
+
* This is the type of the generated mutate.<name>.<verb> function.
|
|
56
|
+
*/
|
|
57
|
+
export type TableMutator<TS extends TableSchema> = {
|
|
58
|
+
/**
|
|
59
|
+
* Writes a row if a row with the same primary key doesn't already exist.
|
|
60
|
+
* Non-primary-key fields that are 'optional' can be omitted or set to
|
|
61
|
+
* `undefined`. Such fields will be assigned the value `null` optimistically
|
|
62
|
+
* and then the default value as defined by the server.
|
|
63
|
+
*/
|
|
64
|
+
insert: (value: InsertValue<TS>) => Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Writes a row unconditionally, overwriting any existing row with the same
|
|
67
|
+
* primary key. Non-primary-key fields that are 'optional' can be omitted or
|
|
68
|
+
* set to `undefined`. Such fields will be assigned the value `null`
|
|
69
|
+
* optimistically and then the default value as defined by the server.
|
|
70
|
+
*/
|
|
71
|
+
upsert: (value: UpsertValue<TS>) => Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Updates a row with the same primary key. If no such row exists, this
|
|
74
|
+
* function does nothing. All non-primary-key fields can be omitted or set to
|
|
75
|
+
* `undefined`. Such fields will be left unchanged from previous value.
|
|
76
|
+
*/
|
|
77
|
+
update: (value: UpdateValue<TS>) => Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Deletes the row with the specified primary key. If no such row exists, this
|
|
80
|
+
* function does nothing.
|
|
81
|
+
*/
|
|
82
|
+
delete: (id: DeleteID<TS>) => Promise<void>;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* A function that executes a CRUD operation.
|
|
86
|
+
* Client and server provide different implementations.
|
|
87
|
+
*/
|
|
88
|
+
export type CRUDExecutor = (table: string, kind: CRUDKind, args: unknown) => Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Creates a MutateCRUD function from a schema and executor.
|
|
91
|
+
* This is the shared implementation used by both client and server.
|
|
92
|
+
*
|
|
93
|
+
* @param schema - The Zero schema
|
|
94
|
+
* @param executor - A function that executes CRUD operations
|
|
95
|
+
* @returns A MutateCRUD function that can be called with CRUDMutateRequest objects
|
|
96
|
+
*/
|
|
97
|
+
export declare function makeMutateCRUDFunction<S extends Schema>(schema: S, executor: CRUDExecutor): MutateCRUD<S>;
|
|
98
|
+
export type CRUDMutator<TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, TKind extends keyof TableMutator<TSchema['tables'][TTable]>, TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0]> = {
|
|
99
|
+
(args: TArgs): CRUDMutateRequest<TSchema, TTable, TKind, TArgs>;
|
|
100
|
+
/**
|
|
101
|
+
* Type-only phantom property to surface mutator types in a covariant position.
|
|
102
|
+
*/
|
|
103
|
+
['~']: Expand<CRUDMutatorTypes<TSchema, TTable, TKind, TArgs>>;
|
|
104
|
+
};
|
|
105
|
+
export type CRUDMutatorTypes<TSchema extends Schema, TTable extends keyof TSchema['tables'] & string, TKind extends keyof TableMutator<TSchema['tables'][TTable]>, TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0]> = 'CRUDMutator' & CRUDMutateRequest<TSchema, TTable, TKind, TArgs>;
|
|
106
|
+
export type CRUDMutateRequest<TSchema extends Schema, TTable extends keyof TSchema['tables'], TKind extends keyof TableMutator<TSchema['tables'][TTable]>, TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0]> = {
|
|
107
|
+
readonly schema: TSchema;
|
|
108
|
+
readonly table: TTable;
|
|
109
|
+
readonly kind: TKind;
|
|
110
|
+
readonly args: TArgs;
|
|
111
|
+
};
|
|
112
|
+
export type AnyCRUDMutateRequest = CRUDMutateRequest<any, any, CRUDKind, any>;
|
|
113
|
+
export type TableCRUDMutators<TSchema extends Schema, TTable extends keyof TSchema['tables'] & string> = {
|
|
114
|
+
[K in keyof TableMutator<TSchema['tables'][TTable]>]: CRUDMutator<TSchema, TTable, K, Parameters<TableMutator<TSchema['tables'][TTable]>[K]>[0]>;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Creates a table CRUD builder that returns `CRUDMutateRequest` objects.
|
|
118
|
+
* These request objects can be passed to `tx.mutate(request)`.
|
|
119
|
+
*/
|
|
120
|
+
export declare function makeTableCRUDRequestBuilder<S extends Schema, T extends keyof S['tables'] & string>(schema: S, table: T): TableCRUDMutators<S, T>;
|
|
121
|
+
/**
|
|
122
|
+
* Creates a schema CRUD builder where each table has methods that return
|
|
123
|
+
* `CRUDMutateRequest` objects. These can be passed to `tx.mutate(request)`.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* const crud = createCRUDBuilder(schema);
|
|
129
|
+
*
|
|
130
|
+
* // Inside a custom mutator:
|
|
131
|
+
* await tx.mutate(crud.user.insert({name: 'Alice'}));
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare function createCRUDBuilder<S extends Schema>(schema: S): SchemaCRUDMutators<S>;
|
|
135
|
+
export type SchemaCRUDMutators<S extends Schema> = {
|
|
136
|
+
[T in keyof S['tables'] & string]: TableCRUDMutators<S, T>;
|
|
137
|
+
};
|
|
138
|
+
export {};
|
|
139
|
+
//# sourceMappingURL=crud.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crud.d.ts","sourceRoot":"","sources":["../../../../../zql/src/mutate/crud.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,+BAA+B,CAAC;AAE1D,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,yCAAyC,CAAC;AACjF,OAAO,KAAK,EAAC,MAAM,EAAE,WAAW,EAAC,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC;AAE5C,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,IAAI;KACxC,KAAK,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC;CAC5D,CAAC;AAEF,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,WAAW,IAAI;IAC7C;;;;;OAKG;IACH,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjD;;;;;OAKG;IACH,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjD;;;;OAIG;IACH,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjD;;;OAGG;IACH,MAAM,EAAE,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5C,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,WAAW,C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AAC;AAGF,MAAM,MAAM,oBAAoB,GAAG,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;AAE9E,MAAM,MAAM,iBAAiB,CAC3B,OAAO,SAAS,MAAM,EACtB,MAAM,SAAS,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,MAAM,IAC7C;KACD,CAAC,IAAI,MAAM,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,WAAW,CAC/D,OAAO,EACP,MAAM,EACN,CAAC,EACD,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC1D;CACF,CAAC;AAEF;;;GAGG;AACH,wBAAgB,2BAA2B,CACzC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,EACpC,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,CAO9C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,MAAM,EAAE,CAAC,GACR,kBAAkB,CAAC,CAAC,CAAC,CAYvB;AAED,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,MAAM,IAAI;KAChD,CAAC,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,GAAG,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC;CAC3D,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { recordProxy } from "../../../shared/src/record-proxy.js";
|
|
2
|
+
const CRUD_KINDS = ["insert", "upsert", "update", "delete"];
|
|
3
|
+
function makeMutateCRUDFunction(schema, executor) {
|
|
4
|
+
const mutate = (request) => {
|
|
5
|
+
const { table, kind, args } = request;
|
|
6
|
+
return executor(table, kind, args);
|
|
7
|
+
};
|
|
8
|
+
if (schema.enableLegacyMutators === true) {
|
|
9
|
+
for (const tableName of Object.keys(schema.tables)) {
|
|
10
|
+
mutate[tableName] = void 0;
|
|
11
|
+
}
|
|
12
|
+
return recordProxy(
|
|
13
|
+
mutate,
|
|
14
|
+
(_value, tableName) => makeTableCRUD(tableName, executor)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return mutate;
|
|
18
|
+
}
|
|
19
|
+
function makeTableCRUD(tableName, executor) {
|
|
20
|
+
return Object.fromEntries(
|
|
21
|
+
CRUD_KINDS.map((kind) => [
|
|
22
|
+
kind,
|
|
23
|
+
(value) => executor(tableName, kind, value)
|
|
24
|
+
])
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
function makeTableCRUDRequestBuilder(schema, table) {
|
|
28
|
+
return Object.fromEntries(
|
|
29
|
+
CRUD_KINDS.map((kind) => [
|
|
30
|
+
kind,
|
|
31
|
+
(args) => ({ schema, table, kind, args })
|
|
32
|
+
])
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
function createCRUDBuilder(schema) {
|
|
36
|
+
return recordProxy(
|
|
37
|
+
schema.tables,
|
|
38
|
+
(_tableSchema, tableName) => makeTableCRUDRequestBuilder(
|
|
39
|
+
schema,
|
|
40
|
+
tableName
|
|
41
|
+
),
|
|
42
|
+
(prop) => {
|
|
43
|
+
throw new Error(`Table ${prop} does not exist in schema`);
|
|
44
|
+
}
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
CRUD_KINDS,
|
|
49
|
+
createCRUDBuilder,
|
|
50
|
+
makeMutateCRUDFunction,
|
|
51
|
+
makeTableCRUDRequestBuilder
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=crud.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crud.js","sources":["../../../../../zql/src/mutate/crud.ts"],"sourcesContent":["import type {Expand} from '../../../shared/src/expand.ts';\nimport {recordProxy} from '../../../shared/src/record-proxy.ts';\nimport type {SchemaValueToTSType} from '../../../zero-types/src/schema-value.ts';\nimport type {Schema, TableSchema} from '../../../zero-types/src/schema.ts';\nimport type {MutateCRUD} from './custom.ts';\n\nexport type SchemaCRUD<S extends Schema> = {\n [Table in keyof S['tables']]: TableCRUD<S['tables'][Table]>;\n};\n\nexport type TableCRUD<S extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exist.\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 CRUDKind = keyof TableCRUD<TableSchema>;\n\nexport const CRUD_KINDS = ['insert', 'upsert', 'update', 'delete'] as const;\n\nexport type DeleteID<S extends TableSchema> = Expand<PrimaryKeyFields<S>>;\n\ntype PrimaryKeyFields<S extends TableSchema> = {\n [K in Extract<\n S['primaryKey'][number],\n keyof S['columns']\n >]: SchemaValueToTSType<S['columns'][K]>;\n};\n\nexport type InsertValue<S extends TableSchema> = Expand<\n PrimaryKeyFields<S> & {\n [K in keyof S['columns'] as S['columns'][K] extends {optional: true}\n ? K\n : never]?: SchemaValueToTSType<S['columns'][K]> | undefined;\n } & {\n [K in keyof S['columns'] as S['columns'][K] extends {optional: true}\n ? never\n : K]: SchemaValueToTSType<S['columns'][K]>;\n }\n>;\n\nexport type UpsertValue<S extends TableSchema> = InsertValue<S>;\n\nexport type UpdateValue<S extends TableSchema> = Expand<\n PrimaryKeyFields<S> & {\n [K in keyof S['columns']]?:\n | SchemaValueToTSType<S['columns'][K]>\n | undefined;\n }\n>;\n\n/**\n * This is the type of the generated mutate.<name>.<verb> function.\n */\nexport type TableMutator<TS extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exist.\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<TS>) => 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<TS>) => 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<TS>) => 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<TS>) => Promise<void>;\n};\n\n/**\n * A function that executes a CRUD operation.\n * Client and server provide different implementations.\n */\nexport type CRUDExecutor = (\n table: string,\n kind: CRUDKind,\n args: unknown,\n) => Promise<void>;\n\n/**\n * Creates a MutateCRUD function from a schema and executor.\n * This is the shared implementation used by both client and server.\n *\n * @param schema - The Zero schema\n * @param executor - A function that executes CRUD operations\n * @returns A MutateCRUD function that can be called with CRUDMutateRequest objects\n */\nexport function makeMutateCRUDFunction<S extends Schema>(\n schema: S,\n executor: CRUDExecutor,\n): MutateCRUD<S> {\n // Create a callable function that accepts CRUDMutateRequest\n const mutate = (request: AnyCRUDMutateRequest) => {\n const {table, kind, args} = request;\n return executor(table, kind, args);\n };\n\n // Only add table properties when enableLegacyMutators is true\n if (schema.enableLegacyMutators === true) {\n // Add table names as keys so the proxy can discover them\n for (const tableName of Object.keys(schema.tables)) {\n (mutate as unknown as Record<string, undefined>)[tableName] = undefined;\n }\n\n // Wrap in proxy that lazily creates and caches table CRUD objects\n return recordProxy(\n mutate as unknown as Record<string, undefined>,\n (_value, tableName) => makeTableCRUD(tableName, executor),\n ) as unknown as MutateCRUD<S>;\n }\n\n return mutate as MutateCRUD<S>;\n}\n\n/**\n * Creates a TableCRUD object that delegates to the executor.\n */\nfunction makeTableCRUD(\n tableName: string,\n executor: CRUDExecutor,\n): TableCRUD<TableSchema> {\n return Object.fromEntries(\n CRUD_KINDS.map(kind => [\n kind,\n (value: unknown) => executor(tableName, kind, value),\n ]),\n ) as TableCRUD<TableSchema>;\n}\n\nexport type CRUDMutator<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'] & string,\n TKind extends keyof TableMutator<TSchema['tables'][TTable]>,\n TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0],\n> = {\n (args: TArgs): CRUDMutateRequest<TSchema, TTable, TKind, TArgs>;\n\n /**\n * Type-only phantom property to surface mutator types in a covariant position.\n */\n ['~']: Expand<CRUDMutatorTypes<TSchema, TTable, TKind, TArgs>>;\n};\n\nexport type CRUDMutatorTypes<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'] & string,\n TKind extends keyof TableMutator<TSchema['tables'][TTable]>,\n TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0],\n> = 'CRUDMutator' & CRUDMutateRequest<TSchema, TTable, TKind, TArgs>;\n\nexport type CRUDMutateRequest<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'],\n TKind extends keyof TableMutator<TSchema['tables'][TTable]>,\n TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0],\n> = {\n readonly schema: TSchema;\n readonly table: TTable;\n readonly kind: TKind;\n readonly args: TArgs;\n};\n\n// oxlint-disable-next-line no-explicit-any\nexport type AnyCRUDMutateRequest = CRUDMutateRequest<any, any, CRUDKind, any>;\n\nexport type TableCRUDMutators<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'] & string,\n> = {\n [K in keyof TableMutator<TSchema['tables'][TTable]>]: CRUDMutator<\n TSchema,\n TTable,\n K,\n Parameters<TableMutator<TSchema['tables'][TTable]>[K]>[0]\n >;\n};\n\n/**\n * Creates a table CRUD builder that returns `CRUDMutateRequest` objects.\n * These request objects can be passed to `tx.mutate(request)`.\n */\nexport function makeTableCRUDRequestBuilder<\n S extends Schema,\n T extends keyof S['tables'] & string,\n>(schema: S, table: T): TableCRUDMutators<S, T> {\n return Object.fromEntries(\n CRUD_KINDS.map(kind => [\n kind,\n (args: unknown) => ({schema, table, kind, args}),\n ]),\n ) as TableCRUDMutators<S, T>;\n}\n\n/**\n * Creates a schema CRUD builder where each table has methods that return\n * `CRUDMutateRequest` objects. These can be passed to `tx.mutate(request)`.\n *\n * @example\n *\n * ```ts\n * const crud = createCRUDBuilder(schema);\n *\n * // Inside a custom mutator:\n * await tx.mutate(crud.user.insert({name: 'Alice'}));\n * ```\n */\nexport function createCRUDBuilder<S extends Schema>(\n schema: S,\n): SchemaCRUDMutators<S> {\n return recordProxy(\n schema.tables,\n (_tableSchema, tableName) =>\n makeTableCRUDRequestBuilder(\n schema,\n tableName as keyof S['tables'] & string,\n ),\n prop => {\n throw new Error(`Table ${prop} does not exist in schema`);\n },\n ) as unknown as SchemaCRUDMutators<S>;\n}\n\nexport type SchemaCRUDMutators<S extends Schema> = {\n [T in keyof S['tables'] & string]: TableCRUDMutators<S, T>;\n};\n"],"names":[],"mappings":";AA2CO,MAAM,aAAa,CAAC,UAAU,UAAU,UAAU,QAAQ;AAqF1D,SAAS,uBACd,QACA,UACe;AAEf,QAAM,SAAS,CAAC,YAAkC;AAChD,UAAM,EAAC,OAAO,MAAM,KAAA,IAAQ;AAC5B,WAAO,SAAS,OAAO,MAAM,IAAI;AAAA,EACnC;AAGA,MAAI,OAAO,yBAAyB,MAAM;AAExC,eAAW,aAAa,OAAO,KAAK,OAAO,MAAM,GAAG;AACjD,aAAgD,SAAS,IAAI;AAAA,IAChE;AAGA,WAAO;AAAA,MACL;AAAA,MACA,CAAC,QAAQ,cAAc,cAAc,WAAW,QAAQ;AAAA,IAAA;AAAA,EAE5D;AAEA,SAAO;AACT;AAKA,SAAS,cACP,WACA,UACwB;AACxB,SAAO,OAAO;AAAA,IACZ,WAAW,IAAI,CAAA,SAAQ;AAAA,MACrB;AAAA,MACA,CAAC,UAAmB,SAAS,WAAW,MAAM,KAAK;AAAA,IAAA,CACpD;AAAA,EAAA;AAEL;AAsDO,SAAS,4BAGd,QAAW,OAAmC;AAC9C,SAAO,OAAO;AAAA,IACZ,WAAW,IAAI,CAAA,SAAQ;AAAA,MACrB;AAAA,MACA,CAAC,UAAmB,EAAC,QAAQ,OAAO,MAAM,KAAA;AAAA,IAAI,CAC/C;AAAA,EAAA;AAEL;AAeO,SAAS,kBACd,QACuB;AACvB,SAAO;AAAA,IACL,OAAO;AAAA,IACP,CAAC,cAAc,cACb;AAAA,MACE;AAAA,MACA;AAAA,IAAA;AAAA,IAEJ,CAAA,SAAQ;AACN,YAAM,IAAI,MAAM,SAAS,IAAI,2BAA2B;AAAA,IAC1D;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type { Expand } from '../../../shared/src/expand.ts';
|
|
2
1
|
import type { AST } from '../../../zero-protocol/src/ast.ts';
|
|
3
2
|
import type { DefaultSchema, DefaultWrappedTransaction } from '../../../zero-types/src/default-types.ts';
|
|
4
|
-
import type {
|
|
5
|
-
import type { Schema, TableSchema } from '../../../zero-types/src/schema.ts';
|
|
3
|
+
import type { Schema } from '../../../zero-types/src/schema.ts';
|
|
6
4
|
import type { ServerSchema } from '../../../zero-types/src/server-schema.ts';
|
|
7
5
|
import type { Format } from '../ivm/view.ts';
|
|
8
6
|
import type { HumanReadable, Query, RunOptions } from '../query/query.ts';
|
|
9
7
|
import type { SchemaQuery } from '../query/schema-query.ts';
|
|
8
|
+
import type { CRUDMutateRequest, SchemaCRUD } from './crud.ts';
|
|
9
|
+
export type { DeleteID, InsertValue, SchemaCRUD, TableCRUD, UpdateValue, UpsertValue, } from './crud.ts';
|
|
10
10
|
type ClientID = string;
|
|
11
11
|
/**
|
|
12
12
|
* A base transaction interface that any Transaction<S, T> is assignable to.
|
|
@@ -32,7 +32,7 @@ export interface TransactionBase<S extends Schema> {
|
|
|
32
32
|
* The reason for the transaction.
|
|
33
33
|
*/
|
|
34
34
|
readonly reason: TransactionReason;
|
|
35
|
-
readonly mutate:
|
|
35
|
+
readonly mutate: MutateCRUD<S>;
|
|
36
36
|
readonly query: SchemaQuery<S>;
|
|
37
37
|
run<TTable extends keyof S['tables'] & string, TReturn>(query: Query<TTable, S, TReturn>, options?: RunOptions): Promise<HumanReadable<TReturn>>;
|
|
38
38
|
}
|
|
@@ -64,54 +64,13 @@ export interface DBTransaction<T> extends Queryable {
|
|
|
64
64
|
interface Queryable {
|
|
65
65
|
query: (query: string, args: unknown[]) => Promise<Iterable<Row>>;
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
* and then the default value as defined by the server.
|
|
76
|
-
*/
|
|
77
|
-
insert: (value: InsertValue<S>) => Promise<void>;
|
|
78
|
-
/**
|
|
79
|
-
* Writes a row unconditionally, overwriting any existing row with the same
|
|
80
|
-
* primary key. Non-primary-key fields that are 'optional' can be omitted or
|
|
81
|
-
* set to `undefined`. Such fields will be assigned the value `null`
|
|
82
|
-
* optimistically and then the default value as defined by the server.
|
|
83
|
-
*/
|
|
84
|
-
upsert: (value: UpsertValue<S>) => Promise<void>;
|
|
85
|
-
/**
|
|
86
|
-
* Updates a row with the same primary key. If no such row exists, this
|
|
87
|
-
* function does nothing. All non-primary-key fields can be omitted or set to
|
|
88
|
-
* `undefined`. Such fields will be left unchanged from previous value.
|
|
89
|
-
*/
|
|
90
|
-
update: (value: UpdateValue<S>) => Promise<void>;
|
|
91
|
-
/**
|
|
92
|
-
* Deletes the row with the specified primary key. If no such row exists, this
|
|
93
|
-
* function does nothing.
|
|
94
|
-
*/
|
|
95
|
-
delete: (id: DeleteID<S>) => Promise<void>;
|
|
96
|
-
};
|
|
97
|
-
export type DeleteID<S extends TableSchema> = Expand<PrimaryKeyFields<S>>;
|
|
98
|
-
type PrimaryKeyFields<S extends TableSchema> = {
|
|
99
|
-
[K in Extract<S['primaryKey'][number], keyof S['columns']>]: SchemaValueToTSType<S['columns'][K]>;
|
|
100
|
-
};
|
|
101
|
-
export type InsertValue<S extends TableSchema> = Expand<PrimaryKeyFields<S> & {
|
|
102
|
-
[K in keyof S['columns'] as S['columns'][K] extends {
|
|
103
|
-
optional: true;
|
|
104
|
-
} ? K : never]?: SchemaValueToTSType<S['columns'][K]> | undefined;
|
|
105
|
-
} & {
|
|
106
|
-
[K in keyof S['columns'] as S['columns'][K] extends {
|
|
107
|
-
optional: true;
|
|
108
|
-
} ? never : K]: SchemaValueToTSType<S['columns'][K]>;
|
|
109
|
-
}>;
|
|
110
|
-
export type UpsertValue<S extends TableSchema> = InsertValue<S>;
|
|
111
|
-
export type UpdateValue<S extends TableSchema> = Expand<PrimaryKeyFields<S> & {
|
|
112
|
-
[K in keyof S['columns']]?: SchemaValueToTSType<S['columns'][K]> | undefined;
|
|
113
|
-
}>;
|
|
67
|
+
/**
|
|
68
|
+
* The type of `tx.mutate` which is:
|
|
69
|
+
* 1. A callable function that accepts a `CRUDMutateRequest`
|
|
70
|
+
* 2. When `enableLegacyMutators` is true, also an object with CRUD methods per table
|
|
71
|
+
*/
|
|
72
|
+
export type MutateCRUD<S extends Schema> = {
|
|
73
|
+
(request: CRUDMutateRequest<S, any, any, any>): Promise<void>;
|
|
74
|
+
} & (S['enableLegacyMutators'] extends true ? SchemaCRUD<S> : {});
|
|
114
75
|
export declare function customMutatorKey(sep: string, parts: string[]): string;
|
|
115
|
-
export declare function splitMutatorKey(key: string, sep: string | RegExp): string[];
|
|
116
|
-
export {};
|
|
117
76
|
//# sourceMappingURL=custom.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../../../../zql/src/mutate/custom.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,
|
|
1
|
+
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../../../../zql/src/mutate/custom.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,mCAAmC,CAAC;AAC3D,OAAO,KAAK,EACV,aAAa,EACb,yBAAyB,EAC1B,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,0CAA0C,CAAC;AAC3E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAC,aAAa,EAAE,KAAK,EAAE,UAAU,EAAC,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAC,iBAAiB,EAAE,UAAU,EAAC,MAAM,WAAW,CAAC;AAC7D,YAAY,EACV,QAAQ,EACR,WAAW,EACX,UAAU,EACV,SAAS,EACT,WAAW,EACX,WAAW,GACZ,MAAM,WAAW,CAAC;AAEnB,KAAK,QAAQ,GAAG,MAAM,CAAC;AAEvB;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,iB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CAAC,CAAC;AAElE,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAQ5D"}
|
|
@@ -8,11 +8,7 @@ function customMutatorKey(sep, parts) {
|
|
|
8
8
|
}
|
|
9
9
|
return parts.join(sep);
|
|
10
10
|
}
|
|
11
|
-
function splitMutatorKey(key, sep) {
|
|
12
|
-
return key.split(sep);
|
|
13
|
-
}
|
|
14
11
|
export {
|
|
15
|
-
customMutatorKey
|
|
16
|
-
splitMutatorKey
|
|
12
|
+
customMutatorKey
|
|
17
13
|
};
|
|
18
14
|
//# sourceMappingURL=custom.js.map
|