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