@fraym/crud 0.1.0 → 0.2.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
@@ -8,6 +8,23 @@ Client implementation in javascript for the CRUD service [streams](https://githu
8
8
  npm i @fraym/crud
9
9
  ```
10
10
 
11
+ ## CLI command
12
+
13
+ Use the `crud` cli command to automatically apply your crud schemas to the crud service.
14
+
15
+ The `--config ./path/crud.config.json` flag can be used to configure the pat to your config file.
16
+
17
+ Your type schemas have to live directly below the path that you specified in `schemaPath` of your config file.
18
+
19
+ ### CLI command config
20
+
21
+ ```json
22
+ {
23
+ "schemaPath": "./src/crud", // path to your crud schema files
24
+ "serverAddress": "127.0.0.1:9000" // address of the crud service
25
+ }
26
+ ```
27
+
11
28
  ## Usage
12
29
 
13
30
  ### Create the clients
@@ -17,7 +34,6 @@ delivery client:
17
34
  ```typescript
18
35
  const deliveryClient = await newDeliveryClient({
19
36
  serverAddress: "127.0.0.1:9000",
20
- groupId: "your-services-group-identifier",
21
37
  });
22
38
  ```
23
39
 
@@ -26,7 +42,6 @@ management client:
26
42
  ```typescript
27
43
  const managementClient = await newManagementClient({
28
44
  serverAddress: "127.0.0.1:9000",
29
- groupId: "your-services-group-identifier",
30
45
  });
31
46
  ```
32
47
 
@@ -0,0 +1,2 @@
1
+ #! /usr/bin/env node
2
+ export {};
@@ -0,0 +1,84 @@
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 fs_1 = __importDefault(require("fs"));
8
+ const yargs_1 = __importDefault(require("yargs/yargs"));
9
+ const helpers_1 = require("yargs/helpers");
10
+ const utilities_1 = require("graphql/utilities");
11
+ const kinds_1 = require("graphql/language/kinds");
12
+ const client_1 = require("../management/client");
13
+ const run = async () => {
14
+ const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
15
+ .config({ schemaPath: "./src", serverAddress: "127.0.0.1:9000" })
16
+ .pkgConf("crud")
17
+ .config("config", "Path of your `crud.config.ts`, default: `./crud.config.ts`", configPath => JSON.parse(fs_1.default.readFileSync(configPath, "utf-8"))).argv;
18
+ const schemaPath = argv.schemaPath;
19
+ const serverAddress = argv.serverAddress;
20
+ const schemas = getSchemas(schemaPath);
21
+ await migrateSchemas(schemas, serverAddress);
22
+ };
23
+ run();
24
+ const getSchemas = (schemaPath) => {
25
+ const schemaFiles = fs_1.default.readdirSync(schemaPath);
26
+ const schemas = {};
27
+ schemaFiles.forEach(fileName => {
28
+ const astSchema = (0, utilities_1.buildSchema)(fs_1.default.readFileSync(schemaPath + "/" + fileName, "utf-8"));
29
+ astSchema.toConfig().types.forEach(t => {
30
+ var _a;
31
+ if (!((_a = t.astNode) === null || _a === void 0 ? void 0 : _a.kind)) {
32
+ return;
33
+ }
34
+ const name = t.toString();
35
+ const typeSchema = (0, utilities_1.buildASTSchema)({
36
+ definitions: [t.astNode],
37
+ kind: kinds_1.Kind.DOCUMENT,
38
+ });
39
+ if (schemas[name]) {
40
+ throw new Error(`duplicate schema for CRUD type "${name}" detected, try renaming one of them as they have to be unique`);
41
+ }
42
+ schemas[name] = (0, utilities_1.printSchema)(typeSchema);
43
+ });
44
+ });
45
+ return schemas;
46
+ };
47
+ const migrateSchemas = async (schemas, serverAddress) => {
48
+ const managementClient = await (0, client_1.newManagementClient)({ serverAddress });
49
+ const existingTypeNames = await managementClient.getAllTypes();
50
+ let createSchema = "";
51
+ let updateSchema = "";
52
+ const typesToCreate = [];
53
+ const typesToUpdate = [];
54
+ const typesToRemove = [];
55
+ existingTypeNames.forEach(existingName => {
56
+ if (!schemas[existingName]) {
57
+ typesToRemove.push(existingName);
58
+ }
59
+ else {
60
+ typesToUpdate.push(existingName);
61
+ updateSchema += `\n${schemas[existingName]}`;
62
+ delete schemas[existingName];
63
+ }
64
+ });
65
+ Object.keys(schemas).forEach(newName => {
66
+ typesToCreate.push(newName);
67
+ createSchema += `\n${schemas[newName]}`;
68
+ });
69
+ if (typesToRemove.length > 0) {
70
+ console.log(`Removing ${typesToRemove.length} types: ${typesToRemove}...`);
71
+ await managementClient.removeTypes(typesToRemove);
72
+ console.log(`Removed ${typesToRemove.length} types`);
73
+ }
74
+ if (typesToUpdate.length > 0) {
75
+ console.log(`Updating ${typesToUpdate.length} types: ${typesToUpdate}...`);
76
+ await managementClient.updateTypes(updateSchema);
77
+ console.log(`Updated ${typesToUpdate.length} types`);
78
+ }
79
+ if (typesToCreate.length > 0) {
80
+ console.log(`Creating ${typesToCreate.length} types: ${typesToCreate}...`);
81
+ await managementClient.createTypes(createSchema);
82
+ console.log(`Created ${typesToCreate.length} types`);
83
+ }
84
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/crud",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "UNLICENSED",
5
5
  "homepage": "https://github.com/fraym/crud-nodejs",
6
6
  "repository": {
@@ -12,25 +12,34 @@
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",
15
+ "build": "npm run clean && tsc && chmod +x dist/cmd/crud.js",
16
16
  "clean": "rm -rf dist",
17
17
  "prepublishOnly": "npm test && npm run lint && npm run build",
18
- "preversion": "npm run lint"
18
+ "preversion": "npm run lint",
19
+ "cmd": "dist/cmd/crud.js",
20
+ "cmd2": "dist/cmd/crud.js --config .tst/crud.config.json"
19
21
  },
20
22
  "files": [
21
23
  "dist/**/*"
22
24
  ],
23
25
  "main": "dist/index.js",
24
26
  "types": "dist/index.d.ts",
27
+ "bin": {
28
+ "crud": "dist/cmd/crud.js"
29
+ },
25
30
  "dependencies": {
26
31
  "@fraym/crud-proto": "^1.0.0-alpha.5",
27
32
  "@grpc/grpc-js": "^1.7.2",
28
- "uuid": "^9.0.0"
33
+ "fs": "^0.0.1-security",
34
+ "graphql": "^16.6.0",
35
+ "yargs": "^17.6.2"
29
36
  },
30
37
  "devDependencies": {
31
38
  "@becklyn/prettier": "^1.0.2",
32
39
  "@types/uuid": "^8.3.4",
40
+ "@types/yargs": "^17.0.13",
33
41
  "prettier": "^2.7.1",
42
+ "uuid": "^9.0.0",
34
43
  "typescript": "^4.8.4"
35
44
  },
36
45
  "prettier": "@becklyn/prettier"