document-drive 0.0.5 → 0.0.7
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 +5 -2
- package/src/server/index.ts +0 -1
- package/src/storage/browser.ts +100 -0
- package/src/storage/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "document-drive",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.ts",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"document-model": "^1.0.13",
|
|
27
|
-
"document-model-libs": "^1.1.21"
|
|
27
|
+
"document-model-libs": "^1.1.21",
|
|
28
|
+
"localforage": "^1.10.0"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
31
|
"sanitize-filename": "^1.6.3"
|
|
@@ -37,6 +38,8 @@
|
|
|
37
38
|
"document-model-libs": "^1.1.23",
|
|
38
39
|
"eslint": "^8.54.0",
|
|
39
40
|
"eslint-config-prettier": "^9.0.0",
|
|
41
|
+
"fake-indexeddb": "^5.0.1",
|
|
42
|
+
"localforage": "^1.10.0",
|
|
40
43
|
"prettier": "^3.1.0",
|
|
41
44
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
42
45
|
"typescript": "^5.3.2",
|
package/src/server/index.ts
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { DocumentDriveDocument } from 'document-model-libs/document-drive';
|
|
2
|
+
import { Document } from 'document-model/document';
|
|
3
|
+
import { IDriveStorage } from './types';
|
|
4
|
+
|
|
5
|
+
export class BrowserStorage implements IDriveStorage {
|
|
6
|
+
private db: Promise<LocalForage>;
|
|
7
|
+
|
|
8
|
+
static DBName = 'DOCUMENT_DRIVES';
|
|
9
|
+
static SEP = ':';
|
|
10
|
+
static DRIVES_KEY = 'DRIVES';
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.db = import('localforage').then(localForage =>
|
|
14
|
+
localForage.default.createInstance({
|
|
15
|
+
name: BrowserStorage.DBName
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
buildKey(...args: string[]) {
|
|
21
|
+
return args.join(BrowserStorage.SEP);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async getDocuments(drive: string) {
|
|
25
|
+
const keys = await (await this.db).keys();
|
|
26
|
+
const driveKey = `${drive}${BrowserStorage.SEP}`;
|
|
27
|
+
return keys
|
|
28
|
+
.filter(key => key.startsWith(driveKey))
|
|
29
|
+
.map(key => key.slice(driveKey.length));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async getDocument(driveId: string, id: string) {
|
|
33
|
+
const document = await (
|
|
34
|
+
await this.db
|
|
35
|
+
).getItem<Document>(this.buildKey(driveId, id));
|
|
36
|
+
if (!document) {
|
|
37
|
+
throw new Error(`Document with id ${id} not found`);
|
|
38
|
+
}
|
|
39
|
+
return document;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async saveDocument(drive: string, id: string, document: Document) {
|
|
43
|
+
await (await this.db).setItem(this.buildKey(drive, id), document);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async deleteDocument(drive: string, id: string) {
|
|
47
|
+
await (await this.db).removeItem(this.buildKey(drive, id));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async getDrives() {
|
|
51
|
+
const drives =
|
|
52
|
+
(await (
|
|
53
|
+
await this.db
|
|
54
|
+
).getItem<DocumentDriveDocument[]>(BrowserStorage.DRIVES_KEY)) ??
|
|
55
|
+
[];
|
|
56
|
+
return drives.map(drive => drive.state.id);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async getDrive(id: string) {
|
|
60
|
+
const drives =
|
|
61
|
+
(await (
|
|
62
|
+
await this.db
|
|
63
|
+
).getItem<DocumentDriveDocument[]>(BrowserStorage.DRIVES_KEY)) ??
|
|
64
|
+
[];
|
|
65
|
+
const drive = drives.find(drive => drive.state.id === id);
|
|
66
|
+
if (!drive) {
|
|
67
|
+
throw new Error(`Drive with id ${id} not found`);
|
|
68
|
+
}
|
|
69
|
+
return drive;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async saveDrive(drive: DocumentDriveDocument) {
|
|
73
|
+
const db = await this.db;
|
|
74
|
+
const drives =
|
|
75
|
+
(await db.getItem<DocumentDriveDocument[]>(
|
|
76
|
+
BrowserStorage.DRIVES_KEY
|
|
77
|
+
)) ?? [];
|
|
78
|
+
const index = drives.findIndex(d => d.state.id === drive.state.id);
|
|
79
|
+
if (index > -1) {
|
|
80
|
+
drives[index] = drive;
|
|
81
|
+
} else {
|
|
82
|
+
drives.push(drive);
|
|
83
|
+
}
|
|
84
|
+
await db.setItem(BrowserStorage.DRIVES_KEY, drives);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async deleteDrive(id: string) {
|
|
88
|
+
const documents = await this.getDocuments(id);
|
|
89
|
+
await Promise.all(documents.map(doc => this.deleteDocument(id, doc)));
|
|
90
|
+
const db = await this.db;
|
|
91
|
+
const drives =
|
|
92
|
+
(await db.getItem<DocumentDriveDocument[]>(
|
|
93
|
+
BrowserStorage.DRIVES_KEY
|
|
94
|
+
)) ?? [];
|
|
95
|
+
await db.setItem(
|
|
96
|
+
BrowserStorage.DRIVES_KEY,
|
|
97
|
+
drives.filter(drive => drive.state.id !== id)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/storage/index.ts
CHANGED