@kubun/protocol 0.2.4 → 0.2.5

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.
@@ -8,7 +8,7 @@ export type SetDocumentModel = Omit<DocumentModel, 'version' | 'behavior' | 'int
8
8
  behavior?: DocumentModelBehavior;
9
9
  uniqueFields?: Array<string>;
10
10
  interfaces?: Array<string>;
11
- schema: JSONSchema.Object;
11
+ schema?: JSONSchema.Object;
12
12
  fieldsMeta?: DocumentFieldsMeta;
13
13
  };
14
14
  type JSONSchemaScalar = JSONSchema.Boolean | JSONSchema.Integer | JSONSchema.Number | JSONSchema.String;
@@ -1458,6 +1458,7 @@ export declare class ClusterBuilder {
1458
1458
  get cluster(): DocumentModelsCluster;
1459
1459
  get length(): number;
1460
1460
  build(): ClusterModel;
1461
+ get(id: DocumentModelID | string | number): DocumentModel;
1461
1462
  add(inputModel: SetDocumentModel): DocumentModelID;
1462
1463
  addAll(models: Array<SetDocumentModel>): Array<DocumentModelID>;
1463
1464
  }
@@ -1 +1 @@
1
- {"version":3,"file":"cluster.d.ts","sourceRoot":"","sources":["../../src/models/cluster.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAA4C,MAAM,gBAAgB,CAAA;AAC1F,OAAO,EAAE,eAAe,EAAyD,MAAM,WAAW,CAAA;AAClG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEnD,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAElB,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EAG1B,MAAM,eAAe,CAAA;AAItB,MAAM,MAAM,qBAAqB,GAAG,SAAS,GAAG,WAAW,GAAG,QAAQ,CAAA;AAEtE,MAAM,MAAM,gBAAgB,GAAG,IAAI,CACjC,aAAa,EACb,SAAS,GAAG,UAAU,GAAG,YAAY,GAAG,QAAQ,GAAG,YAAY,CAChE,GAAG;IACF,OAAO,CAAC,EAAE,MAAM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C,EAAE,EAAE,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,aAAa;IAmBzD,GAAG,CAAC,UAAU,EAAE,gBAAgB,GAAG,eAAe;IAmDlD,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,gBAAgB,CAAC,GAAG,KAAK,CAAC,eAAe,CAAC;CAOhE"}
@@ -80,7 +80,7 @@ function normalizeDocumentSchema(schema) {
80
80
  throw new Error('Document schema must contain at least one property');
81
81
  }
82
82
  const properties = {};
83
- const refs = {};
83
+ const refs = schema.$defs ?? {};
84
84
  for (const [key, value] of entries){
85
85
  properties[key] = {
86
86
  $ref: encodeSchema(refs, value)
@@ -181,15 +181,65 @@ export class ClusterBuilder {
181
181
  record
182
182
  };
183
183
  }
184
+ get(id) {
185
+ let index;
186
+ if (typeof id === 'number') {
187
+ index = id;
188
+ } else {
189
+ const docID = typeof id === 'string' ? DocumentModelID.fromString(id) : id;
190
+ if (!docID.isLocal) {
191
+ throw new Error('Invalid DocumentModelID to access model in cluster: must be a local ID');
192
+ }
193
+ index = docID.index;
194
+ }
195
+ const model = this.#cluster[index];
196
+ if (model == null) {
197
+ throw new RangeError(`Invalid cluster index: ${index}`);
198
+ }
199
+ return model;
200
+ }
184
201
  add(inputModel) {
202
+ const interfaceIDs = inputModel.interfaces ?? [];
203
+ const interfaces = new Set();
204
+ const schemaProperties = {};
205
+ const schemaReferences = {};
206
+ const schemaRequired = new Set();
207
+ const fieldsMeta = {};
208
+ // Add interface fields
209
+ for (const interfaceModel of interfaceIDs.map((id)=>this.get(id))){
210
+ for (const interfaceID of interfaceModel.interfaces){
211
+ interfaces.add(interfaceID);
212
+ }
213
+ for (const requiredKey of interfaceModel.schema.required){
214
+ schemaRequired.add(requiredKey);
215
+ }
216
+ Object.assign(schemaProperties, interfaceModel.schema.properties);
217
+ Object.assign(schemaReferences, interfaceModel.schema.$defs);
218
+ Object.assign(fieldsMeta, interfaceModel.fieldsMeta ?? {});
219
+ }
220
+ // Add model extra fields
221
+ for (const interfaceID of interfaceIDs){
222
+ interfaces.add(interfaceID);
223
+ }
224
+ for (const requiredKey of inputModel.schema?.required ?? []){
225
+ schemaRequired.add(requiredKey);
226
+ }
227
+ Object.assign(schemaProperties, inputModel.schema?.properties ?? {});
228
+ Object.assign(fieldsMeta, inputModel.fieldsMeta ?? {});
185
229
  const index = this.#cluster.length;
186
230
  const model = {
187
231
  version: '1.0',
188
232
  behavior: 'default',
189
- interfaces: [],
190
233
  ...inputModel,
191
- schema: normalizeDocumentSchema(inputModel.schema),
192
- fieldsMeta: inputModel.fieldsMeta ? normalizeFieldsMeta(inputModel.fieldsMeta) : {}
234
+ interfaces: Array.from(interfaces),
235
+ schema: normalizeDocumentSchema({
236
+ type: 'object',
237
+ properties: schemaProperties,
238
+ required: Array.from(schemaRequired),
239
+ additionalProperties: false,
240
+ $defs: schemaReferences
241
+ }),
242
+ fieldsMeta: normalizeFieldsMeta(fieldsMeta)
193
243
  };
194
244
  assertType(validateDocumentModel, model);
195
245
  this.#cluster.push(model);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/protocol",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "license": "see LICENSE.md",
5
5
  "keywords": [],
6
6
  "type": "module",