@appweaver/data 2.4.1 → 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.
Files changed (105) hide show
  1. package/README.md +4 -2
  2. package/config.d.ts +3 -0
  3. package/config.js +3 -0
  4. package/database/database-config.d.ts +9 -0
  5. package/database/database-config.js +56 -1
  6. package/database/database-error.d.ts +8 -0
  7. package/database/database-error.js +18 -0
  8. package/database/prisma-database.js +18 -1
  9. package/database/setup-database.d.ts +23 -0
  10. package/database/setup-database.js +59 -0
  11. package/database/sqlserver-extension.d.ts +18 -0
  12. package/database/sqlserver-extension.js +183 -0
  13. package/database/telemetry-extension.d.ts +10 -0
  14. package/database/telemetry-extension.js +52 -0
  15. package/export/export-service.js +4 -0
  16. package/factory/create-model.d.ts +1 -1
  17. package/factory/create-model.js +135 -5
  18. package/factory/create-service.js +8 -0
  19. package/index.d.ts +3 -0
  20. package/index.js +6 -0
  21. package/internal.d.ts +2 -0
  22. package/internal.js +3 -1
  23. package/module.js +23 -3
  24. package/package.json +3 -3
  25. package/prisma/client/browser.d.ts +10 -0
  26. package/prisma/client/client.d.ts +10 -0
  27. package/prisma/client/commonInputTypes.d.ts +81 -0
  28. package/prisma/client/enums.d.ts +6 -1
  29. package/prisma/client/enums.js +6 -0
  30. package/prisma/client/internal/class.d.ts +22 -0
  31. package/prisma/client/internal/class.js +4 -4
  32. package/prisma/client/internal/prismaNamespace.d.ts +175 -1
  33. package/prisma/client/internal/prismaNamespace.js +19 -1
  34. package/prisma/client/internal/prismaNamespaceBrowser.d.ts +20 -0
  35. package/prisma/client/internal/prismaNamespaceBrowser.js +19 -1
  36. package/prisma/client/models/TwoFactorMethod.d.ts +1140 -0
  37. package/prisma/client/models/TwoFactorMethod.js +2 -0
  38. package/prisma/client/models/TwoFactorRecoveryCode.d.ts +1005 -0
  39. package/prisma/client/models/TwoFactorRecoveryCode.js +2 -0
  40. package/prisma/client/models.d.ts +2 -0
  41. package/resource/resource-service.d.ts +32 -7
  42. package/resource/resource-service.js +248 -35
  43. package/resource/schemas/resource-filter-schema.d.ts +8 -0
  44. package/resource/schemas/resource-filter-schema.js +40 -1
  45. package/resource/schemas/resource-sort-schema.js +10 -0
  46. package/resource/utils/cursor-util.d.ts +13 -0
  47. package/resource/utils/cursor-util.js +21 -0
  48. package/resource/utils/filter-util.d.ts +9 -0
  49. package/resource/utils/filter-util.js +17 -0
  50. package/resource/utils/prune-util.js +3 -1
  51. package/resource/utils/sort-util.d.ts +14 -0
  52. package/resource/utils/sort-util.js +1 -0
  53. package/resource/utils/where-util.d.ts +20 -0
  54. package/resource/utils/where-util.js +210 -0
  55. package/resources.d.ts +4 -0
  56. package/resources.js +17 -9
  57. package/security/auth-config.d.ts +3 -0
  58. package/security/auth-config.js +6 -1
  59. package/security/create-auth-resources.d.ts +3 -3
  60. package/security/create-auth-resources.js +44 -7
  61. package/security/helper.d.ts +6 -0
  62. package/security/helper.js +11 -0
  63. package/security/resources/two-factor-method/model.d.ts +2 -0
  64. package/security/resources/two-factor-method/model.js +64 -0
  65. package/security/resources/two-factor-method/service.d.ts +2 -0
  66. package/security/resources/two-factor-method/service.js +7 -0
  67. package/security/resources/two-factor-recovery-code/model.d.ts +2 -0
  68. package/security/resources/two-factor-recovery-code/model.js +49 -0
  69. package/security/resources/two-factor-recovery-code/service.d.ts +2 -0
  70. package/security/resources/two-factor-recovery-code/service.js +7 -0
  71. package/security/store/database-security-store.d.ts +2 -2
  72. package/security/store/database-security-store.js +11 -1
  73. package/seeder/create-seeder.d.ts +3 -0
  74. package/seeder/create-seeder.js +6 -2
  75. package/seeder/seeder.d.ts +4 -2
  76. package/seeder/seeder.js +56 -23
  77. package/spatial/geo-schema.d.ts +22 -0
  78. package/spatial/geo-schema.js +60 -0
  79. package/spatial/spatial-config.d.ts +21 -0
  80. package/spatial/spatial-config.js +52 -0
  81. package/spatial/spatial-engine.d.ts +78 -0
  82. package/spatial/spatial-engine.js +232 -0
  83. package/spatial/spatial-extension.d.ts +10 -0
  84. package/spatial/spatial-extension.js +100 -0
  85. package/spatial/spatial-service.d.ts +54 -0
  86. package/spatial/spatial-service.js +213 -0
  87. package/spatial/utils/box-util.d.ts +31 -0
  88. package/spatial/utils/box-util.js +62 -0
  89. package/timeseries/database-series-config.d.ts +5 -0
  90. package/timeseries/database-series-config.js +15 -0
  91. package/timeseries/database-time-series.d.ts +36 -0
  92. package/timeseries/database-time-series.js +147 -0
  93. package/timeseries/index.d.ts +2 -0
  94. package/timeseries/index.js +18 -0
  95. package/timeseries/timescale-config.d.ts +6 -0
  96. package/timeseries/timescale-config.js +19 -0
  97. package/timeseries/timescale-time-series.d.ts +21 -0
  98. package/timeseries/timescale-time-series.js +158 -0
  99. package/timeseries/utils/series-sql-util.d.ts +90 -0
  100. package/timeseries/utils/series-sql-util.js +313 -0
  101. package/types/generated.d.ts +92 -0
  102. package/utils/schema-util.d.ts +8 -0
  103. package/utils/schema-util.js +22 -0
  104. package/utils/write-data-util.d.ts +22 -0
  105. package/utils/write-data-util.js +144 -0
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const create_service_1 = require("../../../factory/create-service");
4
+ const helper_1 = require("../../helper");
5
+ exports.default = (0, helper_1.isTwoFactorEnabled)()
6
+ ? (0, create_service_1.createService)({ modelName: 'TwoFactorMethod' })
7
+ : undefined;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("@appweaver/common").ResourceModel | undefined;
2
+ export default _default;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const common_1 = require("@appweaver/common");
4
+ const create_model_1 = require("../../../factory/create-model");
5
+ const helper_1 = require("../../helper");
6
+ const authModel = (0, helper_1.resourceAuthModel)();
7
+ exports.default = (0, helper_1.isTwoFactorEnabled)()
8
+ ? (0, create_model_1.createModel)({
9
+ name: 'TwoFactorRecoveryCode',
10
+ // Cascades from the auth model, so it is soft deleted together with it
11
+ softDelete: !!authModel?.config.softDelete,
12
+ audit: {
13
+ updatedAt: false,
14
+ createdById: false
15
+ },
16
+ scalars: {
17
+ // The SHA-256 of the normalized code, whose randomness makes a slow hash unnecessary
18
+ codeHash: {
19
+ type: 'string',
20
+ maxLength: 64,
21
+ unique: true,
22
+ hidden: true
23
+ },
24
+ usedAt: {
25
+ type: 'dateTime',
26
+ required: false
27
+ }
28
+ },
29
+ ...(authModel
30
+ ? {
31
+ relations: {
32
+ [(0, common_1.uncapitalize)(authModel.name)]: {
33
+ model: authModel.name,
34
+ type: 'manyToOne',
35
+ mappedBy: 'twoFactorRecoveryCodes',
36
+ // The codes are meaningless without their user
37
+ onDelete: 'cascade',
38
+ input: {
39
+ type: 'none'
40
+ },
41
+ output: {
42
+ type: 'none'
43
+ }
44
+ }
45
+ }
46
+ }
47
+ : {})
48
+ })
49
+ : undefined;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("@appweaver/common").Ctor<import("../../..").ResourceService<any, any, any, any, import("@appweaver/common").QueryFilter<any>, any>> | undefined;
2
+ export default _default;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const create_service_1 = require("../../../factory/create-service");
4
+ const helper_1 = require("../../helper");
5
+ exports.default = (0, helper_1.isTwoFactorEnabled)()
6
+ ? (0, create_service_1.createService)({ modelName: 'TwoFactorRecoveryCode' })
7
+ : undefined;
@@ -1,5 +1,5 @@
1
- import { SecurityStore, ValidationResult } from '@appweaver/common';
1
+ import { SecurityStore, TokenValidationResult } from '@appweaver/common';
2
2
  export declare class DatabaseSecurityStore extends SecurityStore {
3
3
  generateOneTimeToken<T = any>(purpose: string, data: T, ttl: number): Promise<string>;
4
- useOneTimeToken<T = any>(token: string, purpose: string, validateContent?: (value: T) => ValidationResult): Promise<T>;
4
+ useOneTimeToken<T = any>(token: string, purpose: string, validateContent?: (value: T) => TokenValidationResult<T>): Promise<T>;
5
5
  }
@@ -38,7 +38,17 @@ class DatabaseSecurityStore extends common_1.SecurityStore {
38
38
  if (validateContent) {
39
39
  const result = validateContent(data);
40
40
  if (!result.valid) {
41
- throw new common_1.AuthError(common_1.ErrorCode.AuthInvalidToken, result.message);
41
+ if (result.revoke) {
42
+ await oneTimeTokens.deleteMany({ where: { id: oneTimeToken.id } });
43
+ }
44
+ else if (result.data !== undefined) {
45
+ await oneTimeTokens.updateMany({
46
+ where: { id: oneTimeToken.id },
47
+ data: { data: result.data }
48
+ });
49
+ }
50
+ throw (result.error ??
51
+ new common_1.AuthError(common_1.ErrorCode.AuthInvalidToken, result.message));
42
52
  }
43
53
  }
44
54
  // Only the one of concurrent uses that removes the token gets its data
@@ -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
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSeeder = createSeeder;
4
4
  const common_1 = require("@appweaver/common");
5
- const database_config_1 = require("../database/database-config");
6
5
  const core_1 = require("@appweaver/core");
7
6
  const internal_1 = require("@appweaver/core/internal");
8
7
  const seeder_1 = require("./seeder");
8
+ const resource_loader_1 = require("../resource/resource-loader");
9
+ const database_config_1 = require("../database/database-config");
9
10
  /**
10
11
  * Creates the database seeder, loading the providers of the project first, and runs the pending seeders unless
11
12
  * `autoSeed` is disabled: the seeders of the modules declaring a `seeders` directory first, then the ones of the
@@ -20,7 +21,10 @@ async function createSeeder(params = {}) {
20
21
  // Define the providers and the services of the modules
21
22
  const modules = await (0, core_1.discoverModules)({ scanPath });
22
23
  await (0, internal_1.loadProviders)(modules, scanPath);
23
- const seeder = new seeder_1.Seeder(seedersPath, params.continueOnError, params.fixWarnings, modules.flatMap(({ module }) => module.seeders ? [{ name: module.name, path: module.seeders }] : []));
24
+ // Load the resources like the application does, i.e. for the JSON fields
25
+ // the database client of SQL Server serializes
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 }] : []), params.dryRun);
24
28
  if (params.autoSeed !== false) {
25
29
  await seeder.seed();
26
30
  }
@@ -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
- constructor(_dirPath: string, _continueOnError?: boolean, _fixWarnings?: boolean, _moduleSeeders?: ModuleSeeders[]);
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 = await this.getSeeder(seederName);
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
- console.log('Fix: seeder checksum updated');
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
- console.log(`Seeding ${seederName}...`);
90
- await this.executeSeeder(seederFile);
91
- console.log(`${seederName} seeded`);
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 seedersWithoutFiles = (await this.findSeedersWithoutFiles(seederFiles)).filter((seeder) => !missing.includes(this.seederOwner(seeder.seederName)));
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
- console.log('Fix: removed seeder from database');
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 ${calledSeedersCount} seeder${calledSeedersCount === 1 ? '' : 's'}`);
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
- /** @internal */
175
- async findSeedersWithoutFiles(seederFiles) {
176
- const seederNames = seederFiles.map((file) => file.name);
177
- return this._db.client().seeder.findMany({
178
- where: {
179
- seederName: { notIn: seederNames }
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;