graphile-i18n 0.0.2 → 0.0.3

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/main/plugin.js CHANGED
@@ -41,12 +41,18 @@ var LangPlugin = function LangPlugin(builder, options) {
41
41
  });
42
42
  var tablesWithLanguageTablesIdInfo = introspection["class"].filter(function (table) {
43
43
  return table.tags.hasOwnProperty('i18n');
44
- }).map(function (table) {
45
- return {
46
- identifier: table.primaryKeyConstraint.keyAttributes[0].name,
47
- idType: table.primaryKeyConstraint.keyAttributes[0].type.name
48
- };
49
- });
44
+ }).reduce(function (m, table) {
45
+ var _table$primaryKeyCons, _table$primaryKeyCons2;
46
+
47
+ if ((_table$primaryKeyCons = table.primaryKeyConstraint) !== null && _table$primaryKeyCons !== void 0 && (_table$primaryKeyCons2 = _table$primaryKeyCons.keyAttributes) !== null && _table$primaryKeyCons2 !== void 0 && _table$primaryKeyCons2[0]) {
48
+ m[table.tags.i18n] = {
49
+ identifier: table.primaryKeyConstraint.keyAttributes[0].name,
50
+ idType: table.primaryKeyConstraint.keyAttributes[0].type.name
51
+ };
52
+ }
53
+
54
+ return m;
55
+ }, {});
50
56
  var languageVariationTables = tablesWithLanguageTables.map(function (table) {
51
57
  return introspection["class"].find(function (t) {
52
58
  return t.name === table.tags.i18n && t.namespaceName === table.namespaceName;
@@ -58,11 +64,11 @@ var LangPlugin = function LangPlugin(builder, options) {
58
64
  i18nTables[table.tags.i18n] = {
59
65
  table: table.name,
60
66
  key: null,
61
- // action_id
67
+ // fk_id
62
68
  connection: inflection.connection(inflection.tableType(table)),
63
69
  attrs: {},
64
70
  fields: {},
65
- keyInfo: tablesWithLanguageTablesIdInfo[i]
71
+ keyInfo: tablesWithLanguageTablesIdInfo[table.tags.i18n]
66
72
  };
67
73
  tables[table.name] = table.tags.i18n;
68
74
  });
@@ -72,12 +78,22 @@ var LangPlugin = function LangPlugin(builder, options) {
72
78
  }).filter(function (c) {
73
79
  return c.foreignClass.name === i18nTables[table.name].table;
74
80
  });
75
- if (foreignConstraintsThatMatter.length !== 1) throw new Error('lang table only supports one foreign key to parent table');
76
- if (foreignConstraintsThatMatter[0].keyAttributes.length !== 1) throw new Error('lang table only supports one non compound foreign key to parent table');
81
+
82
+ if (foreignConstraintsThatMatter.length !== 1) {
83
+ // not sure why, but sometimes a version of the table appears in the introspection that doesn't have keys...
84
+ return;
85
+ }
86
+
87
+ if (foreignConstraintsThatMatter[0].keyAttributes.length !== 1) {
88
+ // not sure why, but sometimes a version of the table appears in the introspection that doesn't have keys...
89
+ return;
90
+ }
91
+
77
92
  i18nTables[table.name].key = foreignConstraintsThatMatter[0].keyAttributes[0].name;
78
93
  var _i18nTables$table$nam = i18nTables[table.name].keyInfo,
79
94
  identifier = _i18nTables$table$nam.identifier,
80
95
  idType = _i18nTables$table$nam.idType;
96
+ if (!identifier) return;
81
97
  table.attributes.forEach(function (attr) {
82
98
  if ([langPluginLanguageCodeColumn, identifier].includes(attr.name)) return;
83
99
 
package/module/plugin.js CHANGED
@@ -16,12 +16,16 @@ export const LangPlugin = (builder, options) => {
16
16
  const introspection = build.pgIntrospectionResultsByKind;
17
17
  const inflection = build.inflection;
18
18
  const tablesWithLanguageTables = introspection.class.filter(table => table.tags.hasOwnProperty('i18n'));
19
- const tablesWithLanguageTablesIdInfo = introspection.class.filter(table => table.tags.hasOwnProperty('i18n')).map(table => {
20
- return {
21
- identifier: table.primaryKeyConstraint.keyAttributes[0].name,
22
- idType: table.primaryKeyConstraint.keyAttributes[0].type.name
23
- };
24
- });
19
+ const tablesWithLanguageTablesIdInfo = introspection.class.filter(table => table.tags.hasOwnProperty('i18n')).reduce((m, table) => {
20
+ if (table.primaryKeyConstraint?.keyAttributes?.[0]) {
21
+ m[table.tags.i18n] = {
22
+ identifier: table.primaryKeyConstraint.keyAttributes[0].name,
23
+ idType: table.primaryKeyConstraint.keyAttributes[0].type.name
24
+ };
25
+ }
26
+
27
+ return m;
28
+ }, {});
25
29
  const languageVariationTables = tablesWithLanguageTables.map(table => introspection.class.find(t => t.name === table.tags.i18n && t.namespaceName === table.namespaceName));
26
30
  const i18nTables = {};
27
31
  const tables = {};
@@ -29,23 +33,33 @@ export const LangPlugin = (builder, options) => {
29
33
  i18nTables[table.tags.i18n] = {
30
34
  table: table.name,
31
35
  key: null,
32
- // action_id
36
+ // fk_id
33
37
  connection: inflection.connection(inflection.tableType(table)),
34
38
  attrs: {},
35
39
  fields: {},
36
- keyInfo: tablesWithLanguageTablesIdInfo[i]
40
+ keyInfo: tablesWithLanguageTablesIdInfo[table.tags.i18n]
37
41
  };
38
42
  tables[table.name] = table.tags.i18n;
39
43
  });
40
44
  languageVariationTables.forEach(table => {
41
45
  const foreignConstraintsThatMatter = table.constraints.filter(c => c.type === 'f').filter(c => c.foreignClass.name === i18nTables[table.name].table);
42
- if (foreignConstraintsThatMatter.length !== 1) throw new Error('lang table only supports one foreign key to parent table');
43
- if (foreignConstraintsThatMatter[0].keyAttributes.length !== 1) throw new Error('lang table only supports one non compound foreign key to parent table');
46
+
47
+ if (foreignConstraintsThatMatter.length !== 1) {
48
+ // not sure why, but sometimes a version of the table appears in the introspection that doesn't have keys...
49
+ return;
50
+ }
51
+
52
+ if (foreignConstraintsThatMatter[0].keyAttributes.length !== 1) {
53
+ // not sure why, but sometimes a version of the table appears in the introspection that doesn't have keys...
54
+ return;
55
+ }
56
+
44
57
  i18nTables[table.name].key = foreignConstraintsThatMatter[0].keyAttributes[0].name;
45
58
  const {
46
59
  identifier,
47
60
  idType
48
61
  } = i18nTables[table.name].keyInfo;
62
+ if (!identifier) return;
49
63
  table.attributes.forEach(attr => {
50
64
  if ([langPluginLanguageCodeColumn, identifier].includes(attr.name)) return;
51
65
 
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "graphile-i18n",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Graphile i18n plugin",
5
- "author": "true <true>",
5
+ "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/graphile-i18n#readme",
7
7
  "license": "SEE LICENSE IN LICENSE",
8
8
  "main": "main/index.js",