on-zero 0.7.2 → 0.7.3-1784533014336
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/helpers/createMutators.cjs +4 -3
- package/dist/cjs/helpers/createMutators.native.js +6 -3
- package/dist/cjs/helpers/createMutators.native.js.map +1 -1
- package/dist/cjs/index.cjs +0 -1
- package/dist/cjs/index.native.js +0 -1
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/orez.cjs +231 -0
- package/dist/cjs/orez.native.js +324 -0
- package/dist/cjs/orez.native.js.map +1 -0
- package/dist/cjs/orez.test.cjs +268 -0
- package/dist/cjs/orez.test.native.js +288 -0
- package/dist/cjs/orez.test.native.js.map +1 -0
- package/dist/cjs/run.cjs +1 -1
- package/dist/cjs/run.native.js +1 -1
- package/dist/cjs/state.cjs +1 -1
- package/dist/cjs/state.native.js +1 -1
- package/dist/cjs/zeroRunner.cjs +1 -1
- package/dist/cjs/zeroRunner.native.js +1 -1
- package/dist/esm/cli.native.js +1 -1
- package/dist/esm/cli.native.js.map +1 -1
- package/dist/esm/combineZeroClients.native.js +2 -2
- package/dist/esm/combineZeroClients.native.js.map +1 -1
- package/dist/esm/createPermissions.native.js +4 -4
- package/dist/esm/createPermissions.native.js.map +1 -1
- package/dist/esm/createSchemaFromDrizzle.native.js +1 -1
- package/dist/esm/createSchemaFromDrizzle.native.js.map +1 -1
- package/dist/esm/createUseQuery.native.js +2 -2
- package/dist/esm/createUseQuery.native.js.map +1 -1
- package/dist/esm/createUseQueryDirect.native.js +2 -2
- package/dist/esm/createUseQueryDirect.native.js.map +1 -1
- package/dist/esm/createZeroClient.authData.test.native.js +2 -2
- package/dist/esm/createZeroClient.authData.test.native.js.map +1 -1
- package/dist/esm/createZeroClient.connection.test.native.js +1 -1
- package/dist/esm/createZeroClient.connection.test.native.js.map +1 -1
- package/dist/esm/createZeroClient.native.js +15 -15
- package/dist/esm/createZeroClient.native.js.map +1 -1
- package/dist/esm/createZeroClient.recovery.test.native.js +1 -1
- package/dist/esm/createZeroClient.recovery.test.native.js.map +1 -1
- package/dist/esm/generate-helpers.native.js.map +1 -1
- package/dist/esm/generate-lite.native.js +1 -1
- package/dist/esm/generate-lite.native.js.map +1 -1
- package/dist/esm/generate-lite.test.native.js +1 -1
- package/dist/esm/generate-lite.test.native.js.map +1 -1
- package/dist/esm/generate.native.js +1 -1
- package/dist/esm/generate.native.js.map +1 -1
- package/dist/esm/generate.test.native.js +1 -1
- package/dist/esm/generate.test.native.js.map +1 -1
- package/dist/esm/helpers/batchQuery.native.js.map +1 -1
- package/dist/esm/helpers/clearZeroClientData.native.js.map +1 -1
- package/dist/esm/helpers/createMutators.mjs +4 -3
- package/dist/esm/helpers/createMutators.mjs.map +1 -1
- package/dist/esm/helpers/createMutators.native.js +8 -5
- package/dist/esm/helpers/createMutators.native.js.map +1 -1
- package/dist/esm/helpers/createMutators.test.native.js +1 -1
- package/dist/esm/helpers/createMutators.test.native.js.map +1 -1
- package/dist/esm/helpers/ensureLoggedIn.native.js +1 -1
- package/dist/esm/helpers/ensureLoggedIn.native.js.map +1 -1
- package/dist/esm/helpers/getAuth.native.js +3 -3
- package/dist/esm/helpers/getAuth.native.js.map +1 -1
- package/dist/esm/helpers/mutatorContext.native.js.map +1 -1
- package/dist/esm/helpers/prettyFormatZeroQuery.native.js.map +1 -1
- package/dist/esm/helpers/queryContext.native.js.map +1 -1
- package/dist/esm/helpers/recoverZeroClient.native.js.map +1 -1
- package/dist/esm/helpers/recoverZeroClient.test.native.js +1 -1
- package/dist/esm/helpers/recoverZeroClient.test.native.js.map +1 -1
- package/dist/esm/helpers/showZeroClientError.native.js +1 -1
- package/dist/esm/helpers/showZeroClientError.native.js.map +1 -1
- package/dist/esm/helpers/useMutation.native.js.map +1 -1
- package/dist/esm/helpers/useMutation.test.native.js +1 -1
- package/dist/esm/helpers/useMutation.test.native.js.map +1 -1
- package/dist/esm/helpers/useZeroDebug.native.js +1 -1
- package/dist/esm/helpers/useZeroDebug.native.js.map +1 -1
- package/dist/esm/httpPull/fixtureSchema.native.js.map +1 -1
- package/dist/esm/httpPull/transport.test.native.js +3 -3
- package/dist/esm/httpPull/transport.test.native.js.map +1 -1
- package/dist/esm/httpPullTransport.native.js.map +1 -1
- package/dist/esm/index.js +0 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +0 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +22 -23
- package/dist/esm/index.native.js.map +1 -1
- package/dist/esm/instanceRegistry.native.js +1 -1
- package/dist/esm/instanceRegistry.native.js.map +1 -1
- package/dist/esm/modelRegistry.native.js.map +1 -1
- package/dist/esm/multi.native.js +3 -3
- package/dist/esm/multi.native.js.map +1 -1
- package/dist/esm/multiInstance.test.native.js +8 -8
- package/dist/esm/multiInstance.test.native.js.map +1 -1
- package/dist/esm/multiInstanceNested.test.native.js +3 -3
- package/dist/esm/multiInstanceNested.test.native.js.map +1 -1
- package/dist/esm/multiPartition.test.native.js +1 -1
- package/dist/esm/multiPartition.test.native.js.map +1 -1
- package/dist/esm/mutations.native.js +1 -1
- package/dist/esm/mutations.native.js.map +1 -1
- package/dist/esm/mutations.test.native.js +2 -2
- package/dist/esm/mutations.test.native.js.map +1 -1
- package/dist/esm/orez.mjs +205 -0
- package/dist/esm/orez.mjs.map +1 -0
- package/dist/esm/orez.native.js +295 -0
- package/dist/esm/orez.native.js.map +1 -0
- package/dist/esm/orez.test.mjs +245 -0
- package/dist/esm/orez.test.mjs.map +1 -0
- package/dist/esm/orez.test.native.js +262 -0
- package/dist/esm/orez.test.native.js.map +1 -0
- package/dist/esm/provideZero.hydration.test.native.js +1 -1
- package/dist/esm/provideZero.hydration.test.native.js.map +1 -1
- package/dist/esm/queryRegistry.native.js.map +1 -1
- package/dist/esm/resolveQuery.native.js +1 -1
- package/dist/esm/resolveQuery.native.js.map +1 -1
- package/dist/esm/run.mjs +1 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/run.native.js +5 -5
- package/dist/esm/run.native.js.map +1 -1
- package/dist/esm/serverWhere.native.js +1 -1
- package/dist/esm/serverWhere.native.js.map +1 -1
- package/dist/esm/serverWhere.test.native.js +3 -3
- package/dist/esm/serverWhere.test.native.js.map +1 -1
- package/dist/esm/state.mjs +1 -1
- package/dist/esm/state.native.js +1 -1
- package/dist/esm/state.native.js.map +1 -1
- package/dist/esm/testSetup.native.js.map +1 -1
- package/dist/esm/types.native.js.map +1 -1
- package/dist/esm/usePermission.test.native.js +3 -3
- package/dist/esm/usePermission.test.native.js.map +1 -1
- package/dist/esm/useQuery.empty.test.native.js +2 -2
- package/dist/esm/useQuery.empty.test.native.js.map +1 -1
- package/dist/esm/useQuery.ssr.test.native.js +2 -2
- package/dist/esm/useQuery.ssr.test.native.js.map +1 -1
- package/dist/esm/vite-plugin.native.js +1 -1
- package/dist/esm/vite-plugin.native.js.map +1 -1
- package/dist/esm/where.native.js +2 -2
- package/dist/esm/where.native.js.map +1 -1
- package/dist/esm/zeroRunner.mjs +1 -1
- package/dist/esm/zeroRunner.mjs.map +1 -1
- package/dist/esm/zeroRunner.native.js +2 -2
- package/dist/esm/zeroRunner.native.js.map +1 -1
- package/dist/esm/zql.native.js +1 -1
- package/dist/esm/zql.native.js.map +1 -1
- package/package.json +7 -23
- package/readme.md +40 -65
- package/src/createZeroClient.tsx +1 -1
- package/src/helpers/createMutators.ts +6 -2
- package/src/index.ts +0 -1
- package/src/orez.test.ts +266 -0
- package/src/orez.ts +344 -0
- package/src/run.ts +1 -1
- package/src/state.ts +1 -1
- package/src/types.ts +0 -5
- package/src/zeroRunner.ts +1 -1
- package/test-fixtures/server-return-types.ts +21 -58
- package/types/helpers/createMutators.d.ts +2 -1
- package/types/helpers/createMutators.d.ts.map +1 -1
- package/types/httpPull/fixtureSchema.d.ts +1 -1
- package/types/index.d.ts +0 -1
- package/types/index.d.ts.map +1 -1
- package/types/orez.d.ts +54 -0
- package/types/orez.d.ts.map +1 -0
- package/types/orez.test.d.ts +2 -0
- package/types/orez.test.d.ts.map +1 -0
- package/types/types.d.ts +0 -4
- package/types/types.d.ts.map +1 -1
- package/dist/cjs/asyncTasks.cjs +0 -51
- package/dist/cjs/asyncTasks.native.js +0 -84
- package/dist/cjs/asyncTasks.native.js.map +0 -1
- package/dist/cjs/createZeroDirectServer.cjs +0 -269
- package/dist/cjs/createZeroDirectServer.native.js +0 -287
- package/dist/cjs/createZeroDirectServer.native.js.map +0 -1
- package/dist/cjs/createZeroSQLiteServer.cjs +0 -50
- package/dist/cjs/createZeroSQLiteServer.native.js +0 -58
- package/dist/cjs/createZeroSQLiteServer.native.js.map +0 -1
- package/dist/cjs/createZeroSQLiteServer.test.cjs +0 -595
- package/dist/cjs/createZeroSQLiteServer.test.native.js +0 -647
- package/dist/cjs/createZeroSQLiteServer.test.native.js.map +0 -1
- package/dist/cjs/createZeroServer.cjs +0 -82
- package/dist/cjs/createZeroServer.native.js +0 -93
- package/dist/cjs/createZeroServer.native.js.map +0 -1
- package/dist/cjs/createZeroServer.test.cjs +0 -378
- package/dist/cjs/createZeroServer.test.native.js +0 -448
- package/dist/cjs/createZeroServer.test.native.js.map +0 -1
- package/dist/cjs/createZeroServerCore.cjs +0 -211
- package/dist/cjs/createZeroServerCore.native.js +0 -355
- package/dist/cjs/createZeroServerCore.native.js.map +0 -1
- package/dist/cjs/httpPull/auth.test.cjs +0 -197
- package/dist/cjs/httpPull/auth.test.native.js +0 -279
- package/dist/cjs/httpPull/auth.test.native.js.map +0 -1
- package/dist/cjs/httpPull/churn.test.cjs +0 -132
- package/dist/cjs/httpPull/churn.test.native.js +0 -155
- package/dist/cjs/httpPull/churn.test.native.js.map +0 -1
- package/dist/cjs/httpPull/completeAck.test.cjs +0 -69
- package/dist/cjs/httpPull/completeAck.test.native.js +0 -76
- package/dist/cjs/httpPull/completeAck.test.native.js.map +0 -1
- package/dist/cjs/httpPull/fixtureServer.cjs +0 -340
- package/dist/cjs/httpPull/fixtureServer.native.js +0 -534
- package/dist/cjs/httpPull/fixtureServer.native.js.map +0 -1
- package/dist/cjs/httpPull/integration.test.cjs +0 -53
- package/dist/cjs/httpPull/integration.test.native.js +0 -60
- package/dist/cjs/httpPull/integration.test.native.js.map +0 -1
- package/dist/cjs/httpPull/rebase.test.cjs +0 -360
- package/dist/cjs/httpPull/rebase.test.native.js +0 -420
- package/dist/cjs/httpPull/rebase.test.native.js.map +0 -1
- package/dist/cjs/httpPull/relations.test.cjs +0 -107
- package/dist/cjs/httpPull/relations.test.native.js +0 -119
- package/dist/cjs/httpPull/relations.test.native.js.map +0 -1
- package/dist/cjs/httpPull/testHarness.cjs +0 -100
- package/dist/cjs/httpPull/testHarness.native.js +0 -112
- package/dist/cjs/httpPull/testHarness.native.js.map +0 -1
- package/dist/cjs/server.cjs +0 -22
- package/dist/cjs/server.native.js +0 -4
- package/dist/cjs/server.native.js.map +0 -1
- package/dist/cjs/sqliteRuntime.cjs +0 -318
- package/dist/cjs/sqliteRuntime.native.js +0 -438
- package/dist/cjs/sqliteRuntime.native.js.map +0 -1
- package/dist/cjs/syncHost.cjs +0 -179
- package/dist/cjs/syncHost.native.js +0 -388
- package/dist/cjs/syncHost.native.js.map +0 -1
- package/dist/cjs/syncHost.test.cjs +0 -240
- package/dist/cjs/syncHost.test.native.js +0 -290
- package/dist/cjs/syncHost.test.native.js.map +0 -1
- package/dist/esm/asyncTasks.mjs +0 -26
- package/dist/esm/asyncTasks.mjs.map +0 -1
- package/dist/esm/asyncTasks.native.js +0 -56
- package/dist/esm/asyncTasks.native.js.map +0 -1
- package/dist/esm/createZeroDirectServer.mjs +0 -241
- package/dist/esm/createZeroDirectServer.mjs.map +0 -1
- package/dist/esm/createZeroDirectServer.native.js +0 -256
- package/dist/esm/createZeroDirectServer.native.js.map +0 -1
- package/dist/esm/createZeroSQLiteServer.mjs +0 -24
- package/dist/esm/createZeroSQLiteServer.mjs.map +0 -1
- package/dist/esm/createZeroSQLiteServer.native.js +0 -29
- package/dist/esm/createZeroSQLiteServer.native.js.map +0 -1
- package/dist/esm/createZeroSQLiteServer.test.mjs +0 -596
- package/dist/esm/createZeroSQLiteServer.test.mjs.map +0 -1
- package/dist/esm/createZeroSQLiteServer.test.native.js +0 -645
- package/dist/esm/createZeroSQLiteServer.test.native.js.map +0 -1
- package/dist/esm/createZeroServer.mjs +0 -57
- package/dist/esm/createZeroServer.mjs.map +0 -1
- package/dist/esm/createZeroServer.native.js +0 -65
- package/dist/esm/createZeroServer.native.js.map +0 -1
- package/dist/esm/createZeroServer.test.mjs +0 -379
- package/dist/esm/createZeroServer.test.mjs.map +0 -1
- package/dist/esm/createZeroServer.test.native.js +0 -446
- package/dist/esm/createZeroServer.test.native.js.map +0 -1
- package/dist/esm/createZeroServerCore.mjs +0 -186
- package/dist/esm/createZeroServerCore.mjs.map +0 -1
- package/dist/esm/createZeroServerCore.native.js +0 -327
- package/dist/esm/createZeroServerCore.native.js.map +0 -1
- package/dist/esm/httpPull/auth.test.mjs +0 -198
- package/dist/esm/httpPull/auth.test.mjs.map +0 -1
- package/dist/esm/httpPull/auth.test.native.js +0 -277
- package/dist/esm/httpPull/auth.test.native.js.map +0 -1
- package/dist/esm/httpPull/churn.test.mjs +0 -133
- package/dist/esm/httpPull/churn.test.mjs.map +0 -1
- package/dist/esm/httpPull/churn.test.native.js +0 -153
- package/dist/esm/httpPull/churn.test.native.js.map +0 -1
- package/dist/esm/httpPull/completeAck.test.mjs +0 -70
- package/dist/esm/httpPull/completeAck.test.mjs.map +0 -1
- package/dist/esm/httpPull/completeAck.test.native.js +0 -74
- package/dist/esm/httpPull/completeAck.test.native.js.map +0 -1
- package/dist/esm/httpPull/fixtureServer.mjs +0 -315
- package/dist/esm/httpPull/fixtureServer.mjs.map +0 -1
- package/dist/esm/httpPull/fixtureServer.native.js +0 -506
- package/dist/esm/httpPull/fixtureServer.native.js.map +0 -1
- package/dist/esm/httpPull/integration.test.mjs +0 -54
- package/dist/esm/httpPull/integration.test.mjs.map +0 -1
- package/dist/esm/httpPull/integration.test.native.js +0 -58
- package/dist/esm/httpPull/integration.test.native.js.map +0 -1
- package/dist/esm/httpPull/rebase.test.mjs +0 -361
- package/dist/esm/httpPull/rebase.test.mjs.map +0 -1
- package/dist/esm/httpPull/rebase.test.native.js +0 -418
- package/dist/esm/httpPull/rebase.test.native.js.map +0 -1
- package/dist/esm/httpPull/relations.test.mjs +0 -108
- package/dist/esm/httpPull/relations.test.mjs.map +0 -1
- package/dist/esm/httpPull/relations.test.native.js +0 -117
- package/dist/esm/httpPull/relations.test.native.js.map +0 -1
- package/dist/esm/httpPull/testHarness.mjs +0 -72
- package/dist/esm/httpPull/testHarness.mjs.map +0 -1
- package/dist/esm/httpPull/testHarness.native.js +0 -81
- package/dist/esm/httpPull/testHarness.native.js.map +0 -1
- package/dist/esm/server.mjs +0 -4
- package/dist/esm/server.mjs.map +0 -1
- package/dist/esm/server.native.js +0 -2
- package/dist/esm/server.native.js.map +0 -1
- package/dist/esm/sqliteRuntime.mjs +0 -289
- package/dist/esm/sqliteRuntime.mjs.map +0 -1
- package/dist/esm/sqliteRuntime.native.js +0 -406
- package/dist/esm/sqliteRuntime.native.js.map +0 -1
- package/dist/esm/syncHost.mjs +0 -153
- package/dist/esm/syncHost.mjs.map +0 -1
- package/dist/esm/syncHost.native.js +0 -359
- package/dist/esm/syncHost.native.js.map +0 -1
- package/dist/esm/syncHost.test.mjs +0 -217
- package/dist/esm/syncHost.test.mjs.map +0 -1
- package/dist/esm/syncHost.test.native.js +0 -264
- package/dist/esm/syncHost.test.native.js.map +0 -1
- package/src/asyncTasks.ts +0 -36
- package/src/createZeroDirectServer.ts +0 -411
- package/src/createZeroSQLiteServer.test.ts +0 -614
- package/src/createZeroSQLiteServer.ts +0 -56
- package/src/createZeroServer.test.ts +0 -421
- package/src/createZeroServer.ts +0 -105
- package/src/createZeroServerCore.ts +0 -296
- package/src/httpPull/auth.test.ts +0 -208
- package/src/httpPull/churn.test.ts +0 -147
- package/src/httpPull/completeAck.test.ts +0 -80
- package/src/httpPull/fixtureServer.ts +0 -391
- package/src/httpPull/integration.test.ts +0 -57
- package/src/httpPull/rebase.test.ts +0 -368
- package/src/httpPull/relations.test.ts +0 -135
- package/src/httpPull/testHarness.ts +0 -95
- package/src/server.native.ts +0 -3
- package/src/server.ts +0 -4
- package/src/sqliteRuntime.ts +0 -512
- package/src/syncHost.test.ts +0 -223
- package/src/syncHost.ts +0 -294
- package/types/asyncTasks.d.ts +0 -11
- package/types/asyncTasks.d.ts.map +0 -1
- package/types/createZeroDirectServer.d.ts +0 -80
- package/types/createZeroDirectServer.d.ts.map +0 -1
- package/types/createZeroSQLiteServer.d.ts +0 -12
- package/types/createZeroSQLiteServer.d.ts.map +0 -1
- package/types/createZeroSQLiteServer.test.d.ts +0 -2
- package/types/createZeroSQLiteServer.test.d.ts.map +0 -1
- package/types/createZeroServer.d.ts +0 -18
- package/types/createZeroServer.d.ts.map +0 -1
- package/types/createZeroServer.test.d.ts +0 -2
- package/types/createZeroServer.test.d.ts.map +0 -1
- package/types/createZeroServerCore.d.ts +0 -256
- package/types/createZeroServerCore.d.ts.map +0 -1
- package/types/httpPull/auth.test.d.ts +0 -2
- package/types/httpPull/auth.test.d.ts.map +0 -1
- package/types/httpPull/churn.test.d.ts +0 -2
- package/types/httpPull/churn.test.d.ts.map +0 -1
- package/types/httpPull/completeAck.test.d.ts +0 -2
- package/types/httpPull/completeAck.test.d.ts.map +0 -1
- package/types/httpPull/fixtureServer.d.ts +0 -14
- package/types/httpPull/fixtureServer.d.ts.map +0 -1
- package/types/httpPull/integration.test.d.ts +0 -2
- package/types/httpPull/integration.test.d.ts.map +0 -1
- package/types/httpPull/rebase.test.d.ts +0 -2
- package/types/httpPull/rebase.test.d.ts.map +0 -1
- package/types/httpPull/relations.test.d.ts +0 -2
- package/types/httpPull/relations.test.d.ts.map +0 -1
- package/types/httpPull/testHarness.d.ts +0 -32
- package/types/httpPull/testHarness.d.ts.map +0 -1
- package/types/server.d.ts +0 -5
- package/types/server.d.ts.map +0 -1
- package/types/server.native.d.ts +0 -1
- package/types/server.native.d.ts.map +0 -1
- package/types/sqliteRuntime.d.ts +0 -71
- package/types/sqliteRuntime.d.ts.map +0 -1
- package/types/syncHost.d.ts +0 -42
- package/types/syncHost.d.ts.map +0 -1
- package/types/syncHost.test.d.ts +0 -2
- package/types/syncHost.test.d.ts.map +0 -1
package/src/orez.test.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { DatabaseSync, type SQLInputValue } from 'node:sqlite'
|
|
2
|
+
|
|
3
|
+
import { createSchema, defineQueries, defineQuery, string, table } from '@rocicorp/zero'
|
|
4
|
+
import {
|
|
5
|
+
createSyncExecutor,
|
|
6
|
+
type ApplicationDatabase,
|
|
7
|
+
type ApplicationTransaction,
|
|
8
|
+
} from 'orez/sync-executor'
|
|
9
|
+
import * as v from 'valibot'
|
|
10
|
+
import { afterEach, describe, expect, test } from 'vitest'
|
|
11
|
+
|
|
12
|
+
import { authDataToClaims, createOrezBindings } from './orez'
|
|
13
|
+
import { getScopedAuthData } from './helpers/mutatorContext'
|
|
14
|
+
import { zql } from './zql'
|
|
15
|
+
|
|
16
|
+
import type { AuthData, MutatorContext } from './types'
|
|
17
|
+
|
|
18
|
+
const project = table('project')
|
|
19
|
+
.columns({
|
|
20
|
+
id: string(),
|
|
21
|
+
ownerId: string(),
|
|
22
|
+
name: string(),
|
|
23
|
+
})
|
|
24
|
+
.primaryKey('id')
|
|
25
|
+
|
|
26
|
+
const schema = createSchema({
|
|
27
|
+
tables: [project],
|
|
28
|
+
relationships: [],
|
|
29
|
+
enableLegacyQueries: true,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const queries = defineQueries({
|
|
33
|
+
project: {
|
|
34
|
+
byOwner: defineQuery(v.object({ ownerId: v.string() }), ({ args }) =>
|
|
35
|
+
zql.project.where('ownerId', args.ownerId),
|
|
36
|
+
),
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('createOrezBindings', () => {
|
|
41
|
+
const databases: DatabaseSync[] = []
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
while (databases.length) databases.pop()!.close()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('an anonymous push keeps authData null so auth-required mutators reject', async () => {
|
|
48
|
+
const sqlite = new DatabaseSync(':memory:')
|
|
49
|
+
databases.push(sqlite)
|
|
50
|
+
sqlite.exec(
|
|
51
|
+
'CREATE TABLE project (id TEXT PRIMARY KEY, ownerId TEXT NOT NULL, name TEXT NOT NULL)',
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
let seenAuthData: AuthData | null | undefined
|
|
55
|
+
const bindings = createOrezBindings({
|
|
56
|
+
schema,
|
|
57
|
+
models: {
|
|
58
|
+
project: {
|
|
59
|
+
mutate: {
|
|
60
|
+
create: async (
|
|
61
|
+
{ authData, tx }: MutatorContext,
|
|
62
|
+
value: { id: string; ownerId: string; name: string },
|
|
63
|
+
) => {
|
|
64
|
+
seenAuthData = authData
|
|
65
|
+
// the guard every takeout mutation relies on
|
|
66
|
+
if (!authData) throw new Error('not authenticated')
|
|
67
|
+
await tx.mutate.project.insert(value)
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
createServerActions: () => ({}),
|
|
73
|
+
queries,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const executor = createSyncExecutor({
|
|
77
|
+
database: sqliteApplicationDatabase(sqlite),
|
|
78
|
+
schema,
|
|
79
|
+
mutators: bindings.mutators,
|
|
80
|
+
effects: {
|
|
81
|
+
runBackground(promise) {
|
|
82
|
+
void promise
|
|
83
|
+
},
|
|
84
|
+
report(error) {
|
|
85
|
+
throw error
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// how a rejected mutator surfaces (app result vs thrown) is the executor's
|
|
91
|
+
// contract and is covered in orez; what this pins is that an anonymous push
|
|
92
|
+
// never receives an authData, so the guard runs at all
|
|
93
|
+
await executor
|
|
94
|
+
.push(
|
|
95
|
+
{
|
|
96
|
+
clientGroupID: 'group-anon',
|
|
97
|
+
mutations: [
|
|
98
|
+
{
|
|
99
|
+
type: 'custom',
|
|
100
|
+
id: 1,
|
|
101
|
+
clientID: 'client-anon',
|
|
102
|
+
name: 'project|create',
|
|
103
|
+
args: [{ id: 'project-1', ownerId: 'anon', name: 'sneaky' }],
|
|
104
|
+
timestamp: 1,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
pushVersion: 1,
|
|
108
|
+
requestID: 'request-anon',
|
|
109
|
+
},
|
|
110
|
+
authDataToClaims(null),
|
|
111
|
+
)
|
|
112
|
+
.catch(() => undefined)
|
|
113
|
+
|
|
114
|
+
expect(seenAuthData).toBe(null)
|
|
115
|
+
expect(sqlite.prepare('SELECT * FROM project').all()).toEqual([])
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('binds auth, queries, pipe keys, effects, and replay to one Orez executor', async () => {
|
|
119
|
+
const sqlite = new DatabaseSync(':memory:')
|
|
120
|
+
databases.push(sqlite)
|
|
121
|
+
sqlite.exec(
|
|
122
|
+
'CREATE TABLE project (id TEXT PRIMARY KEY, ownerId TEXT NOT NULL, name TEXT NOT NULL)',
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
let mutationRuns = 0
|
|
126
|
+
let validatedMutationAuth: string | undefined
|
|
127
|
+
let validatedQueryAuth: string | undefined
|
|
128
|
+
let barrierRuns = 0
|
|
129
|
+
let backgroundRuns = 0
|
|
130
|
+
let backgroundAuth: string | undefined
|
|
131
|
+
const scheduledBackground: Promise<void>[] = []
|
|
132
|
+
|
|
133
|
+
const bindings = createOrezBindings({
|
|
134
|
+
schema,
|
|
135
|
+
models: {
|
|
136
|
+
project: {
|
|
137
|
+
mutate: {
|
|
138
|
+
create: async (
|
|
139
|
+
{ authData, server, tx }: MutatorContext,
|
|
140
|
+
value: { id: string; ownerId: string; name: string },
|
|
141
|
+
) => {
|
|
142
|
+
mutationRuns++
|
|
143
|
+
expect(authData?.id).toBe('user-1')
|
|
144
|
+
await tx.mutate.project.insert(value)
|
|
145
|
+
server?.enqueueTask(
|
|
146
|
+
async () => {
|
|
147
|
+
barrierRuns++
|
|
148
|
+
},
|
|
149
|
+
{ barrier: true },
|
|
150
|
+
)
|
|
151
|
+
server?.enqueueTask(async () => {
|
|
152
|
+
backgroundRuns++
|
|
153
|
+
backgroundAuth = getScopedAuthData()?.id as string | undefined
|
|
154
|
+
})
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
createServerActions: () => ({}),
|
|
160
|
+
queries,
|
|
161
|
+
validateMutation({ authData }) {
|
|
162
|
+
validatedMutationAuth = authData?.id as string | undefined
|
|
163
|
+
},
|
|
164
|
+
validateQuery({ authData }) {
|
|
165
|
+
validatedQueryAuth = authData?.id as string | undefined
|
|
166
|
+
},
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
expect(Object.keys(bindings.mutators)).toEqual(['project|create'])
|
|
170
|
+
|
|
171
|
+
const executor = createSyncExecutor({
|
|
172
|
+
database: sqliteApplicationDatabase(sqlite),
|
|
173
|
+
schema,
|
|
174
|
+
mutators: bindings.mutators,
|
|
175
|
+
effects: {
|
|
176
|
+
runBackground(promise) {
|
|
177
|
+
scheduledBackground.push(promise)
|
|
178
|
+
},
|
|
179
|
+
report(error) {
|
|
180
|
+
throw error
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
})
|
|
184
|
+
const claims = authDataToClaims({ id: 'user-1', role: 'member' } as AuthData)
|
|
185
|
+
const body = {
|
|
186
|
+
clientGroupID: 'group-1',
|
|
187
|
+
mutations: [
|
|
188
|
+
{
|
|
189
|
+
type: 'custom',
|
|
190
|
+
id: 1,
|
|
191
|
+
clientID: 'client-1',
|
|
192
|
+
name: 'project|create',
|
|
193
|
+
args: [{ id: 'project-1', ownerId: 'user-1', name: 'first' }],
|
|
194
|
+
timestamp: 1,
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
pushVersion: 1,
|
|
198
|
+
requestID: 'request-1',
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const first = await executor.push(body, claims)
|
|
202
|
+
expect(first.pushResponse).toMatchObject({
|
|
203
|
+
mutations: [{ result: {} }],
|
|
204
|
+
})
|
|
205
|
+
expect(sqlite.prepare('SELECT * FROM project').all()).toEqual([
|
|
206
|
+
{ id: 'project-1', ownerId: 'user-1', name: 'first' },
|
|
207
|
+
])
|
|
208
|
+
expect(mutationRuns).toBe(1)
|
|
209
|
+
expect(validatedMutationAuth).toBe('user-1')
|
|
210
|
+
expect(barrierRuns).toBe(1)
|
|
211
|
+
await Promise.all(scheduledBackground)
|
|
212
|
+
expect(backgroundRuns).toBe(1)
|
|
213
|
+
expect(backgroundAuth).toBe('user-1')
|
|
214
|
+
|
|
215
|
+
const replay = await executor.push(body, claims)
|
|
216
|
+
expect(replay.pushResponse).toMatchObject({
|
|
217
|
+
mutations: [{ result: { error: 'alreadyProcessed' } }],
|
|
218
|
+
})
|
|
219
|
+
expect(scheduledBackground).toHaveLength(1)
|
|
220
|
+
expect(mutationRuns).toBe(1)
|
|
221
|
+
expect(barrierRuns).toBe(1)
|
|
222
|
+
expect(backgroundRuns).toBe(1)
|
|
223
|
+
|
|
224
|
+
const ast = await bindings.resolveQuery(
|
|
225
|
+
'project|byOwner',
|
|
226
|
+
[{ ownerId: 'user-1' }],
|
|
227
|
+
claims,
|
|
228
|
+
)
|
|
229
|
+
expect(JSON.stringify(ast)).toContain('ownerId')
|
|
230
|
+
expect(JSON.stringify(ast)).toContain('user-1')
|
|
231
|
+
expect(validatedQueryAuth).toBe('user-1')
|
|
232
|
+
})
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
function sqliteApplicationDatabase(sqlite: DatabaseSync): ApplicationDatabase {
|
|
236
|
+
const applicationTransaction: ApplicationTransaction = {
|
|
237
|
+
async exec(sql, params = []) {
|
|
238
|
+
const result = sqlite.prepare(sql).run(...(params as SQLInputValue[]))
|
|
239
|
+
return { changes: Number(result.changes) }
|
|
240
|
+
},
|
|
241
|
+
async query<Row extends Record<string, unknown>>(sql: string, params = []) {
|
|
242
|
+
return sqlite.prepare(sql).all(...(params as SQLInputValue[])) as Row[]
|
|
243
|
+
},
|
|
244
|
+
async queryAst<Result>() {
|
|
245
|
+
return [] as Result
|
|
246
|
+
},
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
dialect: 'sqlite',
|
|
251
|
+
async transaction(work) {
|
|
252
|
+
sqlite.exec('BEGIN')
|
|
253
|
+
try {
|
|
254
|
+
const value = await work(applicationTransaction)
|
|
255
|
+
sqlite.exec('COMMIT')
|
|
256
|
+
return value
|
|
257
|
+
} catch (error) {
|
|
258
|
+
sqlite.exec('ROLLBACK')
|
|
259
|
+
throw error
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
async query<Row extends Record<string, unknown>>(sql: string, params = []) {
|
|
263
|
+
return sqlite.prepare(sql).all(...(params as SQLInputValue[])) as Row[]
|
|
264
|
+
},
|
|
265
|
+
}
|
|
266
|
+
}
|
package/src/orez.ts
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { createBuilder, mustGetQuery } from '@rocicorp/zero'
|
|
2
|
+
import { asQueryInternals } from '@rocicorp/zero/bindings'
|
|
3
|
+
import { handleQueryRequest as zeroHandleQueryRequest } from '@rocicorp/zero/server'
|
|
4
|
+
import { createAsyncContext } from '@take-out/helpers'
|
|
5
|
+
import {
|
|
6
|
+
MutationApplicationError,
|
|
7
|
+
registerMutators,
|
|
8
|
+
type JsonValue,
|
|
9
|
+
type MutatorRegistry,
|
|
10
|
+
type NormalizedClaims,
|
|
11
|
+
type SyncExecutor,
|
|
12
|
+
} from 'orez/sync-executor'
|
|
13
|
+
|
|
14
|
+
import { createPermissions } from './createPermissions'
|
|
15
|
+
import { createMutators } from './helpers/createMutators'
|
|
16
|
+
import { getScopedAuthData, runWithAuthScope } from './helpers/mutatorContext'
|
|
17
|
+
import { runWithQueryContext } from './helpers/queryContext'
|
|
18
|
+
import { getMutationsPermissions } from './modelRegistry'
|
|
19
|
+
import { setCustomQueries } from './run'
|
|
20
|
+
import { getZQL, setEnvironment, setSchema } from './state'
|
|
21
|
+
import { setEvaluatingPermission } from './where'
|
|
22
|
+
import { setRunner } from './zeroRunner'
|
|
23
|
+
|
|
24
|
+
import type {
|
|
25
|
+
AdminRoleMode,
|
|
26
|
+
AuthData,
|
|
27
|
+
GenericModels,
|
|
28
|
+
MutatorContext,
|
|
29
|
+
QueryBuilder,
|
|
30
|
+
Transaction,
|
|
31
|
+
} from './types'
|
|
32
|
+
import type {
|
|
33
|
+
AnyQueryRegistry,
|
|
34
|
+
HumanReadable,
|
|
35
|
+
Query,
|
|
36
|
+
Schema as ZeroSchema,
|
|
37
|
+
} from '@rocicorp/zero'
|
|
38
|
+
|
|
39
|
+
export type ValidateQueryArgs = {
|
|
40
|
+
authData: AuthData | null
|
|
41
|
+
queryName: string
|
|
42
|
+
params: unknown
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ValidateMutationArgs = {
|
|
46
|
+
authData: AuthData | null
|
|
47
|
+
mutatorName: string
|
|
48
|
+
tableName: string
|
|
49
|
+
args: unknown
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type ValidateQueryFn = (args: ValidateQueryArgs) => void
|
|
53
|
+
export type ValidateMutationFn = (args: ValidateMutationArgs) => void | Promise<void>
|
|
54
|
+
|
|
55
|
+
type MutateAuthData = Pick<AuthData, 'id'> & Partial<AuthData>
|
|
56
|
+
|
|
57
|
+
export type MutateOptions = {
|
|
58
|
+
authData?: MutateAuthData
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type ServerMutate<Models extends GenericModels> = {
|
|
62
|
+
[Key in keyof Models]: {
|
|
63
|
+
[K in keyof Models[Key]['mutate']]: Models[Key]['mutate'][K] extends (
|
|
64
|
+
ctx: MutatorContext,
|
|
65
|
+
arg: infer Arg,
|
|
66
|
+
) => any
|
|
67
|
+
? (arg: Arg, options?: MutateOptions) => Promise<void>
|
|
68
|
+
: (options?: MutateOptions) => Promise<void>
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type CreateOrezBindingsOptions<
|
|
73
|
+
Schema extends ZeroSchema,
|
|
74
|
+
Models extends GenericModels,
|
|
75
|
+
ServerActions extends Record<string, unknown>,
|
|
76
|
+
> = {
|
|
77
|
+
schema: Schema
|
|
78
|
+
models: Models
|
|
79
|
+
createServerActions: () => ServerActions
|
|
80
|
+
queries?: AnyQueryRegistry
|
|
81
|
+
mutations?: Record<string, Record<string, unknown>>
|
|
82
|
+
validateQuery?: ValidateQueryFn
|
|
83
|
+
validateMutation?: ValidateMutationFn
|
|
84
|
+
defaultAllowAdminRole?: AdminRoleMode
|
|
85
|
+
mapClaims?: (claims: NormalizedClaims) => AuthData | null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type OrezBindings<Schema extends ZeroSchema, Models extends GenericModels> = {
|
|
89
|
+
mutators: MutatorRegistry<Schema>
|
|
90
|
+
resolveQuery(
|
|
91
|
+
name: string,
|
|
92
|
+
args: readonly JsonValue[],
|
|
93
|
+
claims: NormalizedClaims,
|
|
94
|
+
): Promise<JsonValue>
|
|
95
|
+
transformQueryRequest(options: {
|
|
96
|
+
authData: AuthData | null
|
|
97
|
+
request: Request
|
|
98
|
+
}): ReturnType<typeof zeroHandleQueryRequest>
|
|
99
|
+
server(executor: SyncExecutor<Schema>): {
|
|
100
|
+
mutate: ServerMutate<Models>
|
|
101
|
+
transaction<Value>(
|
|
102
|
+
claims: NormalizedClaims,
|
|
103
|
+
work: (tx: Transaction) => Value | Promise<Value>,
|
|
104
|
+
): Promise<Value>
|
|
105
|
+
query<Result>(
|
|
106
|
+
claims: NormalizedClaims,
|
|
107
|
+
work: (q: QueryBuilder) => Query<any, Schema, Result>,
|
|
108
|
+
): Promise<HumanReadable<Result>>
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function createOrezBindings<
|
|
113
|
+
Schema extends ZeroSchema,
|
|
114
|
+
Models extends GenericModels,
|
|
115
|
+
ServerActions extends Record<string, unknown>,
|
|
116
|
+
>(
|
|
117
|
+
options: CreateOrezBindingsOptions<Schema, Models, ServerActions>,
|
|
118
|
+
): OrezBindings<Schema, Models> {
|
|
119
|
+
setSchema(options.schema, createBuilder(options.schema))
|
|
120
|
+
setEnvironment('server')
|
|
121
|
+
if (options.queries) setCustomQueries(options.queries)
|
|
122
|
+
|
|
123
|
+
const mapClaims = options.mapClaims ?? defaultMapClaims
|
|
124
|
+
const permissions = createPermissions({
|
|
125
|
+
environment: 'server',
|
|
126
|
+
schema: options.schema,
|
|
127
|
+
adminRoleMode: options.defaultAllowAdminRole ?? 'all',
|
|
128
|
+
})
|
|
129
|
+
const registry: Record<string, MutatorRegistry<Schema>[string]> = {}
|
|
130
|
+
const invocation = createAsyncContext<{
|
|
131
|
+
authData: AuthData | null
|
|
132
|
+
ctx: Parameters<MutatorRegistry<Schema>[string]>[0]['ctx']
|
|
133
|
+
}>()
|
|
134
|
+
const decoratedMutators = createMutators({
|
|
135
|
+
authData: null,
|
|
136
|
+
can: permissions.can,
|
|
137
|
+
createServerActions: options.createServerActions,
|
|
138
|
+
environment: 'server',
|
|
139
|
+
models: options.models,
|
|
140
|
+
mutationValidators: options.mutations,
|
|
141
|
+
validateMutation: options.validateMutation
|
|
142
|
+
? async (input) => {
|
|
143
|
+
try {
|
|
144
|
+
await options.validateMutation!(input)
|
|
145
|
+
} catch (error) {
|
|
146
|
+
throw mutationApplicationError(error)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
: undefined,
|
|
150
|
+
resolveAuthData: () => invocation.get()?.authData ?? null,
|
|
151
|
+
enqueueTask(task, taskOptions) {
|
|
152
|
+
const current = invocation.get()
|
|
153
|
+
if (!current) throw new Error('on-zero task scheduled outside an Orez mutation')
|
|
154
|
+
current.ctx.defer(() => runWithAuthScope(current.authData, task), taskOptions)
|
|
155
|
+
},
|
|
156
|
+
}) as Record<string, Record<string, (tx: Transaction, args: unknown) => Promise<void>>>
|
|
157
|
+
|
|
158
|
+
for (const [modelName, model] of Object.entries(options.models)) {
|
|
159
|
+
for (const mutatorName of Object.keys(model.mutate ?? {})) {
|
|
160
|
+
registry[`${modelName}|${mutatorName}`] = async ({ tx, args, ctx }) => {
|
|
161
|
+
const authData = mapClaims(ctx.claims)
|
|
162
|
+
const mutation = decoratedMutators[modelName]?.[mutatorName]
|
|
163
|
+
if (!mutation)
|
|
164
|
+
throw new Error(`unknown on-zero mutator: ${modelName}|${mutatorName}`)
|
|
165
|
+
try {
|
|
166
|
+
await invocation.run({ authData, ctx }, () =>
|
|
167
|
+
mutation(tx as unknown as Transaction, args),
|
|
168
|
+
)
|
|
169
|
+
} catch (error) {
|
|
170
|
+
const name = (error as { name?: unknown })?.name
|
|
171
|
+
if (name === 'MutationApplicationError') throw error
|
|
172
|
+
if (name !== 'PermissionError' && name !== 'ValiError') throw error
|
|
173
|
+
throw mutationApplicationError(error)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const bindings: OrezBindings<Schema, Models> = {
|
|
180
|
+
mutators: registerMutators<Schema, typeof registry>(registry),
|
|
181
|
+
async resolveQuery(name, args, claims) {
|
|
182
|
+
if (!options.queries) {
|
|
183
|
+
throw new Error('No queries registered with createOrezBindings.')
|
|
184
|
+
}
|
|
185
|
+
const authData = mapClaims(claims)
|
|
186
|
+
const query = await runWithQueryContext({ authData }, () =>
|
|
187
|
+
resolveServerQuery({
|
|
188
|
+
authData,
|
|
189
|
+
name,
|
|
190
|
+
args: args[0],
|
|
191
|
+
queries: options.queries!,
|
|
192
|
+
permissions,
|
|
193
|
+
validateQuery: options.validateQuery,
|
|
194
|
+
}),
|
|
195
|
+
)
|
|
196
|
+
return asQueryInternals(query as never).ast as JsonValue
|
|
197
|
+
},
|
|
198
|
+
async transformQueryRequest({ authData, request }) {
|
|
199
|
+
if (!options.queries) {
|
|
200
|
+
throw new Error('No queries registered with createOrezBindings.')
|
|
201
|
+
}
|
|
202
|
+
const handler = (name: string, args: unknown) =>
|
|
203
|
+
resolveServerQuery({
|
|
204
|
+
authData,
|
|
205
|
+
name,
|
|
206
|
+
args,
|
|
207
|
+
queries: options.queries!,
|
|
208
|
+
permissions,
|
|
209
|
+
validateQuery: options.validateQuery,
|
|
210
|
+
})
|
|
211
|
+
const userID = typeof authData?.id === 'string' ? authData.id : undefined
|
|
212
|
+
return runWithQueryContext({ authData: authData || ({} as AuthData) }, () =>
|
|
213
|
+
userID === undefined
|
|
214
|
+
? zeroHandleQueryRequest(handler as never, options.schema, request)
|
|
215
|
+
: zeroHandleQueryRequest({
|
|
216
|
+
handler: handler as never,
|
|
217
|
+
schema: options.schema,
|
|
218
|
+
request,
|
|
219
|
+
userID,
|
|
220
|
+
}),
|
|
221
|
+
)
|
|
222
|
+
},
|
|
223
|
+
server(executor) {
|
|
224
|
+
const transaction = <Value>(
|
|
225
|
+
claims: NormalizedClaims,
|
|
226
|
+
work: (tx: Transaction) => Value | Promise<Value>,
|
|
227
|
+
) => executor.transaction(claims, (tx) => work(tx as unknown as Transaction))
|
|
228
|
+
|
|
229
|
+
setRunner((query) =>
|
|
230
|
+
transaction({ userID: 'server' }, (tx) => tx.run(query as never)),
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
const mutate = new Proxy({} as ServerMutate<Models>, {
|
|
234
|
+
get(_target, modelName: string) {
|
|
235
|
+
return new Proxy(
|
|
236
|
+
{},
|
|
237
|
+
{
|
|
238
|
+
get(_inner, mutatorName: string) {
|
|
239
|
+
const handler = options.models[modelName]?.mutate?.[mutatorName]
|
|
240
|
+
const acceptsArgument =
|
|
241
|
+
typeof handler === 'function' && handler.length > 1
|
|
242
|
+
return (
|
|
243
|
+
argsOrOptions: JsonValue | MutateOptions,
|
|
244
|
+
optionsForArgument?: MutateOptions,
|
|
245
|
+
) => {
|
|
246
|
+
const args = acceptsArgument ? (argsOrOptions as JsonValue) : null
|
|
247
|
+
const mutateOptions = acceptsArgument
|
|
248
|
+
? optionsForArgument
|
|
249
|
+
: (argsOrOptions as MutateOptions | undefined)
|
|
250
|
+
const scoped = mutateOptions?.authData ?? getScopedAuthData()
|
|
251
|
+
const claims = authToClaims(scoped)
|
|
252
|
+
return executor.execute(`${modelName}|${mutatorName}`, args, claims)
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
)
|
|
257
|
+
},
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
mutate,
|
|
262
|
+
transaction,
|
|
263
|
+
query<Result>(
|
|
264
|
+
claims: NormalizedClaims,
|
|
265
|
+
work: (q: QueryBuilder) => Query<any, Schema, Result>,
|
|
266
|
+
) {
|
|
267
|
+
return runWithQueryContext({ authData: mapClaims(claims) }, () =>
|
|
268
|
+
executor.query(claims, (tx) => tx.run(work(getZQL()) as never)),
|
|
269
|
+
) as Promise<HumanReadable<Result>>
|
|
270
|
+
},
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return bindings
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function resolveServerQuery({
|
|
279
|
+
authData,
|
|
280
|
+
name,
|
|
281
|
+
args,
|
|
282
|
+
queries,
|
|
283
|
+
permissions,
|
|
284
|
+
validateQuery,
|
|
285
|
+
}: {
|
|
286
|
+
authData: AuthData | null
|
|
287
|
+
name: string
|
|
288
|
+
args: unknown
|
|
289
|
+
queries: AnyQueryRegistry
|
|
290
|
+
permissions: ReturnType<typeof createPermissions>
|
|
291
|
+
validateQuery?: ValidateQueryFn
|
|
292
|
+
}) {
|
|
293
|
+
if (name.startsWith('permission.')) {
|
|
294
|
+
const table = name.slice('permission.'.length)
|
|
295
|
+
const { objOrId } = args as { objOrId: string | Record<string, unknown> }
|
|
296
|
+
const permission = getMutationsPermissions(table)
|
|
297
|
+
if (!permission)
|
|
298
|
+
throw new Error(`[permission] no permission defined for table: ${table}`)
|
|
299
|
+
setEvaluatingPermission(true)
|
|
300
|
+
try {
|
|
301
|
+
return (getZQL() as any)[table]
|
|
302
|
+
.where((eb: any) =>
|
|
303
|
+
permissions.buildPermissionQuery(authData, eb, permission, objOrId, table),
|
|
304
|
+
)
|
|
305
|
+
.one()
|
|
306
|
+
} finally {
|
|
307
|
+
setEvaluatingPermission(false)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
validateQuery?.({ authData, queryName: name, params: args })
|
|
312
|
+
return (mustGetQuery as any)(queries, name).fn({ args, ctx: authData })
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// claims.userID is the sync ledger identity and is always present — logged-out
|
|
316
|
+
// clients still sync, as 'anon'. app auth is a separate, nullable payload:
|
|
317
|
+
// deriving AuthData from userID would hand every anonymous push a truthy
|
|
318
|
+
// authData and walk straight through `ensure(authData)` guards.
|
|
319
|
+
const AUTH_CLAIM = 'authData'
|
|
320
|
+
|
|
321
|
+
export function authDataToClaims(
|
|
322
|
+
authData: AuthData | null | undefined,
|
|
323
|
+
anonymousUserID = 'anon',
|
|
324
|
+
): NormalizedClaims {
|
|
325
|
+
const userID = typeof authData?.id === 'string' ? authData.id : anonymousUserID
|
|
326
|
+
const claims: Record<string, JsonValue> = { userID }
|
|
327
|
+
if (authData) claims[AUTH_CLAIM] = authData as unknown as JsonValue
|
|
328
|
+
return claims as NormalizedClaims
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function defaultMapClaims(claims: NormalizedClaims): AuthData | null {
|
|
332
|
+
const authData = (claims as unknown as Record<string, unknown>)[AUTH_CLAIM]
|
|
333
|
+
if (!authData || typeof authData !== 'object') return null
|
|
334
|
+
return authData as AuthData
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function authToClaims(authData: AuthData | null | undefined): NormalizedClaims {
|
|
338
|
+
return authDataToClaims(authData, 'server')
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function mutationApplicationError(error: unknown): MutationApplicationError {
|
|
342
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
343
|
+
return new MutationApplicationError(message, message)
|
|
344
|
+
}
|
package/src/run.ts
CHANGED
|
@@ -19,7 +19,7 @@ export function setCustomQueries(queries: AnyQueryRegistry) {
|
|
|
19
19
|
function getCustomQueries(): AnyQueryRegistry {
|
|
20
20
|
if (!customQueriesRef) {
|
|
21
21
|
throw new Error(
|
|
22
|
-
'Custom queries not initialized. Ensure
|
|
22
|
+
'Custom queries not initialized. Ensure client or server bindings have been created.',
|
|
23
23
|
)
|
|
24
24
|
}
|
|
25
25
|
return customQueriesRef
|
package/src/state.ts
CHANGED
|
@@ -18,7 +18,7 @@ const getState = () =>
|
|
|
18
18
|
environment: null,
|
|
19
19
|
}))
|
|
20
20
|
|
|
21
|
-
const errMessage = `
|
|
21
|
+
const errMessage = `Zero client or server bindings have not been created yet!`
|
|
22
22
|
|
|
23
23
|
export const getZQL = () => {
|
|
24
24
|
const { zql } = getState()
|
package/src/types.ts
CHANGED
package/src/zeroRunner.ts
CHANGED
|
@@ -37,7 +37,7 @@ export function getAmbientRunner(instance?: { runner: ZeroRunner | null }): Zero
|
|
|
37
37
|
|
|
38
38
|
if (!runner) {
|
|
39
39
|
throw new Error(
|
|
40
|
-
'Zero runner not initialized. Ensure ProvideZero is mounted
|
|
40
|
+
'Zero runner not initialized. Ensure ProvideZero is mounted or server bindings are active.',
|
|
41
41
|
)
|
|
42
42
|
}
|
|
43
43
|
|