rastack 0.0.50 → 0.0.52
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/CHANGELOG.md +4 -0
- package/auth/context.ts +73 -0
- package/auth/core.ts +293 -0
- package/auth/index.ts +23 -0
- package/components/auto-form/AutoForm.tsx +32 -12
- package/components/auto-form/use-auto-form.ts +75 -6
- package/components/data-table/DataTable.tsx +72 -45
- package/components/data-table/use-data-table.ts +57 -3
- package/components/index.ts +2 -0
- package/db.ts +9 -0
- package/dist/admin.js +13 -13
- package/dist/compile/analyze.d.ts +1 -7
- package/dist/compile/analyze.js +19 -70
- package/dist/compile/entities.js +163 -16
- package/dist/compile/model.d.ts +7 -3
- package/dist/compile/openapi.js +13 -3
- package/dist/compile/program.d.ts +2 -1
- package/dist/compile/program.js +7 -2
- package/dist/define/auth.d.ts +27 -0
- package/dist/define/auth.js +42 -0
- package/dist/define/db.d.ts +24 -1
- package/dist/define/db.js +1 -1
- package/dist/define/index.d.ts +82 -18
- package/dist/define/index.js +27 -21
- package/dist/define/manifest.d.ts +64 -0
- package/dist/define/manifest.js +259 -0
- package/dist/define/markers.d.ts +80 -0
- package/dist/define/markers.js +37 -0
- package/dist/define/types.d.ts +10 -0
- package/dist/define/types.js +26 -0
- package/dist/plugin/core.d.ts +108 -0
- package/dist/plugin/core.js +198 -0
- package/dist/plugin/index.d.ts +112 -0
- package/dist/plugin/index.js +203 -0
- package/dist/wasm/rastack_wasm.js +1 -1
- package/dist/wasm/rastack_wasm_bg.wasm +0 -0
- package/hooks/data.ts +18 -6
- package/hooks/entity.ts +11 -5
- package/hooks/form/entity-form.ts +12 -8
- package/hooks/manifest.ts +50 -14
- package/hooks/registry.ts +25 -8
- package/package.json +1 -1
- package/plugin.ts +8 -0
- package/provider/index.ts +1 -5
- package/provider/provider.tsx +61 -16
- package/provider/types.ts +26 -7
- package/provider/warehouse.ts +4 -3
- package/provider/wasm.ts +3 -1
- package/runtime.ts +1 -0
- package/src/compile/analyze.ts +27 -65
- package/src/compile/entities.ts +193 -19
- package/src/compile/model.ts +8 -9
- package/src/compile/openapi.ts +11 -3
- package/src/compile/program.ts +10 -3
- package/src/define/auth.ts +28 -0
- package/src/define/db.ts +26 -4
- package/src/define/index.ts +118 -24
- package/src/define/manifest.ts +287 -0
- package/src/define/markers.ts +83 -0
- package/src/define/types.ts +11 -0
- package/src/plugin/core.ts +236 -0
- package/src/plugin/index.ts +243 -0
- package/test/auth.spec.ts +207 -0
- package/test/compile.spec.ts +18 -7
- package/test/components.spec.ts +116 -0
- package/test/entities.spec.ts +123 -6
- package/test/plugin.spec.ts +315 -0
- package/test/runtime-manifest.spec.ts +309 -0
- package/test/typed-hooks.spec.ts +7 -2
- package/types.ts +7 -2
- package/wasm/rastack_wasm.js +1 -1
- package/wasm/rastack_wasm_bg.wasm +0 -0
package/dist/define/index.d.ts
CHANGED
|
@@ -22,14 +22,31 @@
|
|
|
22
22
|
* });
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
|
+
/**
|
|
26
|
+
* Every constraint a scalar builder may be handed, merged — the shape the
|
|
27
|
+
* runtime value carries so a manifest can be assembled without the compiler.
|
|
28
|
+
*/
|
|
29
|
+
export interface ScalarFieldOptions {
|
|
30
|
+
maxLength?: number;
|
|
31
|
+
unique?: boolean;
|
|
32
|
+
primaryKey?: boolean;
|
|
33
|
+
default?: unknown;
|
|
34
|
+
null?: boolean;
|
|
35
|
+
autoNow?: boolean;
|
|
36
|
+
autoNowAdd?: boolean;
|
|
37
|
+
}
|
|
25
38
|
/**
|
|
26
39
|
* A scalar field. The literal `__rastackScalar` type is what the compiler
|
|
27
40
|
* reads; `Nullable` phantom-carries the `null: true` option so {@link RowOf}
|
|
28
|
-
* can add `| null` to the inferred row type.
|
|
41
|
+
* can add `| null` to the inferred row type. The authored options ride along
|
|
42
|
+
* on the runtime value too, so `manifestFromResources()` can build the same
|
|
43
|
+
* manifest `rastack compile` emits — with no compile step at all.
|
|
29
44
|
*/
|
|
30
45
|
export interface Scalar<T extends string, Nullable extends boolean = boolean> {
|
|
31
46
|
readonly __rastackScalar: T;
|
|
32
47
|
readonly __rastackNullable?: Nullable;
|
|
48
|
+
/** The constraint options as authored — carried at runtime. */
|
|
49
|
+
readonly options?: ScalarFieldOptions;
|
|
33
50
|
}
|
|
34
51
|
/** Whether an options literal declares `null: true` (as a type). */
|
|
35
52
|
type NullOf<O> = O extends {
|
|
@@ -37,12 +54,16 @@ type NullOf<O> = O extends {
|
|
|
37
54
|
} ? true : false;
|
|
38
55
|
/**
|
|
39
56
|
* A foreign-key field. `App`/`Model` are carried as string-literal type
|
|
40
|
-
* arguments so the compiler can recover the relation target purely from
|
|
57
|
+
* arguments so the compiler can recover the relation target purely from
|
|
58
|
+
* types; the target thunk is *also* kept on the runtime value so
|
|
59
|
+
* `manifestFromResources()` can recover the relation with no compiler.
|
|
41
60
|
*/
|
|
42
61
|
export interface Ref<App extends string, Model extends string> {
|
|
43
62
|
readonly __rastackRef: true;
|
|
44
63
|
readonly __app: App;
|
|
45
64
|
readonly __model: Model;
|
|
65
|
+
/** The authored `() => Resource` thunk — carried at runtime. */
|
|
66
|
+
readonly target?: () => ResourceType<App, Model, any>;
|
|
46
67
|
}
|
|
47
68
|
export interface StringOptions {
|
|
48
69
|
maxLength?: number;
|
|
@@ -67,24 +88,24 @@ export interface DateOptions {
|
|
|
67
88
|
null?: boolean;
|
|
68
89
|
}
|
|
69
90
|
/**
|
|
70
|
-
* Field builders. Each returns a
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
91
|
+
* Field builders. Each returns a value whose *type* encodes the field kind
|
|
92
|
+
* (what the compiler reads — it analyses the source, it does not execute it)
|
|
93
|
+
* and whose runtime shape keeps the authored options/target, so the same
|
|
94
|
+
* definition doubles as its own manifest via `manifestFromResources()`.
|
|
74
95
|
*/
|
|
75
96
|
export declare const s: {
|
|
76
|
-
string<const O extends StringOptions = StringOptions>(
|
|
77
|
-
int<const O extends NumberOptions = NumberOptions>(
|
|
78
|
-
float<const O extends NumberOptions = NumberOptions>(
|
|
79
|
-
bool<const O extends BoolOptions = BoolOptions>(
|
|
80
|
-
datetime<const O extends DateOptions = DateOptions>(
|
|
81
|
-
uuid<const O extends StringOptions = StringOptions>(
|
|
97
|
+
string<const O extends StringOptions = StringOptions>(opts?: O): Scalar<"string", NullOf<O>>;
|
|
98
|
+
int<const O extends NumberOptions = NumberOptions>(opts?: O): Scalar<"int", NullOf<O>>;
|
|
99
|
+
float<const O extends NumberOptions = NumberOptions>(opts?: O): Scalar<"float", NullOf<O>>;
|
|
100
|
+
bool<const O extends BoolOptions = BoolOptions>(opts?: O): Scalar<"bool", NullOf<O>>;
|
|
101
|
+
datetime<const O extends DateOptions = DateOptions>(opts?: O): Scalar<"datetime", NullOf<O>>;
|
|
102
|
+
uuid<const O extends StringOptions = StringOptions>(opts?: O): Scalar<"uuid", NullOf<O>>;
|
|
82
103
|
/**
|
|
83
104
|
* A foreign key. The target is `() => SomeResource`; the compiler resolves
|
|
84
|
-
* the relation from the *type* `Ref<App, Model>` this returns
|
|
85
|
-
*
|
|
105
|
+
* the relation from the *type* `Ref<App, Model>` this returns, and the
|
|
106
|
+
* runtime keeps the thunk so `manifestFromResources()` can resolve it too.
|
|
86
107
|
*/
|
|
87
|
-
ref<A extends string, M extends string>(
|
|
108
|
+
ref<A extends string, M extends string>(thunk: () => ResourceType<A, M, any>): Ref<A, M>;
|
|
88
109
|
};
|
|
89
110
|
export type AnyField = Scalar<string> | Ref<string, string>;
|
|
90
111
|
/** Local-first sync configuration (mirrors the old Python `class Sync:`). */
|
|
@@ -103,17 +124,34 @@ export interface SyncOptions {
|
|
|
103
124
|
* - `ownerField` — the field that records the owning identity (the verified
|
|
104
125
|
* token `sub`). Rows are only listed/read/written by their owner; the field
|
|
105
126
|
* is stamped from the token on create and immutable on update.
|
|
127
|
+
* - `groupField` — the field that records the owning *group* (a verified
|
|
128
|
+
* `cognito:groups` value). Rows are only visible to members of their group,
|
|
129
|
+
* and the dataset defaults to per-group physical partitioning.
|
|
106
130
|
* - `scope: "owner"` — additionally *physically* partitions the resource's
|
|
107
131
|
* Iceberg table per identity (`tenants/{sub}/{app}.db/{model}/…`), so
|
|
108
132
|
* object-storage credentials (e.g. Cognito Identity Pool temp creds) can be
|
|
109
133
|
* IAM-restricted to `tenants/${"${cognito-identity.amazonaws.com:sub}"}/*`.
|
|
134
|
+
* `scope: "group"` partitions per group instead (`groups/{name}/…`);
|
|
135
|
+
* `scope: "shared"` keeps one table.
|
|
110
136
|
* - `adminGroups` — token groups (`cognito:groups`) that bypass row filters.
|
|
137
|
+
*
|
|
138
|
+
* Class/interface entities express the same policy in the type system — the
|
|
139
|
+
* `Owner`/`Group` field brands and `IsAuthenticated`/`AdminGroups<…>`/
|
|
140
|
+
* `OwnerScoped` markers from `rastack/auth`.
|
|
111
141
|
*/
|
|
112
142
|
export interface AccessOptions {
|
|
113
143
|
ownerField?: string;
|
|
114
|
-
|
|
144
|
+
groupField?: string;
|
|
145
|
+
scope?: "owner" | "group" | "shared";
|
|
115
146
|
adminGroups?: string[];
|
|
116
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* The resource-level permission gate — which paths an identity may hit at all.
|
|
150
|
+
* Enforced by `rastack-api-core` before any row is read; mirrored client-side
|
|
151
|
+
* by `rastack/auth`'s `can()` so components hide what the server would reject.
|
|
152
|
+
* Unset means `authenticated` — secure by default.
|
|
153
|
+
*/
|
|
154
|
+
export type Permission = "authenticatedOrReadOnly" | "authenticated" | "public";
|
|
117
155
|
/**
|
|
118
156
|
* One edge of a resource's state machine. `from` is the state (or states) the
|
|
119
157
|
* transition may fire from, `to` the state it enters, and `set` the effect —
|
|
@@ -172,7 +210,7 @@ export interface ResourceOptions {
|
|
|
172
210
|
/** Local-first replication config. */
|
|
173
211
|
sync?: SyncOptions;
|
|
174
212
|
/** Access policy (secure by default). */
|
|
175
|
-
permission?:
|
|
213
|
+
permission?: Permission;
|
|
176
214
|
/** Row-level security + physical tenant partitioning. */
|
|
177
215
|
access?: AccessOptions;
|
|
178
216
|
/** Declarative state machine over one field, enforced on every write surface. */
|
|
@@ -229,4 +267,30 @@ export type RowOf<R> = R extends ResourceType<string, string, infer F, ResourceO
|
|
|
229
267
|
} & {
|
|
230
268
|
[K in keyof F]: FieldValueOf<F[K]>;
|
|
231
269
|
} : never;
|
|
232
|
-
|
|
270
|
+
/** The `"app.model"` registry key a `resource()` definition implies. */
|
|
271
|
+
export type EntityKeyOf<R> = R extends ResourceType<infer A, infer M, any, ResourceOptions> ? `${A}.${M}` : never;
|
|
272
|
+
/**
|
|
273
|
+
* The `EntityTypes` registry entries a module of `resource()` definitions
|
|
274
|
+
* implies — the *hand-written* alternative to the `rastack-env.d.ts` that
|
|
275
|
+
* `rastack compile` emits. Point it at your resources module once and the
|
|
276
|
+
* registry tracks it forever, with no compile step in the loop:
|
|
277
|
+
*
|
|
278
|
+
* ```ts
|
|
279
|
+
* // rastack-env.d.ts — written once by you, never regenerated
|
|
280
|
+
* import type { EntityTypesOf } from "rastack/define";
|
|
281
|
+
* declare module "rastack/hooks/registry" {
|
|
282
|
+
* interface EntityTypes
|
|
283
|
+
* extends EntityTypesOf<typeof import("./resources/airports")> {}
|
|
284
|
+
* }
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* Non-resource exports are dropped, so `typeof import("…")` over a whole
|
|
288
|
+
* module is safe. (Class entities carry their own row type — they never need
|
|
289
|
+
* the registry; interfaces still register via the compile-emitted file.)
|
|
290
|
+
*/
|
|
291
|
+
export type EntityTypesOf<M> = {
|
|
292
|
+
[K in keyof M as EntityKeyOf<M[K]>]: RowOf<M[K]>;
|
|
293
|
+
};
|
|
294
|
+
export { isResourceValue, manifestFromResources, resourceModel, toManifest, } from "./manifest";
|
|
295
|
+
export type { AnyResourceValue, ManifestInput } from "./manifest";
|
|
296
|
+
export type { Manifest, ResourceModel } from "../compile/model";
|
package/dist/define/index.js
CHANGED
|
@@ -24,40 +24,40 @@
|
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
-
exports.s = void 0;
|
|
27
|
+
exports.toManifest = exports.resourceModel = exports.manifestFromResources = exports.isResourceValue = exports.s = void 0;
|
|
28
28
|
exports.resource = resource;
|
|
29
29
|
/**
|
|
30
|
-
* Field builders. Each returns a
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
30
|
+
* Field builders. Each returns a value whose *type* encodes the field kind
|
|
31
|
+
* (what the compiler reads — it analyses the source, it does not execute it)
|
|
32
|
+
* and whose runtime shape keeps the authored options/target, so the same
|
|
33
|
+
* definition doubles as its own manifest via `manifestFromResources()`.
|
|
34
34
|
*/
|
|
35
35
|
exports.s = {
|
|
36
|
-
string(
|
|
37
|
-
return { __rastackScalar: "string" };
|
|
36
|
+
string(opts) {
|
|
37
|
+
return { __rastackScalar: "string", options: opts };
|
|
38
38
|
},
|
|
39
|
-
int(
|
|
40
|
-
return { __rastackScalar: "int" };
|
|
39
|
+
int(opts) {
|
|
40
|
+
return { __rastackScalar: "int", options: opts };
|
|
41
41
|
},
|
|
42
|
-
float(
|
|
43
|
-
return { __rastackScalar: "float" };
|
|
42
|
+
float(opts) {
|
|
43
|
+
return { __rastackScalar: "float", options: opts };
|
|
44
44
|
},
|
|
45
|
-
bool(
|
|
46
|
-
return { __rastackScalar: "bool" };
|
|
45
|
+
bool(opts) {
|
|
46
|
+
return { __rastackScalar: "bool", options: opts };
|
|
47
47
|
},
|
|
48
|
-
datetime(
|
|
49
|
-
return { __rastackScalar: "datetime" };
|
|
48
|
+
datetime(opts) {
|
|
49
|
+
return { __rastackScalar: "datetime", options: opts };
|
|
50
50
|
},
|
|
51
|
-
uuid(
|
|
52
|
-
return { __rastackScalar: "uuid" };
|
|
51
|
+
uuid(opts) {
|
|
52
|
+
return { __rastackScalar: "uuid", options: opts };
|
|
53
53
|
},
|
|
54
54
|
/**
|
|
55
55
|
* A foreign key. The target is `() => SomeResource`; the compiler resolves
|
|
56
|
-
* the relation from the *type* `Ref<App, Model>` this returns
|
|
57
|
-
*
|
|
56
|
+
* the relation from the *type* `Ref<App, Model>` this returns, and the
|
|
57
|
+
* runtime keeps the thunk so `manifestFromResources()` can resolve it too.
|
|
58
58
|
*/
|
|
59
|
-
ref(
|
|
60
|
-
return { __rastackRef: true };
|
|
59
|
+
ref(thunk) {
|
|
60
|
+
return { __rastackRef: true, target: thunk };
|
|
61
61
|
},
|
|
62
62
|
};
|
|
63
63
|
/** Define a resource. Capture app/model (and options) as literal types. */
|
|
@@ -72,3 +72,9 @@ function resource(app, model, fields, options = {}) {
|
|
|
72
72
|
options,
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
|
+
// The runtime manifest builder — a resource() definition is its own manifest.
|
|
76
|
+
var manifest_1 = require("./manifest");
|
|
77
|
+
Object.defineProperty(exports, "isResourceValue", { enumerable: true, get: function () { return manifest_1.isResourceValue; } });
|
|
78
|
+
Object.defineProperty(exports, "manifestFromResources", { enumerable: true, get: function () { return manifest_1.manifestFromResources; } });
|
|
79
|
+
Object.defineProperty(exports, "resourceModel", { enumerable: true, get: function () { return manifest_1.resourceModel; } });
|
|
80
|
+
Object.defineProperty(exports, "toManifest", { enumerable: true, get: function () { return manifest_1.toManifest; } });
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runtime manifest builder — `resource()` definitions are their own
|
|
3
|
+
* manifest, **no `rastack compile` step required**.
|
|
4
|
+
*
|
|
5
|
+
* The DSL values carry everything the canonical manifest needs at runtime
|
|
6
|
+
* (field kinds, constraint options, FK target thunks, resource options), so
|
|
7
|
+
* this module assembles the exact `schema.rastack.json` shape the compiler
|
|
8
|
+
* emits — same fields, same relations, same normalisation — as a pure
|
|
9
|
+
* function:
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* import { Airport, Terminal, Flight } from "./resources/airports";
|
|
13
|
+
*
|
|
14
|
+
* <RAStackProvider mode="local" manifest={[Airport, Terminal, Flight]}>
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The compiler (`tools/src/compile/analyze.ts`) imports the option
|
|
18
|
+
* normalisers below, so the two paths cannot drift: `rastack compile` remains
|
|
19
|
+
* the way to get class/interface entities, `openapi.json`, and the artifacts
|
|
20
|
+
* the Rust backend and admin read — but the typed hooks and the in-browser
|
|
21
|
+
* engine need only this.
|
|
22
|
+
*
|
|
23
|
+
* Everything here is browser-safe: no `typescript`, no filesystem.
|
|
24
|
+
*/
|
|
25
|
+
import type { AccessModel, AdminModel, Manifest, ResourceModel, SyncModel } from "../compile/model";
|
|
26
|
+
import type { ResourceType } from "./index";
|
|
27
|
+
/** Any `resource()` value, generics erased. */
|
|
28
|
+
export type AnyResourceValue = ResourceType<string, string, any, any>;
|
|
29
|
+
/** Everything `toManifest()` understands — see {@link toManifest}. */
|
|
30
|
+
export type ManifestInput = Manifest | object | string | AnyResourceValue | readonly AnyResourceValue[];
|
|
31
|
+
/** Whether a value is a `resource()` definition. */
|
|
32
|
+
export declare function isResourceValue(value: unknown): value is AnyResourceValue;
|
|
33
|
+
/**
|
|
34
|
+
* One `resource()` value → its canonical {@link ResourceModel} — the same
|
|
35
|
+
* shape `rastack compile` derives from the source, produced from the runtime
|
|
36
|
+
* value instead.
|
|
37
|
+
*/
|
|
38
|
+
export declare function resourceModel(res: AnyResourceValue): ResourceModel;
|
|
39
|
+
/**
|
|
40
|
+
* Assemble the canonical manifest from `resource()` values at runtime —
|
|
41
|
+
* accepts the values themselves, arrays of them, or whole module namespaces
|
|
42
|
+
* (`import * as resources from "./resources"`), in any mix. Output order
|
|
43
|
+
* matches the compiler's (app, then model), so the result is byte-for-byte
|
|
44
|
+
* the manifest `rastack compile` would emit for the same definitions.
|
|
45
|
+
*/
|
|
46
|
+
export declare function manifestFromResources(...inputs: Array<AnyResourceValue | readonly AnyResourceValue[] | Record<string, unknown>>): Manifest;
|
|
47
|
+
/**
|
|
48
|
+
* Normalise anything a provider may be handed as "the manifest": a compiled
|
|
49
|
+
* manifest (object or JSON string), a `resource()` value, an array of them,
|
|
50
|
+
* or a module namespace containing them. This is what lets
|
|
51
|
+
* `<RAStackProvider manifest={[Airport, Terminal]}>` work with no compiled
|
|
52
|
+
* `schema.rastack.json` anywhere.
|
|
53
|
+
*/
|
|
54
|
+
export declare function toManifest(input: ManifestInput | null | undefined): Manifest | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Attach a normalised `transitions` block to a resource and derive what the
|
|
57
|
+
* machine implies about its state field: the `states` become the field's
|
|
58
|
+
* allowed-value set (`enum` in OpenAPI, the `member` gate in validation), and
|
|
59
|
+
* `initial` becomes its default when the field doesn't declare one.
|
|
60
|
+
*/
|
|
61
|
+
export declare function applyTransitions(resource: ResourceModel, raw: unknown): void;
|
|
62
|
+
export declare function normaliseSync(sync: any): SyncModel | undefined;
|
|
63
|
+
export declare function normaliseAccess(access: any): AccessModel | undefined;
|
|
64
|
+
export declare function normaliseAdmin(admin: any): AdminModel | false | undefined;
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The runtime manifest builder — `resource()` definitions are their own
|
|
4
|
+
* manifest, **no `rastack compile` step required**.
|
|
5
|
+
*
|
|
6
|
+
* The DSL values carry everything the canonical manifest needs at runtime
|
|
7
|
+
* (field kinds, constraint options, FK target thunks, resource options), so
|
|
8
|
+
* this module assembles the exact `schema.rastack.json` shape the compiler
|
|
9
|
+
* emits — same fields, same relations, same normalisation — as a pure
|
|
10
|
+
* function:
|
|
11
|
+
*
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { Airport, Terminal, Flight } from "./resources/airports";
|
|
14
|
+
*
|
|
15
|
+
* <RAStackProvider mode="local" manifest={[Airport, Terminal, Flight]}>
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* The compiler (`tools/src/compile/analyze.ts`) imports the option
|
|
19
|
+
* normalisers below, so the two paths cannot drift: `rastack compile` remains
|
|
20
|
+
* the way to get class/interface entities, `openapi.json`, and the artifacts
|
|
21
|
+
* the Rust backend and admin read — but the typed hooks and the in-browser
|
|
22
|
+
* engine need only this.
|
|
23
|
+
*
|
|
24
|
+
* Everything here is browser-safe: no `typescript`, no filesystem.
|
|
25
|
+
*/
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.isResourceValue = isResourceValue;
|
|
28
|
+
exports.resourceModel = resourceModel;
|
|
29
|
+
exports.manifestFromResources = manifestFromResources;
|
|
30
|
+
exports.toManifest = toManifest;
|
|
31
|
+
exports.applyTransitions = applyTransitions;
|
|
32
|
+
exports.normaliseSync = normaliseSync;
|
|
33
|
+
exports.normaliseAccess = normaliseAccess;
|
|
34
|
+
exports.normaliseAdmin = normaliseAdmin;
|
|
35
|
+
const transitions_1 = require("../validate/transitions");
|
|
36
|
+
/** Whether a value is a `resource()` definition. */
|
|
37
|
+
function isResourceValue(value) {
|
|
38
|
+
return (!!value &&
|
|
39
|
+
typeof value === "object" &&
|
|
40
|
+
value.__rastack === "resource");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* One `resource()` value → its canonical {@link ResourceModel} — the same
|
|
44
|
+
* shape `rastack compile` derives from the source, produced from the runtime
|
|
45
|
+
* value instead.
|
|
46
|
+
*/
|
|
47
|
+
function resourceModel(res) {
|
|
48
|
+
const fields = [];
|
|
49
|
+
const relations = [];
|
|
50
|
+
for (const [name, value] of Object.entries((res.fields ?? {}))) {
|
|
51
|
+
const field = runtimeField(res, name, value);
|
|
52
|
+
if (!field)
|
|
53
|
+
continue;
|
|
54
|
+
fields.push(field);
|
|
55
|
+
if (field.type === "fk" && field.relation) {
|
|
56
|
+
relations.push({
|
|
57
|
+
field: name,
|
|
58
|
+
target: `${field.relation.app}.${field.relation.model}`,
|
|
59
|
+
kind: "fk",
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const options = (res.options ?? {});
|
|
64
|
+
const model = {
|
|
65
|
+
app: res.app,
|
|
66
|
+
model: res.model,
|
|
67
|
+
fields,
|
|
68
|
+
relations,
|
|
69
|
+
sync: normaliseSync(options.sync),
|
|
70
|
+
admin: normaliseAdmin(options.admin),
|
|
71
|
+
search: options.search,
|
|
72
|
+
ordering: options.ordering,
|
|
73
|
+
permission: options.permission,
|
|
74
|
+
access: normaliseAccess(options.access),
|
|
75
|
+
};
|
|
76
|
+
applyTransitions(model, options.transitions);
|
|
77
|
+
return model;
|
|
78
|
+
}
|
|
79
|
+
/** Resolve one runtime field value to its {@link FieldModel}. */
|
|
80
|
+
function runtimeField(res, name, value) {
|
|
81
|
+
if (!value || typeof value !== "object")
|
|
82
|
+
return undefined;
|
|
83
|
+
const field = value;
|
|
84
|
+
// Foreign key: the runtime value keeps the `() => Resource` thunk, so the
|
|
85
|
+
// relation target is recovered by calling it — the runtime twin of the
|
|
86
|
+
// compiler's "read the Ref<App, Model> type arguments" step.
|
|
87
|
+
if (field.__rastackRef === true) {
|
|
88
|
+
const target = field.target?.();
|
|
89
|
+
if (!isResourceValue(target)) {
|
|
90
|
+
throw new Error(`manifestFromResources: ${res.app}.${res.model}.${name} is an s.ref(...) whose target thunk did not return a resource() value`);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
name,
|
|
94
|
+
type: "fk",
|
|
95
|
+
relation: { app: target.app, model: target.model },
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (typeof field.__rastackScalar !== "string")
|
|
99
|
+
return undefined;
|
|
100
|
+
const model = {
|
|
101
|
+
name,
|
|
102
|
+
type: field.__rastackScalar,
|
|
103
|
+
};
|
|
104
|
+
// Constraint mapping mirrors the compiler's `applyConstraints` exactly.
|
|
105
|
+
const opts = field.options;
|
|
106
|
+
if (opts && typeof opts === "object") {
|
|
107
|
+
if (typeof opts.maxLength === "number")
|
|
108
|
+
model.maxLength = opts.maxLength;
|
|
109
|
+
if (opts.unique === true)
|
|
110
|
+
model.unique = true;
|
|
111
|
+
if (opts.primaryKey === true)
|
|
112
|
+
model.primaryKey = true;
|
|
113
|
+
if (opts.null === true)
|
|
114
|
+
model.null = true;
|
|
115
|
+
if (opts.default !== undefined)
|
|
116
|
+
model.default = opts.default;
|
|
117
|
+
}
|
|
118
|
+
return model;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Assemble the canonical manifest from `resource()` values at runtime —
|
|
122
|
+
* accepts the values themselves, arrays of them, or whole module namespaces
|
|
123
|
+
* (`import * as resources from "./resources"`), in any mix. Output order
|
|
124
|
+
* matches the compiler's (app, then model), so the result is byte-for-byte
|
|
125
|
+
* the manifest `rastack compile` would emit for the same definitions.
|
|
126
|
+
*/
|
|
127
|
+
function manifestFromResources(...inputs) {
|
|
128
|
+
const resources = collectResources(inputs).map(resourceModel);
|
|
129
|
+
resources.sort((a, b) => a.app === b.app ? a.model.localeCompare(b.model) : a.app.localeCompare(b.app));
|
|
130
|
+
return { version: 1, resources };
|
|
131
|
+
}
|
|
132
|
+
/** Flatten values / arrays / module namespaces into the resource list. */
|
|
133
|
+
function collectResources(inputs) {
|
|
134
|
+
const out = [];
|
|
135
|
+
const seen = new Set();
|
|
136
|
+
const push = (res) => {
|
|
137
|
+
if (!seen.has(res)) {
|
|
138
|
+
seen.add(res);
|
|
139
|
+
out.push(res);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
for (const input of inputs) {
|
|
143
|
+
if (isResourceValue(input)) {
|
|
144
|
+
push(input);
|
|
145
|
+
}
|
|
146
|
+
else if (Array.isArray(input)) {
|
|
147
|
+
for (const item of input)
|
|
148
|
+
if (isResourceValue(item))
|
|
149
|
+
push(item);
|
|
150
|
+
}
|
|
151
|
+
else if (input && typeof input === "object") {
|
|
152
|
+
for (const item of Object.values(input)) {
|
|
153
|
+
if (isResourceValue(item))
|
|
154
|
+
push(item);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Normalise anything a provider may be handed as "the manifest": a compiled
|
|
162
|
+
* manifest (object or JSON string), a `resource()` value, an array of them,
|
|
163
|
+
* or a module namespace containing them. This is what lets
|
|
164
|
+
* `<RAStackProvider manifest={[Airport, Terminal]}>` work with no compiled
|
|
165
|
+
* `schema.rastack.json` anywhere.
|
|
166
|
+
*/
|
|
167
|
+
function toManifest(input) {
|
|
168
|
+
if (input === null || input === undefined)
|
|
169
|
+
return undefined;
|
|
170
|
+
if (typeof input === "string")
|
|
171
|
+
return JSON.parse(input);
|
|
172
|
+
if (Array.isArray(input) || isResourceValue(input)) {
|
|
173
|
+
return manifestFromResources(input);
|
|
174
|
+
}
|
|
175
|
+
const record = input;
|
|
176
|
+
// A compiled manifest passes through untouched.
|
|
177
|
+
if (Array.isArray(record.resources))
|
|
178
|
+
return input;
|
|
179
|
+
// A module namespace of resource() exports builds one.
|
|
180
|
+
const values = Object.values(record).filter(isResourceValue);
|
|
181
|
+
if (values.length)
|
|
182
|
+
return manifestFromResources(values);
|
|
183
|
+
return input;
|
|
184
|
+
}
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
// Option normalisers — shared with the compiler (`compile/analyze.ts`), so
|
|
187
|
+
// the runtime and compiled manifests can never disagree about a resource.
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
/**
|
|
190
|
+
* Attach a normalised `transitions` block to a resource and derive what the
|
|
191
|
+
* machine implies about its state field: the `states` become the field's
|
|
192
|
+
* allowed-value set (`enum` in OpenAPI, the `member` gate in validation), and
|
|
193
|
+
* `initial` becomes its default when the field doesn't declare one.
|
|
194
|
+
*/
|
|
195
|
+
function applyTransitions(resource, raw) {
|
|
196
|
+
const transitions = (0, transitions_1.normalizeTransitions)(raw);
|
|
197
|
+
if (!transitions)
|
|
198
|
+
return;
|
|
199
|
+
resource.transitions = transitions;
|
|
200
|
+
const stateField = resource.fields.find((f) => f.name === transitions.field);
|
|
201
|
+
if (stateField && stateField.type !== "fk") {
|
|
202
|
+
stateField.options = [...transitions.states];
|
|
203
|
+
if (stateField.default === undefined && transitions.initial !== undefined) {
|
|
204
|
+
stateField.default = transitions.initial;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function normaliseSync(sync) {
|
|
209
|
+
if (!sync || typeof sync !== "object")
|
|
210
|
+
return undefined;
|
|
211
|
+
const mode = sync.mode === "local" ? "local" : "server";
|
|
212
|
+
if (mode === "server" && !sync.confirmed) {
|
|
213
|
+
// A bare server resource carries no sync semantics.
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
const model = { mode };
|
|
217
|
+
if (typeof sync.scope === "string")
|
|
218
|
+
model.scope = sync.scope;
|
|
219
|
+
if (Array.isArray(sync.confirmed))
|
|
220
|
+
model.confirmed = sync.confirmed;
|
|
221
|
+
if (sync.load === "lazy")
|
|
222
|
+
model.load = "lazy";
|
|
223
|
+
if (sync.clientIds === true)
|
|
224
|
+
model.clientIds = true;
|
|
225
|
+
return model;
|
|
226
|
+
}
|
|
227
|
+
function normaliseAccess(access) {
|
|
228
|
+
if (!access || typeof access !== "object")
|
|
229
|
+
return undefined;
|
|
230
|
+
const model = {};
|
|
231
|
+
if (typeof access.ownerField === "string")
|
|
232
|
+
model.ownerField = access.ownerField;
|
|
233
|
+
if (typeof access.groupField === "string")
|
|
234
|
+
model.groupField = access.groupField;
|
|
235
|
+
if (access.scope === "owner" ||
|
|
236
|
+
access.scope === "group" ||
|
|
237
|
+
access.scope === "shared") {
|
|
238
|
+
model.scope = access.scope;
|
|
239
|
+
}
|
|
240
|
+
else if (model.groupField) {
|
|
241
|
+
// Group-owned datasets are physically partitioned per group by default —
|
|
242
|
+
// that is their point. `scope: "shared"` opts out explicitly.
|
|
243
|
+
model.scope = "group";
|
|
244
|
+
}
|
|
245
|
+
if (Array.isArray(access.adminGroups)) {
|
|
246
|
+
model.adminGroups = access.adminGroups.filter((g) => typeof g === "string");
|
|
247
|
+
}
|
|
248
|
+
return Object.keys(model).length ? model : undefined;
|
|
249
|
+
}
|
|
250
|
+
function normaliseAdmin(admin) {
|
|
251
|
+
if (admin === false)
|
|
252
|
+
return false;
|
|
253
|
+
if (!admin || typeof admin !== "object")
|
|
254
|
+
return undefined;
|
|
255
|
+
const model = {};
|
|
256
|
+
if (Array.isArray(admin.listDisplay))
|
|
257
|
+
model.listDisplay = admin.listDisplay;
|
|
258
|
+
return model;
|
|
259
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entity-level authorization markers — auth as *TypeScript types*.
|
|
3
|
+
*
|
|
4
|
+
* Class and interface entities have no options bag, so their access policy is
|
|
5
|
+
* expressed the same way their columns are: in the type system. Attach a
|
|
6
|
+
* marker with `implements` (classes) or `extends` (interfaces) and the
|
|
7
|
+
* compiler reads the policy straight off the heritage clause:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { AdminGroups, IsAuthenticated, Owner, Group } from "rastack/auth";
|
|
11
|
+
*
|
|
12
|
+
* // Per-user rows: RLS on `owner`, staff sees everything.
|
|
13
|
+
* export class Itinerary implements IsAuthenticated, AdminGroups<"staff"> {
|
|
14
|
+
* owner!: string & Owner;
|
|
15
|
+
* notes!: string;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* // Group-owned dataset: rows belong to a team and are *physically stored*
|
|
19
|
+
* // under that team's own storage prefix (groups/{team}/…).
|
|
20
|
+
* export class Report implements IsAuthenticated, AdminGroups<"staff"> {
|
|
21
|
+
* team!: string & Group;
|
|
22
|
+
* title!: string;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* The markers are phantom types: every property is optional and never exists
|
|
27
|
+
* at runtime, so `implements` needs no members and instances stay plain data.
|
|
28
|
+
* Each marker family uses its own phantom key, so markers combine freely —
|
|
29
|
+
* while two *permission* markers on one entity conflict in the type checker,
|
|
30
|
+
* which is exactly right.
|
|
31
|
+
*
|
|
32
|
+
* JSDoc equivalents exist for every marker (`@rastackPermission`,
|
|
33
|
+
* `@rastackAdminGroups staff, ops`, `@rastackScope owner|group|shared`, and
|
|
34
|
+
* field-level `@owner` / `@group`), mirroring the column-constraint tags.
|
|
35
|
+
*/
|
|
36
|
+
/** The permission gate carried in the type system. */
|
|
37
|
+
export interface PermissionOf<P extends string> {
|
|
38
|
+
readonly __rastackPermission?: P;
|
|
39
|
+
}
|
|
40
|
+
/** Every path requires a verified identity (this is also the default). */
|
|
41
|
+
export interface IsAuthenticated extends PermissionOf<"authenticated"> {
|
|
42
|
+
}
|
|
43
|
+
/** Anonymous callers may read (list/retrieve); writes require identity. */
|
|
44
|
+
export interface IsAuthenticatedOrReadOnly extends PermissionOf<"authenticatedOrReadOnly"> {
|
|
45
|
+
}
|
|
46
|
+
/** No gate — every caller may read and write. Opt-in, never the default. */
|
|
47
|
+
export interface IsPublic extends PermissionOf<"public"> {
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Token groups (`cognito:groups`) that bypass row-level security on this
|
|
51
|
+
* entity — the "is admin" role, driven from scopes: `implements
|
|
52
|
+
* AdminGroups<"staff" | "ops">`. Members see and act on every row.
|
|
53
|
+
*/
|
|
54
|
+
export interface AdminGroups<G extends string> {
|
|
55
|
+
readonly __rastackAdminGroups?: G;
|
|
56
|
+
}
|
|
57
|
+
/** Storage scope carried in the type system. */
|
|
58
|
+
export interface ScopeOf<S extends string> {
|
|
59
|
+
readonly __rastackScope?: S;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Physically partition the entity's table per identity
|
|
63
|
+
* (`tenants/{sub}/…`) — the prefix object-storage IAM can fence per user.
|
|
64
|
+
* Combine with an `Owner` field brand.
|
|
65
|
+
*/
|
|
66
|
+
export interface OwnerScoped extends ScopeOf<"owner"> {
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Physically partition the entity's table per group (`groups/{name}/…`).
|
|
70
|
+
* This is the default whenever a `Group` field brand is present — declare it
|
|
71
|
+
* only for emphasis or with a JSDoc-configured group field.
|
|
72
|
+
*/
|
|
73
|
+
export interface GroupScoped extends ScopeOf<"group"> {
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Keep rows in one shared table (row-level filters still apply). Use to opt a
|
|
77
|
+
* `Group`-branded entity out of the default per-group physical partitioning.
|
|
78
|
+
*/
|
|
79
|
+
export interface SharedStorage extends ScopeOf<"shared"> {
|
|
80
|
+
}
|