@elek-io/core 0.1.0 → 0.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.
package/dist/index.js CHANGED
@@ -6,8 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/index.ts
8
8
  import {
9
- constructorElekIoCoreSchema,
10
- environmentSchema
9
+ constructorElekIoCoreSchema
11
10
  } from "@elek-io/shared";
12
11
  import Fs8 from "fs-extra";
13
12
 
@@ -19,13 +18,13 @@ import {
19
18
  createAssetSchema,
20
19
  currentTimestamp,
21
20
  deleteAssetSchema,
22
- fileTypeSchema as fileTypeSchema2,
23
21
  listAssetsSchema,
22
+ objectTypeSchema as objectTypeSchema2,
24
23
  readAssetSchema,
25
24
  serviceTypeSchema as serviceTypeSchema3,
26
- supportedExtensionSchema,
27
- supportedFileTypeSchema,
28
- supportedMimeTypeSchema,
25
+ supportedAssetExtensionSchema,
26
+ supportedAssetMimeTypeSchema,
27
+ supportedAssetTypeSchema,
29
28
  updateAssetSchema,
30
29
  uuid as uuid2
31
30
  } from "@elek-io/shared";
@@ -104,11 +103,11 @@ var pathTo = {
104
103
  `${id}.${language}.json`
105
104
  );
106
105
  },
107
- values: (projectId) => {
108
- return Path.join(pathTo.project(projectId), "values");
106
+ sharedValues: (projectId) => {
107
+ return Path.join(pathTo.project(projectId), "shared-values");
109
108
  },
110
- valueFile: (projectId, id, language) => {
111
- return Path.join(pathTo.values(projectId), `${id}.${language}.json`);
109
+ sharedValueFile: (projectId, id, language) => {
110
+ return Path.join(pathTo.sharedValues(projectId), `${id}.${language}.json`);
112
111
  },
113
112
  assets: (projectId) => {
114
113
  return Path.join(
@@ -236,8 +235,8 @@ function getDuplicates(arr, key) {
236
235
  // src/service/AbstractCrudService.ts
237
236
  import {
238
237
  fileReferenceSchema,
239
- fileTypeSchema,
240
- gitCommitIconSchema
238
+ gitCommitIconSchema,
239
+ objectTypeSchema
241
240
  } from "@elek-io/shared";
242
241
  import { orderBy, remove } from "lodash-es";
243
242
  var AbstractCrudService = class {
@@ -309,19 +308,19 @@ var AbstractCrudService = class {
309
308
  */
310
309
  async listReferences(type, projectId, collectionId) {
311
310
  switch (type) {
312
- case fileTypeSchema.Enum.asset:
311
+ case objectTypeSchema.Enum.asset:
313
312
  if (!projectId) {
314
313
  throw new RequiredParameterMissingError("projectId");
315
314
  }
316
315
  return this.getFileReferences(pathTo.lfs(projectId));
317
- case fileTypeSchema.Enum.project:
316
+ case objectTypeSchema.Enum.project:
318
317
  return this.getFolderReferences(pathTo.projects);
319
- case fileTypeSchema.Enum.collection:
318
+ case objectTypeSchema.Enum.collection:
320
319
  if (!projectId) {
321
320
  throw new RequiredParameterMissingError("projectId");
322
321
  }
323
322
  return this.getFolderReferences(pathTo.collections(projectId));
324
- case fileTypeSchema.Enum.entry:
323
+ case objectTypeSchema.Enum.entry:
325
324
  if (!projectId) {
326
325
  throw new RequiredParameterMissingError("projectId");
327
326
  }
@@ -331,11 +330,11 @@ var AbstractCrudService = class {
331
330
  return this.getFileReferences(
332
331
  pathTo.collection(projectId, collectionId)
333
332
  );
334
- case fileTypeSchema.Enum.value:
333
+ case objectTypeSchema.Enum.sharedValue:
335
334
  if (!projectId) {
336
335
  throw new RequiredParameterMissingError("projectId");
337
336
  }
338
- return this.getFileReferences(pathTo.values(projectId));
337
+ return this.getFileReferences(pathTo.sharedValues(projectId));
339
338
  default:
340
339
  throw new Error(`Trying to list files of unsupported type "${type}"`);
341
340
  }
@@ -1033,7 +1032,7 @@ var AssetService = class extends AbstractCrudService {
1033
1032
  );
1034
1033
  const assetFile = {
1035
1034
  ...props,
1036
- fileType: "asset",
1035
+ objectType: "asset",
1037
1036
  id,
1038
1037
  created: currentTimestamp(),
1039
1038
  extension: fileType.extension,
@@ -1142,7 +1141,7 @@ var AssetService = class extends AbstractCrudService {
1142
1141
  async list(props) {
1143
1142
  listAssetsSchema.parse(props);
1144
1143
  const assetReferences = await this.listReferences(
1145
- fileTypeSchema2.Enum.asset,
1144
+ objectTypeSchema2.Enum.asset,
1146
1145
  props.projectId
1147
1146
  );
1148
1147
  const list = await returnResolved(
@@ -1168,7 +1167,7 @@ var AssetService = class extends AbstractCrudService {
1168
1167
  }
1169
1168
  async count(props) {
1170
1169
  countAssetsSchema.parse(props);
1171
- const count = (await this.listReferences(fileTypeSchema2.Enum.asset, props.projectId)).length;
1170
+ const count = (await this.listReferences(objectTypeSchema2.Enum.asset, props.projectId)).length;
1172
1171
  return count;
1173
1172
  }
1174
1173
  /**
@@ -1216,14 +1215,14 @@ var AssetService = class extends AbstractCrudService {
1216
1215
  const fileBuffer = await Fs3.readFile(filePath);
1217
1216
  if (IsSvg(fileBuffer.toString()) === true) {
1218
1217
  return {
1219
- extension: supportedExtensionSchema.Enum.svg,
1220
- mimeType: supportedMimeTypeSchema.Enum["image/svg+xml"]
1218
+ extension: supportedAssetExtensionSchema.Enum.svg,
1219
+ mimeType: supportedAssetMimeTypeSchema.Enum["image/svg+xml"]
1221
1220
  };
1222
1221
  }
1223
1222
  }
1224
1223
  const { fileTypeFromFile } = await import("file-type");
1225
1224
  const fileType = await fileTypeFromFile(filePath);
1226
- const result = supportedFileTypeSchema.parse({
1225
+ const result = supportedAssetTypeSchema.parse({
1227
1226
  extension: fileType?.ext,
1228
1227
  mimeType: fileType?.mime
1229
1228
  });
@@ -1238,8 +1237,8 @@ import {
1238
1237
  createCollectionSchema,
1239
1238
  currentTimestamp as currentTimestamp2,
1240
1239
  deleteCollectionSchema,
1241
- fileTypeSchema as fileTypeSchema3,
1242
1240
  listCollectionsSchema,
1241
+ objectTypeSchema as objectTypeSchema3,
1243
1242
  readCollectionSchema,
1244
1243
  serviceTypeSchema as serviceTypeSchema4,
1245
1244
  slug,
@@ -1267,7 +1266,7 @@ var CollectionService = class extends AbstractCrudService {
1267
1266
  );
1268
1267
  const collectionFile = {
1269
1268
  ...props,
1270
- fileType: "collection",
1269
+ objectType: "collection",
1271
1270
  id,
1272
1271
  slug: {
1273
1272
  singular: slug(props.slug.singular),
@@ -1347,7 +1346,7 @@ var CollectionService = class extends AbstractCrudService {
1347
1346
  async list(props) {
1348
1347
  listCollectionsSchema.parse(props);
1349
1348
  const references = await this.listReferences(
1350
- fileTypeSchema3.Enum.collection,
1349
+ objectTypeSchema3.Enum.collection,
1351
1350
  props.projectId
1352
1351
  );
1353
1352
  const list = await returnResolved(
@@ -1368,7 +1367,10 @@ var CollectionService = class extends AbstractCrudService {
1368
1367
  }
1369
1368
  async count(props) {
1370
1369
  countCollectionsSchema.parse(props);
1371
- const count = (await this.listReferences(fileTypeSchema3.Enum.collection, props.projectId)).length;
1370
+ const count = (await this.listReferences(
1371
+ objectTypeSchema3.Enum.collection,
1372
+ props.projectId
1373
+ )).length;
1372
1374
  return count;
1373
1375
  }
1374
1376
  /**
@@ -1381,15 +1383,16 @@ var CollectionService = class extends AbstractCrudService {
1381
1383
 
1382
1384
  // src/service/EntryService.ts
1383
1385
  import {
1386
+ ValueTypeSchema as ValueTypeSchema2,
1384
1387
  countEntriesSchema,
1385
1388
  createEntrySchema,
1386
1389
  currentTimestamp as currentTimestamp4,
1387
1390
  deleteEntrySchema,
1388
1391
  entryFileSchema,
1389
1392
  entrySchema,
1390
- fileTypeSchema as fileTypeSchema5,
1391
- getValueSchemaFromDefinition as getValueSchemaFromDefinition2,
1393
+ getValueContentSchemaFromDefinition,
1392
1394
  listEntriesSchema,
1395
+ objectTypeSchema as objectTypeSchema5,
1393
1396
  readEntrySchema,
1394
1397
  serviceTypeSchema as serviceTypeSchema6,
1395
1398
  updateEntrySchema,
@@ -1397,100 +1400,109 @@ import {
1397
1400
  } from "@elek-io/shared";
1398
1401
  import Fs6 from "fs-extra";
1399
1402
 
1400
- // src/service/ValueService.ts
1403
+ // src/service/SharedValueService.ts
1401
1404
  import {
1405
+ ValueTypeSchema,
1402
1406
  countValuesSchema,
1403
- createValueSchema,
1407
+ createSharedValueSchema,
1404
1408
  currentTimestamp as currentTimestamp3,
1405
- deleteValueSchema,
1406
- fileTypeSchema as fileTypeSchema4,
1407
- getValueSchemaFromDefinition,
1408
- listValuesSchema,
1409
- readValueSchema,
1409
+ deleteSharedValueSchema,
1410
+ listSharedValuesSchema,
1411
+ objectTypeSchema as objectTypeSchema4,
1412
+ readSharedValueSchema,
1410
1413
  serviceTypeSchema as serviceTypeSchema5,
1411
- updateValueSchema,
1414
+ sharedValueFileSchema,
1415
+ sharedValueSchema,
1416
+ updateSharedValueSchema,
1412
1417
  uuid as uuid4,
1413
- validateValueSchema,
1414
- valueFileSchema
1418
+ z
1415
1419
  } from "@elek-io/shared";
1416
1420
  import Fs5 from "fs-extra";
1417
- var ValueService = class extends AbstractCrudService {
1421
+ var SharedValueService = class extends AbstractCrudService {
1418
1422
  constructor(options, jsonFileService, gitService, assetService) {
1419
1423
  super(serviceTypeSchema5.Enum.Value, options);
1420
1424
  this.jsonFileService = jsonFileService;
1421
1425
  this.gitService = gitService;
1422
1426
  }
1423
1427
  /**
1424
- * Creates a new Value
1428
+ * Creates a new shared Value
1425
1429
  */
1426
1430
  async create(props) {
1427
- createValueSchema.parse(props);
1431
+ createSharedValueSchema.parse(props);
1428
1432
  const id = uuid4();
1429
1433
  const projectPath = pathTo.project(props.projectId);
1430
- const valueFilePath = pathTo.valueFile(
1434
+ const sharedValueFilePath = pathTo.sharedValueFile(
1431
1435
  props.projectId,
1432
1436
  id,
1433
1437
  props.language
1434
1438
  );
1435
- const valueFile = {
1439
+ const sharedValueFile = {
1436
1440
  ...props,
1437
- fileType: "value",
1441
+ objectType: "sharedValue",
1438
1442
  id,
1439
1443
  created: currentTimestamp3()
1440
1444
  };
1445
+ this.validate(sharedValueFile);
1441
1446
  await this.jsonFileService.create(
1442
- valueFile,
1443
- valueFilePath,
1444
- valueFileSchema
1447
+ sharedValueFile,
1448
+ sharedValueFilePath,
1449
+ sharedValueFileSchema
1445
1450
  );
1446
- await this.gitService.add(projectPath, [valueFilePath]);
1451
+ await this.gitService.add(projectPath, [sharedValueFilePath]);
1447
1452
  await this.gitService.commit(projectPath, this.gitMessage.create);
1448
- return valueFile;
1453
+ return sharedValueFile;
1449
1454
  }
1450
1455
  /**
1451
- * Returns a Value by ID and language
1456
+ * Returns a shared Value by ID and language
1452
1457
  */
1453
1458
  async read(props) {
1454
- readValueSchema.parse(props);
1455
- const valueFile = await this.jsonFileService.read(
1456
- pathTo.valueFile(props.projectId, props.id, props.language),
1457
- valueFileSchema
1459
+ readSharedValueSchema.parse(props);
1460
+ const sharedValueFile = await this.jsonFileService.read(
1461
+ pathTo.sharedValueFile(
1462
+ props.projectId,
1463
+ props.id,
1464
+ props.language
1465
+ ),
1466
+ sharedValueFileSchema
1458
1467
  );
1459
- return valueFile;
1468
+ return sharedValueFile;
1460
1469
  }
1461
1470
  /**
1462
- * Updates given Value
1471
+ * Updates given shared Values content
1472
+ *
1473
+ * The valueType cannot be changed after creating the shared Value
1463
1474
  */
1464
1475
  async update(props) {
1465
- updateValueSchema.parse(props);
1476
+ updateSharedValueSchema.parse(props);
1466
1477
  const projectPath = pathTo.project(props.projectId);
1467
- const valueFilePath = pathTo.valueFile(
1478
+ const sharedValueFilePath = pathTo.sharedValueFile(
1468
1479
  props.projectId,
1469
1480
  props.id,
1470
1481
  props.language
1471
1482
  );
1472
- const prevValueFile = await this.read(props);
1473
- const valueFile = {
1474
- ...prevValueFile,
1475
- ...props,
1483
+ const prevSharedValueFile = await this.read(props);
1484
+ const sharedValueFile = {
1485
+ ...prevSharedValueFile,
1486
+ content: props.content,
1476
1487
  updated: currentTimestamp3()
1477
1488
  };
1489
+ this.validate(sharedValueFile);
1478
1490
  await this.jsonFileService.update(
1479
- valueFile,
1480
- valueFilePath,
1481
- valueFileSchema
1491
+ sharedValueFile,
1492
+ sharedValueFilePath,
1493
+ sharedValueFileSchema
1482
1494
  );
1483
- await this.gitService.add(projectPath, [valueFilePath]);
1495
+ await this.gitService.add(projectPath, [sharedValueFilePath]);
1484
1496
  await this.gitService.commit(projectPath, this.gitMessage.update);
1485
- return valueFile;
1497
+ return sharedValueFile;
1486
1498
  }
1487
1499
  /**
1488
- * Deletes given Value
1500
+ * Deletes given shared Value
1489
1501
  */
1490
1502
  async delete(props) {
1491
- deleteValueSchema.parse(props);
1503
+ deleteSharedValueSchema.parse(props);
1492
1504
  const projectPath = pathTo.project(props.projectId);
1493
- const valueFilePath = pathTo.valueFile(
1505
+ const valueFilePath = pathTo.sharedValueFile(
1494
1506
  props.projectId,
1495
1507
  props.id,
1496
1508
  props.language
@@ -1500,9 +1512,9 @@ var ValueService = class extends AbstractCrudService {
1500
1512
  await this.gitService.commit(projectPath, this.gitMessage.delete);
1501
1513
  }
1502
1514
  async list(props) {
1503
- listValuesSchema.parse(props);
1515
+ listSharedValuesSchema.parse(props);
1504
1516
  const references = await this.listReferences(
1505
- fileTypeSchema4.Enum.value,
1517
+ objectTypeSchema4.Enum.sharedValue,
1506
1518
  props.projectId
1507
1519
  );
1508
1520
  const list = await returnResolved(
@@ -1527,37 +1539,52 @@ var ValueService = class extends AbstractCrudService {
1527
1539
  }
1528
1540
  async count(props) {
1529
1541
  countValuesSchema.parse(props);
1530
- const count = (await this.listReferences(fileTypeSchema4.Enum.value, props.projectId)).length;
1542
+ const count = (await this.listReferences(
1543
+ objectTypeSchema4.Enum.sharedValue,
1544
+ props.projectId
1545
+ )).length;
1531
1546
  return count;
1532
1547
  }
1533
1548
  /**
1534
- * Checks if given object is of type Value
1549
+ * Checks if given object is a shared Value
1535
1550
  */
1536
- isValue(obj) {
1537
- return valueFileSchema.safeParse(obj).success;
1551
+ isSharedValue(obj) {
1552
+ return sharedValueSchema.safeParse(obj).success;
1538
1553
  }
1539
1554
  /**
1540
- * Reads the given Value from disk and validates it against the ValueDefinition
1555
+ * Reads the given shared Values content based on it's ValueType
1541
1556
  */
1542
- async validate(props) {
1543
- validateValueSchema.parse(props);
1544
- const value = await this.read(props);
1545
- const valueSchema = getValueSchemaFromDefinition(props.definition);
1546
- return valueSchema.safeParse(value.content);
1557
+ validate(sharedValue) {
1558
+ switch (sharedValue.valueType) {
1559
+ case ValueTypeSchema.Enum.boolean:
1560
+ z.boolean().parse(sharedValue.content);
1561
+ break;
1562
+ case ValueTypeSchema.Enum.number:
1563
+ z.number().parse(sharedValue.content);
1564
+ break;
1565
+ case ValueTypeSchema.Enum.string:
1566
+ z.string().parse(sharedValue.content);
1567
+ break;
1568
+ default:
1569
+ throw new Error(
1570
+ `Error validating content of unsupported shared Value with ValueType "${sharedValue.valueType}"`
1571
+ );
1572
+ }
1547
1573
  }
1548
1574
  };
1549
1575
 
1550
1576
  // src/service/EntryService.ts
1551
1577
  var EntryService = class extends AbstractCrudService {
1552
- constructor(options, jsonFileService, gitService, collectionService, valueService) {
1578
+ constructor(options, jsonFileService, gitService, collectionService, assetService, sharedValueService) {
1553
1579
  super(serviceTypeSchema6.Enum.Entry, options);
1554
1580
  this.jsonFileService = jsonFileService;
1555
1581
  this.gitService = gitService;
1556
1582
  this.collectionService = collectionService;
1557
- this.valueService = valueService;
1583
+ this.assetService = assetService;
1584
+ this.sharedValueService = sharedValueService;
1558
1585
  }
1559
1586
  /**
1560
- * Creates a new Entry
1587
+ * Creates a new Entry for given Collection
1561
1588
  */
1562
1589
  async create(props) {
1563
1590
  createEntrySchema.parse(props);
@@ -1573,19 +1600,22 @@ var EntryService = class extends AbstractCrudService {
1573
1600
  projectId: props.projectId,
1574
1601
  id: props.collectionId
1575
1602
  });
1576
- await this.validateValueReferences(
1577
- props.projectId,
1578
- props.collectionId,
1579
- props.valueReferences,
1580
- collection.valueDefinitions
1581
- );
1582
1603
  const entryFile = {
1583
- fileType: "entry",
1604
+ objectType: "entry",
1584
1605
  id,
1585
1606
  language: props.language,
1586
- valueReferences: props.valueReferences,
1607
+ values: props.values,
1587
1608
  created: currentTimestamp4()
1588
1609
  };
1610
+ const entry = await this.toEntry({
1611
+ projectId: props.projectId,
1612
+ entryFile
1613
+ });
1614
+ this.validateValues({
1615
+ collectionId: props.collectionId,
1616
+ valueDefinitions: collection.valueDefinitions,
1617
+ values: entry.values
1618
+ });
1589
1619
  await this.jsonFileService.create(
1590
1620
  entryFile,
1591
1621
  entryFilePath,
@@ -1593,10 +1623,10 @@ var EntryService = class extends AbstractCrudService {
1593
1623
  );
1594
1624
  await this.gitService.add(projectPath, [entryFilePath]);
1595
1625
  await this.gitService.commit(projectPath, this.gitMessage.create);
1596
- return entryFile;
1626
+ return entry;
1597
1627
  }
1598
1628
  /**
1599
- * Returns an Entry by ID and language
1629
+ * Returns an Entry from given Collection by ID and language
1600
1630
  */
1601
1631
  async read(props) {
1602
1632
  readEntrySchema.parse(props);
@@ -1609,10 +1639,10 @@ var EntryService = class extends AbstractCrudService {
1609
1639
  ),
1610
1640
  entryFileSchema
1611
1641
  );
1612
- return entryFile;
1642
+ return await this.toEntry({ projectId: props.projectId, entryFile });
1613
1643
  }
1614
1644
  /**
1615
- * Updates Entry with given ValueReferences
1645
+ * Updates an Entry of given Collection with new Values and shared Values
1616
1646
  */
1617
1647
  async update(props) {
1618
1648
  updateEntrySchema.parse(props);
@@ -1627,12 +1657,6 @@ var EntryService = class extends AbstractCrudService {
1627
1657
  projectId: props.projectId,
1628
1658
  id: props.collectionId
1629
1659
  });
1630
- await this.validateValueReferences(
1631
- props.projectId,
1632
- props.collectionId,
1633
- props.valueReferences,
1634
- collection.valueDefinitions
1635
- );
1636
1660
  const prevEntryFile = await this.read({
1637
1661
  projectId: props.projectId,
1638
1662
  collectionId: props.collectionId,
@@ -1641,9 +1665,18 @@ var EntryService = class extends AbstractCrudService {
1641
1665
  });
1642
1666
  const entryFile = {
1643
1667
  ...prevEntryFile,
1644
- valueReferences: props.valueReferences,
1668
+ values: props.values,
1645
1669
  updated: currentTimestamp4()
1646
1670
  };
1671
+ const entry = await this.toEntry({
1672
+ projectId: props.projectId,
1673
+ entryFile
1674
+ });
1675
+ this.validateValues({
1676
+ collectionId: props.collectionId,
1677
+ valueDefinitions: collection.valueDefinitions,
1678
+ values: entry.values
1679
+ });
1647
1680
  await this.jsonFileService.update(
1648
1681
  entryFile,
1649
1682
  entryFilePath,
@@ -1651,10 +1684,10 @@ var EntryService = class extends AbstractCrudService {
1651
1684
  );
1652
1685
  await this.gitService.add(projectPath, [entryFilePath]);
1653
1686
  await this.gitService.commit(projectPath, this.gitMessage.update);
1654
- return entryFile;
1687
+ return entry;
1655
1688
  }
1656
1689
  /**
1657
- * Deletes given Entry
1690
+ * Deletes given Entry from it's Collection
1658
1691
  */
1659
1692
  async delete(props) {
1660
1693
  deleteEntrySchema.parse(props);
@@ -1672,7 +1705,7 @@ var EntryService = class extends AbstractCrudService {
1672
1705
  async list(props) {
1673
1706
  listEntriesSchema.parse(props);
1674
1707
  const references = await this.listReferences(
1675
- fileTypeSchema5.Enum.entry,
1708
+ objectTypeSchema5.Enum.entry,
1676
1709
  props.projectId,
1677
1710
  props.collectionId
1678
1711
  );
@@ -1700,45 +1733,147 @@ var EntryService = class extends AbstractCrudService {
1700
1733
  async count(props) {
1701
1734
  countEntriesSchema.parse(props);
1702
1735
  return (await this.listReferences(
1703
- fileTypeSchema5.Enum.entry,
1736
+ objectTypeSchema5.Enum.entry,
1704
1737
  props.projectId,
1705
1738
  props.collectionId
1706
1739
  )).length;
1707
1740
  }
1708
1741
  /**
1709
- * Checks if given object of Collection, CollectionItem,
1710
- * Field, Project or Asset is of type CollectionItem
1742
+ * Checks if given object is of type Entry
1711
1743
  */
1712
1744
  isEntry(obj) {
1713
1745
  return entrySchema.safeParse(obj).success;
1714
1746
  }
1715
1747
  /**
1716
- * Validates referenced Values against the Collections definition
1717
- *
1718
- * @todo should probably return all errors occurring during parsing instead of throwing
1748
+ * Returns a Value definition by ID
1719
1749
  */
1720
- async validateValueReferences(projectId, collectionId, valueReferences, valueDefinitions) {
1721
- await Promise.all(
1722
- valueReferences.map(async (reference) => {
1723
- const definition = valueDefinitions.find((def) => {
1724
- if (def.id === reference.definitionId) {
1725
- return true;
1726
- }
1727
- return false;
1750
+ getValueDefinitionById(props) {
1751
+ const definition = props.valueDefinitions.find((def) => {
1752
+ if (def.id === props.id) {
1753
+ return true;
1754
+ }
1755
+ return false;
1756
+ });
1757
+ if (!definition) {
1758
+ throw new Error(
1759
+ `No definition with ID "${props.id}" found in Collection "${props.collectionId}" for given Value reference`
1760
+ );
1761
+ }
1762
+ return definition;
1763
+ }
1764
+ /**
1765
+ * Validates given Values against it's Collections definitions
1766
+ */
1767
+ validateValues(props) {
1768
+ props.values.map((value) => {
1769
+ const definition = this.getValueDefinitionById({
1770
+ collectionId: props.collectionId,
1771
+ valueDefinitions: props.valueDefinitions,
1772
+ id: value.definitionId
1773
+ });
1774
+ const schema = getValueContentSchemaFromDefinition(definition);
1775
+ try {
1776
+ schema.parse(value.content);
1777
+ } catch (error) {
1778
+ console.log("Definition:", definition);
1779
+ console.log("Value:", value);
1780
+ throw error;
1781
+ }
1782
+ });
1783
+ }
1784
+ /**
1785
+ * Validates given shared Value references against it's Collections definitions
1786
+ */
1787
+ // private validateResolvedSharedValues(props: {
1788
+ // collectionId: string;
1789
+ // valueDefinitions: ValueDefinition[];
1790
+ // resolvedSharedValues: ResolvedSharedValueReference[];
1791
+ // }) {
1792
+ // props.resolvedSharedValues.map((value) => {
1793
+ // const definition = this.getValueDefinitionById({
1794
+ // collectionId: props.collectionId,
1795
+ // valueDefinitions: props.valueDefinitions,
1796
+ // id: value.definitionId,
1797
+ // });
1798
+ // const schema = getValueSchemaFromDefinition(definition);
1799
+ // schema.parse(value.resolved.content);
1800
+ // });
1801
+ // }
1802
+ async resolveValueContentReference(props) {
1803
+ switch (props.valueContentReference.referenceObjectType) {
1804
+ case objectTypeSchema5.Enum.asset:
1805
+ return this.resolveValueContentReferenceToAsset({
1806
+ projectId: props.projectId,
1807
+ valueContentReferenceToAsset: props.valueContentReference
1728
1808
  });
1729
- if (!definition) {
1730
- throw new Error(
1731
- `No definition with ID "${reference.definitionId}" found in Collection "${collectionId}" for given Value reference`
1732
- );
1733
- }
1734
- const schema = getValueSchemaFromDefinition2(definition);
1735
- const value = await this.valueService.read({
1736
- ...reference.references,
1737
- projectId
1809
+ case objectTypeSchema5.Enum.sharedValue:
1810
+ return this.resolveValueContentReferenceToSharedValue({
1811
+ projectId: props.projectId,
1812
+ valueContentReferenceToSharedValue: props.valueContentReference
1738
1813
  });
1739
- schema.parse(value.content);
1814
+ default:
1815
+ throw new Error(
1816
+ // @ts-ignore
1817
+ `Tried to resolve unsupported Value reference "${props.valueContentReference.referenceObjectType}"`
1818
+ );
1819
+ }
1820
+ }
1821
+ async resolveValueContentReferenceToAsset(props) {
1822
+ const resolvedReferences = await Promise.all(
1823
+ props.valueContentReferenceToAsset.references.map(async (reference) => {
1824
+ const resolvedAsset = await this.assetService.read({
1825
+ projectId: props.projectId,
1826
+ id: reference.id,
1827
+ language: reference.language
1828
+ });
1829
+ return {
1830
+ ...reference,
1831
+ resolved: resolvedAsset
1832
+ };
1740
1833
  })
1741
1834
  );
1835
+ return {
1836
+ ...props.valueContentReferenceToAsset,
1837
+ references: resolvedReferences
1838
+ };
1839
+ }
1840
+ async resolveValueContentReferenceToSharedValue(props) {
1841
+ const resolvedSharedValue = await this.sharedValueService.read({
1842
+ projectId: props.projectId,
1843
+ id: props.valueContentReferenceToSharedValue.references.id,
1844
+ language: props.valueContentReferenceToSharedValue.references.language
1845
+ });
1846
+ return {
1847
+ ...props.valueContentReferenceToSharedValue,
1848
+ references: {
1849
+ ...props.valueContentReferenceToSharedValue.references,
1850
+ resolved: resolvedSharedValue
1851
+ }
1852
+ };
1853
+ }
1854
+ /**
1855
+ * Creates an Entry from given EntryFile by resolving it's Values
1856
+ */
1857
+ async toEntry(props) {
1858
+ const entry = {
1859
+ ...props.entryFile,
1860
+ values: await Promise.all(
1861
+ props.entryFile.values.map(async (value) => {
1862
+ if (value.valueType === ValueTypeSchema2.Enum.reference) {
1863
+ const resolvedValueContentReference = await this.resolveValueContentReference({
1864
+ projectId: props.projectId,
1865
+ valueContentReference: value.content
1866
+ });
1867
+ return {
1868
+ ...value,
1869
+ content: resolvedValueContentReference
1870
+ };
1871
+ }
1872
+ return value;
1873
+ })
1874
+ )
1875
+ };
1876
+ return entry;
1742
1877
  }
1743
1878
  };
1744
1879
 
@@ -1747,9 +1882,9 @@ import {
1747
1882
  createProjectSchema,
1748
1883
  currentTimestamp as currentTimestamp5,
1749
1884
  deleteProjectSchema,
1750
- fileTypeSchema as fileTypeSchema6,
1751
1885
  gitCommitIconSchema as gitCommitIconSchema2,
1752
1886
  listProjectsSchema,
1887
+ objectTypeSchema as objectTypeSchema6,
1753
1888
  projectFileSchema,
1754
1889
  projectFolderSchema as projectFolderSchema2,
1755
1890
  readProjectSchema,
@@ -1789,7 +1924,7 @@ var SearchService = class extends AbstractCrudService {
1789
1924
  * @param project Project to search in
1790
1925
  * @param query Query to search for
1791
1926
  */
1792
- async search(projectId, query, fileType) {
1927
+ async search(projectId, query, objectType) {
1793
1928
  const results = [];
1794
1929
  const normalizedQuery = query.trim();
1795
1930
  if (normalizedQuery === "") {
@@ -1802,7 +1937,7 @@ var SearchService = class extends AbstractCrudService {
1802
1937
  id: file.id,
1803
1938
  language: file.language,
1804
1939
  name: file.name,
1805
- type: file.fileType,
1940
+ type: file.objectType,
1806
1941
  matches: []
1807
1942
  };
1808
1943
  for (const [key, value] of Object.entries(file)) {
@@ -1842,7 +1977,7 @@ var SearchService = class extends AbstractCrudService {
1842
1977
 
1843
1978
  // src/service/ProjectService.ts
1844
1979
  var ProjectService = class extends AbstractCrudService {
1845
- constructor(options, jsonFileService, userService, gitService, searchService, assetService, collectionService, entryService, valueService) {
1980
+ constructor(options, jsonFileService, userService, gitService, searchService, assetService, collectionService, entryService) {
1846
1981
  super(serviceTypeSchema8.Enum.Project, options);
1847
1982
  this.jsonFileService = jsonFileService;
1848
1983
  this.userService = userService;
@@ -1851,7 +1986,6 @@ var ProjectService = class extends AbstractCrudService {
1851
1986
  this.assetService = assetService;
1852
1987
  this.collectionService = collectionService;
1853
1988
  this.entryService = entryService;
1854
- this.valueService = valueService;
1855
1989
  }
1856
1990
  /**
1857
1991
  * Creates a new Project
@@ -1871,7 +2005,7 @@ var ProjectService = class extends AbstractCrudService {
1871
2005
  };
1872
2006
  const projectFile = {
1873
2007
  ...props,
1874
- fileType: "project",
2008
+ objectType: "project",
1875
2009
  id,
1876
2010
  description: props.description || "",
1877
2011
  settings: Object.assign({}, defaultSettings, props.settings),
@@ -2016,7 +2150,7 @@ var ProjectService = class extends AbstractCrudService {
2016
2150
  if (props) {
2017
2151
  listProjectsSchema.parse(props);
2018
2152
  }
2019
- const references = await this.listReferences(fileTypeSchema6.Enum.project);
2153
+ const references = await this.listReferences(objectTypeSchema6.Enum.project);
2020
2154
  const list = await returnResolved(
2021
2155
  references.map((reference) => {
2022
2156
  return this.read({ id: reference.id });
@@ -2031,7 +2165,7 @@ var ProjectService = class extends AbstractCrudService {
2031
2165
  );
2032
2166
  }
2033
2167
  async count() {
2034
- return (await this.listReferences(fileTypeSchema6.Enum.project)).length;
2168
+ return (await this.listReferences(objectTypeSchema6.Enum.project)).length;
2035
2169
  }
2036
2170
  /**
2037
2171
  * Search all models inside the project for given query
@@ -2052,6 +2186,7 @@ var ProjectService = class extends AbstractCrudService {
2052
2186
  /**
2053
2187
  * Exports given Project to JSON
2054
2188
  *
2189
+ * @todo do not read everything before writing to disk -> stream into file given via props
2055
2190
  * @todo performance tests
2056
2191
  * @todo add progress callback
2057
2192
  */
@@ -2066,26 +2201,9 @@ var ProjectService = class extends AbstractCrudService {
2066
2201
  collectionId: collection.id,
2067
2202
  limit: 0
2068
2203
  })).list;
2069
- const entryExport = await Promise.all(
2070
- entries.map(async (entry) => {
2071
- const valueExport = await Promise.all(
2072
- entry.valueReferences.map(async (valueReference) => {
2073
- return this.valueService.read({
2074
- projectId,
2075
- id: valueReference.references.id,
2076
- language: valueReference.references.language
2077
- });
2078
- })
2079
- );
2080
- return {
2081
- ...entry,
2082
- values: valueExport
2083
- };
2084
- })
2085
- );
2086
2204
  return {
2087
2205
  ...collection,
2088
- entries: entryExport
2206
+ entries
2089
2207
  };
2090
2208
  })
2091
2209
  );
@@ -2136,12 +2254,9 @@ var ProjectService = class extends AbstractCrudService {
2136
2254
  // src/index.ts
2137
2255
  var ElekIoCore = class {
2138
2256
  constructor(props) {
2139
- if (props) {
2140
- constructorElekIoCoreSchema.parse(props);
2141
- }
2142
- const environment = environmentSchema.parse(process.env.NODE_ENV);
2257
+ const parsedProps = constructorElekIoCoreSchema.parse(props);
2143
2258
  const defaults = {
2144
- environment,
2259
+ environment: "production",
2145
2260
  version: "0.0.0",
2146
2261
  file: {
2147
2262
  json: {
@@ -2149,7 +2264,7 @@ var ElekIoCore = class {
2149
2264
  }
2150
2265
  }
2151
2266
  };
2152
- this.options = Object.assign({}, defaults, props);
2267
+ this.options = Object.assign({}, defaults, parsedProps);
2153
2268
  this.jsonFileService = new JsonFileService(this.options);
2154
2269
  this.userService = new UserService(this.jsonFileService);
2155
2270
  this.gitService = new GitService2(this.options, this.userService);
@@ -2158,23 +2273,24 @@ var ElekIoCore = class {
2158
2273
  this.jsonFileService,
2159
2274
  this.gitService
2160
2275
  );
2161
- this.valueService = new ValueService(
2276
+ this.collectionService = new CollectionService(
2162
2277
  this.options,
2163
2278
  this.jsonFileService,
2164
- this.gitService,
2165
- this.assetService
2279
+ this.gitService
2166
2280
  );
2167
- this.collectionService = new CollectionService(
2281
+ this.sharedValueService = new SharedValueService(
2168
2282
  this.options,
2169
2283
  this.jsonFileService,
2170
- this.gitService
2284
+ this.gitService,
2285
+ this.assetService
2171
2286
  );
2172
2287
  this.entryService = new EntryService(
2173
2288
  this.options,
2174
2289
  this.jsonFileService,
2175
2290
  this.gitService,
2176
2291
  this.collectionService,
2177
- this.valueService
2292
+ this.assetService,
2293
+ this.sharedValueService
2178
2294
  );
2179
2295
  this.searchService = new SearchService(
2180
2296
  this.options,
@@ -2189,13 +2305,15 @@ var ElekIoCore = class {
2189
2305
  this.searchService,
2190
2306
  this.assetService,
2191
2307
  this.collectionService,
2192
- this.entryService,
2193
- this.valueService
2308
+ this.entryService
2194
2309
  );
2195
- if (environment !== "production") {
2196
- console.info(`Initializing inside an "${environment}" environment`, {
2197
- options: this.options
2198
- });
2310
+ if (this.options.environment !== "production") {
2311
+ console.info(
2312
+ `Initializing inside an "${this.options.environment}" environment`,
2313
+ {
2314
+ ...this.options
2315
+ }
2316
+ );
2199
2317
  }
2200
2318
  Fs8.mkdirpSync(pathTo.projects);
2201
2319
  Fs8.mkdirpSync(pathTo.tmp);
@@ -2240,8 +2358,8 @@ var ElekIoCore = class {
2240
2358
  /**
2241
2359
  * CRUD methods to work with Values
2242
2360
  */
2243
- get values() {
2244
- return this.valueService;
2361
+ get sharedValues() {
2362
+ return this.sharedValueService;
2245
2363
  }
2246
2364
  };
2247
2365
  export {