@fraym/projections 0.14.1 → 0.15.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Becklyn Studios
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -15,33 +15,12 @@ There is a sandbox available at `http://projections:3000/delivery/graphql/sandbo
15
15
 
16
16
  You need to add the `Tenant-Id` header in order to use the graphQL Endpoint and the sandbox.
17
17
 
18
- ## CLI command
19
-
20
- Use the `projections` cli command to automatically apply your projection schemas to the projections service.
21
-
22
- Your type schemas have to match the glob you specify in the `PROJECTIONS_SCHEMA_GLOB` env variable (default: `./src/**/*.graphql`).
23
-
24
- Delivery API: You can specify the address (and port) of the projections service instance you use in the `PROJECTIONS_SERVER_ADDRESS` env variable (default: `127.0.0.1:9000`).
25
-
26
- Management API: You can specify the address (and port) of the projections service instance you use in the `PROJECTIONS_MANAGEMENT_SERVER_ADDRESS` env variable (default: `http://127.0.0.1`). You will also need to set the `PROJECTIONS_MANAGEMENT_API_TOKEN` variable. The value of that token has to match the token configured in the projections service.
27
-
28
- You might have a seperate permissions directory or file. As soon as your permissions schema enum is not part of the projections glob you can specify a `PERMISSIONS_SCHEMA_GLOB` env variable. It is empty by default but as soon as you provide it it will add the files in that glob to your projections schema, too.
29
-
30
- Use the `PROJECTIONS_NAMESPACE` env variable to restrict all migrations to your namespace. This is useful if multiple apps share the projections service. Note: You cannot name your projection or namespace by a `Fraym` prefix. This is a reserved prefix for fraym apps.
31
-
32
- You need to add a file that contains all built-in directives to your type schemas. The latest version of this file can be found [here](default.graphql).
33
-
34
18
  ### Config
35
19
 
36
20
  Use a `.env` file or env variables to configure cte clients and the command:
37
21
 
38
22
  ```env
39
23
  PROJECTIONS_SERVER_ADDRESS=127.0.0.1:9000
40
- PROJECTIONS_MANAGEMENT_SERVER_ADDRESS=http://127.0.0.1
41
- PROJECTIONS_MANAGEMENT_API_TOKEN=
42
- PROJECTIONS_SCHEMA_GLOB=./src/projections/*.graphql
43
- PERMISSIONS_SCHEMA_GLOB=
44
- PROJECTIONS_NAMESPACE=
45
24
  ```
46
25
 
47
26
  ## Env variable placeholders in migrations
@@ -69,50 +48,12 @@ type TestType {
69
48
 
70
49
  ## Usage
71
50
 
72
- ### Create the clients
73
-
74
- delivery client:
51
+ ### Create the client
75
52
 
76
53
  ```typescript
77
54
  const deliveryClient = await newDeliveryClient();
78
55
  ```
79
56
 
80
- management client:
81
-
82
- ```typescript
83
- const managementClient = await newManagementClient();
84
- ```
85
-
86
- ### Create one or multipe projections
87
-
88
- Projectionw are defined by schemas. A schema can contain more than one projection definition. See [SCHEMA.md](https://github.com/fraym/projections/blob/develop/SCHEMA.md) for a reference.
89
-
90
- ```typescript
91
- await managementClient.create("your schema here");
92
- ```
93
-
94
- ### Update one or multipe projections
95
-
96
- Projectionw are defined by schemas. A schema can contain more than one projection definition. See [SCHEMA.md](https://github.com/fraym/projections/blob/develop/SCHEMA.md) for a reference.
97
-
98
- ```typescript
99
- await managementClient.update("your schema here");
100
- ```
101
-
102
- ### Remove one or multipe projections
103
-
104
- The name of `YourProjection` has to equal your projection name in your schema (also in casing).
105
-
106
- ```typescript
107
- await managementClient.remove(["YourProjection"]);
108
- ```
109
-
110
- ### Get list of existing projections
111
-
112
- ```typescript
113
- const list = await managementClient.getAll();
114
- ```
115
-
116
57
  ### Authorization
117
58
 
118
59
  All delivery client functions make use of the `AuthData` object.
@@ -3,11 +3,5 @@ export interface DeliveryClientConfig {
3
3
  keepaliveInterval?: number;
4
4
  keepaliveTimeout?: number;
5
5
  }
6
- export interface ManagementClientConfig {
7
- serverAddress: string;
8
- apiToken: string;
9
- }
10
6
  export declare const getEnvDeliveryConfig: () => DeliveryClientConfig;
11
- export declare const getEnvManagementConfig: () => ManagementClientConfig;
12
7
  export declare const useDeliveryConfigDefaults: (config?: DeliveryClientConfig) => Required<DeliveryClientConfig>;
13
- export declare const useManagementConfigDefaults: (config?: ManagementClientConfig) => Required<ManagementClientConfig>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useManagementConfigDefaults = exports.useDeliveryConfigDefaults = exports.getEnvManagementConfig = exports.getEnvDeliveryConfig = void 0;
3
+ exports.useDeliveryConfigDefaults = exports.getEnvDeliveryConfig = void 0;
4
4
  const dotenv_1 = require("dotenv");
5
5
  const getEnvDeliveryConfig = () => {
6
6
  var _a;
@@ -23,15 +23,6 @@ const getEnvDeliveryConfig = () => {
23
23
  };
24
24
  };
25
25
  exports.getEnvDeliveryConfig = getEnvDeliveryConfig;
26
- const getEnvManagementConfig = () => {
27
- var _a, _b;
28
- (0, dotenv_1.config)();
29
- return {
30
- serverAddress: (_a = process.env.PROJECTIONS_MANAGEMENT_SERVER_ADDRESS) !== null && _a !== void 0 ? _a : "",
31
- apiToken: (_b = process.env.PROJECTIONS_MANAGEMENT_API_TOKEN) !== null && _b !== void 0 ? _b : "",
32
- };
33
- };
34
- exports.getEnvManagementConfig = getEnvManagementConfig;
35
26
  const useDeliveryConfigDefaults = (config) => {
36
27
  var _a, _b;
37
28
  if (!config) {
@@ -44,13 +35,3 @@ const useDeliveryConfigDefaults = (config) => {
44
35
  };
45
36
  };
46
37
  exports.useDeliveryConfigDefaults = useDeliveryConfigDefaults;
47
- const useManagementConfigDefaults = (config) => {
48
- if (!config) {
49
- config = (0, exports.getEnvManagementConfig)();
50
- }
51
- return {
52
- serverAddress: config.serverAddress,
53
- apiToken: config.apiToken,
54
- };
55
- };
56
- exports.useManagementConfigDefaults = useManagementConfigDefaults;
@@ -11,12 +11,16 @@ const isUpsertValidationResponse = (response) => {
11
11
  };
12
12
  exports.isUpsertValidationResponse = isUpsertValidationResponse;
13
13
  const upsertProjectionData = async (projection, auth, dataId, payload, eventMetadata, serviceClient) => {
14
+ const usedPayload = {};
15
+ for (const key in payload) {
16
+ usedPayload[key] = JSON.stringify(payload[key]);
17
+ }
14
18
  return new Promise((resolve, reject) => {
15
19
  serviceClient.upsertData({
16
20
  projection,
17
21
  auth: (0, auth_1.getProtobufAuthData)(auth),
18
22
  dataId,
19
- payload,
23
+ payload: usedPayload,
20
24
  eventMetadata,
21
25
  }, (error, response) => {
22
26
  if (error) {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./config/config";
2
- export * from "./management/client";
3
2
  export * from "./delivery/client";
4
3
  export { Filter, FieldFilter } from "./delivery/filter";
5
4
  export { GetProjectionDataList } from "./delivery/getDataList";
package/dist/index.js CHANGED
@@ -16,7 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.isUpsertValidationResponse = exports.isUpsertSuccessResponse = void 0;
18
18
  __exportStar(require("./config/config"), exports);
19
- __exportStar(require("./management/client"), exports);
20
19
  __exportStar(require("./delivery/client"), exports);
21
20
  var upsert_1 = require("./delivery/upsert");
22
21
  Object.defineProperty(exports, "isUpsertSuccessResponse", { enumerable: true, get: function () { return upsert_1.isUpsertSuccessResponse; } });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fraym/projections",
3
- "version": "0.14.1",
4
- "license": "UNLICENSED",
3
+ "version": "0.15.0",
4
+ "license": "MIT",
5
5
  "homepage": "https://github.com/fraym/projections-nodejs",
6
6
  "repository": {
7
7
  "type": "git",
@@ -12,20 +12,16 @@
12
12
  "test": "echo \"Error: no test specified\" && exit 0",
13
13
  "format": "prettier --write \"**/*.{ts,tsx,json}\"",
14
14
  "lint": "prettier --check \"**/*.{ts,tsx,json}\"",
15
- "build": "npm run clean && tsc && chmod +x dist/cmd/projections.js",
15
+ "build": "npm run clean && tsc",
16
16
  "clean": "rm -rf dist",
17
17
  "prepublishOnly": "npm test && npm run lint && npm run build",
18
- "preversion": "npm run lint",
19
- "cmd": "dist/cmd/projections.js"
18
+ "preversion": "npm run lint"
20
19
  },
21
20
  "files": [
22
21
  "dist/**/*"
23
22
  ],
24
23
  "main": "dist/index.js",
25
24
  "types": "dist/index.d.ts",
26
- "bin": {
27
- "projections": "dist/cmd/projections.js"
28
- },
29
25
  "dependencies": {
30
26
  "@fraym/projections-proto": "^1.0.0-alpha.17",
31
27
  "@graphql-tools/graphql-file-loader": "^7.5.16",
@@ -1,2 +0,0 @@
1
- #! /usr/bin/env node
2
- export {};
@@ -1,340 +0,0 @@
1
- #! /usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const yargs_1 = __importDefault(require("yargs/yargs"));
8
- const helpers_1 = require("yargs/helpers");
9
- const dotenv_1 = require("dotenv");
10
- const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader");
11
- const load_1 = require("@graphql-tools/load");
12
- const graphql_1 = require("graphql");
13
- const client_1 = require("../management/client");
14
- const run = async () => {
15
- (0, dotenv_1.config)();
16
- const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
17
- .config({
18
- schemaGlob: "./src/**/*.graphql",
19
- permissionsSchemaGlob: "",
20
- serverAddress: "http://127.0.0.1",
21
- apiToken: "",
22
- namespace: "",
23
- })
24
- .pkgConf("projections").argv;
25
- let schemaGlob = argv.schemaGlob;
26
- let permissionsSchemaGlob = argv.permissionsSchemaGlob;
27
- let serverAddress = argv.serverAddress;
28
- let apiToken = argv.apiToken;
29
- let namespace = argv.namespace;
30
- if (process.env.PROJECTIONS_SCHEMA_GLOB) {
31
- schemaGlob = process.env.PROJECTIONS_SCHEMA_GLOB;
32
- }
33
- if (process.env.PERMISSIONS_SCHEMA_GLOB) {
34
- permissionsSchemaGlob = process.env.PERMISSIONS_SCHEMA_GLOB;
35
- }
36
- if (process.env.PROJECTIONS_MANAGEMENT_SERVER_ADDRESS) {
37
- serverAddress = process.env.PROJECTIONS_MANAGEMENT_SERVER_ADDRESS;
38
- }
39
- if (process.env.PROJECTIONS_MANAGEMENT_API_TOKEN) {
40
- apiToken = process.env.PROJECTIONS_MANAGEMENT_API_TOKEN;
41
- }
42
- if (process.env.PROJECTIONS_NAMESPACE) {
43
- namespace = process.env.PROJECTIONS_NAMESPACE;
44
- }
45
- if (namespace === "Fraym") {
46
- throw new Error("Cannot use Fraym as namespace as it is reserved for fraym apps");
47
- }
48
- const schemaGlobs = [`${schemaGlob}`];
49
- if (permissionsSchemaGlob) {
50
- schemaGlobs.push(`${permissionsSchemaGlob}`);
51
- }
52
- const schema = await (0, load_1.loadSchema)(schemaGlobs, {
53
- loaders: [new graphql_file_loader_1.GraphQLFileLoader()],
54
- });
55
- const definitions = getTypeDefinition(schema, namespace);
56
- await migrateSchemas(definitions, serverAddress, apiToken, namespace);
57
- };
58
- run();
59
- const getTypeDefinition = (schema, namespace) => {
60
- const definitions = {};
61
- schema.toConfig().types.forEach(t => {
62
- if (!(t instanceof graphql_1.GraphQLObjectType) && !(t instanceof graphql_1.GraphQLEnumType)) {
63
- return;
64
- }
65
- const name = `${namespace}${t.toString()}`;
66
- ensureValidName(name);
67
- if (definitions[name]) {
68
- throw new Error(`duplicate definition for type "${name}" detected, try renaming one of them as they have to be uniquely named`);
69
- }
70
- if (t instanceof graphql_1.GraphQLObjectType) {
71
- definitions[name] = getTypeDefinitionFromGraphQLObjectType(t, namespace);
72
- return;
73
- }
74
- if (t instanceof graphql_1.GraphQLEnumType) {
75
- definitions[name] = getTypeDefinitionFromGraphQLEnumType(t, namespace);
76
- return;
77
- }
78
- });
79
- return definitions;
80
- };
81
- const getTypeDefinitionFromGraphQLEnumType = (t, namespace) => {
82
- var _a, _b;
83
- const name = `${namespace}${t.toString()}`;
84
- ensureValidName(name);
85
- let enumValuesString = "";
86
- (_b = (_a = t.astNode) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.forEach(value => {
87
- enumValuesString += `\n\t${value.name.value}`;
88
- });
89
- const schema = `enum ${name} {${enumValuesString}\n}`;
90
- return {
91
- isProjection: false,
92
- nestedTypes: [],
93
- schema,
94
- };
95
- };
96
- const getTypeDefinitionFromGraphQLObjectType = (t, namespace) => {
97
- var _a, _b, _c, _d, _e, _f;
98
- let isProjection = false;
99
- if (((_a = t.astNode) === null || _a === void 0 ? void 0 : _a.directives) && ((_b = t.astNode) === null || _b === void 0 ? void 0 : _b.directives.length) > 0) {
100
- const directiveNames = t.astNode.directives.map(directive => directive.name.value);
101
- isProjection = directiveNames.includes("upsertOn");
102
- }
103
- const name = `${namespace}${t.toString()}`;
104
- ensureValidName(name);
105
- let objectDirectivesString = "";
106
- let objectFieldsString = "";
107
- let nestedTypes = [];
108
- (_d = (_c = t.astNode) === null || _c === void 0 ? void 0 : _c.directives) === null || _d === void 0 ? void 0 : _d.forEach(d => {
109
- objectDirectivesString += getDirectiveString(d);
110
- });
111
- (_f = (_e = t.astNode) === null || _e === void 0 ? void 0 : _e.fields) === null || _f === void 0 ? void 0 : _f.forEach(f => {
112
- const { str, nestedTypes: newNestedTypes } = getFieldStringAndNestedTypes(f, namespace);
113
- objectFieldsString += str;
114
- newNestedTypes.forEach(nested => {
115
- if (nestedTypes.indexOf(nested) === -1) {
116
- nestedTypes.push(nested);
117
- }
118
- });
119
- });
120
- const schema = `type ${name}${objectDirectivesString} {${objectFieldsString}\n}`;
121
- return {
122
- isProjection,
123
- nestedTypes,
124
- schema,
125
- };
126
- };
127
- const getFieldStringAndNestedTypes = (f, namespace) => {
128
- var _a;
129
- let directivesString = "";
130
- (_a = f.directives) === null || _a === void 0 ? void 0 : _a.forEach(d => {
131
- directivesString += getDirectiveString(d);
132
- });
133
- const { nestedType, str: typeString } = getTypeData(f.type, namespace);
134
- const nestedTypes = [];
135
- if (nestedType) {
136
- nestedTypes.push(nestedType);
137
- }
138
- return {
139
- str: `\n\t${f.name.value}: ${typeString}${directivesString}`,
140
- nestedTypes,
141
- };
142
- };
143
- const getTypeData = (t, namespace) => {
144
- switch (t.kind) {
145
- case graphql_1.Kind.NAMED_TYPE:
146
- const name = t.name.value;
147
- ensureValidName(`${namespace}${name}`);
148
- return name === "String" ||
149
- name === "Float" ||
150
- name === "ID" ||
151
- name === "Boolean" ||
152
- name === "Int" ||
153
- name === "DateTime" ||
154
- name === "EventEnvelope"
155
- ? {
156
- str: name,
157
- }
158
- : {
159
- str: `${namespace}${name}`,
160
- nestedType: `${namespace}${name}`,
161
- };
162
- case graphql_1.Kind.LIST_TYPE:
163
- const { nestedType: listNestedType, str: listStr } = getTypeData(t.type, namespace);
164
- return {
165
- str: `[${listStr}]`,
166
- nestedType: listNestedType,
167
- };
168
- case graphql_1.Kind.NON_NULL_TYPE:
169
- const { nestedType: nonNullNestedType, str: nonNullStr } = getTypeData(t.type, namespace);
170
- return {
171
- str: `${nonNullStr}!`,
172
- nestedType: nonNullNestedType,
173
- };
174
- }
175
- };
176
- const getDirectiveString = (d) => {
177
- if (!d.arguments || d.arguments.length == 0) {
178
- return ` @${d.name.value}`;
179
- }
180
- let argsString = "";
181
- d.arguments.forEach(a => {
182
- if (argsString !== "") {
183
- argsString += ", ";
184
- }
185
- argsString += `${a.name.value}: ${getValueString(a.value)}`;
186
- });
187
- return ` @${d.name.value}(${argsString})`;
188
- };
189
- const getValueString = (v) => {
190
- switch (v.kind) {
191
- case graphql_1.Kind.LIST:
192
- let valuesString = "";
193
- v.values.forEach(el => {
194
- if (valuesString !== "") {
195
- valuesString += ", ";
196
- }
197
- valuesString += getValueString(el);
198
- });
199
- return `[${valuesString}]`;
200
- case graphql_1.Kind.STRING:
201
- return `"${v.value}"`;
202
- case graphql_1.Kind.FLOAT:
203
- case graphql_1.Kind.INT:
204
- case graphql_1.Kind.BOOLEAN:
205
- return `${v.value}`;
206
- case graphql_1.Kind.NULL:
207
- return `null`;
208
- case graphql_1.Kind.ENUM:
209
- return `${v.value}`;
210
- case graphql_1.Kind.OBJECT:
211
- let objectString = "";
212
- v.fields.forEach(f => {
213
- if (objectString !== "") {
214
- objectString += ", ";
215
- }
216
- objectString += `${f.name.value}: ${getValueString(f.value)}`;
217
- });
218
- return `{${objectString}}`;
219
- }
220
- };
221
- const addNestedTypesToSchema = (definitions, nestedTypeName, nestedTypes) => {
222
- const nestedTypeDefinition = definitions[nestedTypeName];
223
- if (nestedTypes.indexOf(nestedTypeName) !== -1 ||
224
- (nestedTypeDefinition && nestedTypeDefinition.isProjection)) {
225
- return {
226
- schema: "",
227
- nestedTypes: [],
228
- };
229
- }
230
- let newSchema = definitions[nestedTypeName].schema;
231
- if (!nestedTypes.includes(nestedTypeName)) {
232
- nestedTypes.push(nestedTypeName);
233
- }
234
- nestedTypeDefinition.nestedTypes.forEach(nestedNestedTypeName => {
235
- const nestedSchemaData = addNestedTypesToSchema(definitions, nestedNestedTypeName, nestedTypes);
236
- if (nestedSchemaData.schema === "") {
237
- return;
238
- }
239
- newSchema += `\n${nestedSchemaData.schema}`;
240
- nestedSchemaData.nestedTypes.forEach(nestedType => {
241
- if (!nestedTypes.includes(nestedType)) {
242
- nestedTypes.push(nestedType);
243
- }
244
- });
245
- });
246
- return {
247
- schema: newSchema,
248
- nestedTypes: nestedTypes,
249
- };
250
- };
251
- const migrateSchemas = async (definitions, serverAddress, apiToken, namespace) => {
252
- const managementClient = await (0, client_1.newManagementClient)({ serverAddress, apiToken });
253
- let existingProjections = (await managementClient.getAll()).filter(projectionName => !projectionName.startsWith("Crud") &&
254
- !projectionName.startsWith("Fraym") &&
255
- projectionName.startsWith(namespace));
256
- let createSchema = "";
257
- let updateSchema = "";
258
- const projectionsToCreate = [];
259
- const nestedTypesToCreate = [];
260
- const projectionsToUpdate = [];
261
- const nestedTypesToUpdate = [];
262
- const projectionsToRemove = [];
263
- existingProjections.forEach(projectionName => {
264
- if (!definitions[projectionName] || !definitions[projectionName].isProjection) {
265
- projectionsToRemove.push(projectionName);
266
- }
267
- else {
268
- projectionsToUpdate.push(projectionName);
269
- updateSchema += `\n${definitions[projectionName].schema}`;
270
- definitions[projectionName].nestedTypes.forEach(nestedTypeName => {
271
- const nestedSchemaData = addNestedTypesToSchema(definitions, nestedTypeName, nestedTypesToUpdate);
272
- if (nestedSchemaData.schema === "") {
273
- return;
274
- }
275
- updateSchema += `\n${nestedSchemaData.schema}`;
276
- nestedSchemaData.nestedTypes.forEach(nestedType => {
277
- if (!nestedTypesToUpdate.includes(nestedType)) {
278
- nestedTypesToUpdate.push(nestedType);
279
- }
280
- });
281
- });
282
- }
283
- });
284
- Object.keys(definitions).forEach(newName => {
285
- if (!definitions[newName].isProjection || existingProjections.includes(newName)) {
286
- return;
287
- }
288
- projectionsToCreate.push(newName);
289
- createSchema += `\n${definitions[newName].schema}`;
290
- definitions[newName].nestedTypes.forEach(nestedTypeName => {
291
- const nestedSchemaData = addNestedTypesToSchema(definitions, nestedTypeName, nestedTypesToCreate);
292
- if (nestedSchemaData.schema === "") {
293
- return;
294
- }
295
- createSchema += `\n${nestedSchemaData.schema}`;
296
- nestedSchemaData.nestedTypes.forEach(nestedType => {
297
- if (!nestedTypesToCreate.includes(nestedType)) {
298
- nestedTypesToCreate.push(nestedType);
299
- }
300
- });
301
- });
302
- });
303
- if (projectionsToCreate.length > 0) {
304
- console.log(`Creating ${projectionsToCreate.length} projections: ${projectionsToCreate}...`);
305
- await managementClient.upsert(replaceWithEnvData(createSchema));
306
- console.log(`Created ${projectionsToCreate.length} projections`);
307
- }
308
- if (projectionsToUpdate.length > 0) {
309
- console.log(`Updating ${projectionsToUpdate.length} projections: ${projectionsToUpdate}...`);
310
- await managementClient.upsert(replaceWithEnvData(updateSchema));
311
- console.log(`Updated ${projectionsToUpdate.length} projections`);
312
- }
313
- if (projectionsToRemove.length > 0) {
314
- console.log(`Removing ${projectionsToRemove.length} projections: ${projectionsToRemove}...`);
315
- await managementClient.remove(projectionsToRemove);
316
- console.log(`Removed ${projectionsToRemove.length} projections`);
317
- }
318
- };
319
- const replaceWithEnvData = (str) => {
320
- const regex = /{{env\.([a-zA-Z_]+)}}/g;
321
- const matches = str.match(regex);
322
- const envData = {};
323
- matches === null || matches === void 0 ? void 0 : matches.forEach(match => {
324
- var _a;
325
- const variable = match.replace("{{env.", "").replace("}}", "");
326
- if (!envData[variable]) {
327
- envData[variable] = (_a = process.env[variable]) !== null && _a !== void 0 ? _a : "";
328
- }
329
- });
330
- let outputStr = str;
331
- Object.keys(envData).forEach(key => {
332
- outputStr = outputStr.replaceAll(`{{env.${key}}}`, envData[key]);
333
- });
334
- return outputStr;
335
- };
336
- const ensureValidName = (name) => {
337
- if (name.startsWith("Fraym")) {
338
- throw new Error(`Cannot use Fraym as projection name prefix as it is reserved for fraym apps, got ${name}`);
339
- }
340
- };
@@ -1,7 +0,0 @@
1
- import { ManagementClientConfig } from "../config/config";
2
- export interface ManagementClient {
3
- upsert: (schema: string) => Promise<void>;
4
- remove: (projectionNames: string[]) => Promise<void>;
5
- getAll: () => Promise<string[]>;
6
- }
7
- export declare const newManagementClient: (config?: ManagementClientConfig) => Promise<ManagementClient>;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newManagementClient = void 0;
4
- const config_1 = require("../config/config");
5
- const getAll_1 = require("./getAll");
6
- const remove_1 = require("./remove");
7
- const upsert_1 = require("./upsert");
8
- const newManagementClient = async (config) => {
9
- const currentConfig = (0, config_1.useManagementConfigDefaults)(config);
10
- const upsert = async (schema) => {
11
- await (0, upsert_1.upsertProjections)(schema, currentConfig);
12
- };
13
- const remove = async (projectionNames) => {
14
- await (0, remove_1.removeProjections)(projectionNames, currentConfig);
15
- };
16
- const getAll = async () => {
17
- return await (0, getAll_1.getAllProjections)(currentConfig);
18
- };
19
- return {
20
- upsert,
21
- remove,
22
- getAll,
23
- };
24
- };
25
- exports.newManagementClient = newManagementClient;
@@ -1,2 +0,0 @@
1
- import { ManagementClientConfig } from "config/config";
2
- export declare const getAllProjections: (config: ManagementClientConfig) => Promise<string[]>;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAllProjections = void 0;
4
- const getAllProjections = async (config) => {
5
- const response = await fetch(`${config.serverAddress}/management/projections`, {
6
- method: "GET",
7
- headers: {
8
- Authorization: `Bearer ${config.apiToken}`,
9
- },
10
- });
11
- if (!response.ok) {
12
- throw new Error(await response.text());
13
- }
14
- const data = await response.json();
15
- return data.projectionNames;
16
- };
17
- exports.getAllProjections = getAllProjections;
@@ -1,2 +0,0 @@
1
- import { ManagementClientConfig } from "config/config";
2
- export declare const removeProjections: (projectionNames: string[], config: ManagementClientConfig) => Promise<void>;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeProjections = void 0;
4
- const removeProjections = async (projectionNames, config) => {
5
- const response = await fetch(`${config.serverAddress}/management/projections`, {
6
- method: "DELETE",
7
- headers: {
8
- Authorization: `Bearer ${config.apiToken}`,
9
- "Content-Type": "application/json",
10
- },
11
- body: JSON.stringify({
12
- projectionNames,
13
- }),
14
- });
15
- if (!response.ok) {
16
- throw new Error(await response.text());
17
- }
18
- };
19
- exports.removeProjections = removeProjections;
@@ -1,2 +0,0 @@
1
- import { ManagementClientConfig } from "config/config";
2
- export declare const upsertProjections: (schema: string, config: ManagementClientConfig) => Promise<void>;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.upsertProjections = void 0;
4
- const upsertProjections = async (schema, config) => {
5
- const response = await fetch(`${config.serverAddress}/management/projections`, {
6
- method: "POST",
7
- headers: {
8
- Authorization: `Bearer ${config.apiToken}`,
9
- "Content-Type": "application/json",
10
- },
11
- body: JSON.stringify({
12
- schema,
13
- }),
14
- });
15
- if (!response.ok) {
16
- throw new Error(await response.text());
17
- }
18
- };
19
- exports.upsertProjections = upsertProjections;