@stonecrop/graphql-middleware 0.31.0 → 0.32.0
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/index.js +1188 -720
- package/dist/index.js.map +1 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +33 -14
- package/dist/graphql_client.tsbuildinfo +0 -1
- package/dist/src/debug.d.ts +0 -31
- package/dist/src/debug.d.ts.map +0 -1
- package/dist/src/debug.js +0 -49
- package/dist/src/dispatch/transition.d.ts +0 -107
- package/dist/src/dispatch/transition.d.ts.map +0 -1
- package/dist/src/dispatch/transition.js +0 -152
- package/dist/src/fields.d.ts +0 -23
- package/dist/src/fields.d.ts.map +0 -1
- package/dist/src/fields.js +0 -28
- package/dist/src/index.d.ts +0 -16
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -14
- package/dist/src/link-display.d.ts +0 -19
- package/dist/src/link-display.d.ts.map +0 -1
- package/dist/src/link-display.js +0 -93
- package/dist/src/plugin/postgraphile.d.ts +0 -174
- package/dist/src/plugin/postgraphile.d.ts.map +0 -1
- package/dist/src/plugin/postgraphile.js +0 -882
- package/dist/src/preset.d.ts +0 -53
- package/dist/src/preset.d.ts.map +0 -1
- package/dist/src/preset.js +0 -148
- package/dist/src/registry/doctypes.d.ts +0 -95
- package/dist/src/registry/doctypes.d.ts.map +0 -1
- package/dist/src/registry/doctypes.js +0 -251
- package/dist/src/registry/fetchHandlers.d.ts +0 -31
- package/dist/src/registry/fetchHandlers.d.ts.map +0 -1
- package/dist/src/registry/fetchHandlers.js +0 -24
- package/dist/src/tables.d.ts +0 -14
- package/dist/src/tables.d.ts.map +0 -1
- package/dist/src/tables.js +0 -21
- package/dist/src/typeDefs.d.ts +0 -7
- package/dist/src/typeDefs.d.ts.map +0 -1
- package/dist/src/typeDefs.js +0 -118
package/dist/src/preset.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Controls how PostgreSQL column names are mapped to GraphQL field names in the synthesized preset.
|
|
3
|
-
*
|
|
4
|
-
* - `'camel'` (default): `my_column` → `myColumn`. This matches PostGraphile Amber's built-in behaviour.
|
|
5
|
-
* - `'pascal'`: `my_column` → `MyColumn`. Opt in via `createStonecropPreset({ fieldCasing: 'pascal' })` or
|
|
6
|
-
* `grafserv.fieldCasing: 'pascal'` in `nuxt.config.ts`.
|
|
7
|
-
*
|
|
8
|
-
* Smart tag overrides (`@name` on column comments) are respected regardless of this setting.
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
export type FieldCasing = 'camel' | 'pascal';
|
|
12
|
-
/**
|
|
13
|
-
* Creates a Stonecrop-flavoured PostGraphile preset.
|
|
14
|
-
*
|
|
15
|
-
* The returned preset extends `PostGraphileAmberPreset` and applies Stonecrop's
|
|
16
|
-
* recommended defaults. Pass it to `extends` in your PostGraphile configuration:
|
|
17
|
-
*
|
|
18
|
-
* ```typescript
|
|
19
|
-
* import { createStonecropPreset, makePgService, createStonecropPlugin } from '@stonecrop/graphql-middleware'
|
|
20
|
-
*
|
|
21
|
-
* export default {
|
|
22
|
-
* extends: [createStonecropPreset()],
|
|
23
|
-
* pgServices: [makePgService({ connectionString: process.env.DATABASE_URL, schemas: ['public'] })],
|
|
24
|
-
* plugins: [createStonecropPlugin()],
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @param options - Optional configuration. Pass `{ fieldCasing: 'pascal' }` to enable
|
|
29
|
-
* `MyColumn`-style field names instead of the default `myColumn` (camelCase).
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
export declare const createStonecropPreset: (options?: {
|
|
33
|
-
fieldCasing?: FieldCasing;
|
|
34
|
-
}) => GraphileConfig.Preset;
|
|
35
|
-
/**
|
|
36
|
-
* The default Stonecrop PostGraphile preset with camelCase field names.
|
|
37
|
-
*
|
|
38
|
-
* Equivalent to `createStonecropPreset()` with no options. Use this when you
|
|
39
|
-
* do not need to customise field casing:
|
|
40
|
-
*
|
|
41
|
-
* ```typescript
|
|
42
|
-
* import { StonecropPreset, makePgService, createStonecropPlugin } from '@stonecrop/graphql-middleware'
|
|
43
|
-
*
|
|
44
|
-
* export default {
|
|
45
|
-
* extends: [StonecropPreset],
|
|
46
|
-
* pgServices: [makePgService({ connectionString: process.env.DATABASE_URL })],
|
|
47
|
-
* plugins: [createStonecropPlugin()],
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
* @public
|
|
51
|
-
*/
|
|
52
|
-
export declare const StonecropPreset: GraphileConfig.Preset;
|
|
53
|
-
//# sourceMappingURL=preset.d.ts.map
|
package/dist/src/preset.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/preset.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,QAAQ,CAAA;AA4G5C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,qBAAqB,GAAI,UAAU;IAAE,WAAW,CAAC,EAAE,WAAW,CAAA;CAAE,KAAG,cAAc,CAAC,MAM7F,CAAA;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,eAAe,EAAE,cAAc,CAAC,MAAgC,CAAA"}
|
package/dist/src/preset.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import { PostGraphileAmberPreset } from 'postgraphile/presets/amber';
|
|
2
|
-
/**
|
|
3
|
-
* Stops Amber taking `id` for Relay's global object identifier.
|
|
4
|
-
*
|
|
5
|
-
* Amber points `nodeIdFieldName` at `id`, so `PgAttributesPlugin` renames any real column called
|
|
6
|
-
* `id` out of the way to `row_id` -> `rowId`. Stonecrop can use neither name: the middleware
|
|
7
|
-
* resolves columns with raw SQL through `camelToSnake` and never reads the GraphQL surface, so `id`
|
|
8
|
-
* is an opaque base64 node id with no column behind it, and `rowId` names a `row_id` that does not
|
|
9
|
-
* exist. A doctype generated from the un-overridden schema is stamped `primaryKey: true` on that
|
|
10
|
-
* node id and every read of it then fails on a missing column.
|
|
11
|
-
*
|
|
12
|
-
* Both inflectors must move together. Undoing the rename alone collides with the Relay field still
|
|
13
|
-
* sitting on `id`; disabling `NodePlugin` alone removes the Relay field but leaves the column called
|
|
14
|
-
* `rowId`, which is strictly worse — it drops the one case that used to warn. Moving the identifier
|
|
15
|
-
* to `nodeId` keeps Relay working and is the name the schema converter already skips.
|
|
16
|
-
*
|
|
17
|
-
* Mirrors `postgraphile/presets/v4`, which reverses exactly these two inflectors.
|
|
18
|
-
*/
|
|
19
|
-
const StonecropNaturalIdPlugin = {
|
|
20
|
-
name: 'StonecropNaturalIdPlugin',
|
|
21
|
-
version: '0.0.0',
|
|
22
|
-
after: ['PgAttributesPlugin'],
|
|
23
|
-
inflection: {
|
|
24
|
-
replace: {
|
|
25
|
-
// Move Relay's global identifier off `id`...
|
|
26
|
-
nodeIdFieldName() {
|
|
27
|
-
return 'nodeId';
|
|
28
|
-
},
|
|
29
|
-
// ...so the real column can keep its own name.
|
|
30
|
-
_attributeName(previous, options, details) {
|
|
31
|
-
// `previous` is optional in the inflector signature because a replacement may be the first
|
|
32
|
-
// implementation. It never is here — `after: ['PgAttributesPlugin']` guarantees Amber's runs
|
|
33
|
-
// first — but the raw attribute name is the correct identity fallback.
|
|
34
|
-
const name = previous?.(details) ?? details.attributeName;
|
|
35
|
-
if (!details.skipRowId && name === 'row_id') {
|
|
36
|
-
const { codec, attributeName } = details;
|
|
37
|
-
const baseName = codec.attributes[attributeName]?.extensions?.tags?.name ?? attributeName;
|
|
38
|
-
if (typeof baseName === 'string' && baseName.toLowerCase() === 'id' && !codec.isAnonymous) {
|
|
39
|
-
return 'id';
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return name;
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* Exposes the many side of a foreign key the database declares as owning.
|
|
49
|
-
*
|
|
50
|
-
* PostGraphile registers both directions of every foreign key, but Amber gives the many side no
|
|
51
|
-
* `manyRelation:resource:connection` behavior — for a non-unique relation `PgRelationsPlugin`'s
|
|
52
|
-
* inferred behavior is `["resource:select", behavior]` and nothing more. So a parent type carries
|
|
53
|
-
* no field for its children, and a doctype generated from that schema can never contain a child
|
|
54
|
-
* table. Every one a consumer has is hand-authored.
|
|
55
|
-
*
|
|
56
|
-
* `ON DELETE CASCADE` is the discriminator because it is the only place a schema states ownership.
|
|
57
|
-
* `sc_tag.item_id` cascades: the tags are the item's rows and die with it. `sc_order.customer_id`
|
|
58
|
-
* does not: an order references a party that outlives it. The two are otherwise the same shape, so
|
|
59
|
-
* no amount of reading the relation itself can separate them — and turning the many side on for
|
|
60
|
-
* *every* foreign key would make each referenced table look like somebody's child.
|
|
61
|
-
*
|
|
62
|
-
* Read at the gather phase rather than from the built relation: `confdeltype` is on the raw
|
|
63
|
-
* `pg_constraint` and does not survive into `relation.extensions`, which carries only `isIndexed`.
|
|
64
|
-
*
|
|
65
|
-
* Connection only, deliberately. The converter recognises a Connection type through
|
|
66
|
-
* `getConnectionNodeType`; a list field is not a shape it knows, so `+manyRelation:resource:list`
|
|
67
|
-
* would add a second field per relation that generation then drops on the floor.
|
|
68
|
-
*
|
|
69
|
-
* A global `preset.schema.defaultBehavior` cannot do this. Inferred behaviors are applied after the
|
|
70
|
-
* global default and take precedence, so `+manyRelation:resource:connection` there is overridden
|
|
71
|
-
* for every relation — verified, it changes nothing, while `-connection +list` through the same
|
|
72
|
-
* option visibly rewrites the Query fields.
|
|
73
|
-
*/
|
|
74
|
-
const StonecropOwnedRelationsPlugin = {
|
|
75
|
-
name: 'StonecropOwnedRelationsPlugin',
|
|
76
|
-
version: '0.0.0',
|
|
77
|
-
gather: {
|
|
78
|
-
hooks: {
|
|
79
|
-
pgRelations_relation(_info, event) {
|
|
80
|
-
const { relation, pgConstraint } = event;
|
|
81
|
-
// The hook fires once per direction of each constraint; only the backwards one has a
|
|
82
|
-
// many side to expose. `confdeltype` is Postgres' own code: 'c' is CASCADE.
|
|
83
|
-
if (!relation.isReferencee)
|
|
84
|
-
return;
|
|
85
|
-
if (pgConstraint.confdeltype !== 'c')
|
|
86
|
-
return;
|
|
87
|
-
relation.extensions ??= {};
|
|
88
|
-
relation.extensions.tags ??= {};
|
|
89
|
-
relation.extensions.tags.behavior = '+manyRelation:resource:connection';
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
const StonecropFieldCasingPlugin = {
|
|
95
|
-
name: 'StonecropFieldCasingPlugin',
|
|
96
|
-
version: '0.0.0',
|
|
97
|
-
inflection: {
|
|
98
|
-
replace: {
|
|
99
|
-
attribute(previous, options, details) {
|
|
100
|
-
return this.upperCamelCase(this._attributeName(details));
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* Creates a Stonecrop-flavoured PostGraphile preset.
|
|
107
|
-
*
|
|
108
|
-
* The returned preset extends `PostGraphileAmberPreset` and applies Stonecrop's
|
|
109
|
-
* recommended defaults. Pass it to `extends` in your PostGraphile configuration:
|
|
110
|
-
*
|
|
111
|
-
* ```typescript
|
|
112
|
-
* import { createStonecropPreset, makePgService, createStonecropPlugin } from '@stonecrop/graphql-middleware'
|
|
113
|
-
*
|
|
114
|
-
* export default {
|
|
115
|
-
* extends: [createStonecropPreset()],
|
|
116
|
-
* pgServices: [makePgService({ connectionString: process.env.DATABASE_URL, schemas: ['public'] })],
|
|
117
|
-
* plugins: [createStonecropPlugin()],
|
|
118
|
-
* }
|
|
119
|
-
* ```
|
|
120
|
-
*
|
|
121
|
-
* @param options - Optional configuration. Pass `{ fieldCasing: 'pascal' }` to enable
|
|
122
|
-
* `MyColumn`-style field names instead of the default `myColumn` (camelCase).
|
|
123
|
-
* @public
|
|
124
|
-
*/
|
|
125
|
-
export const createStonecropPreset = (options) => ({
|
|
126
|
-
extends: [PostGraphileAmberPreset],
|
|
127
|
-
plugins: options?.fieldCasing === 'pascal'
|
|
128
|
-
? [StonecropNaturalIdPlugin, StonecropOwnedRelationsPlugin, StonecropFieldCasingPlugin]
|
|
129
|
-
: [StonecropNaturalIdPlugin, StonecropOwnedRelationsPlugin],
|
|
130
|
-
});
|
|
131
|
-
/**
|
|
132
|
-
* The default Stonecrop PostGraphile preset with camelCase field names.
|
|
133
|
-
*
|
|
134
|
-
* Equivalent to `createStonecropPreset()` with no options. Use this when you
|
|
135
|
-
* do not need to customise field casing:
|
|
136
|
-
*
|
|
137
|
-
* ```typescript
|
|
138
|
-
* import { StonecropPreset, makePgService, createStonecropPlugin } from '@stonecrop/graphql-middleware'
|
|
139
|
-
*
|
|
140
|
-
* export default {
|
|
141
|
-
* extends: [StonecropPreset],
|
|
142
|
-
* pgServices: [makePgService({ connectionString: process.env.DATABASE_URL })],
|
|
143
|
-
* plugins: [createStonecropPlugin()],
|
|
144
|
-
* }
|
|
145
|
-
* ```
|
|
146
|
-
* @public
|
|
147
|
-
*/
|
|
148
|
-
export const StonecropPreset = createStonecropPreset();
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import type { DoctypeMeta, ValidationError } from '@stonecrop/schema';
|
|
2
|
-
/**
|
|
3
|
-
* Error thrown when a doctype definition fails validation
|
|
4
|
-
* @public
|
|
5
|
-
*/
|
|
6
|
-
export declare class DoctypeValidationError extends Error {
|
|
7
|
-
/** File path or name where the validation error occurred */
|
|
8
|
-
readonly file: string;
|
|
9
|
-
/** List of validation errors found */
|
|
10
|
-
readonly errors: ValidationError[];
|
|
11
|
-
constructor(
|
|
12
|
-
/** File path or name where the validation error occurred */
|
|
13
|
-
file: string,
|
|
14
|
-
/** List of validation errors found */
|
|
15
|
-
errors: ValidationError[]);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Options for loading doctype definitions
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
export interface LoadDoctypesOptions {
|
|
22
|
-
/** Continue loading other files if one fails validation */
|
|
23
|
-
continueOnError?: boolean;
|
|
24
|
-
/** Callback for validation errors when continueOnError is true */
|
|
25
|
-
onError?: (file: string, errors: ValidationError[]) => void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Load doctype definitions from a directory of JSON files
|
|
29
|
-
* @param dir - Directory path containing doctype JSON files
|
|
30
|
-
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
31
|
-
* @public
|
|
32
|
-
*/
|
|
33
|
-
export declare function loadDoctypes(dir: string, options?: LoadDoctypesOptions): void;
|
|
34
|
-
/**
|
|
35
|
-
* Load doctypes from an object (for programmatic use)
|
|
36
|
-
* @param doctypes - Object mapping doctype names to doctype definitions
|
|
37
|
-
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
export declare function loadDoctypesFromObject(doctypes: Record<string, unknown>, options?: LoadDoctypesOptions): void;
|
|
41
|
-
/**
|
|
42
|
-
* Get a doctype by name
|
|
43
|
-
* @param name - Name of the doctype to retrieve
|
|
44
|
-
* @public
|
|
45
|
-
*/
|
|
46
|
-
export declare function getMeta(name: string): DoctypeMeta | undefined;
|
|
47
|
-
/**
|
|
48
|
-
* Get all loaded doctypes
|
|
49
|
-
* @public
|
|
50
|
-
*/
|
|
51
|
-
export declare function getAllMeta(): DoctypeMeta[];
|
|
52
|
-
/**
|
|
53
|
-
* Check if a doctype is registered
|
|
54
|
-
* @param name - Name of the doctype to check
|
|
55
|
-
* @public
|
|
56
|
-
*/
|
|
57
|
-
export declare function hasMeta(name: string): boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Clear all registered doctypes
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
export declare function clearRegistry(): void;
|
|
63
|
-
/**
|
|
64
|
-
* Validate doctype references — link targets, `inherits`, and each link's binding — against what
|
|
65
|
-
* is loaded.
|
|
66
|
-
*
|
|
67
|
-
* Resolution goes through `getMeta`, never through the registry Map directly. The Map is keyed by
|
|
68
|
-
* doctype **name** while a link target is a **slug** (`DoctypeMeta.links[].target` is documented as
|
|
69
|
-
* one, and every doctype in this repo writes one), so a raw `.has()` reports a broken reference for
|
|
70
|
-
* every correct link — measured at 12 of 12 across the two in-repo hosts before this was fixed.
|
|
71
|
-
* `getMeta` is also what the plugin resolves a link with at fetch time, so this asks the same
|
|
72
|
-
* question the runtime asks.
|
|
73
|
-
*
|
|
74
|
-
* `inherits` is checked the same way even though nothing resolves it at runtime (both resolvers
|
|
75
|
-
* only echo it onto the wire) — one rule is cheaper than two, and it cannot be wrong in the
|
|
76
|
-
* permissive direction.
|
|
77
|
-
*
|
|
78
|
-
* The **binding** checks are the same defect class as an unresolvable target, one step further in:
|
|
79
|
-
* a link whose target resolves can still name no way to reach the rows, and the reader's answer for
|
|
80
|
-
* that is `continue` or a `null` — indistinguishable on the wire from a relation that is genuinely
|
|
81
|
-
* empty. Both halves of a declaration are `.optional()` in the schema because which one is required
|
|
82
|
-
* depends on cardinality, which Zod cannot express here; this is where that dependency is enforced.
|
|
83
|
-
*
|
|
84
|
-
* Only what the runtime actually consults is required. A `backlink` naming a column the target
|
|
85
|
-
* table does not have already fails loudly — Postgres rejects the SELECT — so it is left alone;
|
|
86
|
-
* the check exists for the silent cases, not to restate the database's own errors.
|
|
87
|
-
*
|
|
88
|
-
* Returns rather than throws so the caller decides; `assertReferencesResolve` in the plugin is that
|
|
89
|
-
* caller. Kept out of the package's public exports: it reads a module-level registry, so it is only
|
|
90
|
-
* meaningful after this module's own loaders have run.
|
|
91
|
-
*
|
|
92
|
-
* @internal
|
|
93
|
-
*/
|
|
94
|
-
export declare function validateReferences(): ValidationError[];
|
|
95
|
-
//# sourceMappingURL=doctypes.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"doctypes.d.ts","sourceRoot":"","sources":["../../../src/registry/doctypes.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAMrE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;IAE/C,4DAA4D;aAC5C,IAAI,EAAE,MAAM;IAC5B,sCAAsC;aACtB,MAAM,EAAE,eAAe,EAAE;;IAHzC,4DAA4D;IAC5C,IAAI,EAAE,MAAM;IAC5B,sCAAsC;IACtB,MAAM,EAAE,eAAe,EAAE;CAM1C;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IACnC,2DAA2D;IAC3D,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,kEAAkE;IAClE,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,IAAI,CAAA;CAC3D;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,IAAI,CAajF;AAsCD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAE,mBAAwB,GAAG,IAAI,CAgBjH;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAQ7D;AAED;;;GAGG;AACH,wBAAgB,UAAU,IAAI,WAAW,EAAE,CAE1C;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE7C;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,EAAE,CA8FtD"}
|
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { parseDoctype, validateDoctype } from '@stonecrop/schema';
|
|
4
|
-
import { columnBackedFields, flattenFields } from '../fields';
|
|
5
|
-
const doctypeRegistry = new Map();
|
|
6
|
-
/**
|
|
7
|
-
* Error thrown when a doctype definition fails validation
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export class DoctypeValidationError extends Error {
|
|
11
|
-
file;
|
|
12
|
-
errors;
|
|
13
|
-
constructor(
|
|
14
|
-
/** File path or name where the validation error occurred */
|
|
15
|
-
file,
|
|
16
|
-
/** List of validation errors found */
|
|
17
|
-
errors) {
|
|
18
|
-
const errorMessages = errors.map(e => ` ${e.path.join('.')}: ${e.message}`).join('\n');
|
|
19
|
-
super(`Invalid doctype definition in ${file}:\n${errorMessages}`);
|
|
20
|
-
this.file = file;
|
|
21
|
-
this.errors = errors;
|
|
22
|
-
this.name = 'DoctypeValidationError';
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Load doctype definitions from a directory of JSON files
|
|
27
|
-
* @param dir - Directory path containing doctype JSON files
|
|
28
|
-
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
export function loadDoctypes(dir, options = {}) {
|
|
32
|
-
const entries = readdirSync(dir);
|
|
33
|
-
for (const entry of entries) {
|
|
34
|
-
const fullPath = join(dir, entry);
|
|
35
|
-
const stat = statSync(fullPath);
|
|
36
|
-
if (stat.isDirectory()) {
|
|
37
|
-
loadDoctypes(fullPath, options);
|
|
38
|
-
}
|
|
39
|
-
else if (entry.endsWith('.json')) {
|
|
40
|
-
loadDoctypeFile(fullPath, options);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Load and validate a single doctype file
|
|
46
|
-
*/
|
|
47
|
-
function loadDoctypeFile(filePath, options) {
|
|
48
|
-
const content = readFileSync(filePath, 'utf-8');
|
|
49
|
-
let data;
|
|
50
|
-
try {
|
|
51
|
-
data = JSON.parse(content);
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
const parseError = {
|
|
55
|
-
path: [],
|
|
56
|
-
message: `Invalid JSON: ${err instanceof Error ? err.message : String(err)}`,
|
|
57
|
-
};
|
|
58
|
-
if (options.continueOnError) {
|
|
59
|
-
options.onError?.(filePath, [parseError]);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
throw new DoctypeValidationError(filePath, [parseError]);
|
|
63
|
-
}
|
|
64
|
-
const result = validateDoctype(data);
|
|
65
|
-
if (!result.success) {
|
|
66
|
-
if (options.continueOnError) {
|
|
67
|
-
options.onError?.(filePath, result.errors);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
throw new DoctypeValidationError(filePath, result.errors);
|
|
71
|
-
}
|
|
72
|
-
const doctype = parseDoctype(data);
|
|
73
|
-
doctypeRegistry.set(doctype.name, doctype);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Load doctypes from an object (for programmatic use)
|
|
77
|
-
* @param doctypes - Object mapping doctype names to doctype definitions
|
|
78
|
-
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
79
|
-
* @public
|
|
80
|
-
*/
|
|
81
|
-
export function loadDoctypesFromObject(doctypes, options = {}) {
|
|
82
|
-
for (const [name, data] of Object.entries(doctypes)) {
|
|
83
|
-
const withName = { ...(typeof data === 'object' && data !== null ? data : {}), name };
|
|
84
|
-
const result = validateDoctype(withName);
|
|
85
|
-
if (!result.success) {
|
|
86
|
-
if (options.continueOnError) {
|
|
87
|
-
options.onError?.(name, result.errors);
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
throw new DoctypeValidationError(name, result.errors);
|
|
91
|
-
}
|
|
92
|
-
const doctype = parseDoctype(withName);
|
|
93
|
-
doctypeRegistry.set(doctype.name, doctype);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Get a doctype by name
|
|
98
|
-
* @param name - Name of the doctype to retrieve
|
|
99
|
-
* @public
|
|
100
|
-
*/
|
|
101
|
-
export function getMeta(name) {
|
|
102
|
-
const direct = doctypeRegistry.get(name);
|
|
103
|
-
if (direct)
|
|
104
|
-
return direct;
|
|
105
|
-
// Fallback: find by slug (links reference doctypes by slug, not name)
|
|
106
|
-
for (const doctype of doctypeRegistry.values()) {
|
|
107
|
-
if (doctype.slug === name)
|
|
108
|
-
return doctype;
|
|
109
|
-
}
|
|
110
|
-
return undefined;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Get all loaded doctypes
|
|
114
|
-
* @public
|
|
115
|
-
*/
|
|
116
|
-
export function getAllMeta() {
|
|
117
|
-
return Array.from(doctypeRegistry.values());
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Check if a doctype is registered
|
|
121
|
-
* @param name - Name of the doctype to check
|
|
122
|
-
* @public
|
|
123
|
-
*/
|
|
124
|
-
export function hasMeta(name) {
|
|
125
|
-
return doctypeRegistry.has(name);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Clear all registered doctypes
|
|
129
|
-
* @public
|
|
130
|
-
*/
|
|
131
|
-
export function clearRegistry() {
|
|
132
|
-
doctypeRegistry.clear();
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Validate doctype references — link targets, `inherits`, and each link's binding — against what
|
|
136
|
-
* is loaded.
|
|
137
|
-
*
|
|
138
|
-
* Resolution goes through `getMeta`, never through the registry Map directly. The Map is keyed by
|
|
139
|
-
* doctype **name** while a link target is a **slug** (`DoctypeMeta.links[].target` is documented as
|
|
140
|
-
* one, and every doctype in this repo writes one), so a raw `.has()` reports a broken reference for
|
|
141
|
-
* every correct link — measured at 12 of 12 across the two in-repo hosts before this was fixed.
|
|
142
|
-
* `getMeta` is also what the plugin resolves a link with at fetch time, so this asks the same
|
|
143
|
-
* question the runtime asks.
|
|
144
|
-
*
|
|
145
|
-
* `inherits` is checked the same way even though nothing resolves it at runtime (both resolvers
|
|
146
|
-
* only echo it onto the wire) — one rule is cheaper than two, and it cannot be wrong in the
|
|
147
|
-
* permissive direction.
|
|
148
|
-
*
|
|
149
|
-
* The **binding** checks are the same defect class as an unresolvable target, one step further in:
|
|
150
|
-
* a link whose target resolves can still name no way to reach the rows, and the reader's answer for
|
|
151
|
-
* that is `continue` or a `null` — indistinguishable on the wire from a relation that is genuinely
|
|
152
|
-
* empty. Both halves of a declaration are `.optional()` in the schema because which one is required
|
|
153
|
-
* depends on cardinality, which Zod cannot express here; this is where that dependency is enforced.
|
|
154
|
-
*
|
|
155
|
-
* Only what the runtime actually consults is required. A `backlink` naming a column the target
|
|
156
|
-
* table does not have already fails loudly — Postgres rejects the SELECT — so it is left alone;
|
|
157
|
-
* the check exists for the silent cases, not to restate the database's own errors.
|
|
158
|
-
*
|
|
159
|
-
* Returns rather than throws so the caller decides; `assertReferencesResolve` in the plugin is that
|
|
160
|
-
* caller. Kept out of the package's public exports: it reads a module-level registry, so it is only
|
|
161
|
-
* meaningful after this module's own loaders have run.
|
|
162
|
-
*
|
|
163
|
-
* @internal
|
|
164
|
-
*/
|
|
165
|
-
export function validateReferences() {
|
|
166
|
-
const errors = [];
|
|
167
|
-
for (const doctype of doctypeRegistry.values()) {
|
|
168
|
-
// Check inherits reference
|
|
169
|
-
if (doctype.inherits && getMeta(doctype.inherits) === undefined) {
|
|
170
|
-
errors.push({
|
|
171
|
-
path: [doctype.name, 'inherits'],
|
|
172
|
-
message: `References unknown doctype: ${doctype.inherits}`,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
const fields = flattenFields(doctype.fields);
|
|
176
|
-
// Check link field targets — `doctype` is both the link marker and its target.
|
|
177
|
-
for (const field of fields) {
|
|
178
|
-
if (field.kind !== 'field')
|
|
179
|
-
continue;
|
|
180
|
-
const target = field.doctype;
|
|
181
|
-
if (target !== undefined && getMeta(target) === undefined) {
|
|
182
|
-
errors.push({
|
|
183
|
-
path: [doctype.name, 'fields', field.fieldname, 'doctype'],
|
|
184
|
-
message: `Link references unknown doctype: ${target}`,
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// Check link-declaration targets (component-primary links live in the `links` map)
|
|
189
|
-
if (doctype.links) {
|
|
190
|
-
const declared = new Set(fields.map(f => f.fieldname));
|
|
191
|
-
// A foreign key is read from a column, so a link can only bind to a field that has one.
|
|
192
|
-
// The rule itself lives in one place — see `columnBackedFields` — because the read path,
|
|
193
|
-
// the write path and this check all have to answer it the same way.
|
|
194
|
-
const columnBacked = new Set(columnBackedFields(doctype.fields).map(f => f.fieldname));
|
|
195
|
-
for (const [key, link] of Object.entries(doctype.links)) {
|
|
196
|
-
if (getMeta(link.target) === undefined) {
|
|
197
|
-
errors.push({
|
|
198
|
-
path: [doctype.name, 'links', key, 'target'],
|
|
199
|
-
message: `Link references unknown doctype: ${link.target}`,
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
// A custom fetch strategy hands the whole read to its handler, which is given the
|
|
203
|
-
// row and the declaration and finds the target however it likes. The runtime returns
|
|
204
|
-
// before consulting either binding below, so requiring one would refuse a link that
|
|
205
|
-
// works.
|
|
206
|
-
if (link.fetch?.method === 'custom')
|
|
207
|
-
continue;
|
|
208
|
-
// The two cardinalities bind through different halves of the declaration, so the
|
|
209
|
-
// check has to split the same way the reader does.
|
|
210
|
-
if (link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne') {
|
|
211
|
-
// Many-side: the target rows are found by a column on the *target* table, so
|
|
212
|
-
// this doctype needs no field of its own — and the in-repo hosts disagree on
|
|
213
|
-
// whether one exists, which is why only `backlink` can be required here.
|
|
214
|
-
if (!link.backlink) {
|
|
215
|
-
errors.push({
|
|
216
|
-
path: [doctype.name, 'links', key, 'backlink'],
|
|
217
|
-
message: `Link "${key}" is ${link.cardinality} but names no \`backlink\`, so the runtime cannot ` +
|
|
218
|
-
`find the target rows and drops the link with no error. Name the field on "${link.target}" ` +
|
|
219
|
-
`that points back to this doctype.`,
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
// One-side: the foreign key is a column on *this* table, read as
|
|
225
|
-
// `link.fieldname ?? key`. A name matching no declared field is not selected, so the
|
|
226
|
-
// link resolves to null forever and reads as "this record has no such relation".
|
|
227
|
-
const fieldname = link.fieldname ?? key;
|
|
228
|
-
if (!columnBacked.has(fieldname)) {
|
|
229
|
-
errors.push({
|
|
230
|
-
// Point at whichever half actually carries the binding: the `fieldname`
|
|
231
|
-
// property when it is set, otherwise the key itself. Naming `target` here
|
|
232
|
-
// would send the reader to the one part that resolved correctly.
|
|
233
|
-
path: link.fieldname ? [doctype.name, 'links', key, 'fieldname'] : [doctype.name, 'links', key],
|
|
234
|
-
// Two different repairs, so two different diagnoses: a name nothing declares
|
|
235
|
-
// is a typo, while a name that resolves to a column-less field is a binding
|
|
236
|
-
// pointed at the wrong one of several real fields.
|
|
237
|
-
message: declared.has(fieldname)
|
|
238
|
-
? `Link "${key}" binds to field "${fieldname}", which has no database column of its own — ` +
|
|
239
|
-
`it is computed, or a container rather than a value — so there is no foreign key to read ` +
|
|
240
|
-
`and the record read fails on the missing column. Bind the link to the field holding the ` +
|
|
241
|
-
`foreign key.`
|
|
242
|
-
: `Link "${key}" binds to field "${fieldname}", which this doctype does not declare, so it ` +
|
|
243
|
-
`resolves to null with no error. Declare that field, or set \`fieldname\` to the field ` +
|
|
244
|
-
`holding the foreign key. (A link's key is its fieldname unless \`fieldname\` overrides it.)`,
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return errors;
|
|
251
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { PgClient } from '@dataplan/pg';
|
|
2
|
-
import type { LinkDeclaration } from '@stonecrop/schema';
|
|
3
|
-
/**
|
|
4
|
-
* Handler for a custom fetch strategy on a link declaration.
|
|
5
|
-
* Called during `stonecropRecord` resolution when `link.fetch.method === 'custom'`.
|
|
6
|
-
*
|
|
7
|
-
* @param pgClient - Active database client for the current request
|
|
8
|
-
* @param parentRecord - Resolved parent record (scalar fields already populated)
|
|
9
|
-
* @param link - The link declaration, giving access to cardinality and other metadata
|
|
10
|
-
* @returns A single linked record or an array depending on `link.cardinality`
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
export type FetchHandler = (pgClient: PgClient, parentRecord: Record<string, unknown>, link: LinkDeclaration) => Promise<Record<string, unknown> | Record<string, unknown>[]>;
|
|
14
|
-
/**
|
|
15
|
-
* Register a custom fetch handler by name.
|
|
16
|
-
* The name must match the `handler` field on a `CustomFetch` strategy declaration.
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
export declare function registerFetchHandler(name: string, handler: FetchHandler): void;
|
|
20
|
-
/**
|
|
21
|
-
* Retrieve a registered fetch handler by name.
|
|
22
|
-
* Returns `undefined` if no handler has been registered under that name.
|
|
23
|
-
* @public
|
|
24
|
-
*/
|
|
25
|
-
export declare function getFetchHandler(name: string): FetchHandler | undefined;
|
|
26
|
-
/**
|
|
27
|
-
* Remove all registered fetch handlers. Primarily for test isolation.
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export declare function clearFetchHandlers(): void;
|
|
31
|
-
//# sourceMappingURL=fetchHandlers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetchHandlers.d.ts","sourceRoot":"","sources":["../../../src/registry/fetchHandlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,CAC1B,QAAQ,EAAE,QAAQ,EAClB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrC,IAAI,EAAE,eAAe,KACjB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAAA;AAIjE;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,IAAI,CAE9E;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAEtE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const registry = new Map();
|
|
2
|
-
/**
|
|
3
|
-
* Register a custom fetch handler by name.
|
|
4
|
-
* The name must match the `handler` field on a `CustomFetch` strategy declaration.
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export function registerFetchHandler(name, handler) {
|
|
8
|
-
registry.set(name, handler);
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Retrieve a registered fetch handler by name.
|
|
12
|
-
* Returns `undefined` if no handler has been registered under that name.
|
|
13
|
-
* @public
|
|
14
|
-
*/
|
|
15
|
-
export function getFetchHandler(name) {
|
|
16
|
-
return registry.get(name);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Remove all registered fetch handlers. Primarily for test isolation.
|
|
20
|
-
* @public
|
|
21
|
-
*/
|
|
22
|
-
export function clearFetchHandlers() {
|
|
23
|
-
registry.clear();
|
|
24
|
-
}
|
package/dist/src/tables.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The quoted SQL table a doctype reads from: the `tables` override when the adapter supplies one,
|
|
3
|
-
* otherwise the doctype name snake-cased.
|
|
4
|
-
*
|
|
5
|
-
* The one definition of that mapping. It lives in its own module rather than beside either caller
|
|
6
|
-
* because the plugin imports the link-display enrichment, so the enrichment cannot import back —
|
|
7
|
-
* and the copy it made instead was byte-identical, which is the state a mapping is in right before
|
|
8
|
-
* one side learns about schema-qualified names and the other does not.
|
|
9
|
-
*
|
|
10
|
-
* A dotted override is read as `schema.table` and quoted in two parts, so a table in a non-default
|
|
11
|
-
* Postgres schema survives quoting instead of becoming one identifier containing a dot.
|
|
12
|
-
*/
|
|
13
|
-
export declare function resolveTableName(name: string, tables?: Record<string, string>): string;
|
|
14
|
-
//# sourceMappingURL=tables.d.ts.map
|
package/dist/src/tables.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../../src/tables.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAOtF"}
|
package/dist/src/tables.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { pascalToSnake } from '@stonecrop/schema';
|
|
2
|
-
/**
|
|
3
|
-
* The quoted SQL table a doctype reads from: the `tables` override when the adapter supplies one,
|
|
4
|
-
* otherwise the doctype name snake-cased.
|
|
5
|
-
*
|
|
6
|
-
* The one definition of that mapping. It lives in its own module rather than beside either caller
|
|
7
|
-
* because the plugin imports the link-display enrichment, so the enrichment cannot import back —
|
|
8
|
-
* and the copy it made instead was byte-identical, which is the state a mapping is in right before
|
|
9
|
-
* one side learns about schema-qualified names and the other does not.
|
|
10
|
-
*
|
|
11
|
-
* A dotted override is read as `schema.table` and quoted in two parts, so a table in a non-default
|
|
12
|
-
* Postgres schema survives quoting instead of becoming one identifier containing a dot.
|
|
13
|
-
*/
|
|
14
|
-
export function resolveTableName(name, tables) {
|
|
15
|
-
const target = tables?.[name] ?? pascalToSnake(name);
|
|
16
|
-
const dotIndex = target.indexOf('.');
|
|
17
|
-
if (dotIndex > 0) {
|
|
18
|
-
return `"${target.slice(0, dotIndex)}"."${target.slice(dotIndex + 1)}"`;
|
|
19
|
-
}
|
|
20
|
-
return `"${target}"`;
|
|
21
|
-
}
|
package/dist/src/typeDefs.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GraphQL type definitions for Stonecrop's middleware API.
|
|
3
|
-
* Includes stonecropMeta, stonecropRecord, stonecropRecords, stonecropAction, and related types.
|
|
4
|
-
* @public
|
|
5
|
-
*/
|
|
6
|
-
export declare const typeDefs: import("graphql").DocumentNode;
|
|
7
|
-
//# sourceMappingURL=typeDefs.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"typeDefs.d.ts","sourceRoot":"","sources":["../../src/typeDefs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,gCA+GpB,CAAA"}
|