document-drive 1.19.0 → 1.20.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 (252) hide show
  1. package/README.md +4 -0
  2. package/dist/index.d.ts +28 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +18 -0
  5. package/dist/src/cache/memory.d.ts +10 -0
  6. package/dist/src/cache/memory.d.ts.map +1 -0
  7. package/dist/src/cache/memory.js +26 -0
  8. package/dist/src/cache/redis.d.ts +14 -0
  9. package/dist/src/cache/redis.d.ts.map +1 -0
  10. package/dist/src/cache/redis.js +40 -0
  11. package/dist/src/cache/types.d.ts +7 -0
  12. package/dist/src/cache/types.d.ts.map +1 -0
  13. package/dist/src/cache/types.js +1 -0
  14. package/dist/src/drive-document-model/constants.d.ts +2 -0
  15. package/dist/src/drive-document-model/constants.d.ts.map +1 -0
  16. package/dist/src/drive-document-model/constants.js +1 -0
  17. package/dist/src/drive-document-model/gen/actions.d.ts +7 -0
  18. package/dist/src/drive-document-model/gen/actions.d.ts.map +1 -0
  19. package/dist/src/drive-document-model/gen/actions.js +2 -0
  20. package/dist/src/drive-document-model/gen/constants.d.ts +7 -0
  21. package/dist/src/drive-document-model/gen/constants.d.ts.map +1 -0
  22. package/dist/src/drive-document-model/gen/constants.js +16 -0
  23. package/dist/src/drive-document-model/gen/creators.d.ts +3 -0
  24. package/dist/src/drive-document-model/gen/creators.d.ts.map +1 -0
  25. package/dist/src/drive-document-model/gen/creators.js +2 -0
  26. package/dist/src/drive-document-model/gen/document-model.d.ts +3 -0
  27. package/dist/src/drive-document-model/gen/document-model.d.ts.map +1 -0
  28. package/dist/src/drive-document-model/gen/document-model.js +210 -0
  29. package/dist/src/drive-document-model/gen/drive/actions.d.ts +12 -0
  30. package/dist/src/drive-document-model/gen/drive/actions.d.ts.map +1 -0
  31. package/dist/src/drive-document-model/gen/drive/actions.js +1 -0
  32. package/dist/src/drive-document-model/gen/drive/creators.d.ts +11 -0
  33. package/dist/src/drive-document-model/gen/drive/creators.d.ts.map +1 -0
  34. package/dist/src/drive-document-model/gen/drive/creators.js +10 -0
  35. package/dist/src/drive-document-model/gen/drive/error.d.ts +2 -0
  36. package/dist/src/drive-document-model/gen/drive/error.d.ts.map +1 -0
  37. package/dist/src/drive-document-model/gen/drive/error.js +1 -0
  38. package/dist/src/drive-document-model/gen/drive/object.d.ts +14 -0
  39. package/dist/src/drive-document-model/gen/drive/object.d.ts.map +1 -0
  40. package/dist/src/drive-document-model/gen/drive/object.js +28 -0
  41. package/dist/src/drive-document-model/gen/drive/operations.d.ts +14 -0
  42. package/dist/src/drive-document-model/gen/drive/operations.d.ts.map +1 -0
  43. package/dist/src/drive-document-model/gen/drive/operations.js +1 -0
  44. package/dist/src/drive-document-model/gen/node/actions.d.ts +11 -0
  45. package/dist/src/drive-document-model/gen/node/actions.d.ts.map +1 -0
  46. package/dist/src/drive-document-model/gen/node/actions.js +1 -0
  47. package/dist/src/drive-document-model/gen/node/creators.d.ts +10 -0
  48. package/dist/src/drive-document-model/gen/node/creators.d.ts.map +1 -0
  49. package/dist/src/drive-document-model/gen/node/creators.js +9 -0
  50. package/dist/src/drive-document-model/gen/node/error.d.ts +2 -0
  51. package/dist/src/drive-document-model/gen/node/error.d.ts.map +1 -0
  52. package/dist/src/drive-document-model/gen/node/error.js +1 -0
  53. package/dist/src/drive-document-model/gen/node/object.d.ts +13 -0
  54. package/dist/src/drive-document-model/gen/node/object.d.ts.map +1 -0
  55. package/dist/src/drive-document-model/gen/node/object.js +25 -0
  56. package/dist/src/drive-document-model/gen/node/operations.d.ts +13 -0
  57. package/dist/src/drive-document-model/gen/node/operations.d.ts.map +1 -0
  58. package/dist/src/drive-document-model/gen/node/operations.js +1 -0
  59. package/dist/src/drive-document-model/gen/object.d.ts +21 -0
  60. package/dist/src/drive-document-model/gen/object.d.ts.map +1 -0
  61. package/dist/src/drive-document-model/gen/object.js +28 -0
  62. package/dist/src/drive-document-model/gen/reducer.d.ts +4 -0
  63. package/dist/src/drive-document-model/gen/reducer.d.ts.map +1 -0
  64. package/dist/src/drive-document-model/gen/reducer.js +74 -0
  65. package/dist/src/drive-document-model/gen/schema/types.d.ts +176 -0
  66. package/dist/src/drive-document-model/gen/schema/types.d.ts.map +1 -0
  67. package/dist/src/drive-document-model/gen/schema/types.js +1 -0
  68. package/dist/src/drive-document-model/gen/schema/zod.d.ts +87 -0
  69. package/dist/src/drive-document-model/gen/schema/zod.d.ts.map +1 -0
  70. package/dist/src/drive-document-model/gen/schema/zod.js +203 -0
  71. package/dist/src/drive-document-model/gen/types.d.ts +9 -0
  72. package/dist/src/drive-document-model/gen/types.d.ts.map +1 -0
  73. package/dist/src/drive-document-model/gen/types.js +1 -0
  74. package/dist/src/drive-document-model/gen/utils.d.ts +10 -0
  75. package/dist/src/drive-document-model/gen/utils.d.ts.map +1 -0
  76. package/dist/src/drive-document-model/gen/utils.js +27 -0
  77. package/dist/src/drive-document-model/index.d.ts +2 -0
  78. package/dist/src/drive-document-model/index.d.ts.map +1 -0
  79. package/dist/src/drive-document-model/index.js +1 -0
  80. package/dist/src/drive-document-model/module.d.ts +3 -0
  81. package/dist/src/drive-document-model/module.d.ts.map +1 -0
  82. package/dist/src/drive-document-model/module.js +12 -0
  83. package/dist/src/drive-document-model/src/reducers/drive.d.ts +8 -0
  84. package/dist/src/drive-document-model/src/reducers/drive.d.ts.map +1 -0
  85. package/dist/src/drive-document-model/src/reducers/drive.js +37 -0
  86. package/dist/src/drive-document-model/src/reducers/node.d.ts +8 -0
  87. package/dist/src/drive-document-model/src/reducers/node.d.ts.map +1 -0
  88. package/dist/src/drive-document-model/src/reducers/node.js +185 -0
  89. package/dist/src/drive-document-model/src/utils.d.ts +34 -0
  90. package/dist/src/drive-document-model/src/utils.d.ts.map +1 -0
  91. package/dist/src/drive-document-model/src/utils.js +146 -0
  92. package/dist/src/queue/base.d.ts +43 -0
  93. package/dist/src/queue/base.d.ts.map +1 -0
  94. package/dist/src/queue/base.js +241 -0
  95. package/dist/src/queue/redis.d.ts +28 -0
  96. package/dist/src/queue/redis.d.ts.map +1 -0
  97. package/dist/src/queue/redis.js +110 -0
  98. package/dist/src/queue/types.d.ts +55 -0
  99. package/dist/src/queue/types.d.ts.map +1 -0
  100. package/dist/src/queue/types.js +6 -0
  101. package/dist/src/read-mode/errors.d.ts +12 -0
  102. package/dist/src/read-mode/errors.d.ts.map +1 -0
  103. package/dist/src/read-mode/errors.js +17 -0
  104. package/dist/src/read-mode/server.d.ts +4 -0
  105. package/dist/src/read-mode/server.d.ts.map +1 -0
  106. package/dist/src/read-mode/server.js +78 -0
  107. package/dist/src/read-mode/service.d.ts +18 -0
  108. package/dist/src/read-mode/service.d.ts.map +1 -0
  109. package/dist/src/read-mode/service.js +112 -0
  110. package/dist/src/read-mode/types.d.ts +35 -0
  111. package/dist/src/read-mode/types.d.ts.map +1 -0
  112. package/dist/src/read-mode/types.js +1 -0
  113. package/dist/src/server/base-server.d.ts +112 -0
  114. package/dist/src/server/base-server.d.ts.map +1 -0
  115. package/dist/src/server/base-server.js +1280 -0
  116. package/dist/src/server/builder.d.ts +30 -0
  117. package/dist/src/server/builder.d.ts.map +1 -0
  118. package/dist/src/server/builder.js +89 -0
  119. package/dist/src/server/constants.d.ts +2 -0
  120. package/dist/src/server/constants.d.ts.map +1 -0
  121. package/dist/src/server/constants.js +1 -0
  122. package/dist/src/server/error.d.ts +30 -0
  123. package/dist/src/server/error.d.ts.map +1 -0
  124. package/dist/src/server/error.js +47 -0
  125. package/dist/src/server/event-emitter.d.ts +8 -0
  126. package/dist/src/server/event-emitter.d.ts.map +1 -0
  127. package/dist/src/server/event-emitter.js +10 -0
  128. package/dist/src/server/listener/index.d.ts +2 -0
  129. package/dist/src/server/listener/index.d.ts.map +1 -0
  130. package/dist/src/server/listener/index.js +1 -0
  131. package/dist/src/server/listener/listener-manager.d.ts +27 -0
  132. package/dist/src/server/listener/listener-manager.d.ts.map +1 -0
  133. package/dist/src/server/listener/listener-manager.js +401 -0
  134. package/dist/src/server/listener/transmitter/factory.d.ts +8 -0
  135. package/dist/src/server/listener/transmitter/factory.d.ts.map +1 -0
  136. package/dist/src/server/listener/transmitter/factory.js +25 -0
  137. package/dist/src/server/listener/transmitter/internal.d.ts +34 -0
  138. package/dist/src/server/listener/transmitter/internal.d.ts.map +1 -0
  139. package/dist/src/server/listener/transmitter/internal.js +87 -0
  140. package/dist/src/server/listener/transmitter/pull-responder.d.ts +38 -0
  141. package/dist/src/server/listener/transmitter/pull-responder.d.ts.map +1 -0
  142. package/dist/src/server/listener/transmitter/pull-responder.js +256 -0
  143. package/dist/src/server/listener/transmitter/switchboard-push.d.ts +9 -0
  144. package/dist/src/server/listener/transmitter/switchboard-push.d.ts.map +1 -0
  145. package/dist/src/server/listener/transmitter/switchboard-push.js +77 -0
  146. package/dist/src/server/listener/transmitter/types.d.ts +20 -0
  147. package/dist/src/server/listener/transmitter/types.d.ts.map +1 -0
  148. package/dist/src/server/listener/transmitter/types.js +1 -0
  149. package/dist/src/server/listener/util.d.ts +2 -0
  150. package/dist/src/server/listener/util.d.ts.map +1 -0
  151. package/dist/src/server/listener/util.js +22 -0
  152. package/dist/src/server/sync-manager.d.ts +30 -0
  153. package/dist/src/server/sync-manager.d.ts.map +1 -0
  154. package/dist/src/server/sync-manager.js +287 -0
  155. package/dist/src/server/types.d.ts +308 -0
  156. package/dist/src/server/types.d.ts.map +1 -0
  157. package/dist/src/server/types.js +12 -0
  158. package/dist/src/server/utils.d.ts +8 -0
  159. package/dist/src/server/utils.d.ts.map +1 -0
  160. package/dist/src/server/utils.js +47 -0
  161. package/dist/src/storage/base.d.ts +36 -0
  162. package/dist/src/storage/base.d.ts.map +1 -0
  163. package/dist/src/storage/base.js +4 -0
  164. package/dist/src/storage/browser.d.ts +36 -0
  165. package/dist/src/storage/browser.d.ts.map +1 -0
  166. package/dist/src/storage/browser.js +155 -0
  167. package/dist/src/storage/filesystem.d.ts +33 -0
  168. package/dist/src/storage/filesystem.d.ts.map +1 -0
  169. package/dist/src/storage/filesystem.js +197 -0
  170. package/dist/src/storage/memory.d.ts +33 -0
  171. package/dist/src/storage/memory.d.ts.map +1 -0
  172. package/dist/src/storage/memory.js +139 -0
  173. package/dist/src/storage/prisma.d.ts +67 -0
  174. package/dist/src/storage/prisma.d.ts.map +1 -0
  175. package/dist/src/storage/prisma.js +445 -0
  176. package/dist/src/storage/sequelize.d.ts +32 -0
  177. package/dist/src/storage/sequelize.d.ts.map +1 -0
  178. package/dist/src/storage/sequelize.js +373 -0
  179. package/dist/src/storage/types.d.ts +43 -0
  180. package/dist/src/storage/types.d.ts.map +1 -0
  181. package/dist/src/storage/types.js +1 -0
  182. package/dist/src/utils/default-drives-manager.d.ts +29 -0
  183. package/dist/src/utils/default-drives-manager.d.ts.map +1 -0
  184. package/dist/src/utils/default-drives-manager.js +208 -0
  185. package/dist/src/utils/graphql.d.ts +34 -0
  186. package/dist/src/utils/graphql.d.ts.map +1 -0
  187. package/dist/src/utils/graphql.js +183 -0
  188. package/dist/src/utils/logger.d.ts +27 -0
  189. package/dist/src/utils/logger.d.ts.map +1 -0
  190. package/dist/src/utils/logger.js +105 -0
  191. package/dist/src/utils/migrations.d.ts +4 -0
  192. package/dist/src/utils/migrations.d.ts.map +1 -0
  193. package/dist/src/utils/migrations.js +41 -0
  194. package/dist/src/utils/misc.d.ts +11 -0
  195. package/dist/src/utils/misc.d.ts.map +1 -0
  196. package/dist/src/utils/misc.js +43 -0
  197. package/dist/src/utils/run-asap.d.ts +12 -0
  198. package/dist/src/utils/run-asap.d.ts.map +1 -0
  199. package/dist/src/utils/run-asap.js +131 -0
  200. package/dist/test/document-helpers/utils.d.ts +8 -0
  201. package/dist/test/document-helpers/utils.d.ts.map +1 -0
  202. package/dist/test/document-helpers/utils.js +21 -0
  203. package/dist/test/utils.d.ts +48 -0
  204. package/dist/test/utils.d.ts.map +1 -0
  205. package/dist/test/utils.js +132 -0
  206. package/dist/test/vitest-setup.d.ts +2 -0
  207. package/dist/test/vitest-setup.d.ts.map +1 -0
  208. package/dist/test/vitest-setup.js +4 -0
  209. package/dist/tsconfig.tsbuildinfo +1 -0
  210. package/dist/vitest.config.d.ts +3 -0
  211. package/dist/vitest.config.d.ts.map +1 -0
  212. package/dist/vitest.config.js +20 -0
  213. package/package.json +20 -38
  214. package/src/cache/index.ts +0 -2
  215. package/src/cache/memory.ts +0 -33
  216. package/src/cache/redis.ts +0 -56
  217. package/src/cache/types.ts +0 -9
  218. package/src/index.ts +0 -4
  219. package/src/queue/base.ts +0 -320
  220. package/src/queue/index.ts +0 -2
  221. package/src/queue/redis.ts +0 -144
  222. package/src/queue/types.ts +0 -79
  223. package/src/read-mode/errors.ts +0 -19
  224. package/src/read-mode/index.ts +0 -125
  225. package/src/read-mode/service.ts +0 -207
  226. package/src/read-mode/types.ts +0 -108
  227. package/src/server/error.ts +0 -70
  228. package/src/server/index.ts +0 -2444
  229. package/src/server/listener/index.ts +0 -2
  230. package/src/server/listener/manager.ts +0 -652
  231. package/src/server/listener/transmitter/index.ts +0 -4
  232. package/src/server/listener/transmitter/internal.ts +0 -143
  233. package/src/server/listener/transmitter/pull-responder.ts +0 -462
  234. package/src/server/listener/transmitter/switchboard-push.ts +0 -125
  235. package/src/server/listener/transmitter/types.ts +0 -27
  236. package/src/server/types.ts +0 -596
  237. package/src/server/utils.ts +0 -82
  238. package/src/storage/base.ts +0 -81
  239. package/src/storage/browser.ts +0 -238
  240. package/src/storage/filesystem.ts +0 -297
  241. package/src/storage/index.ts +0 -2
  242. package/src/storage/memory.ts +0 -211
  243. package/src/storage/prisma.ts +0 -653
  244. package/src/storage/sequelize.ts +0 -498
  245. package/src/storage/types.ts +0 -97
  246. package/src/utils/default-drives-manager.ts +0 -341
  247. package/src/utils/document-helpers.ts +0 -21
  248. package/src/utils/graphql.ts +0 -301
  249. package/src/utils/index.ts +0 -90
  250. package/src/utils/logger.ts +0 -38
  251. package/src/utils/migrations.ts +0 -58
  252. package/src/utils/run-asap.ts +0 -156
@@ -1,498 +0,0 @@
1
- import {
2
- DocumentDriveLocalState,
3
- DocumentDriveState,
4
- } from "document-model-libs/document-drive";
5
- import {
6
- AttachmentInput,
7
- DocumentHeader,
8
- ExtendedState,
9
- Operation,
10
- OperationScope,
11
- } from "document-model/document";
12
- import { DataTypes, Options, Sequelize } from "sequelize";
13
- import type { SynchronizationUnitQuery } from "../server/types";
14
- import { DocumentDriveStorage, DocumentStorage, IDriveStorage } from "./types";
15
-
16
- export class SequelizeStorage implements IDriveStorage {
17
- private db: Sequelize;
18
-
19
- constructor(options: Options) {
20
- this.db = new Sequelize(options);
21
- }
22
-
23
- public syncModels() {
24
- const Drive = this.db.define("drive", {
25
- slug: {
26
- type: DataTypes.STRING,
27
- primaryKey: true,
28
- },
29
- id: DataTypes.STRING,
30
- });
31
- const Document = this.db.define("document", {
32
- id: {
33
- type: DataTypes.STRING,
34
- primaryKey: true,
35
- },
36
- driveId: {
37
- type: DataTypes.STRING,
38
- primaryKey: true,
39
- },
40
- name: DataTypes.STRING,
41
- documentType: DataTypes.STRING,
42
- initialState: DataTypes.JSON,
43
- lastModified: DataTypes.DATE,
44
- revision: DataTypes.JSON,
45
- });
46
-
47
- const Operation = this.db.define("operation", {
48
- driveId: {
49
- type: DataTypes.STRING,
50
- primaryKey: true,
51
- unique: "unique_operation",
52
- },
53
- documentId: {
54
- type: DataTypes.STRING,
55
- primaryKey: true,
56
- unique: "unique_operation",
57
- },
58
- hash: DataTypes.STRING,
59
- index: {
60
- type: DataTypes.INTEGER,
61
- primaryKey: true,
62
- unique: "unique_operation",
63
- },
64
- input: DataTypes.JSON,
65
- timestamp: DataTypes.DATE,
66
- type: DataTypes.STRING,
67
- scope: {
68
- type: DataTypes.STRING,
69
- primaryKey: true,
70
- unique: "unique_operation",
71
- },
72
- branch: {
73
- type: DataTypes.STRING,
74
- primaryKey: true,
75
- unique: "unique_operation",
76
- },
77
- skip: {
78
- type: DataTypes.INTEGER,
79
- defaultValue: 0,
80
- },
81
- });
82
-
83
- const Attachment = this.db.define("attachment", {
84
- driveId: {
85
- type: DataTypes.STRING,
86
- primaryKey: true,
87
- },
88
- documentId: {
89
- type: DataTypes.STRING,
90
- primaryKey: true,
91
- },
92
- scope: {
93
- type: DataTypes.STRING,
94
- primaryKey: true,
95
- },
96
- branch: {
97
- type: DataTypes.STRING,
98
- primaryKey: true,
99
- },
100
- index: {
101
- type: DataTypes.STRING,
102
- primaryKey: true,
103
- },
104
- hash: {
105
- type: DataTypes.STRING,
106
- primaryKey: true,
107
- },
108
- mimeType: DataTypes.STRING,
109
- fileName: DataTypes.STRING,
110
- extension: DataTypes.STRING,
111
- data: DataTypes.BLOB,
112
- });
113
-
114
- Operation.hasMany(Attachment, {
115
- onDelete: "CASCADE",
116
- });
117
- Attachment.belongsTo(Operation);
118
- Document.hasMany(Operation, {
119
- onDelete: "CASCADE",
120
- });
121
- Operation.belongsTo(Document);
122
-
123
- return this.db.sync({ force: true });
124
- }
125
-
126
- async createDrive(id: string, drive: DocumentDriveStorage): Promise<void> {
127
- await this.createDocument("drives", id, drive as DocumentStorage);
128
- const Drive = this.db.models.drive;
129
- await Drive?.upsert({ id, slug: drive.initialState.state.global.slug });
130
- }
131
- async addDriveOperations(
132
- id: string,
133
- operations: Operation[],
134
- header: DocumentHeader,
135
- ): Promise<void> {
136
- await this.addDocumentOperations("drives", id, operations, header);
137
- }
138
- async createDocument(
139
- drive: string,
140
- id: string,
141
- document: DocumentStorage,
142
- ): Promise<void> {
143
- const Document = this.db.models.document;
144
-
145
- if (!Document) {
146
- throw new Error("Document model not found");
147
- }
148
-
149
- await Document.create({
150
- id: id,
151
- driveId: drive,
152
- name: document.name,
153
- documentType: document.documentType,
154
- initialState: document.initialState,
155
- lastModified: document.lastModified,
156
- revision: document.revision,
157
- });
158
- }
159
- async addDocumentOperations(
160
- drive: string,
161
- id: string,
162
- operations: Operation[],
163
- header: DocumentHeader,
164
- ): Promise<void> {
165
- const document = await this.getDocument(drive, id);
166
- if (!document) {
167
- throw new Error(`Document with id ${id} not found`);
168
- }
169
-
170
- const Operation = this.db.models.operation;
171
- if (!Operation) {
172
- throw new Error("Operation model not found");
173
- }
174
-
175
- await Operation.bulkCreate(
176
- operations.map((op) => ({
177
- driveId: drive,
178
- documentId: id,
179
- hash: op.hash,
180
- index: op.index,
181
- input: op.input,
182
- timestamp: op.timestamp,
183
- type: op.type,
184
- scope: op.scope,
185
- branch: "main",
186
- opId: op.id,
187
- })),
188
- );
189
-
190
- const attachments = operations.reduce<AttachmentInput[]>((acc, op) => {
191
- if (op.attachments?.length) {
192
- return acc.concat(
193
- op.attachments.map((attachment) => ({
194
- driveId: drive,
195
- documentId: id,
196
- scope: op.scope,
197
- branch: "main",
198
- index: op.index,
199
- mimeType: attachment.mimeType,
200
- fileName: attachment.fileName,
201
- extension: attachment.extension,
202
- data: attachment.data,
203
- hash: attachment.hash,
204
- })),
205
- );
206
- }
207
- return acc;
208
- }, []);
209
- if (attachments.length) {
210
- const Attachment = this.db.models.attachment;
211
- if (!Attachment) {
212
- throw new Error("Attachment model not found");
213
- }
214
-
215
- await Attachment.bulkCreate(attachments);
216
- }
217
-
218
- const Document = this.db.models.document;
219
- if (!Document) {
220
- throw new Error("Document model not found");
221
- }
222
-
223
- await Document.update(
224
- {
225
- lastModified: header.lastModified,
226
- revision: header.revision,
227
- },
228
- {
229
- where: {
230
- id: id,
231
- driveId: drive,
232
- },
233
- },
234
- );
235
- }
236
-
237
- async _addDocumentOperationAttachments(
238
- driveId: string,
239
- documentId: string,
240
- operation: Operation,
241
- attachments: AttachmentInput[],
242
- ) {
243
- const Attachment = this.db.models.attachment;
244
- if (!Attachment) {
245
- throw new Error("Attachment model not found");
246
- }
247
-
248
- return Attachment.bulkCreate(
249
- attachments.map((attachment) => ({
250
- driveId: driveId,
251
- documentId: documentId,
252
- scope: operation.scope,
253
- branch: "main",
254
- index: operation.index,
255
- mimeType: attachment.mimeType,
256
- fileName: attachment.fileName,
257
- extension: attachment.extension,
258
- data: attachment.data,
259
- hash: attachment.hash,
260
- })),
261
- );
262
- }
263
-
264
- async getDocuments(drive: string) {
265
- const Document = this.db.models.document;
266
- if (!Document) {
267
- throw new Error("Document model not found");
268
- }
269
-
270
- const result = await Document.findAll({
271
- attributes: ["id"],
272
- where: {
273
- driveId: drive,
274
- },
275
- });
276
-
277
- const ids = result.map((e: { dataValues: { id: string } }) => {
278
- const { id } = e.dataValues;
279
- return id;
280
- });
281
- return ids;
282
- }
283
-
284
- async checkDocumentExists(driveId: string, id: string): Promise<boolean> {
285
- const Document = this.db.models.document;
286
- if (!Document) {
287
- throw new Error("Document model not found");
288
- }
289
- const count = await Document.count({
290
- where: {
291
- id: id,
292
- driveId: driveId,
293
- },
294
- });
295
-
296
- return count > 0;
297
- }
298
-
299
- // @ts-expect-error TODO: fix as this should not be undefined
300
- async getDocument(driveId: string, id: string) {
301
- const Document = this.db.models.document;
302
- if (!Document) {
303
- throw new Error("Document model not found");
304
- }
305
-
306
- const entry = await Document.findOne({
307
- where: {
308
- id: id,
309
- driveId: driveId,
310
- },
311
- include: [
312
- {
313
- model: this.db.models.operation,
314
- as: "operations",
315
- },
316
- ],
317
- });
318
-
319
- if (entry === null) {
320
- throw new Error(`Document with id ${id} not found`);
321
- }
322
-
323
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
324
- const document: {
325
- operations: [
326
- {
327
- hash: string;
328
- index: number;
329
- timestamp: Date;
330
- input: JSON;
331
- type: string;
332
- scope: string;
333
- opId?: string;
334
- skip: number;
335
- },
336
- ];
337
- revision: Required<Record<OperationScope, number>>;
338
- createdAt: Date;
339
- name: string;
340
- updatedAt: Date;
341
- documentType: string;
342
- initialState: ExtendedState<DocumentDriveState, DocumentDriveLocalState>;
343
- } = entry.dataValues;
344
- const Operation = this.db.models.operation;
345
- if (!Operation) {
346
- throw new Error("Operation model not found");
347
- }
348
-
349
- const operations: Operation[] = document.operations.map((op) => ({
350
- hash: op.hash,
351
- index: op.index,
352
- timestamp: new Date(op.timestamp).toISOString(),
353
- input: op.input,
354
- type: op.type,
355
- scope: op.scope as OperationScope,
356
- id: op.opId,
357
- skip: op.skip,
358
- // attachments: fileRegistry
359
- }));
360
-
361
- const doc = {
362
- created: document.createdAt.toISOString(),
363
- name: document.name ? document.name : "",
364
- documentType: document.documentType,
365
- initialState: document.initialState,
366
- lastModified: document.updatedAt.toISOString(),
367
- operations: {
368
- global: operations.filter((op: Operation) => op.scope === "global"),
369
- local: operations.filter((op: Operation) => op.scope === "local"),
370
- },
371
- revision: document.revision,
372
- };
373
-
374
- return doc;
375
- }
376
-
377
- async deleteDocument(drive: string, id: string) {
378
- const Document = this.db.models.document;
379
- if (!Document) {
380
- throw new Error("Document model not found");
381
- }
382
-
383
- await Document.destroy({
384
- where: {
385
- id: id,
386
- driveId: drive,
387
- },
388
- });
389
- }
390
-
391
- async getDrives() {
392
- return this.getDocuments("drives");
393
- }
394
-
395
- async getDrive(id: string) {
396
- const doc = await this.getDocument("drives", id);
397
- return doc as DocumentDriveStorage;
398
- }
399
-
400
- async getDriveBySlug(slug: string) {
401
- const Drive = this.db.models.drive;
402
- if (!Drive) {
403
- throw new Error("Drive model not found");
404
- }
405
-
406
- const driveEntity = await Drive.findOne({
407
- where: {
408
- slug,
409
- },
410
- });
411
-
412
- if (!driveEntity) {
413
- throw new Error(`Drive with slug ${slug} not found`);
414
- }
415
-
416
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
417
- return this.getDrive(driveEntity.dataValues.id);
418
- }
419
-
420
- async deleteDrive(id: string) {
421
- await this.deleteDocument("drives", id);
422
-
423
- const Document = this.db.models.document;
424
- if (!Document) {
425
- throw new Error("Document model not found");
426
- }
427
-
428
- await Document.destroy({
429
- where: {
430
- driveId: id,
431
- },
432
- });
433
-
434
- const Drive = this.db.models.drive;
435
- if (Drive) {
436
- await Drive.destroy({
437
- where: {
438
- id: id,
439
- },
440
- });
441
- }
442
- }
443
-
444
- async getSynchronizationUnitsRevision(
445
- units: SynchronizationUnitQuery[],
446
- ): Promise<
447
- {
448
- driveId: string;
449
- documentId: string;
450
- scope: string;
451
- branch: string;
452
- lastUpdated: string;
453
- revision: number;
454
- }[]
455
- > {
456
- const results = await Promise.allSettled(
457
- units.map(async (unit) => {
458
- try {
459
- const document = await (unit.documentId
460
- ? this.getDocument(unit.driveId, unit.documentId)
461
- : this.getDrive(unit.driveId));
462
- if (!document) {
463
- return undefined;
464
- }
465
- const operation =
466
- document.operations[unit.scope as OperationScope].at(-1);
467
- if (operation) {
468
- return {
469
- driveId: unit.driveId,
470
- documentId: unit.documentId,
471
- scope: unit.scope,
472
- branch: unit.branch,
473
- lastUpdated: operation.timestamp,
474
- revision: operation.index,
475
- };
476
- }
477
- } catch {
478
- return undefined;
479
- }
480
- }),
481
- );
482
- return results.reduce<
483
- {
484
- driveId: string;
485
- documentId: string;
486
- scope: string;
487
- branch: string;
488
- lastUpdated: string;
489
- revision: number;
490
- }[]
491
- >((acc, curr) => {
492
- if (curr.status === "fulfilled" && curr.value !== undefined) {
493
- acc.push(curr.value);
494
- }
495
- return acc;
496
- }, []);
497
- }
498
- }
@@ -1,97 +0,0 @@
1
- import type {
2
- DocumentDriveAction,
3
- DocumentDriveDocument,
4
- } from "document-model-libs/document-drive";
5
- import type {
6
- Action,
7
- BaseAction,
8
- Document,
9
- DocumentHeader,
10
- DocumentOperations,
11
- Operation,
12
- } from "document-model/document";
13
- import type { SynchronizationUnitQuery } from "../server/types";
14
-
15
- export type DocumentStorage<D extends Document = Document> = Omit<
16
- D,
17
- "attachments"
18
- >;
19
-
20
- export type DocumentDriveStorage = DocumentStorage<DocumentDriveDocument>;
21
-
22
- export interface IStorageDelegate {
23
- getCachedOperations(
24
- drive: string,
25
- id: string,
26
- ): Promise<DocumentOperations<Action> | undefined>;
27
- }
28
-
29
- export interface IStorage {
30
- checkDocumentExists(drive: string, id: string): Promise<boolean>;
31
- getDocuments: (drive: string) => Promise<string[]>;
32
- getDocument(drive: string, id: string): Promise<DocumentStorage>;
33
- createDocument(
34
- drive: string,
35
- id: string,
36
- document: DocumentStorage,
37
- ): Promise<void>;
38
- addDocumentOperations(
39
- drive: string,
40
- id: string,
41
- operations: Operation[],
42
- header: DocumentHeader,
43
- ): Promise<void>;
44
- addDocumentOperationsWithTransaction?(
45
- drive: string,
46
- id: string,
47
- callback: (document: DocumentStorage) => Promise<{
48
- operations: Operation[];
49
- header: DocumentHeader;
50
- }>,
51
- ): Promise<void>;
52
- deleteDocument(drive: string, id: string): Promise<void>;
53
- getOperationResultingState?(
54
- drive: string,
55
- id: string,
56
- index: number,
57
- scope: string,
58
- branch: string,
59
- ): Promise<unknown>;
60
- setStorageDelegate?(delegate: IStorageDelegate): void;
61
- getSynchronizationUnitsRevision(units: SynchronizationUnitQuery[]): Promise<
62
- {
63
- driveId: string;
64
- documentId: string;
65
- scope: string;
66
- branch: string;
67
- lastUpdated: string;
68
- revision: number;
69
- }[]
70
- >;
71
- }
72
- export interface IDriveStorage extends IStorage {
73
- getDrives(): Promise<string[]>;
74
- getDrive(id: string): Promise<DocumentDriveStorage>;
75
- getDriveBySlug(slug: string): Promise<DocumentDriveStorage>;
76
- createDrive(id: string, drive: DocumentDriveStorage): Promise<void>;
77
- deleteDrive(id: string): Promise<void>;
78
- clearStorage?(): Promise<void>;
79
- addDriveOperations(
80
- id: string,
81
- operations: Operation<DocumentDriveAction | BaseAction>[],
82
- header: DocumentHeader,
83
- ): Promise<void>;
84
- addDriveOperationsWithTransaction?(
85
- drive: string,
86
- callback: (document: DocumentDriveStorage) => Promise<{
87
- operations: Operation[];
88
- header: DocumentHeader;
89
- }>,
90
- ): Promise<void>;
91
- getDriveOperationResultingState?(
92
- drive: string,
93
- index: number,
94
- scope: string,
95
- branch: string,
96
- ): Promise<unknown>;
97
- }