@fibery/schema 10.3.1 → 11.0.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.
@@ -1,185 +1,253 @@
1
+ import { NotFoundError } from "./errors.js";
1
2
  import { parseType, toTitle } from "./utils.js";
2
- import { once } from "@fibery/helpers/src/_";
3
- import isFunction from "lodash/isFunction.js";
4
- import isPlainObject from "lodash/isPlainObject.js";
3
+ import { relaxable } from "./relaxable.js";
4
+ import { isPlainObject } from "@fibery/helpers/src/_";
5
5
  import { pluralize } from "@fibery/pluralize";
6
6
  //#region src/type-object.ts
7
+ const EMPTY_META = Object.freeze({});
8
+ const DEFAULT_COLOR = "#4978D4";
7
9
  var TypeObject = class {
8
- name;
9
- description;
10
- id;
11
- rawMeta;
12
- isDomain;
13
- isPlatform;
14
- isHighlight;
15
- isMixin;
16
- mixinIcon;
17
- isEnum;
18
- isPrimitive;
19
- isWriterModeByDefault;
20
- nameParts;
21
- color;
22
- units;
23
- installedMixins;
24
- syncSource;
25
- version;
26
- installedMixinTypeObjects;
27
- title;
28
- pluralTitle;
29
- singularTitle;
30
- fieldObjects;
31
- fieldObjectsById;
32
- fieldObjectsByName;
33
- typeComponentFieldObject;
34
- isDeleted;
35
- isEntityRef;
36
- constructor({ rawTypeObject, resolveTypeObject, getTypeComponentTypeToFieldObject, version, factory, resolveMultiRelatedFieldObjects }) {
10
+ raw;
11
+ schema;
12
+ factory;
13
+ /** Lazily filled; declared here so all instances share one hidden class. */
14
+ cache;
15
+ constructor({ rawTypeObject, schema, factory }) {
37
16
  if (!isPlainObject(rawTypeObject)) throw new Error("rawTypeObject arg must be plain object");
38
- if (!isFunction(resolveTypeObject)) throw new Error("resolveTypeObject arg must be function");
39
- this.name = rawTypeObject["fibery/name"];
40
- this.description = rawTypeObject["fibery/description"] || null;
41
- this.id = rawTypeObject["fibery/id"];
42
- const rawMeta = rawTypeObject["fibery/meta"] || {};
43
- this.rawMeta = rawMeta;
44
- this.isDomain = rawMeta["fibery/domain?"] || false;
45
- this.isPlatform = rawMeta["fibery/platform?"] || false;
46
- this.isHighlight = rawMeta["fibery/highlight?"] || false;
47
- this.isMixin = rawMeta["app/mixin?"] || false;
48
- this.mixinIcon = rawMeta["ui/mixin-icon"] || null;
49
- this.isEnum = rawMeta["fibery/enum?"] || false;
50
- this.isPrimitive = rawMeta["fibery/primitive?"] || false;
51
- this.isWriterModeByDefault = rawMeta["ui/writer-mode?"] || false;
52
- this.isDeleted = rawTypeObject["fibery/deleted?"] || false;
53
- this.nameParts = parseType(this.name);
54
- this.color = rawMeta["ui/color"] || "#4978D4";
55
- this.units = rawMeta["ui/units"] || null;
56
- this.installedMixins = new Set(Object.keys(rawMeta["app/mixins"] || {}));
57
- this.syncSource = rawMeta["sync/source"] || null;
58
- this.version = version;
59
- this.isEntityRef = rawTypeObject["fibery/name"] === "fibery/entity-ref";
60
- const fieldObjectsByNameOnceGetter = once(() => {
61
- return factory.makeFieldObjectsByName({
17
+ if (!schema) throw new Error("schema arg must be provided");
18
+ this.raw = rawTypeObject;
19
+ this.schema = schema;
20
+ this.factory = factory;
21
+ this.cache = void 0;
22
+ }
23
+ toJSON() {
24
+ if (process.env.NODE_ENV === "test") return `<schema.typeObjectsByName["${this.raw["fibery/name"]}"]>`;
25
+ return this.raw;
26
+ }
27
+ getFieldObjectById(fieldId) {
28
+ return Object.hasOwn(this.fieldObjectsById, fieldId) ? this.fieldObjectsById[fieldId] : null;
29
+ }
30
+ getFieldObjectByName(fieldName) {
31
+ return Object.hasOwn(this.fieldObjectsByName, fieldName) ? this.fieldObjectsByName[fieldName] : null;
32
+ }
33
+ getFieldObjectByIdOrThrow(fieldId) {
34
+ const fieldObject = this.getFieldObjectById(fieldId);
35
+ if (!fieldObject) {
36
+ if (relaxable("getFieldObjectByIdOrThrow")) return;
37
+ throw new NotFoundError(`!schema.typeObjectsByName["${this.name}"].fieldObjectsById["${fieldId}"]`);
38
+ }
39
+ return fieldObject;
40
+ }
41
+ getFieldObjectByNameOrThrow(fieldName) {
42
+ const fieldObject = this.getFieldObjectByName(fieldName);
43
+ if (!fieldObject) {
44
+ if (relaxable("getFieldObjectByNameOrThrow")) return;
45
+ throw new NotFoundError(`!schema.typeObjectsByName["${this.name}"].fieldObjectsByName["${fieldName}"]`);
46
+ }
47
+ return fieldObject;
48
+ }
49
+ get rawMeta() {
50
+ return this.raw["fibery/meta"] || EMPTY_META;
51
+ }
52
+ get name() {
53
+ return this.raw["fibery/name"];
54
+ }
55
+ get description() {
56
+ return this.raw["fibery/description"] || null;
57
+ }
58
+ get id() {
59
+ return this.raw["fibery/id"];
60
+ }
61
+ get isDeleted() {
62
+ return this.raw["fibery/deleted?"] || false;
63
+ }
64
+ get version() {
65
+ return this.schema.version;
66
+ }
67
+ get isEntityRef() {
68
+ return this.raw["fibery/name"] === "fibery/entity-ref";
69
+ }
70
+ get isDomain() {
71
+ return this.rawMeta["fibery/domain?"] || false;
72
+ }
73
+ get isPlatform() {
74
+ return this.rawMeta["fibery/platform?"] || false;
75
+ }
76
+ get isHighlight() {
77
+ return this.rawMeta["fibery/highlight?"] || false;
78
+ }
79
+ get isMixin() {
80
+ return this.rawMeta["app/mixin?"] || false;
81
+ }
82
+ get mixinIcon() {
83
+ return this.rawMeta["ui/mixin-icon"] || null;
84
+ }
85
+ get isEnum() {
86
+ return this.rawMeta["fibery/enum?"] || false;
87
+ }
88
+ get isPrimitive() {
89
+ return this.rawMeta["fibery/primitive?"] || false;
90
+ }
91
+ get isWriterModeByDefault() {
92
+ return this.rawMeta["ui/writer-mode?"] || false;
93
+ }
94
+ get color() {
95
+ return this.rawMeta["ui/color"] || DEFAULT_COLOR;
96
+ }
97
+ get units() {
98
+ return this.rawMeta["ui/units"] || null;
99
+ }
100
+ get syncSource() {
101
+ return this.rawMeta["sync/source"] || null;
102
+ }
103
+ get nameParts() {
104
+ const cache = this.cache ??= {};
105
+ let value = cache.nameParts;
106
+ if (!value) {
107
+ value = parseType(this.name);
108
+ cache.nameParts = value;
109
+ }
110
+ return value;
111
+ }
112
+ get installedMixins() {
113
+ const cache = this.cache ??= {};
114
+ let value = cache.installedMixins;
115
+ if (!value) {
116
+ value = new Set(Object.keys(this.rawMeta["app/mixins"] || {}));
117
+ cache.installedMixins = value;
118
+ }
119
+ return value;
120
+ }
121
+ get installedMixinTypeObjects() {
122
+ const cache = this.cache ??= {};
123
+ let value = cache.installedMixinTypeObjects;
124
+ if (!value) {
125
+ value = [...this.installedMixins].map((mixinName) => this.schema.resolveTypeObject(mixinName)).filter((typeObject) => typeObject);
126
+ cache.installedMixinTypeObjects = value;
127
+ }
128
+ return value;
129
+ }
130
+ get fieldObjects() {
131
+ const cache = this.cache ??= {};
132
+ let value = cache.fieldObjects;
133
+ if (!value) {
134
+ value = this.raw["fibery/fields"].map((rawFieldObject) => this.factory.makeFieldObject({
135
+ rawFieldObject,
136
+ holderType: this.name,
137
+ schema: this.schema
138
+ }));
139
+ cache.fieldObjects = value;
140
+ }
141
+ return value;
142
+ }
143
+ get fieldObjectsByName() {
144
+ const cache = this.cache ??= {};
145
+ let value = cache.fieldObjectsByName;
146
+ if (!value) {
147
+ value = this.factory.makeFieldObjectsByName({
62
148
  type: this.name,
63
149
  fieldObjects: this.fieldObjects
64
150
  });
65
- });
66
- const fieldObjectsGetter = once(() => rawTypeObject["fibery/fields"].map((rawFieldObject) => {
67
- return factory.makeFieldObject({
68
- rawFieldObject,
69
- holderType: this.name,
70
- resolveTypeObject,
71
- version,
72
- resolveMultiRelatedFieldObjects
151
+ cache.fieldObjectsByName = value;
152
+ }
153
+ return value;
154
+ }
155
+ get fieldObjectsById() {
156
+ const cache = this.cache ??= {};
157
+ let value = cache.fieldObjectsById;
158
+ if (!value) {
159
+ value = this.factory.makeFieldObjectsById({
160
+ type: this.name,
161
+ fieldObjects: this.fieldObjects
73
162
  });
74
- }));
75
- Object.defineProperties(this, {
76
- toJSON: {
77
- value() {
78
- if (process.env.NODE_ENV === "test") return `<schema.typeObjectsByName["${rawTypeObject["fibery/name"]}"]>`;
79
- return rawTypeObject;
80
- },
81
- enumerable: false
82
- },
83
- fieldObjects: {
84
- configurable: false,
85
- enumerable: true,
86
- get: fieldObjectsGetter
87
- },
88
- typeComponentFieldObject: {
89
- configurable: false,
90
- enumerable: true,
91
- get: () => {
92
- return getTypeComponentTypeToFieldObject(this.name);
93
- }
94
- },
95
- fieldObjectsById: {
96
- configurable: false,
97
- enumerable: true,
98
- get: once(() => {
99
- return factory.makeFieldObjectsById({
100
- type: this.name,
101
- fieldObjects: this.fieldObjects
102
- });
103
- })
104
- },
105
- fieldObjectsByName: {
106
- configurable: false,
107
- enumerable: true,
108
- get: fieldObjectsByNameOnceGetter
109
- },
110
- installedMixinTypeObjects: {
111
- configurable: false,
112
- enumerable: true,
113
- get: once(() => [...this.installedMixins].map(resolveTypeObject).filter((typeObject) => typeObject))
114
- },
115
- title: {
116
- configurable: false,
117
- enumerable: true,
118
- get: once(() => toTitle(this.name))
119
- },
120
- pluralTitle: {
121
- configurable: false,
122
- enumerable: true,
123
- get: once(() => pluralize.plural(toTitle(this.name)))
124
- },
125
- singularTitle: {
126
- configurable: false,
127
- enumerable: true,
128
- get: once(() => pluralize.singular(toTitle(this.name)))
129
- }
130
- });
131
- }
132
- _getFieldObjectShortcuts = once(() => {
133
- const hasRank = this.installedMixins.has("fibery/rank-mixin");
134
- return this.fieldObjects.reduce((result, fieldObject) => {
135
- if (fieldObject.isId) {
136
- result.idField = fieldObject.name;
137
- result.idFieldObject = fieldObject;
138
- } else if (fieldObject.isPublicId) {
139
- result.publicIdField = fieldObject.name;
140
- result.publicIdFieldObject = fieldObject;
141
- } else if (fieldObject.isTitle) {
142
- result.titleField = fieldObject.name;
143
- result.titleFieldObject = fieldObject;
144
- } else if (hasRank && fieldObject.name === "fibery/rank") {
145
- result.rankField = fieldObject.name;
146
- result.rankFieldObject = fieldObject;
147
- }
148
- return result;
149
- }, {
150
- idField: null,
151
- idFieldObject: null,
152
- publicIdField: null,
153
- publicIdFieldObject: null,
154
- titleField: null,
155
- titleFieldObject: null,
156
- rankField: null,
157
- rankFieldObject: null
158
- });
159
- });
163
+ cache.fieldObjectsById = value;
164
+ }
165
+ return value;
166
+ }
167
+ get typeComponentFieldObject() {
168
+ const cache = this.cache ??= {};
169
+ if (!cache.hasTypeComponentFieldObject) {
170
+ cache.typeComponentFieldObject = this.schema.resolveTypeComponentFieldObject(this.name);
171
+ cache.hasTypeComponentFieldObject = true;
172
+ }
173
+ return cache.typeComponentFieldObject ?? null;
174
+ }
175
+ get title() {
176
+ const cache = this.cache ??= {};
177
+ let value = cache.title;
178
+ if (value === void 0) {
179
+ value = toTitle(this.name);
180
+ cache.title = value;
181
+ }
182
+ return value;
183
+ }
184
+ get pluralTitle() {
185
+ const cache = this.cache ??= {};
186
+ let value = cache.pluralTitle;
187
+ if (value === void 0) {
188
+ value = pluralize.plural(toTitle(this.name));
189
+ cache.pluralTitle = value;
190
+ }
191
+ return value;
192
+ }
193
+ get singularTitle() {
194
+ const cache = this.cache ??= {};
195
+ let value = cache.singularTitle;
196
+ if (value === void 0) {
197
+ value = pluralize.singular(toTitle(this.name));
198
+ cache.singularTitle = value;
199
+ }
200
+ return value;
201
+ }
202
+ get known() {
203
+ const cache = this.cache ??= {};
204
+ let value = cache.shortcuts;
205
+ if (!value) {
206
+ let idFieldObject = null;
207
+ let publicIdFieldObject = null;
208
+ let titleFieldObject = null;
209
+ let rankFieldObject = null;
210
+ for (const fieldObject of this.fieldObjects) if (fieldObject.isId) idFieldObject = fieldObject;
211
+ else if (fieldObject.isPublicId) publicIdFieldObject = fieldObject;
212
+ else if (fieldObject.isTitle) titleFieldObject = fieldObject;
213
+ else if (fieldObject.name === "fibery/rank" && this.installedMixins.has("fibery/rank-mixin")) rankFieldObject = fieldObject;
214
+ value = {
215
+ idField: idFieldObject?.name ?? null,
216
+ idFieldObject,
217
+ publicIdField: publicIdFieldObject?.name ?? null,
218
+ publicIdFieldObject,
219
+ titleField: titleFieldObject?.name ?? null,
220
+ titleFieldObject,
221
+ rankField: rankFieldObject?.name ?? null,
222
+ rankFieldObject
223
+ };
224
+ cache.shortcuts = value;
225
+ }
226
+ return value;
227
+ }
160
228
  get idField() {
161
- return this._getFieldObjectShortcuts().idField;
229
+ return this.known.idField;
162
230
  }
163
231
  get idFieldObject() {
164
- return this._getFieldObjectShortcuts().idFieldObject;
232
+ return this.known.idFieldObject;
165
233
  }
166
234
  get publicIdField() {
167
- return this._getFieldObjectShortcuts().publicIdField;
235
+ return this.known.publicIdField;
168
236
  }
169
237
  get publicIdFieldObject() {
170
- return this._getFieldObjectShortcuts().publicIdFieldObject;
238
+ return this.known.publicIdFieldObject;
171
239
  }
172
240
  get titleField() {
173
- return this._getFieldObjectShortcuts().titleField;
241
+ return this.known.titleField;
174
242
  }
175
243
  get titleFieldObject() {
176
- return this._getFieldObjectShortcuts().titleFieldObject;
244
+ return this.known.titleFieldObject;
177
245
  }
178
246
  get rankField() {
179
- return this._getFieldObjectShortcuts().rankField;
247
+ return this.known.rankField;
180
248
  }
181
249
  get rankFieldObject() {
182
- return this._getFieldObjectShortcuts().rankFieldObject;
250
+ return this.known.rankFieldObject;
183
251
  }
184
252
  };
185
253
  //#endregion
package/lib/utils.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { SyncSource, TypeObject } from "./type-object.js";
2
- import { RawFieldObject } from "./field-object.js";
3
2
  import { RawSchema } from "./schema.js";
3
+ import { RawFieldObject } from "./field-object.js";
4
4
  //#region src/utils.d.ts
5
5
  declare const withOnlyDefinedGets: (target: any, targetKind?: string) => any;
6
6
  declare const capitalizeFirstChar: (input: string) => string;
@@ -15,13 +15,10 @@ declare const parseEnumType: (type: string) => {
15
15
  targetNamespace: string;
16
16
  };
17
17
  declare const toTitle: (typeOrAttr: string) => string;
18
- declare const parseType: ((arg: string) => {
18
+ declare const parseType: (type: string) => {
19
19
  namespace: string;
20
20
  name: string;
21
- }) & import("@fibery/helpers/src/_").MemoizedFunction<string, {
22
- namespace: string;
23
- name: string;
24
- }>;
21
+ };
25
22
  declare const makeEnumTypeNamePart: ({ holderType, enumName }: {
26
23
  holderType: string;
27
24
  enumName: string;
@@ -31,8 +28,6 @@ declare const attachEnumNamespace: ({ enumNamespace, typeNamePart }: {
31
28
  typeNamePart: string;
32
29
  }) => string;
33
30
  declare const removeDeletedTypesAndFields: (rawSchema: RawSchema) => {
34
- "fibery/id": string;
35
- "fibery/version": string;
36
31
  "fibery/types": {
37
32
  "fibery/fields": RawFieldObject[];
38
33
  "fibery/id": string;
@@ -53,8 +48,13 @@ declare const removeDeletedTypesAndFields: (rawSchema: RawSchema) => {
53
48
  "app/mixins"?: Record<string, boolean>;
54
49
  "sync/source"?: SyncSource;
55
50
  "fibery/secured?"?: boolean;
51
+ "fibery/type-component?"?: boolean;
52
+ "ref/ref-db?"?: boolean;
53
+ "semantic-index/enabled"?: boolean;
56
54
  };
57
55
  }[];
56
+ "fibery/id": string;
57
+ "fibery/version": string;
58
58
  };
59
59
  declare const getTypeObjectTitle: (typeObject: TypeObject, form?: "singular" | "plural") => string;
60
60
  //#endregion
package/lib/utils.js CHANGED
@@ -1,10 +1,6 @@
1
1
  import { NotDefinedGetError } from "./errors.js";
2
2
  import { printJsPath } from "./print-utils.js";
3
- import { memoize } from "@fibery/helpers/src/_";
4
3
  import { traceError } from "@fibery/helpers/utils/trace";
5
- import capitalize from "lodash/capitalize.js";
6
- import flow from "lodash/flow.js";
7
- import last from "lodash/last.js";
8
4
  //#region src/utils.ts
9
5
  const testRunnerKeys = /* @__PURE__ */ new Set([
10
6
  "nodeType",
@@ -41,9 +37,10 @@ const capitalizeFirstChar = (input) => {
41
37
  return str ? str.charAt(0).toUpperCase() + str.slice(1) : str;
42
38
  };
43
39
  const replaceTildes = (str) => (str || "").split("~").join(" ");
44
- const toTypeOrFieldNamePartTitle = flow([capitalizeFirstChar, replaceTildes]);
45
- const toTypeOrFieldTitle = flow([getNamePart, toTypeOrFieldNamePartTitle]);
46
- const toPascalCase = (lispCasedString = "") => lispCasedString && (last(lispCasedString.split("/")) || "").split("-").map(capitalize).join(" ");
40
+ const toTypeOrFieldNamePartTitle = (str) => replaceTildes(capitalizeFirstChar(str || ""));
41
+ const toTypeOrFieldTitle = (str) => toTypeOrFieldNamePartTitle(getNamePart(str || ""));
42
+ const capitalizeWord = (str) => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
43
+ const toPascalCase = (lispCasedString = "") => lispCasedString && (lispCasedString.split("/").at(-1) || "").split("-").map(capitalizeWord).join(" ");
47
44
  const isObsoleteTitle = (str) => {
48
45
  const { name } = splitKeyword(str);
49
46
  return name.toLowerCase() === name && !hasTildes(name);
@@ -88,10 +85,10 @@ const splitKeyword = (keyword) => {
88
85
  name: str.substring(index + 1)
89
86
  };
90
87
  };
91
- const parseType = memoize((type) => {
88
+ const parseType = (type) => {
92
89
  if (!type.includes("/")) traceError(`parseType invalid usage: passed type is '${type}'`);
93
90
  return splitKeyword(type);
94
- });
91
+ };
95
92
  const makeEnumTypeNamePart = ({ holderType, enumName }) => {
96
93
  const { namespace, name } = parseType(holderType);
97
94
  return `${enumName}_${namespace}/${name}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/schema",
3
- "version": "10.3.1",
3
+ "version": "11.0.0",
4
4
  "description": "Fibery schema utils",
5
5
  "license": "UNLICENSED",
6
6
  "files": [
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "lodash": "4.17.21",
25
- "@fibery/helpers": "3.2.1",
25
+ "@fibery/helpers": "3.3.1",
26
26
  "@fibery/pluralize": "1.2.1"
27
27
  }
28
28
  }