oradm-to-gql 32.1.0 → 33.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oradm-to-gql",
3
- "version": "32.1.0",
3
+ "version": "33.2.0",
4
4
  "description": "Oracle Data Modeler CSV Export to Apollo GraphQL Endpoint Generator",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -51,7 +51,7 @@
51
51
  "graphql-server-express": "^0.8.5",
52
52
  "graphql-tools": "^1.0.0",
53
53
  "jest": "^23.2.0",
54
- "knex": "^0.13.0",
54
+ "knex": "^0.95.11",
55
55
  "pg": "^6.4.0",
56
56
  "require-from-string": "^1.2.1",
57
57
  "tg-client-query-builder": "^2.2.0"
@@ -1,32 +1,45 @@
1
- const { isNumber } = require('lodash');
2
- const queryResolver = require('tg-knex-query-resolver');
3
- const applyFilter = require('./applyFilter');
4
- const tableMap = require('../db/table-map.json');
1
+ const { isNumber } = require("lodash");
2
+ const queryResolver = require("tg-knex-query-resolver");
3
+ const applyFilter = require("./applyFilter");
4
+ const tableMap = require("../db/table-map.json");
5
5
 
6
- function buildQuery(qry, tableColumns, entityName, modelName, select, filter, sortBy, pageNumber, pageSize){
7
-
8
- qry = qry(tableMap[entityName].tableName).select(select);
9
-
10
- qry = applyFilter(qry, modelName, filter);
6
+ function buildQuery(
7
+ qry,
8
+ tableColumns,
9
+ entityName,
10
+ modelName,
11
+ select,
12
+ filter,
13
+ sortBy,
14
+ pageNumber,
15
+ pageSize
16
+ ) {
17
+ qry = qry(tableMap[entityName].tableName).select(select);
11
18
 
12
- if(sortBy){
13
- sortBy.forEach((fieldName) => {
14
- if(fieldName.indexOf('-') === 0){
15
- qry = qry.orderBy(tableColumns[fieldName.substr(1)], 'desc');
16
- }else{
17
- qry = qry.orderBy(tableColumns[fieldName], 'asc');
18
- }
19
- });
20
- }else{
21
- qry = qry.orderBy(tableMap[entityName].primaryKeyField, 'asc');
22
- }
23
-
24
- if(isNumber(pageNumber) && isNumber(pageSize)){
25
- let offset = (pageNumber - 1) * pageSize;
26
- qry = qry.limit(pageSize).offset(offset);
19
+ qry = applyFilter(qry, modelName, filter);
20
+
21
+ if (sortBy) {
22
+ sortBy.forEach((fieldName) => {
23
+ if (fieldName.indexOf("-") === 0) {
24
+ qry = qry.orderBy(tableColumns[fieldName.substr(1)], "desc");
25
+ } else {
26
+ qry = qry.orderBy(tableColumns[fieldName], "asc");
27
+ }
28
+ });
29
+ } else {
30
+ if (tableMap[entityName].attributes.nid) {
31
+ qry = qry.orderBy("nid", "asc");
32
+ } else {
33
+ qry = qry.orderBy(tableMap[entityName].primaryKeyField, "asc");
27
34
  }
35
+ }
36
+
37
+ if (isNumber(pageNumber) && isNumber(pageSize)) {
38
+ let offset = (pageNumber - 1) * pageSize;
39
+ qry = qry.limit(pageSize).offset(offset);
40
+ }
28
41
 
29
- return qry;
42
+ return qry;
30
43
  }
31
44
 
32
- module.exports = buildQuery;
45
+ module.exports = buildQuery;
@@ -1,7 +1,10 @@
1
1
  const _ = require('lodash');
2
2
  const maxLength = 25;
3
+
3
4
  function shortenName(name, targetLength){
4
5
  targetLength = targetLength || maxLength;
6
+ if (name.length <= targetLength) return name
7
+
5
8
  var words = _.snakeCase(name).split('_');
6
9
 
7
10
  var newWords = [];
@@ -1,4 +1,5 @@
1
1
  const _ = require("lodash");
2
+ const clone = require("../graphql-generator/clone");
2
3
 
3
4
  module.exports = function extendTableMap(tableMap, opts) {
4
5
  opts = opts || {};
@@ -10,6 +11,10 @@ module.exports = function extendTableMap(tableMap, opts) {
10
11
  tableMap = tableMapExtender(tableMap, opts);
11
12
  }
12
13
 
14
+ if (opts.useUUIDs) {
15
+ tableMap = convertNumericIdsToUUIDs(tableMap, opts);
16
+ }
17
+
13
18
  return tableMap;
14
19
  };
15
20
 
@@ -26,7 +31,7 @@ function addCidFields(tableMap, opts) {
26
31
  scale: "",
27
32
  nullable: true,
28
33
  unique: true,
29
- columnName: "cid"
34
+ columnName: "cid",
30
35
  };
31
36
  } else {
32
37
  throw new Error(
@@ -35,3 +40,40 @@ function addCidFields(tableMap, opts) {
35
40
  }
36
41
  });
37
42
  }
43
+ function convertNumericIdsToUUIDs(tableMap, opts) {
44
+ _.each(tableMap, (table, tableName) => {
45
+ let nid;
46
+ _.each(table.attributes, (attr, attrName) => {
47
+ if (
48
+ (attr.isPrimaryKey || attr.isForeignKey) &&
49
+ attr.dataType === "NUMERIC"
50
+ ) {
51
+ if (attr.isPrimaryKey) {
52
+ nid = clone(attr);
53
+ nid.isPrimaryKey = false;
54
+ nid.nullable = true;
55
+ attr.defaultRawValue = "gen_random_uuid()";
56
+ delete attr.defaultValue;
57
+ delete attr.autoIncrement;
58
+ }
59
+ attr.dataType = "UUID";
60
+ }
61
+ });
62
+ if (nid) {
63
+ table.attributes["nid"] = nid;
64
+ table.attributes["legacy_id"] = {
65
+ name: "legacy_id",
66
+ dataType: "NUMERIC",
67
+ isPrimaryKey: false,
68
+ isForeignKey: false,
69
+ size: "",
70
+ precision: "",
71
+ scale: "",
72
+ nullable: true,
73
+ defaultValue: "",
74
+ columnName: "legacy_id",
75
+ };
76
+ }
77
+ });
78
+ return tableMap;
79
+ }
@@ -52,6 +52,17 @@ module.exports = function (
52
52
  views: [],
53
53
  cleanSchema: getCleanSchemaCommand(dialect, schemaName),
54
54
  cleanCommonSchema: getCleanSchemaCommand(dialect, commonSchemaName),
55
+ extensions: [
56
+ {
57
+ cmd: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA ?? ",
58
+ isCommon: true,
59
+ bindings: [commonSchemaName],
60
+ },
61
+ {
62
+ cmd: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA ?? ",
63
+ bindings: [schemaName],
64
+ },
65
+ ],
55
66
  };
56
67
 
57
68
  _.each(modelSpec, (modelDef, modelName) => {
@@ -88,6 +99,8 @@ module.exports = function (
88
99
  colSpec.unique = attrDef.unique;
89
100
  if (attrDef.defaultValue) {
90
101
  colSpec.defaultValue = attrDef.defaultValue;
102
+ } else if (attrDef.defaultRawValue) {
103
+ colSpec.defaultRawValue = attrDef.defaultRawValue;
91
104
  }
92
105
  tableSpec.columns.push(colSpec);
93
106
 
@@ -1,13 +1,17 @@
1
-
2
- module.exports = function getCleanSchemaCommand(dialect, schema){
3
- if(dialect === 'postgres'){
4
- return [
5
- { cmd: "DROP SCHEMA IF EXISTS ?? CASCADE", bindings: [schema]},
6
- { cmd: "CREATE SCHEMA ?? ", bindings: [schema]},
7
- ];
8
- }else if(dialect === 'oracle'){
9
- return [
10
- { cmd: `
1
+ module.exports = function getCleanSchemaCommand(dialect, schema) {
2
+ if (dialect === "postgres") {
3
+ return [
4
+ { cmd: "DROP SCHEMA IF EXISTS ?? CASCADE", bindings: [schema] },
5
+ { cmd: "CREATE SCHEMA ?? ", bindings: [schema] },
6
+ {
7
+ cmd: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA ?? ",
8
+ bindings: [schema],
9
+ },
10
+ ];
11
+ } else if (dialect === "oracle") {
12
+ return [
13
+ {
14
+ cmd: `
11
15
  BEGIN
12
16
  FOR tableObj in (
13
17
  select 'drop table "' || object_name || '" cascade constraints purge ' str
@@ -30,10 +34,11 @@ module.exports = function getCleanSchemaCommand(dialect, schema){
30
34
  EXECUTE IMMEDIATE ('' || userObj.str);
31
35
  END LOOP;
32
36
 
33
- END;`,
34
- bindings: [schema, schema]}
35
- ];
36
- }
37
+ END;`,
38
+ bindings: [schema, schema],
39
+ },
40
+ ];
41
+ }
37
42
 
38
- throw new Error("Unsupported dialect " + dialect);
39
- }
43
+ throw new Error("Unsupported dialect " + dialect);
44
+ };
@@ -8,15 +8,15 @@ const typeMap = {
8
8
  TIMESTAMP: convertTimestamp,
9
9
  INTEGER: convertInteger,
10
10
  BOOLEAN: convertBoolean,
11
- CLOB: convertVarchar
11
+ CLOB: convertVarchar,
12
+ UUID: convertUUID,
12
13
  };
13
14
 
14
-
15
15
  function convertVarchar(attrDef, attrName, dialect) {
16
16
  var size = Number(attrDef.size || 400);
17
17
  return {
18
18
  type: "string",
19
- typeArgs: [attrName, size]
19
+ typeArgs: [attrName, size],
20
20
  };
21
21
  }
22
22
 
@@ -26,63 +26,70 @@ function convertNumeric(attrDef, attrName, dialect) {
26
26
 
27
27
  return {
28
28
  type: "specificType",
29
- typeArgs: [attrName, "NUMERIC"]
29
+ typeArgs: [attrName, "NUMERIC"],
30
30
  };
31
31
  }
32
32
  if (dialect === "oracle") {
33
33
  return {
34
34
  type: "specificType",
35
- typeArgs: [attrName, "NUMBER"]
35
+ typeArgs: [attrName, "NUMBER"],
36
36
  };
37
37
  }
38
38
  return {
39
39
  type: "decimal",
40
- typeArgs: [attrName]
40
+ typeArgs: [attrName],
41
41
  };
42
42
  }
43
43
  function convertOradoc(attrDef, attrName, dialect) {
44
44
  if (dialect === "oracle") {
45
45
  return {
46
46
  type: "specificType",
47
- typeArgs: [attrName, "CLOB"]
47
+ typeArgs: [attrName, "CLOB"],
48
48
  };
49
49
  }
50
50
  return {
51
51
  type: "json",
52
- typeArgs: [attrName]
52
+ typeArgs: [attrName],
53
53
  };
54
54
  }
55
55
  function convertText(attrDef, attrName, dialect) {
56
56
  if (dialect === "oracle") {
57
57
  return {
58
58
  type: "specificType",
59
- typeArgs: [attrName, "CLOB"]
59
+ typeArgs: [attrName, "CLOB"],
60
60
  };
61
61
  }
62
62
  return {
63
63
  type: "text",
64
- typeArgs: [attrName]
64
+ typeArgs: [attrName],
65
65
  };
66
66
  }
67
67
 
68
68
  function convertTimestamp(attrDef, attrName, dialect) {
69
69
  return {
70
70
  type: "timestamp",
71
- typeArgs: [attrName]
71
+ typeArgs: [attrName],
72
72
  };
73
73
  }
74
74
 
75
75
  function convertInteger(attrDef, attrName, dialect) {
76
76
  return {
77
77
  type: "integer",
78
- typeArgs: [attrName]
78
+ typeArgs: [attrName],
79
79
  };
80
80
  }
81
81
 
82
82
  function convertBoolean(attrDef, attrName, dialect) {
83
83
  return {
84
84
  type: "boolean",
85
- typeArgs: [attrName]
85
+ typeArgs: [attrName],
86
+ };
87
+ }
88
+
89
+ function convertUUID(attrDef, attrName, dialect) {
90
+ return {
91
+ type: "varchar",
92
+ typeArgs: [attrName, 500],
86
93
  };
87
94
  }
88
95
 
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const Promise = require("bluebird");
2
+ const { shortenName } = require("./datamodel-csv-parser/Utils");
2
3
  const {
3
4
  loadResolvers,
4
5
  loadMockResolvers,
@@ -25,6 +26,7 @@ module.exports = (function () {
25
26
  truncateAndSeedDatabase,
26
27
  knexTools,
27
28
  graphqlGenerators,
29
+ shortenName,
28
30
  };
29
31
 
30
32
  function mockPromise() {
@@ -5,6 +5,7 @@ const applySchemaRefsAndConstraints = require("./applySchemaRefsAndConstraints")
5
5
  const applySchemaTriggers = require("./applySchemaTriggers");
6
6
  const applyViews = require("./applyViews");
7
7
  const runInitSql = require("./runInitSql");
8
+ const createExtensions = require("./createExtensions");
8
9
 
9
10
  async function applySchema(db, schemaSpec, initData, options = {}, appConfig) {
10
11
  let {
@@ -43,6 +44,9 @@ async function applySchema(db, schemaSpec, initData, options = {}, appConfig) {
43
44
  }
44
45
 
45
46
  if (!opts.skipTables) {
47
+ log(`[ORADM-APPLY-SCHEMA] Creating extensions`);
48
+ await createExtensions(db, schemaSpec, opts);
49
+
46
50
  log(`[ORADM-APPLY-SCHEMA] Creating tables and functions`);
47
51
  //clean existing schema and create tables and functions
48
52
  await applySchemaTablesAndFunctions(db, schemaSpec, opts, appConfig);
@@ -0,0 +1,23 @@
1
+ const Promise = require("bluebird");
2
+
3
+ function createExtensions(db, schemaSpec, opts = {}) {
4
+ const { logDebug = () => {}, commonSchema, moduleSchema } = opts;
5
+
6
+ db.logSQL(`Creating extensions`);
7
+ return Promise.each(schemaSpec.extensions, (cmdDef) => {
8
+ db.logSQL(cmdDef);
9
+ if (cmdDef.isCommon) {
10
+ if (commonSchema) {
11
+ logDebug(`[ORADM-FUNCTION] Creating common extension`, cmdDef);
12
+ return db.raw(cmdDef.cmd, cmdDef.bindings);
13
+ }
14
+ } else {
15
+ if (moduleSchema) {
16
+ logDebug(`[ORADM-FUNCTION] Creating module extension`, cmdDef);
17
+ return db.raw(cmdDef.cmd, cmdDef.bindings);
18
+ }
19
+ }
20
+ });
21
+ }
22
+
23
+ module.exports = createExtensions;
@@ -29,14 +29,16 @@ function createTables(db, tableSpecs, opts = {}) {
29
29
 
30
30
  logDebug("[ORADM-TABLE] Creating Column " + colSpec.name);
31
31
  logDebug(colSpec);
32
- logDebug(table[colSpec.type].apply);
32
+ // logDebug(table[colSpec.type].apply);
33
33
  if (colSpec.increments) {
34
- col = table.increments(colSpec.name);
34
+ col = table.increments(colSpec.name, {
35
+ primaryKey: colSpec.primaryKey,
36
+ });
35
37
  } else {
36
38
  col = table[colSpec.type].apply(self, colSpec.typeArgs);
37
39
  }
38
40
 
39
- if (colSpec.primaryKey) {
41
+ if (colSpec.primaryKey && !colSpec.increments) {
40
42
  col = col.primary();
41
43
  }
42
44
 
@@ -53,6 +55,8 @@ function createTables(db, tableSpecs, opts = {}) {
53
55
  }
54
56
  }
55
57
  col.notNullable().defaultTo(defaultValue);
58
+ } else if (colSpec.defaultRawValue) {
59
+ col.notNullable().defaultTo(db.raw(colSpec.defaultRawValue));
56
60
  }
57
61
  });
58
62
  tableSpec.indexes.forEach((indexSpec) => {