document-drive 1.5.0 → 1.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/package.json +7 -7
- package/src/storage/drizzle/queries.ts +2 -2
- package/src/storage/drizzle.ts +35 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "document-drive",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.ts",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"nanoevents": "^9.0.0",
|
|
34
34
|
"sanitize-filename": "^1.6.3",
|
|
35
35
|
"uuid": "^9.0.1",
|
|
36
|
-
"document-model": "2.
|
|
37
|
-
"
|
|
38
|
-
"
|
|
36
|
+
"document-model": "2.8.0",
|
|
37
|
+
"document-model-libs": "1.117.1",
|
|
38
|
+
"@powerhousedao/scalars": "1.10.0"
|
|
39
39
|
},
|
|
40
40
|
"optionalDependencies": {
|
|
41
41
|
"@prisma/client": "^5.18.0",
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
"nanoevents": "^9.0.0",
|
|
66
66
|
"sanitize-filename": "^1.6.3",
|
|
67
67
|
"uuid": "^9.0.1",
|
|
68
|
-
"@powerhousedao/scalars": "1.
|
|
69
|
-
"document-model-libs": "1.117.
|
|
70
|
-
"document-model": "2.
|
|
68
|
+
"@powerhousedao/scalars": "1.10.0",
|
|
69
|
+
"document-model-libs": "1.117.1",
|
|
70
|
+
"document-model": "2.8.0"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
73
|
"check-types": "tsc --noEmit",
|
|
@@ -13,7 +13,7 @@ export const getDriveBySlug = async (db: NodePgDatabase, slug: string) => {
|
|
|
13
13
|
export const upsertDrive = async (
|
|
14
14
|
db: NodePgDatabase,
|
|
15
15
|
id: string,
|
|
16
|
-
drive: DocumentDriveStorage
|
|
16
|
+
drive: DocumentDriveStorage,
|
|
17
17
|
) => {
|
|
18
18
|
const [result] = await db
|
|
19
19
|
.update(drivesTable)
|
|
@@ -37,7 +37,7 @@ export const createDocumentQuery = async (
|
|
|
37
37
|
db: NodePgDatabase,
|
|
38
38
|
driveId: string,
|
|
39
39
|
documentId: string,
|
|
40
|
-
document: DocumentStorage
|
|
40
|
+
document: DocumentStorage,
|
|
41
41
|
) => {
|
|
42
42
|
return db.insert(documentsTable).values({
|
|
43
43
|
name: document.name,
|
package/src/storage/drizzle.ts
CHANGED
|
@@ -64,7 +64,7 @@ import { PgQueryResultHKT, PgTransaction } from "drizzle-orm/pg-core";
|
|
|
64
64
|
function storageToOperation(
|
|
65
65
|
op: typeof operationsTable.$inferSelect & {
|
|
66
66
|
attachments?: AttachmentInput[];
|
|
67
|
-
}
|
|
67
|
+
},
|
|
68
68
|
): Operation {
|
|
69
69
|
const operation: Operation = {
|
|
70
70
|
id: op.opId || undefined,
|
|
@@ -92,7 +92,7 @@ export type DrizzleStorageOptions = {
|
|
|
92
92
|
|
|
93
93
|
function getRetryTransactionsClient<T extends NodePgDatabase>(
|
|
94
94
|
db: T,
|
|
95
|
-
backOffOptions?: Partial<IBackOffOptions
|
|
95
|
+
backOffOptions?: Partial<IBackOffOptions>,
|
|
96
96
|
) {
|
|
97
97
|
return db;
|
|
98
98
|
}
|
|
@@ -125,7 +125,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
125
125
|
async addDriveOperations(
|
|
126
126
|
id: string,
|
|
127
127
|
operations: Operation[],
|
|
128
|
-
header: DocumentHeader
|
|
128
|
+
header: DocumentHeader,
|
|
129
129
|
): Promise<void> {
|
|
130
130
|
await this.addDocumentOperations("drives", id, operations, header);
|
|
131
131
|
}
|
|
@@ -135,19 +135,19 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
135
135
|
callback: (document: DocumentDriveStorage) => Promise<{
|
|
136
136
|
operations: Operation<DocumentDriveAction | BaseAction>[];
|
|
137
137
|
header: DocumentHeader;
|
|
138
|
-
}
|
|
138
|
+
}>,
|
|
139
139
|
) {
|
|
140
140
|
return this.addDocumentOperationsWithTransaction(
|
|
141
141
|
"drives",
|
|
142
142
|
drive,
|
|
143
|
-
(document) => callback(document as DocumentDriveStorage)
|
|
143
|
+
(document) => callback(document as DocumentDriveStorage),
|
|
144
144
|
);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
async createDocument(
|
|
148
148
|
drive: string,
|
|
149
149
|
id: string,
|
|
150
|
-
document: DocumentStorage
|
|
150
|
+
document: DocumentStorage,
|
|
151
151
|
): Promise<void> {
|
|
152
152
|
await createDocumentQuery(this.db, drive, id, document);
|
|
153
153
|
}
|
|
@@ -161,7 +161,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
161
161
|
drive: string,
|
|
162
162
|
id: string,
|
|
163
163
|
operations: Operation[],
|
|
164
|
-
header: DocumentHeader
|
|
164
|
+
header: DocumentHeader,
|
|
165
165
|
): Promise<void> {
|
|
166
166
|
try {
|
|
167
167
|
await tx.insert(operationsTable).values(
|
|
@@ -182,7 +182,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
182
182
|
resultingState: op.resultingState
|
|
183
183
|
? Buffer.from(JSON.stringify(op.resultingState))
|
|
184
184
|
: undefined,
|
|
185
|
-
}))
|
|
185
|
+
})),
|
|
186
186
|
);
|
|
187
187
|
|
|
188
188
|
await tx
|
|
@@ -192,7 +192,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
192
192
|
revision: JSON.stringify(header.revision),
|
|
193
193
|
})
|
|
194
194
|
.where(
|
|
195
|
-
and(eq(documentsTable.id, id), eq(documentsTable.driveId, drive))
|
|
195
|
+
and(eq(documentsTable.id, id), eq(documentsTable.driveId, drive)),
|
|
196
196
|
);
|
|
197
197
|
|
|
198
198
|
await Promise.all(
|
|
@@ -211,10 +211,10 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
211
211
|
.where(
|
|
212
212
|
and(
|
|
213
213
|
eq(operationsTable.documentId, id),
|
|
214
|
-
eq(operationsTable.driveId, drive)
|
|
215
|
-
)
|
|
214
|
+
eq(operationsTable.driveId, drive),
|
|
215
|
+
),
|
|
216
216
|
);
|
|
217
|
-
})
|
|
217
|
+
}),
|
|
218
218
|
);
|
|
219
219
|
} catch (e) {
|
|
220
220
|
// P2002: Unique constraint failed
|
|
@@ -260,7 +260,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
260
260
|
operations: Operation[];
|
|
261
261
|
header: DocumentHeader;
|
|
262
262
|
newState?: State<any, any> | undefined;
|
|
263
|
-
}
|
|
263
|
+
}>,
|
|
264
264
|
) {
|
|
265
265
|
let result: {
|
|
266
266
|
operations: Operation[];
|
|
@@ -273,7 +273,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
273
273
|
const document = await this.getDocument(
|
|
274
274
|
drive,
|
|
275
275
|
id,
|
|
276
|
-
tx as unknown as NodePgDatabase<Record<string, never
|
|
276
|
+
tx as unknown as NodePgDatabase<Record<string, never>>,
|
|
277
277
|
);
|
|
278
278
|
if (!document) {
|
|
279
279
|
throw new Error(`Document with id ${id} not found`);
|
|
@@ -286,7 +286,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
286
286
|
{
|
|
287
287
|
accessMode: "read write",
|
|
288
288
|
isolationLevel: "serializable",
|
|
289
|
-
}
|
|
289
|
+
},
|
|
290
290
|
);
|
|
291
291
|
|
|
292
292
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
@@ -301,7 +301,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
301
301
|
drive: string,
|
|
302
302
|
id: string,
|
|
303
303
|
operations: Operation[],
|
|
304
|
-
header: DocumentHeader
|
|
304
|
+
header: DocumentHeader,
|
|
305
305
|
): Promise<void> {
|
|
306
306
|
return this._addDocumentOperations(
|
|
307
307
|
this.db as PgTransaction<
|
|
@@ -312,7 +312,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
312
312
|
drive,
|
|
313
313
|
id,
|
|
314
314
|
operations,
|
|
315
|
-
header
|
|
315
|
+
header,
|
|
316
316
|
);
|
|
317
317
|
}
|
|
318
318
|
async getDocuments(drive: string): Promise<string[]> {
|
|
@@ -329,7 +329,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
329
329
|
.select({ count: count() })
|
|
330
330
|
.from(documentsTable)
|
|
331
331
|
.where(
|
|
332
|
-
and(eq(documentsTable.id, id), eq(documentsTable.driveId, driveId))
|
|
332
|
+
and(eq(documentsTable.id, id), eq(documentsTable.driveId, driveId)),
|
|
333
333
|
);
|
|
334
334
|
if (!result) {
|
|
335
335
|
return false;
|
|
@@ -340,14 +340,14 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
340
340
|
async getDocument(
|
|
341
341
|
driveId: string,
|
|
342
342
|
id: string,
|
|
343
|
-
tx?: NodePgDatabase<Record<string, never
|
|
343
|
+
tx?: NodePgDatabase<Record<string, never>>,
|
|
344
344
|
) {
|
|
345
345
|
const db = tx ?? this.db;
|
|
346
346
|
const [result] = await db
|
|
347
347
|
.select()
|
|
348
348
|
.from(documentsTable)
|
|
349
349
|
.where(
|
|
350
|
-
and(eq(documentsTable.id, id), eq(documentsTable.driveId, driveId))
|
|
350
|
+
and(eq(documentsTable.id, id), eq(documentsTable.driveId, driveId)),
|
|
351
351
|
)
|
|
352
352
|
.limit(1);
|
|
353
353
|
|
|
@@ -357,7 +357,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
357
357
|
|
|
358
358
|
const cachedOperations = (await this.delegate?.getCachedOperations(
|
|
359
359
|
driveId,
|
|
360
|
-
id
|
|
360
|
+
id,
|
|
361
361
|
)) ?? {
|
|
362
362
|
global: [],
|
|
363
363
|
local: [],
|
|
@@ -371,16 +371,16 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
371
371
|
acc[scope] = lastIndex;
|
|
372
372
|
return acc;
|
|
373
373
|
},
|
|
374
|
-
{ global: -1, local: -1 }
|
|
374
|
+
{ global: -1, local: -1 },
|
|
375
375
|
);
|
|
376
376
|
|
|
377
377
|
const conditions = Object.entries(scopeIndex).map(
|
|
378
|
-
([scope, index]) => `("scope" = '${scope}' AND "index" > ${index})
|
|
378
|
+
([scope, index]) => `("scope" = '${scope}' AND "index" > ${index})`,
|
|
379
379
|
);
|
|
380
380
|
conditions.push(
|
|
381
381
|
`("scope" NOT IN (${Object.keys(cachedOperations)
|
|
382
382
|
.map((s) => `'${s}'`)
|
|
383
|
-
.join(", ")}))
|
|
383
|
+
.join(", ")}))`,
|
|
384
384
|
);
|
|
385
385
|
|
|
386
386
|
// retrieves operations with resulting state
|
|
@@ -413,7 +413,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
413
413
|
WHERE "driveId" = ${driveId} AND "documentId" = ${id}
|
|
414
414
|
AND (${conditions.join(" OR ")})
|
|
415
415
|
ORDER BY scope, index;
|
|
416
|
-
|
|
416
|
+
`,
|
|
417
417
|
);
|
|
418
418
|
const operationIds = queryOperations.map((o: Operation) => o.id);
|
|
419
419
|
const attachments = await this.db
|
|
@@ -429,7 +429,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
429
429
|
>(
|
|
430
430
|
(
|
|
431
431
|
acc: Record<string, Operation[]>,
|
|
432
|
-
operation: typeof operationsTable.$inferSelect
|
|
432
|
+
operation: typeof operationsTable.$inferSelect,
|
|
433
433
|
) => {
|
|
434
434
|
const scope = operation.scope as OperationScope;
|
|
435
435
|
if (!acc[scope]) {
|
|
@@ -437,7 +437,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
437
437
|
}
|
|
438
438
|
const result = storageToOperation(operation);
|
|
439
439
|
result.attachments = attachments.filter(
|
|
440
|
-
(a) => a.operationId === operation.id
|
|
440
|
+
(a) => a.operationId === operation.id,
|
|
441
441
|
);
|
|
442
442
|
result.attachments.forEach(({ hash, ...file }) => {
|
|
443
443
|
fileRegistry[hash] = file;
|
|
@@ -445,7 +445,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
445
445
|
acc[scope].push(result);
|
|
446
446
|
return acc;
|
|
447
447
|
},
|
|
448
|
-
cachedOperations
|
|
448
|
+
cachedOperations,
|
|
449
449
|
);
|
|
450
450
|
|
|
451
451
|
const dbDoc = result;
|
|
@@ -476,7 +476,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
476
476
|
await this.db
|
|
477
477
|
.delete(documentsTable)
|
|
478
478
|
.where(
|
|
479
|
-
and(eq(documentsTable.driveId, drive), eq(documentsTable.id, id))
|
|
479
|
+
and(eq(documentsTable.driveId, drive), eq(documentsTable.id, id)),
|
|
480
480
|
);
|
|
481
481
|
} catch (e: unknown) {
|
|
482
482
|
console.error(e);
|
|
@@ -524,7 +524,7 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
524
524
|
documentId: string,
|
|
525
525
|
index: number,
|
|
526
526
|
scope: string,
|
|
527
|
-
branch: string
|
|
527
|
+
branch: string,
|
|
528
528
|
): Promise<unknown> {
|
|
529
529
|
const [operation] = await this.db
|
|
530
530
|
.select()
|
|
@@ -535,8 +535,8 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
535
535
|
eq(operationsTable.documentId, documentId),
|
|
536
536
|
eq(operationsTable.index, index),
|
|
537
537
|
eq(operationsTable.scope, scope),
|
|
538
|
-
eq(operationsTable.branch, branch)
|
|
539
|
-
)
|
|
538
|
+
eq(operationsTable.branch, branch),
|
|
539
|
+
),
|
|
540
540
|
);
|
|
541
541
|
|
|
542
542
|
return operation?.resultingState?.toString();
|
|
@@ -546,19 +546,19 @@ export class DrizzleStorage implements IDriveStorage {
|
|
|
546
546
|
drive: string,
|
|
547
547
|
index: number,
|
|
548
548
|
scope: string,
|
|
549
|
-
branch: string
|
|
549
|
+
branch: string,
|
|
550
550
|
): Promise<unknown> {
|
|
551
551
|
return this.getOperationResultingState(
|
|
552
552
|
"drives",
|
|
553
553
|
drive,
|
|
554
554
|
index,
|
|
555
555
|
scope,
|
|
556
|
-
branch
|
|
556
|
+
branch,
|
|
557
557
|
);
|
|
558
558
|
}
|
|
559
559
|
|
|
560
560
|
async getSynchronizationUnitsRevision(
|
|
561
|
-
units: SynchronizationUnitQuery[]
|
|
561
|
+
units: SynchronizationUnitQuery[],
|
|
562
562
|
): Promise<
|
|
563
563
|
{
|
|
564
564
|
driveId: string;
|