document-drive 1.0.0-experimental.15 → 1.0.0-experimental.17

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-drive",
3
- "version": "1.0.0-experimental.15",
3
+ "version": "1.0.0-experimental.17",
4
4
  "license": "AGPL-3.0-only",
5
5
  "type": "module",
6
6
  "module": "./src/index.ts",
@@ -720,6 +720,10 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
720
720
  private _buildDocument<T extends Document>(
721
721
  documentStorage: DocumentStorage<T>, options?: GetDocumentOptions
722
722
  ): T {
723
+ if (documentStorage.state && (!options || options.checkHashes === false)) {
724
+ return documentStorage as T;
725
+ }
726
+
723
727
  const documentModel = this._getDocumentModel(
724
728
  documentStorage.documentType
725
729
  );
@@ -730,10 +734,6 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
730
734
  ) : documentStorage.operations;
731
735
  const operations = baseUtils.documentHelpers.garbageCollectDocumentOperations(revisionOperations);
732
736
 
733
- if (documentStorage.state && (!options || options.checkHashes === false)) {
734
- return documentStorage as T;
735
- }
736
-
737
737
  return baseUtils.replayDocument(
738
738
  documentStorage.initialState,
739
739
  operations,
@@ -741,7 +741,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
741
741
  undefined,
742
742
  documentStorage,
743
743
  undefined,
744
- { checkHashes: options?.checkHashes ?? true }
744
+ { checkHashes: options?.checkHashes ?? true, reuseOperationResultingState: true }
745
745
  ) as T;
746
746
  }
747
747
 
@@ -841,7 +841,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
841
841
  }>
842
842
  ) {
843
843
  if (!this.storage.addDocumentOperationsWithTransaction) {
844
- const documentStorage = await this.storage.getDocument(drive, id);
844
+ const documentStorage = await this.storage.getDocument(drive, id, skip);
845
845
  const result = await callback(documentStorage);
846
846
  // saves the applied operations to storage
847
847
  if (
@@ -138,6 +138,7 @@ export class PrismaStorage implements IDriveStorage {
138
138
  lastModified: document.lastModified,
139
139
  revision: JSON.stringify(document.revision),
140
140
  id,
141
+ state: JSON.stringify(document.state)
141
142
  }
142
143
  });
143
144
  }
@@ -147,8 +148,7 @@ export class PrismaStorage implements IDriveStorage {
147
148
  drive: string,
148
149
  id: string,
149
150
  operations: Operation[],
150
- header: DocumentHeader,
151
- newState: State<any, any> | undefined = undefined
151
+ state: ExtendedState<unknown, unknown>
152
152
  ): Promise<void> {
153
153
  const document = await this.getDocument(drive, id, tx);
154
154
  if (!document) {
@@ -181,7 +181,7 @@ export class PrismaStorage implements IDriveStorage {
181
181
  data: {
182
182
  lastModified: header.lastModified,
183
183
  revision: JSON.stringify(header.revision),
184
- state: JSON.stringify(newState)
184
+ state: JSON.stringify(header.state)
185
185
  }
186
186
  });
187
187
  } catch (e) {
@@ -244,15 +244,14 @@ export class PrismaStorage implements IDriveStorage {
244
244
  throw new Error(`Document with id ${id} not found`);
245
245
  }
246
246
  result = await callback(document);
247
-
247
+ console.log(result);
248
248
  const { operations, header, newState } = result;
249
249
  return this._addDocumentOperations(
250
250
  tx,
251
251
  drive,
252
252
  id,
253
253
  operations,
254
- header,
255
- newState
254
+ header as ExtendedState<unknown, unknown>,
256
255
  );
257
256
  }, { isolationLevel: "Serializable" });
258
257
 
@@ -311,18 +310,18 @@ export class PrismaStorage implements IDriveStorage {
311
310
  id: id,
312
311
  driveId: driveId
313
312
  },
314
- include: {
315
- operations: {
316
- orderBy: {
317
- index: 'asc'
318
- },
319
- include: {
320
- attachments: true
321
- }
322
- }
323
- }
324
313
  });
325
314
 
315
+ const operations = (await (tx ?? this.db).operation.findMany({
316
+ where: {
317
+ driveId: driveId,
318
+ documentId: id
319
+ },
320
+ orderBy: {
321
+ index: 'asc'
322
+ },
323
+ }))
324
+
326
325
  if (result === null) {
327
326
  throw new Error(`Document with id ${id} not found`);
328
327
  }
@@ -339,14 +338,14 @@ export class PrismaStorage implements IDriveStorage {
339
338
  state: JSON.parse(dbDoc.state!) as State<unknown, unknown>,
340
339
  lastModified: new Date(dbDoc.lastModified).toISOString(),
341
340
  operations: {
342
- global: dbDoc.operations
341
+ global: operations
343
342
  .filter(op => op.scope === 'global' && !op.clipboard)
344
343
  .map(storageToOperation),
345
- local: dbDoc.operations
344
+ local: operations
346
345
  .filter(op => op.scope === 'local' && !op.clipboard)
347
346
  .map(storageToOperation)
348
347
  },
349
- clipboard: dbDoc.operations
348
+ clipboard: operations
350
349
  .filter(op => op.clipboard)
351
350
  .map(storageToOperation),
352
351
  revision: JSON.parse(dbDoc.revision) as Record<OperationScope, number>