@fraym/crud 0.3.1 → 0.5.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
  });
@@ -28,18 +34,38 @@ run();
28
34
  const getTypeDefinition = (schema) => {
29
35
  const definitions = {};
30
36
  schema.toConfig().types.forEach(t => {
31
- var _a;
32
- if (((_a = t.astNode) === null || _a === void 0 ? void 0 : _a.kind) !== graphql_1.Kind.OBJECT_TYPE_DEFINITION || !(t instanceof graphql_1.GraphQLObjectType)) {
37
+ if (!(t instanceof graphql_1.GraphQLObjectType) && !(t instanceof graphql_1.GraphQLEnumType)) {
33
38
  return;
34
39
  }
35
40
  const name = t.toString();
36
41
  if (definitions[name]) {
37
42
  throw new Error(`duplicate definition for type "${name}" detected, try renaming one of them as they have to be uniquely named`);
38
43
  }
39
- definitions[name] = getTypeDefinitionFromGraphQLObjectType(t);
44
+ if (t instanceof graphql_1.GraphQLObjectType) {
45
+ definitions[name] = getTypeDefinitionFromGraphQLObjectType(t);
46
+ return;
47
+ }
48
+ if (t instanceof graphql_1.GraphQLEnumType) {
49
+ definitions[name] = getTypeDefinitionFromGraphQLEnumType(t);
50
+ return;
51
+ }
40
52
  });
41
53
  return definitions;
42
54
  };
55
+ const getTypeDefinitionFromGraphQLEnumType = (t) => {
56
+ var _a, _b;
57
+ const name = t.toString();
58
+ let enumValuesString = "";
59
+ (_b = (_a = t.astNode) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.forEach(value => {
60
+ enumValuesString += `\n\t${value.name.value}`;
61
+ });
62
+ const schema = `enum ${name} {${enumValuesString}\n}`;
63
+ return {
64
+ isCrudType: false,
65
+ nestedTypes: [],
66
+ schema,
67
+ };
68
+ };
43
69
  const getTypeDefinitionFromGraphQLObjectType = (t) => {
44
70
  var _a, _b, _c, _d, _e, _f, _g;
45
71
  const isCrudType = (_c = (((_a = t.astNode) === null || _a === void 0 ? void 0 : _a.directives) &&
@@ -80,7 +106,7 @@ const getFieldStringAndNestedTypes = (f) => {
80
106
  nestedTypes.push(nestedType);
81
107
  }
82
108
  return {
83
- str: `\n${f.name.value}: ${typeString}${directivesString}`,
109
+ str: `\n\t${f.name.value}: ${typeString}${directivesString}`,
84
110
  nestedTypes,
85
111
  };
86
112
  };
@@ -177,23 +203,24 @@ const migrateSchemas = async (definitions, serverAddress) => {
177
203
  typesToUpdate.push(existingName);
178
204
  updateSchema += `\n${definitions[existingName].schema}`;
179
205
  definitions[existingName].nestedTypes.forEach(nestedTypeName => {
180
- if (nestedTypesToUpdate.indexOf(nestedTypeName) !== -1) {
206
+ if (nestedTypesToUpdate.indexOf(nestedTypeName) !== -1 ||
207
+ (definitions[nestedTypeName] && definitions[nestedTypeName].isCrudType)) {
181
208
  return;
182
209
  }
183
210
  updateSchema += `\n${definitions[nestedTypeName].schema}`;
184
211
  nestedTypesToUpdate.push(nestedTypeName);
185
212
  });
186
- delete definitions[existingName];
187
213
  }
188
214
  });
189
215
  Object.keys(definitions).forEach(newName => {
190
- if (!definitions[newName].isCrudType) {
216
+ if (!definitions[newName].isCrudType || existingTypeNames.includes(newName)) {
191
217
  return;
192
218
  }
193
219
  typesToCreate.push(newName);
194
220
  createSchema += `\n${definitions[newName].schema}`;
195
221
  definitions[newName].nestedTypes.forEach(nestedTypeName => {
196
- if (nestedTypesToCreate.indexOf(nestedTypeName) !== -1) {
222
+ if (nestedTypesToCreate.indexOf(nestedTypeName) !== -1 ||
223
+ (definitions[nestedTypeName] && definitions[nestedTypeName].isCrudType)) {
197
224
  return;
198
225
  }
199
226
  createSchema += `\n${definitions[nestedTypeName].schema}`;
@@ -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,33 @@
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
+ }
6
31
  return {
7
32
  serverAddress: config.serverAddress,
8
33
  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.5.0",
4
4
  "license": "UNLICENSED",
5
5
  "homepage": "https://github.com/fraym/crud-nodejs",
6
6
  "repository": {
@@ -16,8 +16,7 @@
16
16
  "clean": "rm -rf dist",
17
17
  "prepublishOnly": "npm test && npm run lint && npm run build",
18
18
  "preversion": "npm run lint",
19
- "cmd": "dist/cmd/crud.js",
20
- "cmd2": "dist/cmd/crud.js --config .tst/crud.config.json"
19
+ "cmd": "dist/cmd/crud.js"
21
20
  },
22
21
  "files": [
23
22
  "dist/**/*"
@@ -28,10 +27,11 @@
28
27
  "crud": "dist/cmd/crud.js"
29
28
  },
30
29
  "dependencies": {
31
- "@fraym/crud-proto": "^1.0.0-alpha.5",
30
+ "@fraym/crud-proto": "^1.0.0-alpha.6",
32
31
  "@graphql-tools/graphql-file-loader": "^7.5.11",
33
32
  "@graphql-tools/load": "^7.8.6",
34
33
  "@grpc/grpc-js": "^1.7.2",
34
+ "dotenv": "^16.0.3",
35
35
  "fs": "^0.0.1-security",
36
36
  "graphql": "^16.6.0",
37
37
  "yargs": "^17.6.2"