@appweaver/data 2.5.0 → 2.6.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 +4 -2
- package/config.d.ts +1 -0
- package/config.js +1 -0
- package/database/database-config.d.ts +9 -0
- package/database/database-config.js +56 -1
- package/database/database-error.d.ts +8 -0
- package/database/database-error.js +18 -0
- package/database/prisma-database.js +16 -5
- package/database/setup-database.d.ts +23 -0
- package/database/setup-database.js +59 -0
- package/database/sqlserver-extension.js +21 -107
- package/database/telemetry-extension.d.ts +10 -0
- package/database/telemetry-extension.js +52 -0
- package/export/export-service.js +4 -0
- package/factory/create-model.d.ts +1 -1
- package/factory/create-model.js +75 -4
- package/factory/create-service.js +8 -0
- package/index.d.ts +2 -0
- package/index.js +5 -0
- package/internal.d.ts +2 -0
- package/internal.js +3 -1
- package/module.js +6 -3
- package/package.json +3 -3
- package/resource/resource-service.d.ts +32 -7
- package/resource/resource-service.js +244 -35
- package/resource/schemas/resource-filter-schema.d.ts +8 -0
- package/resource/schemas/resource-filter-schema.js +40 -1
- package/resource/schemas/resource-sort-schema.js +10 -0
- package/resource/utils/cursor-util.d.ts +13 -0
- package/resource/utils/cursor-util.js +21 -0
- package/resource/utils/filter-util.d.ts +9 -0
- package/resource/utils/filter-util.js +17 -0
- package/resource/utils/sort-util.d.ts +14 -0
- package/resource/utils/sort-util.js +1 -0
- package/resource/utils/where-util.d.ts +20 -0
- package/resource/utils/where-util.js +210 -0
- package/seeder/create-seeder.d.ts +3 -0
- package/seeder/create-seeder.js +1 -1
- package/seeder/seeder.d.ts +4 -2
- package/seeder/seeder.js +56 -23
- package/spatial/geo-schema.d.ts +22 -0
- package/spatial/geo-schema.js +60 -0
- package/spatial/spatial-config.d.ts +21 -0
- package/spatial/spatial-config.js +52 -0
- package/spatial/spatial-engine.d.ts +78 -0
- package/spatial/spatial-engine.js +232 -0
- package/spatial/spatial-extension.d.ts +10 -0
- package/spatial/spatial-extension.js +100 -0
- package/spatial/spatial-service.d.ts +54 -0
- package/spatial/spatial-service.js +213 -0
- package/spatial/utils/box-util.d.ts +31 -0
- package/spatial/utils/box-util.js +62 -0
- package/timeseries/database-time-series.js +2 -13
- package/timeseries/timescale-config.d.ts +1 -1
- package/timeseries/timescale-time-series.d.ts +6 -4
- package/timeseries/timescale-time-series.js +46 -19
- package/utils/schema-util.d.ts +8 -0
- package/utils/schema-util.js +22 -0
- package/utils/write-data-util.d.ts +22 -0
- package/utils/write-data-util.js +144 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Matches a record against a `where` filter of Prisma without the database, i.e. the read restrictions of a policy
|
|
3
|
+
* against the resource of an event. The filter may combine `AND`, `OR` and `NOT` with the scalar conditions (`equals`,
|
|
4
|
+
* `not`, `in`, `notIn`, `lt`, `lte`, `gt`, `gte`, `contains`, `startsWith`, `endsWith`, with `mode: 'insensitive'`).
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} record - The record to match, its dates as `Date` objects or ISO strings.
|
|
7
|
+
* @param {Object} where - The filter, in the native Prisma filter syntax.
|
|
8
|
+
* @return {boolean | undefined} Whether the record matches, or `undefined` for a filter it cannot evaluate, i.e. a
|
|
9
|
+
* relation filter or a field the record lacks, which only the database can match.
|
|
10
|
+
*/
|
|
11
|
+
export declare function matchesWhere(record: Record<string, any>, where: Record<string, any> | null | undefined): boolean | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Maps a query filter of the resource routes, i.e. `{ status: 'open', total: { _gt: 100 } }`, to the `where` filter of
|
|
14
|
+
* Prisma {@link matchesWhere} matches, i.e. the filter of a realtime subscription.
|
|
15
|
+
*
|
|
16
|
+
* @param {Object} filter - The query filter.
|
|
17
|
+
* @param {string} modelName - The name of the model the filter is matched against.
|
|
18
|
+
* @return {Object} The `where` filter.
|
|
19
|
+
*/
|
|
20
|
+
export declare function queryFilterWhere(filter: Record<string, unknown>, modelName: string): Record<string, any>;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.matchesWhere = matchesWhere;
|
|
4
|
+
exports.queryFilterWhere = queryFilterWhere;
|
|
5
|
+
const common_1 = require("@appweaver/common");
|
|
6
|
+
const filter_util_1 = require("./filter-util");
|
|
7
|
+
const OPERATORS = new Set([
|
|
8
|
+
'equals',
|
|
9
|
+
'not',
|
|
10
|
+
'in',
|
|
11
|
+
'notIn',
|
|
12
|
+
'lt',
|
|
13
|
+
'lte',
|
|
14
|
+
'gt',
|
|
15
|
+
'gte',
|
|
16
|
+
'contains',
|
|
17
|
+
'startsWith',
|
|
18
|
+
'endsWith',
|
|
19
|
+
'mode'
|
|
20
|
+
]);
|
|
21
|
+
/**
|
|
22
|
+
* Matches a record against a `where` filter of Prisma without the database, i.e. the read restrictions of a policy
|
|
23
|
+
* against the resource of an event. The filter may combine `AND`, `OR` and `NOT` with the scalar conditions (`equals`,
|
|
24
|
+
* `not`, `in`, `notIn`, `lt`, `lte`, `gt`, `gte`, `contains`, `startsWith`, `endsWith`, with `mode: 'insensitive'`).
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} record - The record to match, its dates as `Date` objects or ISO strings.
|
|
27
|
+
* @param {Object} where - The filter, in the native Prisma filter syntax.
|
|
28
|
+
* @return {boolean | undefined} Whether the record matches, or `undefined` for a filter it cannot evaluate, i.e. a
|
|
29
|
+
* relation filter or a field the record lacks, which only the database can match.
|
|
30
|
+
*/
|
|
31
|
+
function matchesWhere(record, where) {
|
|
32
|
+
if (!where) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
let result = true;
|
|
36
|
+
for (const [key, condition] of Object.entries(where)) {
|
|
37
|
+
if (condition === undefined) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
let matched;
|
|
41
|
+
if (key === 'AND') {
|
|
42
|
+
matched = every(((0, common_1.isArray)(condition) ? condition : [condition]).map((c) => matchesWhere(record, c)));
|
|
43
|
+
}
|
|
44
|
+
else if (key === 'OR') {
|
|
45
|
+
matched = some(condition.map((c) => matchesWhere(record, c)));
|
|
46
|
+
}
|
|
47
|
+
else if (key === 'NOT') {
|
|
48
|
+
matched = negate(some(((0, common_1.isArray)(condition) ? condition : [condition]).map((c) => matchesWhere(record, c))));
|
|
49
|
+
}
|
|
50
|
+
else if (!(key in record)) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
matched = matchesField(record[key], condition);
|
|
55
|
+
}
|
|
56
|
+
result = every([result, matched]);
|
|
57
|
+
if (result === false) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Maps a query filter of the resource routes, i.e. `{ status: 'open', total: { _gt: 100 } }`, to the `where` filter of
|
|
65
|
+
* Prisma {@link matchesWhere} matches, i.e. the filter of a realtime subscription.
|
|
66
|
+
*
|
|
67
|
+
* @param {Object} filter - The query filter.
|
|
68
|
+
* @param {string} modelName - The name of the model the filter is matched against.
|
|
69
|
+
* @return {Object} The `where` filter.
|
|
70
|
+
*/
|
|
71
|
+
function queryFilterWhere(filter, modelName) {
|
|
72
|
+
return (0, filter_util_1.mapQueryFilter)(filter, modelName);
|
|
73
|
+
}
|
|
74
|
+
function matchesField(value, condition) {
|
|
75
|
+
if (!isConditionObject(condition)) {
|
|
76
|
+
return isScalar(value) ? equals(value, condition) : undefined;
|
|
77
|
+
}
|
|
78
|
+
// A nested filter of a relation or a JSON field, matched by the database
|
|
79
|
+
if (!Object.keys(condition).every((key) => OPERATORS.has(key))) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (!isScalar(value)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const insensitive = condition.mode === 'insensitive';
|
|
86
|
+
const results = [];
|
|
87
|
+
for (const [operator, operand] of Object.entries(condition)) {
|
|
88
|
+
if (operand === undefined || operator === 'mode') {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
results.push(compare(value, operator, operand, insensitive));
|
|
92
|
+
}
|
|
93
|
+
return every(results);
|
|
94
|
+
}
|
|
95
|
+
function compare(value, operator, operand, insensitive) {
|
|
96
|
+
switch (operator) {
|
|
97
|
+
case 'equals':
|
|
98
|
+
return equals(value, operand, insensitive);
|
|
99
|
+
case 'not':
|
|
100
|
+
return isConditionObject(operand)
|
|
101
|
+
? negate(matchesField(value, operand))
|
|
102
|
+
: !equals(value, operand, insensitive);
|
|
103
|
+
case 'in':
|
|
104
|
+
return (0, common_1.isArray)(operand)
|
|
105
|
+
? operand.some((o) => equals(value, o, insensitive))
|
|
106
|
+
: undefined;
|
|
107
|
+
case 'notIn':
|
|
108
|
+
return (0, common_1.isArray)(operand)
|
|
109
|
+
? !operand.some((o) => equals(value, o, insensitive))
|
|
110
|
+
: undefined;
|
|
111
|
+
case 'lt':
|
|
112
|
+
case 'lte':
|
|
113
|
+
case 'gt':
|
|
114
|
+
case 'gte':
|
|
115
|
+
return order(value, operator, operand);
|
|
116
|
+
default:
|
|
117
|
+
return matchesText(value, operator, operand, insensitive);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function matchesText(value, operator, operand, insensitive) {
|
|
121
|
+
if (typeof value !== 'string' || typeof operand !== 'string') {
|
|
122
|
+
return value === null ? false : undefined;
|
|
123
|
+
}
|
|
124
|
+
const text = insensitive ? value.toLowerCase() : value;
|
|
125
|
+
const search = insensitive ? operand.toLowerCase() : operand;
|
|
126
|
+
if (operator === 'contains') {
|
|
127
|
+
return text.includes(search);
|
|
128
|
+
}
|
|
129
|
+
return operator === 'startsWith'
|
|
130
|
+
? text.startsWith(search)
|
|
131
|
+
: text.endsWith(search);
|
|
132
|
+
}
|
|
133
|
+
function order(value, operator, operand) {
|
|
134
|
+
if (value === null || operand === null) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const left = comparable(value, operand);
|
|
138
|
+
const right = comparable(operand, value);
|
|
139
|
+
if (left === undefined || right === undefined) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
switch (operator) {
|
|
143
|
+
case 'lt':
|
|
144
|
+
return left < right;
|
|
145
|
+
case 'lte':
|
|
146
|
+
return left <= right;
|
|
147
|
+
case 'gt':
|
|
148
|
+
return left > right;
|
|
149
|
+
default:
|
|
150
|
+
return left >= right;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function equals(value, operand, insensitive = false) {
|
|
154
|
+
if (value === null || operand === null) {
|
|
155
|
+
return value === operand;
|
|
156
|
+
}
|
|
157
|
+
if (insensitive && typeof value === 'string' && typeof operand === 'string') {
|
|
158
|
+
return value.toLowerCase() === operand.toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
const left = comparable(value, operand);
|
|
161
|
+
const right = comparable(operand, value);
|
|
162
|
+
return left !== undefined && left === right;
|
|
163
|
+
}
|
|
164
|
+
/** The value to compare, a date as its time when either side is a date, and a big integer as a number. */
|
|
165
|
+
function comparable(value, other) {
|
|
166
|
+
if (value instanceof Date) {
|
|
167
|
+
return value.getTime();
|
|
168
|
+
}
|
|
169
|
+
if (other instanceof Date && typeof value === 'string') {
|
|
170
|
+
const time = Date.parse(value);
|
|
171
|
+
return Number.isNaN(time) ? undefined : time;
|
|
172
|
+
}
|
|
173
|
+
if (typeof value === 'bigint') {
|
|
174
|
+
return Number(value);
|
|
175
|
+
}
|
|
176
|
+
if (typeof value === 'string' ||
|
|
177
|
+
typeof value === 'number' ||
|
|
178
|
+
typeof value === 'boolean') {
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
function isScalar(value) {
|
|
184
|
+
return (value === null ||
|
|
185
|
+
value instanceof Date ||
|
|
186
|
+
['string', 'number', 'boolean', 'bigint'].includes(typeof value));
|
|
187
|
+
}
|
|
188
|
+
function isConditionObject(value) {
|
|
189
|
+
return (typeof value === 'object' &&
|
|
190
|
+
value !== null &&
|
|
191
|
+
!(0, common_1.isArray)(value) &&
|
|
192
|
+
!(value instanceof Date));
|
|
193
|
+
}
|
|
194
|
+
/** `false` when any is false, `undefined` when any is unknown, `true` otherwise. */
|
|
195
|
+
function every(results) {
|
|
196
|
+
if (results.includes(false)) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return results.includes(undefined) ? undefined : true;
|
|
200
|
+
}
|
|
201
|
+
/** `true` when any is true, `undefined` when any is unknown, `false` otherwise. */
|
|
202
|
+
function some(results) {
|
|
203
|
+
if (results.includes(true)) {
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
return results.includes(undefined) ? undefined : false;
|
|
207
|
+
}
|
|
208
|
+
function negate(result) {
|
|
209
|
+
return result === undefined ? undefined : !result;
|
|
210
|
+
}
|
|
@@ -17,6 +17,9 @@ export type CreateSeederParams = {
|
|
|
17
17
|
* related to the seeders like wrong checksum or deleted seeder files.
|
|
18
18
|
* (default: false) **/
|
|
19
19
|
fixWarnings?: boolean;
|
|
20
|
+
/** Log the pending seeders and the warnings it would fix, without executing
|
|
21
|
+
* them or changing the database. (default: false) **/
|
|
22
|
+
dryRun?: boolean;
|
|
20
23
|
};
|
|
21
24
|
/**
|
|
22
25
|
* Creates the database seeder, loading the providers of the project first, and runs the pending seeders unless
|
package/seeder/create-seeder.js
CHANGED
|
@@ -24,7 +24,7 @@ async function createSeeder(params = {}) {
|
|
|
24
24
|
// Load the resources like the application does, i.e. for the JSON fields
|
|
25
25
|
// the database client of SQL Server serializes
|
|
26
26
|
await (0, resource_loader_1.loadResources)(scanPath, undefined, modules.flatMap(({ module }) => module.resources ? [module.resources] : []));
|
|
27
|
-
const seeder = new seeder_1.Seeder(seedersPath, params.continueOnError, params.fixWarnings, modules.flatMap(({ module }) => module.seeders ? [{ name: module.name, path: module.seeders }] : []));
|
|
27
|
+
const seeder = new seeder_1.Seeder(seedersPath, params.continueOnError, params.fixWarnings, modules.flatMap(({ module }) => module.seeders ? [{ name: module.name, path: module.seeders }] : []), params.dryRun);
|
|
28
28
|
if (params.autoSeed !== false) {
|
|
29
29
|
await seeder.seed();
|
|
30
30
|
}
|
package/seeder/seeder.d.ts
CHANGED
|
@@ -14,11 +14,13 @@ export declare class Seeder extends LifecycleManager {
|
|
|
14
14
|
private readonly _continueOnError;
|
|
15
15
|
private readonly _fixWarnings;
|
|
16
16
|
private readonly _moduleSeeders;
|
|
17
|
-
|
|
17
|
+
private readonly _dryRun;
|
|
18
|
+
constructor(_dirPath: string, _continueOnError?: boolean, _fixWarnings?: boolean, _moduleSeeders?: ModuleSeeders[], _dryRun?: boolean);
|
|
18
19
|
/**
|
|
19
20
|
* Executes the seeders by discovering, validating, and running seeder files.
|
|
20
21
|
* Logs the seeding process, including warnings for checksum mismatches
|
|
21
|
-
* and files that do not exist but are present in the database table.
|
|
22
|
+
* and files that do not exist but are present in the database table. A dry
|
|
23
|
+
* run only logs the seeders it would execute and the warnings it would fix.
|
|
22
24
|
*
|
|
23
25
|
* @return Resolves when all seeders have been processed. Logs relevant
|
|
24
26
|
* information during execution.
|
package/seeder/seeder.js
CHANGED
|
@@ -9,6 +9,7 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
9
9
|
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
10
10
|
const common_1 = require("@appweaver/common");
|
|
11
11
|
const internal_1 = require("@appweaver/core/internal");
|
|
12
|
+
const database_1 = require("../database");
|
|
12
13
|
const core_1 = require("@appweaver/core");
|
|
13
14
|
/**
|
|
14
15
|
* Represents a seeder class that manages the application seeders processes.
|
|
@@ -18,19 +19,22 @@ class Seeder extends internal_1.LifecycleManager {
|
|
|
18
19
|
_continueOnError;
|
|
19
20
|
_fixWarnings;
|
|
20
21
|
_moduleSeeders;
|
|
22
|
+
_dryRun;
|
|
21
23
|
/** @internal */
|
|
22
24
|
_db = (0, core_1.inject)(common_1.Database);
|
|
23
|
-
constructor(_dirPath, _continueOnError = false, _fixWarnings = false, _moduleSeeders = []) {
|
|
25
|
+
constructor(_dirPath, _continueOnError = false, _fixWarnings = false, _moduleSeeders = [], _dryRun = false) {
|
|
24
26
|
super();
|
|
25
27
|
this._dirPath = _dirPath;
|
|
26
28
|
this._continueOnError = _continueOnError;
|
|
27
29
|
this._fixWarnings = _fixWarnings;
|
|
28
30
|
this._moduleSeeders = _moduleSeeders;
|
|
31
|
+
this._dryRun = _dryRun;
|
|
29
32
|
}
|
|
30
33
|
/**
|
|
31
34
|
* Executes the seeders by discovering, validating, and running seeder files.
|
|
32
35
|
* Logs the seeding process, including warnings for checksum mismatches
|
|
33
|
-
* and files that do not exist but are present in the database table.
|
|
36
|
+
* and files that do not exist but are present in the database table. A dry
|
|
37
|
+
* run only logs the seeders it would execute and the warnings it would fix.
|
|
34
38
|
*
|
|
35
39
|
* @return Resolves when all seeders have been processed. Logs relevant
|
|
36
40
|
* information during execution.
|
|
@@ -66,47 +70,66 @@ class Seeder extends internal_1.LifecycleManager {
|
|
|
66
70
|
? ` and ${modulesCount} module${modulesCount === 1 ? '' : 's'}`
|
|
67
71
|
: ''}`);
|
|
68
72
|
console.log('');
|
|
73
|
+
const recorded = await this.recordedSeeders();
|
|
69
74
|
// For each seeder file, skip if it has already been seeded, otherwise
|
|
70
75
|
// execute all of it's exported functions and write results to a database
|
|
71
76
|
for (const seederFile of seederFiles) {
|
|
72
77
|
const seederName = seederFile.name;
|
|
73
|
-
const seeder =
|
|
78
|
+
const seeder = recorded.get(seederName);
|
|
74
79
|
if (seeder) {
|
|
75
80
|
const checksum = await this.seederHash(seederFile.path);
|
|
76
81
|
if (checksum !== seeder.checksum) {
|
|
77
82
|
console.warn(`Warning: seeder ${seederName} has different checksum: ${checksum}, expected checksum: ${seeder.checksum}`);
|
|
78
|
-
if (this._fixWarnings) {
|
|
83
|
+
if (this._fixWarnings && !this._dryRun) {
|
|
79
84
|
await this._db.client().seeder.update({
|
|
80
85
|
where: { id: seeder.id },
|
|
81
86
|
data: { checksum }
|
|
82
87
|
});
|
|
83
|
-
|
|
88
|
+
}
|
|
89
|
+
if (this._fixWarnings) {
|
|
90
|
+
console.log(`Fix: seeder checksum ${this._dryRun ? 'would be ' : ''}updated`);
|
|
84
91
|
}
|
|
85
92
|
console.log('');
|
|
86
93
|
}
|
|
87
94
|
continue;
|
|
88
95
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
96
|
+
if (this._dryRun) {
|
|
97
|
+
console.log(`Would seed ${seederName}`);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
console.log(`Seeding ${seederName}...`);
|
|
101
|
+
await this.executeSeeder(seederFile);
|
|
102
|
+
console.log(`${seederName} seeded`);
|
|
103
|
+
}
|
|
92
104
|
console.log('');
|
|
93
105
|
calledSeedersCount++;
|
|
94
106
|
}
|
|
95
107
|
// The seeders of a missing directory are not reported as deleted
|
|
96
|
-
const
|
|
108
|
+
const seederNames = new Set(seederFiles.map((file) => file.name));
|
|
109
|
+
const seedersWithoutFiles = [...recorded.values()].filter((seeder) => !seederNames.has(seeder.seederName) &&
|
|
110
|
+
!missing.includes(this.seederOwner(seeder.seederName)));
|
|
97
111
|
for (const seeder of seedersWithoutFiles) {
|
|
98
112
|
console.warn(`Warning: seeder ${seeder.seederName} does not exist but is present in database table`);
|
|
99
|
-
if (this._fixWarnings) {
|
|
113
|
+
if (this._fixWarnings && !this._dryRun) {
|
|
100
114
|
await this._db.client().seeder.delete({ where: { id: seeder.id } });
|
|
101
|
-
|
|
115
|
+
}
|
|
116
|
+
if (this._fixWarnings) {
|
|
117
|
+
console.log(`Fix: ${this._dryRun ? 'would remove' : 'removed'} seeder from database`);
|
|
102
118
|
}
|
|
103
119
|
console.log('');
|
|
104
120
|
}
|
|
121
|
+
const seeders = `${calledSeedersCount} seeder${calledSeedersCount === 1 ? '' : 's'}`;
|
|
105
122
|
if (calledSeedersCount === 0) {
|
|
106
123
|
console.log(`No pending seeders to execute`);
|
|
107
124
|
}
|
|
125
|
+
else if (this._dryRun) {
|
|
126
|
+
console.log(`${seeders} would be executed`);
|
|
127
|
+
}
|
|
108
128
|
else {
|
|
109
|
-
console.log(`Executed ${
|
|
129
|
+
console.log(`Executed ${seeders}`);
|
|
130
|
+
}
|
|
131
|
+
if (this._dryRun) {
|
|
132
|
+
console.log('Dry run, no seeder was run.');
|
|
110
133
|
}
|
|
111
134
|
}
|
|
112
135
|
/** Destroys the services the seeder started, i.e. the database connection. */
|
|
@@ -171,14 +194,28 @@ class Seeder extends internal_1.LifecycleManager {
|
|
|
171
194
|
}
|
|
172
195
|
});
|
|
173
196
|
}
|
|
174
|
-
/**
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
197
|
+
/**
|
|
198
|
+
* Reads the recorded seeders by their names. A dry run of a database that is not migrated yet has none, so it lists
|
|
199
|
+
* every seeder.
|
|
200
|
+
*
|
|
201
|
+
* @internal
|
|
202
|
+
*/
|
|
203
|
+
async recordedSeeders() {
|
|
204
|
+
try {
|
|
205
|
+
const seeders = await this._db.client().seeder.findMany();
|
|
206
|
+
return new Map(seeders.map((seeder) => [seeder.seederName, seeder]));
|
|
207
|
+
}
|
|
208
|
+
catch (e) {
|
|
209
|
+
// P1003 and P2021: the database or the seeders table does not exist
|
|
210
|
+
if (this._dryRun &&
|
|
211
|
+
(0, database_1.isPrismaError)(e) &&
|
|
212
|
+
['P1003', 'P2021'].includes(String(e.code))) {
|
|
213
|
+
console.log('The database is not migrated yet, every seeder is pending');
|
|
214
|
+
console.log('');
|
|
215
|
+
return new Map();
|
|
180
216
|
}
|
|
181
|
-
|
|
217
|
+
throw e;
|
|
218
|
+
}
|
|
182
219
|
}
|
|
183
220
|
/** @internal */
|
|
184
221
|
seederOwner(seederName) {
|
|
@@ -186,10 +223,6 @@ class Seeder extends internal_1.LifecycleManager {
|
|
|
186
223
|
return owner?.name ?? '';
|
|
187
224
|
}
|
|
188
225
|
/** @internal */
|
|
189
|
-
async getSeeder(seederName) {
|
|
190
|
-
return this._db.client().seeder.findFirst({ where: { seederName } });
|
|
191
|
-
}
|
|
192
|
-
/** @internal */
|
|
193
226
|
async seederHash(seederFile) {
|
|
194
227
|
return (0, common_1.makeHash)(node_fs_1.default.createReadStream(seederFile, 'utf8'));
|
|
195
228
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TSchema } from '@sinclair/typebox';
|
|
2
|
+
import { GeoField, GeoGeometryType } from '@appweaver/common';
|
|
3
|
+
/** A position `[lng, lat]` in WGS 84 degrees. */
|
|
4
|
+
export declare const GeoPositionSchema: () => TSchema;
|
|
5
|
+
/** A point given by its latitude and longitude. */
|
|
6
|
+
export declare const GeoLatLngSchema: () => TSchema;
|
|
7
|
+
/**
|
|
8
|
+
* The schema of a GeoJSON geometry of a type.
|
|
9
|
+
*
|
|
10
|
+
* @param {GeoGeometryType} type - The GeoJSON type, i.e. `Point`.
|
|
11
|
+
* @return {TSchema} The schema.
|
|
12
|
+
*/
|
|
13
|
+
export declare function geometrySchema(type: GeoGeometryType): TSchema;
|
|
14
|
+
/**
|
|
15
|
+
* The schema of the value of a geo field: a GeoJSON geometry of one of the types of the field, and for the input of a
|
|
16
|
+
* field holding points, a `{ lat, lng }` object too. A field with `required: false` is nullable.
|
|
17
|
+
*
|
|
18
|
+
* @param {GeoField} field - The geo field.
|
|
19
|
+
* @param {boolean} [input=false] - Whether the schema validates an input.
|
|
20
|
+
* @return {TSchema} The schema.
|
|
21
|
+
*/
|
|
22
|
+
export declare function geoFieldSchema(field: GeoField, input?: boolean): TSchema;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeoLatLngSchema = exports.GeoPositionSchema = void 0;
|
|
4
|
+
exports.geometrySchema = geometrySchema;
|
|
5
|
+
exports.geoFieldSchema = geoFieldSchema;
|
|
6
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
7
|
+
const common_1 = require("@appweaver/common");
|
|
8
|
+
/** A position `[lng, lat]` in WGS 84 degrees. */
|
|
9
|
+
const GeoPositionSchema = () => typebox_1.Type.Tuple([
|
|
10
|
+
typebox_1.Type.Number({ minimum: -180, maximum: 180 }),
|
|
11
|
+
typebox_1.Type.Number({ minimum: -90, maximum: 90 })
|
|
12
|
+
], { description: 'A position [lng, lat] in WGS 84 degrees' });
|
|
13
|
+
exports.GeoPositionSchema = GeoPositionSchema;
|
|
14
|
+
/** A point given by its latitude and longitude. */
|
|
15
|
+
const GeoLatLngSchema = () => typebox_1.Type.Object({
|
|
16
|
+
lat: typebox_1.Type.Number({ minimum: -90, maximum: 90 }),
|
|
17
|
+
lng: typebox_1.Type.Number({ minimum: -180, maximum: 180 })
|
|
18
|
+
}, { description: 'A point given by its latitude and longitude' });
|
|
19
|
+
exports.GeoLatLngSchema = GeoLatLngSchema;
|
|
20
|
+
const ring = () => typebox_1.Type.Array((0, exports.GeoPositionSchema)(), { minItems: 4 });
|
|
21
|
+
const polygonCoordinates = () => typebox_1.Type.Array(ring(), { minItems: 1 });
|
|
22
|
+
const COORDINATES = {
|
|
23
|
+
Point: exports.GeoPositionSchema,
|
|
24
|
+
MultiPoint: () => typebox_1.Type.Array((0, exports.GeoPositionSchema)(), { minItems: 1 }),
|
|
25
|
+
LineString: () => typebox_1.Type.Array((0, exports.GeoPositionSchema)(), { minItems: 2 }),
|
|
26
|
+
MultiLineString: () => typebox_1.Type.Array(typebox_1.Type.Array((0, exports.GeoPositionSchema)(), { minItems: 2 }), {
|
|
27
|
+
minItems: 1
|
|
28
|
+
}),
|
|
29
|
+
Polygon: polygonCoordinates,
|
|
30
|
+
MultiPolygon: () => typebox_1.Type.Array(polygonCoordinates(), { minItems: 1 })
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The schema of a GeoJSON geometry of a type.
|
|
34
|
+
*
|
|
35
|
+
* @param {GeoGeometryType} type - The GeoJSON type, i.e. `Point`.
|
|
36
|
+
* @return {TSchema} The schema.
|
|
37
|
+
*/
|
|
38
|
+
function geometrySchema(type) {
|
|
39
|
+
return typebox_1.Type.Object({ type: typebox_1.Type.Literal(type), coordinates: COORDINATES[type]() }, { description: `A GeoJSON ${type}` });
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The schema of the value of a geo field: a GeoJSON geometry of one of the types of the field, and for the input of a
|
|
43
|
+
* field holding points, a `{ lat, lng }` object too. A field with `required: false` is nullable.
|
|
44
|
+
*
|
|
45
|
+
* @param {GeoField} field - The geo field.
|
|
46
|
+
* @param {boolean} [input=false] - Whether the schema validates an input.
|
|
47
|
+
* @return {TSchema} The schema.
|
|
48
|
+
*/
|
|
49
|
+
function geoFieldSchema(field, input = false) {
|
|
50
|
+
const types = common_1.GEO_FIELD_GEOMETRIES[field.type] ?? common_1.GEO_FIELD_GEOMETRIES.geometry;
|
|
51
|
+
const branches = types.map(geometrySchema);
|
|
52
|
+
if (input && types.includes('Point')) {
|
|
53
|
+
branches.push((0, exports.GeoLatLngSchema)());
|
|
54
|
+
}
|
|
55
|
+
const options = (0, common_1.pickProperties)(field, ['hidden', 'description', 'example']);
|
|
56
|
+
const schema = branches.length === 1
|
|
57
|
+
? { ...branches[0], ...options }
|
|
58
|
+
: typebox_1.Type.Union(branches, options);
|
|
59
|
+
return field.required === false ? (0, common_1.Nullable)(schema) : schema;
|
|
60
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** How the spatial filters of the geo fields are matched. */
|
|
2
|
+
export type SpatialProvider = 'postgis' | 'database';
|
|
3
|
+
export declare const spatialConfig: Readonly<{
|
|
4
|
+
PROVIDER: "auto" | "database" | "postgis";
|
|
5
|
+
MAX_CANDIDATES: number;
|
|
6
|
+
MAX_VERTICES: number;
|
|
7
|
+
SETUP_TIMEOUT: number;
|
|
8
|
+
}>;
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the spatial provider of the configured database: `postgis` for `auto` on PostgreSQL, `database` otherwise.
|
|
11
|
+
*
|
|
12
|
+
* @return {SpatialProvider} The provider.
|
|
13
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` for `postgis` on a database other than PostgreSQL.
|
|
14
|
+
*/
|
|
15
|
+
export declare function spatialProvider(): SpatialProvider;
|
|
16
|
+
/**
|
|
17
|
+
* The maximum number of records a spatial filter matches on the configured database.
|
|
18
|
+
*
|
|
19
|
+
* @return {number} The maximum.
|
|
20
|
+
*/
|
|
21
|
+
export declare function spatialMaxCandidates(): number;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.spatialConfig = void 0;
|
|
4
|
+
exports.spatialProvider = spatialProvider;
|
|
5
|
+
exports.spatialMaxCandidates = spatialMaxCandidates;
|
|
6
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
7
|
+
const common_1 = require("@appweaver/common");
|
|
8
|
+
const database_config_1 = require("../database/database-config");
|
|
9
|
+
exports.spatialConfig = (0, common_1.defineConfig)('spatial', {
|
|
10
|
+
/** How the `_near`, `_within` and `_intersects` filters of the geo fields are matched: `auto` (`postgis` on
|
|
11
|
+
* PostgreSQL, `database` otherwise), `postgis` (a PostGIS geometry column with a GiST index, PostgreSQL only) or
|
|
12
|
+
* `database` (the bounding box columns narrow the records, the application matches them, on every database).
|
|
13
|
+
* Changing it changes the generated schema. Default: `'auto'`. */
|
|
14
|
+
PROVIDER: typebox_1.Type.Enum({ auto: 'auto', postgis: 'postgis', database: 'database' }, { default: 'auto' }),
|
|
15
|
+
/** Maximum number of records a `_near`, `_within` or `_intersects` filter matches, a larger match failing with
|
|
16
|
+
* `SPATIAL_TOO_MANY_CANDIDATES`. SQL Server takes at most `2000`, under its limit of parameters of a query.
|
|
17
|
+
* Default: `10000`. */
|
|
18
|
+
MAX_CANDIDATES: typebox_1.Type.Integer({ default: 10000, minimum: 1 }),
|
|
19
|
+
/** Maximum number of positions of a geometry, written or given to a spatial filter. Default: `10000`. */
|
|
20
|
+
MAX_VERTICES: typebox_1.Type.Integer({ default: 10000, minimum: 1 }),
|
|
21
|
+
/** Maximum time in milliseconds the `postgis` provider takes to create its triggers and write the geometries
|
|
22
|
+
* missing at startup, raised for the first start on a large table. Default: `60000` (1 min). */
|
|
23
|
+
SETUP_TIMEOUT: typebox_1.Type.Integer({ default: 60000, minimum: 1 })
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the spatial provider of the configured database: `postgis` for `auto` on PostgreSQL, `database` otherwise.
|
|
27
|
+
*
|
|
28
|
+
* @return {SpatialProvider} The provider.
|
|
29
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` for `postgis` on a database other than PostgreSQL.
|
|
30
|
+
*/
|
|
31
|
+
function spatialProvider() {
|
|
32
|
+
const type = (0, common_1.resolveDatabaseType)(database_config_1.databaseConfig.TYPE, database_config_1.databaseConfig.URL);
|
|
33
|
+
const provider = exports.spatialConfig.PROVIDER;
|
|
34
|
+
if (provider === 'postgis' && type !== common_1.DatabaseType.PostgreSQL) {
|
|
35
|
+
throw new common_1.ConfigurationError(common_1.ErrorCode.ConfigurationInvalid, `The postgis spatial provider needs a PostgreSQL database, not ${type}`);
|
|
36
|
+
}
|
|
37
|
+
if (provider === 'auto') {
|
|
38
|
+
return type === common_1.DatabaseType.PostgreSQL ? 'postgis' : 'database';
|
|
39
|
+
}
|
|
40
|
+
return provider;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The maximum number of records a spatial filter matches on the configured database.
|
|
44
|
+
*
|
|
45
|
+
* @return {number} The maximum.
|
|
46
|
+
*/
|
|
47
|
+
function spatialMaxCandidates() {
|
|
48
|
+
const type = (0, common_1.resolveDatabaseType)(database_config_1.databaseConfig.TYPE, database_config_1.databaseConfig.URL);
|
|
49
|
+
return type === common_1.DatabaseType.SQLServer
|
|
50
|
+
? Math.min(exports.spatialConfig.MAX_CANDIDATES, 2000)
|
|
51
|
+
: exports.spatialConfig.MAX_CANDIDATES;
|
|
52
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { GeoConfig, SpatialCondition, SpatialMatch } from '@appweaver/common';
|
|
2
|
+
import { PrismaDatabase } from '../database/prisma-database';
|
|
3
|
+
/** The geo fields of a model and the table holding them. */
|
|
4
|
+
export type GeoModel = {
|
|
5
|
+
/** The model name */
|
|
6
|
+
name: string;
|
|
7
|
+
/** The table name */
|
|
8
|
+
table: string;
|
|
9
|
+
/** The geo fields */
|
|
10
|
+
fields: GeoConfig;
|
|
11
|
+
};
|
|
12
|
+
/** The trigger keeping the geometry columns of a table in line with its GeoJSON. */
|
|
13
|
+
export type GeometryTrigger = {
|
|
14
|
+
/** The source of its function, compared with the one in the database */
|
|
15
|
+
source: string;
|
|
16
|
+
/** The statements creating the function and the trigger, then writing the geometries missing */
|
|
17
|
+
statements: string[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Finds the records whose geo field matches a spatial condition, for the `_near`, `_within` and `_intersects`
|
|
21
|
+
* operators of the resource queries.
|
|
22
|
+
*/
|
|
23
|
+
export declare abstract class SpatialEngine {
|
|
24
|
+
protected readonly _db: PrismaDatabase;
|
|
25
|
+
constructor(_db: PrismaDatabase);
|
|
26
|
+
/**
|
|
27
|
+
* Prepares the database for the geo fields of the models, called before the first spatial query.
|
|
28
|
+
*
|
|
29
|
+
* @param {GeoModel[]} models - The models with geo fields.
|
|
30
|
+
*/
|
|
31
|
+
abstract setup(models: GeoModel[]): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Finds the records whose geo field matches a condition, ordered by their distance for a `near` condition.
|
|
34
|
+
*
|
|
35
|
+
* @param {GeoModel} model - The model.
|
|
36
|
+
* @param {string} field - The geo field.
|
|
37
|
+
* @param {SpatialCondition} condition - The condition.
|
|
38
|
+
* @param {number} limit - The number of matches past which the search stops, returning more than `limit` of them.
|
|
39
|
+
* @return {Promise<SpatialMatch[]>} The matches, with their distances in meters for a `near` condition.
|
|
40
|
+
*/
|
|
41
|
+
abstract match(model: GeoModel, field: string, condition: SpatialCondition, limit: number): Promise<SpatialMatch[]>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The `database` engine, running on every database: the bounding box columns of a geo field narrow the records in a
|
|
45
|
+
* query of the database, then each geometry is matched in the application.
|
|
46
|
+
*/
|
|
47
|
+
export declare class DatabaseSpatialEngine extends SpatialEngine {
|
|
48
|
+
setup(): Promise<void>;
|
|
49
|
+
match(model: GeoModel, field: string, condition: SpatialCondition, limit: number): Promise<SpatialMatch[]>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The `postgis` engine, for PostgreSQL with the PostGIS extension: a trigger keeps a geometry column of each geo field
|
|
53
|
+
* in line with its GeoJSON, indexed with GiST and queried with the functions of PostGIS. The distances are measured on
|
|
54
|
+
* a sphere, like the `database` engine.
|
|
55
|
+
*/
|
|
56
|
+
export declare class PostgisSpatialEngine extends SpatialEngine {
|
|
57
|
+
/**
|
|
58
|
+
* Creates the trigger writing the geometry columns of every model whose trigger is missing or out of date, and
|
|
59
|
+
* writes the geometries of its records missing them, under an advisory lock, so the instances starting together
|
|
60
|
+
* set them up once. A start with every trigger in place changes nothing.
|
|
61
|
+
*
|
|
62
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` when the PostGIS extension is not installed, or a trigger is
|
|
63
|
+
* out of date and the user of the connection lacks the privileges to replace it.
|
|
64
|
+
*/
|
|
65
|
+
setup(models: GeoModel[]): Promise<void>;
|
|
66
|
+
match(model: GeoModel, field: string, condition: SpatialCondition, limit: number): Promise<SpatialMatch[]>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Builds the trigger of a table writing the geometry column of each geo field from its GeoJSON, on an insert and on
|
|
70
|
+
* an update changing the GeoJSON. Its function reads the columns directly, so a write of the other columns costs a
|
|
71
|
+
* comparison only, and does nothing once a migration dropped one of its columns, until the next start replaces it.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} table - The table name.
|
|
74
|
+
* @param {string[]} fields - The geo fields.
|
|
75
|
+
* @param {string} [schema] - The schema of the table, the one of the connection by default.
|
|
76
|
+
* @return {GeometryTrigger} The source of the function and the statements creating the trigger.
|
|
77
|
+
*/
|
|
78
|
+
export declare function geometryTrigger(table: string, fields: string[], schema?: string): GeometryTrigger;
|