@stamhoofd/backend-statistics-syncer 2.152.0 → 2.160.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 (82) hide show
  1. package/LICENSE.md +7 -7
  2. package/README.md +2 -2
  3. package/dist/index.js +17 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/migrations.js +19 -0
  6. package/dist/migrations.js.map +1 -0
  7. package/dist/src/boot.js +75 -0
  8. package/dist/src/boot.js.map +1 -0
  9. package/dist/src/crons.js +39 -0
  10. package/dist/src/crons.js.map +1 -0
  11. package/dist/src/database.js +88 -0
  12. package/dist/src/database.js.map +1 -0
  13. package/dist/src/migrate.js +38 -0
  14. package/dist/src/migrate.js.map +1 -0
  15. package/dist/src/migrations/1785958573-platform-statistics-schema.sql +382 -0
  16. package/dist/src/migrations/1786203367-postal-code-coordinates.sql +1203 -0
  17. package/dist/src/periods.js +86 -0
  18. package/dist/src/periods.js.map +1 -0
  19. package/dist/src/rows.js +209 -0
  20. package/dist/src/rows.js.map +1 -0
  21. package/dist/src/schema.js +28 -0
  22. package/dist/src/schema.js.map +1 -0
  23. package/dist/src/sources.js +10 -0
  24. package/dist/src/sources.js.map +1 -0
  25. package/dist/src/sync-state.js +36 -0
  26. package/dist/src/sync-state.js.map +1 -0
  27. package/dist/src/sync.js +608 -0
  28. package/dist/src/sync.js.map +1 -0
  29. package/dist/src/writer.js +63 -0
  30. package/dist/src/writer.js.map +1 -0
  31. package/dist-types/index.d.ts +2 -0
  32. package/dist-types/index.d.ts.map +1 -0
  33. package/dist-types/migrations.d.ts +2 -0
  34. package/dist-types/migrations.d.ts.map +1 -0
  35. package/dist-types/src/boot.d.ts +8 -0
  36. package/dist-types/src/boot.d.ts.map +1 -0
  37. package/dist-types/src/crons.d.ts +21 -0
  38. package/dist-types/src/crons.d.ts.map +1 -0
  39. package/dist-types/src/database.d.ts +28 -0
  40. package/dist-types/src/database.d.ts.map +1 -0
  41. package/dist-types/src/migrate.d.ts +2 -0
  42. package/dist-types/src/migrate.d.ts.map +1 -0
  43. package/dist-types/src/periods.d.ts +51 -0
  44. package/dist-types/src/periods.d.ts.map +1 -0
  45. package/dist-types/src/rows.d.ts +212 -0
  46. package/dist-types/src/rows.d.ts.map +1 -0
  47. package/dist-types/src/schema.d.ts +9 -0
  48. package/dist-types/src/schema.d.ts.map +1 -0
  49. package/dist-types/src/sources.d.ts +10 -0
  50. package/dist-types/src/sources.d.ts.map +1 -0
  51. package/dist-types/src/sync-state.d.ts +20 -0
  52. package/dist-types/src/sync-state.d.ts.map +1 -0
  53. package/dist-types/src/sync.d.ts +37 -0
  54. package/dist-types/src/sync.d.ts.map +1 -0
  55. package/dist-types/src/writer.d.ts +24 -0
  56. package/dist-types/src/writer.d.ts.map +1 -0
  57. package/index.ts +5 -8
  58. package/migrations.ts +16 -11
  59. package/package.json +63 -50
  60. package/src/boot.ts +7 -3
  61. package/src/crons.test.ts +33 -8
  62. package/src/crons.ts +7 -1
  63. package/src/database.test.ts +48 -11
  64. package/src/database.ts +30 -7
  65. package/src/migrate.ts +1 -1
  66. package/src/periods.ts +5 -2
  67. package/src/rows.test.ts +96 -16
  68. package/src/rows.ts +28 -6
  69. package/src/schema.test.ts +48 -19
  70. package/src/schema.ts +14 -4
  71. package/src/sync-state.test.ts +3 -1
  72. package/src/sync-state.ts +13 -4
  73. package/src/sync.test.ts +145 -32
  74. package/src/sync.ts +422 -91
  75. package/src/writer.ts +32 -10
  76. package/tsconfig.build.json +4 -11
  77. package/tsconfig.json +1 -1
  78. package/tsconfig.test.json +1 -3
  79. package/tsconfig.types.json +7 -0
  80. package/tests/vitest.global.setup.ts +0 -29
  81. package/tests/vitest.setup.ts +0 -24
  82. package/vitest.config.js +0 -18
package/src/writer.ts CHANGED
@@ -10,30 +10,49 @@ export const batchSize = 500;
10
10
  /** What identifies a row when no other key is given. `members` is keyed per period as well. */
11
11
  export const defaultKeyColumns = ['id'];
12
12
 
13
- export function buildUpsertQuery(table: string, columns: string[], escapeId: (value: string) => string, keyColumns: string[] = defaultKeyColumns): string {
13
+ export function buildUpsertQuery(
14
+ table: string,
15
+ columns: string[],
16
+ escapeId: (value: string) => string,
17
+ keyColumns: string[] = defaultKeyColumns,
18
+ ): string {
14
19
  const assignments = columns
15
- .filter(column => !keyColumns.includes(column))
16
- .map(column => `${escapeId(column)} = new.${escapeId(column)}`);
20
+ .filter((column) => !keyColumns.includes(column))
21
+ .map((column) => `${escapeId(column)} = new.${escapeId(column)}`);
17
22
 
18
23
  // The row alias is the MySQL 8 replacement for VALUES(col), which is deprecated.
19
- return `INSERT INTO ${escapeId(table)} (${columns.map(escapeId).join(', ')}) VALUES ? AS new`
20
- + (assignments.length > 0 ? ` ON DUPLICATE KEY UPDATE ${assignments.join(', ')}` : '');
24
+ return (
25
+ `INSERT INTO ${escapeId(table)} (${columns.map(escapeId).join(', ')}) VALUES ? AS new` +
26
+ (assignments.length > 0 ? ` ON DUPLICATE KEY UPDATE ${assignments.join(', ')}` : '')
27
+ );
21
28
  }
22
29
 
23
30
  /**
24
31
  * Write rows, replacing the ones that are already there. Running the same batch twice is a no-op, so
25
32
  * a run that crashed halfway can simply be repeated.
26
33
  */
27
- export async function upsertRows(table: string, columns: string[], rows: StatisticsRow[], keyColumns: string[] = defaultKeyColumns): Promise<number> {
34
+ export async function upsertRows(
35
+ table: string,
36
+ columns: string[],
37
+ rows: StatisticsRow[],
38
+ keyColumns: string[] = defaultKeyColumns,
39
+ ): Promise<number> {
28
40
  if (rows.length === 0) {
29
41
  return 0;
30
42
  }
31
43
 
32
44
  const connection = getStatisticsConnection();
33
- const query = buildUpsertQuery(table, columns, value => connection.escapeId(value), keyColumns);
45
+ const query = buildUpsertQuery(
46
+ table,
47
+ columns,
48
+ (value) => connection.escapeId(value),
49
+ keyColumns,
50
+ );
34
51
 
35
52
  for (let index = 0; index < rows.length; index += batchSize) {
36
- const batch = rows.slice(index, index + batchSize).map(row => columns.map(column => row[column] ?? null));
53
+ const batch = rows
54
+ .slice(index, index + batchSize)
55
+ .map((row) => columns.map((column) => row[column] ?? null));
37
56
  await connection.insert(query, [batch]);
38
57
  }
39
58
 
@@ -46,7 +65,10 @@ export async function deleteRows(table: string, ids: string[]): Promise<number>
46
65
  }
47
66
 
48
67
  const connection = getStatisticsConnection();
49
- const [result] = await connection.delete(`DELETE FROM ${connection.escapeId(table)} WHERE \`id\` IN (?)`, [ids]);
68
+ const [result] = await connection.delete(
69
+ `DELETE FROM ${connection.escapeId(table)} WHERE \`id\` IN (?)`,
70
+ [ids],
71
+ );
50
72
  return result.affectedRows;
51
73
  }
52
74
 
@@ -67,7 +89,7 @@ export async function* iterateIds(table: string, source: string): AsyncGenerator
67
89
  [source, cursor],
68
90
  { nestTables: false },
69
91
  );
70
- const ids = (rows as unknown as { id: string }[]).map(row => row.id);
92
+ const ids = (rows as unknown as { id: string }[]).map((row) => row.id);
71
93
  if (ids.length === 0) {
72
94
  return;
73
95
  }
@@ -1,17 +1,10 @@
1
1
  {
2
- "extends": "../../../tsconfig.base.json",
2
+ "extends": ["../../../tsconfig.base.json", "../../../tsconfig.runtime.json"],
3
3
  "compilerOptions": {
4
+ "tsBuildInfoFile": ".cache/build.tsbuildinfo",
4
5
  "rootDir": ".",
5
6
  "outDir": "dist"
6
7
  },
7
- "include": [
8
- "./index.ts",
9
- "./migrations.ts",
10
- "./src",
11
- "./stamhoofd.d.ts"
12
- ],
13
- "exclude": [
14
- "./src/**/*.spec.ts",
15
- "./src/**/*.test.ts"
16
- ]
8
+ "include": ["./index.ts", "./migrations.ts", "./src", "./stamhoofd.d.ts"],
9
+ "exclude": ["./src/**/*.spec.ts", "./src/**/*.test.ts"]
17
10
  }
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "files": [],
4
4
  "references": [
5
5
  {
6
- "path": "./tsconfig.build.json"
6
+ "path": "./tsconfig.types.json"
7
7
  },
8
8
  {
9
9
  "path": "./tsconfig.test.json"
@@ -5,9 +5,7 @@
5
5
  "outDir": "dist",
6
6
  "noEmit": true
7
7
  },
8
- "references": [
9
- { "path": "./tsconfig.build.json" }
10
- ],
8
+ "references": [{ "path": "./tsconfig.types.json" }],
11
9
  "include": [
12
10
  "./stamhoofd.d.ts",
13
11
  "../../../jest-extended.d.ts",
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": ["./tsconfig.build.json", "../../../tsconfig.types.json"],
3
+ "compilerOptions": {
4
+ "outDir": "dist-types",
5
+ "tsBuildInfoFile": ".cache/types.tsbuildinfo"
6
+ }
7
+ }
@@ -1,29 +0,0 @@
1
- import { TestUtils } from '@stamhoofd/test-utils';
2
- import path from 'path';
3
-
4
- // Dates are stored and compared in UTC everywhere else in the repo; a date column read back in
5
- // another zone lands a day off.
6
- process.env.TZ = 'UTC';
7
-
8
- if (new Date().getTimezoneOffset() !== 0) {
9
- throw new Error('Process should always run in UTC timezone');
10
- }
11
-
12
- const modelsPath = require.resolve('@stamhoofd/models');
13
-
14
- export async function setup() {
15
- TestUtils.globalSetup();
16
-
17
- const { Database, Migration } = await import('@simonbackx/simple-database');
18
- const { endStatisticsConnection } = await import('../src/database.js');
19
- const { runStatisticsMigrations } = await import('../src/schema.js');
20
-
21
- // The sync reads the source tables, so the main database has to be migrated as well.
22
- if (!await Migration.runAll(path.dirname(modelsPath) + '/migrations')) {
23
- throw new Error('Migrations failed');
24
- }
25
-
26
- await runStatisticsMigrations();
27
- await endStatisticsConnection();
28
- await Database.end();
29
- };
@@ -1,24 +0,0 @@
1
- import { Column, Database } from '@simonbackx/simple-database';
2
- import { endStatisticsConnection } from '../src/database.js';
3
- import { Version } from '@stamhoofd/structures';
4
- import { TestUtils } from '@stamhoofd/test-utils';
5
-
6
- Error.stackTraceLimit = 100;
7
-
8
- // Set version of saved structures
9
- Column.setJSONVersion(Version);
10
-
11
- // Set timezone!
12
- process.env.TZ = 'UTC';
13
-
14
- // Quick check
15
- if (new Date().getTimezoneOffset() !== 0) {
16
- throw new Error('Process should always run in UTC timezone');
17
- }
18
-
19
- afterAll(async () => {
20
- await endStatisticsConnection();
21
- await Database.end();
22
- });
23
-
24
- TestUtils.setup();
package/vitest.config.js DELETED
@@ -1,18 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- include: ['**/*.test.ts'], // only match TypeScript sources, so compiled copies in dist are never picked up
6
- globalSetup: './tests/vitest.global.setup.ts',
7
- setupFiles: ['./tests/vitest.setup.ts'],
8
- watch: false,
9
- globals: true,
10
- root: import.meta.dirname,
11
- isolate: true,
12
- coverage: {
13
- provider: 'v8',
14
- include: ['src/**'],
15
- reporter: ['text', 'html', 'lcov'],
16
- },
17
- },
18
- });