@cepharum/concrete-db 0.2.1 → 0.2.2

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/concrete-db.d.ts CHANGED
@@ -1,32 +1,4 @@
1
- /**
2
- * (c) 2022 cepharum GmbH, Berlin, http://cepharum.de
3
- *
4
- * The MIT License (MIT)
5
- *
6
- * Copyright (c) 2021 cepharum GmbH
7
- *
8
- * Permission is hereby granted, free of charge, to any person obtaining a copy
9
- * of this software and associated documentation files (the "Software"), to deal
10
- * in the Software without restriction, including without limitation the rights
11
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
- * copies of the Software, and to permit persons to whom the Software is
13
- * furnished to do so, subject to the following conditions:
14
- *
15
- * The above copyright notice and this permission notice shall be included in all
16
- * copies or substantial portions of the Software.
17
- *
18
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- * SOFTWARE.
25
- *
26
- * @author: cepharum
27
- */
28
-
29
- declare module "concrete-db" {
1
+ declare namespace ConcreteDB {
30
2
  export interface CuringOptions extends CollectorOptions, ShaperOptions, GeneratorOptions {
31
3
  /**
32
4
  * Selects base folder for writing files of resulting concrete-db.
@@ -260,18 +232,34 @@ declare module "concrete-db" {
260
232
  */
261
233
  export type ShapeCollections = ShapeCollection[];
262
234
 
235
+ /**
236
+ * Defines optional constraints to apply.
237
+ */
238
+ export interface ShapeConstraints {
239
+ /**
240
+ * If set, multiple source records addressing same target record aren't
241
+ * merged, but some exception is thrown.
242
+ *
243
+ * This doesn't affect definition of variants. However, merging single
244
+ * variant from multiple sources is rejected, too.
245
+ */
246
+ singleSource?: boolean;
247
+ }
248
+
263
249
  /**
264
250
  * Describes rules controlling shaping of a model.
265
251
  */
266
252
  export interface ShapeModel {
267
253
  defaults: ShapePropertyDefaults;
268
- properties: ShapeProperties;
269
- collections: ShapeCollections;
270
- contributions: {
254
+ properties?: ShapeProperties;
255
+ collections?: ShapeCollections;
256
+ contributions?: {
271
257
  [contributedModelName: string]: {
272
258
  [targetPropertyName: string]: string;
273
259
  };
274
260
  };
261
+
262
+ constraints?: ShapeConstraints;
275
263
  }
276
264
 
277
265
  export interface Shape {
package/lib/shaper.mjs CHANGED
@@ -166,7 +166,7 @@ export class Shaper extends EventEmitter {
166
166
  const augmentedData = augmentDataSpace( data, metaProperties );
167
167
 
168
168
  metaProperties.$model = this.detectModel( augmentedData, shape );
169
- metaProperties.$shape = this.getShapeOfModel( {}, shape, metaProperties.$model );
169
+ metaProperties.$shape = this.getShapeOfModel( shapesPerModelCache, shape, metaProperties.$model );
170
170
  metaProperties.$id = this.identifyRecord( augmentedData, metaProperties.$shape );
171
171
 
172
172
  if ( !itemsPerModel.hasOwnProperty( metaProperties.$model ) ) {
@@ -551,6 +551,10 @@ export class Shaper extends EventEmitter {
551
551
  * @returns {ModelItem} provided target
552
552
  */
553
553
  mergeModelItem( target, source ) {
554
+ if ( target?.$shape?.constraints?.singleSource ) {
555
+ throw new Error( `merging ${target.$model} with ID ${target.$id} from multiple source records rejected by shape` );
556
+ }
557
+
554
558
  const _to = target || {};
555
559
  const oldNames = target ? Object.keys( _to.$shape.properties ) : [];
556
560
  const newNames = Object.keys( source.$shape.properties );
@@ -558,8 +562,8 @@ export class Shaper extends EventEmitter {
558
562
  // merge properties of items
559
563
  for ( const name of Object.keys( source ) ) {
560
564
  if ( !String( name ).startsWith( "$" ) ) {
561
- const wasCollecting = oldNames.some( n => n.replace( /\s*\[]$/, "" ).trim() === name );
562
- const isCollecting = newNames.some( n => n.replace( /\s*\[]$/, "" ).trim() === name );
565
+ const wasCollecting = oldNames.some( n => n.replace( /\s+/g, "" ) === name + "[]" );
566
+ const isCollecting = newNames.some( n => n.replace( /\s+/g, "" ) === name + "[]" );
563
567
 
564
568
  if ( _to.hasOwnProperty( name ) ) {
565
569
  if ( isCollecting ) {
@@ -709,7 +713,7 @@ export class Shaper extends EventEmitter {
709
713
 
710
714
  for ( const property of Object.keys( properties ) ) {
711
715
  if ( accept.indexOf( property ) > -1 || !String( property ).startsWith( "$" ) ) {
712
- const propertyName = property.replace( /\s*\[]$/, "" );
716
+ const propertyName = property.replace( /\s*\[]\s*$/, "" );
713
717
  const isCollecting = property !== propertyName;
714
718
 
715
719
  const term = properties[property];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cepharum/concrete-db",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "a read-only web database generator",
5
5
  "main": "lib/collector.mjs",
6
6
  "types": "concrete-db.d.ts",