@strapi/database 4.15.0-alpha.0 → 4.15.0
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/README.md +3 -0
- package/dist/connection.d.ts +3 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/dialects/dialect.d.ts +27 -0
- package/dist/dialects/dialect.d.ts.map +1 -0
- package/dist/dialects/index.d.ts +5 -0
- package/dist/dialects/index.d.ts.map +1 -0
- package/dist/dialects/mysql/constants.d.ts +3 -0
- package/dist/dialects/mysql/constants.d.ts.map +1 -0
- package/dist/dialects/mysql/database-inspector.d.ts +12 -0
- package/dist/dialects/mysql/database-inspector.d.ts.map +1 -0
- package/dist/dialects/mysql/index.d.ts +20 -0
- package/dist/dialects/mysql/index.d.ts.map +1 -0
- package/dist/dialects/mysql/schema-inspector.d.ts +13 -0
- package/dist/dialects/mysql/schema-inspector.d.ts.map +1 -0
- package/dist/dialects/postgresql/index.d.ts +14 -0
- package/dist/dialects/postgresql/index.d.ts.map +1 -0
- package/dist/dialects/postgresql/schema-inspector.d.ts +14 -0
- package/dist/dialects/postgresql/schema-inspector.d.ts.map +1 -0
- package/dist/dialects/sqlite/index.d.ts +19 -0
- package/dist/dialects/sqlite/index.d.ts.map +1 -0
- package/dist/dialects/sqlite/schema-inspector.d.ts +13 -0
- package/dist/dialects/sqlite/schema-inspector.d.ts.map +1 -0
- package/dist/entity-manager/entity-repository.d.ts +4 -0
- package/dist/entity-manager/entity-repository.d.ts.map +1 -0
- package/dist/entity-manager/index.d.ts +5 -0
- package/dist/entity-manager/index.d.ts.map +1 -0
- package/dist/entity-manager/morph-relations.d.ts +13 -0
- package/dist/entity-manager/morph-relations.d.ts.map +1 -0
- package/dist/entity-manager/regular-relations.d.ts +83 -0
- package/dist/entity-manager/regular-relations.d.ts.map +1 -0
- package/dist/entity-manager/relations/cloning/regular-relations.d.ts +17 -0
- package/dist/entity-manager/relations/cloning/regular-relations.d.ts.map +1 -0
- package/dist/entity-manager/relations-orderer.d.ts +73 -0
- package/dist/entity-manager/relations-orderer.d.ts.map +1 -0
- package/dist/entity-manager/types.d.ts +97 -0
- package/dist/entity-manager/types.d.ts.map +1 -0
- package/dist/errors/database.d.ts +5 -0
- package/dist/errors/database.d.ts.map +1 -0
- package/dist/errors/index.d.ts +8 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/invalid-date.d.ts +5 -0
- package/dist/errors/invalid-date.d.ts.map +1 -0
- package/dist/errors/invalid-datetime.d.ts +5 -0
- package/dist/errors/invalid-datetime.d.ts.map +1 -0
- package/dist/errors/invalid-relation.d.ts +5 -0
- package/dist/errors/invalid-relation.d.ts.map +1 -0
- package/dist/errors/invalid-time.d.ts +5 -0
- package/dist/errors/invalid-time.d.ts.map +1 -0
- package/dist/errors/not-null.d.ts +7 -0
- package/dist/errors/not-null.d.ts.map +1 -0
- package/dist/fields/biginteger.d.ts +4 -0
- package/dist/fields/biginteger.d.ts.map +1 -0
- package/dist/fields/boolean.d.ts +6 -0
- package/dist/fields/boolean.d.ts.map +1 -0
- package/dist/fields/date.d.ts +6 -0
- package/dist/fields/date.d.ts.map +1 -0
- package/dist/fields/datetime.d.ts +6 -0
- package/dist/fields/datetime.d.ts.map +1 -0
- package/dist/fields/field.d.ts +7 -0
- package/dist/fields/field.d.ts.map +1 -0
- package/dist/fields/index.d.ts +4 -0
- package/dist/fields/index.d.ts.map +1 -0
- package/dist/fields/json.d.ts +6 -0
- package/dist/fields/json.d.ts.map +1 -0
- package/dist/fields/number.d.ts +6 -0
- package/dist/fields/number.d.ts.map +1 -0
- package/dist/fields/shared/parsers.d.ts +4 -0
- package/dist/fields/shared/parsers.d.ts.map +1 -0
- package/dist/fields/string.d.ts +6 -0
- package/dist/fields/string.d.ts.map +1 -0
- package/dist/fields/time.d.ts +6 -0
- package/dist/fields/time.d.ts.map +1 -0
- package/dist/fields/timestamp.d.ts +6 -0
- package/dist/fields/timestamp.d.ts.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6189 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +6157 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lifecycles/index.d.ts +17 -0
- package/dist/lifecycles/index.d.ts.map +1 -0
- package/dist/lifecycles/subscribers/index.d.ts +5 -0
- package/dist/lifecycles/subscribers/index.d.ts.map +1 -0
- package/dist/lifecycles/subscribers/models-lifecycles.d.ts +6 -0
- package/dist/lifecycles/subscribers/models-lifecycles.d.ts.map +1 -0
- package/dist/lifecycles/subscribers/timestamps.d.ts +3 -0
- package/dist/lifecycles/subscribers/timestamps.d.ts.map +1 -0
- package/dist/lifecycles/types.d.ts +25 -0
- package/dist/lifecycles/types.d.ts.map +1 -0
- package/dist/metadata/index.d.ts +10 -0
- package/dist/metadata/index.d.ts.map +1 -0
- package/dist/metadata/metadata.d.ts +22 -0
- package/dist/metadata/metadata.d.ts.map +1 -0
- package/dist/metadata/relations.d.ts +16 -0
- package/dist/metadata/relations.d.ts.map +1 -0
- package/dist/migrations/index.d.ts +12 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/migrations/storage.d.ts +15 -0
- package/dist/migrations/storage.d.ts.map +1 -0
- package/dist/query/helpers/index.d.ts +8 -0
- package/dist/query/helpers/index.d.ts.map +1 -0
- package/dist/query/helpers/join.d.ts +30 -0
- package/dist/query/helpers/join.d.ts.map +1 -0
- package/dist/query/helpers/order-by.d.ts +14 -0
- package/dist/query/helpers/order-by.d.ts.map +1 -0
- package/dist/query/helpers/populate/apply.d.ts +11 -0
- package/dist/query/helpers/populate/apply.d.ts.map +1 -0
- package/dist/query/helpers/populate/index.d.ts +4 -0
- package/dist/query/helpers/populate/index.d.ts.map +1 -0
- package/dist/query/helpers/populate/process.d.ts +24 -0
- package/dist/query/helpers/populate/process.d.ts.map +1 -0
- package/dist/query/helpers/search.d.ts +4 -0
- package/dist/query/helpers/search.d.ts.map +1 -0
- package/dist/query/helpers/streams/index.d.ts +2 -0
- package/dist/query/helpers/streams/index.d.ts.map +1 -0
- package/dist/query/helpers/streams/readable.d.ts +39 -0
- package/dist/query/helpers/streams/readable.d.ts.map +1 -0
- package/dist/query/helpers/transform.d.ts +8 -0
- package/dist/query/helpers/transform.d.ts.map +1 -0
- package/dist/query/helpers/where.d.ts +19 -0
- package/dist/query/helpers/where.d.ts.map +1 -0
- package/dist/query/index.d.ts +3 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/query-builder.d.ts +79 -0
- package/dist/query/query-builder.d.ts.map +1 -0
- package/dist/query/types.d.ts +8 -0
- package/dist/query/types.d.ts.map +1 -0
- package/dist/schema/builder.d.ts +33 -0
- package/dist/schema/builder.d.ts.map +1 -0
- package/dist/schema/diff.d.ts +7 -0
- package/dist/schema/diff.d.ts.map +1 -0
- package/dist/schema/index.d.ts +20 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/schema.d.ts +4 -0
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/schema/storage.d.ts +10 -0
- package/dist/schema/storage.d.ts.map +1 -0
- package/dist/schema/types.d.ts +103 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/transaction-context.d.ts +22 -0
- package/dist/transaction-context.d.ts.map +1 -0
- package/dist/types/index.d.ts +169 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/utils/content-types.d.ts +13 -0
- package/dist/utils/content-types.d.ts.map +1 -0
- package/dist/utils/knex.d.ts +12 -0
- package/dist/utils/knex.d.ts.map +1 -0
- package/dist/utils/types.d.ts +10 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/validations/index.d.ts +6 -0
- package/dist/validations/index.d.ts.map +1 -0
- package/dist/validations/relations/bidirectional.d.ts +12 -0
- package/dist/validations/relations/bidirectional.d.ts.map +1 -0
- package/dist/validations/relations/index.d.ts +7 -0
- package/dist/validations/relations/index.d.ts.map +1 -0
- package/package.json +21 -9
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -4
- package/jest.config.js +0 -6
- package/lib/__tests__/index.test.js +0 -93
- package/lib/__tests__/lifecycles.test.js +0 -55
- package/lib/connection.js +0 -64
- package/lib/dialects/dialect.js +0 -63
- package/lib/dialects/index.js +0 -53
- package/lib/dialects/mysql/constants.js +0 -6
- package/lib/dialects/mysql/database-inspector.js +0 -37
- package/lib/dialects/mysql/index.js +0 -92
- package/lib/dialects/mysql/schema-inspector.js +0 -234
- package/lib/dialects/postgresql/index.js +0 -65
- package/lib/dialects/postgresql/schema-inspector.js +0 -283
- package/lib/dialects/sqlite/index.js +0 -87
- package/lib/dialects/sqlite/schema-inspector.js +0 -151
- package/lib/entity-manager/__tests__/relations-orderer.test.js +0 -186
- package/lib/entity-manager/__tests__/sort-connect-array.test.js +0 -79
- package/lib/entity-manager/entity-repository.js +0 -164
- package/lib/entity-manager/index.js +0 -1385
- package/lib/entity-manager/morph-relations.js +0 -63
- package/lib/entity-manager/regular-relations.js +0 -506
- package/lib/entity-manager/relations/cloning/regular-relations.js +0 -76
- package/lib/entity-manager/relations-orderer.js +0 -225
- package/lib/errors/database.js +0 -12
- package/lib/errors/index.js +0 -17
- package/lib/errors/invalid-date.js +0 -14
- package/lib/errors/invalid-datetime.js +0 -14
- package/lib/errors/invalid-relation.js +0 -14
- package/lib/errors/invalid-time.js +0 -14
- package/lib/errors/not-null.js +0 -15
- package/lib/fields/biginteger.js +0 -17
- package/lib/fields/boolean.js +0 -39
- package/lib/fields/date.js +0 -16
- package/lib/fields/datetime.js +0 -19
- package/lib/fields/field.js +0 -17
- package/lib/fields/index.d.ts +0 -9
- package/lib/fields/index.js +0 -50
- package/lib/fields/json.js +0 -21
- package/lib/fields/number.js +0 -23
- package/lib/fields/shared/parsers.js +0 -71
- package/lib/fields/string.js +0 -17
- package/lib/fields/time.js +0 -17
- package/lib/fields/timestamp.js +0 -19
- package/lib/index.d.ts +0 -198
- package/lib/index.js +0 -129
- package/lib/lifecycles/index.d.ts +0 -51
- package/lib/lifecycles/index.js +0 -90
- package/lib/lifecycles/subscribers/index.d.ts +0 -11
- package/lib/lifecycles/subscribers/models-lifecycles.js +0 -19
- package/lib/lifecycles/subscribers/timestamps.js +0 -65
- package/lib/metadata/index.js +0 -244
- package/lib/metadata/relations.js +0 -578
- package/lib/migrations/index.d.ts +0 -9
- package/lib/migrations/index.js +0 -75
- package/lib/migrations/storage.js +0 -44
- package/lib/query/helpers/index.js +0 -11
- package/lib/query/helpers/join.js +0 -96
- package/lib/query/helpers/order-by.js +0 -70
- package/lib/query/helpers/populate/apply.js +0 -664
- package/lib/query/helpers/populate/index.js +0 -9
- package/lib/query/helpers/populate/process.js +0 -102
- package/lib/query/helpers/search.js +0 -84
- package/lib/query/helpers/streams/index.js +0 -5
- package/lib/query/helpers/streams/readable.js +0 -174
- package/lib/query/helpers/transform.js +0 -84
- package/lib/query/helpers/where.js +0 -365
- package/lib/query/index.js +0 -7
- package/lib/query/query-builder.js +0 -514
- package/lib/schema/__tests__/schema-diff.test.js +0 -231
- package/lib/schema/builder.js +0 -386
- package/lib/schema/diff.js +0 -399
- package/lib/schema/index.d.ts +0 -49
- package/lib/schema/index.js +0 -94
- package/lib/schema/schema.js +0 -202
- package/lib/schema/storage.js +0 -76
- package/lib/transaction-context.js +0 -68
- package/lib/types/index.d.ts +0 -6
- package/lib/types/index.js +0 -35
- package/lib/utils/content-types.js +0 -40
- package/lib/utils/knex.js +0 -22
- package/lib/validations/index.js +0 -20
- package/lib/validations/relations/bidirectional.js +0 -89
- package/lib/validations/relations/index.js +0 -14
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { castArray } = require('lodash/fp');
|
|
4
|
-
const _ = require('lodash/fp');
|
|
5
|
-
const { InvalidRelationError } = require('../errors');
|
|
6
|
-
/**
|
|
7
|
-
* When connecting relations, the order you connect them matters.
|
|
8
|
-
*
|
|
9
|
-
* Example, if you connect the following relations:
|
|
10
|
-
* { id: 5, position: { before: 1 } }
|
|
11
|
-
* { id: 1, position: { before: 2 } }
|
|
12
|
-
* { id: 2, position: { end: true } }
|
|
13
|
-
*
|
|
14
|
-
* Going through the connect array, id 5 has to be connected before id 1,
|
|
15
|
-
* so the order of id5 = id1 - 1. But the order value of id 1 is unknown.
|
|
16
|
-
* The only way to know the order of id 1 is to connect it first.
|
|
17
|
-
*
|
|
18
|
-
* This function makes sure the relations are connected in the right order:
|
|
19
|
-
* { id: 2, position: { end: true } }
|
|
20
|
-
* { id: 1, position: { before: 2 } }
|
|
21
|
-
* { id: 5, position: { before: 1 } }
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
const sortConnectArray = (connectArr, initialArr = [], strictSort = true) => {
|
|
25
|
-
const sortedConnect = [];
|
|
26
|
-
// Boolean to know if we have to recalculate the order of the relations
|
|
27
|
-
let needsSorting = false;
|
|
28
|
-
// Map to validate if relation is already in sortedConnect or DB.
|
|
29
|
-
const relationInInitialArray = initialArr.reduce((acc, rel) => ({ ...acc, [rel.id]: true }), {});
|
|
30
|
-
// Map to store the first index where a relation id is connected
|
|
31
|
-
const mappedRelations = connectArr.reduce((mapper, relation) => {
|
|
32
|
-
const adjacentRelId = relation.position?.before || relation.position?.after;
|
|
33
|
-
|
|
34
|
-
if (!relationInInitialArray[adjacentRelId] && !mapper[adjacentRelId]) {
|
|
35
|
-
needsSorting = true;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// If the relation is already in the array, throw an error
|
|
39
|
-
if (mapper[relation.id]) {
|
|
40
|
-
throw new InvalidRelationError(
|
|
41
|
-
`The relation with id ${relation.id} is already connected. ` +
|
|
42
|
-
'You cannot connect the same relation twice.'
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
[relation.id]: { ...relation, computed: false },
|
|
48
|
-
...mapper,
|
|
49
|
-
};
|
|
50
|
-
}, {});
|
|
51
|
-
|
|
52
|
-
// If we don't need to sort the connect array, we can return it as is
|
|
53
|
-
if (!needsSorting) return connectArr;
|
|
54
|
-
|
|
55
|
-
// Recursively compute in which order the relation should be connected
|
|
56
|
-
const computeRelation = (relation, relationsSeenInBranch) => {
|
|
57
|
-
const adjacentRelId = relation.position?.before || relation.position?.after;
|
|
58
|
-
const adjacentRelation = mappedRelations[adjacentRelId];
|
|
59
|
-
|
|
60
|
-
// If the relation has already been seen in the current branch,
|
|
61
|
-
// it means there is a circular reference
|
|
62
|
-
if (relationsSeenInBranch[adjacentRelId]) {
|
|
63
|
-
throw new InvalidRelationError(
|
|
64
|
-
'A circular reference was found in the connect array. ' +
|
|
65
|
-
'One relation is trying to connect before/after another one that is trying to connect before/after it'
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// This relation has already been computed
|
|
70
|
-
if (mappedRelations[relation.id]?.computed) return;
|
|
71
|
-
|
|
72
|
-
mappedRelations[relation.id].computed = true;
|
|
73
|
-
|
|
74
|
-
// Relation does not have a before or after attribute or is in the initial array
|
|
75
|
-
if (!adjacentRelId || relationInInitialArray[adjacentRelId]) {
|
|
76
|
-
sortedConnect.push(relation);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Look if id is referenced elsewhere in the array
|
|
81
|
-
if (mappedRelations[adjacentRelId]) {
|
|
82
|
-
computeRelation(adjacentRelation, { ...relationsSeenInBranch, [relation.id]: true });
|
|
83
|
-
sortedConnect.push(relation);
|
|
84
|
-
} else if (strictSort) {
|
|
85
|
-
// If we reach this point, it means that the adjacent relation is not in the connect array
|
|
86
|
-
// and it is not in the database.
|
|
87
|
-
throw new InvalidRelationError(
|
|
88
|
-
`There was a problem connecting relation with id ${
|
|
89
|
-
relation.id
|
|
90
|
-
} at position ${JSON.stringify(
|
|
91
|
-
relation.position
|
|
92
|
-
)}. The relation with id ${adjacentRelId} needs to be connected first.`
|
|
93
|
-
);
|
|
94
|
-
} else {
|
|
95
|
-
// We are in non-strict mode so we can push the relation.
|
|
96
|
-
sortedConnect.push({ id: relation.id, position: { end: true } });
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
// Iterate over connectArr and populate sortedConnect
|
|
101
|
-
connectArr.forEach((relation) => computeRelation(relation, {}));
|
|
102
|
-
|
|
103
|
-
return sortedConnect;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Responsible for calculating the relations order when connecting them.
|
|
108
|
-
*
|
|
109
|
-
* The connect method takes an array of relations with positional attributes:
|
|
110
|
-
* - before: the id of the relation to connect before
|
|
111
|
-
* - after: the id of the relation to connect after
|
|
112
|
-
* - end: it should be at the end
|
|
113
|
-
* - start: it should be at the start
|
|
114
|
-
*
|
|
115
|
-
* Example:
|
|
116
|
-
* - Having a connect array like:
|
|
117
|
-
* [ { id: 4, before: 2 }, { id: 4, before: 3}, {id: 5, before: 4} ]
|
|
118
|
-
* - With the initial relations:
|
|
119
|
-
* [ { id: 2, order: 4 }, { id: 3, order: 10 } ]
|
|
120
|
-
* - Step by step, going through the connect array, the array of relations would be:
|
|
121
|
-
* [ { id: 4, order: 3.5 }, { id: 2, order: 4 }, { id: 3, order: 10 } ]
|
|
122
|
-
* [ { id: 2, order: 4 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]
|
|
123
|
-
* [ { id: 2, order: 4 }, { id: 5, order: 3.5 }, { id: 4, order: 3.5 }, { id: 3, order: 10 } ]
|
|
124
|
-
* - The final step would be to recalculate fractional order values.
|
|
125
|
-
* [ { id: 2, order: 4 }, { id: 5, order: 3.33 }, { id: 4, order: 3.66 }, { id: 3, order: 10 } ]
|
|
126
|
-
*
|
|
127
|
-
* @param {Array<*>} initArr - array of relations to initialize the class with
|
|
128
|
-
* @param {string} idColumn - the column name of the id
|
|
129
|
-
* @param {string} orderColumn - the column name of the order
|
|
130
|
-
* @param {boolean} strict - if true, will throw an error if a relation is connected adjacent to
|
|
131
|
-
* another one that does not exist
|
|
132
|
-
* @return {*}
|
|
133
|
-
*/
|
|
134
|
-
const relationsOrderer = (initArr, idColumn, orderColumn, strict) => {
|
|
135
|
-
const computedRelations = _.castArray(initArr || []).map((r) => ({
|
|
136
|
-
init: true,
|
|
137
|
-
id: r[idColumn],
|
|
138
|
-
order: r[orderColumn] || 1,
|
|
139
|
-
}));
|
|
140
|
-
|
|
141
|
-
const maxOrder = _.maxBy('order', computedRelations)?.order || 0;
|
|
142
|
-
|
|
143
|
-
const findRelation = (id) => {
|
|
144
|
-
const idx = computedRelations.findIndex((r) => r.id === id);
|
|
145
|
-
return { idx, relation: computedRelations[idx] };
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const removeRelation = (r) => {
|
|
149
|
-
const { idx } = findRelation(r.id);
|
|
150
|
-
if (idx >= 0) {
|
|
151
|
-
computedRelations.splice(idx, 1);
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
const insertRelation = (r) => {
|
|
156
|
-
let idx;
|
|
157
|
-
|
|
158
|
-
if (r.position?.before) {
|
|
159
|
-
const { idx: _idx, relation } = findRelation(r.position.before);
|
|
160
|
-
if (relation.init) r.order = relation.order - 0.5;
|
|
161
|
-
else r.order = relation.order;
|
|
162
|
-
idx = _idx;
|
|
163
|
-
} else if (r.position?.after) {
|
|
164
|
-
const { idx: _idx, relation } = findRelation(r.position.after);
|
|
165
|
-
if (relation.init) r.order = relation.order + 0.5;
|
|
166
|
-
else r.order = relation.order;
|
|
167
|
-
idx = _idx + 1;
|
|
168
|
-
} else if (r.position?.start) {
|
|
169
|
-
r.order = 0.5;
|
|
170
|
-
idx = 0;
|
|
171
|
-
} else {
|
|
172
|
-
r.order = maxOrder + 0.5;
|
|
173
|
-
idx = computedRelations.length;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// Insert the relation in the array
|
|
177
|
-
computedRelations.splice(idx, 0, r);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
disconnect(relations) {
|
|
182
|
-
castArray(relations).forEach((relation) => {
|
|
183
|
-
removeRelation(relation);
|
|
184
|
-
});
|
|
185
|
-
return this;
|
|
186
|
-
},
|
|
187
|
-
connect(relations) {
|
|
188
|
-
sortConnectArray(castArray(relations), computedRelations, strict).forEach((relation) => {
|
|
189
|
-
this.disconnect(relation);
|
|
190
|
-
|
|
191
|
-
try {
|
|
192
|
-
insertRelation(relation);
|
|
193
|
-
} catch (err) {
|
|
194
|
-
throw new Error(
|
|
195
|
-
`There was a problem connecting relation with id ${
|
|
196
|
-
relation.id
|
|
197
|
-
} at position ${JSON.stringify(
|
|
198
|
-
relation.position
|
|
199
|
-
)}. The list of connect relations is not valid`
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
return this;
|
|
204
|
-
},
|
|
205
|
-
get() {
|
|
206
|
-
return computedRelations;
|
|
207
|
-
},
|
|
208
|
-
/**
|
|
209
|
-
* Get a map between the relation id and its order
|
|
210
|
-
*/
|
|
211
|
-
getOrderMap() {
|
|
212
|
-
return _(computedRelations)
|
|
213
|
-
.groupBy('order')
|
|
214
|
-
.reduce((acc, relations) => {
|
|
215
|
-
if (relations[0]?.init) return acc;
|
|
216
|
-
relations.forEach((relation, idx) => {
|
|
217
|
-
acc[relation.id] = Math.floor(relation.order) + (idx + 1) / (relations.length + 1);
|
|
218
|
-
});
|
|
219
|
-
return acc;
|
|
220
|
-
}, {});
|
|
221
|
-
},
|
|
222
|
-
};
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
module.exports = { relationsOrderer, sortConnectArray };
|
package/lib/errors/database.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
class DatabaseError extends Error {
|
|
4
|
-
constructor(message, details = {}) {
|
|
5
|
-
super();
|
|
6
|
-
this.name = 'DatabaseError';
|
|
7
|
-
this.message = message || 'A database error occured';
|
|
8
|
-
this.details = details;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
module.exports = DatabaseError;
|
package/lib/errors/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DatabaseError = require('./database');
|
|
4
|
-
const NotNullError = require('./not-null');
|
|
5
|
-
const InvalidTimeError = require('./invalid-time');
|
|
6
|
-
const InvalidDateError = require('./invalid-date');
|
|
7
|
-
const InvalidDateTimeError = require('./invalid-datetime');
|
|
8
|
-
const InvalidRelationError = require('./invalid-relation');
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
DatabaseError,
|
|
12
|
-
NotNullError,
|
|
13
|
-
InvalidTimeError,
|
|
14
|
-
InvalidDateError,
|
|
15
|
-
InvalidDateTimeError,
|
|
16
|
-
InvalidRelationError,
|
|
17
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DatabaseError = require('./database');
|
|
4
|
-
|
|
5
|
-
class InvalidDateError extends DatabaseError {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super();
|
|
8
|
-
this.name = 'InvalidTimeFormat';
|
|
9
|
-
this.message = message || 'Invalid date format, expected YYYY-MM-DD';
|
|
10
|
-
this.details = {};
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = InvalidDateError;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DatabaseError = require('./database');
|
|
4
|
-
|
|
5
|
-
class InvalidDateTimeError extends DatabaseError {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super();
|
|
8
|
-
this.name = 'InvalidTimeFormat';
|
|
9
|
-
this.message = message || 'Invalid datetime format, expected a timestamp or an ISO date';
|
|
10
|
-
this.details = {};
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = InvalidDateTimeError;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DatabaseError = require('./database');
|
|
4
|
-
|
|
5
|
-
class InvalidRelationError extends DatabaseError {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super();
|
|
8
|
-
this.name = 'InvalidRelationFormat';
|
|
9
|
-
this.message = message || 'Invalid relation format';
|
|
10
|
-
this.details = {};
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = InvalidRelationError;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DatabaseError = require('./database');
|
|
4
|
-
|
|
5
|
-
class InvalidTimeError extends DatabaseError {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super();
|
|
8
|
-
this.name = 'InvalidTimeFormat';
|
|
9
|
-
this.message = message || 'Invalid time format, expected HH:mm:ss.SSS';
|
|
10
|
-
this.details = {};
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = InvalidTimeError;
|
package/lib/errors/not-null.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DatabaseError = require('./database');
|
|
4
|
-
|
|
5
|
-
class NotNullError extends DatabaseError {
|
|
6
|
-
constructor({ column = '' } = {}) {
|
|
7
|
-
super();
|
|
8
|
-
this.name = 'NotNullError';
|
|
9
|
-
this.message = `Not null constraint violation${column ? ` on column ${column}` : ''}.`;
|
|
10
|
-
this.details = { column };
|
|
11
|
-
this.stack = '';
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = NotNullError;
|
package/lib/fields/biginteger.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { toString } = require('lodash/fp');
|
|
4
|
-
|
|
5
|
-
const NumberField = require('./number');
|
|
6
|
-
|
|
7
|
-
class BigIntegerField extends NumberField {
|
|
8
|
-
toDB(value) {
|
|
9
|
-
return toString(value);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
fromDB(value) {
|
|
13
|
-
return toString(value);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = BigIntegerField;
|
package/lib/fields/boolean.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { toString } = require('lodash/fp');
|
|
4
|
-
|
|
5
|
-
const Field = require('./field');
|
|
6
|
-
|
|
7
|
-
class BooleanField extends Field {
|
|
8
|
-
toDB(value) {
|
|
9
|
-
if (typeof value === 'boolean') return value;
|
|
10
|
-
|
|
11
|
-
if (['true', 't', '1', 1].includes(value)) {
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (['false', 'f', '0', 0].includes(value)) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return Boolean(value);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
fromDB(value) {
|
|
23
|
-
if (typeof value === 'boolean') {
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const strVal = toString(value);
|
|
28
|
-
|
|
29
|
-
if (strVal === '1') {
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
if (strVal === '0') {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = BooleanField;
|
package/lib/fields/date.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { parseDate } = require('./shared/parsers');
|
|
4
|
-
const Field = require('./field');
|
|
5
|
-
|
|
6
|
-
class DateField extends Field {
|
|
7
|
-
toDB(value) {
|
|
8
|
-
return parseDate(value);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
fromDB(value) {
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = DateField;
|
package/lib/fields/datetime.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const dateFns = require('date-fns');
|
|
4
|
-
|
|
5
|
-
const { parseDateTimeOrTimestamp } = require('./shared/parsers');
|
|
6
|
-
const Field = require('./field');
|
|
7
|
-
|
|
8
|
-
class DatetimeField extends Field {
|
|
9
|
-
toDB(value) {
|
|
10
|
-
return parseDateTimeOrTimestamp(value);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
fromDB(value) {
|
|
14
|
-
const cast = new Date(value);
|
|
15
|
-
return dateFns.isValid(cast) ? cast.toISOString() : null;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = DatetimeField;
|
package/lib/fields/field.js
DELETED
package/lib/fields/index.d.ts
DELETED
package/lib/fields/index.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash/fp');
|
|
4
|
-
|
|
5
|
-
const Field = require('./field');
|
|
6
|
-
const StringField = require('./string');
|
|
7
|
-
const JSONField = require('./json');
|
|
8
|
-
const BigIntegerField = require('./biginteger');
|
|
9
|
-
const NumberField = require('./number');
|
|
10
|
-
const DateField = require('./date');
|
|
11
|
-
const TimeField = require('./time');
|
|
12
|
-
const DatetimeField = require('./datetime');
|
|
13
|
-
const TimestampField = require('./timestamp');
|
|
14
|
-
const BooleanField = require('./boolean');
|
|
15
|
-
|
|
16
|
-
const typeToFieldMap = {
|
|
17
|
-
increments: Field,
|
|
18
|
-
password: StringField,
|
|
19
|
-
email: StringField,
|
|
20
|
-
string: StringField,
|
|
21
|
-
uid: StringField,
|
|
22
|
-
richtext: StringField,
|
|
23
|
-
text: StringField,
|
|
24
|
-
enumeration: StringField,
|
|
25
|
-
json: JSONField,
|
|
26
|
-
biginteger: BigIntegerField,
|
|
27
|
-
integer: NumberField,
|
|
28
|
-
float: NumberField,
|
|
29
|
-
decimal: NumberField,
|
|
30
|
-
date: DateField,
|
|
31
|
-
time: TimeField,
|
|
32
|
-
datetime: DatetimeField,
|
|
33
|
-
timestamp: TimestampField,
|
|
34
|
-
boolean: BooleanField,
|
|
35
|
-
blocks: JSONField,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const createField = (attribute) => {
|
|
39
|
-
const { type } = attribute;
|
|
40
|
-
|
|
41
|
-
if (_.has(type, typeToFieldMap)) {
|
|
42
|
-
return new typeToFieldMap[type]({});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
throw new Error(`Undefined field for type ${type}`);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
module.exports = {
|
|
49
|
-
createField,
|
|
50
|
-
};
|
package/lib/fields/json.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const Field = require('./field');
|
|
4
|
-
|
|
5
|
-
class JSONField extends Field {
|
|
6
|
-
toDB(value) {
|
|
7
|
-
return JSON.stringify(value);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
fromDB(value) {
|
|
11
|
-
try {
|
|
12
|
-
if (typeof value === 'string') return JSON.parse(value);
|
|
13
|
-
} catch (error) {
|
|
14
|
-
// Just return the value if it's not a valid JSON string
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
return value;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = JSONField;
|
package/lib/fields/number.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { toNumber } = require('lodash/fp');
|
|
4
|
-
|
|
5
|
-
const Field = require('./field');
|
|
6
|
-
|
|
7
|
-
class NumberField extends Field {
|
|
8
|
-
toDB(value) {
|
|
9
|
-
const numberValue = toNumber(value);
|
|
10
|
-
|
|
11
|
-
if (Number.isNaN(numberValue)) {
|
|
12
|
-
throw new Error(`Expected a valid Number, got ${value}`);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return numberValue;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
fromDB(value) {
|
|
19
|
-
return toNumber(value);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = NumberField;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { isString, padCharsEnd } = require('lodash/fp');
|
|
4
|
-
const dateFns = require('date-fns');
|
|
5
|
-
|
|
6
|
-
const { InvalidDateTimeError, InvalidDateError, InvalidTimeError } = require('../../errors');
|
|
7
|
-
|
|
8
|
-
const DATE_REGEX = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
|
|
9
|
-
const PARTIAL_DATE_REGEX = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])/g;
|
|
10
|
-
const TIME_REGEX = /^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]{1,3})?$/;
|
|
11
|
-
|
|
12
|
-
const parseDateTimeOrTimestamp = (value) => {
|
|
13
|
-
if (dateFns.isDate(value)) return value;
|
|
14
|
-
try {
|
|
15
|
-
const date = dateFns.parseISO(value);
|
|
16
|
-
if (dateFns.isValid(date)) return date;
|
|
17
|
-
|
|
18
|
-
const milliUnixDate = dateFns.parse(value, 'T', new Date());
|
|
19
|
-
if (dateFns.isValid(milliUnixDate)) return milliUnixDate;
|
|
20
|
-
|
|
21
|
-
throw new InvalidDateTimeError(`Invalid format, expected a timestamp or an ISO date`);
|
|
22
|
-
} catch (error) {
|
|
23
|
-
throw new InvalidDateTimeError(`Invalid format, expected a timestamp or an ISO date`);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const parseDate = (value) => {
|
|
28
|
-
if (dateFns.isDate(value)) return dateFns.format(value, 'yyyy-MM-dd');
|
|
29
|
-
|
|
30
|
-
const found = isString(value) ? value.match(PARTIAL_DATE_REGEX) || [] : [];
|
|
31
|
-
const extractedValue = found[0];
|
|
32
|
-
|
|
33
|
-
if (extractedValue && !DATE_REGEX.test(value)) {
|
|
34
|
-
// TODO V5: throw an error when format yyyy-MM-dd is not respected
|
|
35
|
-
// throw new InvalidDateError(`Invalid format, expected yyyy-MM-dd`);
|
|
36
|
-
process.emitWarning(
|
|
37
|
-
`[deprecated] Using a date format other than YYYY-MM-DD will be removed in future versions. Date received: ${value}. Date stored: ${extractedValue}.`
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const date = dateFns.parseISO(extractedValue);
|
|
42
|
-
if (!dateFns.isValid(date)) {
|
|
43
|
-
throw new InvalidDateError(`Invalid date`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return extractedValue;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const parseTime = (value) => {
|
|
50
|
-
if (dateFns.isDate(value)) return dateFns.format(value, 'HH:mm:ss.SSS');
|
|
51
|
-
|
|
52
|
-
if (typeof value !== 'string') {
|
|
53
|
-
throw new InvalidTimeError(`Expected a string, got a ${typeof value}`);
|
|
54
|
-
}
|
|
55
|
-
const result = value.match(TIME_REGEX);
|
|
56
|
-
|
|
57
|
-
if (result === null) {
|
|
58
|
-
throw new InvalidTimeError('Invalid time format, expected HH:mm:ss.SSS');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const [, hours, minutes, seconds, fraction = '.000'] = result;
|
|
62
|
-
const fractionPart = padCharsEnd('0', 3, fraction.slice(1));
|
|
63
|
-
|
|
64
|
-
return `${hours}:${minutes}:${seconds}.${fractionPart}`;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
module.exports = {
|
|
68
|
-
parseDateTimeOrTimestamp,
|
|
69
|
-
parseDate,
|
|
70
|
-
parseTime,
|
|
71
|
-
};
|
package/lib/fields/string.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { toString } = require('lodash/fp');
|
|
4
|
-
|
|
5
|
-
const Field = require('./field');
|
|
6
|
-
|
|
7
|
-
class StringField extends Field {
|
|
8
|
-
toDB(value) {
|
|
9
|
-
return toString(value);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
fromDB(value) {
|
|
13
|
-
return toString(value);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = StringField;
|
package/lib/fields/time.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { parseTime } = require('./shared/parsers');
|
|
4
|
-
const Field = require('./field');
|
|
5
|
-
|
|
6
|
-
class TimeField extends Field {
|
|
7
|
-
toDB(value) {
|
|
8
|
-
return parseTime(value);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
fromDB(value) {
|
|
12
|
-
// make sure that's a string with valid format ?
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = TimeField;
|
package/lib/fields/timestamp.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const dateFns = require('date-fns');
|
|
4
|
-
|
|
5
|
-
const { parseDateTimeOrTimestamp } = require('./shared/parsers');
|
|
6
|
-
const Field = require('./field');
|
|
7
|
-
|
|
8
|
-
class TimestampField extends Field {
|
|
9
|
-
toDB(value) {
|
|
10
|
-
return parseDateTimeOrTimestamp(value);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
fromDB(value) {
|
|
14
|
-
const cast = new Date(value);
|
|
15
|
-
return dateFns.isValid(cast) ? dateFns.format(cast, 'T') : null;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = TimestampField;
|