document-drive 0.0.22 → 0.0.24

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.
@@ -1,16 +1,45 @@
1
- import { DocumentDriveDocument } from 'document-model-libs/document-drive';
2
- import { Document } from 'document-model/document';
1
+ import type {
2
+ DocumentDriveAction,
3
+ DocumentDriveDocument
4
+ } from 'document-model-libs/document-drive';
5
+ import type {
6
+ BaseAction,
7
+ Document,
8
+ DocumentHeader,
9
+ Operation
10
+ } from 'document-model/document';
11
+
12
+ export type DocumentStorage<D extends Document = Document> = Omit<
13
+ D,
14
+ 'state' | 'attachments'
15
+ >;
16
+ export type DocumentDriveStorage = DocumentStorage<DocumentDriveDocument>;
3
17
 
4
18
  export interface IStorage {
5
19
  getDocuments: (drive: string) => Promise<string[]>;
6
- getDocument(drive: string, id: string): Promise<Document>;
7
- saveDocument(drive: string, id: string, document: Document): Promise<void>;
20
+ getDocument(drive: string, id: string): Promise<DocumentStorage>;
21
+ createDocument(
22
+ drive: string,
23
+ id: string,
24
+ document: DocumentStorage
25
+ ): Promise<void>;
26
+ addDocumentOperations(
27
+ drive: string,
28
+ id: string,
29
+ operations: Operation[],
30
+ header: DocumentHeader
31
+ ): Promise<void>;
8
32
  deleteDocument(drive: string, id: string): Promise<void>;
9
33
  }
10
34
 
11
35
  export interface IDriveStorage extends IStorage {
12
36
  getDrives(): Promise<string[]>;
13
- getDrive(id: string): Promise<DocumentDriveDocument>;
14
- saveDrive(drive: DocumentDriveDocument): Promise<void>;
37
+ getDrive(id: string): Promise<DocumentDriveStorage>;
38
+ createDrive(id: string, drive: DocumentDriveStorage): Promise<void>;
15
39
  deleteDrive(id: string): Promise<void>;
40
+ addDriveOperations(
41
+ id: string,
42
+ operations: Operation<DocumentDriveAction | BaseAction>[],
43
+ header: DocumentHeader
44
+ ): Promise<void>;
16
45
  }
package/src/utils.ts CHANGED
@@ -3,7 +3,13 @@ import {
3
3
  documentModel as DocumentDriveModel,
4
4
  z
5
5
  } from 'document-model-libs/document-drive';
6
- import { Document } from 'document-model/document';
6
+ import {
7
+ Action,
8
+ BaseAction,
9
+ Document,
10
+ DocumentOperations,
11
+ Operation
12
+ } from 'document-model/document';
7
13
 
8
14
  export function isDocumentDrive(
9
15
  document: Document
@@ -13,3 +19,14 @@ export function isDocumentDrive(
13
19
  z.DocumentDriveStateSchema().safeParse(document.state.global).success
14
20
  );
15
21
  }
22
+
23
+ export function mergeOperations<A extends Action = Action>(
24
+ currentOperations: DocumentOperations<A>,
25
+ newOperations: Operation<A | BaseAction>[]
26
+ ): DocumentOperations<A> {
27
+ return newOperations.reduce((acc, curr) => {
28
+ const operations = acc[curr.scope] ?? [];
29
+ acc[curr.scope] = [...operations, curr] as Operation<A>[];
30
+ return acc;
31
+ }, currentOperations);
32
+ }