graphile-meta-schema 0.2.5 → 0.2.7

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/has.js DELETED
@@ -1,44 +0,0 @@
1
- export default function (foreignTable, build) {
2
- const {
3
- pgIntrospectionResultsByKind: introspectionResultsByKind,
4
- inflection,
5
- pgOmit: omit
6
- } = build;
7
- return foreignTable.foreignConstraints.filter(con => con.type === 'f').reduce((memo, constraint) => {
8
- if (omit(constraint, 'read')) {
9
- return memo;
10
- }
11
-
12
- const table = introspectionResultsByKind.classById[constraint.classId];
13
-
14
- if (!table) {
15
- return memo;
16
- }
17
-
18
- if (omit(table, 'read')) {
19
- return memo;
20
- }
21
-
22
- const keys = constraint.keyAttributes;
23
- const foreignKeys = constraint.foreignKeyAttributes;
24
-
25
- if (keys.some(key => omit(key, 'read'))) {
26
- return memo;
27
- }
28
-
29
- if (foreignKeys.some(key => omit(key, 'read'))) {
30
- return memo;
31
- }
32
-
33
- const isUnique = !!table.constraints.find(c => (c.type === 'p' || c.type === 'u') && c.keyAttributeNums.length === keys.length && c.keyAttributeNums.every((n, i) => keys[i].num === n));
34
- const fieldName = isUnique ? inflection.singleRelationByKeysBackwards(keys, table, foreignTable, constraint) : inflection.manyRelationByKeys(keys, table, foreignTable, constraint);
35
- memo.push({
36
- referencedBy: table,
37
- isUnique,
38
- fieldName,
39
- type: isUnique ? 'hasOne' : 'hasMany',
40
- keys
41
- });
42
- return memo;
43
- }, []);
44
- }
package/module/index.js DELETED
@@ -1,639 +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
- import { makeExtendSchemaPlugin, gql } from 'graphile-utils';
8
- import m2m from './many-to-many';
9
- import belongsTo from './belongs-to';
10
- import has from './has';
11
- const GIS_TYPES = ['Geometry', 'Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection'];
12
-
13
- const aliasTypes = type => {
14
- switch (type) {
15
- case 'int8':
16
- return 'bigint';
17
-
18
- case 'bool':
19
- return 'boolean';
20
-
21
- case 'bpchar':
22
- return 'char';
23
-
24
- case 'float8':
25
- return 'float';
26
-
27
- case 'float4':
28
- return 'real';
29
-
30
- case 'int4':
31
- return 'int';
32
-
33
- case 'int2':
34
- return 'smallint';
35
-
36
- default:
37
- return type;
38
- }
39
- };
40
-
41
- export const PgMetaschemaPlugin = makeExtendSchemaPlugin((build, schemaOptions) => {
42
- /** @type {import('graphile-build-pg').PgIntrospectionResultsByKind} */
43
- const introspection = build.pgIntrospectionResultsByKind;
44
- const inflection = build.inflection;
45
- /** @type {string[]} */
46
-
47
- const schemas = schemaOptions.pgSchemas;
48
- const pgGetGqlTypeByTypeIdAndModifier = build.pgGetGqlTypeByTypeIdAndModifier;
49
- return {
50
- typeDefs: gql`
51
- type MetaschemaType {
52
- pgAlias: String!
53
- pgType: String!
54
- gqlType: String!
55
- subtype: String
56
- modifier: Int
57
- typmod: JSON
58
- isArray: Boolean!
59
- }
60
- type MetaschemaField {
61
- name: String!
62
- type: MetaschemaType!
63
- }
64
- type MetaschemaTableInflection {
65
- # https://github.com/graphile/graphile-engine/blob/v4/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js
66
- allRows: String!
67
- allRowsSimple: String!
68
- tableFieldName: String!
69
- tableType: String!
70
- createPayloadType: String!
71
- orderByType: String!
72
- filterType: String
73
- inputType: String!
74
- patchType: String
75
- conditionType: String!
76
- patchField: String!
77
- edge: String!
78
- edgeField: String!
79
- connection: String!
80
- typeName: String!
81
- enumType: String!
82
-
83
- updatePayloadType: String
84
- deletePayloadType: String!
85
- deleteByPrimaryKey: String
86
- updateByPrimaryKey: String
87
-
88
- createField: String!
89
- createInputType: String!
90
- }
91
- type MetaschemaTableQuery {
92
- all: String!
93
- one: String!
94
- create: String!
95
- update: String
96
- delete: String
97
- }
98
- type MetaschemaTableManyToManyRelation {
99
- fieldName: String
100
- type: String
101
- leftKeyAttributes: [MetaschemaField]!
102
- rightKeyAttributes: [MetaschemaField]!
103
- junctionLeftKeyAttributes: [MetaschemaField]!
104
- junctionRightKeyAttributes: [MetaschemaField]!
105
- junctionTable: MetaschemaTable!
106
- rightTable: MetaschemaTable!
107
- junctionLeftConstraint: MetaschemaForeignKeyConstraint!
108
- junctionRightConstraint: MetaschemaForeignKeyConstraint!
109
- }
110
- type MetaschemaTableHasRelation {
111
- fieldName: String
112
- type: String
113
- referencedBy: MetaschemaTable!
114
- isUnique: Boolean!
115
- keys: [MetaschemaField]
116
- }
117
- type MetaschemaTableBelongsToRelation {
118
- fieldName: String
119
- type: String
120
- references: MetaschemaTable!
121
- isUnique: Boolean!
122
- keys: [MetaschemaField]
123
- }
124
- type MetaschemaTableRelation {
125
- hasOne: [MetaschemaTableHasRelation]
126
- hasMany: [MetaschemaTableHasRelation]
127
- has: [MetaschemaTableHasRelation]
128
- belongsTo: [MetaschemaTableBelongsToRelation]
129
- manyToMany: [MetaschemaTableManyToManyRelation]
130
- }
131
-
132
- type MetaschemaTable {
133
- name: String!
134
- query: MetaschemaTableQuery!
135
- inflection: MetaschemaTableInflection!
136
- relations: MetaschemaTableRelation
137
- fields: [MetaschemaField]
138
- constraints: [MetaschemaConstraint]
139
- foreignKeyConstraints: [MetaschemaForeignKeyConstraint]
140
- primaryKeyConstraints: [MetaschemaPrimaryKeyConstraint]
141
- uniqueConstraints: [MetaschemaUniqueConstraint]
142
- checkConstraints: [MetaschemaCheckConstraint]
143
- exclusionConstraints: [MetaschemaExclusionConstraint]
144
- }
145
- union MetaschemaConstraint =
146
- MetaschemaForeignKeyConstraint
147
- | MetaschemaUniqueConstraint
148
- | MetaschemaPrimaryKeyConstraint
149
- | MetaschemaCheckConstraint
150
- | MetaschemaExclusionConstraint
151
- type MetaschemaForeignKeyConstraint {
152
- name: String!
153
- fields: [MetaschemaField]
154
- refTable: MetaschemaTable
155
- refFields: [MetaschemaField]
156
- }
157
- type MetaschemaUniqueConstraint {
158
- name: String!
159
- fields: [MetaschemaField]
160
- }
161
- type MetaschemaPrimaryKeyConstraint {
162
- name: String!
163
- fields: [MetaschemaField]
164
- }
165
- type MetaschemaCheckConstraint {
166
- name: String!
167
- fields: [MetaschemaField]
168
- }
169
- type MetaschemaExclusionConstraint {
170
- name: String!
171
- fields: [MetaschemaField]
172
- }
173
- type Metaschema {
174
- tables: [MetaschemaTable]
175
- }
176
- extend type Query {
177
- _meta: Metaschema
178
- }
179
- `,
180
- resolvers: {
181
- // TODO determine why check constraints aren't coming through
182
- MetaschemaCheckConstraint: {
183
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
184
- fields(constraint) {
185
- return constraint.keyAttributes;
186
- }
187
-
188
- },
189
- MetaschemaExclusionConstraint: {
190
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
191
- fields(constraint) {
192
- return constraint.keyAttributes;
193
- }
194
-
195
- },
196
- MetaschemaUniqueConstraint: {
197
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
198
- fields(constraint) {
199
- return constraint.keyAttributes;
200
- }
201
-
202
- },
203
- MetaschemaPrimaryKeyConstraint: {
204
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
205
- fields(constraint) {
206
- return constraint.keyAttributes;
207
- }
208
-
209
- },
210
- MetaschemaForeignKeyConstraint: {
211
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
212
- fields(constraint) {
213
- return constraint.keyAttributes;
214
- },
215
-
216
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
217
- refTable(constraint) {
218
- return constraint.foreignClass;
219
- },
220
-
221
- /** @param constraint {import('graphile-build-pg').PgConstraint} */
222
- refFields(constraint) {
223
- return constraint.foreignKeyAttributes;
224
- }
225
-
226
- },
227
- MetaschemaType: {
228
- /** @param attr {import('graphile-build-pg').PgType} */
229
- pgType(type) {
230
- // TODO what is the best API here?
231
- // 1. we could return original _name, e.g. _citext (= citext[])
232
- // 2. we could return original type name and include isArray
233
- if (type.isPgArray && type.arrayItemType?.name) {
234
- return type.arrayItemType.name;
235
- }
236
-
237
- return type.name;
238
- },
239
-
240
- pgAlias(type) {
241
- if (type.isPgArray && type.arrayItemType?.name) {
242
- return aliasTypes(type.arrayItemType.name);
243
- }
244
-
245
- return aliasTypes(type.name);
246
- },
247
-
248
- gqlType(type) {
249
- const gqlType = pgGetGqlTypeByTypeIdAndModifier(type.id, type.attrTypeModifier);
250
-
251
- switch (gqlType.name) {
252
- case 'GeometryInterface':
253
- case 'GeometryPoint':
254
- case 'GeometryPolygon':
255
- return 'GeoJSON';
256
-
257
- default:
258
- return gqlType;
259
- }
260
- },
261
-
262
- subtype(type) {
263
- const gqlType = pgGetGqlTypeByTypeIdAndModifier(type.id, type.attrTypeModifier);
264
-
265
- switch (gqlType.name) {
266
- case 'GeometryInterface':
267
- case 'GeometryPoint':
268
- case 'GeometryPolygon':
269
- return gqlType.name;
270
-
271
- default:
272
- return null;
273
- }
274
- },
275
-
276
- typmod(type) {
277
- const modifier = type.attrTypeModifier;
278
- if (!modifier) return null;
279
-
280
- if (type.name === 'geography' || type.name === 'geometry') {
281
- // Ref: https://github.com/postgis/postgis/blob/2.5.2/liblwgeom/liblwgeom.h.in#L156-L173
282
- // #define TYPMOD_GET_SRID(typmod) ((((typmod) & 0x0FFFFF00) - ((typmod) & 0x10000000)) >> 8)
283
- // #define TYPMOD_GET_TYPE(typmod) ((typmod & 0x000000FC)>>2)
284
- // #define TYPMOD_GET_Z(typmod) ((typmod & 0x00000002)>>1)
285
- // #define TYPMOD_GET_M(typmod) (typmod & 0x00000001)
286
- const srid = (modifier & 0x0fffff00) - (modifier & 0x10000000) >> 8;
287
- const subtype = (modifier & 0x000000fc) >> 2;
288
- const hasZ = (modifier & 0x00000002) >> 1 === 1;
289
- const hasM = (modifier & 0x00000001) === 1;
290
-
291
- if (subtype < GIS_TYPES.length) {
292
- return {
293
- srid,
294
- subtype,
295
- hasZ,
296
- hasM,
297
- gisType: GIS_TYPES[subtype]
298
- };
299
- }
300
- }
301
-
302
- return {
303
- modifier
304
- };
305
- },
306
-
307
- modifier(type) {
308
- return type.attrTypeModifier;
309
- },
310
-
311
- isArray(type) {
312
- return type.isPgArray;
313
- }
314
-
315
- },
316
- MetaschemaField: {
317
- /** @param attr {import('graphile-build-pg').PgAttribute} */
318
- name(attr) {
319
- return inflection.column(attr);
320
- },
321
-
322
- /** @param attr {import('graphile-build-pg').PgAttribute} */
323
- type(attr) {
324
- if (attr.typeModifier > 0) {
325
- return _objectSpread(_objectSpread({}, attr.type), {}, {
326
- attrTypeModifier: attr.typeModifier
327
- });
328
- }
329
-
330
- return attr.type;
331
- }
332
-
333
- },
334
- MetaschemaTableInflection: {
335
- deleteByPrimaryKey(table) {
336
- if (!table.primaryKeyConstraint?.keyAttributes?.length) return null;
337
- return inflection.deleteByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
338
- },
339
-
340
- updateByPrimaryKey(table) {
341
- if (!table.primaryKeyConstraint?.keyAttributes?.length) return null;
342
- return inflection.updateByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
343
- },
344
-
345
- createField(table) {
346
- return inflection.createField(table);
347
- },
348
-
349
- createInputType(table) {
350
- return inflection.createInputType(table);
351
- },
352
-
353
- allRows(table) {
354
- return inflection.allRows(table);
355
- },
356
-
357
- allRowsSimple(table) {
358
- return inflection.allRowsSimple(table);
359
- },
360
-
361
- tableFieldName(table) {
362
- return inflection.tableFieldName(table);
363
- },
364
-
365
- tableType(table) {
366
- return inflection.tableType(table);
367
- },
368
-
369
- orderByType(table) {
370
- return inflection.orderByType(inflection.tableType(table));
371
- },
372
-
373
- filterType(table) {
374
- if (typeof inflection.filterType === 'function') return inflection.filterType(inflection.tableType(table));
375
- return null;
376
- },
377
-
378
- inputType(table) {
379
- return inflection.inputType(inflection.tableType(table));
380
- },
381
-
382
- patchType(table) {
383
- return inflection.patchType(inflection.tableType(table));
384
- },
385
-
386
- conditionType(table) {
387
- return inflection.conditionType(inflection.tableType(table));
388
- },
389
-
390
- patchField(table) {
391
- return inflection.patchField(inflection.tableType(table));
392
- },
393
-
394
- edge(table) {
395
- return inflection.edge(inflection.tableType(table));
396
- },
397
-
398
- edgeField(table) {
399
- return inflection.edgeField(table);
400
- },
401
-
402
- connection(table) {
403
- return inflection.connection(inflection.tableType(table));
404
- },
405
-
406
- typeName(table) {
407
- return inflection._typeName(table);
408
- },
409
-
410
- enumType(table) {
411
- return inflection.enumType(table);
412
- },
413
-
414
- createPayloadType(table) {
415
- return inflection.createPayloadType(table);
416
- },
417
-
418
- updatePayloadType(table) {
419
- return inflection.updatePayloadType(table);
420
- },
421
-
422
- deletePayloadType(table) {
423
- return inflection.deletePayloadType(table);
424
- }
425
-
426
- },
427
- MetaschemaTableQuery: {
428
- delete(table) {
429
- if (!table.primaryKeyConstraint?.keyAttributes?.length) return null;
430
- return inflection.deleteByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
431
- },
432
-
433
- update(table) {
434
- if (!table.primaryKeyConstraint?.keyAttributes?.length) return null;
435
- return inflection.updateByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
436
- },
437
-
438
- create(table) {
439
- return inflection.createField(table);
440
- },
441
-
442
- all(table) {
443
- return inflection.allRows(table);
444
- },
445
-
446
- one(table) {
447
- return inflection.tableFieldName(table);
448
- }
449
-
450
- },
451
- MetaschemaTableRelation: {
452
- hasOne(table) {
453
- return has(table, build).filter(a => a.type === 'hasOne');
454
- },
455
-
456
- hasMany(table) {
457
- return has(table, build).filter(a => a.type === 'hasMany');
458
- },
459
-
460
- belongsTo(table) {
461
- return belongsTo(table, build);
462
- },
463
-
464
- has(table) {
465
- return has(table, build);
466
- },
467
-
468
- manyToMany(table) {
469
- return m2m(table, build);
470
- }
471
-
472
- },
473
- MetaschemaTableBelongsToRelation: {
474
- type() {
475
- return 'BelongsTo';
476
- }
477
-
478
- },
479
- MetaschemaTableManyToManyRelation: {
480
- type() {
481
- return 'ManyToMany';
482
- },
483
-
484
- leftKeyAttributes(relation) {
485
- return relation.leftKeyAttributes;
486
- },
487
-
488
- junctionLeftKeyAttributes(relation) {
489
- return relation.junctionLeftKeyAttributes;
490
- },
491
-
492
- junctionRightKeyAttributes(relation) {
493
- return relation.junctionRightKeyAttributes;
494
- },
495
-
496
- rightKeyAttributes(relation) {
497
- return relation.rightKeyAttributes;
498
- },
499
-
500
- junctionTable(relation) {
501
- return relation.junctionTable;
502
- },
503
-
504
- rightTable(relation) {
505
- return relation.rightTable;
506
- },
507
-
508
- junctionLeftConstraint(relation) {
509
- return relation.junctionLeftConstraint;
510
- },
511
-
512
- junctionRightConstraint(relation) {
513
- return relation.junctionRightConstraint;
514
- },
515
-
516
- fieldName(relation) {
517
- if (!inflection.manyToManyRelationByKeys) {
518
- return null;
519
- }
520
-
521
- const {
522
- leftKeyAttributes,
523
- junctionLeftKeyAttributes,
524
- junctionRightKeyAttributes,
525
- rightKeyAttributes,
526
- junctionTable,
527
- rightTable,
528
- junctionLeftConstraint,
529
- junctionRightConstraint
530
- } = relation;
531
- return inflection.manyToManyRelationByKeys(leftKeyAttributes, junctionLeftKeyAttributes, junctionRightKeyAttributes, rightKeyAttributes, junctionTable, rightTable, junctionLeftConstraint, junctionRightConstraint);
532
- }
533
-
534
- },
535
- MetaschemaTable: {
536
- relations(table) {
537
- return table;
538
- },
539
-
540
- /** @param table {import('graphile-build-pg').PgClass} */
541
- name(table) {
542
- return inflection.tableType(table); // return inflection._tableName(table);
543
- },
544
-
545
- /** @param table {import('graphile-build-pg').PgClass} */
546
- fields(table) {
547
- return table.attributes.filter(attr => {
548
- if (attr.num < 1) return false; // low-level props
549
-
550
- return true;
551
- });
552
- },
553
-
554
- /** @param table {import('graphile-build-pg').PgClass} */
555
- inflection(table) {
556
- // return table so the MetaschemaTableInflection resolver uses that as input
557
- return table;
558
- },
559
-
560
- /** @param table {import('graphile-build-pg').PgClass} */
561
- query(table) {
562
- return table;
563
- },
564
-
565
- /** @param table {import('graphile-build-pg').PgClass} */
566
- constraints(table) {
567
- return table.constraints;
568
- },
569
-
570
- /** @param table {import('graphile-build-pg').PgClass} */
571
- foreignKeyConstraints(table) {
572
- return table.constraints.filter(c => c.type === 'f');
573
- },
574
-
575
- /** @param table {import('graphile-build-pg').PgClass} */
576
- primaryKeyConstraints(table) {
577
- return table.constraints.filter(c => c.type === 'p');
578
- },
579
-
580
- /** @param table {import('graphile-build-pg').PgClass} */
581
- uniqueConstraints(table) {
582
- return table.constraints.filter(c => c.type === 'u');
583
- },
584
-
585
- /** @param table {import('graphile-build-pg').PgClass} */
586
- checkConstraints(table) {
587
- return table.constraints.filter(c => c.type === 'c');
588
- },
589
-
590
- /** @param table {import('graphile-build-pg').PgClass} */
591
- exclusionConstraints(table) {
592
- return table.constraints.filter(c => c.type === 'x');
593
- }
594
-
595
- },
596
- MetaschemaConstraint: {
597
- /** @param obj {import('graphile-build-pg').PgConstraint} */
598
- __resolveType(obj) {
599
- switch (obj.type) {
600
- case 'p':
601
- return 'MetaschemaPrimaryKeyConstraint';
602
-
603
- case 'f':
604
- return 'MetaschemaForeignKeyConstraint';
605
-
606
- case 'c':
607
- return 'MetaschemaCheckConstraint';
608
-
609
- case 'u':
610
- return 'MetaschemaUniqueConstraint';
611
-
612
- case 'x':
613
- return 'MetaschemaExclusionConstraint';
614
- }
615
- }
616
-
617
- },
618
- Metaschema: {
619
- tables() {
620
- return introspection.class.filter(kls => {
621
- if (!schemas.includes(kls.namespaceName)) return false; // r = ordinary table, i = index, S = sequence, t = TOAST table, v = view, m = materialized view, c = composite type, f = foreign table, p = partitioned table, I = partitioned index
622
-
623
- if (kls.classKind !== 'r') return false;
624
- return true;
625
- });
626
- }
627
-
628
- },
629
- Query: {
630
- _meta() {
631
- // just placeholder
632
- return {};
633
- }
634
-
635
- }
636
- }
637
- };
638
- });
639
- export default PgMetaschemaPlugin;