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.
@@ -1,79 +0,0 @@
1
- function arraysAreEqual(array1, array2) {
2
- return array1.length === array2.length && array1.every((el, i) => array2[i] === el);
3
- } // Given a `leftTable`, trace through the foreign key relations
4
- // and identify a `junctionTable` and `rightTable`.
5
- // Returns a list of data objects for these many-to-many relationships.
6
-
7
-
8
- export default function manyToManyRelationships(leftTable, build) {
9
- const {
10
- pgIntrospectionResultsByKind: introspectionResultsByKind,
11
- pgOmit: omit
12
- } = build;
13
- return leftTable.foreignConstraints.filter(con => con.type === 'f').reduce((memoLeft, junctionLeftConstraint) => {
14
- if (omit(junctionLeftConstraint, 'read') || omit(junctionLeftConstraint, 'manyToMany')) {
15
- return memoLeft;
16
- }
17
-
18
- const junctionTable = introspectionResultsByKind.classById[junctionLeftConstraint.classId];
19
-
20
- if (!junctionTable) {
21
- throw new Error(`Could not find the table that referenced us (constraint: ${junctionLeftConstraint.name})`);
22
- }
23
-
24
- if (omit(junctionTable, 'read') || omit(junctionTable, 'manyToMany')) {
25
- return memoLeft;
26
- }
27
-
28
- const memoRight = junctionTable.constraints.filter(con => con.id !== junctionLeftConstraint.id && // Don't follow the same constraint back to the left table
29
- con.type === 'f' && !omit(con, 'read') && !omit(con, 'manyToMany')).reduce((memoRight, junctionRightConstraint) => {
30
- const rightTable = junctionRightConstraint.foreignClass;
31
-
32
- if (omit(rightTable, 'read') || omit(rightTable, 'manyToMany')) {
33
- return memoRight;
34
- }
35
-
36
- const leftKeyAttributes = junctionLeftConstraint.foreignKeyAttributes;
37
- const junctionLeftKeyAttributes = junctionLeftConstraint.keyAttributes;
38
- const junctionRightKeyAttributes = junctionRightConstraint.keyAttributes;
39
- const rightKeyAttributes = junctionRightConstraint.foreignKeyAttributes; // Ensure keys were found
40
-
41
- if (!leftKeyAttributes.every(_ => _) || !junctionLeftKeyAttributes.every(_ => _) || !junctionRightKeyAttributes.every(_ => _) || !rightKeyAttributes.every(_ => _)) {
42
- throw new Error('Could not find key columns!');
43
- } // Ensure keys can be read
44
-
45
-
46
- if (leftKeyAttributes.some(attr => omit(attr, 'read')) || junctionLeftKeyAttributes.some(attr => omit(attr, 'read')) || junctionRightKeyAttributes.some(attr => omit(attr, 'read')) || rightKeyAttributes.some(attr => omit(attr, 'read'))) {
47
- return memoRight;
48
- } // Ensure both constraints are single-column
49
- // TODO: handle multi-column
50
-
51
-
52
- if (leftKeyAttributes.length > 1 || rightKeyAttributes.length > 1) {
53
- return memoRight;
54
- } // Ensure junction constraint keys are not unique (which would result in a one-to-one relation)
55
-
56
-
57
- const junctionLeftConstraintIsUnique = !!junctionTable.constraints.find(c => ['p', 'u'].includes(c.type) && arraysAreEqual(c.keyAttributeNums, junctionLeftKeyAttributes.map(attr => attr.num)));
58
- const junctionRightConstraintIsUnique = !!junctionTable.constraints.find(c => ['p', 'u'].includes(c.type) && arraysAreEqual(c.keyAttributeNums, junctionRightKeyAttributes.map(attr => attr.num)));
59
-
60
- if (junctionLeftConstraintIsUnique || junctionRightConstraintIsUnique) {
61
- return memoRight;
62
- }
63
-
64
- const allowsMultipleEdgesToNode = !junctionTable.constraints.find(c => ['p', 'u'].includes(c.type) && arraysAreEqual(c.keyAttributeNums.concat().sort(), [...junctionLeftKeyAttributes.map(obj => obj.num), ...junctionRightKeyAttributes.map(obj => obj.num)].sort()));
65
- return [...memoRight, {
66
- leftKeyAttributes,
67
- junctionLeftKeyAttributes,
68
- junctionRightKeyAttributes,
69
- rightKeyAttributes,
70
- junctionTable,
71
- rightTable,
72
- junctionLeftConstraint,
73
- junctionRightConstraint,
74
- allowsMultipleEdgesToNode
75
- }];
76
- }, []);
77
- return [...memoLeft, ...memoRight];
78
- }, []);
79
- }