@rocicorp/zero 0.25.10-canary.4 → 0.25.10-canary.7
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/adapters/prisma.d.ts +2 -0
- package/out/zero/src/adapters/prisma.d.ts.map +1 -0
- package/out/zero/src/adapters/prisma.js +6 -0
- package/out/zero/src/adapters/prisma.js.map +1 -0
- package/out/zero-cache/src/custom/fetch.d.ts +1 -0
- package/out/zero-cache/src/custom/fetch.d.ts.map +1 -1
- package/out/zero-cache/src/custom/fetch.js +3 -0
- package/out/zero-cache/src/custom/fetch.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.d.ts +2 -2
- package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.js +11 -3
- package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +1 -0
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +4 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js +2 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
- package/out/zero-client/src/client/options.d.ts +10 -0
- package/out/zero-client/src/client/options.d.ts.map +1 -1
- package/out/zero-client/src/client/options.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero.d.ts +1 -1
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +8 -2
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-protocol/src/connect.d.ts +4 -0
- package/out/zero-protocol/src/connect.d.ts.map +1 -1
- package/out/zero-protocol/src/connect.js +3 -1
- package/out/zero-protocol/src/connect.js.map +1 -1
- package/out/zero-protocol/src/protocol-version.d.ts +1 -1
- package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
- package/out/zero-protocol/src/protocol-version.js +1 -1
- package/out/zero-protocol/src/protocol-version.js.map +1 -1
- package/out/zero-protocol/src/up.d.ts +2 -0
- package/out/zero-protocol/src/up.d.ts.map +1 -1
- package/out/zero-react/src/zero-provider.js +1 -1
- package/out/zero-react/src/zero-provider.js.map +1 -1
- package/out/zero-server/src/adapters/drizzle.d.ts +18 -13
- package/out/zero-server/src/adapters/drizzle.d.ts.map +1 -1
- package/out/zero-server/src/adapters/drizzle.js.map +1 -1
- package/out/zero-server/src/adapters/pg.d.ts +19 -13
- package/out/zero-server/src/adapters/pg.d.ts.map +1 -1
- package/out/zero-server/src/adapters/pg.js.map +1 -1
- package/out/zero-server/src/adapters/postgresjs.d.ts +19 -13
- package/out/zero-server/src/adapters/postgresjs.d.ts.map +1 -1
- package/out/zero-server/src/adapters/postgresjs.js.map +1 -1
- package/out/zero-server/src/adapters/prisma.d.ts +66 -0
- package/out/zero-server/src/adapters/prisma.d.ts.map +1 -0
- package/out/zero-server/src/adapters/prisma.js +63 -0
- package/out/zero-server/src/adapters/prisma.js.map +1 -0
- package/out/zero-solid/src/use-zero.js +1 -1
- package/out/zero-solid/src/use-zero.js.map +1 -1
- package/package.json +5 -1
|
@@ -2,7 +2,7 @@ import "../../shared/src/valita.js";
|
|
|
2
2
|
import { clientSchemaSchema } from "./client-schema.js";
|
|
3
3
|
import { deleteClientsBodySchema } from "./delete-clients.js";
|
|
4
4
|
import { upQueriesPatchSchema } from "./queries-patch.js";
|
|
5
|
-
import { object, number, string, tuple, literal, array } from "@badrap/valita";
|
|
5
|
+
import { object, number, string, tuple, literal, array, record } from "@badrap/valita";
|
|
6
6
|
const connectedBodySchema = object({
|
|
7
7
|
wsid: string(),
|
|
8
8
|
timestamp: number().optional()
|
|
@@ -21,8 +21,10 @@ const initConnectionBodySchema = object({
|
|
|
21
21
|
deleted: deleteClientsBodySchema.optional(),
|
|
22
22
|
// parameters to configure the mutate endpoint
|
|
23
23
|
userPushURL: string().optional(),
|
|
24
|
+
userPushHeaders: record(string()).optional(),
|
|
24
25
|
// parameters to configure the query endpoint
|
|
25
26
|
userQueryURL: string().optional(),
|
|
27
|
+
userQueryHeaders: record(string()).optional(),
|
|
26
28
|
/**
|
|
27
29
|
* `activeClients` is an optional array of client IDs that are currently active
|
|
28
30
|
* in the client group. This is used to inform the server about the clients
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.js","sources":["../../../../zero-protocol/src/connect.ts"],"sourcesContent":["import * as v from '../../shared/src/valita.ts';\nimport {clientSchemaSchema} from './client-schema.ts';\nimport {deleteClientsBodySchema} from './delete-clients.ts';\nimport {upQueriesPatchSchema} from './queries-patch.ts';\n\n/**\n * After opening a websocket the client waits for a `connected` message\n * from the server. It then sends an `initConnection` message to the\n * server. The server waits for the `initConnection` message before\n * beginning to send pokes to the newly connected client, so as to avoid\n * syncing lots of queries which are no longer desired by the client.\n */\n\nexport const connectedBodySchema = v.object({\n wsid: v.string(),\n timestamp: v.number().optional(),\n});\n\nexport const connectedMessageSchema = v.tuple([\n v.literal('connected'),\n connectedBodySchema,\n]);\n\nconst initConnectionBodySchema = v.object({\n desiredQueriesPatch: upQueriesPatchSchema,\n // As the schema can be large, client only sends when it does not have a\n // server snapshot (i.e. a snapshot with a cookie). Once it has a server\n // snapshot it will assume the zero-cache already has the schema for this\n // client's client group in the CVR store.\n clientSchema: clientSchemaSchema.optional(),\n deleted: deleteClientsBodySchema.optional(),\n // parameters to configure the mutate endpoint\n userPushURL: v.string().optional(),\n // parameters to configure the query endpoint\n userQueryURL: v.string().optional(),\n\n /**\n * `activeClients` is an optional array of client IDs that are currently active\n * in the client group. This is used to inform the server about the clients\n * that are currently active (aka running, aka alive), so it can inactive\n * queries from inactive clients.\n */\n activeClients: v.array(v.string()).optional(),\n});\n\nexport const initConnectionMessageSchema = v.tuple([\n v.literal('initConnection'),\n initConnectionBodySchema,\n]);\n\nexport type ConnectedBody = v.Infer<typeof connectedBodySchema>;\nexport type ConnectedMessage = v.Infer<typeof connectedMessageSchema>;\nexport type InitConnectionBody = v.Infer<typeof initConnectionBodySchema>;\nexport type InitConnectionMessage = v.Infer<typeof initConnectionMessageSchema>;\n\nexport function encodeSecProtocols(\n initConnectionMessage: InitConnectionMessage | undefined,\n authToken: string | undefined,\n): string {\n const protocols = {\n initConnectionMessage,\n authToken,\n };\n // WS sec protocols needs to be URI encoded. To save space, we base64 encode\n // the JSON before URI encoding it. But InitConnectionMessage can contain\n // arbitrary unicode strings, so we need to encode the JSON as UTF-8 first.\n // Phew!\n const bytes = new TextEncoder().encode(JSON.stringify(protocols));\n\n // Convert bytes to string without spreading all bytes as arguments\n // to avoid \"Maximum call stack size exceeded\" error with large data\n const s = Array.from(bytes, byte => String.fromCharCode(byte)).join('');\n\n return encodeURIComponent(btoa(s));\n}\n\nexport function decodeSecProtocols(secProtocol: string): {\n initConnectionMessage: InitConnectionMessage | undefined;\n authToken: string | undefined;\n} {\n const binString = atob(decodeURIComponent(secProtocol));\n const bytes = Uint8Array.from(binString, c => c.charCodeAt(0));\n return JSON.parse(new TextDecoder().decode(bytes));\n}\n"],"names":["v.object","v.string","v.number","v.tuple","v.literal","v.array"],"mappings":";;;;;AAaO,MAAM,sBAAsBA,OAAS;AAAA,EAC1C,MAAMC,OAAE;AAAA,EACR,WAAWC,OAAE,EAAS,SAAA;AACxB,CAAC;AAEM,MAAM,yBAAyBC,MAAQ;AAAA,EAC5CC,QAAU,WAAW;AAAA,EACrB;AACF,CAAC;AAED,MAAM,2BAA2BJ,OAAS;AAAA,EACxC,qBAAqB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKrB,cAAc,mBAAmB,SAAA;AAAA,EACjC,SAAS,wBAAwB,SAAA;AAAA;AAAA,EAEjC,aAAaC,OAAE,EAAS,SAAA;AAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"connect.js","sources":["../../../../zero-protocol/src/connect.ts"],"sourcesContent":["import * as v from '../../shared/src/valita.ts';\nimport {clientSchemaSchema} from './client-schema.ts';\nimport {deleteClientsBodySchema} from './delete-clients.ts';\nimport {upQueriesPatchSchema} from './queries-patch.ts';\n\n/**\n * After opening a websocket the client waits for a `connected` message\n * from the server. It then sends an `initConnection` message to the\n * server. The server waits for the `initConnection` message before\n * beginning to send pokes to the newly connected client, so as to avoid\n * syncing lots of queries which are no longer desired by the client.\n */\n\nexport const connectedBodySchema = v.object({\n wsid: v.string(),\n timestamp: v.number().optional(),\n});\n\nexport const connectedMessageSchema = v.tuple([\n v.literal('connected'),\n connectedBodySchema,\n]);\n\nconst initConnectionBodySchema = v.object({\n desiredQueriesPatch: upQueriesPatchSchema,\n // As the schema can be large, client only sends when it does not have a\n // server snapshot (i.e. a snapshot with a cookie). Once it has a server\n // snapshot it will assume the zero-cache already has the schema for this\n // client's client group in the CVR store.\n clientSchema: clientSchemaSchema.optional(),\n deleted: deleteClientsBodySchema.optional(),\n // parameters to configure the mutate endpoint\n userPushURL: v.string().optional(),\n userPushHeaders: v.record(v.string()).optional(),\n // parameters to configure the query endpoint\n userQueryURL: v.string().optional(),\n userQueryHeaders: v.record(v.string()).optional(),\n\n /**\n * `activeClients` is an optional array of client IDs that are currently active\n * in the client group. This is used to inform the server about the clients\n * that are currently active (aka running, aka alive), so it can inactive\n * queries from inactive clients.\n */\n activeClients: v.array(v.string()).optional(),\n});\n\nexport const initConnectionMessageSchema = v.tuple([\n v.literal('initConnection'),\n initConnectionBodySchema,\n]);\n\nexport type ConnectedBody = v.Infer<typeof connectedBodySchema>;\nexport type ConnectedMessage = v.Infer<typeof connectedMessageSchema>;\nexport type InitConnectionBody = v.Infer<typeof initConnectionBodySchema>;\nexport type InitConnectionMessage = v.Infer<typeof initConnectionMessageSchema>;\n\nexport function encodeSecProtocols(\n initConnectionMessage: InitConnectionMessage | undefined,\n authToken: string | undefined,\n): string {\n const protocols = {\n initConnectionMessage,\n authToken,\n };\n // WS sec protocols needs to be URI encoded. To save space, we base64 encode\n // the JSON before URI encoding it. But InitConnectionMessage can contain\n // arbitrary unicode strings, so we need to encode the JSON as UTF-8 first.\n // Phew!\n const bytes = new TextEncoder().encode(JSON.stringify(protocols));\n\n // Convert bytes to string without spreading all bytes as arguments\n // to avoid \"Maximum call stack size exceeded\" error with large data\n const s = Array.from(bytes, byte => String.fromCharCode(byte)).join('');\n\n return encodeURIComponent(btoa(s));\n}\n\nexport function decodeSecProtocols(secProtocol: string): {\n initConnectionMessage: InitConnectionMessage | undefined;\n authToken: string | undefined;\n} {\n const binString = atob(decodeURIComponent(secProtocol));\n const bytes = Uint8Array.from(binString, c => c.charCodeAt(0));\n return JSON.parse(new TextDecoder().decode(bytes));\n}\n"],"names":["v.object","v.string","v.number","v.tuple","v.literal","v.record","v.array"],"mappings":";;;;;AAaO,MAAM,sBAAsBA,OAAS;AAAA,EAC1C,MAAMC,OAAE;AAAA,EACR,WAAWC,OAAE,EAAS,SAAA;AACxB,CAAC;AAEM,MAAM,yBAAyBC,MAAQ;AAAA,EAC5CC,QAAU,WAAW;AAAA,EACrB;AACF,CAAC;AAED,MAAM,2BAA2BJ,OAAS;AAAA,EACxC,qBAAqB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKrB,cAAc,mBAAmB,SAAA;AAAA,EACjC,SAAS,wBAAwB,SAAA;AAAA;AAAA,EAEjC,aAAaC,OAAE,EAAS,SAAA;AAAA,EACxB,iBAAiBI,OAASJ,OAAE,CAAQ,EAAE,SAAA;AAAA;AAAA,EAEtC,cAAcA,OAAE,EAAS,SAAA;AAAA,EACzB,kBAAkBI,OAASJ,OAAE,CAAQ,EAAE,SAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQvC,eAAeK,MAAQL,OAAE,CAAQ,EAAE,SAAA;AACrC,CAAC;AAEM,MAAM,8BAA8BE,MAAQ;AAAA,EACjDC,QAAU,gBAAgB;AAAA,EAC1B;AACF,CAAC;AAOM,SAAS,mBACd,uBACA,WACQ;AACR,QAAM,YAAY;AAAA,IAChB;AAAA,IACA;AAAA,EAAA;AAMF,QAAM,QAAQ,IAAI,YAAA,EAAc,OAAO,KAAK,UAAU,SAAS,CAAC;AAIhE,QAAM,IAAI,MAAM,KAAK,OAAO,CAAA,SAAQ,OAAO,aAAa,IAAI,CAAC,EAAE,KAAK,EAAE;AAEtE,SAAO,mBAAmB,KAAK,CAAC,CAAC;AACnC;AAEO,SAAS,mBAAmB,aAGjC;AACA,QAAM,YAAY,KAAK,mBAAmB,WAAW,CAAC;AACtD,QAAM,QAAQ,WAAW,KAAK,WAAW,OAAK,EAAE,WAAW,CAAC,CAAC;AAC7D,SAAO,KAAK,MAAM,IAAI,cAAc,OAAO,KAAK,CAAC;AACnD;"}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* release. The server (`zero-cache`) must be deployed before clients start
|
|
11
11
|
* running the new code.
|
|
12
12
|
*/
|
|
13
|
-
export declare const PROTOCOL_VERSION =
|
|
13
|
+
export declare const PROTOCOL_VERSION = 45;
|
|
14
14
|
/**
|
|
15
15
|
* The minimum server-supported sync protocol version (i.e. the version
|
|
16
16
|
* declared in the "/sync/v{#}/connect" URL). The contract for
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol-version.d.ts","sourceRoot":"","sources":["../../../../zero-protocol/src/protocol-version.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;
|
|
1
|
+
{"version":3,"file":"protocol-version.d.ts","sourceRoot":"","sources":["../../../../zero-protocol/src/protocol-version.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AAwCH,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC;;;;;;;;;GASG;AACH,eAAO,MAAM,kCAAkC,KAAK,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol-version.js","sources":["../../../../zero-protocol/src/protocol-version.ts"],"sourcesContent":["import {assert} from '../../shared/src/asserts.ts';\n\n/**\n * The current `PROTOCOL_VERSION` of the code.\n *\n * The `PROTOCOL_VERSION` encompasses both the wire-protocol of the `/sync/...`\n * connection between the browser and `zero-cache`, as well as the format of\n * the `AST` objects stored in both components (i.e. IDB and CVR).\n *\n * A change in the `AST` schema (e.g. new functionality added) must be\n * accompanied by an increment of the `PROTOCOL_VERSION` and a new major\n * release. The server (`zero-cache`) must be deployed before clients start\n * running the new code.\n */\n// History:\n// -- Version 5 adds support for `pokeEnd.cookie`. (0.14)\n// -- Version 6 makes `pokeStart.cookie` optional. (0.16)\n// -- Version 7 introduces the initConnection.clientSchema field. (0.17)\n// -- Version 8 drops support for Version 5 (0.18).\n// -- Version 11 adds inspect queries. (0.18)\n// -- Version 12 adds 'timestamp' and 'date' types to the ClientSchema ValueType. (not shipped, reversed by version 14)\n// -- Version 14 removes 'timestamp' and 'date' types from the ClientSchema ValueType. (0.18)\n// -- Version 15 adds a `userPushParams` field to `initConnection` (0.19)\n// -- Version 16 adds a new error type (alreadyProcessed) to mutation responses (0.19)\n// -- Version 17 deprecates `AST` in downstream query puts. It was never used anyway. (0.21)\n// -- Version 18 adds `name` and `args` to the `queries-patch` protocol (0.21)\n// -- Version 19 adds `activeClients` to the `initConnection` protocol (0.22)\n// -- Version 20 changes inspector down message (0.22)\n// -- Version 21 removes `AST` in downstream query puts which was deprecated in Version 17, removes support for versions < 18 (0.22)\n// -- Version 22 adds an optional 'userQueryParams' field to `initConnection` (0.22)\n// -- Version 23 add `mutationResults` to poke (0.22)\n// -- Version 24 adds `ackMutationResults` to upstream (0.22).\n// -- version 25 modifies `mutationsResults` to include `del` patches (0.22)\n// -- version 26 adds inspect/metrics and adds metrics to inspect/query (0.23)\n// -- version 27 adds inspect/version (0.23)\n// -- version 28 adds more inspect/metrics (0.23)\n// -- version 29 adds error responses for custom queries (0.23)\n// -- version 30 adds an optional primaryKey to the ClientSchema (0.24)\n// -- version 31 adds admin password authentication to inspector RPC calls (0.24)\n// -- version 32 adds analyze-query to the inspector RPC calls (0.24)\n// -- version 33 adds `flip` to CorrelatedSubquery (0.25)\n// -- version 34 moves `flip` from CorrelatedSubquery to CorrelatedSubqueryCondition (0.25)\n// -- version 35 adds `readRows`, `readRowCountsByQuery` and `readRowCount` to analyze-query result (0.25)\n// -- version 36 changes inspector analyze-query and adds error response to RPC (0.25)\n// -- version 37 adds `elapsed` to AnalyzeQueryResult (0.25)\n// -- version 38 adds structured push/transform error responses (0.25)\n// -- version 39 removes per-transform error types and adds `message` to app error (0.25)\n// -- version 40 adds `dbRowScansByQuery` to AnalyzeQueryResult (0.25)\n// -- version 41 makes ClientSchema.primaryKey required (0.25)\n// -- version 42 adds planner events to AnalyzeQueryResult (0.25)\n// -- version 43 renames `plans` to `sqlitePlans`, `plannerEvents` to `joinPlans`, and `plannerDebug` option to `joinPlans` (0.25)\n// -- version 44 adds profileID to connection URL (0.25)\nexport const PROTOCOL_VERSION =
|
|
1
|
+
{"version":3,"file":"protocol-version.js","sources":["../../../../zero-protocol/src/protocol-version.ts"],"sourcesContent":["import {assert} from '../../shared/src/asserts.ts';\n\n/**\n * The current `PROTOCOL_VERSION` of the code.\n *\n * The `PROTOCOL_VERSION` encompasses both the wire-protocol of the `/sync/...`\n * connection between the browser and `zero-cache`, as well as the format of\n * the `AST` objects stored in both components (i.e. IDB and CVR).\n *\n * A change in the `AST` schema (e.g. new functionality added) must be\n * accompanied by an increment of the `PROTOCOL_VERSION` and a new major\n * release. The server (`zero-cache`) must be deployed before clients start\n * running the new code.\n */\n// History:\n// -- Version 5 adds support for `pokeEnd.cookie`. (0.14)\n// -- Version 6 makes `pokeStart.cookie` optional. (0.16)\n// -- Version 7 introduces the initConnection.clientSchema field. (0.17)\n// -- Version 8 drops support for Version 5 (0.18).\n// -- Version 11 adds inspect queries. (0.18)\n// -- Version 12 adds 'timestamp' and 'date' types to the ClientSchema ValueType. (not shipped, reversed by version 14)\n// -- Version 14 removes 'timestamp' and 'date' types from the ClientSchema ValueType. (0.18)\n// -- Version 15 adds a `userPushParams` field to `initConnection` (0.19)\n// -- Version 16 adds a new error type (alreadyProcessed) to mutation responses (0.19)\n// -- Version 17 deprecates `AST` in downstream query puts. It was never used anyway. (0.21)\n// -- Version 18 adds `name` and `args` to the `queries-patch` protocol (0.21)\n// -- Version 19 adds `activeClients` to the `initConnection` protocol (0.22)\n// -- Version 20 changes inspector down message (0.22)\n// -- Version 21 removes `AST` in downstream query puts which was deprecated in Version 17, removes support for versions < 18 (0.22)\n// -- Version 22 adds an optional 'userQueryParams' field to `initConnection` (0.22)\n// -- Version 23 add `mutationResults` to poke (0.22)\n// -- Version 24 adds `ackMutationResults` to upstream (0.22).\n// -- version 25 modifies `mutationsResults` to include `del` patches (0.22)\n// -- version 26 adds inspect/metrics and adds metrics to inspect/query (0.23)\n// -- version 27 adds inspect/version (0.23)\n// -- version 28 adds more inspect/metrics (0.23)\n// -- version 29 adds error responses for custom queries (0.23)\n// -- version 30 adds an optional primaryKey to the ClientSchema (0.24)\n// -- version 31 adds admin password authentication to inspector RPC calls (0.24)\n// -- version 32 adds analyze-query to the inspector RPC calls (0.24)\n// -- version 33 adds `flip` to CorrelatedSubquery (0.25)\n// -- version 34 moves `flip` from CorrelatedSubquery to CorrelatedSubqueryCondition (0.25)\n// -- version 35 adds `readRows`, `readRowCountsByQuery` and `readRowCount` to analyze-query result (0.25)\n// -- version 36 changes inspector analyze-query and adds error response to RPC (0.25)\n// -- version 37 adds `elapsed` to AnalyzeQueryResult (0.25)\n// -- version 38 adds structured push/transform error responses (0.25)\n// -- version 39 removes per-transform error types and adds `message` to app error (0.25)\n// -- version 40 adds `dbRowScansByQuery` to AnalyzeQueryResult (0.25)\n// -- version 41 makes ClientSchema.primaryKey required (0.25)\n// -- version 42 adds planner events to AnalyzeQueryResult (0.25)\n// -- version 43 renames `plans` to `sqlitePlans`, `plannerEvents` to `joinPlans`, and `plannerDebug` option to `joinPlans` (0.25)\n// -- version 44 adds profileID to connection URL (0.25)\n// -- version 45 adds userPushHeaders and userQueryHeaders to initConnection (0.25)\nexport const PROTOCOL_VERSION = 45;\n\n/**\n * The minimum server-supported sync protocol version (i.e. the version\n * declared in the \"/sync/v{#}/connect\" URL). The contract for\n * backwards compatibility is that a `zero-cache` supports the current\n * `PROTOCOL_VERSION` and at least the previous one (i.e. `PROTOCOL_VERSION - 1`)\n * if not earlier ones as well. This corresponds to supporting clients running\n * the current release and the previous (major) release. Any client connections\n * from protocol versions before `MIN_SERVER_SUPPORTED_PROTOCOL_VERSION` are\n * closed with a `VersionNotSupported` error.\n */\nexport const MIN_SERVER_SUPPORTED_SYNC_PROTOCOL = 30;\n\nassert(MIN_SERVER_SUPPORTED_SYNC_PROTOCOL < PROTOCOL_VERSION);\n"],"names":[],"mappings":";AAqDO,MAAM,mBAAmB;AAYzB,MAAM,qCAAqC;AAElD,OAAO,qCAAqC,gBAAgB;"}
|
|
@@ -27,7 +27,9 @@ export declare const upstreamSchema: v.UnionType<[v.TupleType<[v.Type<"initConne
|
|
|
27
27
|
readonly clientGroupIDs?: readonly string[] | undefined;
|
|
28
28
|
}>;
|
|
29
29
|
userPushURL: v.Optional<string>;
|
|
30
|
+
userPushHeaders: v.Optional<Record<string, string>>;
|
|
30
31
|
userQueryURL: v.Optional<string>;
|
|
32
|
+
userQueryHeaders: v.Optional<Record<string, string>>;
|
|
31
33
|
activeClients: v.Optional<string[]>;
|
|
32
34
|
}, undefined>]>, v.TupleType<[v.Type<"ping">, v.ObjectType<{}, undefined>]>, v.TupleType<[v.Type<"deleteClients">, v.UnionType<[v.ObjectType<Readonly<{
|
|
33
35
|
clientIDs: v.Optional<readonly string[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"up.d.ts","sourceRoot":"","sources":["../../../../zero-protocol/src/up.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,4BAA4B,CAAC;AAUhD,eAAO,MAAM,cAAc
|
|
1
|
+
{"version":3,"file":"up.d.ts","sourceRoot":"","sources":["../../../../zero-protocol/src/up.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,4BAA4B,CAAC;AAUhD,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAU1B,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,CAAC"}
|
|
@@ -57,7 +57,7 @@ function ZeroProvider({ children, init, ...props }) {
|
|
|
57
57
|
};
|
|
58
58
|
}, [init, ...keysWithoutAuth]);
|
|
59
59
|
useEffect(() => {
|
|
60
|
-
if (!zero) return;
|
|
60
|
+
if (!zero || isExternalZero) return;
|
|
61
61
|
const authChanged = auth !== prevAuthRef.current;
|
|
62
62
|
if (authChanged) {
|
|
63
63
|
prevAuthRef.current = auth;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zero-provider.js","sources":["../../../../zero-react/src/zero-provider.tsx"],"sourcesContent":["import {\n createContext,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from 'react';\nimport {stringCompare} from '../../shared/src/string-compare.ts';\nimport {\n Zero,\n type CustomMutatorDefs,\n type DefaultContext,\n type DefaultSchema,\n type Schema,\n type ZeroOptions,\n} from './zero.ts';\n\n// oxlint-disable-next-line no-explicit-any\nexport const ZeroContext = createContext<Zero<any, any, any> | undefined>(\n undefined,\n);\n\nexport function useZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(): Zero<S, MD, Context> {\n const zero = useContext(ZeroContext);\n if (zero === undefined) {\n throw new Error('useZero must be used within a ZeroProvider');\n }\n return zero as Zero<S, MD, Context>;\n}\n\n/**\n * @deprecated Use {@linkcode useZero} instead, alongside default types defined with:\n *\n * ```ts\n * declare module '@rocicorp/zero' {\n * interface DefaultTypes {\n * schema: typeof schema;\n * context: Context;\n * }\n * }\n */\nexport function createUseZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>() {\n return () => useZero<S, MD, Context>();\n}\n\nexport type ZeroProviderProps<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n> = (ZeroOptions<S, MD, Context> | {zero: Zero<S, MD, Context>}) & {\n init?: (zero: Zero<S, MD, Context>) => void;\n children: ReactNode;\n};\n\nconst NO_AUTH_SET = Symbol();\n\nexport function ZeroProvider<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>({children, init, ...props}: ZeroProviderProps<S, MD, Context>) {\n const isExternalZero = 'zero' in props;\n\n const [zero, setZero] = useState<Zero<S, MD, Context> | undefined>(\n isExternalZero ? props.zero : undefined,\n );\n\n const auth = 'auth' in props ? props.auth : NO_AUTH_SET;\n const prevAuthRef = useRef<typeof auth>(auth);\n\n const keysWithoutAuth = useMemo(\n () =>\n Object.entries(props)\n .filter(([key]) => key !== 'auth')\n .sort(([a], [b]) => stringCompare(a, b))\n .map(([_, value]) => value),\n [props],\n );\n\n // If Zero is not passed in, we construct it, but only client-side.\n // Zero doesn't really work SSR today so this is usually the right thing.\n // When we support Zero SSR this will either become a breaking change or\n // more likely server support will be opt-in with a new prop on this\n // component.\n useEffect(() => {\n if (isExternalZero) {\n setZero(props.zero);\n return;\n }\n\n const z = new Zero(props);\n init?.(z);\n setZero(z);\n\n return () => {\n void z.close();\n setZero(undefined);\n };\n // we intentionally don't include auth in the dependency array\n // to avoid closing zero when auth changes\n }, [init, ...keysWithoutAuth]);\n\n useEffect(() => {\n if (!zero) return;\n\n const authChanged = auth !== prevAuthRef.current;\n\n if (authChanged) {\n prevAuthRef.current = auth;\n void zero.connection.connect({\n auth: auth === NO_AUTH_SET ? undefined : auth,\n });\n }\n }, [auth, zero]);\n\n return (\n zero && <ZeroContext.Provider value={zero}>{children}</ZeroContext.Provider>\n );\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBO,MAAM,cAAc;AAAA,EACzB;AACF;AAEO,SAAS,UAIU;AACxB,QAAM,OAAO,WAAW,WAAW;AACnC,MAAI,SAAS,QAAW;AACtB,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AACA,SAAO;AACT;AAaO,SAAS,gBAIZ;AACF,SAAO,MAAM,QAAA;AACf;AAWA,MAAM,cAAc,OAAA;AAEb,SAAS,aAId,EAAC,UAAU,MAAM,GAAG,SAA2C;AAC/D,QAAM,iBAAiB,UAAU;AAEjC,QAAM,CAAC,MAAM,OAAO,IAAI;AAAA,IACtB,iBAAiB,MAAM,OAAO;AAAA,EAAA;AAGhC,QAAM,OAAO,UAAU,QAAQ,MAAM,OAAO;AAC5C,QAAM,cAAc,OAAoB,IAAI;AAE5C,QAAM,kBAAkB;AAAA,IACtB,MACE,OAAO,QAAQ,KAAK,EACjB,OAAO,CAAC,CAAC,GAAG,MAAM,QAAQ,MAAM,EAChC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,EACtC,IAAI,CAAC,CAAC,GAAG,KAAK,MAAM,KAAK;AAAA,IAC9B,CAAC,KAAK;AAAA,EAAA;AAQR,YAAU,MAAM;AACd,QAAI,gBAAgB;AAClB,cAAQ,MAAM,IAAI;AAClB;AAAA,IACF;AAEA,UAAM,IAAI,IAAI,KAAK,KAAK;AACxB,WAAO,CAAC;AACR,YAAQ,CAAC;AAET,WAAO,MAAM;AACX,WAAK,EAAE,MAAA;AACP,cAAQ,MAAS;AAAA,IACnB;AAAA,EAGF,GAAG,CAAC,MAAM,GAAG,eAAe,CAAC;AAE7B,YAAU,MAAM;AACd,QAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"zero-provider.js","sources":["../../../../zero-react/src/zero-provider.tsx"],"sourcesContent":["import {\n createContext,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from 'react';\nimport {stringCompare} from '../../shared/src/string-compare.ts';\nimport {\n Zero,\n type CustomMutatorDefs,\n type DefaultContext,\n type DefaultSchema,\n type Schema,\n type ZeroOptions,\n} from './zero.ts';\n\n// oxlint-disable-next-line no-explicit-any\nexport const ZeroContext = createContext<Zero<any, any, any> | undefined>(\n undefined,\n);\n\nexport function useZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(): Zero<S, MD, Context> {\n const zero = useContext(ZeroContext);\n if (zero === undefined) {\n throw new Error('useZero must be used within a ZeroProvider');\n }\n return zero as Zero<S, MD, Context>;\n}\n\n/**\n * @deprecated Use {@linkcode useZero} instead, alongside default types defined with:\n *\n * ```ts\n * declare module '@rocicorp/zero' {\n * interface DefaultTypes {\n * schema: typeof schema;\n * context: Context;\n * }\n * }\n */\nexport function createUseZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>() {\n return () => useZero<S, MD, Context>();\n}\n\nexport type ZeroProviderProps<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n> = (ZeroOptions<S, MD, Context> | {zero: Zero<S, MD, Context>}) & {\n init?: (zero: Zero<S, MD, Context>) => void;\n children: ReactNode;\n};\n\nconst NO_AUTH_SET = Symbol();\n\nexport function ZeroProvider<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>({children, init, ...props}: ZeroProviderProps<S, MD, Context>) {\n const isExternalZero = 'zero' in props;\n\n const [zero, setZero] = useState<Zero<S, MD, Context> | undefined>(\n isExternalZero ? props.zero : undefined,\n );\n\n const auth = 'auth' in props ? props.auth : NO_AUTH_SET;\n const prevAuthRef = useRef<typeof auth>(auth);\n\n const keysWithoutAuth = useMemo(\n () =>\n Object.entries(props)\n .filter(([key]) => key !== 'auth')\n .sort(([a], [b]) => stringCompare(a, b))\n .map(([_, value]) => value),\n [props],\n );\n\n // If Zero is not passed in, we construct it, but only client-side.\n // Zero doesn't really work SSR today so this is usually the right thing.\n // When we support Zero SSR this will either become a breaking change or\n // more likely server support will be opt-in with a new prop on this\n // component.\n useEffect(() => {\n if (isExternalZero) {\n setZero(props.zero);\n return;\n }\n\n const z = new Zero(props);\n init?.(z);\n setZero(z);\n\n return () => {\n void z.close();\n setZero(undefined);\n };\n // we intentionally don't include auth in the dependency array\n // to avoid closing zero when auth changes\n }, [init, ...keysWithoutAuth]);\n\n useEffect(() => {\n if (!zero || isExternalZero) return;\n\n const authChanged = auth !== prevAuthRef.current;\n\n if (authChanged) {\n prevAuthRef.current = auth;\n void zero.connection.connect({\n auth: auth === NO_AUTH_SET ? undefined : auth,\n });\n }\n }, [auth, zero]);\n\n return (\n zero && <ZeroContext.Provider value={zero}>{children}</ZeroContext.Provider>\n );\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBO,MAAM,cAAc;AAAA,EACzB;AACF;AAEO,SAAS,UAIU;AACxB,QAAM,OAAO,WAAW,WAAW;AACnC,MAAI,SAAS,QAAW;AACtB,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AACA,SAAO;AACT;AAaO,SAAS,gBAIZ;AACF,SAAO,MAAM,QAAA;AACf;AAWA,MAAM,cAAc,OAAA;AAEb,SAAS,aAId,EAAC,UAAU,MAAM,GAAG,SAA2C;AAC/D,QAAM,iBAAiB,UAAU;AAEjC,QAAM,CAAC,MAAM,OAAO,IAAI;AAAA,IACtB,iBAAiB,MAAM,OAAO;AAAA,EAAA;AAGhC,QAAM,OAAO,UAAU,QAAQ,MAAM,OAAO;AAC5C,QAAM,cAAc,OAAoB,IAAI;AAE5C,QAAM,kBAAkB;AAAA,IACtB,MACE,OAAO,QAAQ,KAAK,EACjB,OAAO,CAAC,CAAC,GAAG,MAAM,QAAQ,MAAM,EAChC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,EACtC,IAAI,CAAC,CAAC,GAAG,KAAK,MAAM,KAAK;AAAA,IAC9B,CAAC,KAAK;AAAA,EAAA;AAQR,YAAU,MAAM;AACd,QAAI,gBAAgB;AAClB,cAAQ,MAAM,IAAI;AAClB;AAAA,IACF;AAEA,UAAM,IAAI,IAAI,KAAK,KAAK;AACxB,WAAO,CAAC;AACR,YAAQ,CAAC;AAET,WAAO,MAAM;AACX,WAAK,EAAE,MAAA;AACP,cAAQ,MAAS;AAAA,IACnB;AAAA,EAGF,GAAG,CAAC,MAAM,GAAG,eAAe,CAAC;AAE7B,YAAU,MAAM;AACd,QAAI,CAAC,QAAQ,eAAgB;AAE7B,UAAM,cAAc,SAAS,YAAY;AAEzC,QAAI,aAAa;AACf,kBAAY,UAAU;AACtB,WAAK,KAAK,WAAW,QAAQ;AAAA,QAC3B,MAAM,SAAS,cAAc,SAAY;AAAA,MAAA,CAC1C;AAAA,IACH;AAAA,EACF,GAAG,CAAC,MAAM,IAAI,CAAC;AAEf,SACE,QAAQ,oBAAC,YAAY,UAAZ,EAAqB,OAAO,MAAO,UAAS;AAEzD;"}
|
|
@@ -35,24 +35,29 @@ export declare function toIterableRows(result: unknown): Iterable<Row>;
|
|
|
35
35
|
* ```ts
|
|
36
36
|
* import {Pool} from 'pg';
|
|
37
37
|
* import {drizzle} from 'drizzle-orm/node-postgres';
|
|
38
|
-
* import
|
|
38
|
+
* import {defineMutator, defineMutators} from '@rocicorp/zero';
|
|
39
|
+
* import {zeroDrizzle} from '@rocicorp/zero/server/adapters/drizzle';
|
|
40
|
+
* import {z} from 'zod/mini';
|
|
39
41
|
*
|
|
40
42
|
* const pool = new Pool({connectionString: process.env.ZERO_UPSTREAM_DB!});
|
|
41
43
|
* const drizzleDb = drizzle(pool, {schema: drizzleSchema});
|
|
42
|
-
*
|
|
43
44
|
* const zql = zeroDrizzle(schema, drizzleDb);
|
|
44
45
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* )
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* }
|
|
46
|
+
* export const serverMutators = defineMutators({
|
|
47
|
+
* user: {
|
|
48
|
+
* create: defineMutator(
|
|
49
|
+
* z.object({id: z.string(), name: z.string()}),
|
|
50
|
+
* async ({tx, args}) => {
|
|
51
|
+
* if (tx.location !== 'server') {
|
|
52
|
+
* throw new Error('Server-only mutator');
|
|
53
|
+
* }
|
|
54
|
+
* await tx.dbTransaction.wrappedTransaction
|
|
55
|
+
* .insert(drizzleSchema.user)
|
|
56
|
+
* .values({id: args.id, name: args.name, status: 'active'});
|
|
57
|
+
* },
|
|
58
|
+
* ),
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
56
61
|
* ```
|
|
57
62
|
*/
|
|
58
63
|
export declare function zeroDrizzle<TSchema extends Schema, TDrizzle extends DrizzleDatabase>(schema: TSchema, client: TDrizzle): ZQLDatabase<TSchema, DrizzleTransaction<TDrizzle>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drizzle.d.ts","sourceRoot":"","sources":["../../../../../zero-server/src/adapters/drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,KAAK,GAAG,EAAC,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EACV,UAAU,EACV,gBAAgB,EAChB,aAAa,EACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAC,0BAA0B,EAAC,MAAM,uBAAuB,CAAC;AAGtE,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAE9D,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,GAAG,EACJ,MAAM,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EAAE,EAAE,CAAC,EAAE,EAAE,aAAa,CAAC,YAAY,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC;CASd;AAkCD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,CAiBrE;AAUD,wBAAgB,cAAc,CAAC,MAAM,EAAE,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,CAuB7D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,WAAW,CACzB,OAAO,SAAS,MAAM,EACtB,QAAQ,SAAS,eAAe,EAEhC,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,QAAQ,GACf,WAAW,CAAC,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAKpD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drizzle.js","sources":["../../../../../zero-server/src/adapters/drizzle.ts"],"sourcesContent":["import {sql, type SQL} from 'drizzle-orm';\nimport type {\n PgDatabase,\n PgQueryResultHKT,\n PgTransaction,\n} from 'drizzle-orm/pg-core';\nimport type {ExtractTablesWithRelations} from 'drizzle-orm/relations';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\nexport type DrizzleDatabase<\n TQueryResult extends PgQueryResultHKT = PgQueryResultHKT,\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> = PgDatabase<TQueryResult, TSchema>;\n\n/**\n * Helper type for the wrapped transaction used by drizzle-orm.\n *\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, DrizzleTransaction<typeof drizzleDb>>`.\n */\nexport type DrizzleTransaction<\n TDbOrSchema extends DrizzleDatabase | Record<string, unknown>,\n TSchema extends Record<string, unknown> = TDbOrSchema extends PgDatabase<\n PgQueryResultHKT,\n infer TInferredSchema\n >\n ? TInferredSchema\n : TDbOrSchema,\n> = PgTransaction<\n PgQueryResultHKT,\n TSchema,\n ExtractTablesWithRelations<TSchema>\n>;\n\nexport class DrizzleConnection<\n TDrizzle extends DrizzleDatabase,\n TTransaction extends\n DrizzleTransaction<TDrizzle> = DrizzleTransaction<TDrizzle>,\n> implements DBConnection<TTransaction>\n{\n readonly #drizzle: TDrizzle;\n\n constructor(drizzle: TDrizzle) {\n this.#drizzle = drizzle;\n }\n\n transaction<T>(\n fn: (tx: DBTransaction<TTransaction>) => Promise<T>,\n ): Promise<T> {\n return this.#drizzle.transaction(drizzleTx =>\n fn(\n new DrizzleInternalTransaction(\n drizzleTx,\n ) as DBTransaction<TTransaction>,\n ),\n );\n }\n}\n\nclass DrizzleInternalTransaction<\n TTransaction extends DrizzleTransaction<DrizzleDatabase>,\n> implements DBTransaction<TTransaction>\n{\n readonly wrappedTransaction: TTransaction;\n\n constructor(drizzleTx: TTransaction) {\n this.wrappedTransaction = drizzleTx;\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n\n async query(sql: string, params: unknown[]): Promise<Iterable<Row>> {\n const stmt = fromDollarParams(sql, params);\n const result = await this.wrappedTransaction.execute(stmt);\n return toIterableRows(result);\n }\n}\n\n/**\n * Turn `$1, $2...` placeholders into a Drizzle SQL object with bound params.\n */\nexport function fromDollarParams(text: string, params: unknown[]): SQL {\n const re = /\\$(\\d+)/g;\n const s = sql.empty();\n let last = 0;\n let m: RegExpExecArray | null;\n\n while ((m = re.exec(text)) !== null) {\n const idx = Number(m[1]) - 1;\n if (idx < 0 || idx >= params.length) {\n throw new Error(`Missing param for $${m[1]}`);\n }\n if (m.index > last) s.append(sql.raw(text.slice(last, m.index)));\n s.append(sql`${params[idx]}`); // parameterized value\n last = m.index + m[0].length;\n }\n if (last < text.length) s.append(sql.raw(text.slice(last)));\n return s;\n}\n\nfunction isIterable(value: unknown): value is Iterable<unknown> {\n return (\n // oxlint-disable-next-line eqeqeq\n value != null &&\n typeof (value as Iterable<unknown>)[Symbol.iterator] === 'function'\n );\n}\n\nexport function toIterableRows(result: unknown): Iterable<Row> {\n if (result === null || result === undefined) {\n return [] as Row[];\n }\n if (Array.isArray(result)) {\n return result as Row[];\n }\n if (isIterable(result)) {\n return result as Iterable<Row>;\n }\n if (typeof result === 'object') {\n const rows = (result as {rows?: unknown}).rows;\n if (rows === null || rows === undefined) {\n return [] as Row[];\n }\n if (Array.isArray(rows)) {\n return rows as Row[];\n }\n if (isIterable(rows)) {\n return rows as Iterable<Row>;\n }\n }\n throw new TypeError('Drizzle query result is not iterable');\n}\n\n/**\n * Wrap a `drizzle-orm` database for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying drizzle transaction.\n * Use {@link DrizzleTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param client - Drizzle database.\n *\n * @example\n * ```ts\n * import {Pool} from 'pg';\n * import {drizzle} from 'drizzle-orm/node-postgres';\n * import
|
|
1
|
+
{"version":3,"file":"drizzle.js","sources":["../../../../../zero-server/src/adapters/drizzle.ts"],"sourcesContent":["import {sql, type SQL} from 'drizzle-orm';\nimport type {\n PgDatabase,\n PgQueryResultHKT,\n PgTransaction,\n} from 'drizzle-orm/pg-core';\nimport type {ExtractTablesWithRelations} from 'drizzle-orm/relations';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\nexport type DrizzleDatabase<\n TQueryResult extends PgQueryResultHKT = PgQueryResultHKT,\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> = PgDatabase<TQueryResult, TSchema>;\n\n/**\n * Helper type for the wrapped transaction used by drizzle-orm.\n *\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, DrizzleTransaction<typeof drizzleDb>>`.\n */\nexport type DrizzleTransaction<\n TDbOrSchema extends DrizzleDatabase | Record<string, unknown>,\n TSchema extends Record<string, unknown> = TDbOrSchema extends PgDatabase<\n PgQueryResultHKT,\n infer TInferredSchema\n >\n ? TInferredSchema\n : TDbOrSchema,\n> = PgTransaction<\n PgQueryResultHKT,\n TSchema,\n ExtractTablesWithRelations<TSchema>\n>;\n\nexport class DrizzleConnection<\n TDrizzle extends DrizzleDatabase,\n TTransaction extends\n DrizzleTransaction<TDrizzle> = DrizzleTransaction<TDrizzle>,\n> implements DBConnection<TTransaction>\n{\n readonly #drizzle: TDrizzle;\n\n constructor(drizzle: TDrizzle) {\n this.#drizzle = drizzle;\n }\n\n transaction<T>(\n fn: (tx: DBTransaction<TTransaction>) => Promise<T>,\n ): Promise<T> {\n return this.#drizzle.transaction(drizzleTx =>\n fn(\n new DrizzleInternalTransaction(\n drizzleTx,\n ) as DBTransaction<TTransaction>,\n ),\n );\n }\n}\n\nclass DrizzleInternalTransaction<\n TTransaction extends DrizzleTransaction<DrizzleDatabase>,\n> implements DBTransaction<TTransaction>\n{\n readonly wrappedTransaction: TTransaction;\n\n constructor(drizzleTx: TTransaction) {\n this.wrappedTransaction = drizzleTx;\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n\n async query(sql: string, params: unknown[]): Promise<Iterable<Row>> {\n const stmt = fromDollarParams(sql, params);\n const result = await this.wrappedTransaction.execute(stmt);\n return toIterableRows(result);\n }\n}\n\n/**\n * Turn `$1, $2...` placeholders into a Drizzle SQL object with bound params.\n */\nexport function fromDollarParams(text: string, params: unknown[]): SQL {\n const re = /\\$(\\d+)/g;\n const s = sql.empty();\n let last = 0;\n let m: RegExpExecArray | null;\n\n while ((m = re.exec(text)) !== null) {\n const idx = Number(m[1]) - 1;\n if (idx < 0 || idx >= params.length) {\n throw new Error(`Missing param for $${m[1]}`);\n }\n if (m.index > last) s.append(sql.raw(text.slice(last, m.index)));\n s.append(sql`${params[idx]}`); // parameterized value\n last = m.index + m[0].length;\n }\n if (last < text.length) s.append(sql.raw(text.slice(last)));\n return s;\n}\n\nfunction isIterable(value: unknown): value is Iterable<unknown> {\n return (\n // oxlint-disable-next-line eqeqeq\n value != null &&\n typeof (value as Iterable<unknown>)[Symbol.iterator] === 'function'\n );\n}\n\nexport function toIterableRows(result: unknown): Iterable<Row> {\n if (result === null || result === undefined) {\n return [] as Row[];\n }\n if (Array.isArray(result)) {\n return result as Row[];\n }\n if (isIterable(result)) {\n return result as Iterable<Row>;\n }\n if (typeof result === 'object') {\n const rows = (result as {rows?: unknown}).rows;\n if (rows === null || rows === undefined) {\n return [] as Row[];\n }\n if (Array.isArray(rows)) {\n return rows as Row[];\n }\n if (isIterable(rows)) {\n return rows as Iterable<Row>;\n }\n }\n throw new TypeError('Drizzle query result is not iterable');\n}\n\n/**\n * Wrap a `drizzle-orm` database for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying drizzle transaction.\n * Use {@link DrizzleTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param client - Drizzle database.\n *\n * @example\n * ```ts\n * import {Pool} from 'pg';\n * import {drizzle} from 'drizzle-orm/node-postgres';\n * import {defineMutator, defineMutators} from '@rocicorp/zero';\n * import {zeroDrizzle} from '@rocicorp/zero/server/adapters/drizzle';\n * import {z} from 'zod/mini';\n *\n * const pool = new Pool({connectionString: process.env.ZERO_UPSTREAM_DB!});\n * const drizzleDb = drizzle(pool, {schema: drizzleSchema});\n * const zql = zeroDrizzle(schema, drizzleDb);\n *\n * export const serverMutators = defineMutators({\n * user: {\n * create: defineMutator(\n * z.object({id: z.string(), name: z.string()}),\n * async ({tx, args}) => {\n * if (tx.location !== 'server') {\n * throw new Error('Server-only mutator');\n * }\n * await tx.dbTransaction.wrappedTransaction\n * .insert(drizzleSchema.user)\n * .values({id: args.id, name: args.name, status: 'active'});\n * },\n * ),\n * },\n * });\n * ```\n */\nexport function zeroDrizzle<\n TSchema extends Schema,\n TDrizzle extends DrizzleDatabase,\n>(\n schema: TSchema,\n client: TDrizzle,\n): ZQLDatabase<TSchema, DrizzleTransaction<TDrizzle>> {\n return new ZQLDatabase(\n new DrizzleConnection<TDrizzle, DrizzleTransaction<TDrizzle>>(client),\n schema,\n );\n}\n"],"names":["sql"],"mappings":";;;AA8CO,MAAM,kBAKb;AAAA,EACW;AAAA,EAET,YAAY,SAAmB;AAC7B,SAAK,WAAW;AAAA,EAClB;AAAA,EAEA,YACE,IACY;AACZ,WAAO,KAAK,SAAS;AAAA,MAAY,CAAA,cAC/B;AAAA,QACE,IAAI;AAAA,UACF;AAAA,QAAA;AAAA,MACF;AAAA,IACF;AAAA,EAEJ;AACF;AAEA,MAAM,2BAGN;AAAA,EACW;AAAA,EAET,YAAY,WAAyB;AACnC,SAAK,qBAAqB;AAAA,EAC5B;AAAA,EAEA,SACE,KACA,QACA,QACA,cACiC;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAAA,EAEA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I,MAAM,QAAQ,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AACA,QAAI,WAAW,IAAI,GAAG;AACpB,aAAO;AAAA,IACT;AAAA,EACF;AACA,QAAM,IAAI,UAAU,sCAAsC;AAC5D;AAwCO,SAAS,YAId,QACA,QACoD;AACpD,SAAO,IAAI;AAAA,IACT,IAAI,kBAA0D,MAAM;AAAA,IACpE;AAAA,EAAA;AAEJ;"}
|
|
@@ -37,23 +37,29 @@ export declare class NodePgTransactionInternal implements DBTransaction<NodePgTr
|
|
|
37
37
|
* @example
|
|
38
38
|
* ```ts
|
|
39
39
|
* import {Pool} from 'pg';
|
|
40
|
+
* import {defineMutator, defineMutators} from '@rocicorp/zero';
|
|
41
|
+
* import {zeroNodePg} from '@rocicorp/zero/server/adapters/pg';
|
|
42
|
+
* import {z} from 'zod/mini';
|
|
40
43
|
*
|
|
41
44
|
* const pool = new Pool({connectionString: process.env.ZERO_UPSTREAM_DB!});
|
|
42
45
|
* const zql = zeroNodePg(schema, pool);
|
|
43
46
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* )
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
47
|
+
* export const serverMutators = defineMutators({
|
|
48
|
+
* user: {
|
|
49
|
+
* create: defineMutator(
|
|
50
|
+
* z.object({id: z.string(), name: z.string()}),
|
|
51
|
+
* async ({tx, args}) => {
|
|
52
|
+
* if (tx.location !== 'server') {
|
|
53
|
+
* throw new Error('Server-only mutator');
|
|
54
|
+
* }
|
|
55
|
+
* await tx.dbTransaction.wrappedTransaction.query(
|
|
56
|
+
* 'INSERT INTO "user" (id, name, status) VALUES ($1, $2, $3)',
|
|
57
|
+
* [args.id, args.name, 'active'],
|
|
58
|
+
* );
|
|
59
|
+
* },
|
|
60
|
+
* ),
|
|
61
|
+
* },
|
|
62
|
+
* });
|
|
57
63
|
* ```
|
|
58
64
|
*/
|
|
59
65
|
export declare function zeroNodePg<S extends Schema>(schema: S, pg: NodePgTransaction | string): ZQLDatabase<S, NodePgTransaction>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg.d.ts","sourceRoot":"","sources":["../../../../../zero-server/src/adapters/pg.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,IAAI,CAAC;AAC/B,OAAO,EAAC,IAAI,EAAE,KAAK,UAAU,EAAC,MAAM,IAAI,CAAC;AACzC,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,mCAAmC,CAAC;AAC3D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,0CAA0C,CAAC;AAC3E,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,GAAG,EACJ,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,iCAAiC,CAAC;AAEnE,OAAO,EAAC,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAE/C,YAAY,EAAC,WAAW,EAAC,CAAC;AAE1B;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,IAAI,GAAG,UAAU,GAAG,MAAM,CAAC;AAE3D,qBAAa,gBAAiB,YAAW,YAAY,CAAC,iBAAiB,CAAC;;gBAG1D,IAAI,EAAE,iBAAiB;IAI7B,WAAW,CAAC,IAAI,EACpB,EAAE,EAAE,CAAC,EAAE,EAAE,aAAa,CAAC,iBAAiB,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAC1D,OAAO,CAAC,IAAI,CAAC;CAqBjB;AAED,qBAAa,yBACX,YAAW,aAAa,CAAC,iBAAiB,CAAC;IAE3C,QAAQ,CAAC,kBAA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,aAAa,CAAC,iBAAiB,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAC1D,OAAO,CAAC,IAAI,CAAC;CAqBjB;AAED,qBAAa,yBACX,YAAW,aAAa,CAAC,iBAAiB,CAAC;IAE3C,QAAQ,CAAC,kBAAkB,EAAE,iBAAiB,CAAC;gBAEnC,MAAM,EAAE,iBAAiB;IAIrC,QAAQ,CAAC,OAAO,EACd,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACzB,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAU5B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;CAI5D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EACzC,MAAM,EAAE,CAAC,EACT,EAAE,EAAE,iBAAiB,GAAG,MAAM,qCAM/B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg.js","sources":["../../../../../zero-server/src/adapters/pg.ts"],"sourcesContent":["import type {Client} from 'pg';\nimport {Pool, type PoolClient} from 'pg';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\n/**\n * Helper type for the wrapped transaction used by node-postgres.\n *\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, NodePgTransaction>`.\n */\nexport type NodePgTransaction = Pool | PoolClient | Client;\n\nexport class NodePgConnection implements DBConnection<NodePgTransaction> {\n readonly #pool: NodePgTransaction;\n\n constructor(pool: NodePgTransaction) {\n this.#pool = pool;\n }\n\n async transaction<TRet>(\n fn: (tx: DBTransaction<NodePgTransaction>) => Promise<TRet>,\n ): Promise<TRet> {\n const client =\n this.#pool instanceof Pool ? await this.#pool.connect() : this.#pool;\n try {\n await client.query('BEGIN');\n const result = await fn(new NodePgTransactionInternal(client));\n await client.query('COMMIT');\n return result;\n } catch (error) {\n try {\n await client.query('ROLLBACK');\n } catch {\n // ignore rollback error; original error will be thrown\n }\n throw error;\n } finally {\n if (this.#pool instanceof Pool && 'release' in client) {\n client.release();\n }\n }\n }\n}\n\nexport class NodePgTransactionInternal\n implements DBTransaction<NodePgTransaction>\n{\n readonly wrappedTransaction: NodePgTransaction;\n\n constructor(client: NodePgTransaction) {\n this.wrappedTransaction = client;\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n\n async query(sql: string, params: unknown[]): Promise<Row[]> {\n const res = await this.wrappedTransaction.query(sql, params as unknown[]);\n return res.rows as Row[];\n }\n}\n\n/**\n * Wrap a `pg` Pool for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying node-postgres client.\n * Use {@link NodePgTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param pg - `pg` Pool or connection string.\n *\n * @example\n * ```ts\n * import {Pool} from 'pg';\n *\n * const pool = new Pool({connectionString: process.env.ZERO_UPSTREAM_DB!});\n * const zql = zeroNodePg(schema, pool);\n *\n *
|
|
1
|
+
{"version":3,"file":"pg.js","sources":["../../../../../zero-server/src/adapters/pg.ts"],"sourcesContent":["import type {Client} from 'pg';\nimport {Pool, type PoolClient} from 'pg';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\n/**\n * Helper type for the wrapped transaction used by node-postgres.\n *\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, NodePgTransaction>`.\n */\nexport type NodePgTransaction = Pool | PoolClient | Client;\n\nexport class NodePgConnection implements DBConnection<NodePgTransaction> {\n readonly #pool: NodePgTransaction;\n\n constructor(pool: NodePgTransaction) {\n this.#pool = pool;\n }\n\n async transaction<TRet>(\n fn: (tx: DBTransaction<NodePgTransaction>) => Promise<TRet>,\n ): Promise<TRet> {\n const client =\n this.#pool instanceof Pool ? await this.#pool.connect() : this.#pool;\n try {\n await client.query('BEGIN');\n const result = await fn(new NodePgTransactionInternal(client));\n await client.query('COMMIT');\n return result;\n } catch (error) {\n try {\n await client.query('ROLLBACK');\n } catch {\n // ignore rollback error; original error will be thrown\n }\n throw error;\n } finally {\n if (this.#pool instanceof Pool && 'release' in client) {\n client.release();\n }\n }\n }\n}\n\nexport class NodePgTransactionInternal\n implements DBTransaction<NodePgTransaction>\n{\n readonly wrappedTransaction: NodePgTransaction;\n\n constructor(client: NodePgTransaction) {\n this.wrappedTransaction = client;\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n\n async query(sql: string, params: unknown[]): Promise<Row[]> {\n const res = await this.wrappedTransaction.query(sql, params as unknown[]);\n return res.rows as Row[];\n }\n}\n\n/**\n * Wrap a `pg` Pool for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying node-postgres client.\n * Use {@link NodePgTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param pg - `pg` Pool or connection string.\n *\n * @example\n * ```ts\n * import {Pool} from 'pg';\n * import {defineMutator, defineMutators} from '@rocicorp/zero';\n * import {zeroNodePg} from '@rocicorp/zero/server/adapters/pg';\n * import {z} from 'zod/mini';\n *\n * const pool = new Pool({connectionString: process.env.ZERO_UPSTREAM_DB!});\n * const zql = zeroNodePg(schema, pool);\n *\n * export const serverMutators = defineMutators({\n * user: {\n * create: defineMutator(\n * z.object({id: z.string(), name: z.string()}),\n * async ({tx, args}) => {\n * if (tx.location !== 'server') {\n * throw new Error('Server-only mutator');\n * }\n * await tx.dbTransaction.wrappedTransaction.query(\n * 'INSERT INTO \"user\" (id, name, status) VALUES ($1, $2, $3)',\n * [args.id, args.name, 'active'],\n * );\n * },\n * ),\n * },\n * });\n * ```\n */\nexport function zeroNodePg<S extends Schema>(\n schema: S,\n pg: NodePgTransaction | string,\n) {\n if (typeof pg === 'string') {\n pg = new Pool({connectionString: pg});\n }\n return new ZQLDatabase(new NodePgConnection(pg), schema);\n}\n"],"names":[],"mappings":";;;AAwBO,MAAM,iBAA4D;AAAA,EAC9D;AAAA,EAET,YAAY,MAAyB;AACnC,SAAK,QAAQ;AAAA,EACf;AAAA,EAEA,MAAM,YACJ,IACe;AACf,UAAM,SACJ,KAAK,iBAAiB,OAAO,MAAM,KAAK,MAAM,YAAY,KAAK;AACjE,QAAI;AACF,YAAM,OAAO,MAAM,OAAO;AAC1B,YAAM,SAAS,MAAM,GAAG,IAAI,0BAA0B,MAAM,CAAC;AAC7D,YAAM,OAAO,MAAM,QAAQ;AAC3B,aAAO;AAAA,IACT,SAAS,OAAO;AACd,UAAI;AACF,cAAM,OAAO,MAAM,UAAU;AAAA,MAC/B,QAAQ;AAAA,MAER;AACA,YAAM;AAAA,IACR,UAAA;AACE,UAAI,KAAK,iBAAiB,QAAQ,aAAa,QAAQ;AACrD,eAAO,QAAA;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF;AAEO,MAAM,0BAEb;AAAA,EACW;AAAA,EAET,YAAY,QAA2B;AACrC,SAAK,qBAAqB;AAAA,EAC5B;AAAA,EAEA,SACE,KACA,QACA,QACA,cACiC;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAAA,EAEA,MAAM,MAAM,KAAa,QAAmC;AAC1D,UAAM,MAAM,MAAM,KAAK,mBAAmB,MAAM,KAAK,MAAmB;AACxE,WAAO,IAAI;AAAA,EACb;AACF;AAuCO,SAAS,WACd,QACA,IACA;AACA,MAAI,OAAO,OAAO,UAAU;AAC1B,SAAK,IAAI,KAAK,EAAC,kBAAkB,IAAG;AAAA,EACtC;AACA,SAAO,IAAI,YAAY,IAAI,iBAAiB,EAAE,GAAG,MAAM;AACzD;"}
|
|
@@ -37,23 +37,29 @@ export declare class PostgresJsTransactionInternal<T extends Record<string, unkn
|
|
|
37
37
|
* @example
|
|
38
38
|
* ```ts
|
|
39
39
|
* import postgres from 'postgres';
|
|
40
|
+
* import {defineMutator, defineMutators} from '@rocicorp/zero';
|
|
41
|
+
* import {zeroPostgresJS} from '@rocicorp/zero/server/adapters/postgresjs';
|
|
42
|
+
* import {z} from 'zod/mini';
|
|
40
43
|
*
|
|
41
44
|
* const sql = postgres(process.env.ZERO_UPSTREAM_DB!);
|
|
42
45
|
* const zql = zeroPostgresJS(schema, sql);
|
|
43
46
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* )
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
47
|
+
* export const serverMutators = defineMutators({
|
|
48
|
+
* user: {
|
|
49
|
+
* create: defineMutator(
|
|
50
|
+
* z.object({id: z.string(), name: z.string()}),
|
|
51
|
+
* async ({tx, args}) => {
|
|
52
|
+
* if (tx.location !== 'server') {
|
|
53
|
+
* throw new Error('Server-only mutator');
|
|
54
|
+
* }
|
|
55
|
+
* await tx.dbTransaction.wrappedTransaction`
|
|
56
|
+
* INSERT INTO "user" (id, name, status)
|
|
57
|
+
* VALUES (${args.id}, ${args.name}, ${'active'})
|
|
58
|
+
* `;
|
|
59
|
+
* },
|
|
60
|
+
* ),
|
|
61
|
+
* },
|
|
62
|
+
* });
|
|
57
63
|
* ```
|
|
58
64
|
*/
|
|
59
65
|
export declare function zeroPostgresJS<S extends Schema, T extends Record<string, unknown> = Record<string, unknown>>(schema: S, pg: postgres.Sql<T> | string): ZQLDatabase<S, PostgresJsTransaction<T>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgresjs.d.ts","sourceRoot":"","sources":["../../../../../zero-server/src/adapters/postgresjs.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAEhC,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,mCAAmC,CAAC;AAC3D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,0CAA0C,CAAC;AAC3E,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,GAAG,EACJ,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,iCAAiC,CAAC;AAEnE,OAAO,EAAC,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAE/C,YAAY,EAAC,WAAW,EAAC,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,CAC/B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG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E,CAAC;IAIrD,QAAQ,CAAC,OAAO,EACd,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACzB,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;CASnC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,cAAc,CAC5B,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC3D,MAAM,EAAE,CAAC,EAAE,EAAE,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,4CAKxC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgresjs.js","sources":["../../../../../zero-server/src/adapters/postgresjs.ts"],"sourcesContent":["import postgres from 'postgres';\nimport type {JSONValue} from '../../../shared/src/json.ts';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\n/**\n * Helper type for the wrapped transaction used by postgres.js.\n *\n * @typeParam T - The row-shape context bound to the postgres.js client.\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, PostgresJsTransaction>`.\n */\nexport type PostgresJsTransaction<\n T extends Record<string, unknown> = Record<string, unknown>,\n> = postgres.TransactionSql<T>;\n\nexport class PostgresJSConnection<T extends Record<string, unknown>>\n implements DBConnection<PostgresJsTransaction<T>>\n{\n readonly #pg: postgres.Sql<T>;\n constructor(pg: postgres.Sql<T>) {\n this.#pg = pg;\n }\n\n transaction<TRet>(\n fn: (tx: DBTransaction<PostgresJsTransaction<T>>) => Promise<TRet>,\n ): Promise<TRet> {\n return this.#pg.begin(pgTx =>\n fn(new PostgresJsTransactionInternal(pgTx)),\n ) as Promise<TRet>;\n }\n}\n\nexport class PostgresJsTransactionInternal<T extends Record<string, unknown>>\n implements DBTransaction<PostgresJsTransaction<T>>\n{\n readonly wrappedTransaction: PostgresJsTransaction<T>;\n constructor(pgTx: PostgresJsTransaction<T>) {\n this.wrappedTransaction = pgTx;\n }\n\n query(sql: string, params: unknown[]): Promise<Row[]> {\n return this.wrappedTransaction.unsafe(sql, params as JSONValue[]);\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n}\n\n/**\n * Wrap a `postgres` client for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying postgres.js transaction.\n * Use {@link PostgresJsTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param pg - `postgres` client or connection string.\n *\n * @example\n * ```ts\n * import postgres from 'postgres';\n *\n * const sql = postgres(process.env.ZERO_UPSTREAM_DB!);\n * const zql = zeroPostgresJS(schema, sql);\n *\n *
|
|
1
|
+
{"version":3,"file":"postgresjs.js","sources":["../../../../../zero-server/src/adapters/postgresjs.ts"],"sourcesContent":["import postgres from 'postgres';\nimport type {JSONValue} from '../../../shared/src/json.ts';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\n/**\n * Helper type for the wrapped transaction used by postgres.js.\n *\n * @typeParam T - The row-shape context bound to the postgres.js client.\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, PostgresJsTransaction>`.\n */\nexport type PostgresJsTransaction<\n T extends Record<string, unknown> = Record<string, unknown>,\n> = postgres.TransactionSql<T>;\n\nexport class PostgresJSConnection<T extends Record<string, unknown>>\n implements DBConnection<PostgresJsTransaction<T>>\n{\n readonly #pg: postgres.Sql<T>;\n constructor(pg: postgres.Sql<T>) {\n this.#pg = pg;\n }\n\n transaction<TRet>(\n fn: (tx: DBTransaction<PostgresJsTransaction<T>>) => Promise<TRet>,\n ): Promise<TRet> {\n return this.#pg.begin(pgTx =>\n fn(new PostgresJsTransactionInternal(pgTx)),\n ) as Promise<TRet>;\n }\n}\n\nexport class PostgresJsTransactionInternal<T extends Record<string, unknown>>\n implements DBTransaction<PostgresJsTransaction<T>>\n{\n readonly wrappedTransaction: PostgresJsTransaction<T>;\n constructor(pgTx: PostgresJsTransaction<T>) {\n this.wrappedTransaction = pgTx;\n }\n\n query(sql: string, params: unknown[]): Promise<Row[]> {\n return this.wrappedTransaction.unsafe(sql, params as JSONValue[]);\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n}\n\n/**\n * Wrap a `postgres` client for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying postgres.js transaction.\n * Use {@link PostgresJsTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param pg - `postgres` client or connection string.\n *\n * @example\n * ```ts\n * import postgres from 'postgres';\n * import {defineMutator, defineMutators} from '@rocicorp/zero';\n * import {zeroPostgresJS} from '@rocicorp/zero/server/adapters/postgresjs';\n * import {z} from 'zod/mini';\n *\n * const sql = postgres(process.env.ZERO_UPSTREAM_DB!);\n * const zql = zeroPostgresJS(schema, sql);\n *\n * export const serverMutators = defineMutators({\n * user: {\n * create: defineMutator(\n * z.object({id: z.string(), name: z.string()}),\n * async ({tx, args}) => {\n * if (tx.location !== 'server') {\n * throw new Error('Server-only mutator');\n * }\n * await tx.dbTransaction.wrappedTransaction`\n * INSERT INTO \"user\" (id, name, status)\n * VALUES (${args.id}, ${args.name}, ${'active'})\n * `;\n * },\n * ),\n * },\n * });\n * ```\n */\nexport function zeroPostgresJS<\n S extends Schema,\n T extends Record<string, unknown> = Record<string, unknown>,\n>(schema: S, pg: postgres.Sql<T> | string) {\n if (typeof pg === 'string') {\n pg = postgres(pg) as postgres.Sql<T>;\n }\n return new ZQLDatabase(new PostgresJSConnection(pg), schema);\n}\n"],"names":[],"mappings":";;;AA2BO,MAAM,qBAEb;AAAA,EACW;AAAA,EACT,YAAY,IAAqB;AAC/B,SAAK,MAAM;AAAA,EACb;AAAA,EAEA,YACE,IACe;AACf,WAAO,KAAK,IAAI;AAAA,MAAM,CAAA,SACpB,GAAG,IAAI,8BAA8B,IAAI,CAAC;AAAA,IAAA;AAAA,EAE9C;AACF;AAEO,MAAM,8BAEb;AAAA,EACW;AAAA,EACT,YAAY,MAAgC;AAC1C,SAAK,qBAAqB;AAAA,EAC5B;AAAA,EAEA,MAAM,KAAa,QAAmC;AACpD,WAAO,KAAK,mBAAmB,OAAO,KAAK,MAAqB;AAAA,EAClE;AAAA,EAEA,SACE,KACA,QACA,QACA,cACiC;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AACF;AAuCO,SAAS,eAGd,QAAW,IAA8B;AACzC,MAAI,OAAO,OAAO,UAAU;AAC1B,SAAK,SAAS,EAAE;AAAA,EAClB;AACA,SAAO,IAAI,YAAY,IAAI,qBAAqB,EAAE,GAAG,MAAM;AAC7D;"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Schema } from '../../../zero-types/src/schema.ts';
|
|
2
|
+
import type { DBConnection, DBTransaction } from '../../../zql/src/mutate/custom.ts';
|
|
3
|
+
import { ZQLDatabase } from '../zql-database.ts';
|
|
4
|
+
export type { ZQLDatabase };
|
|
5
|
+
export type PrismaTransactionLike = {
|
|
6
|
+
$queryRawUnsafe: (query: string, ...params: unknown[]) => Promise<unknown>;
|
|
7
|
+
};
|
|
8
|
+
export type PrismaClientLike<TTransaction extends PrismaTransactionLike = PrismaTransactionLike> = {
|
|
9
|
+
$transaction: <T>(fn: (tx: TTransaction) => Promise<T>) => Promise<T>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Helper type for the wrapped transaction used by Prisma.
|
|
13
|
+
*
|
|
14
|
+
* @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, PrismaTransaction<typeof prisma>>`.
|
|
15
|
+
*/
|
|
16
|
+
export type PrismaTransaction<TClient extends PrismaClientLike = PrismaClientLike> = TClient extends PrismaClientLike<infer TTransaction> ? TTransaction : PrismaTransactionLike;
|
|
17
|
+
export declare class PrismaConnection<TClient extends PrismaClientLike> implements DBConnection<PrismaTransaction<TClient>> {
|
|
18
|
+
#private;
|
|
19
|
+
constructor(client: TClient);
|
|
20
|
+
transaction<T>(fn: (tx: DBTransaction<PrismaTransaction<TClient>>) => Promise<T>): Promise<T>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Wrap a Prisma client for Zero ZQL.
|
|
24
|
+
*
|
|
25
|
+
* Provides ZQL querying plus access to the underlying Prisma transaction.
|
|
26
|
+
* Use {@link PrismaTransaction} to type your server mutator transaction.
|
|
27
|
+
*
|
|
28
|
+
* @param schema - Zero schema.
|
|
29
|
+
* @param client - Prisma client.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import {PrismaPg} from '@prisma/adapter-pg';
|
|
34
|
+
* import {PrismaClient} from '@prisma/client';
|
|
35
|
+
* import {defineMutator, defineMutators} from '@rocicorp/zero';
|
|
36
|
+
* import {zeroPrisma} from '@rocicorp/zero/server/adapters/prisma';
|
|
37
|
+
* import {z} from 'zod/mini';
|
|
38
|
+
*
|
|
39
|
+
* const prisma = new PrismaClient({
|
|
40
|
+
* adapter: new PrismaPg({connectionString: process.env.ZERO_UPSTREAM_DB!}),
|
|
41
|
+
* });
|
|
42
|
+
* const zql = zeroPrisma(schema, prisma);
|
|
43
|
+
*
|
|
44
|
+
* export const serverMutators = defineMutators({
|
|
45
|
+
* user: {
|
|
46
|
+
* create: defineMutator(
|
|
47
|
+
* z.object({id: z.string(), name: z.string()}),
|
|
48
|
+
* async ({tx, args}) => {
|
|
49
|
+
* if (tx.location !== 'server') {
|
|
50
|
+
* throw new Error('Server-only mutator');
|
|
51
|
+
* }
|
|
52
|
+
* await tx.dbTransaction.wrappedTransaction.user.create({
|
|
53
|
+
* data: {
|
|
54
|
+
* id: args.id,
|
|
55
|
+
* name: args.name,
|
|
56
|
+
* status: 'active',
|
|
57
|
+
* },
|
|
58
|
+
* });
|
|
59
|
+
* },
|
|
60
|
+
* ),
|
|
61
|
+
* },
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function zeroPrisma<TSchema extends Schema, TClient extends PrismaClientLike>(schema: TSchema, client: TClient): ZQLDatabase<TSchema, PrismaTransaction<TClient>>;
|
|
66
|
+
//# sourceMappingURL=prisma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prisma.d.ts","sourceRoot":"","sources":["../../../../../zero-server/src/adapters/prisma.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAE9D,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EAEd,MAAM,mCAAmC,CAAC;AAG3C,OAAO,EAAC,WAAW,E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executor.js";
|
|
2
|
+
import { ZQLDatabase } from "../zql-database.js";
|
|
3
|
+
class PrismaConnection {
|
|
4
|
+
#client;
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.#client = client;
|
|
7
|
+
}
|
|
8
|
+
transaction(fn) {
|
|
9
|
+
return this.#client.$transaction(
|
|
10
|
+
(prismaTx) => fn(
|
|
11
|
+
new PrismaInternalTransaction(prismaTx)
|
|
12
|
+
)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class PrismaInternalTransaction {
|
|
17
|
+
wrappedTransaction;
|
|
18
|
+
constructor(prismaTx) {
|
|
19
|
+
this.wrappedTransaction = prismaTx;
|
|
20
|
+
}
|
|
21
|
+
runQuery(ast, format, schema, serverSchema) {
|
|
22
|
+
return executePostgresQuery(
|
|
23
|
+
this,
|
|
24
|
+
ast,
|
|
25
|
+
format,
|
|
26
|
+
schema,
|
|
27
|
+
serverSchema
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
async query(sql, params) {
|
|
31
|
+
const result = await this.wrappedTransaction.$queryRawUnsafe(
|
|
32
|
+
sql,
|
|
33
|
+
...params
|
|
34
|
+
);
|
|
35
|
+
return toIterableRows(result);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function isIterable(value) {
|
|
39
|
+
return (
|
|
40
|
+
// oxlint-disable-next-line eqeqeq
|
|
41
|
+
value != null && typeof value[Symbol.iterator] === "function"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function toIterableRows(result) {
|
|
45
|
+
if (result === null || result === void 0) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(result)) {
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
if (isIterable(result)) {
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
function zeroPrisma(schema, client) {
|
|
57
|
+
return new ZQLDatabase(new PrismaConnection(client), schema);
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
PrismaConnection,
|
|
61
|
+
zeroPrisma
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=prisma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prisma.js","sources":["../../../../../zero-server/src/adapters/prisma.ts"],"sourcesContent":["import type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {Format} from '../../../zero-types/src/format.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {\n DBConnection,\n DBTransaction,\n Row,\n} from '../../../zql/src/mutate/custom.ts';\nimport type {HumanReadable} from '../../../zql/src/query/query.ts';\nimport {executePostgresQuery} from '../pg-query-executor.ts';\nimport {ZQLDatabase} from '../zql-database.ts';\n\nexport type {ZQLDatabase};\n\nexport type PrismaTransactionLike = {\n $queryRawUnsafe: (query: string, ...params: unknown[]) => Promise<unknown>;\n};\n\nexport type PrismaClientLike<\n TTransaction extends PrismaTransactionLike = PrismaTransactionLike,\n> = {\n $transaction: <T>(fn: (tx: TTransaction) => Promise<T>) => Promise<T>;\n};\n\n/**\n * Helper type for the wrapped transaction used by Prisma.\n *\n * @remarks Use with `ServerTransaction` as `ServerTransaction<Schema, PrismaTransaction<typeof prisma>>`.\n */\nexport type PrismaTransaction<\n TClient extends PrismaClientLike = PrismaClientLike,\n> =\n TClient extends PrismaClientLike<infer TTransaction>\n ? TTransaction\n : PrismaTransactionLike;\n\nexport class PrismaConnection<TClient extends PrismaClientLike>\n implements DBConnection<PrismaTransaction<TClient>>\n{\n readonly #client: TClient;\n\n constructor(client: TClient) {\n this.#client = client;\n }\n\n transaction<T>(\n fn: (tx: DBTransaction<PrismaTransaction<TClient>>) => Promise<T>,\n ): Promise<T> {\n return this.#client.$transaction(prismaTx =>\n fn(\n new PrismaInternalTransaction(prismaTx) as DBTransaction<\n PrismaTransaction<TClient>\n >,\n ),\n );\n }\n}\n\nclass PrismaInternalTransaction<TTransaction extends PrismaTransactionLike>\n implements DBTransaction<TTransaction>\n{\n readonly wrappedTransaction: TTransaction;\n\n constructor(prismaTx: TTransaction) {\n this.wrappedTransaction = prismaTx;\n }\n\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>> {\n return executePostgresQuery<TReturn>(\n this,\n ast,\n format,\n schema,\n serverSchema,\n );\n }\n\n async query(sql: string, params: unknown[]): Promise<Iterable<Row>> {\n const result = await this.wrappedTransaction.$queryRawUnsafe(\n sql,\n ...params,\n );\n return toIterableRows(result);\n }\n}\n\nfunction isIterable(value: unknown): value is Iterable<unknown> {\n return (\n // oxlint-disable-next-line eqeqeq\n value != null &&\n typeof (value as Iterable<unknown>)[Symbol.iterator] === 'function'\n );\n}\n\nfunction toIterableRows(result: unknown): Iterable<Row> {\n if (result === null || result === undefined) {\n return [] as Row[];\n }\n if (Array.isArray(result)) {\n return result as Row[];\n }\n if (isIterable(result)) {\n return result as Iterable<Row>;\n }\n return [] as Row[];\n}\n\n/**\n * Wrap a Prisma client for Zero ZQL.\n *\n * Provides ZQL querying plus access to the underlying Prisma transaction.\n * Use {@link PrismaTransaction} to type your server mutator transaction.\n *\n * @param schema - Zero schema.\n * @param client - Prisma client.\n *\n * @example\n * ```ts\n * import {PrismaPg} from '@prisma/adapter-pg';\n * import {PrismaClient} from '@prisma/client';\n * import {defineMutator, defineMutators} from '@rocicorp/zero';\n * import {zeroPrisma} from '@rocicorp/zero/server/adapters/prisma';\n * import {z} from 'zod/mini';\n *\n * const prisma = new PrismaClient({\n * adapter: new PrismaPg({connectionString: process.env.ZERO_UPSTREAM_DB!}),\n * });\n * const zql = zeroPrisma(schema, prisma);\n *\n * export const serverMutators = defineMutators({\n * user: {\n * create: defineMutator(\n * z.object({id: z.string(), name: z.string()}),\n * async ({tx, args}) => {\n * if (tx.location !== 'server') {\n * throw new Error('Server-only mutator');\n * }\n * await tx.dbTransaction.wrappedTransaction.user.create({\n * data: {\n * id: args.id,\n * name: args.name,\n * status: 'active',\n * },\n * });\n * },\n * ),\n * },\n * });\n * ```\n */\nexport function zeroPrisma<\n TSchema extends Schema,\n TClient extends PrismaClientLike,\n>(\n schema: TSchema,\n client: TClient,\n): ZQLDatabase<TSchema, PrismaTransaction<TClient>> {\n return new ZQLDatabase(new PrismaConnection(client), schema);\n}\n"],"names":[],"mappings":";;AAqCO,MAAM,iBAEb;AAAA,EACW;AAAA,EAET,YAAY,QAAiB;AAC3B,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,YACE,IACY;AACZ,WAAO,KAAK,QAAQ;AAAA,MAAa,CAAA,aAC/B;AAAA,QACE,IAAI,0BAA0B,QAAQ;AAAA,MAAA;AAAA,IAGxC;AAAA,EAEJ;AACF;AAEA,MAAM,0BAEN;AAAA,EACW;AAAA,EAET,YAAY,UAAwB;AAClC,SAAK,qBAAqB;AAAA,EAC5B;AAAA,EAEA,SACE,KACA,QACA,QACA,cACiC;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAAA,EAEA,MAAM,MAAM,KAAa,QAA2C;AAClE,UAAM,SAAS,MAAM,KAAK,mBAAmB;AAAA,MAC3C;AAAA,MACA,GAAG;AAAA,IAAA;AAEL,WAAO,eAAe,MAAM;AAAA,EAC9B;AACF;AAEA,SAAS,WAAW,OAA4C;AAC9D;AAAA;AAAA,IAEE,SAAS,QACT,OAAQ,MAA4B,OAAO,QAAQ,MAAM;AAAA;AAE7D;AAEA,SAAS,eAAe,QAAgC;AACtD,MAAI,WAAW,QAAQ,WAAW,QAAW;AAC3C,WAAO,CAAA;AAAA,EACT;AACA,MAAI,MAAM,QAAQ,MAAM,GAAG;AACzB,WAAO;AAAA,EACT;AACA,MAAI,WAAW,MAAM,GAAG;AACtB,WAAO;AAAA,EACT;AACA,SAAO,CAAA;AACT;AA6CO,SAAS,WAId,QACA,QACkD;AAClD,SAAO,IAAI,YAAY,IAAI,iBAAiB,MAAM,GAAG,MAAM;AAC7D;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-zero.js","sources":["../../../../zero-solid/src/use-zero.ts"],"sourcesContent":["import {\n batch,\n createContext,\n createEffect,\n createMemo,\n onCleanup,\n splitProps,\n untrack,\n useContext,\n type Accessor,\n type JSX,\n} from 'solid-js';\nimport {\n Zero,\n type CustomMutatorDefs,\n type DefaultContext,\n type DefaultSchema,\n type Schema,\n type ZeroOptions,\n} from './zero.ts';\n\nconst ZeroContext = createContext<\n // oxlint-disable-next-line no-explicit-any\n Accessor<Zero<any, any, any>> | undefined\n>(undefined);\n\n/**\n * @deprecated Use {@linkcode ZeroProvider} instead of managing your own Zero instance.\n */\nexport function createZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(options: ZeroOptions<S, MD, Context>): Zero<S, MD, Context> {\n const opts = {\n ...options,\n batchViewUpdates: batch,\n };\n return new Zero(opts);\n}\n\nexport function useZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(): () => Zero<S, MD, Context> {\n const zero = useContext(ZeroContext);\n\n if (zero === undefined) {\n throw new Error('useZero must be used within a ZeroProvider');\n }\n return zero;\n}\n\n/**\n * @deprecated Use {@linkcode useZero} instead, alongside default types defined with:\n *\n * ```ts\n * declare module '@rocicorp/zero' {\n * interface DefaultTypes {\n * schema: typeof schema;\n * context: Context;\n * }\n * }\n */\nexport function createUseZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>() {\n return () => useZero<S, MD, Context>();\n}\n\nconst NO_AUTH_SET = Symbol();\n\nexport function ZeroProvider<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(\n props: {\n children: JSX.Element;\n init?: (zero: Zero<S, MD, Context>) => void;\n } & (\n | {\n zero: Zero<S, MD, Context>;\n }\n | ZeroOptions<S, MD, Context>\n ),\n) {\n const zero = createMemo(() => {\n if ('zero' in props) {\n return props.zero;\n }\n\n const [, options] = splitProps(props, ['children', 'auth']);\n\n const authValue = untrack(() => props.auth);\n const createdZero = new Zero({\n ...options,\n ...(authValue !== undefined ? {auth: authValue} : {}),\n batchViewUpdates: batch,\n });\n options.init?.(createdZero);\n onCleanup(() => createdZero.close());\n return createdZero;\n });\n\n const auth = createMemo<\n typeof NO_AUTH_SET | ZeroOptions<S, MD, Context>['auth']\n >(() => ('auth' in props ? props.auth : NO_AUTH_SET));\n\n let prevAuth: typeof NO_AUTH_SET | ZeroOptions<S, MD, Context>['auth'] =\n NO_AUTH_SET;\n\n createEffect(() => {\n const currentZero = zero();\n if (!currentZero) {\n return;\n }\n\n const currentAuth = auth();\n\n if (prevAuth === NO_AUTH_SET) {\n prevAuth = currentAuth;\n return;\n }\n\n if (currentAuth !== prevAuth) {\n prevAuth = currentAuth;\n void currentZero.connection.connect({\n auth: currentAuth === NO_AUTH_SET ? undefined : currentAuth,\n });\n }\n });\n\n return ZeroContext.Provider({\n value: zero,\n get children() {\n return props.children;\n },\n });\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,MAAM,cAAc,cAGlB,MAAS;AAKJ,SAAS,WAId,SAA4D;AAC5D,QAAM,OAAO;AAAA,IACX,GAAG;AAAA,IACH,kBAAkB;AAAA,EAAA;AAEpB,SAAO,IAAI,KAAK,IAAI;AACtB;AAEO,SAAS,UAIgB;AAC9B,QAAM,OAAO,WAAW,WAAW;AAEnC,MAAI,SAAS,QAAW;AACtB,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AACA,SAAO;AACT;AAaO,SAAS,gBAIZ;AACF,SAAO,MAAM,QAAA;AACf;AAEA,MAAM,cAAc,OAAA;AAEb,SAAS,aAKd,OASA;AACA,QAAM,OAAO,WAAW,MAAM;AAC5B,QAAI,UAAU,OAAO;AACnB,aAAO,MAAM;AAAA,IACf;AAEA,UAAM,CAAA,EAAG,OAAO,IAAI,WAAW,OAAO,CAAC,YAAY,MAAM,CAAC;AAE1D,UAAM,YAAY,QAAQ,MAAM,MAAM,IAAI;AAC1C,UAAM,cAAc,IAAI,KAAK;AAAA,MAC3B,GAAG;AAAA,MACH,GAAI,cAAc,SAAY,EAAC,MAAM,UAAA,IAAa,CAAA;AAAA,MAClD,kBAAkB;AAAA,IAAA,CACnB;AACD,YAAQ,OAAO,WAAW;AAC1B,cAAU,MAAM,YAAY,OAAO;AACnC,WAAO;AAAA,EACT,CAAC;AAED,QAAM,OAAO,WAEX,MAAO,UAAU,QAAQ,MAAM,OAAO,WAAY;AAEpD,MAAI,WACF;AAEF,eAAa,MAAM;AACjB,UAAM,cAAc,KAAA;AACpB,QAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"use-zero.js","sources":["../../../../zero-solid/src/use-zero.ts"],"sourcesContent":["import {\n batch,\n createContext,\n createEffect,\n createMemo,\n onCleanup,\n splitProps,\n untrack,\n useContext,\n type Accessor,\n type JSX,\n} from 'solid-js';\nimport {\n Zero,\n type CustomMutatorDefs,\n type DefaultContext,\n type DefaultSchema,\n type Schema,\n type ZeroOptions,\n} from './zero.ts';\n\nconst ZeroContext = createContext<\n // oxlint-disable-next-line no-explicit-any\n Accessor<Zero<any, any, any>> | undefined\n>(undefined);\n\n/**\n * @deprecated Use {@linkcode ZeroProvider} instead of managing your own Zero instance.\n */\nexport function createZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(options: ZeroOptions<S, MD, Context>): Zero<S, MD, Context> {\n const opts = {\n ...options,\n batchViewUpdates: batch,\n };\n return new Zero(opts);\n}\n\nexport function useZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(): () => Zero<S, MD, Context> {\n const zero = useContext(ZeroContext);\n\n if (zero === undefined) {\n throw new Error('useZero must be used within a ZeroProvider');\n }\n return zero;\n}\n\n/**\n * @deprecated Use {@linkcode useZero} instead, alongside default types defined with:\n *\n * ```ts\n * declare module '@rocicorp/zero' {\n * interface DefaultTypes {\n * schema: typeof schema;\n * context: Context;\n * }\n * }\n */\nexport function createUseZero<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>() {\n return () => useZero<S, MD, Context>();\n}\n\nconst NO_AUTH_SET = Symbol();\n\nexport function ZeroProvider<\n S extends Schema = DefaultSchema,\n MD extends CustomMutatorDefs | undefined = undefined,\n Context = DefaultContext,\n>(\n props: {\n children: JSX.Element;\n init?: (zero: Zero<S, MD, Context>) => void;\n } & (\n | {\n zero: Zero<S, MD, Context>;\n }\n | ZeroOptions<S, MD, Context>\n ),\n) {\n const zero = createMemo(() => {\n if ('zero' in props) {\n return props.zero;\n }\n\n const [, options] = splitProps(props, ['children', 'auth']);\n\n const authValue = untrack(() => props.auth);\n const createdZero = new Zero({\n ...options,\n ...(authValue !== undefined ? {auth: authValue} : {}),\n batchViewUpdates: batch,\n });\n options.init?.(createdZero);\n onCleanup(() => createdZero.close());\n return createdZero;\n });\n\n const auth = createMemo<\n typeof NO_AUTH_SET | ZeroOptions<S, MD, Context>['auth']\n >(() => ('auth' in props ? props.auth : NO_AUTH_SET));\n\n let prevAuth: typeof NO_AUTH_SET | ZeroOptions<S, MD, Context>['auth'] =\n NO_AUTH_SET;\n\n createEffect(() => {\n const currentZero = zero();\n if (!currentZero || 'zero' in props) {\n return;\n }\n\n const currentAuth = auth();\n\n if (prevAuth === NO_AUTH_SET) {\n prevAuth = currentAuth;\n return;\n }\n\n if (currentAuth !== prevAuth) {\n prevAuth = currentAuth;\n void currentZero.connection.connect({\n auth: currentAuth === NO_AUTH_SET ? undefined : currentAuth,\n });\n }\n });\n\n return ZeroContext.Provider({\n value: zero,\n get children() {\n return props.children;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