on-zero 0.7.2-1784530701923 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/asyncTasks.cjs +51 -0
- package/dist/cjs/asyncTasks.native.js +84 -0
- package/dist/cjs/asyncTasks.native.js.map +1 -0
- package/dist/cjs/createZeroDirectServer.cjs +269 -0
- package/dist/cjs/createZeroDirectServer.native.js +287 -0
- package/dist/cjs/createZeroDirectServer.native.js.map +1 -0
- package/dist/cjs/createZeroSQLiteServer.cjs +50 -0
- package/dist/cjs/createZeroSQLiteServer.native.js +58 -0
- package/dist/cjs/createZeroSQLiteServer.native.js.map +1 -0
- package/dist/cjs/createZeroSQLiteServer.test.cjs +595 -0
- package/dist/cjs/createZeroSQLiteServer.test.native.js +647 -0
- package/dist/cjs/createZeroSQLiteServer.test.native.js.map +1 -0
- package/dist/cjs/createZeroServer.cjs +82 -0
- package/dist/cjs/createZeroServer.native.js +93 -0
- package/dist/cjs/createZeroServer.native.js.map +1 -0
- package/dist/cjs/createZeroServer.test.cjs +378 -0
- package/dist/cjs/createZeroServer.test.native.js +448 -0
- package/dist/cjs/createZeroServer.test.native.js.map +1 -0
- package/dist/cjs/createZeroServerCore.cjs +211 -0
- package/dist/cjs/createZeroServerCore.native.js +355 -0
- package/dist/cjs/createZeroServerCore.native.js.map +1 -0
- package/dist/cjs/helpers/createMutators.cjs +3 -4
- package/dist/cjs/helpers/createMutators.native.js +3 -6
- package/dist/cjs/helpers/createMutators.native.js.map +1 -1
- package/dist/cjs/httpPull/auth.test.cjs +197 -0
- package/dist/cjs/httpPull/auth.test.native.js +279 -0
- package/dist/cjs/httpPull/auth.test.native.js.map +1 -0
- package/dist/cjs/httpPull/churn.test.cjs +132 -0
- package/dist/cjs/httpPull/churn.test.native.js +155 -0
- package/dist/cjs/httpPull/churn.test.native.js.map +1 -0
- package/dist/cjs/httpPull/completeAck.test.cjs +69 -0
- package/dist/cjs/httpPull/completeAck.test.native.js +76 -0
- package/dist/cjs/httpPull/completeAck.test.native.js.map +1 -0
- package/dist/cjs/httpPull/fixtureServer.cjs +340 -0
- package/dist/cjs/httpPull/fixtureServer.native.js +534 -0
- package/dist/cjs/httpPull/fixtureServer.native.js.map +1 -0
- package/dist/cjs/httpPull/integration.test.cjs +53 -0
- package/dist/cjs/httpPull/integration.test.native.js +60 -0
- package/dist/cjs/httpPull/integration.test.native.js.map +1 -0
- package/dist/cjs/httpPull/rebase.test.cjs +360 -0
- package/dist/cjs/httpPull/rebase.test.native.js +420 -0
- package/dist/cjs/httpPull/rebase.test.native.js.map +1 -0
- package/dist/cjs/httpPull/relations.test.cjs +107 -0
- package/dist/cjs/httpPull/relations.test.native.js +119 -0
- package/dist/cjs/httpPull/relations.test.native.js.map +1 -0
- package/dist/cjs/httpPull/testHarness.cjs +100 -0
- package/dist/cjs/httpPull/testHarness.native.js +112 -0
- package/dist/cjs/httpPull/testHarness.native.js.map +1 -0
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/index.native.js +1 -0
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/run.cjs +1 -1
- package/dist/cjs/run.native.js +1 -1
- package/dist/cjs/server.cjs +22 -0
- package/dist/cjs/server.native.js +4 -0
- package/dist/cjs/server.native.js.map +1 -0
- package/dist/cjs/sqliteRuntime.cjs +318 -0
- package/dist/cjs/sqliteRuntime.native.js +438 -0
- package/dist/cjs/sqliteRuntime.native.js.map +1 -0
- package/dist/cjs/state.cjs +1 -1
- package/dist/cjs/state.native.js +1 -1
- package/dist/cjs/syncHost.cjs +179 -0
- package/dist/cjs/syncHost.native.js +388 -0
- package/dist/cjs/syncHost.native.js.map +1 -0
- package/dist/cjs/syncHost.test.cjs +240 -0
- package/dist/cjs/syncHost.test.native.js +290 -0
- package/dist/cjs/syncHost.test.native.js.map +1 -0
- package/dist/cjs/zeroRunner.cjs +1 -1
- package/dist/cjs/zeroRunner.native.js +1 -1
- package/dist/esm/asyncTasks.mjs +26 -0
- package/dist/esm/asyncTasks.mjs.map +1 -0
- package/dist/esm/asyncTasks.native.js +56 -0
- package/dist/esm/asyncTasks.native.js.map +1 -0
- package/dist/esm/createZeroDirectServer.mjs +241 -0
- package/dist/esm/createZeroDirectServer.mjs.map +1 -0
- package/dist/esm/createZeroDirectServer.native.js +256 -0
- package/dist/esm/createZeroDirectServer.native.js.map +1 -0
- package/dist/esm/createZeroSQLiteServer.mjs +24 -0
- package/dist/esm/createZeroSQLiteServer.mjs.map +1 -0
- package/dist/esm/createZeroSQLiteServer.native.js +29 -0
- package/dist/esm/createZeroSQLiteServer.native.js.map +1 -0
- package/dist/esm/createZeroSQLiteServer.test.mjs +596 -0
- package/dist/esm/createZeroSQLiteServer.test.mjs.map +1 -0
- package/dist/esm/createZeroSQLiteServer.test.native.js +645 -0
- package/dist/esm/createZeroSQLiteServer.test.native.js.map +1 -0
- package/dist/esm/createZeroServer.mjs +57 -0
- package/dist/esm/createZeroServer.mjs.map +1 -0
- package/dist/esm/createZeroServer.native.js +65 -0
- package/dist/esm/createZeroServer.native.js.map +1 -0
- package/dist/esm/createZeroServer.test.mjs +379 -0
- package/dist/esm/createZeroServer.test.mjs.map +1 -0
- package/dist/esm/createZeroServer.test.native.js +446 -0
- package/dist/esm/createZeroServer.test.native.js.map +1 -0
- package/dist/esm/createZeroServerCore.mjs +186 -0
- package/dist/esm/createZeroServerCore.mjs.map +1 -0
- package/dist/esm/createZeroServerCore.native.js +327 -0
- package/dist/esm/createZeroServerCore.native.js.map +1 -0
- package/dist/esm/helpers/createMutators.mjs +3 -4
- package/dist/esm/helpers/createMutators.mjs.map +1 -1
- package/dist/esm/helpers/createMutators.native.js +3 -6
- package/dist/esm/helpers/createMutators.native.js.map +1 -1
- package/dist/esm/httpPull/auth.test.mjs +198 -0
- package/dist/esm/httpPull/auth.test.mjs.map +1 -0
- package/dist/esm/httpPull/auth.test.native.js +277 -0
- package/dist/esm/httpPull/auth.test.native.js.map +1 -0
- package/dist/esm/httpPull/churn.test.mjs +133 -0
- package/dist/esm/httpPull/churn.test.mjs.map +1 -0
- package/dist/esm/httpPull/churn.test.native.js +153 -0
- package/dist/esm/httpPull/churn.test.native.js.map +1 -0
- package/dist/esm/httpPull/completeAck.test.mjs +70 -0
- package/dist/esm/httpPull/completeAck.test.mjs.map +1 -0
- package/dist/esm/httpPull/completeAck.test.native.js +74 -0
- package/dist/esm/httpPull/completeAck.test.native.js.map +1 -0
- package/dist/esm/httpPull/fixtureServer.mjs +315 -0
- package/dist/esm/httpPull/fixtureServer.mjs.map +1 -0
- package/dist/esm/httpPull/fixtureServer.native.js +506 -0
- package/dist/esm/httpPull/fixtureServer.native.js.map +1 -0
- package/dist/esm/httpPull/integration.test.mjs +54 -0
- package/dist/esm/httpPull/integration.test.mjs.map +1 -0
- package/dist/esm/httpPull/integration.test.native.js +58 -0
- package/dist/esm/httpPull/integration.test.native.js.map +1 -0
- package/dist/esm/httpPull/rebase.test.mjs +361 -0
- package/dist/esm/httpPull/rebase.test.mjs.map +1 -0
- package/dist/esm/httpPull/rebase.test.native.js +418 -0
- package/dist/esm/httpPull/rebase.test.native.js.map +1 -0
- package/dist/esm/httpPull/relations.test.mjs +108 -0
- package/dist/esm/httpPull/relations.test.mjs.map +1 -0
- package/dist/esm/httpPull/relations.test.native.js +117 -0
- package/dist/esm/httpPull/relations.test.native.js.map +1 -0
- package/dist/esm/httpPull/testHarness.mjs +72 -0
- package/dist/esm/httpPull/testHarness.mjs.map +1 -0
- package/dist/esm/httpPull/testHarness.native.js +81 -0
- package/dist/esm/httpPull/testHarness.native.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +1 -0
- package/dist/esm/index.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 +1 -1
- package/dist/esm/server.mjs +4 -0
- package/dist/esm/server.mjs.map +1 -0
- package/dist/esm/server.native.js +2 -0
- package/dist/esm/server.native.js.map +1 -0
- package/dist/esm/sqliteRuntime.mjs +289 -0
- package/dist/esm/sqliteRuntime.mjs.map +1 -0
- package/dist/esm/sqliteRuntime.native.js +406 -0
- package/dist/esm/sqliteRuntime.native.js.map +1 -0
- package/dist/esm/state.mjs +1 -1
- package/dist/esm/state.native.js +1 -1
- package/dist/esm/syncHost.mjs +153 -0
- package/dist/esm/syncHost.mjs.map +1 -0
- package/dist/esm/syncHost.native.js +359 -0
- package/dist/esm/syncHost.native.js.map +1 -0
- package/dist/esm/syncHost.test.mjs +217 -0
- package/dist/esm/syncHost.test.mjs.map +1 -0
- package/dist/esm/syncHost.test.native.js +264 -0
- package/dist/esm/syncHost.test.native.js.map +1 -0
- package/dist/esm/zeroRunner.mjs +1 -1
- package/dist/esm/zeroRunner.mjs.map +1 -1
- package/dist/esm/zeroRunner.native.js +1 -1
- package/package.json +23 -8
- package/readme.md +65 -40
- package/src/asyncTasks.ts +36 -0
- package/src/createZeroClient.tsx +1 -1
- package/src/createZeroDirectServer.ts +411 -0
- package/src/createZeroSQLiteServer.test.ts +614 -0
- package/src/createZeroSQLiteServer.ts +56 -0
- package/src/createZeroServer.test.ts +421 -0
- package/src/createZeroServer.ts +105 -0
- package/src/createZeroServerCore.ts +296 -0
- package/src/helpers/createMutators.ts +2 -6
- package/src/httpPull/auth.test.ts +208 -0
- package/src/httpPull/churn.test.ts +147 -0
- package/src/httpPull/completeAck.test.ts +80 -0
- package/src/httpPull/fixtureServer.ts +391 -0
- package/src/httpPull/integration.test.ts +57 -0
- package/src/httpPull/rebase.test.ts +368 -0
- package/src/httpPull/relations.test.ts +135 -0
- package/src/httpPull/testHarness.ts +95 -0
- package/src/index.ts +1 -0
- package/src/run.ts +1 -1
- package/src/server.native.ts +3 -0
- package/src/server.ts +4 -0
- package/src/sqliteRuntime.ts +512 -0
- package/src/state.ts +1 -1
- package/src/syncHost.test.ts +223 -0
- package/src/syncHost.ts +294 -0
- package/src/types.ts +5 -0
- package/src/zeroRunner.ts +1 -1
- package/test-fixtures/server-return-types.ts +58 -21
- package/types/asyncTasks.d.ts +11 -0
- package/types/asyncTasks.d.ts.map +1 -0
- package/types/createZeroDirectServer.d.ts +80 -0
- package/types/createZeroDirectServer.d.ts.map +1 -0
- package/types/createZeroSQLiteServer.d.ts +12 -0
- package/types/createZeroSQLiteServer.d.ts.map +1 -0
- package/types/createZeroSQLiteServer.test.d.ts +2 -0
- package/types/createZeroSQLiteServer.test.d.ts.map +1 -0
- package/types/createZeroServer.d.ts +18 -0
- package/types/createZeroServer.d.ts.map +1 -0
- package/types/createZeroServer.test.d.ts +2 -0
- package/types/createZeroServer.test.d.ts.map +1 -0
- package/types/createZeroServerCore.d.ts +256 -0
- package/types/createZeroServerCore.d.ts.map +1 -0
- package/types/helpers/createMutators.d.ts +1 -2
- package/types/helpers/createMutators.d.ts.map +1 -1
- package/types/httpPull/auth.test.d.ts +2 -0
- package/types/httpPull/auth.test.d.ts.map +1 -0
- package/types/httpPull/churn.test.d.ts +2 -0
- package/types/httpPull/churn.test.d.ts.map +1 -0
- package/types/httpPull/completeAck.test.d.ts +2 -0
- package/types/httpPull/completeAck.test.d.ts.map +1 -0
- package/types/httpPull/fixtureSchema.d.ts +1 -1
- package/types/httpPull/fixtureServer.d.ts +14 -0
- package/types/httpPull/fixtureServer.d.ts.map +1 -0
- package/types/httpPull/integration.test.d.ts +2 -0
- package/types/httpPull/integration.test.d.ts.map +1 -0
- package/types/httpPull/rebase.test.d.ts +2 -0
- package/types/httpPull/rebase.test.d.ts.map +1 -0
- package/types/httpPull/relations.test.d.ts +2 -0
- package/types/httpPull/relations.test.d.ts.map +1 -0
- package/types/httpPull/testHarness.d.ts +32 -0
- package/types/httpPull/testHarness.d.ts.map +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/server.d.ts +5 -0
- package/types/server.d.ts.map +1 -0
- package/types/server.native.d.ts +1 -0
- package/types/server.native.d.ts.map +1 -0
- package/types/sqliteRuntime.d.ts +71 -0
- package/types/sqliteRuntime.d.ts.map +1 -0
- package/types/syncHost.d.ts +42 -0
- package/types/syncHost.d.ts.map +1 -0
- package/types/syncHost.test.d.ts +2 -0
- package/types/syncHost.test.d.ts.map +1 -0
- package/types/types.d.ts +4 -0
- package/types/types.d.ts.map +1 -1
- package/dist/cjs/orez.cjs +0 -227
- package/dist/cjs/orez.native.js +0 -314
- package/dist/cjs/orez.native.js.map +0 -1
- package/dist/cjs/orez.test.cjs +0 -211
- package/dist/cjs/orez.test.native.js +0 -226
- package/dist/cjs/orez.test.native.js.map +0 -1
- package/dist/esm/orez.mjs +0 -202
- package/dist/esm/orez.mjs.map +0 -1
- package/dist/esm/orez.native.js +0 -286
- package/dist/esm/orez.native.js.map +0 -1
- package/dist/esm/orez.test.mjs +0 -188
- package/dist/esm/orez.test.mjs.map +0 -1
- package/dist/esm/orez.test.native.js +0 -200
- package/dist/esm/orez.test.native.js.map +0 -1
- package/src/orez.test.ts +0 -195
- package/src/orez.ts +0 -331
- package/types/orez.d.ts +0 -53
- package/types/orez.d.ts.map +0 -1
- package/types/orez.test.d.ts +0 -2
- package/types/orez.test.d.ts.map +0 -1
package/src/orez.ts
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
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
|
|
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
|
|
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
|
-
function defaultMapClaims(claims: NormalizedClaims): AuthData {
|
|
316
|
-
return {
|
|
317
|
-
...claims,
|
|
318
|
-
id: typeof claims.id === 'string' ? claims.id : claims.userID,
|
|
319
|
-
} as AuthData
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
function authToClaims(authData: AuthData | null | undefined): NormalizedClaims {
|
|
323
|
-
const data = (authData ?? {}) as unknown as Record<string, JsonValue>
|
|
324
|
-
const userID = typeof data.id === 'string' ? data.id : 'server'
|
|
325
|
-
return { ...data, userID }
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
function mutationApplicationError(error: unknown): MutationApplicationError {
|
|
329
|
-
const message = error instanceof Error ? error.message : String(error)
|
|
330
|
-
return new MutationApplicationError(message, message)
|
|
331
|
-
}
|
package/types/orez.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { handleQueryRequest as zeroHandleQueryRequest } from '@rocicorp/zero/server';
|
|
2
|
-
import { type JsonValue, type MutatorRegistry, type NormalizedClaims, type SyncExecutor } from 'orez/sync-executor';
|
|
3
|
-
import type { AdminRoleMode, AuthData, GenericModels, MutatorContext, QueryBuilder, Transaction } from './types';
|
|
4
|
-
import type { AnyQueryRegistry, HumanReadable, Query, Schema as ZeroSchema } from '@rocicorp/zero';
|
|
5
|
-
export type ValidateQueryArgs = {
|
|
6
|
-
authData: AuthData | null;
|
|
7
|
-
queryName: string;
|
|
8
|
-
params: unknown;
|
|
9
|
-
};
|
|
10
|
-
export type ValidateMutationArgs = {
|
|
11
|
-
authData: AuthData | null;
|
|
12
|
-
mutatorName: string;
|
|
13
|
-
tableName: string;
|
|
14
|
-
args: unknown;
|
|
15
|
-
};
|
|
16
|
-
export type ValidateQueryFn = (args: ValidateQueryArgs) => void;
|
|
17
|
-
export type ValidateMutationFn = (args: ValidateMutationArgs) => void | Promise<void>;
|
|
18
|
-
type MutateAuthData = Pick<AuthData, 'id'> & Partial<AuthData>;
|
|
19
|
-
export type MutateOptions = {
|
|
20
|
-
authData?: MutateAuthData;
|
|
21
|
-
};
|
|
22
|
-
export type ServerMutate<Models extends GenericModels> = {
|
|
23
|
-
[Key in keyof Models]: {
|
|
24
|
-
[K in keyof Models[Key]['mutate']]: Models[Key]['mutate'][K] extends (ctx: MutatorContext, arg: infer Arg) => any ? (arg: Arg, options?: MutateOptions) => Promise<void> : (options?: MutateOptions) => Promise<void>;
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
export type CreateOrezBindingsOptions<Schema extends ZeroSchema, Models extends GenericModels, ServerActions extends Record<string, unknown>> = {
|
|
28
|
-
schema: Schema;
|
|
29
|
-
models: Models;
|
|
30
|
-
createServerActions: () => ServerActions;
|
|
31
|
-
queries?: AnyQueryRegistry;
|
|
32
|
-
mutations?: Record<string, Record<string, unknown>>;
|
|
33
|
-
validateQuery?: ValidateQueryFn;
|
|
34
|
-
validateMutation?: ValidateMutationFn;
|
|
35
|
-
defaultAllowAdminRole?: AdminRoleMode;
|
|
36
|
-
mapClaims?: (claims: NormalizedClaims) => AuthData;
|
|
37
|
-
};
|
|
38
|
-
export type OrezBindings<Schema extends ZeroSchema, Models extends GenericModels> = {
|
|
39
|
-
mutators: MutatorRegistry<Schema>;
|
|
40
|
-
resolveQuery(name: string, args: readonly JsonValue[], claims: NormalizedClaims): Promise<JsonValue>;
|
|
41
|
-
transformQueryRequest(options: {
|
|
42
|
-
authData: AuthData | null;
|
|
43
|
-
request: Request;
|
|
44
|
-
}): ReturnType<typeof zeroHandleQueryRequest>;
|
|
45
|
-
server(executor: SyncExecutor<Schema>): {
|
|
46
|
-
mutate: ServerMutate<Models>;
|
|
47
|
-
transaction<Value>(claims: NormalizedClaims, work: (tx: Transaction) => Value | Promise<Value>): Promise<Value>;
|
|
48
|
-
query<Result>(claims: NormalizedClaims, work: (q: QueryBuilder) => Query<any, Schema, Result>): Promise<HumanReadable<Result>>;
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
export declare function createOrezBindings<Schema extends ZeroSchema, Models extends GenericModels, ServerActions extends Record<string, unknown>>(options: CreateOrezBindingsOptions<Schema, Models, ServerActions>): OrezBindings<Schema, Models>;
|
|
52
|
-
export {};
|
|
53
|
-
//# sourceMappingURL=orez.d.ts.map
|
package/types/orez.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"orez.d.ts","sourceRoot":"","sources":["../src/orez.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,IAAI,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AAEpF,OAAO,EAGL,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAClB,MAAM,oBAAoB,CAAA;AAY3B,OAAO,KAAK,EACV,aAAa,EACb,QAAQ,EACR,aAAa,EACb,cAAc,EACd,YAAY,EACZ,WAAW,EACZ,MAAM,SAAS,CAAA;AAChB,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,KAAK,EACL,MAAM,IAAI,UAAU,EACrB,MAAM,gBAAgB,C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AM,EAAE,gBAAgB,EACxB,IAAI,EAAE,CAAC,CAAC,EAAE,YAAY,KAAK,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,GACpD,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAA;KAClC,CAAA;CACF,CAAA;AAED,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,UAAU,EACzB,MAAM,SAAS,aAAa,EAC5B,aAAa,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAE7C,OAAO,EAAE,yBAAyB,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,CAAC,GAChE,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CA8J9B"}
|
package/types/orez.test.d.ts
DELETED
package/types/orez.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"orez.test.d.ts","sourceRoot":"","sources":["../src/orez.test.ts"],"names":[],"mappings":""}
|