mocody 0.4.38 → 0.5.0-alpha
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/dist/core/base-schema.d.ts +13 -13
- package/dist/core/base-schema.js +11 -11
- package/dist/couch/couch-data-operation.d.ts +92 -92
- package/dist/couch/couch-data-operation.js +580 -580
- package/dist/couch/couch-data-operation.js.map +1 -1
- package/dist/couch/couch-filter-query-operation.d.ts +45 -47
- package/dist/couch/couch-filter-query-operation.js +371 -438
- package/dist/couch/couch-filter-query-operation.js.map +1 -1
- package/dist/couch/couch-initializer.d.ts +126 -126
- package/dist/couch/couch-initializer.js +189 -189
- package/dist/couch/couch-initializer.js.map +1 -1
- package/dist/couch/couch-manage-table.d.ts +46 -46
- package/dist/couch/couch-manage-table.js +103 -103
- package/dist/couch/couch-manage-table.js.map +1 -1
- package/dist/dynamo/dynamo-data-operation.d.ts +102 -99
- package/dist/dynamo/dynamo-data-operation.js +932 -797
- package/dist/dynamo/dynamo-data-operation.js.map +1 -1
- package/dist/dynamo/dynamo-filter-query-operation.d.ts +29 -29
- package/dist/dynamo/dynamo-filter-query-operation.js +603 -690
- package/dist/dynamo/dynamo-filter-query-operation.js.map +1 -1
- package/dist/dynamo/dynamo-filter-query-partiql-operation.d.ts +22 -0
- package/dist/dynamo/dynamo-filter-query-partiql-operation.js +457 -0
- package/dist/dynamo/dynamo-filter-query-partiql-operation.js.map +1 -0
- package/dist/dynamo/dynamo-helper.d.ts +1 -1
- package/dist/dynamo/dynamo-helper.js +8 -8
- package/dist/dynamo/dynamo-initializer.d.ts +15 -14
- package/dist/dynamo/dynamo-initializer.js +66 -58
- package/dist/dynamo/dynamo-initializer.js.map +1 -1
- package/dist/dynamo/dynamo-manage-table.d.ts +33 -33
- package/dist/dynamo/dynamo-manage-table.js +419 -419
- package/dist/dynamo/dynamo-manage-table.js.map +1 -1
- package/dist/dynamo/dynamo-query-partiql-processor.d.ts +31 -0
- package/dist/dynamo/dynamo-query-partiql-processor.js +277 -0
- package/dist/dynamo/dynamo-query-partiql-processor.js.map +1 -0
- package/dist/dynamo/dynamo-query-scan-processor.d.ts +25 -25
- package/dist/dynamo/dynamo-query-scan-processor.js +268 -268
- package/dist/dynamo/dynamo-query-scan-processor.js.map +1 -1
- package/dist/helpers/base-joi-helper.d.ts +2 -2
- package/dist/helpers/base-joi-helper.js +12 -12
- package/dist/helpers/constants.d.ts +3 -3
- package/dist/helpers/constants.js +6 -6
- package/dist/helpers/errors.d.ts +13 -13
- package/dist/helpers/errors.js +38 -38
- package/dist/helpers/logging-service.d.ts +9 -9
- package/dist/helpers/logging-service.js +53 -53
- package/dist/helpers/logging-service.js.map +1 -1
- package/dist/helpers/mocody-utils.d.ts +16 -16
- package/dist/helpers/mocody-utils.js +58 -58
- package/dist/helpers/query-validator.d.ts +20 -20
- package/dist/helpers/query-validator.js +96 -96
- package/dist/helpers/util-service.d.ts +29 -29
- package/dist/helpers/util-service.js +155 -155
- package/dist/index.d.ts +12 -12
- package/dist/index.js +23 -23
- package/dist/model/index.d.ts +42 -42
- package/dist/model/index.js +6 -6
- package/dist/mongo/mongo-data-operation.d.ts +90 -90
- package/dist/mongo/mongo-data-operation.js +607 -607
- package/dist/mongo/mongo-data-operation.js.map +1 -1
- package/dist/mongo/mongo-filter-query-operation.d.ts +45 -47
- package/dist/mongo/mongo-filter-query-operation.js +368 -433
- package/dist/mongo/mongo-filter-query-operation.js.map +1 -1
- package/dist/mongo/mongo-initializer.d.ts +16 -16
- package/dist/mongo/mongo-initializer.js +47 -47
- package/dist/mongo/mongo-initializer.js.map +1 -1
- package/dist/mongo/mongo-table-manager.d.ts +31 -31
- package/dist/mongo/mongo-table-manager.js +104 -104
- package/dist/mongo/mongo-table-manager.js.map +1 -1
- package/dist/pouch/pouch-data-operation.d.ts +90 -90
- package/dist/pouch/pouch-data-operation.js +583 -583
- package/dist/pouch/pouch-data-operation.js.map +1 -1
- package/dist/pouch/pouch-filter-query-operation.d.ts +45 -47
- package/dist/pouch/pouch-filter-query-operation.js +371 -438
- package/dist/pouch/pouch-filter-query-operation.js.map +1 -1
- package/dist/pouch/pouch-initializer.d.ts +111 -109
- package/dist/pouch/pouch-initializer.js +357 -357
- package/dist/pouch/pouch-initializer.js.map +1 -1
- package/dist/pouch/pouch-manage-table.d.ts +35 -35
- package/dist/pouch/pouch-manage-table.js +99 -99
- package/dist/pouch/pouch-manage-table.js.map +1 -1
- package/dist/type/index.d.ts +118 -119
- package/dist/type/index.js +2 -2
- package/package.json +17 -16
|
@@ -1,358 +1,358 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MocodyInitializerPouch = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const nano_1 = tslib_1.__importDefault(require("nano"));
|
|
6
|
-
const throat_1 = tslib_1.__importDefault(require("throat"));
|
|
7
|
-
const pouchdb_1 = tslib_1.__importDefault(require("pouchdb"));
|
|
8
|
-
const pouchdb_find_1 = tslib_1.__importDefault(require("pouchdb-find"));
|
|
9
|
-
const logging_service_1 = require("../helpers/logging-service");
|
|
10
|
-
const errors_1 = require("../helpers/errors");
|
|
11
|
-
const util_service_1 = require("../helpers/util-service");
|
|
12
|
-
const concurrency = (0, throat_1.default)(1);
|
|
13
|
-
class MocodyInitializerPouch {
|
|
14
|
-
constructor(baseConfig) {
|
|
15
|
-
Object.defineProperty(this, "_databaseInstance", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
writable: true,
|
|
19
|
-
value: void 0
|
|
20
|
-
});
|
|
21
|
-
Object.defineProperty(this, "_pouchInstance", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: void 0
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "_documentScope", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: void 0
|
|
32
|
-
});
|
|
33
|
-
Object.defineProperty(this, "baseConfig", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: void 0
|
|
38
|
-
});
|
|
39
|
-
this.baseConfig = baseConfig;
|
|
40
|
-
}
|
|
41
|
-
async deleteIndex({ ddoc, name }) {
|
|
42
|
-
if (this.isLocalFirst()) {
|
|
43
|
-
return await this.pouch_deleteIndex({ ddoc, name });
|
|
44
|
-
}
|
|
45
|
-
const path = ["_index", ddoc, "json", name].join("/");
|
|
46
|
-
const instance = await this.getInstance();
|
|
47
|
-
const config = this.getRemoteFirstConfig();
|
|
48
|
-
const result = await instance.relax({
|
|
49
|
-
db: config.pouchConfig.databaseName,
|
|
50
|
-
method: "DELETE",
|
|
51
|
-
path,
|
|
52
|
-
content_type: "application/json",
|
|
53
|
-
});
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
async pouch_deleteIndex({ ddoc, name }) {
|
|
57
|
-
const db01 = await this.pouch_getDbInstance();
|
|
58
|
-
return await db01.deleteIndex({ ddoc, name });
|
|
59
|
-
}
|
|
60
|
-
async getIndexes() {
|
|
61
|
-
if (this.isLocalFirst()) {
|
|
62
|
-
return await this.pouch_getIndexes();
|
|
63
|
-
}
|
|
64
|
-
const config = this.getRemoteFirstConfig();
|
|
65
|
-
const instance = await this.getInstance();
|
|
66
|
-
const result = await instance.request({
|
|
67
|
-
db: config.pouchConfig.databaseName,
|
|
68
|
-
method: "GET",
|
|
69
|
-
path: "_index",
|
|
70
|
-
content_type: "application/json",
|
|
71
|
-
});
|
|
72
|
-
logging_service_1.LoggingService.log({ indexes: result });
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
async pouch_getIndexes() {
|
|
76
|
-
const db01 = await this.pouch_getDbInstance();
|
|
77
|
-
return await db01.getIndexes();
|
|
78
|
-
}
|
|
79
|
-
async createIndex({ indexName, fields }) {
|
|
80
|
-
if (this.isLocalFirst()) {
|
|
81
|
-
return await this.pouch_createIndex({ indexName, fields });
|
|
82
|
-
}
|
|
83
|
-
return this.couch_createIndex({ indexName, fields });
|
|
84
|
-
}
|
|
85
|
-
async couch_createIndex({ indexName, fields, dbx, }) {
|
|
86
|
-
const instance = dbx || (await this.getDocInstance());
|
|
87
|
-
const result = await instance.createIndex({
|
|
88
|
-
index: {
|
|
89
|
-
fields: fields,
|
|
90
|
-
},
|
|
91
|
-
name: indexName,
|
|
92
|
-
ddoc: indexName,
|
|
93
|
-
type: "json",
|
|
94
|
-
partitioned: true,
|
|
95
|
-
});
|
|
96
|
-
logging_service_1.LoggingService.log(result);
|
|
97
|
-
return {
|
|
98
|
-
id: result.id,
|
|
99
|
-
name: result.name,
|
|
100
|
-
result: result.result,
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
async pouch_createIndex({ indexName, fields, dbx, }) {
|
|
104
|
-
const db01 = dbx || (await this.pouch_getDbInstance());
|
|
105
|
-
return await new Promise((resolve, reject) => {
|
|
106
|
-
return db01.createIndex({
|
|
107
|
-
index: {
|
|
108
|
-
fields: fields,
|
|
109
|
-
name: indexName,
|
|
110
|
-
ddoc: indexName,
|
|
111
|
-
type: "json",
|
|
112
|
-
},
|
|
113
|
-
}, (error, result) => {
|
|
114
|
-
if (error) {
|
|
115
|
-
reject(error);
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
resolve(result);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
async getDocInstance() {
|
|
124
|
-
if (!this._documentScope) {
|
|
125
|
-
const config = this.getRemoteFirstConfig();
|
|
126
|
-
const serverScope = await this.getInstance();
|
|
127
|
-
const db = serverScope.db.use(config.pouchConfig.databaseName);
|
|
128
|
-
try {
|
|
129
|
-
if (config?.indexes?.length) {
|
|
130
|
-
for (const indexItem of config.indexes) {
|
|
131
|
-
await this.couch_createIndex({ ...indexItem, dbx: db });
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
catch (error) {
|
|
136
|
-
logging_service_1.LoggingService.error(error);
|
|
137
|
-
}
|
|
138
|
-
this._documentScope = db;
|
|
139
|
-
}
|
|
140
|
-
return this._documentScope;
|
|
141
|
-
}
|
|
142
|
-
async getById({ nativeId }) {
|
|
143
|
-
if (this.isLocalFirst()) {
|
|
144
|
-
return await this.pouch_getById({ nativeId });
|
|
145
|
-
}
|
|
146
|
-
const db01 = await this.getDocInstance();
|
|
147
|
-
return await db01.get(nativeId);
|
|
148
|
-
}
|
|
149
|
-
async pouch_getById({ nativeId }) {
|
|
150
|
-
const db01 = await this.pouch_getDbInstance();
|
|
151
|
-
return await db01.get(nativeId);
|
|
152
|
-
}
|
|
153
|
-
async getManyByIds({ nativeIds }) {
|
|
154
|
-
if (this.isLocalFirst()) {
|
|
155
|
-
return await this.pouch_getManyByIds({ nativeIds });
|
|
156
|
-
}
|
|
157
|
-
const db01 = await this.getDocInstance();
|
|
158
|
-
const dataList = await db01.list({
|
|
159
|
-
keys: nativeIds,
|
|
160
|
-
include_docs: true,
|
|
161
|
-
});
|
|
162
|
-
return dataList;
|
|
163
|
-
}
|
|
164
|
-
async pouch_getManyByIds({ nativeIds }) {
|
|
165
|
-
const db01 = await this.pouch_getDbInstance();
|
|
166
|
-
const dataList = await db01.allDocs({
|
|
167
|
-
keys: nativeIds,
|
|
168
|
-
include_docs: true,
|
|
169
|
-
});
|
|
170
|
-
return dataList;
|
|
171
|
-
}
|
|
172
|
-
async createDoc({ validatedData }) {
|
|
173
|
-
if (this.isLocalFirst()) {
|
|
174
|
-
return await this.pouch_createDoc({ validatedData });
|
|
175
|
-
}
|
|
176
|
-
const db01 = await this.getDocInstance();
|
|
177
|
-
return await db01.insert(validatedData);
|
|
178
|
-
}
|
|
179
|
-
async pouch_createDoc({ validatedData }) {
|
|
180
|
-
const db01 = await this.pouch_getDbInstance();
|
|
181
|
-
return await db01.put(validatedData);
|
|
182
|
-
}
|
|
183
|
-
async updateDoc({ docRev, validatedData }) {
|
|
184
|
-
if (this.isLocalFirst()) {
|
|
185
|
-
return await this.pouch_updateDoc({ validatedData, docRev });
|
|
186
|
-
}
|
|
187
|
-
const db01 = await this.getDocInstance();
|
|
188
|
-
return await db01.insert({ ...validatedData, _rev: docRev });
|
|
189
|
-
}
|
|
190
|
-
async pouch_updateDoc({ docRev, validatedData }) {
|
|
191
|
-
const db01 = await this.pouch_getDbInstance();
|
|
192
|
-
return await db01.put({ ...validatedData, _rev: docRev });
|
|
193
|
-
}
|
|
194
|
-
async getList({ featureEntity, size, skip }) {
|
|
195
|
-
if (this.isLocalFirst()) {
|
|
196
|
-
return await this.pouch_getList({
|
|
197
|
-
featureEntity,
|
|
198
|
-
size,
|
|
199
|
-
skip,
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
const db01 = await this.getDocInstance();
|
|
203
|
-
const data = await db01.list({
|
|
204
|
-
include_docs: true,
|
|
205
|
-
startkey: featureEntity,
|
|
206
|
-
endkey: `${featureEntity}\ufff0`,
|
|
207
|
-
inclusive_end: true,
|
|
208
|
-
limit: size ?? undefined,
|
|
209
|
-
skip: skip ?? undefined,
|
|
210
|
-
});
|
|
211
|
-
return data;
|
|
212
|
-
}
|
|
213
|
-
async pouch_getList({ featureEntity, size, skip, }) {
|
|
214
|
-
const db01 = await this.pouch_getDbInstance();
|
|
215
|
-
const data = await db01.allDocs({
|
|
216
|
-
include_docs: true,
|
|
217
|
-
startkey: featureEntity,
|
|
218
|
-
endkey: `${featureEntity}\ufff0`,
|
|
219
|
-
inclusive_end: true,
|
|
220
|
-
limit: size ?? undefined,
|
|
221
|
-
skip: skip ?? undefined,
|
|
222
|
-
});
|
|
223
|
-
return data;
|
|
224
|
-
}
|
|
225
|
-
async findPartitionedDocs({ featureEntity, selector, fields, use_index, sort, limit, skip }) {
|
|
226
|
-
if (this.isLocalFirst()) {
|
|
227
|
-
return await this.pouch_findPartitionedDocs({
|
|
228
|
-
featureEntity,
|
|
229
|
-
selector,
|
|
230
|
-
fields,
|
|
231
|
-
use_index,
|
|
232
|
-
sort,
|
|
233
|
-
limit,
|
|
234
|
-
skip,
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
const db01 = await this.getDocInstance();
|
|
238
|
-
return await db01.partitionedFind(featureEntity, {
|
|
239
|
-
selector: { ...selector },
|
|
240
|
-
fields,
|
|
241
|
-
use_index,
|
|
242
|
-
sort: sort?.length ? sort : undefined,
|
|
243
|
-
limit,
|
|
244
|
-
skip,
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
async pouch_findPartitionedDocs({ featureEntity, selector, fields, use_index, sort, limit, skip, }) {
|
|
248
|
-
const db01 = await this.pouch_getDbInstance();
|
|
249
|
-
const paramsQ = {
|
|
250
|
-
selector: { ...selector },
|
|
251
|
-
fields,
|
|
252
|
-
use_index,
|
|
253
|
-
sort: sort?.length ? sort : undefined,
|
|
254
|
-
limit,
|
|
255
|
-
skip,
|
|
256
|
-
};
|
|
257
|
-
logging_service_1.LoggingService.logAsString(paramsQ);
|
|
258
|
-
return await db01.find(paramsQ);
|
|
259
|
-
}
|
|
260
|
-
async deleteById({ docRev, nativeId }) {
|
|
261
|
-
const db01 = await this.getDocInstance();
|
|
262
|
-
return await db01.destroy(nativeId, docRev);
|
|
263
|
-
}
|
|
264
|
-
async getInstance() {
|
|
265
|
-
return await concurrency(() => this.getInstanceBase());
|
|
266
|
-
}
|
|
267
|
-
async getInstanceBase() {
|
|
268
|
-
if (this.baseConfig?.configType !== "REMOTE_FIRST") {
|
|
269
|
-
throw new errors_1.MocodyGenericError("Remote database uri not defined");
|
|
270
|
-
}
|
|
271
|
-
const { pouchConfig } = this.baseConfig;
|
|
272
|
-
if (!pouchConfig?.databaseName) {
|
|
273
|
-
throw new errors_1.MocodyGenericError("Remote database config not defined");
|
|
274
|
-
}
|
|
275
|
-
if (!this._databaseInstance) {
|
|
276
|
-
const { host, port, cookie } = pouchConfig;
|
|
277
|
-
const protocol = pouchConfig.protocol || "http";
|
|
278
|
-
if (pouchConfig.password && pouchConfig.username) {
|
|
279
|
-
const pw = encodeURIComponent(pouchConfig.password);
|
|
280
|
-
const uname = encodeURIComponent(pouchConfig.username);
|
|
281
|
-
const url = `${protocol}://${uname}:${pw}@${host}:${port}`;
|
|
282
|
-
this._databaseInstance = (0, nano_1.default)({ url, cookie });
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
const url = `${protocol}://${host}:${port}`;
|
|
286
|
-
this._databaseInstance = (0, nano_1.default)({ url, cookie });
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return await Promise.resolve(this._databaseInstance);
|
|
290
|
-
}
|
|
291
|
-
async pouch_getDbInstance() {
|
|
292
|
-
return await concurrency(() => this.pouch_getDbInstanceBase());
|
|
293
|
-
}
|
|
294
|
-
isLocalFirst() {
|
|
295
|
-
return this.baseConfig?.configType === "LOCAL_FIRST";
|
|
296
|
-
}
|
|
297
|
-
getRemoteFirstConfig() {
|
|
298
|
-
if (this.baseConfig?.configType !== "REMOTE_FIRST") {
|
|
299
|
-
throw new errors_1.MocodyGenericError("REMOTE_FIRST not configured");
|
|
300
|
-
}
|
|
301
|
-
if (!this.baseConfig?.pouchConfig) {
|
|
302
|
-
throw new errors_1.MocodyGenericError("couchDbUri not defined");
|
|
303
|
-
}
|
|
304
|
-
return this.baseConfig;
|
|
305
|
-
}
|
|
306
|
-
async pouch_getDbInstanceBase() {
|
|
307
|
-
if (!this._pouchInstance) {
|
|
308
|
-
if (this.baseConfig?.configType !== "LOCAL_FIRST") {
|
|
309
|
-
throw new errors_1.MocodyGenericError("LOCAL_FIRST not configured");
|
|
310
|
-
}
|
|
311
|
-
if (!this.baseConfig?.sqliteDbFilePath) {
|
|
312
|
-
throw new errors_1.MocodyGenericError("sqliteDbFilePath not defined");
|
|
313
|
-
}
|
|
314
|
-
const { sqliteDbFilePath, liveSync, indexes } = this.baseConfig;
|
|
315
|
-
pouchdb_1.default.plugin(require("pouchdb-adapter-node-websql"));
|
|
316
|
-
pouchdb_1.default.plugin(pouchdb_find_1.default);
|
|
317
|
-
this._pouchInstance = new pouchdb_1.default(sqliteDbFilePath, { adapter: "websql" });
|
|
318
|
-
try {
|
|
319
|
-
if (indexes?.length) {
|
|
320
|
-
for (const indexItem of indexes) {
|
|
321
|
-
await this.pouch_createIndex({ ...indexItem, dbx: this._pouchInstance });
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
catch (error) {
|
|
326
|
-
logging_service_1.LoggingService.error(error);
|
|
327
|
-
}
|
|
328
|
-
if (this.baseConfig.couchConfig) {
|
|
329
|
-
const { password, protocol, username, host, port } = this.baseConfig.couchConfig;
|
|
330
|
-
const pw = encodeURIComponent(password);
|
|
331
|
-
const uname = encodeURIComponent(username);
|
|
332
|
-
const couchDbSyncUri = `${protocol}://${uname}:${pw}@${host}:${port}`;
|
|
333
|
-
if (liveSync) {
|
|
334
|
-
this._pouchInstance
|
|
335
|
-
.sync(couchDbSyncUri, {
|
|
336
|
-
live: true,
|
|
337
|
-
retry: true,
|
|
338
|
-
})
|
|
339
|
-
.on("change", (change) => {
|
|
340
|
-
logging_service_1.LoggingService.log({ replication_change: change });
|
|
341
|
-
})
|
|
342
|
-
.on("paused", (info) => {
|
|
343
|
-
logging_service_1.LoggingService.log({ replication_info: info });
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
this._pouchInstance.sync(couchDbSyncUri).on("error", (err) => {
|
|
348
|
-
logging_service_1.LoggingService.log({ replication_error: err });
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
await util_service_1.UtilService.waitUntilMilliseconds(800);
|
|
353
|
-
}
|
|
354
|
-
return await Promise.resolve(this._pouchInstance);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
exports.MocodyInitializerPouch = MocodyInitializerPouch;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MocodyInitializerPouch = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const nano_1 = tslib_1.__importDefault(require("nano"));
|
|
6
|
+
const throat_1 = tslib_1.__importDefault(require("throat"));
|
|
7
|
+
const pouchdb_1 = tslib_1.__importDefault(require("pouchdb"));
|
|
8
|
+
const pouchdb_find_1 = tslib_1.__importDefault(require("pouchdb-find"));
|
|
9
|
+
const logging_service_1 = require("../helpers/logging-service");
|
|
10
|
+
const errors_1 = require("../helpers/errors");
|
|
11
|
+
const util_service_1 = require("../helpers/util-service");
|
|
12
|
+
const concurrency = (0, throat_1.default)(1);
|
|
13
|
+
class MocodyInitializerPouch {
|
|
14
|
+
constructor(baseConfig) {
|
|
15
|
+
Object.defineProperty(this, "_databaseInstance", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "_pouchInstance", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "_documentScope", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "baseConfig", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
39
|
+
this.baseConfig = baseConfig;
|
|
40
|
+
}
|
|
41
|
+
async deleteIndex({ ddoc, name }) {
|
|
42
|
+
if (this.isLocalFirst()) {
|
|
43
|
+
return await this.pouch_deleteIndex({ ddoc, name });
|
|
44
|
+
}
|
|
45
|
+
const path = ["_index", ddoc, "json", name].join("/");
|
|
46
|
+
const instance = await this.getInstance();
|
|
47
|
+
const config = this.getRemoteFirstConfig();
|
|
48
|
+
const result = await instance.relax({
|
|
49
|
+
db: config.pouchConfig.databaseName,
|
|
50
|
+
method: "DELETE",
|
|
51
|
+
path,
|
|
52
|
+
content_type: "application/json",
|
|
53
|
+
});
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
async pouch_deleteIndex({ ddoc, name }) {
|
|
57
|
+
const db01 = await this.pouch_getDbInstance();
|
|
58
|
+
return await db01.deleteIndex({ ddoc, name });
|
|
59
|
+
}
|
|
60
|
+
async getIndexes() {
|
|
61
|
+
if (this.isLocalFirst()) {
|
|
62
|
+
return await this.pouch_getIndexes();
|
|
63
|
+
}
|
|
64
|
+
const config = this.getRemoteFirstConfig();
|
|
65
|
+
const instance = await this.getInstance();
|
|
66
|
+
const result = await instance.request({
|
|
67
|
+
db: config.pouchConfig.databaseName,
|
|
68
|
+
method: "GET",
|
|
69
|
+
path: "_index",
|
|
70
|
+
content_type: "application/json",
|
|
71
|
+
});
|
|
72
|
+
logging_service_1.LoggingService.log({ indexes: result });
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
async pouch_getIndexes() {
|
|
76
|
+
const db01 = await this.pouch_getDbInstance();
|
|
77
|
+
return await db01.getIndexes();
|
|
78
|
+
}
|
|
79
|
+
async createIndex({ indexName, fields }) {
|
|
80
|
+
if (this.isLocalFirst()) {
|
|
81
|
+
return await this.pouch_createIndex({ indexName, fields });
|
|
82
|
+
}
|
|
83
|
+
return this.couch_createIndex({ indexName, fields });
|
|
84
|
+
}
|
|
85
|
+
async couch_createIndex({ indexName, fields, dbx, }) {
|
|
86
|
+
const instance = dbx || (await this.getDocInstance());
|
|
87
|
+
const result = await instance.createIndex({
|
|
88
|
+
index: {
|
|
89
|
+
fields: fields,
|
|
90
|
+
},
|
|
91
|
+
name: indexName,
|
|
92
|
+
ddoc: indexName,
|
|
93
|
+
type: "json",
|
|
94
|
+
partitioned: true,
|
|
95
|
+
});
|
|
96
|
+
logging_service_1.LoggingService.log(result);
|
|
97
|
+
return {
|
|
98
|
+
id: result.id,
|
|
99
|
+
name: result.name,
|
|
100
|
+
result: result.result,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async pouch_createIndex({ indexName, fields, dbx, }) {
|
|
104
|
+
const db01 = dbx || (await this.pouch_getDbInstance());
|
|
105
|
+
return await new Promise((resolve, reject) => {
|
|
106
|
+
return db01.createIndex({
|
|
107
|
+
index: {
|
|
108
|
+
fields: fields,
|
|
109
|
+
name: indexName,
|
|
110
|
+
ddoc: indexName,
|
|
111
|
+
type: "json",
|
|
112
|
+
},
|
|
113
|
+
}, (error, result) => {
|
|
114
|
+
if (error) {
|
|
115
|
+
reject(error);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
resolve(result);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
async getDocInstance() {
|
|
124
|
+
if (!this._documentScope) {
|
|
125
|
+
const config = this.getRemoteFirstConfig();
|
|
126
|
+
const serverScope = await this.getInstance();
|
|
127
|
+
const db = serverScope.db.use(config.pouchConfig.databaseName);
|
|
128
|
+
try {
|
|
129
|
+
if (config?.indexes?.length) {
|
|
130
|
+
for (const indexItem of config.indexes) {
|
|
131
|
+
await this.couch_createIndex({ ...indexItem, dbx: db });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
logging_service_1.LoggingService.error(error);
|
|
137
|
+
}
|
|
138
|
+
this._documentScope = db;
|
|
139
|
+
}
|
|
140
|
+
return this._documentScope;
|
|
141
|
+
}
|
|
142
|
+
async getById({ nativeId }) {
|
|
143
|
+
if (this.isLocalFirst()) {
|
|
144
|
+
return await this.pouch_getById({ nativeId });
|
|
145
|
+
}
|
|
146
|
+
const db01 = await this.getDocInstance();
|
|
147
|
+
return await db01.get(nativeId);
|
|
148
|
+
}
|
|
149
|
+
async pouch_getById({ nativeId }) {
|
|
150
|
+
const db01 = await this.pouch_getDbInstance();
|
|
151
|
+
return await db01.get(nativeId);
|
|
152
|
+
}
|
|
153
|
+
async getManyByIds({ nativeIds }) {
|
|
154
|
+
if (this.isLocalFirst()) {
|
|
155
|
+
return await this.pouch_getManyByIds({ nativeIds });
|
|
156
|
+
}
|
|
157
|
+
const db01 = await this.getDocInstance();
|
|
158
|
+
const dataList = await db01.list({
|
|
159
|
+
keys: nativeIds,
|
|
160
|
+
include_docs: true,
|
|
161
|
+
});
|
|
162
|
+
return dataList;
|
|
163
|
+
}
|
|
164
|
+
async pouch_getManyByIds({ nativeIds }) {
|
|
165
|
+
const db01 = await this.pouch_getDbInstance();
|
|
166
|
+
const dataList = await db01.allDocs({
|
|
167
|
+
keys: nativeIds,
|
|
168
|
+
include_docs: true,
|
|
169
|
+
});
|
|
170
|
+
return dataList;
|
|
171
|
+
}
|
|
172
|
+
async createDoc({ validatedData }) {
|
|
173
|
+
if (this.isLocalFirst()) {
|
|
174
|
+
return await this.pouch_createDoc({ validatedData });
|
|
175
|
+
}
|
|
176
|
+
const db01 = await this.getDocInstance();
|
|
177
|
+
return await db01.insert(validatedData);
|
|
178
|
+
}
|
|
179
|
+
async pouch_createDoc({ validatedData }) {
|
|
180
|
+
const db01 = await this.pouch_getDbInstance();
|
|
181
|
+
return await db01.put(validatedData);
|
|
182
|
+
}
|
|
183
|
+
async updateDoc({ docRev, validatedData }) {
|
|
184
|
+
if (this.isLocalFirst()) {
|
|
185
|
+
return await this.pouch_updateDoc({ validatedData, docRev });
|
|
186
|
+
}
|
|
187
|
+
const db01 = await this.getDocInstance();
|
|
188
|
+
return await db01.insert({ ...validatedData, _rev: docRev });
|
|
189
|
+
}
|
|
190
|
+
async pouch_updateDoc({ docRev, validatedData }) {
|
|
191
|
+
const db01 = await this.pouch_getDbInstance();
|
|
192
|
+
return await db01.put({ ...validatedData, _rev: docRev });
|
|
193
|
+
}
|
|
194
|
+
async getList({ featureEntity, size, skip }) {
|
|
195
|
+
if (this.isLocalFirst()) {
|
|
196
|
+
return await this.pouch_getList({
|
|
197
|
+
featureEntity,
|
|
198
|
+
size,
|
|
199
|
+
skip,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
const db01 = await this.getDocInstance();
|
|
203
|
+
const data = await db01.list({
|
|
204
|
+
include_docs: true,
|
|
205
|
+
startkey: featureEntity,
|
|
206
|
+
endkey: `${featureEntity}\ufff0`,
|
|
207
|
+
inclusive_end: true,
|
|
208
|
+
limit: size ?? undefined,
|
|
209
|
+
skip: skip ?? undefined,
|
|
210
|
+
});
|
|
211
|
+
return data;
|
|
212
|
+
}
|
|
213
|
+
async pouch_getList({ featureEntity, size, skip, }) {
|
|
214
|
+
const db01 = await this.pouch_getDbInstance();
|
|
215
|
+
const data = await db01.allDocs({
|
|
216
|
+
include_docs: true,
|
|
217
|
+
startkey: featureEntity,
|
|
218
|
+
endkey: `${featureEntity}\ufff0`,
|
|
219
|
+
inclusive_end: true,
|
|
220
|
+
limit: size ?? undefined,
|
|
221
|
+
skip: skip ?? undefined,
|
|
222
|
+
});
|
|
223
|
+
return data;
|
|
224
|
+
}
|
|
225
|
+
async findPartitionedDocs({ featureEntity, selector, fields, use_index, sort, limit, skip }) {
|
|
226
|
+
if (this.isLocalFirst()) {
|
|
227
|
+
return await this.pouch_findPartitionedDocs({
|
|
228
|
+
featureEntity,
|
|
229
|
+
selector,
|
|
230
|
+
fields,
|
|
231
|
+
use_index,
|
|
232
|
+
sort,
|
|
233
|
+
limit,
|
|
234
|
+
skip,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
const db01 = await this.getDocInstance();
|
|
238
|
+
return await db01.partitionedFind(featureEntity, {
|
|
239
|
+
selector: { ...selector },
|
|
240
|
+
fields,
|
|
241
|
+
use_index,
|
|
242
|
+
sort: sort?.length ? sort : undefined,
|
|
243
|
+
limit,
|
|
244
|
+
skip,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
async pouch_findPartitionedDocs({ featureEntity, selector, fields, use_index, sort, limit, skip, }) {
|
|
248
|
+
const db01 = await this.pouch_getDbInstance();
|
|
249
|
+
const paramsQ = {
|
|
250
|
+
selector: { ...selector },
|
|
251
|
+
fields,
|
|
252
|
+
use_index,
|
|
253
|
+
sort: sort?.length ? sort : undefined,
|
|
254
|
+
limit,
|
|
255
|
+
skip,
|
|
256
|
+
};
|
|
257
|
+
logging_service_1.LoggingService.logAsString(paramsQ);
|
|
258
|
+
return await db01.find(paramsQ);
|
|
259
|
+
}
|
|
260
|
+
async deleteById({ docRev, nativeId }) {
|
|
261
|
+
const db01 = await this.getDocInstance();
|
|
262
|
+
return await db01.destroy(nativeId, docRev);
|
|
263
|
+
}
|
|
264
|
+
async getInstance() {
|
|
265
|
+
return await concurrency(() => this.getInstanceBase());
|
|
266
|
+
}
|
|
267
|
+
async getInstanceBase() {
|
|
268
|
+
if (this.baseConfig?.configType !== "REMOTE_FIRST") {
|
|
269
|
+
throw new errors_1.MocodyGenericError("Remote database uri not defined");
|
|
270
|
+
}
|
|
271
|
+
const { pouchConfig } = this.baseConfig;
|
|
272
|
+
if (!pouchConfig?.databaseName) {
|
|
273
|
+
throw new errors_1.MocodyGenericError("Remote database config not defined");
|
|
274
|
+
}
|
|
275
|
+
if (!this._databaseInstance) {
|
|
276
|
+
const { host, port, cookie } = pouchConfig;
|
|
277
|
+
const protocol = pouchConfig.protocol || "http";
|
|
278
|
+
if (pouchConfig.password && pouchConfig.username) {
|
|
279
|
+
const pw = encodeURIComponent(pouchConfig.password);
|
|
280
|
+
const uname = encodeURIComponent(pouchConfig.username);
|
|
281
|
+
const url = `${protocol}://${uname}:${pw}@${host}:${port}`;
|
|
282
|
+
this._databaseInstance = (0, nano_1.default)({ url, cookie });
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
const url = `${protocol}://${host}:${port}`;
|
|
286
|
+
this._databaseInstance = (0, nano_1.default)({ url, cookie });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return await Promise.resolve(this._databaseInstance);
|
|
290
|
+
}
|
|
291
|
+
async pouch_getDbInstance() {
|
|
292
|
+
return await concurrency(() => this.pouch_getDbInstanceBase());
|
|
293
|
+
}
|
|
294
|
+
isLocalFirst() {
|
|
295
|
+
return this.baseConfig?.configType === "LOCAL_FIRST";
|
|
296
|
+
}
|
|
297
|
+
getRemoteFirstConfig() {
|
|
298
|
+
if (this.baseConfig?.configType !== "REMOTE_FIRST") {
|
|
299
|
+
throw new errors_1.MocodyGenericError("REMOTE_FIRST not configured");
|
|
300
|
+
}
|
|
301
|
+
if (!this.baseConfig?.pouchConfig) {
|
|
302
|
+
throw new errors_1.MocodyGenericError("couchDbUri not defined");
|
|
303
|
+
}
|
|
304
|
+
return this.baseConfig;
|
|
305
|
+
}
|
|
306
|
+
async pouch_getDbInstanceBase() {
|
|
307
|
+
if (!this._pouchInstance) {
|
|
308
|
+
if (this.baseConfig?.configType !== "LOCAL_FIRST") {
|
|
309
|
+
throw new errors_1.MocodyGenericError("LOCAL_FIRST not configured");
|
|
310
|
+
}
|
|
311
|
+
if (!this.baseConfig?.sqliteDbFilePath) {
|
|
312
|
+
throw new errors_1.MocodyGenericError("sqliteDbFilePath not defined");
|
|
313
|
+
}
|
|
314
|
+
const { sqliteDbFilePath, liveSync, indexes } = this.baseConfig;
|
|
315
|
+
pouchdb_1.default.plugin(require("pouchdb-adapter-node-websql"));
|
|
316
|
+
pouchdb_1.default.plugin(pouchdb_find_1.default);
|
|
317
|
+
this._pouchInstance = new pouchdb_1.default(sqliteDbFilePath, { adapter: "websql" });
|
|
318
|
+
try {
|
|
319
|
+
if (indexes?.length) {
|
|
320
|
+
for (const indexItem of indexes) {
|
|
321
|
+
await this.pouch_createIndex({ ...indexItem, dbx: this._pouchInstance });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
logging_service_1.LoggingService.error(error);
|
|
327
|
+
}
|
|
328
|
+
if (this.baseConfig.couchConfig) {
|
|
329
|
+
const { password, protocol, username, host, port } = this.baseConfig.couchConfig;
|
|
330
|
+
const pw = encodeURIComponent(password);
|
|
331
|
+
const uname = encodeURIComponent(username);
|
|
332
|
+
const couchDbSyncUri = `${protocol}://${uname}:${pw}@${host}:${port}`;
|
|
333
|
+
if (liveSync) {
|
|
334
|
+
this._pouchInstance
|
|
335
|
+
.sync(couchDbSyncUri, {
|
|
336
|
+
live: true,
|
|
337
|
+
retry: true,
|
|
338
|
+
})
|
|
339
|
+
.on("change", (change) => {
|
|
340
|
+
logging_service_1.LoggingService.log({ replication_change: change });
|
|
341
|
+
})
|
|
342
|
+
.on("paused", (info) => {
|
|
343
|
+
logging_service_1.LoggingService.log({ replication_info: info });
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
this._pouchInstance.sync(couchDbSyncUri).on("error", (err) => {
|
|
348
|
+
logging_service_1.LoggingService.log({ replication_error: err });
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
await util_service_1.UtilService.waitUntilMilliseconds(800);
|
|
353
|
+
}
|
|
354
|
+
return await Promise.resolve(this._pouchInstance);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
exports.MocodyInitializerPouch = MocodyInitializerPouch;
|
|
358
358
|
//# sourceMappingURL=pouch-initializer.js.map
|