@eventcatalog/sdk 2.8.1 → 2.8.3

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.
@@ -575,6 +575,42 @@ var addEntityToService = (directory) => async (id, entity, version) => {
575
575
  await rmServiceById(directory)(id, version);
576
576
  await writeService(pathToResource)(service, { format: extension === ".md" ? "md" : "mdx" });
577
577
  };
578
+ var addDataStoreToService = (directory) => async (id, operation, dataStore, version) => {
579
+ let service = await getService(directory)(id, version);
580
+ const servicePath = await getResourcePath(directory, id, version);
581
+ const extension = extname(servicePath?.fullPath || "");
582
+ if (operation === "writesTo") {
583
+ if (service.writesTo === void 0) {
584
+ service.writesTo = [];
585
+ }
586
+ for (let i = 0; i < service.writesTo.length; i++) {
587
+ if (service.writesTo[i].id === dataStore.id && service.writesTo[i].version === dataStore.version) {
588
+ return;
589
+ }
590
+ }
591
+ service.writesTo.push({ id: dataStore.id, version: dataStore.version });
592
+ } else if (operation === "readsFrom") {
593
+ if (service.readsFrom === void 0) {
594
+ service.readsFrom = [];
595
+ }
596
+ for (let i = 0; i < service.readsFrom.length; i++) {
597
+ if (service.readsFrom[i].id === dataStore.id && service.readsFrom[i].version === dataStore.version) {
598
+ return;
599
+ }
600
+ }
601
+ service.readsFrom.push({ id: dataStore.id, version: dataStore.version });
602
+ } else {
603
+ throw new Error(`Operation ${operation} is invalid, only 'writesTo' and 'readsFrom' are supported`);
604
+ }
605
+ const existingResource = await findFileById(directory, id, version);
606
+ if (!existingResource) {
607
+ throw new Error(`Cannot find service ${id} in the catalog`);
608
+ }
609
+ const path5 = existingResource.split(/[\\/]+services/)[0];
610
+ const pathToResource = join6(path5, "services");
611
+ await rmServiceById(directory)(id, version);
612
+ await writeService(pathToResource)(service, { format: extension === ".md" ? "md" : "mdx" });
613
+ };
578
614
 
579
615
  // src/domains.ts
580
616
  import fs6 from "fs/promises";
@@ -1028,6 +1064,17 @@ var writeContainerToService = (directory) => async (container, service, options
1028
1064
  await writeResource(directory, { ...container }, { ...options, path: pathForContainer, type: "container" });
1029
1065
  };
1030
1066
 
1067
+ // src/data-stores.ts
1068
+ var getDataStore = getContainer;
1069
+ var getDataStores = getContainers;
1070
+ var writeDataStore = writeContainer;
1071
+ var versionDataStore = versionContainer;
1072
+ var rmDataStore = rmContainer;
1073
+ var rmDataStoreById = rmContainerById;
1074
+ var dataStoreHasVersion = containerHasVersion;
1075
+ var addFileToDataStore = addFileToContainer;
1076
+ var writeDataStoreToService = writeContainerToService;
1077
+
1031
1078
  // src/index.ts
1032
1079
  var index_default = (path5) => {
1033
1080
  return {
@@ -1469,6 +1516,23 @@ var index_default = (path5) => {
1469
1516
  * ```
1470
1517
  */
1471
1518
  addEventToService: addMessageToService(join15(path5)),
1519
+ /**
1520
+ * Add a data store to a service by it's id.
1521
+ *
1522
+ * Optionally specify a version to add the data store to a specific version of the service.
1523
+ *
1524
+ * @example
1525
+ * ```ts
1526
+ * import utils from '@eventcatalog/utils';
1527
+ *
1528
+ * const { addDataStoreToService } = utils('/path/to/eventcatalog');
1529
+ *
1530
+ * // adds a new data store (orders-db) that the InventoryService will write to
1531
+ * await addDataStoreToService('InventoryService', 'writesTo', { id: 'orders-db', version: '2.0.0' });
1532
+ *
1533
+ * ```
1534
+ */
1535
+ addDataStoreToService: addDataStoreToService(join15(path5)),
1472
1536
  /**
1473
1537
  * Add a command to a service by it's id.
1474
1538
  *
@@ -1864,42 +1928,42 @@ var index_default = (path5) => {
1864
1928
  * @param options - Optional options to write the data store
1865
1929
  *
1866
1930
  */
1867
- writeDataStore: writeContainer(join15(path5, "containers")),
1931
+ writeDataStore: writeDataStore(join15(path5, "containers")),
1868
1932
  /**
1869
- * Returns a container from EventCatalog
1870
- * @param id - The id of the container to retrieve
1933
+ * Returns a data store from EventCatalog
1934
+ * @param id - The id of the data store to retrieve
1871
1935
  * @param version - Optional id of the version to get (supports semver)
1872
1936
  * @returns Container|Undefined
1873
1937
  */
1874
- getDataStore: getContainer(join15(path5)),
1938
+ getDataStore: getDataStore(join15(path5)),
1875
1939
  /**
1876
1940
  * Returns all data stores from EventCatalog
1877
1941
  * @param latestOnly - optional boolean, set to true to get only latest versions
1878
1942
  * @returns Container[]|Undefined
1879
1943
  */
1880
- getDataStores: getContainers(join15(path5)),
1944
+ getDataStores: getDataStores(join15(path5)),
1881
1945
  /**
1882
1946
  * Version a data store by its id
1883
1947
  * @param id - The id of the data store to version
1884
1948
  */
1885
- versionDataStore: versionContainer(join15(path5, "containers")),
1949
+ versionDataStore: versionDataStore(join15(path5, "containers")),
1886
1950
  /**
1887
1951
  * Remove a data store by its path
1888
1952
  * @param path - The path to the data store to remove
1889
1953
  */
1890
- rmDataStore: rmContainer(join15(path5, "containers")),
1954
+ rmDataStore: rmDataStore(join15(path5, "containers")),
1891
1955
  /**
1892
1956
  * Remove a data store by its id
1893
1957
  * @param id - The id of the data store to remove
1894
1958
  */
1895
- rmDataStoreById: rmContainerById(join15(path5)),
1959
+ rmDataStoreById: rmDataStoreById(join15(path5)),
1896
1960
  /**
1897
1961
  * Check to see if a data store version exists
1898
1962
  * @param id - The id of the data store
1899
1963
  * @param version - The version of the data store (supports semver)
1900
1964
  * @returns
1901
1965
  */
1902
- dataStoreHasVersion: containerHasVersion(join15(path5)),
1966
+ dataStoreHasVersion: dataStoreHasVersion(join15(path5)),
1903
1967
  /**
1904
1968
  * Adds a file to a data store by its id
1905
1969
  * @param id - The id of the data store to add the file to
@@ -1907,14 +1971,14 @@ var index_default = (path5) => {
1907
1971
  * @param version - Optional version of the data store to add the file to
1908
1972
  * @returns
1909
1973
  */
1910
- addFileToDataStore: addFileToContainer(join15(path5)),
1974
+ addFileToDataStore: addFileToDataStore(join15(path5)),
1911
1975
  /**
1912
1976
  * Writes a data store to a service by its id
1913
1977
  * @param dataStore - The data store to write
1914
1978
  * @param service - The service to write the data store to
1915
1979
  * @returns
1916
1980
  */
1917
- writeDataStoreToService: writeContainerToService(join15(path5))
1981
+ writeDataStoreToService: writeDataStoreToService(join15(path5))
1918
1982
  };
1919
1983
  };
1920
1984