@verdant-web/server 3.0.5 → 3.1.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 (70) hide show
  1. package/dist/esm/ServerLibrary.test.js +10 -0
  2. package/dist/esm/ServerLibrary.test.js.map +1 -1
  3. package/dist/esm/storage/index.d.ts +1 -0
  4. package/dist/esm/storage/index.js +1 -0
  5. package/dist/esm/storage/index.js.map +1 -1
  6. package/dist/esm/storage/sql/database.d.ts +9 -0
  7. package/dist/esm/storage/sql/database.js +24 -0
  8. package/dist/esm/storage/sql/database.js.map +1 -0
  9. package/dist/esm/storage/sql/sqlStorage.d.ts +2 -1
  10. package/dist/esm/storage/sql/sqlStorage.js +5 -13
  11. package/dist/esm/storage/sql/sqlStorage.js.map +1 -1
  12. package/dist/esm/storage/sql/tables.d.ts +2 -0
  13. package/dist/esm/storage/sqlShard/Databases.d.ts +21 -0
  14. package/dist/esm/storage/sqlShard/Databases.js +64 -0
  15. package/dist/esm/storage/sqlShard/Databases.js.map +1 -0
  16. package/dist/esm/storage/sqlShard/SqlBaselines.d.ts +23 -0
  17. package/dist/esm/storage/sqlShard/SqlBaselines.js +98 -0
  18. package/dist/esm/storage/sqlShard/SqlBaselines.js.map +1 -0
  19. package/dist/esm/storage/sqlShard/SqlFileMetadata.d.ts +18 -0
  20. package/dist/esm/storage/sqlShard/SqlFileMetadata.js +70 -0
  21. package/dist/esm/storage/sqlShard/SqlFileMetadata.js.map +1 -0
  22. package/dist/esm/storage/sqlShard/SqlOperations.d.ts +18 -0
  23. package/dist/esm/storage/sqlShard/SqlOperations.js +104 -0
  24. package/dist/esm/storage/sqlShard/SqlOperations.js.map +1 -0
  25. package/dist/esm/storage/sqlShard/SqlReplicas.d.ts +32 -0
  26. package/dist/esm/storage/sqlShard/SqlReplicas.js +164 -0
  27. package/dist/esm/storage/sqlShard/SqlReplicas.js.map +1 -0
  28. package/dist/esm/storage/sqlShard/_testData/unifiedData.d.ts +30 -0
  29. package/dist/esm/storage/sqlShard/_testData/unifiedData.js +42 -0
  30. package/dist/esm/storage/sqlShard/_testData/unifiedData.js.map +1 -0
  31. package/dist/esm/storage/sqlShard/database.d.ts +6 -0
  32. package/dist/esm/storage/sqlShard/database.js +28 -0
  33. package/dist/esm/storage/sqlShard/database.js.map +1 -0
  34. package/dist/esm/storage/sqlShard/migrations/v1.d.ts +3 -0
  35. package/dist/esm/storage/sqlShard/migrations/v1.js +48 -0
  36. package/dist/esm/storage/sqlShard/migrations/v1.js.map +1 -0
  37. package/dist/esm/storage/sqlShard/migrations.d.ts +5 -0
  38. package/dist/esm/storage/sqlShard/migrations.js +3 -0
  39. package/dist/esm/storage/sqlShard/migrations.js.map +1 -0
  40. package/dist/esm/storage/sqlShard/sqlShardStorage.d.ts +7 -0
  41. package/dist/esm/storage/sqlShard/sqlShardStorage.js +55 -0
  42. package/dist/esm/storage/sqlShard/sqlShardStorage.js.map +1 -0
  43. package/dist/esm/storage/sqlShard/tables.d.ts +38 -0
  44. package/dist/esm/storage/sqlShard/tables.js +2 -0
  45. package/dist/esm/storage/sqlShard/tables.js.map +1 -0
  46. package/dist/esm/storage/sqlShard/transfer.d.ts +6 -0
  47. package/dist/esm/storage/sqlShard/transfer.js +76 -0
  48. package/dist/esm/storage/sqlShard/transfer.js.map +1 -0
  49. package/dist/esm/storage/sqlShard/transfer.test.d.ts +1 -0
  50. package/dist/esm/storage/sqlShard/transfer.test.js +94 -0
  51. package/dist/esm/storage/sqlShard/transfer.test.js.map +1 -0
  52. package/package.json +2 -1
  53. package/src/ServerLibrary.test.ts +10 -0
  54. package/src/storage/index.ts +1 -0
  55. package/src/storage/sql/database.ts +32 -0
  56. package/src/storage/sql/sqlStorage.ts +6 -8
  57. package/src/storage/sql/tables.ts +4 -0
  58. package/src/storage/sqlShard/Databases.ts +80 -0
  59. package/src/storage/sqlShard/SqlBaselines.ts +140 -0
  60. package/src/storage/sqlShard/SqlFileMetadata.ts +98 -0
  61. package/src/storage/sqlShard/SqlOperations.ts +146 -0
  62. package/src/storage/sqlShard/SqlReplicas.ts +237 -0
  63. package/src/storage/sqlShard/_testData/unifiedData.ts +52 -0
  64. package/src/storage/sqlShard/database.ts +37 -0
  65. package/src/storage/sqlShard/migrations/v1.ts +53 -0
  66. package/src/storage/sqlShard/migrations.ts +3 -0
  67. package/src/storage/sqlShard/sqlShardStorage.ts +81 -0
  68. package/src/storage/sqlShard/tables.ts +45 -0
  69. package/src/storage/sqlShard/transfer.test.ts +130 -0
  70. package/src/storage/sqlShard/transfer.ts +107 -0
@@ -0,0 +1,107 @@
1
+ import { Kysely } from 'kysely';
2
+ import { openDatabase as openUnifiedDatabase } from '../sql/database.js';
3
+ import { Database as UnifiedDatabase } from '../sql/tables.js';
4
+ import { Database } from './tables.js';
5
+ import { openDatabase } from './database.js';
6
+
7
+ export async function transferToShards({
8
+ file,
9
+ directory,
10
+ }: {
11
+ file: string;
12
+ directory: string;
13
+ }) {
14
+ console.info(
15
+ `Transferring from unified database (${file}) to shards (${directory})...`,
16
+ );
17
+ const { db: unifiedDb, ready } = openUnifiedDatabase(file);
18
+ await ready;
19
+ const libraries = await unifiedDb
20
+ .selectFrom('ReplicaInfo')
21
+ .select('ReplicaInfo.libraryId')
22
+ .distinct()
23
+ .execute();
24
+
25
+ const dbs = await Promise.all(
26
+ libraries.map(async ({ libraryId }) => {
27
+ const db = await openDatabase(directory, libraryId);
28
+ await copyLibrary(libraryId, unifiedDb, db);
29
+ return db;
30
+ }),
31
+ );
32
+
33
+ return libraries.reduce(
34
+ (acc, { libraryId }, i) => {
35
+ acc[libraryId] = dbs[i];
36
+ return acc;
37
+ },
38
+ {} as Record<string, Kysely<Database>>,
39
+ );
40
+ }
41
+
42
+ async function copyLibrary(
43
+ libraryId: string,
44
+ source: Kysely<UnifiedDatabase>,
45
+ dest: Kysely<Database>,
46
+ ) {
47
+ const operations = await source
48
+ .selectFrom('OperationHistory')
49
+ .where('libraryId', '=', libraryId)
50
+ .selectAll()
51
+ .execute();
52
+ const baselines = await source
53
+ .selectFrom('DocumentBaseline')
54
+ .where('libraryId', '=', libraryId)
55
+ .selectAll()
56
+ .execute();
57
+ const replicas = await source
58
+ .selectFrom('ReplicaInfo')
59
+ .where('libraryId', '=', libraryId)
60
+ .selectAll()
61
+ .execute();
62
+ const fileMetadata = await source
63
+ .selectFrom('FileMetadata')
64
+ .where('libraryId', '=', libraryId)
65
+ .selectAll()
66
+ .execute();
67
+
68
+ for (const operation of operations) {
69
+ // @ts-expect-error
70
+ delete operation.libraryId;
71
+ }
72
+ for (const baseline of baselines) {
73
+ // @ts-expect-error
74
+ delete baseline.libraryId;
75
+ }
76
+ for (const replica of replicas) {
77
+ // @ts-expect-error
78
+ delete replica.libraryId;
79
+ }
80
+ for (const metadata of fileMetadata) {
81
+ // @ts-expect-error
82
+ delete metadata.libraryId;
83
+ }
84
+
85
+ await dest.transaction().execute(async (trx) => {
86
+ const actions: Promise<any>[] = [];
87
+ if (operations.length) {
88
+ actions.push(
89
+ trx.insertInto('OperationHistory').values(operations).execute(),
90
+ );
91
+ }
92
+ if (baselines.length) {
93
+ actions.push(
94
+ trx.insertInto('DocumentBaseline').values(baselines).execute(),
95
+ );
96
+ }
97
+ if (replicas.length) {
98
+ actions.push(trx.insertInto('ReplicaInfo').values(replicas).execute());
99
+ }
100
+ if (fileMetadata.length) {
101
+ actions.push(
102
+ trx.insertInto('FileMetadata').values(fileMetadata).execute(),
103
+ );
104
+ }
105
+ await Promise.all(actions);
106
+ });
107
+ }