@nu-art/live-docs-backend 0.401.8 → 0.500.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.
@@ -1,5 +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
+ import { API_LiveDoc } from '@nu-art/live-docs-shared/api';
3
4
  export declare const CollectionName_LiveDocs = "live-docs";
4
5
  type Config = {
5
6
  projectId: string;
@@ -8,6 +9,9 @@ export declare class ModuleBE_LiveDocs_Class extends Module<Config> {
8
9
  private livedocs;
9
10
  constructor();
10
11
  protected init(): void;
12
+ get(params: API_LiveDoc['get']['Params']): Promise<API_LiveDoc['get']['Response']>;
13
+ upsert(document: API_LiveDoc['upsert']['Body']): Promise<API_LiveDoc['upsert']['Response']>;
14
+ history(params: API_LiveDoc['history']['Params']): Promise<API_LiveDoc['history']['Response']>;
11
15
  changeHistory(params: LiveDocHistoryReqParams): Promise<DB_Document>;
12
16
  updateLiveDoc(document: Request_UpdateDocument): Promise<DB_Document>;
13
17
  private getLiveDocHistory;
@@ -15,95 +15,151 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
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);
22
+ }
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
+ }
48
+ }
49
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
50
+ done = true;
51
+ };
18
52
  import { addItemToArrayAtIndex, ApiException, auditBy, BadImplementationException, Module, removeItemFromArray } from '@nu-art/ts-common';
19
53
  import { ModuleBE_Firebase } from '@nu-art/firebase-backend/v1';
20
- import { addRoutes, createBodyServerApi, createQueryServerApi } from '@nu-art/thunderstorm-backend';
54
+ import { ApiHandler } from '@nu-art/http-server';
21
55
  import { ApiDef_LiveDoc } from '@nu-art/live-docs-shared/api';
22
56
  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
- ]);
38
- }
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
- });
61
- }
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 = [];
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(CollectionName_LiveDocs, ['key']);
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.runInTransaction(async (transaction) => {
96
+ const docsHistory = await transaction.queryUnique(this.livedocs, { where: { key } });
97
+ if (!docsHistory)
98
+ throw new BadImplementationException(`Cannot change history of an non-existing doc with key: ${key}`);
99
+ switch (params.change) {
100
+ case 'redo':
101
+ if (docsHistory.index === 0)
102
+ throw new ApiException(402, 'Nothing to redo anymore');
103
+ docsHistory.index--;
104
+ break;
105
+ case 'undo':
106
+ if (docsHistory.index === docsHistory.docs.length - 1)
107
+ throw new ApiException(402, 'Nothing to undo anymore');
108
+ docsHistory.index++;
109
+ break;
110
+ }
111
+ docsHistory._audit = auditBy('temp-no-user');
112
+ await transaction.upsert(this.livedocs, docsHistory);
113
+ return docsHistory.docs[docsHistory.index];
114
+ });
115
+ }
116
+ async updateLiveDoc(document) {
117
+ const liveDocHistory = await this.getLiveDocHistory(document.key);
118
+ const docDB = {
119
+ ...document,
120
+ _audit: auditBy('user.userId')
121
+ };
122
+ if (!liveDocHistory.index)
77
123
  liveDocHistory.index = 0;
124
+ if (!liveDocHistory.docs) {
125
+ this.logDebug('no history array.. creating a new one');
126
+ liveDocHistory.docs = [];
127
+ }
128
+ if (liveDocHistory.index > liveDocHistory.docs.length) {
129
+ liveDocHistory.index = liveDocHistory.docs.length - 1;
130
+ if (liveDocHistory.index < 0)
131
+ liveDocHistory.index = 0;
132
+ }
133
+ if (liveDocHistory.index > 0) {
134
+ this.logDebug(`Rewriting history, current index ${liveDocHistory.index}`);
135
+ // this.logDebug(`Rewriting history, current history ${JSON.stringify(liveDocHistory.docs, null, 2)}`);
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.upsert(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 docFromDB = await this.livedocs.queryUnique({ where: { key: docKey } });
147
+ return docFromDB || { 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
+ //@ts-ignore
158
+ delete liveDoc.key;
159
+ }
160
+ return liveDoc;
105
161
  }
106
- return liveDoc;
107
- }
108
- }
162
+ };
163
+ })();
164
+ export { ModuleBE_LiveDocs_Class };
109
165
  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.8",
3
+ "version": "0.500.0",
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.8",
42
- "@nu-art/firebase-backend": "0.401.8",
43
- "@nu-art/firebase-shared": "0.401.8",
44
- "@nu-art/thunderstorm-backend": "0.401.8",
45
- "@nu-art/thunderstorm-shared": "0.401.8",
46
- "@nu-art/ts-common": "0.401.8",
41
+ "@nu-art/live-docs-shared": "0.500.0",
42
+ "@nu-art/firebase-backend": "0.500.0",
43
+ "@nu-art/firebase-shared": "0.500.0",
44
+ "@nu-art/ts-common": "0.500.0",
47
45
  "express": "^4.18.2",
48
46
  "firebase": "^11.9.0",
49
47
  "firebase-admin": "13.4.0",
@@ -51,7 +49,9 @@
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": "{{THUNDERSTORM_VERSION}}",
54
+ "@nu-art/api-types": "{{THUNDERSTORM_VERSION}}"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@types/react": "^18.0.0",