@fraym/projections 0.11.0 → 0.12.1

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/README.md CHANGED
@@ -20,7 +20,10 @@ You need to add the `Tenant-Id` header in order to use the graphQL Endpoint and
20
20
  Use the `projections` cli command to automatically apply your projection schemas to the projections service.
21
21
 
22
22
  Your type schemas have to match the glob you specify in the `PROJECTIONS_SCHEMA_GLOB` env variable (default: `./src/**/*.graphql`).
23
- 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`).
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.
24
27
 
25
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.
26
29
 
@@ -34,6 +37,8 @@ Use a `.env` file or env variables to configure cte clients and the command:
34
37
 
35
38
  ```env
36
39
  PROJECTIONS_SERVER_ADDRESS=127.0.0.1:9000
40
+ PROJECTIONS_MANAGEMENT_SERVER_ADDRESS=http://127.0.0.1
41
+ PROJECTIONS_MANAGEMENT_API_TOKEN=
37
42
  PROJECTIONS_SCHEMA_GLOB=./src/projections/*.graphql
38
43
  PERMISSIONS_SCHEMA_GLOB=
39
44
  PROJECTIONS_NAMESPACE=
@@ -275,7 +280,7 @@ const dataList = await client.getDataList(
275
280
  );
276
281
  ```
277
282
 
278
- ### Gracefully close the clients
283
+ ### Gracefully close the delivery client
279
284
 
280
285
  You won't lose any data if you don't. Use it for your peace of mind.
281
286
 
@@ -17,13 +17,15 @@ const run = async () => {
17
17
  .config({
18
18
  schemaGlob: "./src/**/*.graphql",
19
19
  permissionsSchemaGlob: "",
20
- serverAddress: "127.0.0.1:9000",
20
+ serverAddress: "http://127.0.0.1",
21
+ apiToken: "",
21
22
  namespace: "",
22
23
  })
23
24
  .pkgConf("projections").argv;
24
25
  let schemaGlob = argv.schemaGlob;
25
26
  let permissionsSchemaGlob = argv.permissionsSchemaGlob;
26
27
  let serverAddress = argv.serverAddress;
28
+ let apiToken = argv.apiToken;
27
29
  let namespace = argv.namespace;
28
30
  if (process.env.PROJECTIONS_SCHEMA_GLOB) {
29
31
  schemaGlob = process.env.PROJECTIONS_SCHEMA_GLOB;
@@ -31,8 +33,11 @@ const run = async () => {
31
33
  if (process.env.PERMISSIONS_SCHEMA_GLOB) {
32
34
  permissionsSchemaGlob = process.env.PERMISSIONS_SCHEMA_GLOB;
33
35
  }
34
- if (process.env.PROJECTIONS_SERVER_ADDRESS) {
35
- serverAddress = process.env.PROJECTIONS_SERVER_ADDRESS;
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;
36
41
  }
37
42
  if (process.env.PROJECTIONS_NAMESPACE) {
38
43
  namespace = process.env.PROJECTIONS_NAMESPACE;
@@ -48,7 +53,7 @@ const run = async () => {
48
53
  loaders: [new graphql_file_loader_1.GraphQLFileLoader()],
49
54
  });
50
55
  const definitions = getTypeDefinition(schema, namespace);
51
- await migrateSchemas(definitions, serverAddress, namespace);
56
+ await migrateSchemas(definitions, serverAddress, apiToken, namespace);
52
57
  };
53
58
  run();
54
59
  const getTypeDefinition = (schema, namespace) => {
@@ -243,8 +248,8 @@ const addNestedTypesToSchema = (definitions, nestedTypeName, nestedTypes) => {
243
248
  nestedTypes: nestedTypes,
244
249
  };
245
250
  };
246
- const migrateSchemas = async (definitions, serverAddress, namespace) => {
247
- const managementClient = await (0, client_1.newManagementClient)({ serverAddress });
251
+ const migrateSchemas = async (definitions, serverAddress, apiToken, namespace) => {
252
+ const managementClient = await (0, client_1.newManagementClient)({ serverAddress, apiToken });
248
253
  let existingProjections = (await managementClient.getAll()).filter(projectionName => !projectionName.startsWith("Crud") &&
249
254
  !projectionName.startsWith("Fraym") &&
250
255
  projectionName.startsWith(namespace));
@@ -297,12 +302,12 @@ const migrateSchemas = async (definitions, serverAddress, namespace) => {
297
302
  });
298
303
  if (projectionsToCreate.length > 0) {
299
304
  console.log(`Creating ${projectionsToCreate.length} projections: ${projectionsToCreate}...`);
300
- await managementClient.update(createSchema);
305
+ await managementClient.upsert(createSchema);
301
306
  console.log(`Created ${projectionsToCreate.length} projections`);
302
307
  }
303
308
  if (projectionsToUpdate.length > 0) {
304
309
  console.log(`Updating ${projectionsToUpdate.length} projections: ${projectionsToUpdate}...`);
305
- await managementClient.update(updateSchema);
310
+ await managementClient.upsert(updateSchema);
306
311
  console.log(`Updated ${projectionsToUpdate.length} projections`);
307
312
  }
308
313
  if (projectionsToRemove.length > 0) {
@@ -1,7 +1,13 @@
1
- export interface ClientConfig {
1
+ export interface DeliveryClientConfig {
2
2
  serverAddress: string;
3
3
  keepaliveInterval?: number;
4
4
  keepaliveTimeout?: number;
5
5
  }
6
- export declare const getEnvConfig: () => ClientConfig;
7
- export declare const useConfigDefaults: (config?: ClientConfig) => Required<ClientConfig>;
6
+ export interface ManagementClientConfig {
7
+ serverAddress: string;
8
+ apiToken: string;
9
+ }
10
+ export declare const getEnvDeliveryConfig: () => DeliveryClientConfig;
11
+ export declare const getEnvManagementConfig: () => ManagementClientConfig;
12
+ export declare const useDeliveryConfigDefaults: (config?: DeliveryClientConfig) => Required<DeliveryClientConfig>;
13
+ export declare const useManagementConfigDefaults: (config?: ManagementClientConfig) => Required<ManagementClientConfig>;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useConfigDefaults = exports.getEnvConfig = void 0;
3
+ exports.useManagementConfigDefaults = exports.useDeliveryConfigDefaults = exports.getEnvManagementConfig = exports.getEnvDeliveryConfig = void 0;
4
4
  const dotenv_1 = require("dotenv");
5
- const getEnvConfig = () => {
5
+ const getEnvDeliveryConfig = () => {
6
6
  var _a;
7
7
  (0, dotenv_1.config)();
8
8
  const serverAddress = (_a = process.env.PROJECTIONS_SERVER_ADDRESS) !== null && _a !== void 0 ? _a : "";
@@ -22,11 +22,20 @@ const getEnvConfig = () => {
22
22
  keepaliveTimeout,
23
23
  };
24
24
  };
25
- exports.getEnvConfig = getEnvConfig;
26
- const useConfigDefaults = (config) => {
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
+ const useDeliveryConfigDefaults = (config) => {
27
36
  var _a, _b;
28
37
  if (!config) {
29
- config = (0, exports.getEnvConfig)();
38
+ config = (0, exports.getEnvDeliveryConfig)();
30
39
  }
31
40
  return {
32
41
  serverAddress: config.serverAddress,
@@ -34,4 +43,14 @@ const useConfigDefaults = (config) => {
34
43
  keepaliveInterval: (_b = config.keepaliveInterval) !== null && _b !== void 0 ? _b : 40 * 1000,
35
44
  };
36
45
  };
37
- exports.useConfigDefaults = useConfigDefaults;
46
+ 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;
@@ -1,4 +1,4 @@
1
- import { ClientConfig } from "../config/config";
1
+ import { DeliveryClientConfig } from "../config/config";
2
2
  import { Filter } from "./filter";
3
3
  import { GetProjectionDataList } from "./getDataList";
4
4
  import { Order } from "./order";
@@ -12,4 +12,4 @@ export interface DeliveryClient {
12
12
  deleteDataByFilter: (projection: string, authData: AuthData, filter?: Filter) => Promise<number>;
13
13
  close: () => Promise<void>;
14
14
  }
15
- export declare const newDeliveryClient: (config?: ClientConfig) => Promise<DeliveryClient>;
15
+ export declare const newDeliveryClient: (config?: DeliveryClientConfig) => Promise<DeliveryClient>;
@@ -9,7 +9,7 @@ const getDataList_1 = require("./getDataList");
9
9
  const upsert_1 = require("./upsert");
10
10
  const delete_1 = require("./delete");
11
11
  const newDeliveryClient = async (config) => {
12
- config = (0, config_1.useConfigDefaults)(config);
12
+ config = (0, config_1.useDeliveryConfigDefaults)(config);
13
13
  const serviceClient = new projections_proto_1.DeliveryServiceClient(config.serverAddress, grpc_js_1.credentials.createInsecure(), {
14
14
  "grpc.keepalive_time_ms": config.keepaliveInterval,
15
15
  "grpc.keepalive_timeout_ms": config.keepaliveTimeout,
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { ClientConfig, getEnvConfig } from "./config/config";
1
+ export * from "./config/config";
2
2
  export * from "./management/client";
3
3
  export * from "./delivery/client";
4
4
  export { Filter, FieldFilter } from "./delivery/filter";
package/dist/index.js CHANGED
@@ -14,9 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isUpsertValidationResponse = exports.isUpsertSuccessResponse = exports.getEnvConfig = void 0;
18
- var config_1 = require("./config/config");
19
- Object.defineProperty(exports, "getEnvConfig", { enumerable: true, get: function () { return config_1.getEnvConfig; } });
17
+ exports.isUpsertValidationResponse = exports.isUpsertSuccessResponse = void 0;
18
+ __exportStar(require("./config/config"), exports);
20
19
  __exportStar(require("./management/client"), exports);
21
20
  __exportStar(require("./delivery/client"), exports);
22
21
  var upsert_1 = require("./delivery/upsert");
@@ -1,9 +1,7 @@
1
- import { ClientConfig } from "../config/config";
1
+ import { ManagementClientConfig } from "../config/config";
2
2
  export interface ManagementClient {
3
- create: (schema: string) => Promise<void>;
4
- update: (schema: string) => Promise<void>;
3
+ upsert: (schema: string) => Promise<void>;
5
4
  remove: (projectionNames: string[]) => Promise<void>;
6
5
  getAll: () => Promise<string[]>;
7
- close: () => Promise<void>;
8
6
  }
9
- export declare const newManagementClient: (config?: ClientConfig) => Promise<ManagementClient>;
7
+ export declare const newManagementClient: (config?: ManagementClientConfig) => Promise<ManagementClient>;
@@ -1,41 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.newManagementClient = void 0;
4
- const projections_proto_1 = require("@fraym/projections-proto");
5
- const grpc_js_1 = require("@grpc/grpc-js");
6
4
  const config_1 = require("../config/config");
7
- const create_1 = require("./create");
8
5
  const getAll_1 = require("./getAll");
9
6
  const remove_1 = require("./remove");
10
- const update_1 = require("./update");
7
+ const upsert_1 = require("./upsert");
11
8
  const newManagementClient = async (config) => {
12
- config = (0, config_1.useConfigDefaults)(config);
13
- const serviceClient = new projections_proto_1.ManagementServiceClient(config.serverAddress, grpc_js_1.credentials.createInsecure(), {
14
- "grpc.keepalive_time_ms": config.keepaliveInterval,
15
- "grpc.keepalive_timeout_ms": config.keepaliveTimeout,
16
- "grpc.keepalive_permit_without_calls": 1,
17
- });
18
- const create = async (schema) => {
19
- await (0, create_1.createProjections)(schema, serviceClient);
20
- };
21
- const update = async (schema) => {
22
- await (0, update_1.updateProjections)(schema, serviceClient);
9
+ const currentConfig = (0, config_1.useManagementConfigDefaults)(config);
10
+ const upsert = async (schema) => {
11
+ await (0, upsert_1.upsertProjections)(schema, currentConfig);
23
12
  };
24
13
  const remove = async (projectionNames) => {
25
- await (0, remove_1.removeProjections)(projectionNames, serviceClient);
14
+ await (0, remove_1.removeProjections)(projectionNames, currentConfig);
26
15
  };
27
16
  const getAll = async () => {
28
- return await (0, getAll_1.getAllProjections)(serviceClient);
29
- };
30
- const close = async () => {
31
- serviceClient.close();
17
+ return await (0, getAll_1.getAllProjections)(currentConfig);
32
18
  };
33
19
  return {
34
- create,
35
- update,
20
+ upsert,
36
21
  remove,
37
22
  getAll,
38
- close,
39
23
  };
40
24
  };
41
25
  exports.newManagementClient = newManagementClient;
@@ -1,2 +1,2 @@
1
- import { ManagementServiceClient } from "@fraym/projections-proto";
2
- export declare const getAllProjections: (serviceClient: ManagementServiceClient) => Promise<string[]>;
1
+ import { ManagementClientConfig } from "config/config";
2
+ export declare const getAllProjections: (config: ManagementClientConfig) => Promise<string[]>;
@@ -1,15 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAllProjections = void 0;
4
- const getAllProjections = async (serviceClient) => {
5
- return new Promise((resolve, reject) => {
6
- serviceClient.getProjections({}, (error, response) => {
7
- if (error) {
8
- reject(error.message);
9
- return;
10
- }
11
- resolve(response.projectionNames);
12
- });
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
+ },
13
10
  });
11
+ if (!response.ok) {
12
+ throw new Error(await response.text());
13
+ }
14
+ const data = await response.json();
15
+ return data.projectionNames;
14
16
  };
15
17
  exports.getAllProjections = getAllProjections;
@@ -1,2 +1,2 @@
1
- import { ManagementServiceClient } from "@fraym/projections-proto";
2
- export declare const removeProjections: (projectionNames: string[], serviceClient: ManagementServiceClient) => Promise<void>;
1
+ import { ManagementClientConfig } from "config/config";
2
+ export declare const removeProjections: (projectionNames: string[], config: ManagementClientConfig) => Promise<void>;
@@ -1,17 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.removeProjections = void 0;
4
- const removeProjections = async (projectionNames, serviceClient) => {
5
- return new Promise((resolve, reject) => {
6
- serviceClient.removeProjections({
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({
7
12
  projectionNames,
8
- }, error => {
9
- if (error) {
10
- reject(error.message);
11
- return;
12
- }
13
- resolve();
14
- });
13
+ }),
15
14
  });
15
+ if (!response.ok) {
16
+ throw new Error(await response.text());
17
+ }
16
18
  };
17
19
  exports.removeProjections = removeProjections;
@@ -0,0 +1,2 @@
1
+ import { ManagementClientConfig } from "config/config";
2
+ export declare const upsertProjections: (schema: string, config: ManagementClientConfig) => Promise<void>;
@@ -0,0 +1,19 @@
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/projections",
3
- "version": "0.11.0",
3
+ "version": "0.12.1",
4
4
  "license": "UNLICENSED",
5
5
  "homepage": "https://github.com/fraym/projections-nodejs",
6
6
  "repository": {
@@ -1,2 +0,0 @@
1
- import { ManagementServiceClient } from "@fraym/projections-proto";
2
- export declare const createProjections: (schema: string, serviceClient: ManagementServiceClient) => Promise<void>;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createProjections = void 0;
4
- const createProjections = async (schema, serviceClient) => {
5
- return new Promise((resolve, reject) => {
6
- serviceClient.createProjections({
7
- schema,
8
- }, error => {
9
- if (error) {
10
- reject(error.message);
11
- return;
12
- }
13
- resolve();
14
- });
15
- });
16
- };
17
- exports.createProjections = createProjections;
@@ -1,2 +0,0 @@
1
- import { ManagementServiceClient } from "@fraym/projections-proto";
2
- export declare const updateProjections: (schema: string, serviceClient: ManagementServiceClient) => Promise<void>;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.updateProjections = void 0;
4
- const updateProjections = async (schema, serviceClient) => {
5
- return new Promise((resolve, reject) => {
6
- serviceClient.updateProjections({
7
- schema,
8
- }, error => {
9
- if (error) {
10
- reject(error.message);
11
- return;
12
- }
13
- resolve();
14
- });
15
- });
16
- };
17
- exports.updateProjections = updateProjections;