@fraym/projections 0.9.0 → 0.10.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.
@@ -0,0 +1,7 @@
1
+ import { AuthData as PbAuthData } from "@fraym/projections-proto";
2
+ export interface AuthData {
3
+ tenantId: string;
4
+ scopes: string[];
5
+ data: Record<string, any>;
6
+ }
7
+ export declare const getProtobufAuthData: (auth: AuthData) => PbAuthData;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getProtobufAuthData = void 0;
4
+ const getProtobufAuthData = (auth) => {
5
+ const data = {};
6
+ for (let key in auth.data) {
7
+ data[key] = JSON.stringify(auth.data[key]);
8
+ }
9
+ return {
10
+ tenantId: auth.tenantId,
11
+ scopes: auth.scopes,
12
+ data,
13
+ };
14
+ };
15
+ exports.getProtobufAuthData = getProtobufAuthData;
@@ -2,9 +2,14 @@ import { ClientConfig } from "../config/config";
2
2
  import { Filter } from "./filter";
3
3
  import { GetProjectionDataList } from "./getDataList";
4
4
  import { Order } from "./order";
5
+ import { AuthData } from "./auth";
6
+ import { UpsertResponse } from "./upsert";
5
7
  export interface DeliveryClient {
6
- getData: <T extends {}>(tenantId: string, type: string, id: string, returnEmptyDataIfNotFound?: boolean) => Promise<T | null>;
7
- getDataList: <T extends {}>(tenantId: string, type: string, limit?: number, page?: number, filter?: Filter, order?: Order[]) => Promise<GetProjectionDataList<T> | null>;
8
+ getData: <T extends {}>(projection: string, authData: AuthData, id: string, filter?: Filter, returnEmptyDataIfNotFound?: boolean) => Promise<T | null>;
9
+ getDataList: <T extends {}>(projection: string, authData: AuthData, limit?: number, page?: number, filter?: Filter, order?: Order[]) => Promise<GetProjectionDataList<T> | null>;
10
+ upsertData: <T extends {}>(projection: string, authData: AuthData, dataId: string, payload: T) => Promise<UpsertResponse<T>>;
11
+ deleteDataById: (projection: string, authData: AuthData, dataId: string) => Promise<number>;
12
+ deleteDataByFilter: (projection: string, authData: AuthData, filter?: Filter) => Promise<number>;
8
13
  close: () => Promise<void>;
9
14
  }
10
15
  export declare const newDeliveryClient: (config?: ClientConfig) => Promise<DeliveryClient>;
@@ -6,6 +6,8 @@ const grpc_js_1 = require("@grpc/grpc-js");
6
6
  const config_1 = require("../config/config");
7
7
  const getData_1 = require("./getData");
8
8
  const getDataList_1 = require("./getDataList");
9
+ const upsert_1 = require("./upsert");
10
+ const delete_1 = require("./delete");
9
11
  const newDeliveryClient = async (config) => {
10
12
  config = (0, config_1.useConfigDefaults)(config);
11
13
  const serviceClient = new projections_proto_1.DeliveryServiceClient(config.serverAddress, grpc_js_1.credentials.createInsecure(), {
@@ -13,11 +15,20 @@ const newDeliveryClient = async (config) => {
13
15
  "grpc.keepalive_timeout_ms": config.keepaliveTimeout,
14
16
  "grpc.keepalive_permit_without_calls": 1,
15
17
  });
16
- const getData = async (tenantId, projection, id, returnEmptyDataIfNotFound = false) => {
17
- return await (0, getData_1.getProjectionData)(tenantId, projection, id, returnEmptyDataIfNotFound, serviceClient);
18
+ const getData = async (projection, auth, id, filter = { fields: {}, and: [], or: [] }, returnEmptyDataIfNotFound = false) => {
19
+ return await (0, getData_1.getProjectionData)(projection, auth, id, filter, returnEmptyDataIfNotFound, serviceClient);
18
20
  };
19
- const getDataList = async (tenantId, projection, limit = 0, page = 1, filter = { fields: {}, and: [], or: [] }, order = []) => {
20
- return await (0, getDataList_1.getProjectionDataList)(tenantId, projection, limit, page, filter, order, serviceClient);
21
+ const getDataList = async (projection, auth, limit = 0, page = 1, filter = { fields: {}, and: [], or: [] }, order = []) => {
22
+ return await (0, getDataList_1.getProjectionDataList)(projection, auth, limit, page, filter, order, serviceClient);
23
+ };
24
+ const upsertData = async (projection, authData, dataId, payload) => {
25
+ return (0, upsert_1.upsertProjectionData)(projection, authData, dataId, payload, serviceClient);
26
+ };
27
+ const deleteDataById = async (projection, authData, dataId) => {
28
+ return (0, delete_1.deleteProjectionData)(projection, authData, dataId, { fields: {}, and: [], or: [] }, serviceClient);
29
+ };
30
+ const deleteDataByFilter = async (projection, authData, filter = { fields: {}, and: [], or: [] }) => {
31
+ return (0, delete_1.deleteProjectionData)(projection, authData, "", filter, serviceClient);
21
32
  };
22
33
  const close = async () => {
23
34
  serviceClient.close();
@@ -25,6 +36,9 @@ const newDeliveryClient = async (config) => {
25
36
  return {
26
37
  getData,
27
38
  getDataList,
39
+ upsertData,
40
+ deleteDataById,
41
+ deleteDataByFilter,
28
42
  close,
29
43
  };
30
44
  };
@@ -0,0 +1,4 @@
1
+ import { DeliveryServiceClient } from "@fraym/projections-proto";
2
+ import { AuthData } from "./auth";
3
+ import { Filter } from "./filter";
4
+ export declare const deleteProjectionData: (projection: string, auth: AuthData, dataId: string, filter: Filter, serviceClient: DeliveryServiceClient) => Promise<number>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteProjectionData = void 0;
4
+ const auth_1 = require("./auth");
5
+ const filter_1 = require("./filter");
6
+ const deleteProjectionData = async (projection, auth, dataId, filter, serviceClient) => {
7
+ return new Promise((resolve, reject) => {
8
+ serviceClient.deleteData({
9
+ projection,
10
+ auth: (0, auth_1.getProtobufAuthData)(auth),
11
+ dataId,
12
+ filter: (0, filter_1.getProtobufDataFilter)(filter),
13
+ }, (error, response) => {
14
+ if (error) {
15
+ reject(error.message);
16
+ return;
17
+ }
18
+ resolve(response.numberOfDeletedEntries);
19
+ });
20
+ });
21
+ };
22
+ exports.deleteProjectionData = deleteProjectionData;
@@ -1,2 +1,4 @@
1
1
  import { DeliveryServiceClient } from "@fraym/projections-proto";
2
- export declare const getProjectionData: <T extends {}>(tenantId: string, projection: string, dataId: string, returnEmptyDataIfNotFound: boolean, serviceClient: DeliveryServiceClient) => Promise<T | null>;
2
+ import { AuthData } from "./auth";
3
+ import { Filter } from "./filter";
4
+ export declare const getProjectionData: <T extends {}>(projection: string, auth: AuthData, dataId: string, filter: Filter, returnEmptyDataIfNotFound: boolean, serviceClient: DeliveryServiceClient) => Promise<T | null>;
@@ -1,30 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getProjectionData = void 0;
4
- const getProjectionData = async (tenantId, projection, dataId, returnEmptyDataIfNotFound, serviceClient) => {
4
+ const auth_1 = require("./auth");
5
+ const filter_1 = require("./filter");
6
+ const getProjectionData = async (projection, auth, dataId, filter, returnEmptyDataIfNotFound, serviceClient) => {
5
7
  return new Promise((resolve, reject) => {
6
8
  serviceClient.getData({
7
- tenantId,
8
9
  projection,
10
+ auth: (0, auth_1.getProtobufAuthData)(auth),
9
11
  dataId,
10
- limit: 0,
11
- page: 0,
12
+ filter: (0, filter_1.getProtobufDataFilter)(filter),
12
13
  returnEmptyDataIfNotFound,
13
- filter: { fields: {}, and: [], or: [] },
14
- order: [],
15
14
  }, (error, response) => {
16
15
  if (error) {
17
16
  reject(error.message);
18
17
  return;
19
18
  }
20
- if (response.result.length !== 1) {
19
+ const result = response.result;
20
+ if (!result) {
21
21
  resolve(null);
22
22
  return;
23
23
  }
24
24
  const data = {};
25
- const resultData = response.result[0].data;
26
- for (const key in resultData) {
27
- data[key] = JSON.parse(resultData[key]);
25
+ for (const key in result.data) {
26
+ data[key] = JSON.parse(result.data[key]);
28
27
  }
29
28
  resolve(data);
30
29
  });
@@ -1,4 +1,5 @@
1
1
  import { DeliveryServiceClient } from "@fraym/projections-proto";
2
+ import { AuthData } from "./auth";
2
3
  import { Filter } from "./filter";
3
4
  import { Order } from "./order";
4
5
  export interface GetProjectionDataList<T extends {}> {
@@ -6,4 +7,4 @@ export interface GetProjectionDataList<T extends {}> {
6
7
  page: number;
7
8
  data: T[];
8
9
  }
9
- export declare const getProjectionDataList: <T extends {}>(tenantId: string, projection: string, limit: number, page: number, filter: Filter, order: Order[], serviceClient: DeliveryServiceClient) => Promise<GetProjectionDataList<T> | null>;
10
+ export declare const getProjectionDataList: <T extends {}>(projection: string, auth: AuthData, limit: number, page: number, filter: Filter, order: Order[], serviceClient: DeliveryServiceClient) => Promise<GetProjectionDataList<T> | null>;
@@ -1,17 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getProjectionDataList = void 0;
4
+ const auth_1 = require("./auth");
4
5
  const filter_1 = require("./filter");
5
6
  const order_1 = require("./order");
6
- const getProjectionDataList = async (tenantId, projection, limit, page, filter, order, serviceClient) => {
7
+ const getProjectionDataList = async (projection, auth, limit, page, filter, order, serviceClient) => {
7
8
  return new Promise((resolve, reject) => {
8
- serviceClient.getData({
9
- tenantId,
9
+ serviceClient.getDataList({
10
10
  projection,
11
- dataId: "",
11
+ auth: (0, auth_1.getProtobufAuthData)(auth),
12
12
  limit,
13
13
  page,
14
- returnEmptyDataIfNotFound: false,
15
14
  filter: (0, filter_1.getProtobufDataFilter)(filter),
16
15
  order: (0, order_1.getProtobufDataOrder)(order),
17
16
  }, (error, response) => {
@@ -0,0 +1,13 @@
1
+ import { DeliveryServiceClient } from "@fraym/projections-proto";
2
+ import { AuthData } from "./auth";
3
+ export type UpsertResponse<T extends {}> = UpsertSuccessResponse<T> | UpsertValidationResponse;
4
+ export interface UpsertSuccessResponse<T extends {}> {
5
+ data: T;
6
+ }
7
+ export interface UpsertValidationResponse {
8
+ validationErrors: string[];
9
+ fieldValidationErrors: Record<string, string>;
10
+ }
11
+ export declare const isUpsertSuccessResponse: <T extends {}>(response: UpsertResponse<T>) => response is UpsertSuccessResponse<T>;
12
+ export declare const isUpsertValidationResponse: <T extends {}>(response: UpsertResponse<T>) => response is UpsertValidationResponse;
13
+ export declare const upsertProjectionData: <T extends {}>(projection: string, auth: AuthData, dataId: string, payload: T, serviceClient: DeliveryServiceClient) => Promise<UpsertResponse<T>>;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.upsertProjectionData = exports.isUpsertValidationResponse = exports.isUpsertSuccessResponse = void 0;
4
+ const auth_1 = require("./auth");
5
+ const isUpsertSuccessResponse = (response) => {
6
+ return response.hasOwnProperty("data");
7
+ };
8
+ exports.isUpsertSuccessResponse = isUpsertSuccessResponse;
9
+ const isUpsertValidationResponse = (response) => {
10
+ return !response.hasOwnProperty("data");
11
+ };
12
+ exports.isUpsertValidationResponse = isUpsertValidationResponse;
13
+ const upsertProjectionData = async (projection, auth, dataId, payload, serviceClient) => {
14
+ return new Promise((resolve, reject) => {
15
+ serviceClient.upsertData({
16
+ projection,
17
+ auth: (0, auth_1.getProtobufAuthData)(auth),
18
+ dataId,
19
+ payload,
20
+ }, (error, response) => {
21
+ if (error) {
22
+ reject(error.message);
23
+ return;
24
+ }
25
+ if (response.validationErrors || response.fieldValidationErrors) {
26
+ resolve({
27
+ validationErrors: response.validationErrors,
28
+ fieldValidationErrors: response.fieldValidationErrors,
29
+ });
30
+ return;
31
+ }
32
+ const data = {};
33
+ for (const key in response.newData) {
34
+ data[key] = JSON.parse(response.newData[key]);
35
+ }
36
+ resolve({
37
+ data,
38
+ });
39
+ });
40
+ });
41
+ };
42
+ exports.upsertProjectionData = upsertProjectionData;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
+ export { ClientConfig, getEnvConfig } from "./config/config";
1
2
  export * from "./management/client";
2
3
  export * from "./delivery/client";
3
4
  export { Filter, FieldFilter } from "./delivery/filter";
5
+ export { GetProjectionDataList } from "./delivery/getDataList";
6
+ export { AuthData } from "./delivery/auth";
4
7
  export { Order } from "./delivery/order";
5
- export { ClientConfig, getEnvConfig } from "./config/config";
8
+ export { UpsertResponse, UpsertSuccessResponse, UpsertValidationResponse, isUpsertSuccessResponse, isUpsertValidationResponse, } from "./delivery/upsert";
package/dist/index.js CHANGED
@@ -14,8 +14,11 @@ 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.getEnvConfig = void 0;
18
- __exportStar(require("./management/client"), exports);
19
- __exportStar(require("./delivery/client"), exports);
17
+ exports.isUpsertValidationResponse = exports.isUpsertSuccessResponse = exports.getEnvConfig = void 0;
20
18
  var config_1 = require("./config/config");
21
19
  Object.defineProperty(exports, "getEnvConfig", { enumerable: true, get: function () { return config_1.getEnvConfig; } });
20
+ __exportStar(require("./management/client"), exports);
21
+ __exportStar(require("./delivery/client"), exports);
22
+ var upsert_1 = require("./delivery/upsert");
23
+ Object.defineProperty(exports, "isUpsertSuccessResponse", { enumerable: true, get: function () { return upsert_1.isUpsertSuccessResponse; } });
24
+ Object.defineProperty(exports, "isUpsertValidationResponse", { enumerable: true, get: function () { return upsert_1.isUpsertValidationResponse; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/projections",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "license": "UNLICENSED",
5
5
  "homepage": "https://github.com/fraym/projections-nodejs",
6
6
  "repository": {
@@ -27,20 +27,20 @@
27
27
  "projections": "dist/cmd/projections.js"
28
28
  },
29
29
  "dependencies": {
30
- "@fraym/projections-proto": "^1.0.0-alpha.12",
31
- "@graphql-tools/graphql-file-loader": "^7.5.11",
32
- "@graphql-tools/load": "^7.8.6",
33
- "@grpc/grpc-js": "^1.8.7",
30
+ "@fraym/projections-proto": "^1.0.0-alpha.14",
31
+ "@graphql-tools/graphql-file-loader": "^7.5.16",
32
+ "@graphql-tools/load": "^7.8.13",
33
+ "@grpc/grpc-js": "^1.8.12",
34
34
  "dotenv": "^16.0.3",
35
35
  "graphql": "^16.6.0",
36
- "yargs": "^17.6.2"
36
+ "yargs": "^17.7.1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@becklyn/prettier": "^1.0.2",
40
- "@types/uuid": "^8.3.4",
41
- "@types/yargs": "^17.0.13",
42
- "prettier": "^2.7.1",
43
- "typescript": "^4.8.4",
40
+ "@types/uuid": "^9.0.1",
41
+ "@types/yargs": "^17.0.22",
42
+ "prettier": "^2.8.4",
43
+ "typescript": "^5.0.2",
44
44
  "uuid": "^9.0.0"
45
45
  },
46
46
  "prettier": "@becklyn/prettier"