@nu-art/live-docs-backend 0.401.9 → 0.500.6

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,6 +1,6 @@
1
1
  import { Module } from '@nu-art/ts-common';
2
2
  import { DB_Document, LiveDocHistoryReqParams, LiveDocReqParams, Request_UpdateDocument } from '@nu-art/live-docs-shared';
3
- export declare const CollectionName_LiveDocs = "live-docs";
3
+ import { API_LiveDoc } from '@nu-art/live-docs-shared/api';
4
4
  type Config = {
5
5
  projectId: string;
6
6
  };
@@ -8,6 +8,9 @@ export declare class ModuleBE_LiveDocs_Class extends Module<Config> {
8
8
  private livedocs;
9
9
  constructor();
10
10
  protected init(): void;
11
+ get(params: API_LiveDoc['get']['Params']): Promise<API_LiveDoc['get']['Response']>;
12
+ upsert(document: API_LiveDoc['upsert']['Body']): Promise<API_LiveDoc['upsert']['Response']>;
13
+ history(params: API_LiveDoc['history']['Params']): Promise<API_LiveDoc['history']['Response']>;
11
14
  changeHistory(params: LiveDocHistoryReqParams): Promise<DB_Document>;
12
15
  updateLiveDoc(document: Request_UpdateDocument): Promise<DB_Document>;
13
16
  private getLiveDocHistory;
@@ -15,95 +15,149 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- import { addItemToArrayAtIndex, ApiException, auditBy, BadImplementationException, Module, removeItemFromArray } from '@nu-art/ts-common';
19
- import { ModuleBE_Firebase } from '@nu-art/firebase-backend/v1';
20
- import { addRoutes, createBodyServerApi, createQueryServerApi } from '@nu-art/thunderstorm-backend';
21
- import { ApiDef_LiveDoc } from '@nu-art/live-docs-shared/api';
22
- export const CollectionName_LiveDocs = 'live-docs';
23
- export class ModuleBE_LiveDocs_Class extends Module {
24
- livedocs;
25
- constructor() {
26
- super();
27
- }
28
- init() {
29
- super.init();
30
- this.setDefaultConfig({ projectId: process.env.GCLOUD_PROJECT || '' });
31
- const firestore = ModuleBE_Firebase.createAdminSession(this.config.projectId).getFirestore();
32
- this.livedocs = firestore.getCollection(CollectionName_LiveDocs, ['key']);
33
- addRoutes([
34
- createQueryServerApi(ApiDef_LiveDoc.v1.get, this.getLiveDoc),
35
- createBodyServerApi(ApiDef_LiveDoc.v1.upsert, this.updateLiveDoc),
36
- createQueryServerApi(ApiDef_LiveDoc.v1.history, this.changeHistory)
37
- ]);
18
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
19
+ var useValue = arguments.length > 2;
20
+ for (var i = 0; i < initializers.length; i++) {
21
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
38
22
  }
39
- async changeHistory(params) {
40
- const key = params.key;
41
- return await this.livedocs.runInTransaction(async (transaction) => {
42
- const docsHistory = await transaction.queryUnique(this.livedocs, { where: { key } });
43
- if (!docsHistory)
44
- throw new BadImplementationException(`Cannot change history of an non-existing doc with key: ${key}`);
45
- switch (params.change) {
46
- case 'redo':
47
- if (docsHistory.index === 0)
48
- throw new ApiException(402, 'Nothing to redo anymore');
49
- docsHistory.index--;
50
- break;
51
- case 'undo':
52
- if (docsHistory.index === docsHistory.docs.length - 1)
53
- throw new ApiException(402, 'Nothing to undo anymore');
54
- docsHistory.index++;
55
- break;
56
- }
57
- docsHistory._audit = auditBy('temp-no-user');
58
- await transaction.upsert(this.livedocs, docsHistory);
59
- return docsHistory.docs[docsHistory.index];
60
- });
23
+ return useValue ? value : void 0;
24
+ };
25
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
26
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
27
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
28
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
29
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
30
+ var _, done = false;
31
+ for (var i = decorators.length - 1; i >= 0; i--) {
32
+ var context = {};
33
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
34
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
35
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
36
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
37
+ if (kind === "accessor") {
38
+ if (result === void 0) continue;
39
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
40
+ if (_ = accept(result.get)) descriptor.get = _;
41
+ if (_ = accept(result.set)) descriptor.set = _;
42
+ if (_ = accept(result.init)) initializers.unshift(_);
43
+ }
44
+ else if (_ = accept(result)) {
45
+ if (kind === "field") initializers.unshift(_);
46
+ else descriptor[key] = _;
47
+ }
61
48
  }
62
- async updateLiveDoc(document) {
63
- const liveDocHistory = await this.getLiveDocHistory(document.key);
64
- const docDB = {
65
- ...document,
66
- _audit: auditBy('user.userId')
67
- };
68
- if (!liveDocHistory.index)
69
- liveDocHistory.index = 0;
70
- if (!liveDocHistory.docs) {
71
- this.logDebug('no history array.. creating a new one');
72
- liveDocHistory.docs = [];
49
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
50
+ done = true;
51
+ };
52
+ import { addItemToArrayAtIndex, ApiException, auditBy, BadImplementationException, Module, removeItemFromArray } from '@nu-art/ts-common';
53
+ import { DBDef_DocumentHistory, } from '@nu-art/live-docs-shared';
54
+ import { ModuleBE_Firebase } from '@nu-art/firebase-backend';
55
+ import { ApiHandler } from '@nu-art/http-server';
56
+ import { ApiDef_LiveDoc } from '@nu-art/live-docs-shared/api';
57
+ let ModuleBE_LiveDocs_Class = (() => {
58
+ let _classSuper = Module;
59
+ let _instanceExtraInitializers = [];
60
+ let _get_decorators;
61
+ let _upsert_decorators;
62
+ let _history_decorators;
63
+ return class ModuleBE_LiveDocs_Class extends _classSuper {
64
+ static {
65
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
66
+ _get_decorators = [ApiHandler(ApiDef_LiveDoc.get)];
67
+ _upsert_decorators = [ApiHandler(ApiDef_LiveDoc.upsert)];
68
+ _history_decorators = [ApiHandler(ApiDef_LiveDoc.history)];
69
+ __esDecorate(this, null, _get_decorators, { kind: "method", name: "get", static: false, private: false, access: { has: obj => "get" in obj, get: obj => obj.get }, metadata: _metadata }, null, _instanceExtraInitializers);
70
+ __esDecorate(this, null, _upsert_decorators, { kind: "method", name: "upsert", static: false, private: false, access: { has: obj => "upsert" in obj, get: obj => obj.upsert }, metadata: _metadata }, null, _instanceExtraInitializers);
71
+ __esDecorate(this, null, _history_decorators, { kind: "method", name: "history", static: false, private: false, access: { has: obj => "history" in obj, get: obj => obj.history }, metadata: _metadata }, null, _instanceExtraInitializers);
72
+ if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
73
+ }
74
+ livedocs = __runInitializers(this, _instanceExtraInitializers);
75
+ constructor() {
76
+ super();
77
+ }
78
+ init() {
79
+ super.init();
80
+ this.setDefaultConfig({ projectId: process.env.GCLOUD_PROJECT || '' });
81
+ const firestore = ModuleBE_Firebase.createAdminSession(this.config.projectId).getFirestore();
82
+ this.livedocs = firestore.getCollection(DBDef_DocumentHistory);
83
+ }
84
+ async get(params) {
85
+ return this.getLiveDoc(params);
73
86
  }
74
- if (liveDocHistory.index > liveDocHistory.docs.length) {
75
- liveDocHistory.index = liveDocHistory.docs.length - 1;
76
- if (liveDocHistory.index < 0)
87
+ async upsert(document) {
88
+ return this.updateLiveDoc(document);
89
+ }
90
+ async history(params) {
91
+ return this.changeHistory(params);
92
+ }
93
+ async changeHistory(params) {
94
+ const key = params.key;
95
+ return await this.livedocs.runTransaction(async () => {
96
+ const results = await this.livedocs.query.custom({ where: { key } });
97
+ const docsHistory = results[0];
98
+ if (!docsHistory)
99
+ throw new BadImplementationException(`Cannot change history of an non-existing doc with key: ${key}`);
100
+ switch (params.change) {
101
+ case 'redo':
102
+ if (docsHistory.index === 0)
103
+ throw new ApiException(402, 'Nothing to redo anymore');
104
+ docsHistory.index--;
105
+ break;
106
+ case 'undo':
107
+ if (docsHistory.index === docsHistory.docs.length - 1)
108
+ throw new ApiException(402, 'Nothing to undo anymore');
109
+ docsHistory.index++;
110
+ break;
111
+ }
112
+ docsHistory._audit = auditBy('temp-no-user');
113
+ await this.livedocs.set.item(docsHistory);
114
+ return docsHistory.docs[docsHistory.index];
115
+ });
116
+ }
117
+ async updateLiveDoc(document) {
118
+ const liveDocHistory = await this.getLiveDocHistory(document.key);
119
+ const docDB = {
120
+ ...document,
121
+ _audit: auditBy('user.userId')
122
+ };
123
+ if (!liveDocHistory.index)
77
124
  liveDocHistory.index = 0;
125
+ if (!liveDocHistory.docs) {
126
+ this.logDebug('no history array.. creating a new one');
127
+ liveDocHistory.docs = [];
128
+ }
129
+ if (liveDocHistory.index > liveDocHistory.docs.length) {
130
+ liveDocHistory.index = liveDocHistory.docs.length - 1;
131
+ if (liveDocHistory.index < 0)
132
+ liveDocHistory.index = 0;
133
+ }
134
+ if (liveDocHistory.index > 0) {
135
+ this.logDebug(`Rewriting history, current index ${liveDocHistory.index}`);
136
+ liveDocHistory.docs.splice(0, liveDocHistory.index);
137
+ }
138
+ if (liveDocHistory.docs.length === 0 || liveDocHistory.docs[0].document !== docDB.document)
139
+ addItemToArrayAtIndex(liveDocHistory.docs, docDB, 0);
140
+ if (liveDocHistory.docs.length > 30)
141
+ removeItemFromArray(liveDocHistory.docs, liveDocHistory.docs[liveDocHistory.docs.length - 1]);
142
+ await this.livedocs.set.item(liveDocHistory);
143
+ return docDB;
78
144
  }
79
- if (liveDocHistory.index > 0) {
80
- this.logDebug(`Rewriting history, current index ${liveDocHistory.index}`);
81
- // this.logDebug(`Rewriting history, current history ${JSON.stringify(liveDocHistory.docs, null, 2)}`);
82
- liveDocHistory.docs.splice(0, liveDocHistory.index);
145
+ async getLiveDocHistory(docKey) {
146
+ const results = await this.livedocs.query.custom({ where: { key: docKey } });
147
+ return results[0] || { docs: [], key: docKey, index: 0 };
83
148
  }
84
- if (liveDocHistory.docs.length === 0 || liveDocHistory.docs[0].document !== docDB.document)
85
- addItemToArrayAtIndex(liveDocHistory.docs, docDB, 0);
86
- if (liveDocHistory.docs.length > 30)
87
- removeItemFromArray(liveDocHistory.docs, liveDocHistory.docs[liveDocHistory.docs.length - 1]);
88
- await this.livedocs.upsert(liveDocHistory);
89
- return docDB;
90
- }
91
- async getLiveDocHistory(docKey) {
92
- const docFromDB = await this.livedocs.queryUnique({ where: { key: docKey } });
93
- return docFromDB || { docs: [], key: docKey, index: 0 };
94
- }
95
- async getLiveDoc(params) {
96
- const liveDocHistory = await this.getLiveDocHistory(params.key);
97
- let liveDoc = {
98
- document: ''
99
- };
100
- if (liveDocHistory.docs && liveDocHistory.docs.length > 0 && liveDocHistory.docs[liveDocHistory.index]) {
101
- this.logDebug(`Getting live doc from index: ${liveDocHistory.index}`);
102
- liveDoc = liveDocHistory.docs[liveDocHistory.index];
103
- //@ts-ignore
104
- delete liveDoc.key;
149
+ async getLiveDoc(params) {
150
+ const liveDocHistory = await this.getLiveDocHistory(params.key);
151
+ let liveDoc = {
152
+ document: ''
153
+ };
154
+ if (liveDocHistory.docs && liveDocHistory.docs.length > 0 && liveDocHistory.docs[liveDocHistory.index]) {
155
+ this.logDebug(`Getting live doc from index: ${liveDocHistory.index}`);
156
+ liveDoc = liveDocHistory.docs[liveDocHistory.index];
157
+ }
158
+ return liveDoc;
105
159
  }
106
- return liveDoc;
107
- }
108
- }
160
+ };
161
+ })();
162
+ export { ModuleBE_LiveDocs_Class };
109
163
  export const ModuleBE_LiveDocs = new ModuleBE_LiveDocs_Class();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/live-docs-backend",
3
- "version": "0.401.9",
3
+ "version": "0.500.6",
4
4
  "description": "Live docs Backend",
5
5
  "keywords": [
6
6
  "TacB0sS",
@@ -38,12 +38,10 @@
38
38
  }
39
39
  ],
40
40
  "dependencies": {
41
- "@nu-art/live-docs-shared": "0.401.9",
42
- "@nu-art/firebase-backend": "0.401.9",
43
- "@nu-art/firebase-shared": "0.401.9",
44
- "@nu-art/thunderstorm-backend": "0.401.9",
45
- "@nu-art/thunderstorm-shared": "0.401.9",
46
- "@nu-art/ts-common": "0.401.9",
41
+ "@nu-art/live-docs-shared": "0.500.6",
42
+ "@nu-art/firebase-backend": "0.500.6",
43
+ "@nu-art/firebase-shared": "0.500.6",
44
+ "@nu-art/ts-common": "0.500.6",
47
45
  "express": "^4.18.2",
48
46
  "firebase": "^11.9.0",
49
47
  "firebase-admin": "13.4.0",
@@ -51,12 +49,15 @@
51
49
  "moment": "^2.29.4",
52
50
  "react": "^18.0.0",
53
51
  "react-dom": "^18.0.0",
54
- "react-router-dom": "^6.9.0"
52
+ "react-router-dom": "^6.9.0",
53
+ "@nu-art/http-server": "0.500.6",
54
+ "@nu-art/api-types": "0.500.6"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@types/react": "^18.0.0",
58
58
  "@types/express": "^4.17.17",
59
- "@types/history": "^4.7.2"
59
+ "@types/history": "^4.7.2",
60
+ "@types/request": "^2.48.1"
60
61
  },
61
62
  "unitConfig": {
62
63
  "type": "typescript-lib"