@fraym/crud 0.3.1 → 0.4.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/README.md CHANGED
@@ -13,22 +13,22 @@ npm i @fraym/crud
13
13
  You can access the graphQL api at `http://crud:3000/delivery/graphql`.
14
14
  There is a sandbox available at `http://crud:3000/delivery/graphql/sandbox`.
15
15
 
16
+ You need to add the `Tenant-Id` header in order to use the graphQL Endpoint and the sandbox.
17
+
16
18
  ## CLI command
17
19
 
18
20
  Use the `crud` cli command to automatically apply your crud schemas to the crud service.
19
21
 
20
- The `--config ./path/crud.config.json` flag can be used to configure the path of your config file.
22
+ Your type schemas have to match the glob you specify in the `CRUD_SCHEMA_GLOB` env variable (default: `./src/**/*.graphql`).
23
+ You can specify the address (and port) of the crud service instance you use in the `CRUD_SERVER_ADDRESS` env variable (default: `127.0.0.1:9000`).
21
24
 
22
- Your type schemas have to match the glob you specify in `schemaGlob` of he config file (default: `./src/**/*.graphql`).
23
- You can specify the address (and port) of the crud service instance you use in `serverAddress` of the config file (default: `127.0.0.1:9000`).
25
+ ### Config
24
26
 
25
- ### CLI command config
27
+ use a `.env` file or env variables to configure cte clients and the command:
26
28
 
27
- ```json
28
- {
29
- "schemaGlob": "./src/crud/*.graphql", // path to your crud schema files
30
- "serverAddress": "127.0.0.1:9000" // address of the crud service
31
- }
29
+ ```env
30
+ CRUD_SERVER_ADDRESS=127.0.0.1:9000
31
+ CRUD_SCHEMA_GLOB=./src/crud/*.graphql
32
32
  ```
33
33
 
34
34
  ## Usage
@@ -38,17 +38,13 @@ You can specify the address (and port) of the crud service instance you use in `
38
38
  delivery client:
39
39
 
40
40
  ```typescript
41
- const deliveryClient = await newDeliveryClient({
42
- serverAddress: "127.0.0.1:9000",
43
- });
41
+ const deliveryClient = await newDeliveryClient();
44
42
  ```
45
43
 
46
44
  management client:
47
45
 
48
46
  ```typescript
49
- const managementClient = await newManagementClient({
50
- serverAddress: "127.0.0.1:9000",
51
- });
47
+ const managementClient = await newManagementClient();
52
48
  ```
53
49
 
54
50
  ### Create one or multipe CRUD types
package/dist/cmd/crud.js CHANGED
@@ -4,20 +4,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- const fs_1 = __importDefault(require("fs"));
8
7
  const yargs_1 = __importDefault(require("yargs/yargs"));
9
8
  const helpers_1 = require("yargs/helpers");
10
9
  const client_1 = require("../management/client");
11
10
  const load_1 = require("@graphql-tools/load");
12
11
  const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader");
13
12
  const graphql_1 = require("graphql");
13
+ const dotenv_1 = require("dotenv");
14
14
  const run = async () => {
15
+ (0, dotenv_1.config)();
15
16
  const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
16
17
  .config({ schemaGlob: "./src/**/*.graphql", serverAddress: "127.0.0.1:9000" })
17
- .pkgConf("crud")
18
- .config("config", "Path of your `crud.config.ts`, default: `./crud.config.ts`", configPath => JSON.parse(fs_1.default.readFileSync(configPath, "utf-8"))).argv;
19
- const schemaGlob = argv.schemaGlob;
20
- const serverAddress = argv.serverAddress;
18
+ .pkgConf("crud").argv;
19
+ let schemaGlob = argv.schemaGlob;
20
+ let serverAddress = argv.serverAddress;
21
+ if (process.env.CRUD_SCHEMA_GLOB) {
22
+ schemaGlob = process.env.CRUD_SCHEMA_GLOB;
23
+ }
24
+ if (process.env.CRUD_SERVER_ADDRESS) {
25
+ serverAddress = process.env.CRUD_SERVER_ADDRESS;
26
+ }
21
27
  const schema = await (0, load_1.loadSchema)(`${schemaGlob}`, {
22
28
  loaders: [new graphql_file_loader_1.GraphQLFileLoader()],
23
29
  });
@@ -3,4 +3,5 @@ export interface ClientConfig {
3
3
  keepaliveInterval?: number;
4
4
  keepaliveTimeout?: number;
5
5
  }
6
- export declare const useConfigDefaults: (config: ClientConfig) => Required<ClientConfig>;
6
+ export declare const getEnvConfig: () => ClientConfig;
7
+ export declare const useConfigDefaults: (config?: ClientConfig) => Required<ClientConfig>;
@@ -1,8 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useConfigDefaults = void 0;
3
+ exports.useConfigDefaults = exports.getEnvConfig = void 0;
4
+ const dotenv_1 = require("dotenv");
5
+ const getEnvConfig = () => {
6
+ var _a;
7
+ (0, dotenv_1.config)();
8
+ const serverAddress = (_a = process.env.CRUD_SERVER_ADDRESS) !== null && _a !== void 0 ? _a : "";
9
+ let keepaliveInterval;
10
+ let keepaliveTimeout;
11
+ const keepaliveIntervalString = process.env.CRUD_CONNECTION_KEEPALIVE_INTERVAL;
12
+ const keepaliveTimeoutString = process.env.CRUD_CONNECTION_KEEPALIVE_INTERVAL;
13
+ if (keepaliveIntervalString) {
14
+ keepaliveInterval = parseInt(keepaliveIntervalString, 10);
15
+ }
16
+ if (keepaliveTimeoutString) {
17
+ keepaliveTimeout = parseInt(keepaliveTimeoutString, 10);
18
+ }
19
+ return {
20
+ serverAddress,
21
+ keepaliveInterval,
22
+ keepaliveTimeout,
23
+ };
24
+ };
25
+ exports.getEnvConfig = getEnvConfig;
4
26
  const useConfigDefaults = (config) => {
5
27
  var _a, _b;
28
+ if (!config) {
29
+ config = (0, exports.getEnvConfig)();
30
+ }
31
+ console.log(config);
6
32
  return {
7
33
  serverAddress: config.serverAddress,
8
34
  keepaliveTimeout: (_a = config.keepaliveTimeout) !== null && _a !== void 0 ? _a : 3 * 1000,
@@ -6,8 +6,8 @@ export interface DeliveryClient {
6
6
  create: (tenantId: string, type: string, data: Record<string, any>) => Promise<CreatedCrudData>;
7
7
  update: (tenantId: string, type: string, id: string, data: Record<string, any>) => Promise<void>;
8
8
  delete: (tenantId: string, type: string, id: string) => Promise<void>;
9
- getData: (tenantId: string, type: string, id: string) => Promise<GetCrudData | null>;
9
+ getData: (tenantId: string, type: string, id: string, returnEmptyDataIfNotFound?: boolean) => Promise<GetCrudData | null>;
10
10
  getDataList: (tenantId: string, type: string, limit?: number, page?: number) => Promise<GetCrudDataList | null>;
11
11
  close: () => Promise<void>;
12
12
  }
13
- export declare const newDeliveryClient: (config: ClientConfig) => Promise<DeliveryClient>;
13
+ export declare const newDeliveryClient: (config?: ClientConfig) => Promise<DeliveryClient>;
@@ -25,8 +25,8 @@ const newDeliveryClient = async (config) => {
25
25
  const deleter = async (tenantId, type, id) => {
26
26
  return await (0, delete_1.deleteCrudData)(tenantId, type, id, serviceClient);
27
27
  };
28
- const getData = async (tenantId, type, id) => {
29
- return await (0, getData_1.getCrudData)(tenantId, type, id, serviceClient);
28
+ const getData = async (tenantId, type, id, returnEmptyDataIfNotFound = false) => {
29
+ return await (0, getData_1.getCrudData)(tenantId, type, id, returnEmptyDataIfNotFound, serviceClient);
30
30
  };
31
31
  const getDataList = async (tenantId, type, limit = 0, page = 1) => {
32
32
  return await (0, getDataList_1.getCrudDataList)(tenantId, type, limit, page, serviceClient);
@@ -1,3 +1,3 @@
1
1
  import { DeliveryServiceClient } from "@fraym/crud-proto";
2
2
  export declare type GetCrudData = Record<string, any>;
3
- export declare const getCrudData: (tenantId: string, type: string, id: string, serviceClient: DeliveryServiceClient) => Promise<GetCrudData | null>;
3
+ export declare const getCrudData: (tenantId: string, type: string, id: string, returnEmptyDataIfNotFound: boolean, serviceClient: DeliveryServiceClient) => Promise<GetCrudData | null>;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCrudData = void 0;
4
- const getCrudData = async (tenantId, type, id, serviceClient) => {
4
+ const getCrudData = async (tenantId, type, id, returnEmptyDataIfNotFound, serviceClient) => {
5
5
  return new Promise((resolve, reject) => {
6
6
  serviceClient.getEntries({
7
7
  tenantId,
@@ -9,6 +9,7 @@ const getCrudData = async (tenantId, type, id, serviceClient) => {
9
9
  id,
10
10
  limit: 0,
11
11
  page: 0,
12
+ returnEmptyDataIfNotFound,
12
13
  }, (error, response) => {
13
14
  if (error) {
14
15
  reject(error.message);
@@ -9,6 +9,7 @@ const getCrudDataList = async (tenantId, type, limit, page, serviceClient) => {
9
9
  id: "",
10
10
  limit,
11
11
  page,
12
+ returnEmptyDataIfNotFound: false,
12
13
  }, (error, response) => {
13
14
  if (error) {
14
15
  reject(error.message);
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from "./delivery/client";
2
2
  export * from "./management/client";
3
- export { ClientConfig } from "./config/config";
3
+ export { ClientConfig, getEnvConfig } from "./config/config";
package/dist/index.js CHANGED
@@ -14,5 +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.getEnvConfig = void 0;
17
18
  __exportStar(require("./delivery/client"), exports);
18
19
  __exportStar(require("./management/client"), exports);
20
+ var config_1 = require("./config/config");
21
+ Object.defineProperty(exports, "getEnvConfig", { enumerable: true, get: function () { return config_1.getEnvConfig; } });
@@ -6,4 +6,4 @@ export interface ManagementClient {
6
6
  getAllTypes: () => Promise<string[]>;
7
7
  close: () => Promise<void>;
8
8
  }
9
- export declare const newManagementClient: (config: ClientConfig) => Promise<ManagementClient>;
9
+ export declare const newManagementClient: (config?: ClientConfig) => Promise<ManagementClient>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/crud",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "license": "UNLICENSED",
5
5
  "homepage": "https://github.com/fraym/crud-nodejs",
6
6
  "repository": {
@@ -28,10 +28,11 @@
28
28
  "crud": "dist/cmd/crud.js"
29
29
  },
30
30
  "dependencies": {
31
- "@fraym/crud-proto": "^1.0.0-alpha.5",
31
+ "@fraym/crud-proto": "^1.0.0-alpha.6",
32
32
  "@graphql-tools/graphql-file-loader": "^7.5.11",
33
33
  "@graphql-tools/load": "^7.8.6",
34
34
  "@grpc/grpc-js": "^1.7.2",
35
+ "dotenv": "^16.0.3",
35
36
  "fs": "^0.0.1-security",
36
37
  "graphql": "^16.6.0",
37
38
  "yargs": "^17.6.2"