@fibery/schema 11.0.0 → 11.2.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.
@@ -0,0 +1,20 @@
1
+ //#region src/brands.d.ts
2
+ declare const HOLDER: unique symbol;
3
+ declare const KIND: unique symbol;
4
+ type HolderBrand<H extends string> = {
5
+ readonly [HOLDER]?: H;
6
+ };
7
+ type KindBrand<K extends string> = {
8
+ readonly [KIND]?: K;
9
+ };
10
+ type Id = string & KindBrand<"id">;
11
+ type PublicId = string & KindBrand<"publicId">;
12
+ type Name = string & KindBrand<"name">;
13
+ type TypeId = Id & HolderBrand<"type">;
14
+ type FieldId = Id & HolderBrand<"field">;
15
+ type EntityId = Id & HolderBrand<"entity">;
16
+ type TypeName = Name & HolderBrand<"type">;
17
+ type FieldName = Name & HolderBrand<"field">;
18
+ type SpaceName = Name & HolderBrand<"space">;
19
+ //#endregion
20
+ export { EntityId, FieldId, FieldName, Id, Name, PublicId, SpaceName, TypeId, TypeName };
package/lib/brands.js ADDED
File without changes
@@ -122,6 +122,8 @@ declare class FieldObject {
122
122
  name: string;
123
123
  }>;
124
124
  get title(): string;
125
+ /** the target type object, or null when the relation points at a removed Database */
126
+ resolveTypeObject(): TypeObject | null;
125
127
  get typeObject(): TypeObject;
126
128
  get holderTypeObject(): TypeObject;
127
129
  get mixinTypeObject(): TypeObject | null;
@@ -1,3 +1,5 @@
1
+ import { NotFoundError } from "./errors.js";
2
+ import { relaxable } from "./relaxable.js";
1
3
  import { parseType, toNonEnumTitle } from "./utils.js";
2
4
  import { isPlainObject } from "@fibery/helpers/src/_";
3
5
  //#region src/field-object.ts
@@ -130,20 +132,33 @@ var FieldObject = class {
130
132
  }
131
133
  return value;
132
134
  }
133
- get typeObject() {
135
+ /** the target type object, or null when the relation points at a removed Database */
136
+ resolveTypeObject() {
134
137
  const cache = this.cache ??= {};
135
- let value = cache.typeObject;
136
- if (!value) {
137
- value = this.schema.resolveTypeObject(this.type);
138
- cache.typeObject = value;
138
+ const value = cache.typeObject;
139
+ if (value) return value;
140
+ const typeObject = this.schema.resolveTypeObject(this.type);
141
+ if (typeObject) cache.typeObject = typeObject;
142
+ return typeObject;
143
+ }
144
+ get typeObject() {
145
+ const typeObject = this.resolveTypeObject();
146
+ if (!typeObject) {
147
+ if (relaxable("fieldObject.typeObject")) return;
148
+ throw new NotFoundError(`!schema.typeObjectsByName["${this.type}"]`);
139
149
  }
140
- return value;
150
+ return typeObject;
141
151
  }
142
152
  get holderTypeObject() {
143
153
  const cache = this.cache ??= {};
144
154
  let value = cache.holderTypeObject;
145
155
  if (!value) {
146
- value = this.schema.resolveTypeObject(this.holderType);
156
+ const holderTypeObject = this.schema.resolveTypeObject(this.holderType);
157
+ if (!holderTypeObject) {
158
+ if (relaxable("fieldObject.holderTypeObject")) return;
159
+ throw new NotFoundError(`!schema.typeObjectsByName["${this.holderType}"]`);
160
+ }
161
+ value = holderTypeObject;
147
162
  cache.holderTypeObject = value;
148
163
  }
149
164
  return value;
package/lib/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import { EntityId, FieldId, FieldName, Id, Name, PublicId, SpaceName, TypeId, TypeName } from "./brands.js";
1
2
  import { NotDefinedGetError, NotFoundError } from "./errors.js";
2
3
  import { RawTypeObject, SyncSource, TypeObject, TypeObjectFactory, TypeObjectParams } from "./type-object.js";
3
4
  import { RawSchema, Schema, SchemaRef } from "./schema.js";
4
5
  import { Cardinality, FieldObject, FieldObjectParams, LinkRule, LinkRuleOperand, RawFieldObject, SyncField, SyncRelation } from "./field-object.js";
5
6
  import { Factory, factory, protectiveFactory } from "./factory.js";
6
7
  import { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets } from "./utils.js";
7
- export { Cardinality, Factory, FieldObject, FieldObjectParams, LinkRule, LinkRuleOperand, NotDefinedGetError, NotFoundError, RawFieldObject, RawSchema, RawTypeObject, Schema, SchemaRef, SyncField, SyncRelation, SyncSource, TypeObject, TypeObjectFactory, TypeObjectParams, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
8
+ export { Cardinality, EntityId, Factory, FieldId, FieldName, FieldObject, FieldObjectParams, Id, LinkRule, LinkRuleOperand, Name, NotDefinedGetError, NotFoundError, PublicId, RawFieldObject, RawSchema, RawTypeObject, Schema, SchemaRef, SpaceName, SyncField, SyncRelation, SyncSource, TypeId, TypeName, TypeObject, TypeObjectFactory, TypeObjectParams, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
package/lib/schema.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { TypeId, TypeName } from "./brands.js";
1
2
  import { RawTypeObject, TypeObject } from "./type-object.js";
2
3
  import { FieldObject } from "./field-object.js";
3
4
  import { Factory } from "./factory.js";
@@ -13,7 +14,7 @@ type RawSchema = {
13
14
  */
14
15
  type SchemaRef = {
15
16
  readonly version: string;
16
- resolveTypeObject: (type: string) => TypeObject;
17
+ resolveTypeObject: (type: string) => TypeObject | null;
17
18
  resolveTypeComponentFieldObject: (type: string) => FieldObject | null;
18
19
  resolveMultiRelatedFieldObjects: (multiRelation: string) => ReadonlyArray<FieldObject>;
19
20
  };
@@ -35,15 +36,15 @@ declare class Schema implements SchemaRef {
35
36
  };
36
37
  };
37
38
  /** @internal resolves a field's `fibery/type` to its type object */
38
- resolveTypeObject(type: string): TypeObject;
39
+ resolveTypeObject(type: TypeName): TypeObject | null;
39
40
  /** @internal the type-component field pointing at `type`, if any */
40
41
  resolveTypeComponentFieldObject(type: string): FieldObject | null;
41
42
  /** @internal the non-entity-ref ends of a multi-relation */
42
43
  resolveMultiRelatedFieldObjects(multiRelation: string): ReadonlyArray<FieldObject>;
43
- getTypeObjectById(typeId: string): TypeObject | null;
44
- getTypeObjectByName(typeName: string): TypeObject | null;
45
- getTypeObjectByIdOrThrow(typeId: string): TypeObject;
46
- getTypeObjectByNameOrThrow(typeName: string): TypeObject;
44
+ getTypeObjectById(typeId: TypeId): TypeObject | null;
45
+ getTypeObjectByName(typeName: TypeName): TypeObject | null;
46
+ getTypeObjectByIdOrThrow(typeId: TypeId): TypeObject;
47
+ getTypeObjectByNameOrThrow(typeName: TypeName): TypeObject;
47
48
  }
48
49
  //#endregion
49
50
  export { RawSchema, Schema, SchemaRef };
package/lib/schema.js CHANGED
@@ -31,7 +31,7 @@ var Schema = class {
31
31
  }
32
32
  /** @internal resolves a field's `fibery/type` to its type object */
33
33
  resolveTypeObject(type) {
34
- return this.typeObjectsByName[type] || null;
34
+ return this.getTypeObjectByName(type);
35
35
  }
36
36
  /** @internal the type-component field pointing at `type`, if any */
37
37
  resolveTypeComponentFieldObject(type) {
@@ -1,3 +1,4 @@
1
+ import { FieldId, FieldName } from "./brands.js";
1
2
  import { SchemaRef } from "./schema.js";
2
3
  import { FieldObject, FieldObjectParams, RawFieldObject } from "./field-object.js";
3
4
  //#region src/type-object.d.ts
@@ -57,10 +58,10 @@ declare class TypeObject {
57
58
  private cache;
58
59
  constructor({ rawTypeObject, schema, factory }: TypeObjectParams);
59
60
  toJSON(): string | RawTypeObject;
60
- getFieldObjectById(fieldId: string): FieldObject | null;
61
- getFieldObjectByName(fieldName: string): FieldObject | null;
62
- getFieldObjectByIdOrThrow(fieldId: string): FieldObject;
63
- getFieldObjectByNameOrThrow(fieldName: string): FieldObject;
61
+ getFieldObjectById(fieldId: FieldId): FieldObject | null;
62
+ getFieldObjectByName(fieldName: FieldName): FieldObject | null;
63
+ getFieldObjectByIdOrThrow(fieldId: FieldId): FieldObject;
64
+ getFieldObjectByNameOrThrow(fieldName: FieldName): FieldObject;
64
65
  get rawMeta(): Readonly<RawTypeObject["fibery/meta"]>;
65
66
  get name(): string;
66
67
  get description(): string | null;
@@ -1,7 +1,7 @@
1
1
  import { NotFoundError } from "./errors.js";
2
- import { parseType, toTitle } from "./utils.js";
3
2
  import { relaxable } from "./relaxable.js";
4
- import { isPlainObject } from "@fibery/helpers/src/_";
3
+ import { parseType, toTitle } from "./utils.js";
4
+ import { isPlainObject, truthy } from "@fibery/helpers/src/_";
5
5
  import { pluralize } from "@fibery/pluralize";
6
6
  //#region src/type-object.ts
7
7
  const EMPTY_META = Object.freeze({});
@@ -122,7 +122,7 @@ var TypeObject = class {
122
122
  const cache = this.cache ??= {};
123
123
  let value = cache.installedMixinTypeObjects;
124
124
  if (!value) {
125
- value = [...this.installedMixins].map((mixinName) => this.schema.resolveTypeObject(mixinName)).filter((typeObject) => typeObject);
125
+ value = [...this.installedMixins].map((mixinName) => this.schema.resolveTypeObject(mixinName)).filter(truthy);
126
126
  cache.installedMixinTypeObjects = value;
127
127
  }
128
128
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/schema",
3
- "version": "11.0.0",
3
+ "version": "11.2.0",
4
4
  "description": "Fibery schema utils",
5
5
  "license": "UNLICENSED",
6
6
  "files": [