graphile-meta-schema 0.2.4 → 0.2.6
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/LICENSE +3 -1
- package/README.md +101 -4
- package/belongs-to.d.ts +3 -0
- package/belongs-to.js +37 -0
- package/esm/belongs-to.js +35 -0
- package/esm/has.js +36 -0
- package/esm/index.js +503 -0
- package/esm/many-to-many.js +81 -0
- package/esm/types.js +1 -0
- package/has.d.ts +3 -0
- package/has.js +38 -0
- package/index.d.ts +4 -0
- package/index.js +509 -0
- package/many-to-many.d.ts +3 -0
- package/many-to-many.js +83 -0
- package/package.json +28 -43
- package/types.d.ts +105 -0
- package/types.js +2 -0
- package/main/index.js +0 -496
- package/main/many-to-many.js +0 -124
- package/module/index.js +0 -629
- package/module/many-to-many.js +0 -79
package/main/index.js
DELETED
|
@@ -1,496 +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["default"] = exports.PgMetaschemaPlugin = void 0;
|
|
9
|
-
|
|
10
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
|
-
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral"));
|
|
13
|
-
|
|
14
|
-
var _graphileUtils = require("graphile-utils");
|
|
15
|
-
|
|
16
|
-
var _manyToMany = _interopRequireDefault(require("./many-to-many"));
|
|
17
|
-
|
|
18
|
-
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; }
|
|
19
|
-
|
|
20
|
-
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; }
|
|
21
|
-
|
|
22
|
-
function _templateObject() {
|
|
23
|
-
var data = (0, _taggedTemplateLiteral2["default"])(["\n type MetaschemaType {\n pgAlias: String!\n pgType: String!\n gqlType: String!\n subtype: String\n modifier: Int\n typmod: JSON\n isArray: Boolean!\n }\n type MetaschemaField {\n name: String!\n type: MetaschemaType!\n }\n type MetaschemaTableInflection {\n # https://github.com/graphile/graphile-engine/blob/v4/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js\n allRows: String!\n allRowsSimple: String!\n tableFieldName: String!\n tableType: String!\n createPayloadType: String!\n orderByType: String!\n filterType: String\n inputType: String!\n patchType: String\n conditionType: String!\n patchField: String!\n edge: String!\n edgeField: String!\n connection: String!\n typeName: String!\n enumType: String!\n\n updatePayloadType: String\n deletePayloadType: String!\n deleteByPrimaryKey: String\n updateByPrimaryKey: String\n\n createField: String!\n createInputType: String!\n }\n type MetaschemaTableQuery {\n all: String!\n one: String!\n create: String!\n update: String\n delete: String\n }\n type MetaschemaTableManyToManyRelation {\n query: String\n leftKeyAttributes: [MetaschemaField]\n rightKeyAttributes: [MetaschemaField]\n junctionLeftKeyAttributes: [MetaschemaField]\n junctionRightKeyAttributes: [MetaschemaField]\n junctionTable: MetaschemaTable\n rightTable: MetaschemaTable\n junctionLeftConstraint: MetaschemaForeignKeyConstraint\n junctionRightConstraint: MetaschemaForeignKeyConstraint\n }\n type MetaschemaTableOneToOneRelation {\n query: JSON\n }\n\n type MetaschemaTableRelation {\n oneToOne: [MetaschemaTableOneToOneRelation]\n hasOne: JSON\n belongsTo: JSON\n hasMany: JSON\n manyToMany: [MetaschemaTableManyToManyRelation]\n }\n type MetaschemaTable {\n name: String!\n query: MetaschemaTableQuery!\n inflection: MetaschemaTableInflection!\n relations: MetaschemaTableRelation\n fields: [MetaschemaField]\n constraints: [MetaschemaConstraint]\n foreignKeyConstraints: [MetaschemaForeignKeyConstraint]\n primaryKeyConstraints: [MetaschemaPrimaryKeyConstraint]\n uniqueConstraints: [MetaschemaUniqueConstraint]\n checkConstraints: [MetaschemaCheckConstraint]\n exclusionConstraints: [MetaschemaExclusionConstraint]\n }\n union MetaschemaConstraint =\n MetaschemaForeignKeyConstraint\n | MetaschemaUniqueConstraint\n | MetaschemaPrimaryKeyConstraint\n | MetaschemaCheckConstraint\n | MetaschemaExclusionConstraint\n type MetaschemaForeignKeyConstraint {\n name: String!\n fields: [MetaschemaField]\n refTable: MetaschemaTable\n refFields: [MetaschemaField]\n }\n type MetaschemaUniqueConstraint {\n name: String!\n fields: [MetaschemaField]\n }\n type MetaschemaPrimaryKeyConstraint {\n name: String!\n fields: [MetaschemaField]\n }\n type MetaschemaCheckConstraint {\n name: String!\n fields: [MetaschemaField]\n }\n type MetaschemaExclusionConstraint {\n name: String!\n fields: [MetaschemaField]\n }\n type Metaschema {\n tables: [MetaschemaTable]\n }\n extend type Query {\n _meta: Metaschema\n }\n "]);
|
|
24
|
-
|
|
25
|
-
_templateObject = function _templateObject() {
|
|
26
|
-
return data;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
return data;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var GIS_TYPES = ['Geometry', 'Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection'];
|
|
33
|
-
|
|
34
|
-
var aliasTypes = function aliasTypes(type) {
|
|
35
|
-
switch (type) {
|
|
36
|
-
case 'int8':
|
|
37
|
-
return 'bigint';
|
|
38
|
-
|
|
39
|
-
case 'bool':
|
|
40
|
-
return 'boolean';
|
|
41
|
-
|
|
42
|
-
case 'bpchar':
|
|
43
|
-
return 'char';
|
|
44
|
-
|
|
45
|
-
case 'float8':
|
|
46
|
-
return 'float';
|
|
47
|
-
|
|
48
|
-
case 'float4':
|
|
49
|
-
return 'real';
|
|
50
|
-
|
|
51
|
-
case 'int4':
|
|
52
|
-
return 'int';
|
|
53
|
-
|
|
54
|
-
case 'int2':
|
|
55
|
-
return 'smallint';
|
|
56
|
-
|
|
57
|
-
default:
|
|
58
|
-
return type;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
var PgMetaschemaPlugin = (0, _graphileUtils.makeExtendSchemaPlugin)(function (build, schemaOptions) {
|
|
63
|
-
/** @type {import('graphile-build-pg').PgIntrospectionResultsByKind} */
|
|
64
|
-
var introspection = build.pgIntrospectionResultsByKind;
|
|
65
|
-
var inflection = build.inflection;
|
|
66
|
-
/** @type {string[]} */
|
|
67
|
-
|
|
68
|
-
var schemas = schemaOptions.pgSchemas;
|
|
69
|
-
var pgGetGqlTypeByTypeIdAndModifier = build.pgGetGqlTypeByTypeIdAndModifier;
|
|
70
|
-
return {
|
|
71
|
-
typeDefs: (0, _graphileUtils.gql)(_templateObject()),
|
|
72
|
-
resolvers: {
|
|
73
|
-
// TODO determine why check constraints aren't coming through
|
|
74
|
-
MetaschemaCheckConstraint: {
|
|
75
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
76
|
-
fields: function fields(constraint) {
|
|
77
|
-
return constraint.keyAttributes;
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
MetaschemaExclusionConstraint: {
|
|
81
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
82
|
-
fields: function fields(constraint) {
|
|
83
|
-
return constraint.keyAttributes;
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
MetaschemaUniqueConstraint: {
|
|
87
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
88
|
-
fields: function fields(constraint) {
|
|
89
|
-
return constraint.keyAttributes;
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
MetaschemaPrimaryKeyConstraint: {
|
|
93
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
94
|
-
fields: function fields(constraint) {
|
|
95
|
-
return constraint.keyAttributes;
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
MetaschemaForeignKeyConstraint: {
|
|
99
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
100
|
-
fields: function fields(constraint) {
|
|
101
|
-
return constraint.keyAttributes;
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
105
|
-
refTable: function refTable(constraint) {
|
|
106
|
-
return constraint.foreignClass;
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
/** @param constraint {import('graphile-build-pg').PgConstraint} */
|
|
110
|
-
refFields: function refFields(constraint) {
|
|
111
|
-
return constraint.foreignKeyAttributes;
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
MetaschemaType: {
|
|
115
|
-
/** @param attr {import('graphile-build-pg').PgType} */
|
|
116
|
-
pgType: function pgType(type) {
|
|
117
|
-
var _type$arrayItemType;
|
|
118
|
-
|
|
119
|
-
// TODO what is the best API here?
|
|
120
|
-
// 1. we could return original _name, e.g. _citext (= citext[])
|
|
121
|
-
// 2. we could return original type name and include isArray
|
|
122
|
-
if (type.isPgArray && ((_type$arrayItemType = type.arrayItemType) === null || _type$arrayItemType === void 0 ? void 0 : _type$arrayItemType.name)) {
|
|
123
|
-
return type.arrayItemType.name;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return type.name;
|
|
127
|
-
},
|
|
128
|
-
pgAlias: function pgAlias(type) {
|
|
129
|
-
var _type$arrayItemType2;
|
|
130
|
-
|
|
131
|
-
if (type.isPgArray && ((_type$arrayItemType2 = type.arrayItemType) === null || _type$arrayItemType2 === void 0 ? void 0 : _type$arrayItemType2.name)) {
|
|
132
|
-
return aliasTypes(type.arrayItemType.name);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return aliasTypes(type.name);
|
|
136
|
-
},
|
|
137
|
-
gqlType: function gqlType(type) {
|
|
138
|
-
var gqlType = pgGetGqlTypeByTypeIdAndModifier(type.id, type.attrTypeModifier);
|
|
139
|
-
|
|
140
|
-
switch (gqlType.name) {
|
|
141
|
-
case 'GeometryInterface':
|
|
142
|
-
case 'GeometryPoint':
|
|
143
|
-
case 'GeometryPolygon':
|
|
144
|
-
return 'GeoJSON';
|
|
145
|
-
|
|
146
|
-
default:
|
|
147
|
-
return gqlType;
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
subtype: function subtype(type) {
|
|
151
|
-
var gqlType = pgGetGqlTypeByTypeIdAndModifier(type.id, type.attrTypeModifier);
|
|
152
|
-
|
|
153
|
-
switch (gqlType.name) {
|
|
154
|
-
case 'GeometryInterface':
|
|
155
|
-
case 'GeometryPoint':
|
|
156
|
-
case 'GeometryPolygon':
|
|
157
|
-
return gqlType.name;
|
|
158
|
-
|
|
159
|
-
default:
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
typmod: function typmod(type) {
|
|
164
|
-
var modifier = type.attrTypeModifier;
|
|
165
|
-
if (!modifier) return null;
|
|
166
|
-
|
|
167
|
-
if (type.name === 'geography' || type.name === 'geometry') {
|
|
168
|
-
// Ref: https://github.com/postgis/postgis/blob/2.5.2/liblwgeom/liblwgeom.h.in#L156-L173
|
|
169
|
-
// #define TYPMOD_GET_SRID(typmod) ((((typmod) & 0x0FFFFF00) - ((typmod) & 0x10000000)) >> 8)
|
|
170
|
-
// #define TYPMOD_GET_TYPE(typmod) ((typmod & 0x000000FC)>>2)
|
|
171
|
-
// #define TYPMOD_GET_Z(typmod) ((typmod & 0x00000002)>>1)
|
|
172
|
-
// #define TYPMOD_GET_M(typmod) (typmod & 0x00000001)
|
|
173
|
-
var srid = (modifier & 0x0fffff00) - (modifier & 0x10000000) >> 8;
|
|
174
|
-
var subtype = (modifier & 0x000000fc) >> 2;
|
|
175
|
-
var hasZ = (modifier & 0x00000002) >> 1 === 1;
|
|
176
|
-
var hasM = (modifier & 0x00000001) === 1;
|
|
177
|
-
|
|
178
|
-
if (subtype < GIS_TYPES.length) {
|
|
179
|
-
return {
|
|
180
|
-
srid: srid,
|
|
181
|
-
subtype: subtype,
|
|
182
|
-
hasZ: hasZ,
|
|
183
|
-
hasM: hasM,
|
|
184
|
-
gisType: GIS_TYPES[subtype]
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return {
|
|
190
|
-
modifier: modifier
|
|
191
|
-
};
|
|
192
|
-
},
|
|
193
|
-
modifier: function modifier(type) {
|
|
194
|
-
return type.attrTypeModifier;
|
|
195
|
-
},
|
|
196
|
-
isArray: function isArray(type) {
|
|
197
|
-
return type.isPgArray;
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
MetaschemaField: {
|
|
201
|
-
/** @param attr {import('graphile-build-pg').PgAttribute} */
|
|
202
|
-
name: function name(attr) {
|
|
203
|
-
return inflection.column(attr);
|
|
204
|
-
},
|
|
205
|
-
|
|
206
|
-
/** @param attr {import('graphile-build-pg').PgAttribute} */
|
|
207
|
-
type: function type(attr) {
|
|
208
|
-
if (attr.typeModifier > 0) {
|
|
209
|
-
return _objectSpread(_objectSpread({}, attr.type), {}, {
|
|
210
|
-
attrTypeModifier: attr.typeModifier
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return attr.type;
|
|
215
|
-
}
|
|
216
|
-
},
|
|
217
|
-
MetaschemaTableInflection: {
|
|
218
|
-
deleteByPrimaryKey: function deleteByPrimaryKey(table) {
|
|
219
|
-
var _table$primaryKeyCons, _table$primaryKeyCons2;
|
|
220
|
-
|
|
221
|
-
if (!((_table$primaryKeyCons = table.primaryKeyConstraint) === null || _table$primaryKeyCons === void 0 ? void 0 : (_table$primaryKeyCons2 = _table$primaryKeyCons.keyAttributes) === null || _table$primaryKeyCons2 === void 0 ? void 0 : _table$primaryKeyCons2.length)) return null;
|
|
222
|
-
return inflection.deleteByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
|
|
223
|
-
},
|
|
224
|
-
updateByPrimaryKey: function updateByPrimaryKey(table) {
|
|
225
|
-
var _table$primaryKeyCons3, _table$primaryKeyCons4;
|
|
226
|
-
|
|
227
|
-
if (!((_table$primaryKeyCons3 = table.primaryKeyConstraint) === null || _table$primaryKeyCons3 === void 0 ? void 0 : (_table$primaryKeyCons4 = _table$primaryKeyCons3.keyAttributes) === null || _table$primaryKeyCons4 === void 0 ? void 0 : _table$primaryKeyCons4.length)) return null;
|
|
228
|
-
return inflection.updateByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
|
|
229
|
-
},
|
|
230
|
-
createField: function createField(table) {
|
|
231
|
-
return inflection.createField(table);
|
|
232
|
-
},
|
|
233
|
-
createInputType: function createInputType(table) {
|
|
234
|
-
return inflection.createInputType(table);
|
|
235
|
-
},
|
|
236
|
-
allRows: function allRows(table) {
|
|
237
|
-
return inflection.allRows(table);
|
|
238
|
-
},
|
|
239
|
-
allRowsSimple: function allRowsSimple(table) {
|
|
240
|
-
return inflection.allRowsSimple(table);
|
|
241
|
-
},
|
|
242
|
-
tableFieldName: function tableFieldName(table) {
|
|
243
|
-
return inflection.tableFieldName(table);
|
|
244
|
-
},
|
|
245
|
-
tableType: function tableType(table) {
|
|
246
|
-
return inflection.tableType(table);
|
|
247
|
-
},
|
|
248
|
-
orderByType: function orderByType(table) {
|
|
249
|
-
return inflection.orderByType(inflection.tableType(table));
|
|
250
|
-
},
|
|
251
|
-
filterType: function filterType(table) {
|
|
252
|
-
if (typeof inflection.filterType === 'function') return inflection.filterType(inflection.tableType(table));
|
|
253
|
-
return null;
|
|
254
|
-
},
|
|
255
|
-
inputType: function inputType(table) {
|
|
256
|
-
return inflection.inputType(inflection.tableType(table));
|
|
257
|
-
},
|
|
258
|
-
patchType: function patchType(table) {
|
|
259
|
-
return inflection.patchType(inflection.tableType(table));
|
|
260
|
-
},
|
|
261
|
-
conditionType: function conditionType(table) {
|
|
262
|
-
return inflection.conditionType(inflection.tableType(table));
|
|
263
|
-
},
|
|
264
|
-
patchField: function patchField(table) {
|
|
265
|
-
return inflection.patchField(inflection.tableType(table));
|
|
266
|
-
},
|
|
267
|
-
edge: function edge(table) {
|
|
268
|
-
return inflection.edge(inflection.tableType(table));
|
|
269
|
-
},
|
|
270
|
-
edgeField: function edgeField(table) {
|
|
271
|
-
return inflection.edgeField(table);
|
|
272
|
-
},
|
|
273
|
-
connection: function connection(table) {
|
|
274
|
-
return inflection.connection(inflection.tableType(table));
|
|
275
|
-
},
|
|
276
|
-
typeName: function typeName(table) {
|
|
277
|
-
return inflection._typeName(table);
|
|
278
|
-
},
|
|
279
|
-
enumType: function enumType(table) {
|
|
280
|
-
return inflection.enumType(table);
|
|
281
|
-
},
|
|
282
|
-
createPayloadType: function createPayloadType(table) {
|
|
283
|
-
return inflection.createPayloadType(table);
|
|
284
|
-
},
|
|
285
|
-
updatePayloadType: function updatePayloadType(table) {
|
|
286
|
-
return inflection.updatePayloadType(table);
|
|
287
|
-
},
|
|
288
|
-
deletePayloadType: function deletePayloadType(table) {
|
|
289
|
-
return inflection.deletePayloadType(table);
|
|
290
|
-
}
|
|
291
|
-
},
|
|
292
|
-
MetaschemaTableQuery: {
|
|
293
|
-
"delete": function _delete(table) {
|
|
294
|
-
var _table$primaryKeyCons5, _table$primaryKeyCons6;
|
|
295
|
-
|
|
296
|
-
if (!((_table$primaryKeyCons5 = table.primaryKeyConstraint) === null || _table$primaryKeyCons5 === void 0 ? void 0 : (_table$primaryKeyCons6 = _table$primaryKeyCons5.keyAttributes) === null || _table$primaryKeyCons6 === void 0 ? void 0 : _table$primaryKeyCons6.length)) return null;
|
|
297
|
-
return inflection.deleteByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
|
|
298
|
-
},
|
|
299
|
-
update: function update(table) {
|
|
300
|
-
var _table$primaryKeyCons7, _table$primaryKeyCons8;
|
|
301
|
-
|
|
302
|
-
if (!((_table$primaryKeyCons7 = table.primaryKeyConstraint) === null || _table$primaryKeyCons7 === void 0 ? void 0 : (_table$primaryKeyCons8 = _table$primaryKeyCons7.keyAttributes) === null || _table$primaryKeyCons8 === void 0 ? void 0 : _table$primaryKeyCons8.length)) return null;
|
|
303
|
-
return inflection.updateByKeys(table.primaryKeyConstraint.keyAttributes, table, table.primaryKeyConstraint);
|
|
304
|
-
},
|
|
305
|
-
create: function create(table) {
|
|
306
|
-
return inflection.createField(table);
|
|
307
|
-
},
|
|
308
|
-
all: function all(table) {
|
|
309
|
-
return inflection.allRows(table);
|
|
310
|
-
},
|
|
311
|
-
one: function one(table) {
|
|
312
|
-
return inflection.tableFieldName(table);
|
|
313
|
-
}
|
|
314
|
-
},
|
|
315
|
-
MetaschemaTableRelation: {
|
|
316
|
-
oneToOne: function oneToOne(table) {
|
|
317
|
-
return null;
|
|
318
|
-
},
|
|
319
|
-
oneToMany: function oneToMany(table) {
|
|
320
|
-
return null;
|
|
321
|
-
},
|
|
322
|
-
manyToOne: function manyToOne(table) {
|
|
323
|
-
return null;
|
|
324
|
-
},
|
|
325
|
-
hasOne: function hasOne(table) {
|
|
326
|
-
return null;
|
|
327
|
-
},
|
|
328
|
-
belongsTo: function belongsTo(table) {
|
|
329
|
-
return null;
|
|
330
|
-
},
|
|
331
|
-
hasMany: function hasMany(table) {
|
|
332
|
-
return null;
|
|
333
|
-
},
|
|
334
|
-
manyToMany: function manyToMany(table) {
|
|
335
|
-
if (!inflection.manyToManyRelationByKeys) {
|
|
336
|
-
return null;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
return (0, _manyToMany["default"])(table, build);
|
|
340
|
-
}
|
|
341
|
-
},
|
|
342
|
-
MetaschemaTableOneToOneRelation: {
|
|
343
|
-
query: function query(relation) {
|
|
344
|
-
return null;
|
|
345
|
-
}
|
|
346
|
-
},
|
|
347
|
-
MetaschemaTableManyToManyRelation: {
|
|
348
|
-
leftKeyAttributes: function leftKeyAttributes(relation) {
|
|
349
|
-
return relation.leftKeyAttributes;
|
|
350
|
-
},
|
|
351
|
-
junctionLeftKeyAttributes: function junctionLeftKeyAttributes(relation) {
|
|
352
|
-
return relation.junctionLeftKeyAttributes;
|
|
353
|
-
},
|
|
354
|
-
junctionRightKeyAttributes: function junctionRightKeyAttributes(relation) {
|
|
355
|
-
return relation.junctionRightKeyAttributes;
|
|
356
|
-
},
|
|
357
|
-
rightKeyAttributes: function rightKeyAttributes(relation) {
|
|
358
|
-
return relation.rightKeyAttributes;
|
|
359
|
-
},
|
|
360
|
-
junctionTable: function junctionTable(relation) {
|
|
361
|
-
return relation.junctionTable;
|
|
362
|
-
},
|
|
363
|
-
rightTable: function rightTable(relation) {
|
|
364
|
-
return relation.rightTable;
|
|
365
|
-
},
|
|
366
|
-
junctionLeftConstraint: function junctionLeftConstraint(relation) {
|
|
367
|
-
return relation.junctionLeftConstraint;
|
|
368
|
-
},
|
|
369
|
-
junctionRightConstraint: function junctionRightConstraint(relation) {
|
|
370
|
-
return relation.junctionRightConstraint;
|
|
371
|
-
},
|
|
372
|
-
query: function query(relation) {
|
|
373
|
-
var leftKeyAttributes = relation.leftKeyAttributes,
|
|
374
|
-
junctionLeftKeyAttributes = relation.junctionLeftKeyAttributes,
|
|
375
|
-
junctionRightKeyAttributes = relation.junctionRightKeyAttributes,
|
|
376
|
-
rightKeyAttributes = relation.rightKeyAttributes,
|
|
377
|
-
junctionTable = relation.junctionTable,
|
|
378
|
-
rightTable = relation.rightTable,
|
|
379
|
-
junctionLeftConstraint = relation.junctionLeftConstraint,
|
|
380
|
-
junctionRightConstraint = relation.junctionRightConstraint;
|
|
381
|
-
return inflection.manyToManyRelationByKeys(leftKeyAttributes, junctionLeftKeyAttributes, junctionRightKeyAttributes, rightKeyAttributes, junctionTable, rightTable, junctionLeftConstraint, junctionRightConstraint);
|
|
382
|
-
}
|
|
383
|
-
},
|
|
384
|
-
MetaschemaTable: {
|
|
385
|
-
relations: function relations(table) {
|
|
386
|
-
return table;
|
|
387
|
-
},
|
|
388
|
-
|
|
389
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
390
|
-
name: function name(table) {
|
|
391
|
-
return inflection.tableType(table); // return inflection._tableName(table);
|
|
392
|
-
},
|
|
393
|
-
|
|
394
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
395
|
-
fields: function fields(table) {
|
|
396
|
-
return table.attributes.filter(function (attr) {
|
|
397
|
-
if (attr.num < 1) return false; // low-level props
|
|
398
|
-
|
|
399
|
-
return true;
|
|
400
|
-
});
|
|
401
|
-
},
|
|
402
|
-
|
|
403
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
404
|
-
inflection: function inflection(table) {
|
|
405
|
-
// return table so the MetaschemaTableInflection resolver uses that as input
|
|
406
|
-
return table;
|
|
407
|
-
},
|
|
408
|
-
|
|
409
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
410
|
-
query: function query(table) {
|
|
411
|
-
return table;
|
|
412
|
-
},
|
|
413
|
-
|
|
414
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
415
|
-
constraints: function constraints(table) {
|
|
416
|
-
return table.constraints;
|
|
417
|
-
},
|
|
418
|
-
|
|
419
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
420
|
-
foreignKeyConstraints: function foreignKeyConstraints(table) {
|
|
421
|
-
return table.constraints.filter(function (c) {
|
|
422
|
-
return c.type === 'f';
|
|
423
|
-
});
|
|
424
|
-
},
|
|
425
|
-
|
|
426
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
427
|
-
primaryKeyConstraints: function primaryKeyConstraints(table) {
|
|
428
|
-
return table.constraints.filter(function (c) {
|
|
429
|
-
return c.type === 'p';
|
|
430
|
-
});
|
|
431
|
-
},
|
|
432
|
-
|
|
433
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
434
|
-
uniqueConstraints: function uniqueConstraints(table) {
|
|
435
|
-
return table.constraints.filter(function (c) {
|
|
436
|
-
return c.type === 'u';
|
|
437
|
-
});
|
|
438
|
-
},
|
|
439
|
-
|
|
440
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
441
|
-
checkConstraints: function checkConstraints(table) {
|
|
442
|
-
return table.constraints.filter(function (c) {
|
|
443
|
-
return c.type === 'c';
|
|
444
|
-
});
|
|
445
|
-
},
|
|
446
|
-
|
|
447
|
-
/** @param table {import('graphile-build-pg').PgClass} */
|
|
448
|
-
exclusionConstraints: function exclusionConstraints(table) {
|
|
449
|
-
return table.constraints.filter(function (c) {
|
|
450
|
-
return c.type === 'x';
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
},
|
|
454
|
-
MetaschemaConstraint: {
|
|
455
|
-
/** @param obj {import('graphile-build-pg').PgConstraint} */
|
|
456
|
-
__resolveType: function __resolveType(obj) {
|
|
457
|
-
switch (obj.type) {
|
|
458
|
-
case 'p':
|
|
459
|
-
return 'MetaschemaPrimaryKeyConstraint';
|
|
460
|
-
|
|
461
|
-
case 'f':
|
|
462
|
-
return 'MetaschemaForeignKeyConstraint';
|
|
463
|
-
|
|
464
|
-
case 'c':
|
|
465
|
-
return 'MetaschemaCheckConstraint';
|
|
466
|
-
|
|
467
|
-
case 'u':
|
|
468
|
-
return 'MetaschemaUniqueConstraint';
|
|
469
|
-
|
|
470
|
-
case 'x':
|
|
471
|
-
return 'MetaschemaExclusionConstraint';
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
},
|
|
475
|
-
Metaschema: {
|
|
476
|
-
tables: function tables() {
|
|
477
|
-
return introspection["class"].filter(function (kls) {
|
|
478
|
-
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
|
|
479
|
-
|
|
480
|
-
if (kls.classKind !== 'r') return false;
|
|
481
|
-
return true;
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
},
|
|
485
|
-
Query: {
|
|
486
|
-
_meta: function _meta() {
|
|
487
|
-
// just placeholder
|
|
488
|
-
return {};
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
};
|
|
493
|
-
});
|
|
494
|
-
exports.PgMetaschemaPlugin = PgMetaschemaPlugin;
|
|
495
|
-
var _default = PgMetaschemaPlugin;
|
|
496
|
-
exports["default"] = _default;
|
package/main/many-to-many.js
DELETED
|
@@ -1,124 +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["default"] = manyToManyRelationships;
|
|
9
|
-
|
|
10
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
11
|
-
|
|
12
|
-
function arraysAreEqual(array1, array2) {
|
|
13
|
-
return array1.length === array2.length && array1.every(function (el, i) {
|
|
14
|
-
return array2[i] === el;
|
|
15
|
-
});
|
|
16
|
-
} // Given a `leftTable`, trace through the foreign key relations
|
|
17
|
-
// and identify a `junctionTable` and `rightTable`.
|
|
18
|
-
// Returns a list of data objects for these many-to-many relationships.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
function manyToManyRelationships(leftTable, build) {
|
|
22
|
-
var introspectionResultsByKind = build.pgIntrospectionResultsByKind,
|
|
23
|
-
omit = build.pgOmit;
|
|
24
|
-
return leftTable.foreignConstraints.filter(function (con) {
|
|
25
|
-
return con.type === 'f';
|
|
26
|
-
}).reduce(function (memoLeft, junctionLeftConstraint) {
|
|
27
|
-
if (omit(junctionLeftConstraint, 'read') || omit(junctionLeftConstraint, 'manyToMany')) {
|
|
28
|
-
return memoLeft;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
var junctionTable = introspectionResultsByKind.classById[junctionLeftConstraint.classId];
|
|
32
|
-
|
|
33
|
-
if (!junctionTable) {
|
|
34
|
-
throw new Error("Could not find the table that referenced us (constraint: ".concat(junctionLeftConstraint.name, ")"));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (omit(junctionTable, 'read') || omit(junctionTable, 'manyToMany')) {
|
|
38
|
-
return memoLeft;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
var memoRight = junctionTable.constraints.filter(function (con) {
|
|
42
|
-
return con.id !== junctionLeftConstraint.id && // Don't follow the same constraint back to the left table
|
|
43
|
-
con.type === 'f' && !omit(con, 'read') && !omit(con, 'manyToMany');
|
|
44
|
-
}).reduce(function (memoRight, junctionRightConstraint) {
|
|
45
|
-
var rightTable = junctionRightConstraint.foreignClass;
|
|
46
|
-
|
|
47
|
-
if (omit(rightTable, 'read') || omit(rightTable, 'manyToMany')) {
|
|
48
|
-
return memoRight;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
var leftKeyAttributes = junctionLeftConstraint.foreignKeyAttributes;
|
|
52
|
-
var junctionLeftKeyAttributes = junctionLeftConstraint.keyAttributes;
|
|
53
|
-
var junctionRightKeyAttributes = junctionRightConstraint.keyAttributes;
|
|
54
|
-
var rightKeyAttributes = junctionRightConstraint.foreignKeyAttributes; // Ensure keys were found
|
|
55
|
-
|
|
56
|
-
if (!leftKeyAttributes.every(function (_) {
|
|
57
|
-
return _;
|
|
58
|
-
}) || !junctionLeftKeyAttributes.every(function (_) {
|
|
59
|
-
return _;
|
|
60
|
-
}) || !junctionRightKeyAttributes.every(function (_) {
|
|
61
|
-
return _;
|
|
62
|
-
}) || !rightKeyAttributes.every(function (_) {
|
|
63
|
-
return _;
|
|
64
|
-
})) {
|
|
65
|
-
throw new Error('Could not find key columns!');
|
|
66
|
-
} // Ensure keys can be read
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (leftKeyAttributes.some(function (attr) {
|
|
70
|
-
return omit(attr, 'read');
|
|
71
|
-
}) || junctionLeftKeyAttributes.some(function (attr) {
|
|
72
|
-
return omit(attr, 'read');
|
|
73
|
-
}) || junctionRightKeyAttributes.some(function (attr) {
|
|
74
|
-
return omit(attr, 'read');
|
|
75
|
-
}) || rightKeyAttributes.some(function (attr) {
|
|
76
|
-
return omit(attr, 'read');
|
|
77
|
-
})) {
|
|
78
|
-
return memoRight;
|
|
79
|
-
} // Ensure both constraints are single-column
|
|
80
|
-
// TODO: handle multi-column
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (leftKeyAttributes.length > 1 || rightKeyAttributes.length > 1) {
|
|
84
|
-
return memoRight;
|
|
85
|
-
} // Ensure junction constraint keys are not unique (which would result in a one-to-one relation)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
var junctionLeftConstraintIsUnique = !!junctionTable.constraints.find(function (c) {
|
|
89
|
-
return ['p', 'u'].includes(c.type) && arraysAreEqual(c.keyAttributeNums, junctionLeftKeyAttributes.map(function (attr) {
|
|
90
|
-
return attr.num;
|
|
91
|
-
}));
|
|
92
|
-
});
|
|
93
|
-
var junctionRightConstraintIsUnique = !!junctionTable.constraints.find(function (c) {
|
|
94
|
-
return ['p', 'u'].includes(c.type) && arraysAreEqual(c.keyAttributeNums, junctionRightKeyAttributes.map(function (attr) {
|
|
95
|
-
return attr.num;
|
|
96
|
-
}));
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
if (junctionLeftConstraintIsUnique || junctionRightConstraintIsUnique) {
|
|
100
|
-
return memoRight;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
var allowsMultipleEdgesToNode = !junctionTable.constraints.find(function (c) {
|
|
104
|
-
return ['p', 'u'].includes(c.type) && arraysAreEqual(c.keyAttributeNums.concat().sort(), [].concat((0, _toConsumableArray2["default"])(junctionLeftKeyAttributes.map(function (obj) {
|
|
105
|
-
return obj.num;
|
|
106
|
-
})), (0, _toConsumableArray2["default"])(junctionRightKeyAttributes.map(function (obj) {
|
|
107
|
-
return obj.num;
|
|
108
|
-
}))).sort());
|
|
109
|
-
});
|
|
110
|
-
return [].concat((0, _toConsumableArray2["default"])(memoRight), [{
|
|
111
|
-
leftKeyAttributes: leftKeyAttributes,
|
|
112
|
-
junctionLeftKeyAttributes: junctionLeftKeyAttributes,
|
|
113
|
-
junctionRightKeyAttributes: junctionRightKeyAttributes,
|
|
114
|
-
rightKeyAttributes: rightKeyAttributes,
|
|
115
|
-
junctionTable: junctionTable,
|
|
116
|
-
rightTable: rightTable,
|
|
117
|
-
junctionLeftConstraint: junctionLeftConstraint,
|
|
118
|
-
junctionRightConstraint: junctionRightConstraint,
|
|
119
|
-
allowsMultipleEdgesToNode: allowsMultipleEdgesToNode
|
|
120
|
-
}]);
|
|
121
|
-
}, []);
|
|
122
|
-
return [].concat((0, _toConsumableArray2["default"])(memoLeft), (0, _toConsumableArray2["default"])(memoRight));
|
|
123
|
-
}, []);
|
|
124
|
-
}
|