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/module/index.js DELETED
@@ -1,384 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
-
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
4
-
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
6
-
7
- // https://raw.githubusercontent.com/graphile/pg-simplify-inflector/master/index.js
8
- function fixCapitalisedPlural(fn) {
9
- return function (str) {
10
- const original = fn.call(this, str);
11
- return original.replace(/[0-9]S(?=[A-Z]|$)/g, match => match.toLowerCase());
12
- };
13
- }
14
-
15
- function fixChangePlural(fn) {
16
- return function (str) {
17
- const matches = str.match(/([A-Z]|_[a-z0-9])[a-z0-9]*_*$/);
18
- const index = matches ? matches.index + matches[1].length - 1 : 0;
19
- const suffixMatches = str.match(/_*$/);
20
- const suffixIndex = suffixMatches.index;
21
- const prefix = str.substr(0, index);
22
- const word = str.substr(index, suffixIndex - index);
23
- const suffix = str.substr(suffixIndex);
24
- return `${prefix}${fn.call(this, word)}${suffix}`;
25
- };
26
- }
27
-
28
- export function PgSimpleInflector(builder, {
29
- pgSimpleCollections,
30
- pgOmitListSuffix,
31
- pgSimplifyPatch = true,
32
- pgSimplifyAllRows = true,
33
- pgShortPk = true,
34
- pgSimplifyMultikeyRelations = true,
35
- pgSimplifyOppositeBaseNames = true,
36
- nodeIdFieldName = 'nodeId'
37
- }) {
38
- const hasConnections = pgSimpleCollections !== 'only';
39
- const hasSimpleCollections = pgSimpleCollections === 'only' || pgSimpleCollections === 'both';
40
-
41
- if (hasSimpleCollections && !hasConnections && pgOmitListSuffix !== true && pgOmitListSuffix !== false) {
42
- // eslint-disable-next-line no-console
43
- 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.');
44
- }
45
-
46
- const connectionSuffix = pgOmitListSuffix ? '-connection' : '';
47
- const ConnectionSuffix = pgOmitListSuffix ? 'Connection' : '';
48
- const listSuffix = pgOmitListSuffix ? '' : '-list';
49
- const ListSuffix = pgOmitListSuffix ? '' : 'List';
50
- builder.hook('inflection', oldInflection => {
51
- return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, oldInflection), {}, {
52
- /*
53
- * This solves the issue with `blah-table1s` becoming `blahTable1S`
54
- * (i.e. the capital S at the end) or `table1-connection becoming `Table1SConnection`
55
- */
56
- camelCase: fixCapitalisedPlural(oldInflection.camelCase),
57
- upperCamelCase: fixCapitalisedPlural(oldInflection.upperCamelCase),
58
-
59
- /*
60
- * Pluralize/singularize only supports single words, so only run
61
- * on the final segment of a name.
62
- */
63
- pluralize: fixChangePlural(oldInflection.pluralize),
64
- singularize: fixChangePlural(oldInflection.singularize),
65
-
66
- distinctPluralize(str) {
67
- const singular = this.singularize(str);
68
- const plural = this.pluralize(singular);
69
-
70
- if (singular !== plural) {
71
- return plural;
72
- }
73
-
74
- if (plural.endsWith('ch') || plural.endsWith('s') || plural.endsWith('sh') || plural.endsWith('x') || plural.endsWith('z')) {
75
- return plural + 'es';
76
- } else if (plural.endsWith('y')) {
77
- return plural.slice(0, -1) + 'ies';
78
- } else {
79
- return plural + 's';
80
- }
81
- },
82
-
83
- // Fix a naming bug
84
- deletedNodeId(table) {
85
- return this.camelCase(`deleted-${this.singularize(table.name)}-${nodeIdFieldName}`);
86
- },
87
-
88
- getBaseName(columnName) {
89
- const matches = columnName.match(/^(.+?)(_row_id|_id|_uuid|_fk|_pk|RowId|Id|Uuid|UUID|Fk|Pk)$/);
90
-
91
- if (matches) {
92
- return matches[1];
93
- }
94
-
95
- return null;
96
- },
97
-
98
- baseNameMatches(baseName, otherName) {
99
- const singularizedName = this.singularize(otherName);
100
- return baseName === singularizedName;
101
- },
102
-
103
- baseNameMatchesAny(baseName, otherName) {
104
- return this.singularize(baseName) === this.singularize(otherName);
105
- },
106
-
107
- /* This is a good method to override. */
108
- getOppositeBaseName(baseName) {
109
- return pgSimplifyOppositeBaseNames && ({
110
- /*
111
- * Changes to this list are breaking changes and will require a
112
- * major version update, so we need to group as many together as
113
- * possible! Rather than sending a PR, please look for an open
114
- * issue called something like "Add more opposites" (if there isn't
115
- * one then please open it) and add your suggestions to the GitHub
116
- * comments.
117
- */
118
- // NOTE: reason to be careful using this:
119
- // field names to take into account this particular case (e.g. events with event.parent_id could not have parent OR child fields)
120
- inviter: 'invitee',
121
- parent: 'child',
122
- child: 'parent',
123
- owner: 'owned',
124
- author: 'authored',
125
- editor: 'edited',
126
- reviewer: 'reviewed'
127
- }[baseName] || null);
128
- },
129
-
130
- getBaseNameFromKeys(detailedKeys) {
131
- if (detailedKeys.length === 1) {
132
- const key = detailedKeys[0];
133
-
134
- const columnName = this._columnName(key);
135
-
136
- return this.getBaseName(columnName);
137
- }
138
-
139
- if (pgSimplifyMultikeyRelations) {
140
- const columnNames = detailedKeys.map(key => this._columnName(key));
141
- const baseNames = columnNames.map(columnName => this.getBaseName(columnName)); // Check none are null
142
-
143
- if (baseNames.every(n => n)) {
144
- return baseNames.join('-');
145
- }
146
- }
147
-
148
- return null;
149
- }
150
-
151
- }, pgSimplifyPatch ? {
152
- patchField() {
153
- return 'patch';
154
- }
155
-
156
- } : null), pgSimplifyAllRows ? {
157
- allRows(table) {
158
- return this.camelCase(this.distinctPluralize(this._singularizedTableName(table)) + connectionSuffix);
159
- },
160
-
161
- allRowsSimple(table) {
162
- return this.camelCase(this.distinctPluralize(this._singularizedTableName(table)) + listSuffix);
163
- }
164
-
165
- } : null), {}, {
166
- computedColumn(pseudoColumnName, proc, _table) {
167
- return proc.tags.fieldName ? proc.tags.fieldName + (proc.returnsSet ? ConnectionSuffix : '') : this.camelCase(pseudoColumnName + (proc.returnsSet ? connectionSuffix : ''));
168
- },
169
-
170
- computedColumnList(pseudoColumnName, proc, _table) {
171
- return proc.tags.fieldName ? proc.tags.fieldName + ListSuffix : this.camelCase(pseudoColumnName + listSuffix);
172
- },
173
-
174
- singleRelationByKeys(detailedKeys, table, _foreignTable, constraint) {
175
- if (constraint.tags.fieldName) {
176
- return constraint.tags.fieldName;
177
- }
178
-
179
- const baseName = this.getBaseNameFromKeys(detailedKeys);
180
-
181
- if (constraint.classId === constraint.foreignClassId) {
182
- if (this.baseNameMatchesAny(baseName, table.name)) {
183
- return oldInflection.singleRelationByKeys(detailedKeys, table, _foreignTable, constraint);
184
- }
185
- }
186
-
187
- if (baseName) {
188
- return this.camelCase(baseName);
189
- }
190
-
191
- if (this.baseNameMatches(baseName, table.name)) {
192
- return this.camelCase(`${this._singularizedTableName(table)}`);
193
- }
194
-
195
- return oldInflection.singleRelationByKeys(detailedKeys, table, _foreignTable, constraint);
196
- },
197
-
198
- singleRelationByKeysBackwards(detailedKeys, table, foreignTable, constraint) {
199
- if (constraint.tags.foreignSingleFieldName) {
200
- return constraint.tags.foreignSingleFieldName;
201
- }
202
-
203
- if (constraint.tags.foreignFieldName) {
204
- return constraint.tags.foreignFieldName;
205
- }
206
-
207
- const baseName = this.getBaseNameFromKeys(detailedKeys);
208
- const oppositeBaseName = baseName && this.getOppositeBaseName(baseName); // added
209
- // if (constraint.classId === constraint.foreignClassId) {
210
- // if (this.baseNameMatches(baseName, foreignTable.name)) {
211
- // return oldInflection.singleRelationByKeysBackwards(
212
- // detailedKeys,
213
- // table,
214
- // foreignTable,
215
- // constraint
216
- // );
217
- // }
218
- // }
219
-
220
- if (oppositeBaseName) {
221
- return this.camelCase(`${oppositeBaseName}-${this._singularizedTableName(table)}`);
222
- }
223
-
224
- if (this.baseNameMatches(baseName, foreignTable.name)) {
225
- return this.camelCase(`${this._singularizedTableName(table)}`);
226
- }
227
-
228
- return oldInflection.singleRelationByKeysBackwards(detailedKeys, table, foreignTable, constraint);
229
- },
230
-
231
- _manyRelationByKeysBase(detailedKeys, table, _foreignTable, _constraint) {
232
- const baseName = this.getBaseNameFromKeys(detailedKeys);
233
- const oppositeBaseName = baseName && this.getOppositeBaseName(baseName); // added
234
-
235
- if (_constraint.classId === _constraint.foreignClassId) {
236
- if (this.baseNameMatches(baseName, table.name)) {
237
- return null;
238
- }
239
- }
240
-
241
- if (oppositeBaseName) {
242
- return this.camelCase(`${oppositeBaseName}-${this.distinctPluralize(this._singularizedTableName(table))}`);
243
- }
244
-
245
- if (this.baseNameMatches(baseName, _foreignTable.name)) {
246
- return this.camelCase(`${this.distinctPluralize(this._singularizedTableName(table))}`);
247
- }
248
-
249
- return null;
250
- },
251
-
252
- manyRelationByKeys(detailedKeys, table, foreignTable, constraint) {
253
- if (constraint.tags.foreignFieldName) {
254
- if (constraint.tags.foreignSimpleFieldName) {
255
- return constraint.tags.foreignFieldName;
256
- } else {
257
- return constraint.tags.foreignFieldName + ConnectionSuffix;
258
- }
259
- }
260
-
261
- const base = this._manyRelationByKeysBase(detailedKeys, table, foreignTable, constraint);
262
-
263
- if (base) {
264
- return base + ConnectionSuffix;
265
- }
266
-
267
- return oldInflection.manyRelationByKeys(detailedKeys, table, foreignTable, constraint) + ConnectionSuffix;
268
- },
269
-
270
- manyRelationByKeysSimple(detailedKeys, table, foreignTable, constraint) {
271
- if (constraint.tags.foreignSimpleFieldName) {
272
- return constraint.tags.foreignSimpleFieldName;
273
- }
274
-
275
- if (constraint.tags.foreignFieldName) {
276
- return constraint.tags.foreignFieldName + ListSuffix;
277
- }
278
-
279
- const base = this._manyRelationByKeysBase(detailedKeys, table, foreignTable, constraint);
280
-
281
- if (base) {
282
- return base + ListSuffix;
283
- }
284
-
285
- return oldInflection.manyRelationByKeys(detailedKeys, table, foreignTable, constraint) + ListSuffix;
286
- },
287
-
288
- functionQueryName(proc) {
289
- return this.camelCase(this._functionName(proc) + (proc.returnsSet ? connectionSuffix : ''));
290
- },
291
-
292
- functionQueryNameList(proc) {
293
- return this.camelCase(this._functionName(proc) + listSuffix);
294
- }
295
-
296
- }, pgShortPk ? {
297
- tableNode(table) {
298
- return this.camelCase(`${this._singularizedTableName(table)}-by-${nodeIdFieldName}`);
299
- },
300
-
301
- rowByUniqueKeys(detailedKeys, table, constraint) {
302
- if (constraint.tags.fieldName) {
303
- return constraint.tags.fieldName;
304
- }
305
-
306
- if (constraint.type === 'p') {
307
- // Primary key, shorten!
308
- return this.camelCase(this._singularizedTableName(table));
309
- } else {
310
- return this.camelCase(`${this._singularizedTableName(table)}-by-${detailedKeys.map(key => this.column(key)).join('-and-')}`);
311
- }
312
- },
313
-
314
- updateByKeys(detailedKeys, table, constraint) {
315
- if (constraint.tags.updateFieldName) {
316
- return constraint.tags.updateFieldName;
317
- }
318
-
319
- if (constraint.type === 'p') {
320
- return this.camelCase(`update-${this._singularizedTableName(table)}`);
321
- } else {
322
- return this.camelCase(`update-${this._singularizedTableName(table)}-by-${detailedKeys.map(key => this.column(key)).join('-and-')}`);
323
- }
324
- },
325
-
326
- deleteByKeys(detailedKeys, table, constraint) {
327
- if (constraint.tags.deleteFieldName) {
328
- return constraint.tags.deleteFieldName;
329
- }
330
-
331
- if (constraint.type === 'p') {
332
- // Primary key, shorten!
333
- return this.camelCase(`delete-${this._singularizedTableName(table)}`);
334
- } else {
335
- return this.camelCase(`delete-${this._singularizedTableName(table)}-by-${detailedKeys.map(key => this.column(key)).join('-and-')}`);
336
- }
337
- },
338
-
339
- updateByKeysInputType(detailedKeys, table, constraint) {
340
- if (constraint.tags.updateFieldName) {
341
- return this.upperCamelCase(`${constraint.tags.updateFieldName}-input`);
342
- }
343
-
344
- if (constraint.type === 'p') {
345
- // Primary key, shorten!
346
- return this.upperCamelCase(`update-${this._singularizedTableName(table)}-input`);
347
- } else {
348
- return this.upperCamelCase(`update-${this._singularizedTableName(table)}-by-${detailedKeys.map(key => this.column(key)).join('-and-')}-input`);
349
- }
350
- },
351
-
352
- deleteByKeysInputType(detailedKeys, table, constraint) {
353
- if (constraint.tags.deleteFieldName) {
354
- return this.upperCamelCase(`${constraint.tags.deleteFieldName}-input`);
355
- }
356
-
357
- if (constraint.type === 'p') {
358
- // Primary key, shorten!
359
- return this.upperCamelCase(`delete-${this._singularizedTableName(table)}-input`);
360
- } else {
361
- return this.upperCamelCase(`delete-${this._singularizedTableName(table)}-by-${detailedKeys.map(key => this.column(key)).join('-and-')}-input`);
362
- }
363
- },
364
-
365
- updateNode(table) {
366
- return this.camelCase(`update-${this._singularizedTableName(table)}-by-${nodeIdFieldName}`);
367
- },
368
-
369
- deleteNode(table) {
370
- return this.camelCase(`delete-${this._singularizedTableName(table)}-by-${nodeIdFieldName}`);
371
- },
372
-
373
- updateNodeInputType(table) {
374
- return this.upperCamelCase(`update-${this._singularizedTableName(table)}-by-${nodeIdFieldName}-input`);
375
- },
376
-
377
- deleteNodeInputType(table) {
378
- return this.upperCamelCase(`delete-${this._singularizedTableName(table)}-by-${nodeIdFieldName}-input`);
379
- }
380
-
381
- } : null);
382
- });
383
- }
384
- export default PgSimpleInflector;