graphile-simple-inflector 0.1.1 → 0.1.4

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/index.js ADDED
@@ -0,0 +1,333 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PgSimpleInflector = void 0;
4
+ const fixCapitalisedPlural = (fn) => function capitalisedPlural(str) {
5
+ const original = fn.call(this, str);
6
+ return original.replace(/[0-9]S(?=[A-Z]|$)/g, (match) => match.toLowerCase());
7
+ };
8
+ const fixChangePlural = (fn) => function changePlural(str) {
9
+ const matches = str.match(/([A-Z]|_[a-z0-9])[a-z0-9]*_*$/);
10
+ const index = matches ? (matches.index ?? 0) + matches[1].length - 1 : 0;
11
+ const suffixMatches = str.match(/_*$/);
12
+ const suffixIndex = suffixMatches && suffixMatches.index !== undefined ? suffixMatches.index : str.length;
13
+ const prefix = str.slice(0, index);
14
+ const word = str.slice(index, suffixIndex);
15
+ const suffix = str.slice(suffixIndex);
16
+ return `${prefix}${fn.call(this, word)}${suffix}`;
17
+ };
18
+ const DEFAULT_NODE_ID = 'nodeId';
19
+ const PgSimpleInflector = (builder, { pgSimpleCollections, pgOmitListSuffix, pgSimplifyPatch = true, pgSimplifyAllRows = true, pgShortPk = true, pgSimplifyMultikeyRelations = true, pgSimplifyOppositeBaseNames = true, nodeIdFieldName = DEFAULT_NODE_ID, } = {}) => {
20
+ const hasConnections = pgSimpleCollections !== 'only';
21
+ const hasSimpleCollections = pgSimpleCollections === 'only' || pgSimpleCollections === 'both';
22
+ if (hasSimpleCollections &&
23
+ !hasConnections &&
24
+ pgOmitListSuffix !== true &&
25
+ pgOmitListSuffix !== false) {
26
+ // eslint-disable-next-line no-console
27
+ console.warn('You can simplify the inflector further by adding `{graphileBuildOptions: {pgOmitListSuffix: true}}` to the options passed to PostGraphile, however be aware that doing so will mean that later enabling relay connections will be a breaking change. To dismiss this message, set `pgOmitListSuffix` to false instead.');
28
+ }
29
+ const connectionSuffix = pgOmitListSuffix ? '-connection' : '';
30
+ const ConnectionSuffix = pgOmitListSuffix ? 'Connection' : '';
31
+ const listSuffix = pgOmitListSuffix ? '' : '-list';
32
+ const ListSuffix = pgOmitListSuffix ? '' : 'List';
33
+ builder.hook('inflection', (oldInflection) => {
34
+ const inflection = {
35
+ ...oldInflection,
36
+ /*
37
+ * This solves the issue with `blah-table1s` becoming `blahTable1S`
38
+ * (i.e. the capital S at the end) or `table1-connection becoming `Table1SConnection`
39
+ */
40
+ camelCase: fixCapitalisedPlural(oldInflection.camelCase),
41
+ upperCamelCase: fixCapitalisedPlural(oldInflection.upperCamelCase),
42
+ /*
43
+ * Pluralize/singularize only supports single words, so only run
44
+ * on the final segment of a name.
45
+ */
46
+ pluralize: fixChangePlural(oldInflection.pluralize),
47
+ singularize: fixChangePlural(oldInflection.singularize),
48
+ distinctPluralize(str) {
49
+ const singular = this.singularize(str);
50
+ const plural = this.pluralize(singular);
51
+ if (singular !== plural) {
52
+ return plural;
53
+ }
54
+ if (plural.endsWith('ch') ||
55
+ plural.endsWith('s') ||
56
+ plural.endsWith('sh') ||
57
+ plural.endsWith('x') ||
58
+ plural.endsWith('z')) {
59
+ return `${plural}es`;
60
+ }
61
+ else if (plural.endsWith('y')) {
62
+ return `${plural.slice(0, -1)}ies`;
63
+ }
64
+ else {
65
+ return `${plural}s`;
66
+ }
67
+ },
68
+ // Fix a naming bug
69
+ deletedNodeId(table) {
70
+ return this.camelCase(`deleted-${this.singularize(table.name)}-${nodeIdFieldName}`);
71
+ },
72
+ getBaseName(columnName) {
73
+ const matches = columnName.match(/^(.+?)(_row_id|_id|_uuid|_fk|_pk|RowId|Id|Uuid|UUID|Fk|Pk)$/);
74
+ if (matches) {
75
+ return matches[1];
76
+ }
77
+ return null;
78
+ },
79
+ baseNameMatches(baseName, otherName) {
80
+ const singularizedName = this.singularize(otherName);
81
+ return baseName === singularizedName;
82
+ },
83
+ baseNameMatchesAny(baseName, otherName) {
84
+ if (!baseName)
85
+ return false;
86
+ return this.singularize(baseName) === this.singularize(otherName);
87
+ },
88
+ /* This is a good method to override. */
89
+ getOppositeBaseName(baseName) {
90
+ return (pgSimplifyOppositeBaseNames &&
91
+ ({
92
+ /*
93
+ * Changes to this list are breaking changes and will require a
94
+ * major version update, so we need to group as many together as
95
+ * possible! Rather than sending a PR, please look for an open
96
+ * issue called something like "Add more opposites" (if there isn't
97
+ * one then please open it) and add your suggestions to the GitHub
98
+ * comments.
99
+ */
100
+ // NOTE: reason to be careful using this:
101
+ // field names to take into account this particular case (e.g. events with event.parent_id could not have parent OR child fields)
102
+ inviter: 'invitee',
103
+ parent: 'child',
104
+ child: 'parent',
105
+ owner: 'owned',
106
+ author: 'authored',
107
+ editor: 'edited',
108
+ reviewer: 'reviewed',
109
+ }[baseName ?? ''] || null));
110
+ },
111
+ getBaseNameFromKeys(detailedKeys) {
112
+ if (detailedKeys.length === 1) {
113
+ const key = detailedKeys[0];
114
+ const columnName = this._columnName(key);
115
+ return this.getBaseName?.(columnName) ?? null;
116
+ }
117
+ if (pgSimplifyMultikeyRelations) {
118
+ const columnNames = detailedKeys.map((key) => this._columnName(key));
119
+ const baseNames = columnNames.map((columnName) => this.getBaseName?.(columnName) ?? null);
120
+ // Check none are null
121
+ if (baseNames.every((n) => n)) {
122
+ return baseNames.join('-');
123
+ }
124
+ }
125
+ return null;
126
+ },
127
+ ...(pgSimplifyPatch
128
+ ? {
129
+ patchField() {
130
+ return 'patch';
131
+ },
132
+ }
133
+ : {}),
134
+ ...(pgSimplifyAllRows
135
+ ? {
136
+ allRows(table) {
137
+ return this.camelCase(this.distinctPluralize(this._singularizedTableName(table)) + connectionSuffix);
138
+ },
139
+ allRowsSimple(table) {
140
+ return this.camelCase(this.distinctPluralize(this._singularizedTableName(table)) + listSuffix);
141
+ },
142
+ }
143
+ : {}),
144
+ computedColumn(pseudoColumnName, proc) {
145
+ return proc.tags.fieldName
146
+ ? `${proc.tags.fieldName}${proc.returnsSet ? ConnectionSuffix : ''}`
147
+ : this.camelCase(pseudoColumnName + (proc.returnsSet ? connectionSuffix : ''));
148
+ },
149
+ computedColumnList(pseudoColumnName, proc) {
150
+ return proc.tags.fieldName
151
+ ? `${proc.tags.fieldName}${ListSuffix}`
152
+ : this.camelCase(pseudoColumnName + listSuffix);
153
+ },
154
+ singleRelationByKeys(detailedKeys, table, _foreignTable, constraint) {
155
+ if (constraint.tags.fieldName) {
156
+ return constraint.tags.fieldName;
157
+ }
158
+ const baseName = this.getBaseNameFromKeys(detailedKeys);
159
+ if (constraint.classId === constraint.foreignClassId) {
160
+ if (baseName && this.baseNameMatchesAny?.(baseName, table.name)) {
161
+ return oldInflection.singleRelationByKeys(detailedKeys, table, _foreignTable, constraint);
162
+ }
163
+ }
164
+ if (baseName) {
165
+ return this.camelCase(baseName);
166
+ }
167
+ if (this.baseNameMatches?.(baseName, table.name)) {
168
+ return this.camelCase(`${this._singularizedTableName(table)}`);
169
+ }
170
+ return oldInflection.singleRelationByKeys(detailedKeys, table, _foreignTable, constraint);
171
+ },
172
+ singleRelationByKeysBackwards(detailedKeys, table, foreignTable, constraint) {
173
+ if (constraint.tags.foreignSingleFieldName) {
174
+ return constraint.tags.foreignSingleFieldName;
175
+ }
176
+ if (constraint.tags.foreignFieldName) {
177
+ return constraint.tags.foreignFieldName;
178
+ }
179
+ const baseName = this.getBaseNameFromKeys(detailedKeys);
180
+ const oppositeBaseName = baseName && this.getOppositeBaseName?.(baseName);
181
+ if (oppositeBaseName) {
182
+ return this.camelCase(`${oppositeBaseName}-${this._singularizedTableName(table)}`);
183
+ }
184
+ if (baseName && this.baseNameMatches?.(baseName, foreignTable.name)) {
185
+ return this.camelCase(`${this._singularizedTableName(table)}`);
186
+ }
187
+ return oldInflection.singleRelationByKeysBackwards(detailedKeys, table, foreignTable, constraint);
188
+ },
189
+ _manyRelationByKeysBase(detailedKeys, table, foreignTable, constraint) {
190
+ const baseName = this.getBaseNameFromKeys(detailedKeys);
191
+ const oppositeBaseName = baseName && this.getOppositeBaseName?.(baseName);
192
+ if (constraint.classId === constraint.foreignClassId) {
193
+ if (baseName && this.baseNameMatches?.(baseName, table.name)) {
194
+ return null;
195
+ }
196
+ }
197
+ if (oppositeBaseName) {
198
+ return this.camelCase(`${oppositeBaseName}-${this.distinctPluralize(this._singularizedTableName(table))}`);
199
+ }
200
+ if (baseName && this.baseNameMatches?.(baseName, foreignTable.name)) {
201
+ return this.camelCase(`${this.distinctPluralize(this._singularizedTableName(table))}`);
202
+ }
203
+ return null;
204
+ },
205
+ manyRelationByKeys(detailedKeys, table, foreignTable, constraint) {
206
+ if (constraint.tags.foreignFieldName) {
207
+ if (constraint.tags.foreignSimpleFieldName) {
208
+ return constraint.tags.foreignFieldName;
209
+ }
210
+ else {
211
+ return `${constraint.tags.foreignFieldName}${ConnectionSuffix}`;
212
+ }
213
+ }
214
+ const base = this._manyRelationByKeysBase?.(detailedKeys, table, foreignTable, constraint);
215
+ if (base) {
216
+ return base + ConnectionSuffix;
217
+ }
218
+ return (oldInflection.manyRelationByKeys(detailedKeys, table, foreignTable, constraint) + ConnectionSuffix);
219
+ },
220
+ manyRelationByKeysSimple(detailedKeys, table, foreignTable, constraint) {
221
+ if (constraint.tags.foreignSimpleFieldName) {
222
+ return constraint.tags.foreignSimpleFieldName;
223
+ }
224
+ if (constraint.tags.foreignFieldName) {
225
+ return `${constraint.tags.foreignFieldName}${ListSuffix}`;
226
+ }
227
+ const base = this._manyRelationByKeysBase?.(detailedKeys, table, foreignTable, constraint);
228
+ if (base) {
229
+ return base + ListSuffix;
230
+ }
231
+ return (oldInflection.manyRelationByKeys(detailedKeys, table, foreignTable, constraint) + ListSuffix);
232
+ },
233
+ functionQueryName(proc) {
234
+ return this.camelCase(this._functionName(proc) + (proc.returnsSet ? connectionSuffix : ''));
235
+ },
236
+ functionQueryNameList(proc) {
237
+ return this.camelCase(this._functionName(proc) + listSuffix);
238
+ },
239
+ ...(pgShortPk
240
+ ? {
241
+ tableNode(table) {
242
+ return this.camelCase(`${this._singularizedTableName(table)}-by-${nodeIdFieldName}`);
243
+ },
244
+ rowByUniqueKeys(detailedKeys, table, constraint) {
245
+ if (constraint.tags.fieldName) {
246
+ return constraint.tags.fieldName;
247
+ }
248
+ if (constraint.type === 'p') {
249
+ // Primary key, shorten!
250
+ return this.camelCase(this._singularizedTableName(table));
251
+ }
252
+ else {
253
+ return this.camelCase(`${this._singularizedTableName(table)}-by-${detailedKeys
254
+ .map((key) => this.column(key))
255
+ .join('-and-')}`);
256
+ }
257
+ },
258
+ updateByKeys(detailedKeys, table, constraint) {
259
+ if (constraint.tags.updateFieldName) {
260
+ return constraint.tags.updateFieldName;
261
+ }
262
+ if (constraint.type === 'p') {
263
+ // Primary key, shorten!
264
+ return this.camelCase(`update-${this._singularizedTableName(table)}`);
265
+ }
266
+ else {
267
+ return this.camelCase(`update-${this._singularizedTableName(table)}-by-${detailedKeys
268
+ .map((key) => this.column(key))
269
+ .join('-and-')}`);
270
+ }
271
+ },
272
+ deleteByKeys(detailedKeys, table, constraint) {
273
+ if (constraint.tags.deleteFieldName) {
274
+ return constraint.tags.deleteFieldName;
275
+ }
276
+ if (constraint.type === 'p') {
277
+ // Primary key, shorten!
278
+ return this.camelCase(`delete-${this._singularizedTableName(table)}`);
279
+ }
280
+ else {
281
+ return this.camelCase(`delete-${this._singularizedTableName(table)}-by-${detailedKeys
282
+ .map((key) => this.column(key))
283
+ .join('-and-')}`);
284
+ }
285
+ },
286
+ updateByKeysInputType(detailedKeys, table, constraint) {
287
+ if (constraint.tags.updateFieldName) {
288
+ return this.upperCamelCase(`${constraint.tags.updateFieldName}-input`);
289
+ }
290
+ if (constraint.type === 'p') {
291
+ // Primary key, shorten!
292
+ return this.upperCamelCase(`update-${this._singularizedTableName(table)}-input`);
293
+ }
294
+ else {
295
+ return this.upperCamelCase(`update-${this._singularizedTableName(table)}-by-${detailedKeys
296
+ .map((key) => this.column(key))
297
+ .join('-and-')}-input`);
298
+ }
299
+ },
300
+ deleteByKeysInputType(detailedKeys, table, constraint) {
301
+ if (constraint.tags.deleteFieldName) {
302
+ return this.upperCamelCase(`${constraint.tags.deleteFieldName}-input`);
303
+ }
304
+ if (constraint.type === 'p') {
305
+ // Primary key, shorten!
306
+ return this.upperCamelCase(`delete-${this._singularizedTableName(table)}-input`);
307
+ }
308
+ else {
309
+ return this.upperCamelCase(`delete-${this._singularizedTableName(table)}-by-${detailedKeys
310
+ .map((key) => this.column(key))
311
+ .join('-and-')}-input`);
312
+ }
313
+ },
314
+ updateNode(table) {
315
+ return this.camelCase(`update-${this._singularizedTableName(table)}-by-${nodeIdFieldName}`);
316
+ },
317
+ deleteNode(table) {
318
+ return this.camelCase(`delete-${this._singularizedTableName(table)}-by-${nodeIdFieldName}`);
319
+ },
320
+ updateNodeInputType(table) {
321
+ return this.upperCamelCase(`update-${this._singularizedTableName(table)}-by-${nodeIdFieldName}-input`);
322
+ },
323
+ deleteNodeInputType(table) {
324
+ return this.upperCamelCase(`delete-${this._singularizedTableName(table)}-by-${nodeIdFieldName}-input`);
325
+ },
326
+ }
327
+ : {}),
328
+ };
329
+ return inflection;
330
+ });
331
+ };
332
+ exports.PgSimpleInflector = PgSimpleInflector;
333
+ exports.default = exports.PgSimpleInflector;
package/package.json CHANGED
@@ -1,36 +1,30 @@
1
1
  {
2
2
  "name": "graphile-simple-inflector",
3
- "version": "0.1.1",
4
- "description": "simple inflector",
3
+ "version": "0.1.4",
4
+ "description": "Simple inflector plugin for Graphile/PostGraphile",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
- "homepage": "https://github.com/pyramation/graphile-simple-inflector#readme",
6
+ "homepage": "https://github.com/launchql/launchql",
7
7
  "license": "SEE LICENSE IN LICENSE",
8
- "main": "main/index.js",
9
- "module": "module/index.js",
10
- "directories": {
11
- "lib": "src",
12
- "test": "__tests__"
13
- },
14
- "files": [
15
- "main",
16
- "module"
17
- ],
8
+ "main": "index.js",
9
+ "module": "esm/index.js",
10
+ "types": "index.d.ts",
18
11
  "scripts": {
19
- "build:main": "BABEL_ENV=production babel src --out-dir main --delete-dir-on-start",
20
- "build:module": "MODULE=true babel src --out-dir module --delete-dir-on-start",
21
- "build": "npm run build:module && npm run build:main",
22
- "prepublish": "npm run build",
23
- "lint": "eslint src --fix",
12
+ "clean": "makage clean",
13
+ "copy": "makage assets",
14
+ "prepack": "npm run build",
15
+ "build": "makage build",
16
+ "build:dev": "makage build --dev",
17
+ "lint": "eslint . --fix",
24
18
  "test": "jest",
25
- "test:watch": "jest --watch",
26
- "test:debug": "node --inspect node_modules/.bin/jest --runInBand"
19
+ "test:watch": "jest --watch"
27
20
  },
28
21
  "publishConfig": {
29
- "access": "public"
22
+ "access": "public",
23
+ "directory": "dist"
30
24
  },
31
25
  "repository": {
32
26
  "type": "git",
33
- "url": "https://github.com/pyramation/graphile-simple-inflector"
27
+ "url": "https://github.com/launchql/launchql"
34
28
  },
35
29
  "keywords": [
36
30
  "graphile",
@@ -38,37 +32,20 @@
38
32
  "postgres",
39
33
  "graphql",
40
34
  "inflection",
41
- "plugin"
35
+ "plugin",
36
+ "launchql"
42
37
  ],
43
38
  "bugs": {
44
- "url": "https://github.com/pyramation/graphile-simple-inflector/issues"
39
+ "url": "https://github.com/launchql/launchql/issues"
45
40
  },
46
41
  "devDependencies": {
47
- "@babel/cli": "7.8.4",
48
- "@babel/core": "7.8.4",
49
- "@babel/plugin-proposal-class-properties": "7.8.3",
50
- "@babel/plugin-proposal-export-default-from": "7.8.3",
51
- "@babel/plugin-proposal-object-rest-spread": "7.9.6",
52
- "@babel/plugin-transform-runtime": "7.9.6",
53
- "@babel/preset-env": "7.9.6",
54
- "@babel/preset-react": "7.9.4",
55
- "babel-core": "7.0.0-bridge.0",
56
- "babel-eslint": "10.0.3",
57
- "babel-jest": "25.1.0",
58
- "babel-plugin-macros": "2.8.0",
59
- "eslint": "6.8.0",
60
- "eslint-config-prettier": "^6.10.0",
61
- "eslint-config-react-app": "5.2.0",
62
- "eslint-plugin-prettier": "^3.1.2",
63
- "eslint-plugin-react": "^7.18.3",
64
- "eslint-plugin-react-hooks": "4.0.0",
65
- "graphile-test": "^0.1.0",
66
- "jest": "^24.5.0",
67
- "jest-in-case": "^1.0.2",
68
- "prettier": "^1.19.1",
69
- "regenerator-runtime": "^0.13.2"
42
+ "graphile-test": "^2.8.5",
43
+ "graphql-tag": "2.12.6",
44
+ "makage": "^0.1.6",
45
+ "pgsql-test": "^2.14.8"
70
46
  },
71
47
  "dependencies": {
72
- "@babel/runtime": "^7.4.2"
73
- }
48
+ "graphile-build": "^4.14.1"
49
+ },
50
+ "gitHead": "e4643c07b6a961b964bbb132308366fc549504a1"
74
51
  }