dsqlbase 0.2.1 → 0.3.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/CHANGELOG.md +32 -0
- package/README.md +41 -60
- package/dist/client/clients/base.d.ts +20 -0
- package/dist/client/clients/base.d.ts.map +1 -0
- package/dist/client/clients/base.js +52 -0
- package/dist/client/clients/base.js.map +1 -0
- package/dist/client/clients/dsql.d.ts +24 -0
- package/dist/client/clients/dsql.d.ts.map +1 -0
- package/dist/client/clients/dsql.js +31 -0
- package/dist/client/clients/dsql.js.map +1 -0
- package/dist/client/clients/index.d.ts +6 -0
- package/dist/client/clients/index.d.ts.map +1 -0
- package/dist/client/clients/index.js +6 -0
- package/dist/client/clients/index.js.map +1 -0
- package/dist/client/clients/model.d.ts +169 -0
- package/dist/client/clients/model.d.ts.map +1 -0
- package/dist/client/{model/client.js → clients/model.js} +82 -53
- package/dist/client/clients/model.js.map +1 -0
- package/dist/client/clients/transaction.d.ts +9 -0
- package/dist/client/clients/transaction.d.ts.map +1 -0
- package/dist/client/{transaction/transaction-client.js → clients/transaction.js} +7 -7
- package/dist/client/clients/transaction.js.map +1 -0
- package/dist/client/clients/union.d.ts +39 -0
- package/dist/client/clients/union.d.ts.map +1 -0
- package/dist/client/clients/union.js +54 -0
- package/dist/client/clients/union.js.map +1 -0
- package/dist/client/create.d.ts +15 -18
- package/dist/client/create.d.ts.map +1 -1
- package/dist/client/create.js +10 -27
- package/dist/client/create.js.map +1 -1
- package/dist/client/index.d.ts +4 -8
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -6
- package/dist/client/index.js.map +1 -1
- package/dist/client/normalizers/base.d.ts +467 -0
- package/dist/client/normalizers/base.d.ts.map +1 -0
- package/dist/client/normalizers/base.js.map +1 -0
- package/dist/client/normalizers/context.d.ts +14 -0
- package/dist/client/normalizers/context.d.ts.map +1 -0
- package/dist/client/normalizers/context.js +16 -0
- package/dist/client/normalizers/context.js.map +1 -0
- package/dist/client/normalizers/index.d.ts +8 -0
- package/dist/client/normalizers/index.d.ts.map +1 -0
- package/dist/client/normalizers/index.js +8 -0
- package/dist/client/normalizers/index.js.map +1 -0
- package/dist/client/normalizers/pagination.d.ts +87 -0
- package/dist/client/normalizers/pagination.d.ts.map +1 -0
- package/dist/client/normalizers/pagination.js +245 -0
- package/dist/client/normalizers/pagination.js.map +1 -0
- package/dist/client/normalizers/request.d.ts +41 -0
- package/dist/client/normalizers/request.d.ts.map +1 -0
- package/dist/client/normalizers/request.js +346 -0
- package/dist/client/normalizers/request.js.map +1 -0
- package/dist/client/normalizers/rules.d.ts +132 -0
- package/dist/client/normalizers/rules.d.ts.map +1 -0
- package/dist/client/normalizers/rules.js +82 -0
- package/dist/client/normalizers/rules.js.map +1 -0
- package/dist/client/normalizers/utils.d.ts +6 -0
- package/dist/client/normalizers/utils.d.ts.map +1 -0
- package/dist/client/normalizers/utils.js +18 -0
- package/dist/client/normalizers/utils.js.map +1 -0
- package/dist/client/normalizers/where.d.ts +39 -0
- package/dist/client/normalizers/where.d.ts.map +1 -0
- package/dist/client/normalizers/where.js +230 -0
- package/dist/client/normalizers/where.js.map +1 -0
- package/dist/client/utils/occ-retry.d.ts.map +1 -0
- package/dist/client/utils/occ-retry.js.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/schema/columns/guid.d.ts +21 -21
- package/dist/schema/columns/guid.d.ts.map +1 -1
- package/dist/schema/columns/guid.js +24 -25
- package/dist/schema/columns/guid.js.map +1 -1
- package/dist/schema/index.d.ts +0 -2
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +0 -2
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/relations.d.ts +22 -13
- package/dist/schema/relations.d.ts.map +1 -1
- package/dist/schema/relations.js +9 -1
- package/dist/schema/relations.js.map +1 -1
- package/dist/schema/table.d.ts +2 -2
- package/dist/schema/table.d.ts.map +1 -1
- package/dist/schema/table.js.map +1 -1
- package/dist/schema/union.d.ts +13 -8
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +14 -9
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/utils/global-id.d.ts +3 -3
- package/dist/schema/utils/global-id.js +3 -3
- package/dist/schema/utils/global-id.js.map +1 -1
- package/dist/schema/utils/guid-hooks.d.ts +49 -0
- package/dist/schema/utils/guid-hooks.d.ts.map +1 -0
- package/dist/schema/utils/guid-hooks.js +155 -0
- package/dist/schema/utils/guid-hooks.js.map +1 -0
- package/package.json +8 -6
- package/dist/client/database/base.d.ts +0 -99
- package/dist/client/database/base.d.ts.map +0 -1
- package/dist/client/database/base.js +0 -216
- package/dist/client/database/base.js.map +0 -1
- package/dist/client/database/client.d.ts +0 -26
- package/dist/client/database/client.d.ts.map +0 -1
- package/dist/client/database/client.js +0 -47
- package/dist/client/database/client.js.map +0 -1
- package/dist/client/database/index.d.ts +0 -65
- package/dist/client/database/index.d.ts.map +0 -1
- package/dist/client/database/index.js +0 -3
- package/dist/client/database/index.js.map +0 -1
- package/dist/client/model/base.d.ts +0 -747
- package/dist/client/model/base.d.ts.map +0 -1
- package/dist/client/model/base.js.map +0 -1
- package/dist/client/model/client.d.ts +0 -139
- package/dist/client/model/client.d.ts.map +0 -1
- package/dist/client/model/client.js.map +0 -1
- package/dist/client/model/filters.d.ts +0 -276
- package/dist/client/model/filters.d.ts.map +0 -1
- package/dist/client/model/filters.js +0 -470
- package/dist/client/model/filters.js.map +0 -1
- package/dist/client/model/normalizer.d.ts +0 -143
- package/dist/client/model/normalizer.d.ts.map +0 -1
- package/dist/client/model/normalizer.js +0 -641
- package/dist/client/model/normalizer.js.map +0 -1
- package/dist/client/nodes.d.ts +0 -62
- package/dist/client/nodes.d.ts.map +0 -1
- package/dist/client/nodes.js +0 -378
- package/dist/client/nodes.js.map +0 -1
- package/dist/client/pagination/cursor.d.ts +0 -40
- package/dist/client/pagination/cursor.d.ts.map +0 -1
- package/dist/client/pagination/cursor.js +0 -79
- package/dist/client/pagination/cursor.js.map +0 -1
- package/dist/client/pagination/page.d.ts +0 -23
- package/dist/client/pagination/page.d.ts.map +0 -1
- package/dist/client/pagination/page.js +0 -37
- package/dist/client/pagination/page.js.map +0 -1
- package/dist/client/transaction/index.d.ts +0 -2
- package/dist/client/transaction/index.d.ts.map +0 -1
- package/dist/client/transaction/index.js +0 -2
- package/dist/client/transaction/index.js.map +0 -1
- package/dist/client/transaction/occ-retry.d.ts.map +0 -1
- package/dist/client/transaction/occ-retry.js.map +0 -1
- package/dist/client/transaction/transaction-client.d.ts +0 -9
- package/dist/client/transaction/transaction-client.d.ts.map +0 -1
- package/dist/client/transaction/transaction-client.js.map +0 -1
- package/dist/client/union/client.d.ts +0 -55
- package/dist/client/union/client.d.ts.map +0 -1
- package/dist/client/union/client.js +0 -74
- package/dist/client/union/client.js.map +0 -1
- package/dist/schema/embedded.d.ts +0 -31
- package/dist/schema/embedded.d.ts.map +0 -1
- package/dist/schema/embedded.js +0 -33
- package/dist/schema/embedded.js.map +0 -1
- package/dist/schema/tenant.d.ts +0 -31
- package/dist/schema/tenant.d.ts.map +0 -1
- package/dist/schema/tenant.js +0 -33
- package/dist/schema/tenant.js.map +0 -1
- /package/dist/client/{model → normalizers}/base.js +0 -0
- /package/dist/client/{transaction → utils}/occ-retry.d.ts +0 -0
- /package/dist/client/{transaction → utils}/occ-retry.js +0 -0
package/dist/schema/index.js
CHANGED
|
@@ -20,11 +20,9 @@ export { timestamp, datetime, TimestampColumnDefinition, } from "./columns/times
|
|
|
20
20
|
export { uuid, UUIDColumnDefinition } from "./columns/uuid.js";
|
|
21
21
|
export { varchar } from "./columns/varchar.js";
|
|
22
22
|
export { domain, $enum } from "./domain.js";
|
|
23
|
-
export { embedded } from "./embedded.js";
|
|
24
23
|
export { namespace, schema } from "./namespace.js";
|
|
25
24
|
export { sequence } from "./sequence.js";
|
|
26
25
|
export { table } from "./table.js";
|
|
27
|
-
export { tenantScope } from "./tenant.js";
|
|
28
26
|
export { union } from "./union.js";
|
|
29
27
|
export { relations, belongsTo, hasMany, hasOne } from "./relations.js";
|
|
30
28
|
//# sourceMappingURL=index.js.map
|
package/dist/schema/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,IAAI,EAA8C,MAAM,mBAAmB,CAAC;AACrF,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EACL,QAAQ,EACR,wBAAwB,GAGzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,IAAI,EAAE,oBAAoB,EAAa,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,QAAQ,EACR,QAAQ,GAGT,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,IAAI,EACJ,KAAK,EACL,oBAAoB,GAIrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AASxD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;A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C7C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AASxD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,IAAI,EAA0B,MAAM,mBAAmB,CAAC;AACjE,OAAO,EACL,SAAS,EACT,QAAQ,EACR,yBAAyB,GAG1B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { array } from \"./columns/array.js\";\nexport { bigint, int8 } from \"./columns/bigint.js\";\nexport { boolean, bool } from \"./columns/boolean.js\";\nexport { bytea } from \"./columns/bytea.js\";\nexport { char } from \"./columns/char.js\";\nexport { date, type DateValueType, type DateColumnOptions } from \"./columns/date.js\";\nexport { double, float8 } from \"./columns/double.js\";\nexport {\n identity,\n IdentityColumnDefinition,\n type IdentityColumnOptions,\n type IdentityConfig,\n} from \"./columns/identity.js\";\nexport { guid, GuidColumnDefinition, type Guid } from \"./columns/guid.js\";\nexport { int, int4 } from \"./columns/int.js\";\nexport {\n interval,\n duration,\n type IntervalColumnOptions,\n type IntervalValueType,\n} from \"./columns/interval.js\";\nexport {\n json,\n jsonb,\n JsonColumnDefinition,\n type JsonSchemaFor,\n type JsonShape,\n type WithSchema,\n} from \"./columns/json.js\";\nexport { record } from \"./columns/record.js\";\nexport { decimal, numeric } from \"./columns/numeric.js\";\nexport type {\n DecimalConstructor,\n DecimalLike,\n NumericInputType,\n NumericMode,\n NumericOptions,\n NumericValueType,\n} from \"./columns/numeric.js\";\nexport { real, float4 } from \"./columns/real.js\";\nexport { smallint, int2 } from \"./columns/smallint.js\";\nexport { text } from \"./columns/text.js\";\nexport { time, type TimeColumnOptions } from \"./columns/time.js\";\nexport {\n timestamp,\n datetime,\n TimestampColumnDefinition,\n type DateTimeColumnOptions,\n type TimestampColumnConfig,\n} from \"./columns/timestamp.js\";\nexport { uuid, UUIDColumnDefinition } from \"./columns/uuid.js\";\nexport { varchar } from \"./columns/varchar.js\";\n\nexport { domain, $enum } from \"./domain.js\";\nexport { namespace, schema } from \"./namespace.js\";\nexport { sequence } from \"./sequence.js\";\nexport { table } from \"./table.js\";\nexport { union } from \"./union.js\";\n\nexport { relations, belongsTo, hasMany, hasOne } from \"./relations.js\";\n\nexport { type Duration } from \"./utils/duration.js\";\nexport { type GlobalId, type GlobalIdErrorCode } from \"./utils/global-id.js\";\nexport type { StandardSchemaV1 } from \"./utils/standard-schema.js\";\nexport { type DateTimeMode } from \"./utils/date.js\";\n"]}
|
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { AnyUnionDefinition, FieldRelation, RelationsDefinition, RelationType } from "@dsqlbase/core/definition";
|
|
2
|
+
import { AnyRelationObject } from "@dsqlbase/core/definition/relations";
|
|
3
|
+
/**
|
|
4
|
+
* Declares the relations of a table, or of a union. A union's relations are every member's,
|
|
5
|
+
* each from its own column: `relations(posts, { comments: hasMany(comments, { from:
|
|
6
|
+
* [posts.columns.id], to: [comments.columns.postId] }) })` joins comments from a photo, from a
|
|
7
|
+
* video, and from the union. Its target must be a table.
|
|
8
|
+
*/
|
|
9
|
+
export declare function relations<TSource extends AnyRelationObject, TRelations extends Record<string, FieldRelation<RelationType, TSource, AnyRelationObject>>>(table: TSource, relations: TRelations): RelationsDefinition<TSource, TRelations>;
|
|
3
10
|
/**
|
|
4
11
|
* `to` names the target's columns. For a union target it is either shared fields from
|
|
5
12
|
* `union.columns`, or one column list per member when members store the key differently.
|
|
6
13
|
*/
|
|
7
|
-
export declare function hasMany<TSource extends
|
|
8
|
-
from: FieldRelation<TSource, TTarget
|
|
9
|
-
to: FieldRelation<TSource, TTarget
|
|
10
|
-
}): FieldRelation<TSource, TTarget
|
|
14
|
+
export declare function hasMany<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(target: TTarget, config: {
|
|
15
|
+
from: FieldRelation<"has_many", TSource, TTarget>["from"];
|
|
16
|
+
to: FieldRelation<"has_many", TSource, TTarget>["to"];
|
|
17
|
+
}): FieldRelation<"has_many", TSource, TTarget>;
|
|
11
18
|
/**
|
|
12
|
-
* The belongs-to side of a relation. A belongs-to a union also
|
|
19
|
+
* The belongs-to side of a relation. A belongs-to a union may also take `discriminator`: the
|
|
13
20
|
* source column holding which member each row points at, as that member's schema alias.
|
|
21
|
+
* Without one, the member is the first, in declaration order, holding the key — computed when
|
|
22
|
+
* the query runs, one key lookup per member per row.
|
|
14
23
|
*/
|
|
15
|
-
export declare function belongsTo<TSource extends
|
|
16
|
-
from: FieldRelation<TSource, TTarget
|
|
17
|
-
to: FieldRelation<TSource, TTarget
|
|
24
|
+
export declare function belongsTo<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(target: TTarget, config: {
|
|
25
|
+
from: FieldRelation<"belongs_to", TSource, TTarget>["from"];
|
|
26
|
+
to: FieldRelation<"belongs_to", TSource, TTarget>["to"];
|
|
18
27
|
} & (TTarget extends AnyUnionDefinition ? {
|
|
19
|
-
discriminator
|
|
28
|
+
discriminator?: NonNullable<FieldRelation<"belongs_to", TSource, TTarget>["discriminator"]>;
|
|
20
29
|
} : {
|
|
21
30
|
discriminator?: never;
|
|
22
|
-
})): FieldRelation<TSource, TTarget
|
|
23
|
-
export declare function hasOne<TSource extends
|
|
31
|
+
})): FieldRelation<"belongs_to", TSource, TTarget>;
|
|
32
|
+
export declare function hasOne<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(target: TTarget, config: Pick<FieldRelation<"has_one", TSource, TTarget>, "from" | "to">): FieldRelation<"has_one", TSource, TTarget>;
|
|
24
33
|
//# sourceMappingURL=relations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relations.d.ts","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"relations.d.ts","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,aAAa,EAEb,mBAAmB,EACnB,YAAY,EACb,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,SAAS,CACvB,OAAO,SAAS,iBAAiB,EACjC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,YAAY,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC,EAC1F,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,4CAEtC;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,OAAO,SAAS,iBAAiB,EAAE,OAAO,SAAS,iBAAiB,EAC1F,MAAM,EAAE,OAAO,EACf,MAAM,EAAE;IACN,IAAI,EAAE,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;IAC1D,EAAE,EAAE,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;CACvD,GACA,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAO7C;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,SAAS,iBAAiB,EAAE,OAAO,SAAS,iBAAiB,EAC5F,MAAM,EAAE,OAAO,EACf,MAAM,EAAE;IACN,IAAI,EAAE,aAAa,CAAC,YAAY,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;IAC5D,EAAE,EAAE,aAAa,CAAC,YAAY,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;CACzD,GAAG,CAAC,OAAO,SAAS,kBAAkB,GACnC;IACE,aAAa,CAAC,EAAE,WAAW,CAAC,aAAa,CAAC,YAAY,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC;CAC7F,GACD;IAAE,aAAa,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC,GAC7B,aAAa,CAAC,YAAY,EAAE,OAAO,EAAE,OAAO,CAAC,CAQ/C;AAED,wBAAgB,MAAM,CAAC,OAAO,SAAS,iBAAiB,EAAE,OAAO,SAAS,iBAAiB,EACzF,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC,GACtE,aAAa,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAO5C"}
|
package/dist/schema/relations.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { Relation, RelationsDefinition, } from "@dsqlbase/core/definition";
|
|
2
|
+
/**
|
|
3
|
+
* Declares the relations of a table, or of a union. A union's relations are every member's,
|
|
4
|
+
* each from its own column: `relations(posts, { comments: hasMany(comments, { from:
|
|
5
|
+
* [posts.columns.id], to: [comments.columns.postId] }) })` joins comments from a photo, from a
|
|
6
|
+
* video, and from the union. Its target must be a table.
|
|
7
|
+
*/
|
|
2
8
|
export function relations(table, relations) {
|
|
3
9
|
return new RelationsDefinition(table, relations);
|
|
4
10
|
}
|
|
@@ -15,8 +21,10 @@ export function hasMany(target, config) {
|
|
|
15
21
|
};
|
|
16
22
|
}
|
|
17
23
|
/**
|
|
18
|
-
* The belongs-to side of a relation. A belongs-to a union also
|
|
24
|
+
* The belongs-to side of a relation. A belongs-to a union may also take `discriminator`: the
|
|
19
25
|
* source column holding which member each row points at, as that member's schema alias.
|
|
26
|
+
* Without one, the member is the first, in declaration order, holding the key — computed when
|
|
27
|
+
* the query runs, one key lookup per member per row.
|
|
20
28
|
*/
|
|
21
29
|
export function belongsTo(target, config) {
|
|
22
30
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relations.js","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"relations.js","sourceRoot":"","sources":["../../src/schema/relations.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,QAAQ,EACR,mBAAmB,GAEpB,MAAM,2BAA2B,CAAC;AAGnC;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAGvB,KAAc,EAAE,SAAqB;IACrC,OAAO,IAAI,mBAAmB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AACnD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,OAAO,CACrB,MAAe,EACf,MAGC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ,CAAC,QAAQ;QACvB,MAAM;QACN,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,EAAE;KAC0C,CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CACvB,MAAe,EACf,MAO8B;IAE9B,OAAO;QACL,IAAI,EAAE,QAAQ,CAAC,UAAU;QACzB,MAAM;QACN,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,GAAG,CAAC,MAAM,CAAC,aAAa,IAAI,EAAE,aAAa,EAAE,MAAM,CAAC,aAAa,EAAE,CAAC;KACX,CAAC;AAC9D,CAAC;AAED,MAAM,UAAU,MAAM,CACpB,MAAe,EACf,MAAuE;IAEvE,OAAO;QACL,IAAI,EAAE,QAAQ,CAAC,OAAO;QACtB,MAAM;QACN,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,EAAE;KACyC,CAAC;AAC3D,CAAC","sourcesContent":["import {\n AnyUnionDefinition,\n FieldRelation,\n Relation,\n RelationsDefinition,\n RelationType,\n} from \"@dsqlbase/core/definition\";\nimport { AnyRelationObject } from \"@dsqlbase/core/definition/relations\";\n\n/**\n * Declares the relations of a table, or of a union. A union's relations are every member's,\n * each from its own column: `relations(posts, { comments: hasMany(comments, { from:\n * [posts.columns.id], to: [comments.columns.postId] }) })` joins comments from a photo, from a\n * video, and from the union. Its target must be a table.\n */\nexport function relations<\n TSource extends AnyRelationObject,\n TRelations extends Record<string, FieldRelation<RelationType, TSource, AnyRelationObject>>,\n>(table: TSource, relations: TRelations) {\n return new RelationsDefinition(table, relations);\n}\n\n/**\n * `to` names the target's columns. For a union target it is either shared fields from\n * `union.columns`, or one column list per member when members store the key differently.\n */\nexport function hasMany<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(\n target: TTarget,\n config: {\n from: FieldRelation<\"has_many\", TSource, TTarget>[\"from\"];\n to: FieldRelation<\"has_many\", TSource, TTarget>[\"to\"];\n }\n): FieldRelation<\"has_many\", TSource, TTarget> {\n return {\n type: Relation.HAS_MANY,\n target,\n from: config.from,\n to: config.to,\n } as const as FieldRelation<\"has_many\", TSource, TTarget>;\n}\n\n/**\n * The belongs-to side of a relation. A belongs-to a union may also take `discriminator`: the\n * source column holding which member each row points at, as that member's schema alias.\n * Without one, the member is the first, in declaration order, holding the key — computed when\n * the query runs, one key lookup per member per row.\n */\nexport function belongsTo<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(\n target: TTarget,\n config: {\n from: FieldRelation<\"belongs_to\", TSource, TTarget>[\"from\"];\n to: FieldRelation<\"belongs_to\", TSource, TTarget>[\"to\"];\n } & (TTarget extends AnyUnionDefinition\n ? {\n discriminator?: NonNullable<FieldRelation<\"belongs_to\", TSource, TTarget>[\"discriminator\"]>;\n }\n : { discriminator?: never })\n): FieldRelation<\"belongs_to\", TSource, TTarget> {\n return {\n type: Relation.BELONGS_TO,\n target,\n from: config.from,\n to: config.to,\n ...(config.discriminator && { discriminator: config.discriminator }),\n } as const as FieldRelation<\"belongs_to\", TSource, TTarget>;\n}\n\nexport function hasOne<TSource extends AnyRelationObject, TTarget extends AnyRelationObject>(\n target: TTarget,\n config: Pick<FieldRelation<\"has_one\", TSource, TTarget>, \"from\" | \"to\">\n): FieldRelation<\"has_one\", TSource, TTarget> {\n return {\n type: Relation.HAS_ONE,\n target,\n from: config.from,\n to: config.to,\n } as const as FieldRelation<\"has_one\", TSource, TTarget>;\n}\n"]}
|
package/dist/schema/table.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TableDefinition,
|
|
1
|
+
import { TableDefinition, AnyColumnDefinition, AnyNamespaceDefinition } from "@dsqlbase/core/definition";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a table schema with specified columns and optional schema association.
|
|
4
4
|
*
|
|
@@ -16,5 +16,5 @@ import { TableDefinition, AnyNamespaceDefinition, TableColumnDefinitions } from
|
|
|
16
16
|
* });
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
|
-
export declare function table<TName extends string, TColumns extends
|
|
19
|
+
export declare function table<TName extends string, TColumns extends Record<string, AnyColumnDefinition>>(name: TName, columns: TColumns): TableDefinition<TName, TColumns, AnyNamespaceDefinition>;
|
|
20
20
|
//# sourceMappingURL=table.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,sBAAsB,EACvB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AAEH,wBAAgB,KAAK,CAAC,KAAK,SAAS,MAAM,EAAE,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,EAC9F,IAAI,EAAE,KAAK,EACX,OAAO,EAAE,QAAQ,GAChB,eAAe,CAAC,KAAK,EAAE,QAAQ,EAAE,sBAAsB,CAAC,CAE1D"}
|
package/dist/schema/table.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,GAGhB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,UAAU,KAAK,CACnB,IAAW,EACX,OAAiB;IAEjB,OAAO,IAAI,eAAe,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import {\n TableDefinition,\n
|
|
1
|
+
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/schema/table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,GAGhB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,UAAU,KAAK,CACnB,IAAW,EACX,OAAiB;IAEjB,OAAO,IAAI,eAAe,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import {\n TableDefinition,\n AnyColumnDefinition,\n AnyNamespaceDefinition,\n} from \"@dsqlbase/core/definition\";\n\n/**\n * Defines a table schema with specified columns and optional schema association.\n *\n * @param name Table name in the database\n * @param columns An object defining the columns of the table, where keys are field names used at runtime and values are ColumnDefinition instances that specify the column's properties and constraints.\n * @returns A new instance of TableDefinition representing the defined table schema.\n *\n * @example\n *\n * ```ts\n * const users = table(\"users\", {\n * id: uuid(\"id\").primaryKey(),\n * name: text(\"name\").notNull(),\n * email: text(\"email\").unique(),\n * });\n * ```\n */\n\nexport function table<TName extends string, TColumns extends Record<string, AnyColumnDefinition>>(\n name: TName,\n columns: TColumns\n): TableDefinition<TName, TColumns, AnyNamespaceDefinition> {\n return new TableDefinition(name, { columns });\n}\n"]}
|
package/dist/schema/union.d.ts
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
|
-
import { AnyUnionMembers, UnionDefinition } from "@dsqlbase/core/definition";
|
|
1
|
+
import { AnyUnionMembers, UnionColumnsConfig, UnionConfig, UnionDefinition } from "@dsqlbase/core/definition";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a set of tables that can stand in for one another — a GraphQL union or interface.
|
|
4
4
|
*
|
|
5
5
|
* Key every member by the alias it is exported under in the schema; the client refuses any
|
|
6
|
-
* other key when it is built.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* other key when it is built. The second argument declares the fields that work across the
|
|
7
|
+
* union — a relation's `to`, and shared `select` / `where` / `orderBy` — as `union.columns`.
|
|
8
|
+
* Each is the column every member stores under the same field: the same database name and
|
|
9
|
+
* type, and `NOT NULL` on every member when it is declared so. A union produces no DDL.
|
|
9
10
|
*
|
|
10
11
|
* @param members The member tables, keyed by schema alias.
|
|
12
|
+
* @param columns The shared fields, keyed by field name, each declared as its column.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
14
|
-
* export const photos = table("photos", { id: guid("id").primaryKey(), userId:
|
|
15
|
-
* export const videos = table("videos", { id: guid("id").primaryKey(), userId:
|
|
16
|
+
* export const photos = table("photos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
17
|
+
* export const videos = table("videos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
16
18
|
*
|
|
17
|
-
* export const posts = union(
|
|
19
|
+
* export const posts = union("posts", {
|
|
20
|
+
* members: { photos, videos },
|
|
21
|
+
* columns: { id: guid("id"), userId: guid("user_id") }
|
|
22
|
+
* });
|
|
18
23
|
*
|
|
19
24
|
* export const userRelations = relations(users, {
|
|
20
25
|
* feed: hasMany(posts, { from: [users.columns.id], to: [posts.columns.userId] }),
|
|
21
26
|
* });
|
|
22
27
|
* ```
|
|
23
28
|
*/
|
|
24
|
-
export declare function union<TMembers extends AnyUnionMembers
|
|
29
|
+
export declare function union<TName extends string, TMembers extends AnyUnionMembers, TColumns extends UnionColumnsConfig<TMembers>>(name: TName, config: UnionConfig<TMembers, TColumns>): UnionDefinition<TName, TMembers, TColumns>;
|
|
25
30
|
//# sourceMappingURL=union.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,WAAW,EACX,eAAe,EAChB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,KAAK,CACnB,KAAK,SAAS,MAAM,EACpB,QAAQ,SAAS,eAAe,EAChC,QAAQ,SAAS,kBAAkB,CAAC,QAAQ,CAAC,EAE7C,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,GACtC,eAAe,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAE5C"}
|
package/dist/schema/union.js
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
import { UnionDefinition } from "@dsqlbase/core/definition";
|
|
1
|
+
import { UnionDefinition, } from "@dsqlbase/core/definition";
|
|
2
2
|
/**
|
|
3
3
|
* Defines a set of tables that can stand in for one another — a GraphQL union or interface.
|
|
4
4
|
*
|
|
5
5
|
* Key every member by the alias it is exported under in the schema; the client refuses any
|
|
6
|
-
* other key when it is built.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* other key when it is built. The second argument declares the fields that work across the
|
|
7
|
+
* union — a relation's `to`, and shared `select` / `where` / `orderBy` — as `union.columns`.
|
|
8
|
+
* Each is the column every member stores under the same field: the same database name and
|
|
9
|
+
* type, and `NOT NULL` on every member when it is declared so. A union produces no DDL.
|
|
9
10
|
*
|
|
10
11
|
* @param members The member tables, keyed by schema alias.
|
|
12
|
+
* @param columns The shared fields, keyed by field name, each declared as its column.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
14
|
-
* export const photos = table("photos", { id: guid("id").primaryKey(), userId:
|
|
15
|
-
* export const videos = table("videos", { id: guid("id").primaryKey(), userId:
|
|
16
|
+
* export const photos = table("photos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
17
|
+
* export const videos = table("videos", { id: guid("id").primaryKey(), userId: guid("user_id") });
|
|
16
18
|
*
|
|
17
|
-
* export const posts = union(
|
|
19
|
+
* export const posts = union("posts", {
|
|
20
|
+
* members: { photos, videos },
|
|
21
|
+
* columns: { id: guid("id"), userId: guid("user_id") }
|
|
22
|
+
* });
|
|
18
23
|
*
|
|
19
24
|
* export const userRelations = relations(users, {
|
|
20
25
|
* feed: hasMany(posts, { from: [users.columns.id], to: [posts.columns.userId] }),
|
|
21
26
|
* });
|
|
22
27
|
* ```
|
|
23
28
|
*/
|
|
24
|
-
export function union(
|
|
25
|
-
return new UnionDefinition(
|
|
29
|
+
export function union(name, config) {
|
|
30
|
+
return new UnionDefinition(name, config);
|
|
26
31
|
}
|
|
27
32
|
//# sourceMappingURL=union.js.map
|
package/dist/schema/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,eAAe,GAChB,MAAM,2BAA2B,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,KAAK,CAKnB,IAAW,EACX,MAAuC;IAEvC,OAAO,IAAI,eAAe,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAC3C,CAAC","sourcesContent":["import {\n AnyUnionMembers,\n UnionColumnsConfig,\n UnionConfig,\n UnionDefinition,\n} from \"@dsqlbase/core/definition\";\n\n/**\n * Defines a set of tables that can stand in for one another — a GraphQL union or interface.\n *\n * Key every member by the alias it is exported under in the schema; the client refuses any\n * other key when it is built. The second argument declares the fields that work across the\n * union — a relation's `to`, and shared `select` / `where` / `orderBy` — as `union.columns`.\n * Each is the column every member stores under the same field: the same database name and\n * type, and `NOT NULL` on every member when it is declared so. A union produces no DDL.\n *\n * @param members The member tables, keyed by schema alias.\n * @param columns The shared fields, keyed by field name, each declared as its column.\n *\n * @example\n * ```ts\n * export const photos = table(\"photos\", { id: guid(\"id\").primaryKey(), userId: guid(\"user_id\") });\n * export const videos = table(\"videos\", { id: guid(\"id\").primaryKey(), userId: guid(\"user_id\") });\n *\n * export const posts = union(\"posts\", {\n * members: { photos, videos },\n * columns: { id: guid(\"id\"), userId: guid(\"user_id\") }\n * });\n *\n * export const userRelations = relations(users, {\n * feed: hasMany(posts, { from: [users.columns.id], to: [posts.columns.userId] }),\n * });\n * ```\n */\nexport function union<\n TName extends string,\n TMembers extends AnyUnionMembers,\n TColumns extends UnionColumnsConfig<TMembers>,\n>(\n name: TName,\n config: UnionConfig<TMembers, TColumns>\n): UnionDefinition<TName, TMembers, TColumns> {\n return new UnionDefinition(name, config);\n}\n"]}
|
|
@@ -42,9 +42,9 @@ export type GlobalId = {
|
|
|
42
42
|
* Wraps a primary key as an opaque global id.
|
|
43
43
|
*
|
|
44
44
|
* The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather
|
|
45
|
-
* than a bare value so the key field is named rather than assumed to be `id`, and so
|
|
46
|
-
*
|
|
47
|
-
*
|
|
45
|
+
* than a bare value so the key field is named rather than assumed to be `id`, and so a
|
|
46
|
+
* composite key is one object carrying every field of it. Rows only emit single-field ids, from
|
|
47
|
+
* their `guid()` columns; a composite one is built here by the caller.
|
|
48
48
|
*
|
|
49
49
|
* Both the table key and the pk field names are **aliases**: the names the client addresses
|
|
50
50
|
* (`members`, not `team_members`), so an id follows the client-visible identity rather than
|
|
@@ -31,9 +31,9 @@ export const GLOBAL_ID_PREFIX = "guid:";
|
|
|
31
31
|
* Wraps a primary key as an opaque global id.
|
|
32
32
|
*
|
|
33
33
|
* The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather
|
|
34
|
-
* than a bare value so the key field is named rather than assumed to be `id`, and so
|
|
35
|
-
*
|
|
36
|
-
*
|
|
34
|
+
* than a bare value so the key field is named rather than assumed to be `id`, and so a
|
|
35
|
+
* composite key is one object carrying every field of it. Rows only emit single-field ids, from
|
|
36
|
+
* their `guid()` columns; a composite one is built here by the caller.
|
|
37
37
|
*
|
|
38
38
|
* Both the table key and the pk field names are **aliases**: the names the client addresses
|
|
39
39
|
* (`members`, not `team_members`), so an id follows the client-visible identity rather than
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-id.js","sourceRoot":"","sources":["../../../src/schema/utils/global-id.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH;;;;;GAKG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,IAAI,CAAoB;IAEjC,YAAY,IAAuB,EAAE,OAAe;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;A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CAAC,CAAC;AAC9E,CAAC","sourcesContent":["/**\n * Global ids: the opaque form a row's primary key takes when it leaves the ORM.\n *\n * Deliberately **not** in `@dsqlbase/core`. Nothing below the client has an opinion about the\n * shape of an id — core sees a `uuid` column with a codec, exactly as it sees an `interval`\n * column with a `Duration` codec (`./duration.ts`). The wrapping is a client concern, so it\n * lives here with the rest of the codec helpers.\n */\n\n/** What went wrong with a global id. */\nexport type GlobalIdErrorCode =\n /** The value is not a well-formed `guid:` string. */\n | \"format\"\n /** The value decoded, but names a table or key fields the caller did not expect. */\n | \"key_mismatch\"\n /** The value decoded, but its table key is not a node in this schema. */\n | \"unknown_node\";\n\n/**\n * A global id could not be read, or does not point where it was used.\n *\n * `key_mismatch` is the one that earns the format its keep: a wrapped `workspaces` id handed\n * to a `users.id` filter is caught here, where a bare uuid would simply have matched nothing.\n */\nexport class GlobalIdError extends Error {\n readonly code: GlobalIdErrorCode;\n\n constructor(code: GlobalIdErrorCode, message: string) {\n super(message);\n\n this.name = \"GlobalIdError\";\n this.code = code;\n }\n}\n\n/**\n * The prefix every global id carries.\n *\n * It is what makes \"is this already wrapped?\" an exact question rather than a heuristic: a\n * uuid never starts with `guid:`, so a column can accept both forms without guessing.\n */\nexport const GLOBAL_ID_PREFIX = \"guid:\";\n\n/** A decoded global id: the table it names, and the primary key it carries. */\nexport type GlobalId = {\n /** The node key — the schema alias of the table, unless the column overrode it. */\n readonly key: string;\n\n /** The primary key, by **field** name (`teamId`), not by database column name (`team_id`). */\n readonly pk: Record<string, string>;\n};\n\n/**\n * Wraps a primary key as an opaque global id.\n *\n * The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather\n * than a bare value so the key field is named rather than assumed to be `id`, and so
|
|
1
|
+
{"version":3,"file":"global-id.js","sourceRoot":"","sources":["../../../src/schema/utils/global-id.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH;;;;;GAKG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,IA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,KAAK,sCAAsC,CAAC,CAAC;IACrF,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["/**\n * Global ids: the opaque form a row's primary key takes when it leaves the ORM.\n *\n * Deliberately **not** in `@dsqlbase/core`. Nothing below the client has an opinion about the\n * shape of an id — core sees a `uuid` column with a codec, exactly as it sees an `interval`\n * column with a `Duration` codec (`./duration.ts`). The wrapping is a client concern, so it\n * lives here with the rest of the codec helpers.\n */\n\n/** What went wrong with a global id. */\nexport type GlobalIdErrorCode =\n /** The value is not a well-formed `guid:` string. */\n | \"format\"\n /** The value decoded, but names a table or key fields the caller did not expect. */\n | \"key_mismatch\"\n /** The value decoded, but its table key is not a node in this schema. */\n | \"unknown_node\";\n\n/**\n * A global id could not be read, or does not point where it was used.\n *\n * `key_mismatch` is the one that earns the format its keep: a wrapped `workspaces` id handed\n * to a `users.id` filter is caught here, where a bare uuid would simply have matched nothing.\n */\nexport class GlobalIdError extends Error {\n readonly code: GlobalIdErrorCode;\n\n constructor(code: GlobalIdErrorCode, message: string) {\n super(message);\n\n this.name = \"GlobalIdError\";\n this.code = code;\n }\n}\n\n/**\n * The prefix every global id carries.\n *\n * It is what makes \"is this already wrapped?\" an exact question rather than a heuristic: a\n * uuid never starts with `guid:`, so a column can accept both forms without guessing.\n */\nexport const GLOBAL_ID_PREFIX = \"guid:\";\n\n/** A decoded global id: the table it names, and the primary key it carries. */\nexport type GlobalId = {\n /** The node key — the schema alias of the table, unless the column overrode it. */\n readonly key: string;\n\n /** The primary key, by **field** name (`teamId`), not by database column name (`team_id`). */\n readonly pk: Record<string, string>;\n};\n\n/**\n * Wraps a primary key as an opaque global id.\n *\n * The payload is `[key, pk]`, JSON-encoded and base64url-wrapped. `pk` is an object rather\n * than a bare value so the key field is named rather than assumed to be `id`, and so a\n * composite key is one object carrying every field of it. Rows only emit single-field ids, from\n * their `guid()` columns; a composite one is built here by the caller.\n *\n * Both the table key and the pk field names are **aliases**: the names the client addresses\n * (`members`, not `team_members`), so an id follows the client-visible identity rather than\n * the physical one and a database rename does not invalidate ids in the wild.\n */\nexport function encodeGlobalId(key: string, pk: Record<string, string>): string {\n const payload = JSON.stringify([key, pk]);\n\n return `${GLOBAL_ID_PREFIX}${Buffer.from(payload, \"utf8\").toString(\"base64url\")}`;\n}\n\n/** Whether a value is a global id rather than a raw column value. */\nexport function isGlobalId(value: unknown): value is string {\n return typeof value === \"string\" && value.startsWith(GLOBAL_ID_PREFIX);\n}\n\n/**\n * Reads a global id back into its table key and primary key.\n *\n * `expectedKey` is the wrong-table safety net: pass it wherever the caller already knows which\n * table the id has to name — a `guid` column's own key, say — and a wrapped id from another\n * table throws instead of quietly matching nothing.\n *\n * The payload is never trusted. Whether the decoded fields are actually that table's primary\n * key is a separate question, answered against the registry by the caller.\n */\nexport function decodeGlobalId(value: string, expectedKey?: string): GlobalId {\n if (!isGlobalId(value)) {\n throw new GlobalIdError(\n \"format\",\n `\"${value}\" is not a global id: it has no \"${GLOBAL_ID_PREFIX}\" prefix.`\n );\n }\n\n const payload = decodePayload(value);\n\n if (!Array.isArray(payload) || payload.length !== 2) {\n throw new GlobalIdError(\"format\", `\"${value}\" does not carry a [key, pk] payload.`);\n }\n\n const [key, pk] = payload as [unknown, unknown];\n\n if (typeof key !== \"string\" || key.length === 0) {\n throw new GlobalIdError(\"format\", `\"${value}\" does not name a table.`);\n }\n\n if (!isPlainRecord(pk) || Object.keys(pk).length === 0) {\n throw new GlobalIdError(\"format\", `\"${value}\" does not carry a primary key.`);\n }\n\n for (const [field, fieldValue] of Object.entries(pk)) {\n if (typeof fieldValue !== \"string\") {\n throw new GlobalIdError(\n \"format\",\n `\"${value}\" carries a non-string value for key field \"${field}\".`\n );\n }\n }\n\n if (expectedKey !== undefined && key !== expectedKey) {\n throw new GlobalIdError(\n \"key_mismatch\",\n `Global id names \"${key}\", but \"${expectedKey}\" was expected here.`\n );\n }\n\n return { key, pk: pk as Record<string, string> };\n}\n\nfunction decodePayload(value: string): unknown {\n // `Buffer.from(..., \"base64url\")` never throws — it drops whatever it cannot read — so\n // malformed base64 surfaces here as JSON that does not parse rather than as a decode error.\n const json = Buffer.from(value.slice(GLOBAL_ID_PREFIX.length), \"base64url\").toString(\"utf8\");\n\n try {\n return JSON.parse(json);\n } catch {\n throw new GlobalIdError(\"format\", `\"${value}\" does not decode to a JSON payload.`);\n }\n}\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AnyColumn, ColumnHooks, ComputedDiscriminator } from "@dsqlbase/core";
|
|
2
|
+
/** What a `guid()` column's values name: one node, the same for every row. */
|
|
3
|
+
export type GuidBinding = {
|
|
4
|
+
/** The node this column's values name. */
|
|
5
|
+
readonly key: string;
|
|
6
|
+
/** The field the target node's primary key is addressed by (`id`, `membershipId`, …). */
|
|
7
|
+
readonly keyField: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* What a `guid()` column whose node is named per row names: a key pointing at a union's member
|
|
11
|
+
* — the `from` of a belongs-to a union, or the foreign key of a relation whose source is one.
|
|
12
|
+
* The discriminator names each row's member alias, which is the node key.
|
|
13
|
+
*/
|
|
14
|
+
export type DynamicGuidBinding = {
|
|
15
|
+
/**
|
|
16
|
+
* What names each row's member: a stored column, or a discriminator core computes when the
|
|
17
|
+
* query runs, when the relation declares none. Either is read from the row by its `name`.
|
|
18
|
+
*/
|
|
19
|
+
readonly discriminator: AnyColumn | ComputedDiscriminator;
|
|
20
|
+
/**
|
|
21
|
+
* The stored discriminator's field name, as `create` / `update` data names it; `undefined`
|
|
22
|
+
* for a computed one, which there is nothing to write to.
|
|
23
|
+
*/
|
|
24
|
+
readonly discriminatorField: string | undefined;
|
|
25
|
+
/** Member alias → the field its key is addressed by in a payload (`id`, …). */
|
|
26
|
+
readonly members: Readonly<Record<string, string>>;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The hooks every `guid()` column carries: a read wraps the raw uuid as a global id, a write or
|
|
30
|
+
* a filter unwraps one. What an id names comes from the column's references, read when the hook
|
|
31
|
+
* first runs — they are linked and frozen by then — and kept per built column.
|
|
32
|
+
*
|
|
33
|
+
* One object for every guid column, which is what lets `validateSchema` require both sides of a
|
|
34
|
+
* relation pair to carry it, and refuse a guid column referencing two targets.
|
|
35
|
+
*/
|
|
36
|
+
export declare const guidHooks: ColumnHooks<string>;
|
|
37
|
+
/** The binding of a `guid()` column; `undefined` for a column that names one node per row, or is no guid. */
|
|
38
|
+
export declare function getGuidBinding(column: AnyColumn): GuidBinding | undefined;
|
|
39
|
+
/** The binding of a `guid()` column that names its node per row; `undefined` otherwise. */
|
|
40
|
+
export declare function getDynamicGuidBinding(column: AnyColumn): DynamicGuidBinding | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* The member a wrapped id names, checked against the union: its key must be a member, and its
|
|
43
|
+
* payload must carry that member's key field. `undefined` for a value that is not a global id.
|
|
44
|
+
*/
|
|
45
|
+
export declare function decodeMemberId(value: unknown, members: Readonly<Record<string, string>>): {
|
|
46
|
+
key: string;
|
|
47
|
+
value: string;
|
|
48
|
+
} | undefined;
|
|
49
|
+
//# sourceMappingURL=guid-hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guid-hooks.d.ts","sourceRoot":"","sources":["../../../src/schema/utils/guid-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EAET,WAAW,EACX,qBAAqB,EAEtB,MAAM,gBAAgB,CAAC;AAGxB,8EAA8E;AAC9E,MAAM,MAAM,WAAW,GAAG;IACxB,0CAA0C;IAC1C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB,yFAAyF;IACzF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,QAAQ,CAAC,aAAa,EAAE,SAAS,GAAG,qBAAqB,CAAC;IAE1D;;;OAGG;IACH,QAAQ,CAAC,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IAEhD,+EAA+E;IAC/E,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACpD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,EAAE,WAAW,CAAC,MAAM,CAiDzC,CAAC;AAEF,6GAA6G;AAC7G,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,GAAG,WAAW,GAAG,SAAS,CAOzE;AAED,2FAA2F;AAC3F,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,GAAG,kBAAkB,GAAG,SAAS,CAOvF;AAkED;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GACxC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CA4B5C"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ComputedDiscriminator, } from "@dsqlbase/core";
|
|
2
|
+
import { GlobalIdError, decodeGlobalId, encodeGlobalId, isGlobalId } from "./global-id.js";
|
|
3
|
+
/**
|
|
4
|
+
* The hooks every `guid()` column carries: a read wraps the raw uuid as a global id, a write or
|
|
5
|
+
* a filter unwraps one. What an id names comes from the column's references, read when the hook
|
|
6
|
+
* first runs — they are linked and frozen by then — and kept per built column.
|
|
7
|
+
*
|
|
8
|
+
* One object for every guid column, which is what lets `validateSchema` require both sides of a
|
|
9
|
+
* relation pair to carry it, and refuse a guid column referencing two targets.
|
|
10
|
+
*/
|
|
11
|
+
export const guidHooks = {
|
|
12
|
+
filter(value, { column }) {
|
|
13
|
+
if (!isGlobalId(value)) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
const { pk } = decodeGlobalId(value);
|
|
17
|
+
if (!Object.hasOwn(pk, column.fieldName)) {
|
|
18
|
+
throw new Error(`Global id for "${column.fieldName}" carries ${Object.keys(pk).join(", ")} rather than its key "${column.fieldName}".`);
|
|
19
|
+
}
|
|
20
|
+
return pk[column.fieldName];
|
|
21
|
+
},
|
|
22
|
+
read(value, { column, row }) {
|
|
23
|
+
const binding = bindingOf(column);
|
|
24
|
+
if ("key" in binding) {
|
|
25
|
+
return encodeGlobalId(binding.key, { [binding.keyField]: value });
|
|
26
|
+
}
|
|
27
|
+
// A NULL discriminator, or one naming no member, reads the id raw.
|
|
28
|
+
const member = row?.[binding.discriminator.name];
|
|
29
|
+
return typeof member === "string" && Object.hasOwn(binding.members, member)
|
|
30
|
+
? encodeGlobalId(member, { [binding.members[member]]: value })
|
|
31
|
+
: value;
|
|
32
|
+
},
|
|
33
|
+
write(value, { column }) {
|
|
34
|
+
const binding = bindingOf(column);
|
|
35
|
+
return "key" in binding
|
|
36
|
+
? unwrap(value, binding.key, binding.keyField)
|
|
37
|
+
: (decodeMemberId(value, binding.members)?.value ?? value);
|
|
38
|
+
},
|
|
39
|
+
writeDeps(value) {
|
|
40
|
+
if (!isGlobalId(value)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const { key } = decodeGlobalId(value);
|
|
44
|
+
return key;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
/** The binding of a `guid()` column; `undefined` for a column that names one node per row, or is no guid. */
|
|
48
|
+
export function getGuidBinding(column) {
|
|
49
|
+
if (column.hooks !== guidHooks) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const binding = bindingOf(column);
|
|
53
|
+
return "key" in binding ? binding : undefined;
|
|
54
|
+
}
|
|
55
|
+
/** The binding of a `guid()` column that names its node per row; `undefined` otherwise. */
|
|
56
|
+
export function getDynamicGuidBinding(column) {
|
|
57
|
+
if (column.hooks !== guidHooks) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const binding = bindingOf(column);
|
|
61
|
+
return "discriminator" in binding ? binding : undefined;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Bindings per built column. A built column belongs to one registry, so a binding — which
|
|
65
|
+
* depends on the alias a table is exported under — never leaks into another client over the
|
|
66
|
+
* same schema.
|
|
67
|
+
*/
|
|
68
|
+
const BINDINGS = new WeakMap();
|
|
69
|
+
function bindingOf(column) {
|
|
70
|
+
let binding = BINDINGS.get(column);
|
|
71
|
+
if (!binding) {
|
|
72
|
+
binding = resolveBinding(column);
|
|
73
|
+
BINDINGS.set(column, binding);
|
|
74
|
+
}
|
|
75
|
+
return binding;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* What a guid column's ids name, from where its references end:
|
|
79
|
+
*
|
|
80
|
+
* - no reference: its own table and field — a primary key, or a column no relation points
|
|
81
|
+
* through;
|
|
82
|
+
* - a chain of table references: the column it ends at — `articles.authorId` reads as an
|
|
83
|
+
* authors id through `articles.author`;
|
|
84
|
+
* - a union reference: the member its row's discriminator names.
|
|
85
|
+
*
|
|
86
|
+
* `validateSchema` has refused every other shape — two targets, a ring, a member reached
|
|
87
|
+
* through no unique key — so the first end found is the only one.
|
|
88
|
+
*/
|
|
89
|
+
function resolveBinding(column) {
|
|
90
|
+
const { ends } = column.resolveReferences();
|
|
91
|
+
const end = ends.find((candidate) => candidate.kind === "union") ?? ends[0];
|
|
92
|
+
if (!end || (end.kind === "table" && end.column === column)) {
|
|
93
|
+
return { key: column.table.alias, keyField: fieldOf(column.table, column) };
|
|
94
|
+
}
|
|
95
|
+
if (end.kind === "table") {
|
|
96
|
+
return { key: end.column.table.alias, keyField: fieldOf(end.column.table, end.column) };
|
|
97
|
+
}
|
|
98
|
+
const { discriminator, members } = end.reference;
|
|
99
|
+
return {
|
|
100
|
+
discriminator,
|
|
101
|
+
discriminatorField: discriminator instanceof ComputedDiscriminator
|
|
102
|
+
? undefined
|
|
103
|
+
: fieldOf(discriminator.table, discriminator),
|
|
104
|
+
members: Object.fromEntries(Object.entries(members).map(([member, target]) => [member, fieldOf(target.table, target)])),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** The field a built column is declared under on its table. */
|
|
108
|
+
function fieldOf(table, column) {
|
|
109
|
+
const entry = table.getColumnEntries().find(([, candidate]) => candidate === column);
|
|
110
|
+
return entry?.[0] ?? column.name;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The member a wrapped id names, checked against the union: its key must be a member, and its
|
|
114
|
+
* payload must carry that member's key field. `undefined` for a value that is not a global id.
|
|
115
|
+
*/
|
|
116
|
+
export function decodeMemberId(value, members) {
|
|
117
|
+
if (!isGlobalId(value)) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const { key, pk } = decodeGlobalId(value);
|
|
121
|
+
const keyField = members[key];
|
|
122
|
+
if (keyField === undefined) {
|
|
123
|
+
throw new GlobalIdError("key_mismatch", `Global id for "${key}" cannot be stored here: this column holds ids for ` +
|
|
124
|
+
`${Object.keys(members)
|
|
125
|
+
.map((member) => `"${member}"`)
|
|
126
|
+
.join(", ")}.`);
|
|
127
|
+
}
|
|
128
|
+
if (!Object.hasOwn(pk, keyField)) {
|
|
129
|
+
throw new GlobalIdError("key_mismatch", `Global id for "${key}" carries ${Object.keys(pk)
|
|
130
|
+
.map((field) => `"${field}"`)
|
|
131
|
+
.join(", ")} rather than its key "${keyField}".`);
|
|
132
|
+
}
|
|
133
|
+
return { key, value: pk[keyField] };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Reads a written or filtered value back down to what the column stores.
|
|
137
|
+
*
|
|
138
|
+
* Lenient by design: a raw uuid is accepted, because ids reach an application from places
|
|
139
|
+
* that never went through the ORM. A *wrapped* id, though, is checked — that is the whole
|
|
140
|
+
* point of carrying the table in the value. `decodeGlobalId` catches the wrong table; the
|
|
141
|
+
* field check below catches a payload that names the right table but the wrong key.
|
|
142
|
+
*/
|
|
143
|
+
function unwrap(value, alias, fieldName) {
|
|
144
|
+
if (!isGlobalId(value)) {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
const { pk } = decodeGlobalId(value, alias);
|
|
148
|
+
if (!Object.hasOwn(pk, fieldName)) {
|
|
149
|
+
throw new GlobalIdError("key_mismatch", `Global id for "${alias}" carries ${Object.keys(pk)
|
|
150
|
+
.map((field) => `"${field}"`)
|
|
151
|
+
.join(", ")} rather than its key "${fieldName}".`);
|
|
152
|
+
}
|
|
153
|
+
return pk[fieldName];
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=guid-hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guid-hooks.js","sourceRoot":"","sources":["../../../src/schema/utils/guid-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,qBAAqB,GAEtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAiC3F;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,SAAS,GAAwB;IAC5C,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE;QACtB,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,EAAE,EAAE,EAAE,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;QAErC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,KAAK,CACb,kBAAkB,MAAM,CAAC,SAAS,aAA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AACH,SAAS,MAAM,CAAC,KAAa,EAAE,KAAa,EAAE,SAAiB;IAC7D,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,EAAE,EAAE,EAAE,GAAG,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAE5C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,aAAa,CACrB,cAAc,EACd,kBAAkB,KAAK,aAAa,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;aAChD,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,GAAG,CAAC;aAC5B,IAAI,CAAC,IAAI,CAAC,yBAAyB,SAAS,IAAI,CACpD,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,CAAC,SAAS,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import {\n AnyColumn,\n AnyTable,\n ColumnHooks,\n ComputedDiscriminator,\n ReferenceEnd,\n} from \"@dsqlbase/core\";\nimport { GlobalIdError, decodeGlobalId, encodeGlobalId, isGlobalId } from \"./global-id.js\";\n\n/** What a `guid()` column's values name: one node, the same for every row. */\nexport type GuidBinding = {\n /** The node this column's values name. */\n readonly key: string;\n\n /** The field the target node's primary key is addressed by (`id`, `membershipId`, …). */\n readonly keyField: string;\n};\n\n/**\n * What a `guid()` column whose node is named per row names: a key pointing at a union's member\n * — the `from` of a belongs-to a union, or the foreign key of a relation whose source is one.\n * The discriminator names each row's member alias, which is the node key.\n */\nexport type DynamicGuidBinding = {\n /**\n * What names each row's member: a stored column, or a discriminator core computes when the\n * query runs, when the relation declares none. Either is read from the row by its `name`.\n */\n readonly discriminator: AnyColumn | ComputedDiscriminator;\n\n /**\n * The stored discriminator's field name, as `create` / `update` data names it; `undefined`\n * for a computed one, which there is nothing to write to.\n */\n readonly discriminatorField: string | undefined;\n\n /** Member alias → the field its key is addressed by in a payload (`id`, …). */\n readonly members: Readonly<Record<string, string>>;\n};\n\n/**\n * The hooks every `guid()` column carries: a read wraps the raw uuid as a global id, a write or\n * a filter unwraps one. What an id names comes from the column's references, read when the hook\n * first runs — they are linked and frozen by then — and kept per built column.\n *\n * One object for every guid column, which is what lets `validateSchema` require both sides of a\n * relation pair to carry it, and refuse a guid column referencing two targets.\n */\nexport const guidHooks: ColumnHooks<string> = {\n filter(value, { column }) {\n if (!isGlobalId(value)) {\n return value;\n }\n\n const { pk } = decodeGlobalId(value);\n\n if (!Object.hasOwn(pk, column.fieldName)) {\n throw new Error(\n `Global id for \"${column.fieldName}\" carries ${Object.keys(pk).join(\n \", \"\n )} rather than its key \"${column.fieldName}\".`\n );\n }\n\n return pk[column.fieldName];\n },\n read(value, { column, row }) {\n const binding = bindingOf(column);\n\n if (\"key\" in binding) {\n return encodeGlobalId(binding.key, { [binding.keyField]: value });\n }\n\n // A NULL discriminator, or one naming no member, reads the id raw.\n const member = row?.[binding.discriminator.name];\n\n return typeof member === \"string\" && Object.hasOwn(binding.members, member)\n ? encodeGlobalId(member, { [binding.members[member]]: value })\n : value;\n },\n\n write(value, { column }) {\n const binding = bindingOf(column);\n\n return \"key\" in binding\n ? unwrap(value, binding.key, binding.keyField)\n : (decodeMemberId(value, binding.members)?.value ?? value);\n },\n\n writeDeps(value) {\n if (!isGlobalId(value)) {\n return null;\n }\n\n const { key } = decodeGlobalId(value);\n return key;\n },\n};\n\n/** The binding of a `guid()` column; `undefined` for a column that names one node per row, or is no guid. */\nexport function getGuidBinding(column: AnyColumn): GuidBinding | undefined {\n if (column.hooks !== guidHooks) {\n return undefined;\n }\n\n const binding = bindingOf(column);\n return \"key\" in binding ? binding : undefined;\n}\n\n/** The binding of a `guid()` column that names its node per row; `undefined` otherwise. */\nexport function getDynamicGuidBinding(column: AnyColumn): DynamicGuidBinding | undefined {\n if (column.hooks !== guidHooks) {\n return undefined;\n }\n\n const binding = bindingOf(column);\n return \"discriminator\" in binding ? binding : undefined;\n}\n\n/**\n * Bindings per built column. A built column belongs to one registry, so a binding — which\n * depends on the alias a table is exported under — never leaks into another client over the\n * same schema.\n */\nconst BINDINGS = new WeakMap<AnyColumn, GuidBinding | DynamicGuidBinding>();\n\nfunction bindingOf(column: AnyColumn): GuidBinding | DynamicGuidBinding {\n let binding = BINDINGS.get(column);\n\n if (!binding) {\n binding = resolveBinding(column);\n BINDINGS.set(column, binding);\n }\n\n return binding;\n}\n\n/**\n * What a guid column's ids name, from where its references end:\n *\n * - no reference: its own table and field — a primary key, or a column no relation points\n * through;\n * - a chain of table references: the column it ends at — `articles.authorId` reads as an\n * authors id through `articles.author`;\n * - a union reference: the member its row's discriminator names.\n *\n * `validateSchema` has refused every other shape — two targets, a ring, a member reached\n * through no unique key — so the first end found is the only one.\n */\nfunction resolveBinding(column: AnyColumn): GuidBinding | DynamicGuidBinding {\n const { ends } = column.resolveReferences();\n const end: ReferenceEnd | undefined =\n ends.find((candidate) => candidate.kind === \"union\") ?? ends[0];\n\n if (!end || (end.kind === \"table\" && end.column === column)) {\n return { key: column.table.alias, keyField: fieldOf(column.table, column) };\n }\n\n if (end.kind === \"table\") {\n return { key: end.column.table.alias, keyField: fieldOf(end.column.table, end.column) };\n }\n\n const { discriminator, members } = end.reference;\n\n return {\n discriminator,\n discriminatorField:\n discriminator instanceof ComputedDiscriminator\n ? undefined\n : fieldOf(discriminator.table, discriminator),\n members: Object.fromEntries(\n Object.entries(members).map(([member, target]) => [member, fieldOf(target.table, target)])\n ),\n };\n}\n\n/** The field a built column is declared under on its table. */\nfunction fieldOf(table: AnyTable, column: AnyColumn): string {\n const entry = table.getColumnEntries().find(([, candidate]) => candidate === column);\n\n return entry?.[0] ?? column.name;\n}\n\n/**\n * The member a wrapped id names, checked against the union: its key must be a member, and its\n * payload must carry that member's key field. `undefined` for a value that is not a global id.\n */\nexport function decodeMemberId(\n value: unknown,\n members: Readonly<Record<string, string>>\n): { key: string; value: string } | undefined {\n if (!isGlobalId(value)) {\n return undefined;\n }\n\n const { key, pk } = decodeGlobalId(value);\n const keyField = members[key];\n\n if (keyField === undefined) {\n throw new GlobalIdError(\n \"key_mismatch\",\n `Global id for \"${key}\" cannot be stored here: this column holds ids for ` +\n `${Object.keys(members)\n .map((member) => `\"${member}\"`)\n .join(\", \")}.`\n );\n }\n\n if (!Object.hasOwn(pk, keyField)) {\n throw new GlobalIdError(\n \"key_mismatch\",\n `Global id for \"${key}\" carries ${Object.keys(pk)\n .map((field) => `\"${field}\"`)\n .join(\", \")} rather than its key \"${keyField}\".`\n );\n }\n\n return { key, value: pk[keyField] };\n}\n\n/**\n * Reads a written or filtered value back down to what the column stores.\n *\n * Lenient by design: a raw uuid is accepted, because ids reach an application from places\n * that never went through the ORM. A *wrapped* id, though, is checked — that is the whole\n * point of carrying the table in the value. `decodeGlobalId` catches the wrong table; the\n * field check below catches a payload that names the right table but the wrong key.\n */\nfunction unwrap(value: string, alias: string, fieldName: string): string {\n if (!isGlobalId(value)) {\n return value;\n }\n\n const { pk } = decodeGlobalId(value, alias);\n\n if (!Object.hasOwn(pk, fieldName)) {\n throw new GlobalIdError(\n \"key_mismatch\",\n `Global id for \"${alias}\" carries ${Object.keys(pk)\n .map((field) => `\"${field}\"`)\n .join(\", \")} rather than its key \"${fieldName}\".`\n );\n }\n\n return pk[fieldName];\n}\n"]}
|