over-zero 0.0.10 → 0.0.13
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/LICENSE +21 -0
- package/dist/cjs/{build/schema.cjs → builder.cjs} +10 -7
- package/dist/cjs/{helpers/zeroEmitter.js → builder.js} +11 -7
- package/dist/cjs/builder.js.map +6 -0
- package/dist/cjs/builder.native.js +35 -0
- package/dist/cjs/builder.native.js.map +1 -0
- package/dist/cjs/cli.cjs +299 -0
- package/dist/cjs/cli.js +292 -0
- package/dist/cjs/cli.js.map +6 -0
- package/dist/cjs/cli.native.js +386 -0
- package/dist/cjs/cli.native.js.map +1 -0
- package/dist/cjs/createPermissions.cjs +22 -56
- package/dist/cjs/createPermissions.js +22 -64
- package/dist/cjs/createPermissions.js.map +1 -1
- package/dist/cjs/createPermissions.native.js +61 -87
- package/dist/cjs/createPermissions.native.js.map +1 -6
- package/dist/cjs/createZeroClient.cjs +41 -21
- package/dist/cjs/createZeroClient.js +23 -16
- package/dist/cjs/createZeroClient.js.map +1 -1
- package/dist/cjs/createZeroClient.native.js +139 -81
- package/dist/cjs/createZeroClient.native.js.map +1 -6
- package/dist/cjs/createZeroServer.cjs +40 -34
- package/dist/cjs/createZeroServer.js +35 -27
- package/dist/cjs/createZeroServer.js.map +2 -2
- package/dist/cjs/createZeroServer.native.js +119 -77
- package/dist/cjs/createZeroServer.native.js.map +1 -6
- package/dist/cjs/helpers/batchQuery.native.js +25 -18
- package/dist/cjs/helpers/batchQuery.native.js.map +1 -6
- package/dist/cjs/helpers/context.js.map +1 -1
- package/dist/cjs/helpers/context.native.js +20 -19
- package/dist/cjs/helpers/context.native.js.map +1 -6
- package/dist/cjs/helpers/createMutators.cjs +2 -2
- package/dist/cjs/helpers/createMutators.js +2 -2
- package/dist/cjs/helpers/createMutators.js.map +1 -1
- package/dist/cjs/helpers/createMutators.native.js +52 -41
- package/dist/cjs/helpers/createMutators.native.js.map +1 -6
- package/dist/cjs/helpers/didRunPermissionCheck.native.js +27 -22
- package/dist/cjs/helpers/didRunPermissionCheck.native.js.map +1 -6
- package/dist/cjs/helpers/ensureLoggedIn.cjs +2 -2
- package/dist/cjs/helpers/ensureLoggedIn.js +2 -2
- package/dist/cjs/helpers/ensureLoggedIn.js.map +1 -1
- package/dist/cjs/helpers/ensureLoggedIn.native.js +25 -18
- package/dist/cjs/helpers/ensureLoggedIn.native.js.map +1 -6
- package/dist/cjs/helpers/getQueryOrMutatorAuthData.cjs +30 -0
- package/dist/cjs/{build/schema.native.js → helpers/getQueryOrMutatorAuthData.js} +9 -13
- package/dist/cjs/helpers/getQueryOrMutatorAuthData.js.map +6 -0
- package/dist/cjs/helpers/getQueryOrMutatorAuthData.native.js +33 -0
- package/dist/cjs/helpers/getQueryOrMutatorAuthData.native.js.map +1 -0
- package/dist/cjs/helpers/mutatorContext.cjs +40 -0
- package/dist/cjs/helpers/mutatorContext.js +36 -0
- package/dist/cjs/helpers/mutatorContext.js.map +6 -0
- package/dist/cjs/helpers/mutatorContext.native.js +43 -0
- package/dist/cjs/helpers/mutatorContext.native.js.map +1 -0
- package/dist/cjs/helpers/prettyFormatZeroQuery.native.js +102 -82
- package/dist/cjs/helpers/prettyFormatZeroQuery.native.js.map +1 -6
- package/dist/cjs/helpers/queryContext.cjs +40 -0
- package/dist/cjs/helpers/{getDidRunPermissionCheck.native.js → queryContext.js} +21 -13
- package/dist/cjs/helpers/queryContext.js.map +6 -0
- package/dist/cjs/helpers/queryContext.native.js +43 -0
- package/dist/cjs/helpers/queryContext.native.js.map +1 -0
- package/dist/cjs/helpers/queryOrMutatorAuthData.js +1 -0
- package/dist/cjs/helpers/queryOrMutatorAuthData.native.js +2 -0
- package/dist/cjs/helpers/queryOrMutatorContext.js +1 -0
- package/dist/cjs/helpers/queryOrMutatorContext.native.js +2 -0
- package/dist/cjs/helpers/useZeroDebug.native.js +63 -50
- package/dist/cjs/helpers/useZeroDebug.native.js.map +1 -6
- package/dist/cjs/index.cjs +3 -2
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.native.js +21 -26
- package/dist/cjs/index.native.js.map +1 -6
- package/dist/cjs/mutations.cjs +1 -1
- package/dist/cjs/mutations.js +1 -1
- package/dist/cjs/mutations.js.map +1 -1
- package/dist/cjs/mutations.native.js +33 -28
- package/dist/cjs/mutations.native.js.map +1 -6
- package/dist/cjs/query.cjs +34 -0
- package/dist/cjs/{helpers/zeroEmitter.native.js → query.js} +13 -11
- package/dist/cjs/query.js.map +6 -0
- package/dist/cjs/query.native.js +39 -0
- package/dist/cjs/query.native.js.map +1 -0
- package/dist/cjs/{helpers/getDidRunPermissionCheck.cjs → queryBuilder.cjs} +10 -6
- package/dist/cjs/{build/schema.js → queryBuilder.js} +11 -7
- package/dist/cjs/queryBuilder.js.map +6 -0
- package/dist/cjs/queryBuilder.native.js +35 -0
- package/dist/cjs/queryBuilder.native.js.map +1 -0
- package/dist/cjs/server.native.js +13 -12
- package/dist/cjs/server.native.js.map +1 -6
- package/dist/cjs/state.cjs +52 -0
- package/dist/cjs/state.js +40 -0
- package/dist/cjs/state.js.map +6 -0
- package/dist/cjs/state.native.js +55 -0
- package/dist/cjs/state.native.js.map +1 -0
- package/dist/cjs/types.native.js +9 -5
- package/dist/cjs/types.native.js.map +1 -6
- package/dist/cjs/where.cjs +9 -2
- package/dist/cjs/where.js +8 -2
- package/dist/cjs/where.js.map +2 -2
- package/dist/cjs/where.native.js +29 -19
- package/dist/cjs/where.native.js.map +1 -6
- package/dist/cjs/{helpers/zeroEmitter.cjs → zql.cjs} +10 -6
- package/dist/cjs/{helpers/getDidRunPermissionCheck.js → zql.js} +11 -6
- package/dist/cjs/zql.js.map +6 -0
- package/dist/cjs/zql.native.js +35 -0
- package/dist/cjs/zql.native.js.map +1 -0
- package/dist/esm/builder.js +10 -0
- package/dist/esm/builder.js.map +6 -0
- package/dist/esm/builder.mjs +8 -0
- package/dist/esm/builder.mjs.map +1 -0
- package/dist/esm/builder.native.js +9 -0
- package/dist/esm/builder.native.js.map +1 -0
- package/dist/esm/cli.js +278 -0
- package/dist/esm/cli.js.map +6 -0
- package/dist/esm/cli.mjs +278 -0
- package/dist/esm/cli.mjs.map +1 -0
- package/dist/esm/cli.native.js +362 -0
- package/dist/esm/cli.native.js.map +1 -0
- package/dist/esm/createPermissions.js +21 -64
- package/dist/esm/createPermissions.js.map +1 -1
- package/dist/esm/createPermissions.mjs +20 -54
- package/dist/esm/createPermissions.mjs.map +1 -1
- package/dist/esm/createPermissions.native.js +26 -66
- package/dist/esm/createPermissions.native.js.map +1 -1
- package/dist/esm/createZeroClient.js +24 -16
- package/dist/esm/createZeroClient.js.map +1 -1
- package/dist/esm/createZeroClient.mjs +40 -20
- package/dist/esm/createZeroClient.mjs.map +1 -1
- package/dist/esm/createZeroClient.native.js +56 -25
- package/dist/esm/createZeroClient.native.js.map +1 -1
- package/dist/esm/createZeroServer.js +36 -19
- package/dist/esm/createZeroServer.js.map +1 -1
- package/dist/esm/createZeroServer.mjs +35 -18
- package/dist/esm/createZeroServer.mjs.map +1 -1
- package/dist/esm/createZeroServer.native.js +43 -23
- package/dist/esm/createZeroServer.native.js.map +1 -1
- package/dist/esm/helpers/context.js.map +1 -1
- package/dist/esm/helpers/context.mjs.map +1 -1
- package/dist/esm/helpers/context.native.js.map +1 -1
- package/dist/esm/helpers/createMutators.js +1 -1
- package/dist/esm/helpers/createMutators.mjs +1 -1
- package/dist/esm/helpers/createMutators.native.js +1 -1
- package/dist/esm/helpers/ensureLoggedIn.js +2 -2
- package/dist/esm/helpers/ensureLoggedIn.js.map +1 -1
- package/dist/esm/helpers/ensureLoggedIn.mjs +2 -2
- package/dist/esm/helpers/ensureLoggedIn.mjs.map +1 -1
- package/dist/esm/helpers/ensureLoggedIn.native.js +2 -2
- package/dist/esm/helpers/ensureLoggedIn.native.js.map +1 -1
- package/dist/esm/helpers/getQueryOrMutatorAuthData.js +9 -0
- package/dist/esm/helpers/getQueryOrMutatorAuthData.js.map +6 -0
- package/dist/esm/helpers/getQueryOrMutatorAuthData.mjs +7 -0
- package/dist/esm/helpers/getQueryOrMutatorAuthData.mjs.map +1 -0
- package/dist/esm/helpers/getQueryOrMutatorAuthData.native.js +7 -0
- package/dist/esm/helpers/getQueryOrMutatorAuthData.native.js.map +1 -0
- package/dist/esm/helpers/mutatorContext.js +20 -0
- package/dist/esm/helpers/mutatorContext.js.map +6 -0
- package/dist/esm/helpers/mutatorContext.mjs +15 -0
- package/dist/esm/helpers/mutatorContext.mjs.map +1 -0
- package/dist/esm/helpers/mutatorContext.native.js +15 -0
- package/dist/esm/helpers/mutatorContext.native.js.map +1 -0
- package/dist/esm/helpers/queryContext.js +20 -0
- package/dist/esm/helpers/queryContext.js.map +6 -0
- package/dist/esm/helpers/queryContext.mjs +15 -0
- package/dist/esm/helpers/queryContext.mjs.map +1 -0
- package/dist/esm/helpers/queryContext.native.js +15 -0
- package/dist/esm/helpers/queryContext.native.js.map +1 -0
- package/dist/esm/helpers/queryOrMutatorAuthData.js +1 -0
- package/dist/esm/helpers/queryOrMutatorAuthData.mjs +2 -0
- package/dist/esm/helpers/queryOrMutatorAuthData.native.js +2 -0
- package/dist/esm/helpers/queryOrMutatorContext.js +1 -0
- package/dist/esm/helpers/queryOrMutatorContext.mjs +2 -0
- package/dist/esm/helpers/queryOrMutatorContext.mjs.map +1 -0
- package/dist/esm/helpers/queryOrMutatorContext.native.js +2 -0
- package/dist/esm/helpers/queryOrMutatorContext.native.js.map +1 -0
- package/dist/esm/index.js +3 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +3 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +3 -2
- package/dist/esm/index.native.js.map +1 -1
- package/dist/esm/mutations.js +1 -1
- package/dist/esm/mutations.js.map +1 -1
- package/dist/esm/mutations.mjs +1 -1
- package/dist/esm/mutations.mjs.map +1 -1
- package/dist/esm/mutations.native.js +1 -1
- package/dist/esm/mutations.native.js.map +1 -1
- package/dist/esm/query.js +13 -0
- package/dist/esm/query.js.map +6 -0
- package/dist/esm/query.mjs +11 -0
- package/dist/esm/query.mjs.map +1 -0
- package/dist/esm/query.native.js +13 -0
- package/dist/esm/query.native.js.map +1 -0
- package/dist/esm/queryBuilder.js +10 -0
- package/dist/esm/queryBuilder.js.map +6 -0
- package/dist/esm/queryBuilder.mjs +8 -0
- package/dist/esm/queryBuilder.mjs.map +1 -0
- package/dist/esm/queryBuilder.native.js +9 -0
- package/dist/esm/queryBuilder.native.js.map +1 -0
- package/dist/esm/state.js +24 -0
- package/dist/esm/state.js.map +6 -0
- package/dist/esm/state.mjs +25 -0
- package/dist/esm/state.mjs.map +1 -0
- package/dist/esm/state.native.js +25 -0
- package/dist/esm/state.native.js.map +1 -0
- package/dist/esm/where.js +9 -2
- package/dist/esm/where.js.map +2 -2
- package/dist/esm/where.mjs +9 -2
- package/dist/esm/where.mjs.map +1 -1
- package/dist/esm/where.native.js +10 -2
- package/dist/esm/where.native.js.map +1 -1
- package/dist/esm/zql.js +10 -0
- package/dist/esm/zql.js.map +6 -0
- package/dist/esm/zql.mjs +8 -0
- package/dist/esm/zql.mjs.map +1 -0
- package/dist/esm/zql.native.js +9 -0
- package/dist/esm/zql.native.js.map +1 -0
- package/package.json +13 -3
- package/readme.md +460 -13
- package/src/cli.ts +511 -0
- package/src/createPermissions.ts +21 -123
- package/src/createZeroClient.tsx +107 -23
- package/src/createZeroServer.ts +64 -29
- package/src/helpers/createMutators.ts +1 -1
- package/src/helpers/ensureLoggedIn.ts +2 -2
- package/src/helpers/getQueryOrMutatorAuthData.ts +11 -0
- package/src/helpers/{context.ts → mutatorContext.ts} +2 -6
- package/src/index.ts +3 -2
- package/src/mutations.ts +1 -1
- package/src/state.ts +32 -0
- package/src/types.ts +11 -5
- package/src/where.ts +33 -8
- package/src/zql.ts +9 -0
- package/types/builder.d.ts +3 -0
- package/types/builder.d.ts.map +1 -0
- package/types/cli.d.ts +3 -0
- package/types/cli.d.ts.map +1 -0
- package/types/createPermissions.d.ts +3 -27
- package/types/createPermissions.d.ts.map +1 -1
- package/types/createZeroClient.d.ts +16 -4
- package/types/createZeroClient.d.ts.map +1 -1
- package/types/createZeroServer.d.ts +33 -6
- package/types/createZeroServer.d.ts.map +1 -1
- package/types/helpers/context.d.ts.map +1 -1
- package/types/helpers/getQueryOrMutatorAuthData.d.ts +3 -0
- package/types/helpers/getQueryOrMutatorAuthData.d.ts.map +1 -0
- package/types/helpers/mutatorContext.d.ts +5 -0
- package/types/helpers/mutatorContext.d.ts.map +1 -0
- package/types/helpers/queryContext.d.ts +5 -0
- package/types/helpers/queryContext.d.ts.map +1 -0
- package/types/index.d.ts +3 -2
- package/types/index.d.ts.map +1 -1
- package/types/state.d.ts +8 -0
- package/types/state.d.ts.map +1 -0
- package/types/types.d.ts +5 -4
- package/types/types.d.ts.map +1 -1
- package/types/where.d.ts +3 -2
- package/types/where.d.ts.map +1 -1
- package/types/zql.d.ts +3 -0
- package/types/zql.d.ts.map +1 -0
- package/dist/cjs/build/readPermissions.cjs +0 -51
- package/dist/cjs/build/readPermissions.js +0 -48
- package/dist/cjs/build/readPermissions.js.map +0 -6
- package/dist/cjs/build/readPermissions.native.js +0 -56
- package/dist/cjs/build/readPermissions.native.js.map +0 -6
- package/dist/cjs/build/schema.js.map +0 -6
- package/dist/cjs/build/schema.native.js.map +0 -6
- package/dist/cjs/createClient.cjs +0 -89
- package/dist/cjs/createClient.js +0 -76
- package/dist/cjs/createClient.js.map +0 -6
- package/dist/cjs/createClient.native.js +0 -81
- package/dist/cjs/createClient.native.js.map +0 -6
- package/dist/cjs/createMutations.cjs +0 -50
- package/dist/cjs/createMutations.js +0 -43
- package/dist/cjs/createMutations.js.map +0 -6
- package/dist/cjs/createMutations.native.js +0 -50
- package/dist/cjs/createMutations.native.js.map +0 -6
- package/dist/cjs/createServer.cjs +0 -92
- package/dist/cjs/createServer.js +0 -71
- package/dist/cjs/createServer.js.map +0 -6
- package/dist/cjs/createServer.native.js +0 -75
- package/dist/cjs/createServer.native.js.map +0 -6
- package/dist/cjs/createZero.cjs +0 -128
- package/dist/cjs/createZero.js +0 -107
- package/dist/cjs/createZero.js.map +0 -6
- package/dist/cjs/createZero.native.js +0 -115
- package/dist/cjs/createZero.native.js.map +0 -6
- package/dist/cjs/helpers/clearZeroDatabase.cjs +0 -57
- package/dist/cjs/helpers/clearZeroDatabase.js +0 -57
- package/dist/cjs/helpers/clearZeroDatabase.js.map +0 -6
- package/dist/cjs/helpers/clearZeroDatabase.native.js +0 -71
- package/dist/cjs/helpers/clearZeroDatabase.native.js.map +0 -6
- package/dist/cjs/helpers/getAuthData.js +0 -1
- package/dist/cjs/helpers/getAuthData.native.js +0 -2
- package/dist/cjs/helpers/getDidRunPermissionCheck.js.map +0 -6
- package/dist/cjs/helpers/getDidRunPermissionCheck.native.js.map +0 -6
- package/dist/cjs/helpers/setupZeroClientGlobalEffects.cjs +0 -40
- package/dist/cjs/helpers/setupZeroClientGlobalEffects.js +0 -36
- package/dist/cjs/helpers/setupZeroClientGlobalEffects.js.map +0 -6
- package/dist/cjs/helpers/setupZeroClientGlobalEffects.native.js +0 -36
- package/dist/cjs/helpers/setupZeroClientGlobalEffects.native.js.map +0 -6
- package/dist/cjs/helpers/useAuthData.js +0 -1
- package/dist/cjs/helpers/useAuthData.native.js +0 -2
- package/dist/cjs/helpers/useZDB.cjs +0 -70
- package/dist/cjs/helpers/useZDB.js +0 -51
- package/dist/cjs/helpers/useZDB.js.map +0 -6
- package/dist/cjs/helpers/useZDB.native.js +0 -68
- package/dist/cjs/helpers/useZDB.native.js.map +0 -6
- package/dist/cjs/helpers/zeroEmitter.js.map +0 -6
- package/dist/cjs/helpers/zeroEmitter.native.js.map +0 -6
- package/dist/esm/build/readPermissions.js +0 -36
- package/dist/esm/build/readPermissions.js.map +0 -6
- package/dist/esm/build/readPermissions.mjs +0 -28
- package/dist/esm/build/readPermissions.mjs.map +0 -1
- package/dist/esm/build/readPermissions.native.js +0 -34
- package/dist/esm/build/readPermissions.native.js.map +0 -1
- package/dist/esm/build/schema.js +0 -7
- package/dist/esm/build/schema.js.map +0 -6
- package/dist/esm/build/schema.mjs +0 -4
- package/dist/esm/build/schema.mjs.map +0 -1
- package/dist/esm/build/schema.native.js +0 -4
- package/dist/esm/build/schema.native.js.map +0 -1
- package/dist/esm/createClient.js +0 -68
- package/dist/esm/createClient.js.map +0 -6
- package/dist/esm/createClient.mjs +0 -66
- package/dist/esm/createClient.mjs.map +0 -1
- package/dist/esm/createClient.native.js +0 -74
- package/dist/esm/createClient.native.js.map +0 -1
- package/dist/esm/createMutations.js +0 -27
- package/dist/esm/createMutations.js.map +0 -6
- package/dist/esm/createMutations.mjs +0 -27
- package/dist/esm/createMutations.mjs.map +0 -1
- package/dist/esm/createMutations.native.js +0 -29
- package/dist/esm/createMutations.native.js.map +0 -1
- package/dist/esm/createServer.js +0 -54
- package/dist/esm/createServer.js.map +0 -6
- package/dist/esm/createServer.mjs +0 -58
- package/dist/esm/createServer.mjs.map +0 -1
- package/dist/esm/createServer.native.js +0 -61
- package/dist/esm/createServer.native.js.map +0 -1
- package/dist/esm/createZero.js +0 -99
- package/dist/esm/createZero.js.map +0 -6
- package/dist/esm/createZero.mjs +0 -105
- package/dist/esm/createZero.mjs.map +0 -1
- package/dist/esm/createZero.native.js +0 -119
- package/dist/esm/createZero.native.js.map +0 -1
- package/dist/esm/helpers/clearZeroDatabase.js +0 -42
- package/dist/esm/helpers/clearZeroDatabase.js.map +0 -6
- package/dist/esm/helpers/clearZeroDatabase.mjs +0 -34
- package/dist/esm/helpers/clearZeroDatabase.mjs.map +0 -1
- package/dist/esm/helpers/clearZeroDatabase.native.js +0 -50
- package/dist/esm/helpers/clearZeroDatabase.native.js.map +0 -1
- package/dist/esm/helpers/getAuthData.js +0 -1
- package/dist/esm/helpers/getAuthData.js.map +0 -6
- package/dist/esm/helpers/getAuthData.mjs +0 -2
- package/dist/esm/helpers/getAuthData.native.js +0 -2
- package/dist/esm/helpers/getDidRunPermissionCheck.js +0 -5
- package/dist/esm/helpers/getDidRunPermissionCheck.js.map +0 -6
- package/dist/esm/helpers/getDidRunPermissionCheck.mjs +0 -4
- package/dist/esm/helpers/getDidRunPermissionCheck.mjs.map +0 -1
- package/dist/esm/helpers/getDidRunPermissionCheck.native.js +0 -6
- package/dist/esm/helpers/getDidRunPermissionCheck.native.js.map +0 -1
- package/dist/esm/helpers/setupZeroClientGlobalEffects.js +0 -40
- package/dist/esm/helpers/setupZeroClientGlobalEffects.js.map +0 -6
- package/dist/esm/helpers/setupZeroClientGlobalEffects.mjs +0 -41
- package/dist/esm/helpers/setupZeroClientGlobalEffects.mjs.map +0 -1
- package/dist/esm/helpers/setupZeroClientGlobalEffects.native.js +0 -41
- package/dist/esm/helpers/setupZeroClientGlobalEffects.native.js.map +0 -1
- package/dist/esm/helpers/useAuthData.js +0 -1
- package/dist/esm/helpers/useAuthData.js.map +0 -6
- package/dist/esm/helpers/useAuthData.mjs +0 -2
- package/dist/esm/helpers/useAuthData.native.js +0 -2
- package/dist/esm/helpers/useZDB.js +0 -38
- package/dist/esm/helpers/useZDB.js.map +0 -6
- package/dist/esm/helpers/useZDB.mjs +0 -47
- package/dist/esm/helpers/useZDB.mjs.map +0 -1
- package/dist/esm/helpers/useZDB.native.js +0 -55
- package/dist/esm/helpers/useZDB.native.js.map +0 -1
- package/dist/esm/helpers/zeroEmitter.js +0 -6
- package/dist/esm/helpers/zeroEmitter.js.map +0 -6
- package/dist/esm/helpers/zeroEmitter.mjs +0 -4
- package/dist/esm/helpers/zeroEmitter.mjs.map +0 -1
- package/dist/esm/helpers/zeroEmitter.native.js +0 -4
- package/dist/esm/helpers/zeroEmitter.native.js.map +0 -1
- /package/dist/cjs/helpers/{getAuthData.cjs → queryOrMutatorAuthData.cjs} +0 -0
- /package/dist/cjs/helpers/{getAuthData.js.map → queryOrMutatorAuthData.js.map} +0 -0
- /package/dist/{esm/helpers/getAuthData.mjs.map → cjs/helpers/queryOrMutatorAuthData.native.js.map} +0 -0
- /package/dist/cjs/helpers/{useAuthData.cjs → queryOrMutatorContext.cjs} +0 -0
- /package/dist/cjs/helpers/{getAuthData.native.js.map → queryOrMutatorContext.js.map} +0 -0
- /package/dist/{esm/helpers/getAuthData.native.js.map → cjs/helpers/queryOrMutatorContext.native.js.map} +0 -0
- /package/dist/{cjs/helpers/useAuthData.js.map → esm/helpers/queryOrMutatorAuthData.js.map} +0 -0
- /package/dist/esm/helpers/{useAuthData.mjs.map → queryOrMutatorAuthData.mjs.map} +0 -0
- /package/dist/esm/helpers/{useAuthData.native.js.map → queryOrMutatorAuthData.native.js.map} +0 -0
- /package/dist/{cjs/helpers/useAuthData.native.js.map → esm/helpers/queryOrMutatorContext.js.map} +0 -0
package/src/createPermissions.ts
CHANGED
|
@@ -4,65 +4,31 @@ import type {
|
|
|
4
4
|
Query,
|
|
5
5
|
Schema as ZeroSchema,
|
|
6
6
|
} from '@rocicorp/zero'
|
|
7
|
-
import {
|
|
8
|
-
import { ensure, EnsureError, objectEntries } from '@vxrn/helpers'
|
|
9
|
-
import { runWithContext } from './helpers/context'
|
|
7
|
+
import { ensure, EnsureError } from '@vxrn/helpers'
|
|
10
8
|
import { setDidRunPermissionCheck } from './helpers/didRunPermissionCheck'
|
|
9
|
+
import { mutatorContext } from './helpers/mutatorContext'
|
|
11
10
|
import { prettyFormatZeroQuery } from './helpers/prettyFormatZeroQuery'
|
|
12
|
-
import type {
|
|
13
|
-
AuthData,
|
|
14
|
-
Can,
|
|
15
|
-
MutatorContext,
|
|
16
|
-
TableName,
|
|
17
|
-
Transaction,
|
|
18
|
-
Where,
|
|
19
|
-
} from './types'
|
|
11
|
+
import type { AuthData, Can, TableName, Transaction, Where } from './types'
|
|
20
12
|
import { getWhereTableName } from './where'
|
|
21
13
|
|
|
22
14
|
export function createPermissions<Schema extends ZeroSchema>({
|
|
23
15
|
environment,
|
|
24
16
|
schema,
|
|
25
|
-
models,
|
|
26
|
-
getContext,
|
|
27
17
|
}: {
|
|
28
18
|
environment: 'client' | 'server'
|
|
29
19
|
schema: Schema
|
|
30
|
-
models: any
|
|
31
|
-
getContext: () => MutatorContext
|
|
32
20
|
}) {
|
|
33
|
-
type
|
|
34
|
-
| 'read'
|
|
35
|
-
| 'write'
|
|
36
|
-
| 'insert'
|
|
37
|
-
| 'update'
|
|
38
|
-
| 'delete'
|
|
39
|
-
| 'select'
|
|
40
|
-
|
|
41
|
-
type PermissionsConditions = Partial<
|
|
42
|
-
Record<PermissionPresetActions | (string & {}), Condition | boolean>
|
|
43
|
-
>
|
|
21
|
+
type PermissionReturn = Condition | boolean
|
|
44
22
|
|
|
45
23
|
type PermissionsWhere<Table extends TableName = TableName> = Where<
|
|
46
24
|
Table,
|
|
47
|
-
|
|
25
|
+
PermissionReturn
|
|
48
26
|
>
|
|
49
27
|
|
|
50
|
-
|
|
51
|
-
select: 'read',
|
|
52
|
-
insert: 'write',
|
|
53
|
-
update: 'write',
|
|
54
|
-
upsert: 'write',
|
|
55
|
-
delete: 'write',
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function buildPermissionQuery<
|
|
59
|
-
PermissionWhere extends PermissionsWhere,
|
|
60
|
-
Action extends string,
|
|
61
|
-
>(
|
|
28
|
+
function buildPermissionQuery<PermissionWhere extends PermissionsWhere>(
|
|
62
29
|
authData: AuthData | null,
|
|
63
30
|
eb: ExpressionBuilder<any, any>,
|
|
64
31
|
permissionWhere: PermissionWhere,
|
|
65
|
-
action: Action,
|
|
66
32
|
// TODO until i can get a working PickPrimaryKeys<'message'>
|
|
67
33
|
objOrId: Record<string, any> | string
|
|
68
34
|
) {
|
|
@@ -79,22 +45,17 @@ export function createPermissions<Schema extends ZeroSchema>({
|
|
|
79
45
|
}
|
|
80
46
|
|
|
81
47
|
const primaryKeys = tableSchema.primaryKey
|
|
82
|
-
const
|
|
83
|
-
const fallbackAction = fallbackActions[action]
|
|
48
|
+
const permissionReturn = permissionWhere(eb, authData)
|
|
84
49
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
(fallbackAction ? permissionQueryBuilder[fallbackAction] : undefined)
|
|
88
|
-
|
|
89
|
-
if (permissionCondition == null) {
|
|
90
|
-
throw new Error(`No permission defined for ${action} (or ${fallbackAction})`)
|
|
50
|
+
if (permissionReturn == null) {
|
|
51
|
+
throw new Error(`No permission defined for ${tableName}`)
|
|
91
52
|
}
|
|
92
53
|
|
|
93
|
-
if (
|
|
54
|
+
if (permissionReturn === true) {
|
|
94
55
|
return eb.cmpLit(true, '=', true)
|
|
95
56
|
}
|
|
96
57
|
|
|
97
|
-
if (
|
|
58
|
+
if (permissionReturn === false) {
|
|
98
59
|
return eb.cmpLit(true, '=', false)
|
|
99
60
|
}
|
|
100
61
|
|
|
@@ -105,39 +66,27 @@ export function createPermissions<Schema extends ZeroSchema>({
|
|
|
105
66
|
primaryKeyWheres.push(eb.cmp(key as any, value))
|
|
106
67
|
}
|
|
107
68
|
|
|
108
|
-
return eb.and(
|
|
69
|
+
return eb.and(permissionReturn, ...primaryKeyWheres)
|
|
109
70
|
}
|
|
110
71
|
|
|
111
|
-
const can: Can = async (where,
|
|
112
|
-
const ctx = getContext()
|
|
113
|
-
const tableName = getWhereTableName(where)
|
|
114
|
-
if (!tableName) {
|
|
115
|
-
throw new Error(`Must use where('table') style where to pass to can()`)
|
|
116
|
-
}
|
|
117
|
-
|
|
72
|
+
const can: Can = async (where, obj) => {
|
|
118
73
|
// on client we always allow! we only check on server (like zero does)
|
|
119
74
|
if (environment === 'server') {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
obj
|
|
127
|
-
)
|
|
75
|
+
const ctx = mutatorContext()
|
|
76
|
+
const tableName = getWhereTableName(where)
|
|
77
|
+
if (!tableName) {
|
|
78
|
+
throw new Error(`Must use where('table') style where to pass to can()`)
|
|
79
|
+
}
|
|
80
|
+
await ensurePermission(ctx.tx, ctx.authData, tableName, where, obj)
|
|
128
81
|
setDidRunPermissionCheck(ctx)
|
|
129
82
|
}
|
|
130
83
|
}
|
|
131
84
|
|
|
132
|
-
async function ensurePermission
|
|
133
|
-
PW extends PermissionsWhere,
|
|
134
|
-
Action extends keyof ReturnType<PW>,
|
|
135
|
-
>(
|
|
85
|
+
async function ensurePermission(
|
|
136
86
|
tx: Transaction,
|
|
137
87
|
authData: AuthData | null,
|
|
138
88
|
tableName: TableName,
|
|
139
89
|
where: Where,
|
|
140
|
-
actionIn: Action,
|
|
141
90
|
obj: any // TODO until i can get a working PickPrimaryKeys<'message'>
|
|
142
91
|
): Promise<void> {
|
|
143
92
|
if (authData?.role === 'admin') {
|
|
@@ -145,16 +94,13 @@ export function createPermissions<Schema extends ZeroSchema>({
|
|
|
145
94
|
return
|
|
146
95
|
}
|
|
147
96
|
|
|
148
|
-
const action = String(actionIn)
|
|
149
|
-
const name = `${tableName}.${action}`
|
|
150
|
-
// @ts-ignore TODO its the right type but need to figure out better way to get TableName
|
|
151
97
|
const queryBase = tx.query[tableName] as Query<any, any>
|
|
152
98
|
let query: Query<any, any, any> | null = null
|
|
153
99
|
|
|
154
100
|
try {
|
|
155
101
|
query = queryBase
|
|
156
102
|
.where((eb) => {
|
|
157
|
-
return buildPermissionQuery(authData, eb, where,
|
|
103
|
+
return buildPermissionQuery(authData, eb, where, obj)
|
|
158
104
|
})
|
|
159
105
|
.one()
|
|
160
106
|
|
|
@@ -174,56 +120,8 @@ export function createPermissions<Schema extends ZeroSchema>({
|
|
|
174
120
|
}
|
|
175
121
|
}
|
|
176
122
|
|
|
177
|
-
const readPermissions = definePermissions<AuthData, Schema>(schema, async () => {
|
|
178
|
-
const permissionsEntries = await Promise.all(
|
|
179
|
-
objectEntries(models)
|
|
180
|
-
// non permissioned models dont turn into read permissions
|
|
181
|
-
.filter(([_, model]) => !!model.permissions)
|
|
182
|
-
.map(async ([key, model]) => {
|
|
183
|
-
return await runWithContext(
|
|
184
|
-
{
|
|
185
|
-
authData: { id: '', role: undefined, email: '' },
|
|
186
|
-
} as any,
|
|
187
|
-
() => {
|
|
188
|
-
return [
|
|
189
|
-
key,
|
|
190
|
-
{
|
|
191
|
-
row: {
|
|
192
|
-
select: [
|
|
193
|
-
(authData: AuthData, eb: ExpressionBuilder<any, any>) => {
|
|
194
|
-
const out = model.permissions(eb, authData).read
|
|
195
|
-
|
|
196
|
-
if (out === true) {
|
|
197
|
-
return eb.and()
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (out === false) {
|
|
201
|
-
return eb.cmpLit(true, '=', false)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return out
|
|
205
|
-
},
|
|
206
|
-
],
|
|
207
|
-
// we have permissions on these through our model system with custom mutators:
|
|
208
|
-
insert: ANYONE_CAN,
|
|
209
|
-
update: ANYONE_CAN,
|
|
210
|
-
delete: ANYONE_CAN,
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
]
|
|
214
|
-
}
|
|
215
|
-
)
|
|
216
|
-
})
|
|
217
|
-
)
|
|
218
|
-
|
|
219
|
-
const permissions = Object.fromEntries(permissionsEntries)
|
|
220
|
-
|
|
221
|
-
return permissions as any
|
|
222
|
-
})
|
|
223
|
-
|
|
224
123
|
return {
|
|
225
124
|
can,
|
|
226
125
|
buildPermissionQuery,
|
|
227
|
-
readPermissions,
|
|
228
126
|
}
|
|
229
127
|
}
|
package/src/createZeroClient.tsx
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
HumanReadable,
|
|
3
|
+
Query,
|
|
4
|
+
ReadonlyJSONValue,
|
|
5
|
+
Row,
|
|
6
|
+
SyncedQuery,
|
|
7
|
+
Zero,
|
|
8
|
+
ZeroOptions,
|
|
9
|
+
Schema as ZeroSchema,
|
|
10
|
+
} from '@rocicorp/zero'
|
|
11
|
+
import { syncedQuery } from '@rocicorp/zero'
|
|
2
12
|
import { useZero, ZeroProvider, useQuery as zeroUseQuery } from '@rocicorp/zero/react'
|
|
3
13
|
import { createEmitter, mapObject } from '@vxrn/helpers'
|
|
4
14
|
import { createContext, use, useMemo, type ReactNode } from 'react'
|
|
5
15
|
import { createPermissions } from './createPermissions'
|
|
6
|
-
import { context } from './helpers/context'
|
|
7
16
|
import { createMutators } from './helpers/createMutators'
|
|
8
17
|
import { prettyFormatZeroQuery } from './helpers/prettyFormatZeroQuery'
|
|
9
18
|
import { useZeroDebug } from './helpers/useZeroDebug'
|
|
10
|
-
|
|
19
|
+
import { setAuthData, setSchema } from './state'
|
|
11
20
|
import type { AuthData, GenericModels, GetZeroMutators, ZeroEvent } from './types'
|
|
12
21
|
|
|
13
22
|
export function createZeroClient<
|
|
@@ -18,7 +27,9 @@ export function createZeroClient<
|
|
|
18
27
|
type ZeroInstance = Zero<Schema, ZeroMutators>
|
|
19
28
|
type TableName = keyof ZeroInstance['query']
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
setSchema(schema)
|
|
31
|
+
|
|
32
|
+
const modelWritePermissions = mapObject(models, (val) => val.permissions) as {
|
|
22
33
|
[K in TableName]: K extends keyof Models ? Models[K]['permissions'] : never
|
|
23
34
|
}
|
|
24
35
|
|
|
@@ -34,8 +45,6 @@ export function createZeroClient<
|
|
|
34
45
|
|
|
35
46
|
const permissionsHelpers = createPermissions<Schema>({
|
|
36
47
|
schema,
|
|
37
|
-
models,
|
|
38
|
-
getContext: context,
|
|
39
48
|
environment: 'client',
|
|
40
49
|
})
|
|
41
50
|
|
|
@@ -52,22 +61,15 @@ export function createZeroClient<
|
|
|
52
61
|
// and things generally aren't changing with permissions rapidly, so lets
|
|
53
62
|
// cache the last results and use that when first rendering, they will
|
|
54
63
|
// always update once the query resolves
|
|
55
|
-
function usePermission<
|
|
56
|
-
K extends TableName,
|
|
57
|
-
Action extends 'insert' | 'update' | 'delete' | 'select',
|
|
58
|
-
>(
|
|
64
|
+
function usePermission<K extends TableName>(
|
|
59
65
|
table: K,
|
|
60
|
-
action: Action,
|
|
61
66
|
objOrId: string | Partial<Row<Schema['tables'][K]>> | undefined,
|
|
62
67
|
enabled = typeof objOrId !== 'undefined',
|
|
63
68
|
debug = false
|
|
64
69
|
): boolean | null {
|
|
65
|
-
// we fallback to just table.action, to avoid flickers for now
|
|
66
|
-
const keyBase = `${String(table)}${action}`
|
|
67
|
-
const key = `${keyBase}${typeof objOrId === 'string' ? objOrId : JSON.stringify(objOrId)}`
|
|
68
70
|
// const cacheVal = permissionCache.get(key) ?? permissionCache.get(keyBase)
|
|
69
71
|
const authData = useAuthData()
|
|
70
|
-
const permission =
|
|
72
|
+
const permission = modelWritePermissions[table]
|
|
71
73
|
|
|
72
74
|
const query = (() => {
|
|
73
75
|
let baseQuery = zero.query[table].one()
|
|
@@ -81,7 +83,6 @@ export function createZeroClient<
|
|
|
81
83
|
authData,
|
|
82
84
|
eb,
|
|
83
85
|
permission,
|
|
84
|
-
action,
|
|
85
86
|
objOrId as any
|
|
86
87
|
)
|
|
87
88
|
})
|
|
@@ -94,7 +95,7 @@ export function createZeroClient<
|
|
|
94
95
|
if (debug) {
|
|
95
96
|
console.info(
|
|
96
97
|
`usePermission()`,
|
|
97
|
-
{ data, status,
|
|
98
|
+
{ data, status, authData, permission },
|
|
98
99
|
prettyFormatZeroQuery(query)
|
|
99
100
|
)
|
|
100
101
|
}
|
|
@@ -110,16 +111,97 @@ export function createZeroClient<
|
|
|
110
111
|
return allowed
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
type PlainQueryFn<
|
|
115
|
+
TArg = any,
|
|
116
|
+
TReturn extends Query<any, any, any> = Query<any, any, any>,
|
|
117
|
+
> = (args: TArg) => TReturn
|
|
118
|
+
|
|
119
|
+
type UseQueryOptions = {
|
|
120
|
+
enabled?: boolean | undefined
|
|
121
|
+
ttl?: 'always' | 'never' | number | undefined
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
type QueryResultDetails = ReturnType<typeof zeroUseQuery>[1]
|
|
125
|
+
type QueryResult<TReturn> = readonly [HumanReadable<TReturn>, QueryResultDetails]
|
|
117
126
|
|
|
118
|
-
|
|
127
|
+
const queryCache = new Map<string, SyncedQuery<any, any, any, any, any>>()
|
|
128
|
+
|
|
129
|
+
const parseAny = (x: unknown[]): [ReadonlyJSONValue] => [x[0] as ReadonlyJSONValue]
|
|
130
|
+
|
|
131
|
+
// Overload 1: Original API - Query or SyncedQuery with options
|
|
132
|
+
function useQuery<TTable extends keyof Schema['tables'] & string, TReturn>(
|
|
133
|
+
query:
|
|
134
|
+
| Query<Schema, TTable, TReturn>
|
|
135
|
+
| SyncedQuery<any, any, any, any, Query<Schema, TTable, TReturn>>,
|
|
136
|
+
options?: UseQueryOptions | boolean
|
|
137
|
+
): QueryResult<TReturn>
|
|
138
|
+
|
|
139
|
+
// Overload 2: Plain function with params
|
|
140
|
+
function useQuery<TArg, TTable extends keyof Schema['tables'] & string, TReturn>(
|
|
141
|
+
fn: PlainQueryFn<TArg, Query<Schema, TTable, TReturn>>,
|
|
142
|
+
params: TArg,
|
|
143
|
+
options?: UseQueryOptions | boolean
|
|
144
|
+
): QueryResult<TReturn>
|
|
145
|
+
|
|
146
|
+
// Overload 3: Plain function with no params
|
|
147
|
+
function useQuery<TTable extends keyof Schema['tables'] & string, TReturn>(
|
|
148
|
+
fn: PlainQueryFn<void, Query<Schema, TTable, TReturn>>,
|
|
149
|
+
options?: UseQueryOptions | boolean
|
|
150
|
+
): QueryResult<TReturn>
|
|
151
|
+
|
|
152
|
+
// Implementation - keep it simple with any
|
|
153
|
+
function useQuery(...args: any[]): any {
|
|
154
|
+
const [queryOrFn, paramsOrOptions, optionsArg] = args
|
|
155
|
+
|
|
156
|
+
// Detect which calling pattern is being used
|
|
157
|
+
const isPlainFunction = typeof queryOrFn === 'function' && !('queryName' in queryOrFn)
|
|
158
|
+
|
|
159
|
+
const { actualQuery, options } = useMemo(() => {
|
|
160
|
+
if (!isPlainFunction) {
|
|
161
|
+
// Pattern 1: Original API - useQuery(query, options)
|
|
162
|
+
return {
|
|
163
|
+
actualQuery: queryOrFn,
|
|
164
|
+
options: paramsOrOptions,
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const fn = queryOrFn
|
|
169
|
+
const queryName = fn.name || 'anonymousQuery'
|
|
170
|
+
|
|
171
|
+
// Determine if this is Pattern 2 (with params) or Pattern 3 (no params)
|
|
172
|
+
const hasParams =
|
|
173
|
+
optionsArg !== undefined ||
|
|
174
|
+
(paramsOrOptions &&
|
|
175
|
+
typeof paramsOrOptions === 'object' &&
|
|
176
|
+
!('enabled' in paramsOrOptions) &&
|
|
177
|
+
!('ttl' in paramsOrOptions))
|
|
178
|
+
|
|
179
|
+
const params = hasParams ? paramsOrOptions : undefined
|
|
180
|
+
const opts = hasParams ? optionsArg : paramsOrOptions
|
|
181
|
+
|
|
182
|
+
let synced = queryCache.get(queryName)
|
|
183
|
+
if (!synced) {
|
|
184
|
+
synced = syncedQuery(queryName, parseAny, (arg: ReadonlyJSONValue) => {
|
|
185
|
+
return fn(arg)
|
|
186
|
+
})
|
|
187
|
+
queryCache.set(queryName, synced)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Call the SyncedQuery with params if provided
|
|
191
|
+
const query = params !== undefined ? (synced as any)(params) : synced
|
|
192
|
+
|
|
193
|
+
return { actualQuery: query, options: opts }
|
|
194
|
+
}, [queryOrFn, paramsOrOptions, optionsArg, isPlainFunction])
|
|
195
|
+
|
|
196
|
+
const out = zeroUseQuery(actualQuery, options)
|
|
119
197
|
|
|
120
198
|
if (process.env.NODE_ENV === 'development') {
|
|
121
199
|
// biome-ignore lint/correctness/useHookAtTopLevel: ok
|
|
122
|
-
useZeroDebug(
|
|
200
|
+
useZeroDebug(actualQuery, options, out)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (disable) {
|
|
204
|
+
return [null, { type: 'unknown' }] as never
|
|
123
205
|
}
|
|
124
206
|
|
|
125
207
|
return out
|
|
@@ -134,6 +216,8 @@ export function createZeroClient<
|
|
|
134
216
|
authData?: any
|
|
135
217
|
}) => {
|
|
136
218
|
const mutators = useMemo(() => {
|
|
219
|
+
setAuthData(authData)
|
|
220
|
+
|
|
137
221
|
return createMutators({
|
|
138
222
|
models,
|
|
139
223
|
environment: 'client',
|
package/src/createZeroServer.ts
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
HumanReadable,
|
|
3
|
+
Query,
|
|
4
|
+
ReadonlyJSONValue,
|
|
5
|
+
SyncedQuery,
|
|
6
|
+
Schema as ZeroSchema,
|
|
7
|
+
} from '@rocicorp/zero'
|
|
2
8
|
import type { TransactionProviderInput } from '@rocicorp/zero/pg'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
9
|
+
import { handleGetQueriesRequest, PushProcessor } from '@rocicorp/zero/pg'
|
|
10
|
+
import { zeroNodePg } from '@rocicorp/zero/server/adapters/pg'
|
|
5
11
|
import { assertString, randomId } from '@vxrn/helpers'
|
|
6
|
-
import
|
|
12
|
+
import { Pool } from 'pg'
|
|
7
13
|
import { createPermissions } from './createPermissions'
|
|
8
|
-
import { context, isInZeroMutation } from './helpers/context'
|
|
9
14
|
import { createMutators } from './helpers/createMutators'
|
|
15
|
+
import { isInZeroMutation, mutatorContext } from './helpers/mutatorContext'
|
|
16
|
+
import { setAuthData, setSchema } from './state'
|
|
10
17
|
import type {
|
|
11
18
|
AsyncAction,
|
|
12
19
|
AuthData,
|
|
@@ -19,11 +26,16 @@ export function createZeroServer<
|
|
|
19
26
|
Schema extends ZeroSchema,
|
|
20
27
|
Models extends GenericModels,
|
|
21
28
|
ServerActions extends Record<string, unknown>,
|
|
29
|
+
Queries extends Record<string, SyncedQuery<any, any, any, any, any>> = Record<
|
|
30
|
+
string,
|
|
31
|
+
never
|
|
32
|
+
>,
|
|
22
33
|
>({
|
|
23
34
|
createServerActions,
|
|
24
35
|
database,
|
|
25
36
|
schema,
|
|
26
37
|
models,
|
|
38
|
+
queries,
|
|
27
39
|
}: {
|
|
28
40
|
/**
|
|
29
41
|
* The DB connection string, same as ZERO_UPSTREAM_DB
|
|
@@ -32,26 +44,25 @@ export function createZeroServer<
|
|
|
32
44
|
schema: Schema
|
|
33
45
|
models: Models
|
|
34
46
|
createServerActions: () => ServerActions
|
|
47
|
+
queries?: Queries
|
|
35
48
|
}) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
setSchema(schema)
|
|
50
|
+
|
|
51
|
+
const dbString = assertString(database, `createZeroServer "database"`)
|
|
52
|
+
|
|
53
|
+
const zeroDb = zeroNodePg(
|
|
54
|
+
schema,
|
|
55
|
+
new Pool({
|
|
56
|
+
connectionString: dbString,
|
|
57
|
+
})
|
|
45
58
|
)
|
|
46
59
|
|
|
47
60
|
const permissions = createPermissions<Schema>({
|
|
48
61
|
environment: 'server',
|
|
49
62
|
schema,
|
|
50
|
-
models,
|
|
51
|
-
getContext: context,
|
|
52
63
|
})
|
|
53
64
|
|
|
54
|
-
const processor = new PushProcessor(
|
|
65
|
+
const processor = new PushProcessor(zeroDb)
|
|
55
66
|
|
|
56
67
|
const handleMutationRequest = async ({
|
|
57
68
|
authData,
|
|
@@ -96,6 +107,33 @@ export function createZeroServer<
|
|
|
96
107
|
}
|
|
97
108
|
}
|
|
98
109
|
|
|
110
|
+
const handleQueryRequest = async ({
|
|
111
|
+
authData,
|
|
112
|
+
request,
|
|
113
|
+
}: {
|
|
114
|
+
authData: AuthData | null
|
|
115
|
+
request: Request
|
|
116
|
+
}) => {
|
|
117
|
+
function getQuery(name: string, args: readonly ReadonlyJSONValue[]) {
|
|
118
|
+
const q = queries?.[name]
|
|
119
|
+
if (!q) {
|
|
120
|
+
throw new Error(`No such query: ${name}`)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
// @ts-expect-error zero bug atm
|
|
125
|
+
query: q(...args),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
setAuthData(authData || {})
|
|
130
|
+
const response = await handleGetQueriesRequest(getQuery, schema, request)
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
response,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
99
137
|
const mutate = async (
|
|
100
138
|
run: (tx: Transaction, mutators: GetZeroMutators<Models>) => Promise<void>,
|
|
101
139
|
authData?: Pick<AuthData, 'email' | 'id'> & Partial<AuthData>
|
|
@@ -129,11 +167,11 @@ export function createZeroServer<
|
|
|
129
167
|
>(query: CB): Promise<Returns> {
|
|
130
168
|
try {
|
|
131
169
|
if (isInZeroMutation()) {
|
|
132
|
-
const { tx } =
|
|
170
|
+
const { tx } = mutatorContext()
|
|
133
171
|
return await query(tx)
|
|
134
172
|
}
|
|
135
173
|
// @ts-ignore type
|
|
136
|
-
const output = await
|
|
174
|
+
const output = await zeroDb.transaction(query, dummyTransactionInput)
|
|
137
175
|
return output as any
|
|
138
176
|
} catch (err) {
|
|
139
177
|
console.error(`Error running transaction(): ${err}`)
|
|
@@ -141,16 +179,12 @@ export function createZeroServer<
|
|
|
141
179
|
}
|
|
142
180
|
}
|
|
143
181
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
): Promise<ReturnType<GetQuery>> {
|
|
151
|
-
return await transaction(async (tx) => {
|
|
152
|
-
return await cb(tx.query)
|
|
153
|
-
})
|
|
182
|
+
function query<R>(
|
|
183
|
+
cb: (q: Transaction['query']) => Query<Schema, any, R>
|
|
184
|
+
): Promise<HumanReadable<R>> {
|
|
185
|
+
return transaction(async (tx) => {
|
|
186
|
+
return cb(tx.query)
|
|
187
|
+
}) as any
|
|
154
188
|
}
|
|
155
189
|
|
|
156
190
|
// This is needed temporarily and will be cleaned up in the future.
|
|
@@ -163,6 +197,7 @@ export function createZeroServer<
|
|
|
163
197
|
|
|
164
198
|
return {
|
|
165
199
|
handleMutationRequest,
|
|
200
|
+
handleQueryRequest,
|
|
166
201
|
transaction,
|
|
167
202
|
mutate,
|
|
168
203
|
query,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ensure } from '@vxrn/helpers'
|
|
2
2
|
import type { AuthData } from '../types'
|
|
3
|
-
import {
|
|
3
|
+
import { mutatorContext } from './mutatorContext'
|
|
4
4
|
|
|
5
5
|
export const ensureLoggedIn = (): AuthData => {
|
|
6
|
-
const { authData } =
|
|
6
|
+
const { authData } = mutatorContext()
|
|
7
7
|
ensure(authData, 'logged in')
|
|
8
8
|
return authData
|
|
9
9
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getAuthData } from '../state'
|
|
2
|
+
import type { AuthData } from '../types'
|
|
3
|
+
import { isInZeroMutation, mutatorContext } from './mutatorContext'
|
|
4
|
+
|
|
5
|
+
export function getQueryOrMutatorAuthData(): AuthData {
|
|
6
|
+
if (isInZeroMutation()) {
|
|
7
|
+
return mutatorContext().authData as AuthData
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return getAuthData()
|
|
11
|
+
}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { createAsyncContext } from '@vxrn/helpers'
|
|
2
2
|
import type { MutatorContext } from '../types'
|
|
3
3
|
|
|
4
|
-
// TODO likely should be called "DataContext" or "ZeroContext" as its useful for permissions outside mutators
|
|
5
|
-
|
|
6
4
|
const asyncContext = createAsyncContext<MutatorContext>()
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export function context(): MutatorContext {
|
|
6
|
+
export function mutatorContext(): MutatorContext {
|
|
11
7
|
const currentContext = asyncContext.get()
|
|
12
8
|
if (!currentContext) {
|
|
13
|
-
throw new Error('
|
|
9
|
+
throw new Error('mutatorContext must be called within a mutator')
|
|
14
10
|
}
|
|
15
11
|
|
|
16
12
|
return currentContext
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export * from './createPermissions'
|
|
2
2
|
export * from './helpers/batchQuery'
|
|
3
|
-
export * from './helpers/context'
|
|
4
3
|
export * from './helpers/createMutators'
|
|
5
4
|
export * from './helpers/ensureLoggedIn'
|
|
5
|
+
export * from './helpers/mutatorContext'
|
|
6
6
|
|
|
7
|
+
export * from './createZeroClient'
|
|
7
8
|
export * from './mutations'
|
|
8
9
|
export * from './where'
|
|
9
|
-
export * from './
|
|
10
|
+
export * from './zql'
|
|
10
11
|
|
|
11
12
|
export type * from './types'
|