apexify.js 1.2.1
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/README.md +1117 -0
- package/index.js +111 -0
- package/lib/ai/apexAI.js +312 -0
- package/lib/ai/functions/chunkString.js +8 -0
- package/lib/ai/functions/draw.js +347 -0
- package/lib/ai/functions/generateVoiceResponse.js +239 -0
- package/lib/ai/functions/processImageAttachments.js +93 -0
- package/lib/ai/functions/processMessageContent.js +48 -0
- package/lib/ai/functions/readFiles.js +95 -0
- package/lib/ai/functions/shouldDrawImage.js +12 -0
- package/lib/ai/utils.js +18 -0
- package/lib/canvas/ApexPainter.js +1194 -0
- package/lib/canvas/themes/level ara.ttf +0 -0
- package/lib/canvas/themes/levelFont.ttf +0 -0
- package/lib/canvas/themes/levels-card.js +872 -0
- package/lib/canvas/themes/music-card.js +342 -0
- package/lib/canvas/themes/numbers.ttf +0 -0
- package/lib/canvas/themes/tajawal.ttf +0 -0
- package/lib/database/MongoDB.js +126 -0
- package/lib/database/NanoDB.js +1318 -0
- package/lib/database/mongoDb/aggregate.js +120 -0
- package/lib/database/mongoDb/countDocs.js +115 -0
- package/lib/database/mongoDb/createCollection.js +125 -0
- package/lib/database/mongoDb/dataSize.js +89 -0
- package/lib/database/mongoDb/distinct.js +110 -0
- package/lib/database/mongoDb/drop.js +76 -0
- package/lib/database/mongoDb/find.js +122 -0
- package/lib/database/mongoDb/geoNear.js +91 -0
- package/lib/database/mongoDb/index.js +71 -0
- package/lib/database/mongoDb/listCollections.js +81 -0
- package/lib/database/mongoDb/migrateAndPrune.js +89 -0
- package/lib/database/mongoDb/migrateData.js +79 -0
- package/lib/database/mongoDb/remove.js +73 -0
- package/lib/database/mongoDb/removeMany.js +73 -0
- package/lib/database/mongoDb/removeManyExcept.js +91 -0
- package/lib/database/mongoDb/removeSpecific.js +93 -0
- package/lib/database/mongoDb/save.js +94 -0
- package/lib/database/mongoDb/searchMany.js +109 -0
- package/lib/database/mongoDb/textSearch.js +88 -0
- package/lib/database/mongoDb/updateAll.js +80 -0
- package/lib/database/mongoDb/updateAllExcept.js +108 -0
- package/lib/database/mongoDb/updateData.js +106 -0
- package/lib/database/nanoDb/fetchData.js +39 -0
- package/lib/database/nanoDb/removeField.js +46 -0
- package/lib/database/nanoDb/saveData.js +68 -0
- package/lib/database/nanoDb/updateFilter.js +24 -0
- package/lib/database/ready-schemas.js +245 -0
- package/lib/database/utils.js +58 -0
- package/lib/discord/discord-build/commands-(prefix)/music/functions/buttons.js +361 -0
- package/lib/discord/discord-build/commands-(prefix)/music/functions/end.js +58 -0
- package/lib/discord/discord-build/commands-(prefix)/music/functions/start.js +115 -0
- package/lib/discord/discord-build/commands-(prefix)/music/play.js +152 -0
- package/lib/discord/discord-build/commands-(prefix)/music/previous.js +82 -0
- package/lib/discord/discord-build/commands-(prefix)/music/resume.js +93 -0
- package/lib/discord/discord-build/commands-(prefix)/music/skip.js +82 -0
- package/lib/discord/discord-build/commands-(prefix)/music/stop.js +79 -0
- package/lib/discord/discord-build/components/buttons.js +75 -0
- package/lib/discord/discord-build/components/menus.js +81 -0
- package/lib/discord/discord-build/components/paginator.js +156 -0
- package/lib/discord/discord-build/components/permsChecker.js +81 -0
- package/lib/discord/discord-build/utils.js +21 -0
- package/lib/discord/events/eventer.js +100 -0
- package/lib/discord/events/handler/drawMenu.js +351 -0
- package/lib/discord/events/handler/tools.js +652 -0
- package/lib/discord/events/prefixRegister.js +136 -0
- package/lib/discord/events/prefixResponder.js +163 -0
- package/lib/discord/events/slashRegister.js +199 -0
- package/lib/discord/events/slashResponder.js +108 -0
- package/lib/discord/events/starter.js +335 -0
- package/lib/discord/functions/perms.js +19 -0
- package/lib/discord/utils.js +6 -0
- package/lib/general-functions/discord/type-writer.js +77 -0
- package/lib/general-functions/utils.js +0 -0
- package/lib/utils.js +90 -0
- package/package.json +118 -0
|
@@ -0,0 +1,1318 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
39
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
+
function step(op) {
|
|
42
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
45
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
+
switch (op[0]) {
|
|
47
|
+
case 0: case 1: t = op; break;
|
|
48
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
+
default:
|
|
52
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
+
if (t[2]) _.ops.pop();
|
|
57
|
+
_.trys.pop(); continue;
|
|
58
|
+
}
|
|
59
|
+
op = body.call(thisArg, _);
|
|
60
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
65
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
66
|
+
if (ar || !(i in from)) {
|
|
67
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
68
|
+
ar[i] = from[i];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
72
|
+
};
|
|
73
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
74
|
+
exports.NanoDb = void 0;
|
|
75
|
+
var events_1 = require("events");
|
|
76
|
+
var utils_1 = require("./utils");
|
|
77
|
+
var fs = require("fs");
|
|
78
|
+
var fs_1 = require("fs");
|
|
79
|
+
var zlib = require("zlib");
|
|
80
|
+
var path = require("path");
|
|
81
|
+
var crypto = require("crypto-browserify");
|
|
82
|
+
var NanoDb = /** @class */ (function (_super) {
|
|
83
|
+
__extends(NanoDb, _super);
|
|
84
|
+
function NanoDb(filePath) {
|
|
85
|
+
var _this = _super.call(this) || this;
|
|
86
|
+
_this.indexes = new Map();
|
|
87
|
+
if (!filePath)
|
|
88
|
+
throw new Error("Error: filePath is required. Usage: new NanoDb('./data.json')");
|
|
89
|
+
_this.filePath = filePath;
|
|
90
|
+
_this.dataPromise = _this.loadData();
|
|
91
|
+
return _this;
|
|
92
|
+
}
|
|
93
|
+
NanoDb.prototype.loadData = function () {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
95
|
+
var data, err_1;
|
|
96
|
+
return __generator(this, function (_a) {
|
|
97
|
+
switch (_a.label) {
|
|
98
|
+
case 0:
|
|
99
|
+
_a.trys.push([0, 2, , 3]);
|
|
100
|
+
return [4 /*yield*/, fs_1.promises.readFile(this.filePath, 'utf8')];
|
|
101
|
+
case 1:
|
|
102
|
+
data = _a.sent();
|
|
103
|
+
return [2 /*return*/, JSON.parse(data)];
|
|
104
|
+
case 2:
|
|
105
|
+
err_1 = _a.sent();
|
|
106
|
+
return [2 /*return*/, []];
|
|
107
|
+
case 3: return [2 /*return*/];
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
NanoDb.prototype.addOne = function (newData, options) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
114
|
+
var result, error_1;
|
|
115
|
+
return __generator(this, function (_a) {
|
|
116
|
+
switch (_a.label) {
|
|
117
|
+
case 0:
|
|
118
|
+
_a.trys.push([0, 2, , 3]);
|
|
119
|
+
return [4 /*yield*/, (0, utils_1.saveData)(this.filePath, newData, options && options.uniqueKeys)];
|
|
120
|
+
case 1:
|
|
121
|
+
result = _a.sent();
|
|
122
|
+
if (result.success) {
|
|
123
|
+
this.emit('dataInserted', newData);
|
|
124
|
+
}
|
|
125
|
+
this.dataPromise = this.loadData();
|
|
126
|
+
return [2 /*return*/, result];
|
|
127
|
+
case 2:
|
|
128
|
+
error_1 = _a.sent();
|
|
129
|
+
return [2 /*return*/, {
|
|
130
|
+
success: false,
|
|
131
|
+
message: 'Error saving data',
|
|
132
|
+
errorMessage: error_1.message,
|
|
133
|
+
documentsCount: 0,
|
|
134
|
+
}];
|
|
135
|
+
case 3: return [2 /*return*/];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
NanoDb.prototype.displayData = function (options) {
|
|
141
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
142
|
+
var data, filteredData, sortOrder_1, page, pageSize, startIndex, endIndex, paginatedData, error_2;
|
|
143
|
+
return __generator(this, function (_a) {
|
|
144
|
+
switch (_a.label) {
|
|
145
|
+
case 0:
|
|
146
|
+
_a.trys.push([0, 2, , 3]);
|
|
147
|
+
return [4 /*yield*/, this.dataPromise];
|
|
148
|
+
case 1:
|
|
149
|
+
data = _a.sent();
|
|
150
|
+
filteredData = data;
|
|
151
|
+
if (options === null || options === void 0 ? void 0 : options.filters) {
|
|
152
|
+
filteredData = data.filter(function (document) { return (0, utils_1.matchesFilterQuery)(document, options.filters); });
|
|
153
|
+
}
|
|
154
|
+
if (options === null || options === void 0 ? void 0 : options.sortBy) {
|
|
155
|
+
sortOrder_1 = (options === null || options === void 0 ? void 0 : options.sortOrder) === 'desc' ? -1 : 1;
|
|
156
|
+
filteredData.sort(function (a, b) { return (a[options.sortBy] > b[options.sortBy] ? sortOrder_1 : -sortOrder_1); });
|
|
157
|
+
}
|
|
158
|
+
page = (options === null || options === void 0 ? void 0 : options.page) || 1;
|
|
159
|
+
pageSize = (options === null || options === void 0 ? void 0 : options.pageSize) || 10;
|
|
160
|
+
startIndex = (page - 1) * pageSize;
|
|
161
|
+
endIndex = startIndex + pageSize;
|
|
162
|
+
paginatedData = filteredData.slice(startIndex, endIndex);
|
|
163
|
+
return [2 /*return*/, {
|
|
164
|
+
success: true,
|
|
165
|
+
message: 'Data displayed successfully',
|
|
166
|
+
documentsCount: paginatedData.length,
|
|
167
|
+
displayedResults: paginatedData,
|
|
168
|
+
}];
|
|
169
|
+
case 2:
|
|
170
|
+
error_2 = _a.sent();
|
|
171
|
+
return [2 /*return*/, {
|
|
172
|
+
success: false,
|
|
173
|
+
message: 'Error displaying data',
|
|
174
|
+
errorMessage: error_2.message,
|
|
175
|
+
documentsCount: 0,
|
|
176
|
+
displayedResults: [],
|
|
177
|
+
}];
|
|
178
|
+
case 3: return [2 /*return*/];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
NanoDb.prototype.removeOne = function (filterQuery, removeFilter) {
|
|
184
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
185
|
+
var data, result, error_3;
|
|
186
|
+
return __generator(this, function (_a) {
|
|
187
|
+
switch (_a.label) {
|
|
188
|
+
case 0:
|
|
189
|
+
_a.trys.push([0, 3, , 4]);
|
|
190
|
+
return [4 /*yield*/, this.dataPromise];
|
|
191
|
+
case 1:
|
|
192
|
+
data = _a.sent();
|
|
193
|
+
result = (0, utils_1.removeFields)(data, filterQuery, removeFilter);
|
|
194
|
+
return [4 /*yield*/, fs_1.promises.writeFile(this.filePath, JSON.stringify(result, null, 2), 'utf8')];
|
|
195
|
+
case 2:
|
|
196
|
+
_a.sent();
|
|
197
|
+
this.emit('dataRemoved', {
|
|
198
|
+
success: true,
|
|
199
|
+
message: 'Successfully removed the specified field(s) from a single document.',
|
|
200
|
+
documentsCount: result.length,
|
|
201
|
+
});
|
|
202
|
+
return [2 /*return*/, {
|
|
203
|
+
success: true,
|
|
204
|
+
message: 'Successfully removed the specified field(s) from a single document.',
|
|
205
|
+
documentsCount: result.length,
|
|
206
|
+
}];
|
|
207
|
+
case 3:
|
|
208
|
+
error_3 = _a.sent();
|
|
209
|
+
this.emit('dataRemoveError', {
|
|
210
|
+
success: false,
|
|
211
|
+
message: 'Error removing field(s) from a single document.',
|
|
212
|
+
errorMessage: error_3.message,
|
|
213
|
+
documentsCount: 0,
|
|
214
|
+
});
|
|
215
|
+
return [2 /*return*/, {
|
|
216
|
+
success: false,
|
|
217
|
+
message: 'Error removing field(s) from a single document.',
|
|
218
|
+
errorMessage: error_3.message,
|
|
219
|
+
documentsCount: 0,
|
|
220
|
+
}];
|
|
221
|
+
case 4: return [2 /*return*/];
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
NanoDb.prototype.removeDoc = function (filterQuery) {
|
|
227
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
228
|
+
var data, indexToRemove, error_4;
|
|
229
|
+
return __generator(this, function (_a) {
|
|
230
|
+
switch (_a.label) {
|
|
231
|
+
case 0:
|
|
232
|
+
_a.trys.push([0, 5, , 6]);
|
|
233
|
+
return [4 /*yield*/, this.dataPromise];
|
|
234
|
+
case 1:
|
|
235
|
+
data = _a.sent();
|
|
236
|
+
indexToRemove = void 0;
|
|
237
|
+
if (Object.keys(filterQuery).length === 0) {
|
|
238
|
+
indexToRemove = 0;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
indexToRemove = data.findIndex(function (document) { return (0, utils_1.matchesFilterQuery)(document, filterQuery); });
|
|
242
|
+
}
|
|
243
|
+
if (!(indexToRemove !== -1)) return [3 /*break*/, 3];
|
|
244
|
+
data.splice(indexToRemove, 1);
|
|
245
|
+
return [4 /*yield*/, fs_1.promises.writeFile(this.filePath, JSON.stringify(data, null, 2), 'utf8')];
|
|
246
|
+
case 2:
|
|
247
|
+
_a.sent();
|
|
248
|
+
return [2 /*return*/, {
|
|
249
|
+
success: true,
|
|
250
|
+
message: 'Successfully removed the specified document.',
|
|
251
|
+
documentsCount: data.length,
|
|
252
|
+
}];
|
|
253
|
+
case 3: return [2 /*return*/, {
|
|
254
|
+
success: false,
|
|
255
|
+
message: 'Document not found for removal.',
|
|
256
|
+
documentsCount: data.length,
|
|
257
|
+
fileSizeInBytes: 0,
|
|
258
|
+
}];
|
|
259
|
+
case 4: return [3 /*break*/, 6];
|
|
260
|
+
case 5:
|
|
261
|
+
error_4 = _a.sent();
|
|
262
|
+
return [2 /*return*/, {
|
|
263
|
+
success: false,
|
|
264
|
+
message: 'Error removing document.',
|
|
265
|
+
errorMessage: error_4.message,
|
|
266
|
+
documentsCount: 0,
|
|
267
|
+
fileSizeInBytes: 0,
|
|
268
|
+
}];
|
|
269
|
+
case 6: return [2 /*return*/];
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
NanoDb.prototype.updateData = function (filterQuery, updateOptions, upsert) {
|
|
275
|
+
if (upsert === void 0) { upsert = false; }
|
|
276
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
277
|
+
var data, indexToUpdate, updatedDocument, newDocument, error_5;
|
|
278
|
+
return __generator(this, function (_a) {
|
|
279
|
+
switch (_a.label) {
|
|
280
|
+
case 0:
|
|
281
|
+
_a.trys.push([0, 6, , 7]);
|
|
282
|
+
return [4 /*yield*/, this.dataPromise];
|
|
283
|
+
case 1:
|
|
284
|
+
data = _a.sent();
|
|
285
|
+
indexToUpdate = data.findIndex(function (document) { return (0, utils_1.matchesFilterQuery)(document, filterQuery); });
|
|
286
|
+
if (!(indexToUpdate !== -1)) return [3 /*break*/, 3];
|
|
287
|
+
return [4 /*yield*/, (0, utils_1.updateOption)(data[indexToUpdate], updateOptions)];
|
|
288
|
+
case 2:
|
|
289
|
+
updatedDocument = _a.sent();
|
|
290
|
+
data[indexToUpdate] = updatedDocument;
|
|
291
|
+
this.emit('dataUpdated', {
|
|
292
|
+
success: true,
|
|
293
|
+
message: 'Successfully updated the specified document.',
|
|
294
|
+
documentsCount: data.length,
|
|
295
|
+
fileSizeInBytes: 0,
|
|
296
|
+
});
|
|
297
|
+
return [3 /*break*/, 4];
|
|
298
|
+
case 3:
|
|
299
|
+
if (upsert) {
|
|
300
|
+
newDocument = __assign(__assign({}, filterQuery), updateOptions);
|
|
301
|
+
data.push(newDocument);
|
|
302
|
+
this.emit('dataInserted', {
|
|
303
|
+
success: true,
|
|
304
|
+
message: 'Successfully inserted the specified document.',
|
|
305
|
+
documentsCount: data.length,
|
|
306
|
+
fileSizeInBytes: 0,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
this.emit('dataUpdateError', {
|
|
311
|
+
success: false,
|
|
312
|
+
message: 'Document not found for update.',
|
|
313
|
+
documentsCount: data.length,
|
|
314
|
+
fileSizeInBytes: 0,
|
|
315
|
+
});
|
|
316
|
+
return [2 /*return*/, {
|
|
317
|
+
success: false,
|
|
318
|
+
message: 'Document not found for update.',
|
|
319
|
+
documentsCount: data.length,
|
|
320
|
+
fileSizeInBytes: 0,
|
|
321
|
+
}];
|
|
322
|
+
}
|
|
323
|
+
_a.label = 4;
|
|
324
|
+
case 4: return [4 /*yield*/, fs_1.promises.writeFile(this.filePath, JSON.stringify(data, null, 2), 'utf8')];
|
|
325
|
+
case 5:
|
|
326
|
+
_a.sent();
|
|
327
|
+
return [2 /*return*/, {
|
|
328
|
+
success: true,
|
|
329
|
+
message: 'Successfully updated/inserted the specified document.',
|
|
330
|
+
documentsCount: data.length,
|
|
331
|
+
fileSizeInBytes: 0,
|
|
332
|
+
}];
|
|
333
|
+
case 6:
|
|
334
|
+
error_5 = _a.sent();
|
|
335
|
+
this.emit('dataUpdateError', {
|
|
336
|
+
success: false,
|
|
337
|
+
message: 'Error updating/inserting document.',
|
|
338
|
+
errorMessage: error_5.message,
|
|
339
|
+
documentsCount: 0,
|
|
340
|
+
fileSizeInBytes: 0,
|
|
341
|
+
});
|
|
342
|
+
return [2 /*return*/, {
|
|
343
|
+
success: false,
|
|
344
|
+
message: 'Error updating/inserting document.',
|
|
345
|
+
errorMessage: error_5.message,
|
|
346
|
+
documentsCount: 0,
|
|
347
|
+
fileSizeInBytes: 0,
|
|
348
|
+
}];
|
|
349
|
+
case 7: return [2 /*return*/];
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
};
|
|
354
|
+
NanoDb.prototype.findData = function (query) {
|
|
355
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
356
|
+
var data, foundResult, _i, data_1, document_1, isMatch, key, documentValue, queryValue, error_6;
|
|
357
|
+
return __generator(this, function (_a) {
|
|
358
|
+
switch (_a.label) {
|
|
359
|
+
case 0:
|
|
360
|
+
_a.trys.push([0, 2, , 3]);
|
|
361
|
+
return [4 /*yield*/, this.dataPromise];
|
|
362
|
+
case 1:
|
|
363
|
+
data = _a.sent();
|
|
364
|
+
foundResult = null;
|
|
365
|
+
for (_i = 0, data_1 = data; _i < data_1.length; _i++) {
|
|
366
|
+
document_1 = data_1[_i];
|
|
367
|
+
isMatch = true;
|
|
368
|
+
for (key in query) {
|
|
369
|
+
documentValue = String(document_1[key]).toLowerCase();
|
|
370
|
+
queryValue = String(query[key]).toLowerCase();
|
|
371
|
+
if (query[key] !== 'any' && documentValue !== queryValue) {
|
|
372
|
+
isMatch = false;
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (isMatch) {
|
|
377
|
+
foundResult = __assign({}, document_1);
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (!foundResult) {
|
|
382
|
+
return [2 /*return*/, {
|
|
383
|
+
success: false,
|
|
384
|
+
message: 'No data found matching the search.',
|
|
385
|
+
documentsCount: 0,
|
|
386
|
+
foundResults: null,
|
|
387
|
+
fileSizeInBytes: 0,
|
|
388
|
+
}];
|
|
389
|
+
}
|
|
390
|
+
return [2 /*return*/, {
|
|
391
|
+
success: true,
|
|
392
|
+
message: 'Data found successfully',
|
|
393
|
+
documentsCount: 1,
|
|
394
|
+
foundResults: [foundResult],
|
|
395
|
+
fileSizeInBytes: 0,
|
|
396
|
+
}];
|
|
397
|
+
case 2:
|
|
398
|
+
error_6 = _a.sent();
|
|
399
|
+
return [2 /*return*/, {
|
|
400
|
+
success: false,
|
|
401
|
+
message: 'Error finding data',
|
|
402
|
+
errorMessage: error_6.message,
|
|
403
|
+
documentsCount: 0,
|
|
404
|
+
foundResults: [],
|
|
405
|
+
fileSizeInBytes: 0,
|
|
406
|
+
}];
|
|
407
|
+
case 3: return [2 /*return*/];
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
NanoDb.prototype.updateMany = function (filterQuery, updateOptions, upsert) {
|
|
413
|
+
if (upsert === void 0) { upsert = false; }
|
|
414
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
415
|
+
var data, matchingDocuments, _loop_1, _i, matchingDocuments_1, matchingDocument, newDocument, error_7;
|
|
416
|
+
return __generator(this, function (_a) {
|
|
417
|
+
switch (_a.label) {
|
|
418
|
+
case 0:
|
|
419
|
+
_a.trys.push([0, 9, , 10]);
|
|
420
|
+
return [4 /*yield*/, this.dataPromise];
|
|
421
|
+
case 1:
|
|
422
|
+
data = _a.sent();
|
|
423
|
+
matchingDocuments = data.filter(function (document) { return (0, utils_1.matchesFilterQuery)(document, filterQuery); });
|
|
424
|
+
if (!(matchingDocuments.length > 0)) return [3 /*break*/, 6];
|
|
425
|
+
_loop_1 = function (matchingDocument) {
|
|
426
|
+
var indexToUpdate, updatedDocument;
|
|
427
|
+
return __generator(this, function (_b) {
|
|
428
|
+
switch (_b.label) {
|
|
429
|
+
case 0:
|
|
430
|
+
indexToUpdate = data.findIndex(function (doc) { return doc === matchingDocument; });
|
|
431
|
+
return [4 /*yield*/, (0, utils_1.updateOption)(matchingDocument, updateOptions)];
|
|
432
|
+
case 1:
|
|
433
|
+
updatedDocument = _b.sent();
|
|
434
|
+
data[indexToUpdate] = updatedDocument;
|
|
435
|
+
return [2 /*return*/];
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
};
|
|
439
|
+
_i = 0, matchingDocuments_1 = matchingDocuments;
|
|
440
|
+
_a.label = 2;
|
|
441
|
+
case 2:
|
|
442
|
+
if (!(_i < matchingDocuments_1.length)) return [3 /*break*/, 5];
|
|
443
|
+
matchingDocument = matchingDocuments_1[_i];
|
|
444
|
+
return [5 /*yield**/, _loop_1(matchingDocument)];
|
|
445
|
+
case 3:
|
|
446
|
+
_a.sent();
|
|
447
|
+
_a.label = 4;
|
|
448
|
+
case 4:
|
|
449
|
+
_i++;
|
|
450
|
+
return [3 /*break*/, 2];
|
|
451
|
+
case 5:
|
|
452
|
+
this.emit('dataUpdated', {
|
|
453
|
+
filterQuery: filterQuery,
|
|
454
|
+
updateOptions: updateOptions,
|
|
455
|
+
updatedCount: matchingDocuments.length,
|
|
456
|
+
});
|
|
457
|
+
return [3 /*break*/, 7];
|
|
458
|
+
case 6:
|
|
459
|
+
if (upsert) {
|
|
460
|
+
newDocument = __assign(__assign({}, filterQuery), updateOptions);
|
|
461
|
+
data.push(newDocument);
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
return [2 /*return*/, {
|
|
465
|
+
success: false,
|
|
466
|
+
message: 'No documents found for update.',
|
|
467
|
+
documentsCount: data.length,
|
|
468
|
+
fileSizeInBytes: 0,
|
|
469
|
+
}];
|
|
470
|
+
}
|
|
471
|
+
_a.label = 7;
|
|
472
|
+
case 7: return [4 /*yield*/, fs_1.promises.writeFile(this.filePath, JSON.stringify(data, null, 2), 'utf8')];
|
|
473
|
+
case 8:
|
|
474
|
+
_a.sent();
|
|
475
|
+
return [2 /*return*/, {
|
|
476
|
+
success: true,
|
|
477
|
+
message: 'Successfully updated/inserted the specified documents.',
|
|
478
|
+
documentsCount: data.length,
|
|
479
|
+
fileSizeInBytes: 0,
|
|
480
|
+
}];
|
|
481
|
+
case 9:
|
|
482
|
+
error_7 = _a.sent();
|
|
483
|
+
this.emit('dataUpdateError', {
|
|
484
|
+
error: error_7,
|
|
485
|
+
methodName: 'updateMany',
|
|
486
|
+
});
|
|
487
|
+
return [2 /*return*/, {
|
|
488
|
+
success: false,
|
|
489
|
+
message: 'Error updating/inserting documents.',
|
|
490
|
+
errorMessage: error_7.message,
|
|
491
|
+
documentsCount: 0,
|
|
492
|
+
fileSizeInBytes: 0,
|
|
493
|
+
}];
|
|
494
|
+
case 10: return [2 /*return*/];
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
NanoDb.prototype.drop = function () {
|
|
500
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
501
|
+
var error_8;
|
|
502
|
+
return __generator(this, function (_a) {
|
|
503
|
+
switch (_a.label) {
|
|
504
|
+
case 0:
|
|
505
|
+
_a.trys.push([0, 2, , 3]);
|
|
506
|
+
return [4 /*yield*/, fs_1.promises.writeFile(this.filePath, '[]', 'utf8')];
|
|
507
|
+
case 1:
|
|
508
|
+
_a.sent();
|
|
509
|
+
this.dataPromise = this.loadData();
|
|
510
|
+
this.emit('dataDropped', {
|
|
511
|
+
methodName: 'drop',
|
|
512
|
+
});
|
|
513
|
+
return [2 /*return*/, {
|
|
514
|
+
success: true,
|
|
515
|
+
message: 'Data dropped successfully',
|
|
516
|
+
documentsCount: 0,
|
|
517
|
+
fileSizeInBytes: 0,
|
|
518
|
+
}];
|
|
519
|
+
case 2:
|
|
520
|
+
error_8 = _a.sent();
|
|
521
|
+
this.emit('dataUpdateError', {
|
|
522
|
+
error: error_8,
|
|
523
|
+
methodName: 'drop',
|
|
524
|
+
});
|
|
525
|
+
return [2 /*return*/, {
|
|
526
|
+
success: false,
|
|
527
|
+
message: 'Error dropping data',
|
|
528
|
+
errorMessage: error_8.message,
|
|
529
|
+
documentsCount: 0,
|
|
530
|
+
fileSizeInBytes: 0,
|
|
531
|
+
}];
|
|
532
|
+
case 3: return [2 /*return*/];
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
};
|
|
537
|
+
NanoDb.prototype.countDoc = function () {
|
|
538
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
539
|
+
var data, documentCount, error_9;
|
|
540
|
+
return __generator(this, function (_a) {
|
|
541
|
+
switch (_a.label) {
|
|
542
|
+
case 0:
|
|
543
|
+
_a.trys.push([0, 2, , 3]);
|
|
544
|
+
return [4 /*yield*/, this.dataPromise];
|
|
545
|
+
case 1:
|
|
546
|
+
data = _a.sent();
|
|
547
|
+
documentCount = data.length;
|
|
548
|
+
return [2 /*return*/, {
|
|
549
|
+
success: true,
|
|
550
|
+
message: 'Document count retrieved successfully.',
|
|
551
|
+
documentsCount: documentCount,
|
|
552
|
+
fileSizeInBytes: 0,
|
|
553
|
+
}];
|
|
554
|
+
case 2:
|
|
555
|
+
error_9 = _a.sent();
|
|
556
|
+
return [2 /*return*/, {
|
|
557
|
+
success: false,
|
|
558
|
+
message: 'Error counting documents.',
|
|
559
|
+
errorMessage: error_9.message,
|
|
560
|
+
documentsCount: 0,
|
|
561
|
+
fileSizeInBytes: 0,
|
|
562
|
+
}];
|
|
563
|
+
case 3: return [2 /*return*/];
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
NanoDb.prototype.dataSize = function () {
|
|
569
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
570
|
+
var fileStats, fileSizeInBytes, error_10;
|
|
571
|
+
return __generator(this, function (_a) {
|
|
572
|
+
switch (_a.label) {
|
|
573
|
+
case 0:
|
|
574
|
+
_a.trys.push([0, 2, , 3]);
|
|
575
|
+
return [4 /*yield*/, fs_1.promises.stat(this.filePath)];
|
|
576
|
+
case 1:
|
|
577
|
+
fileStats = _a.sent();
|
|
578
|
+
fileSizeInBytes = fileStats.size;
|
|
579
|
+
return [2 /*return*/, {
|
|
580
|
+
success: true,
|
|
581
|
+
message: 'Data file size retrieved successfully.',
|
|
582
|
+
fileSizeInBytes: fileSizeInBytes,
|
|
583
|
+
documentsCount: 0,
|
|
584
|
+
}];
|
|
585
|
+
case 2:
|
|
586
|
+
error_10 = _a.sent();
|
|
587
|
+
return [2 /*return*/, {
|
|
588
|
+
success: false,
|
|
589
|
+
message: 'Error retrieving data file size.',
|
|
590
|
+
errorMessage: error_10.message,
|
|
591
|
+
fileSizeInBytes: 0,
|
|
592
|
+
documentsCount: 0,
|
|
593
|
+
}];
|
|
594
|
+
case 3: return [2 /*return*/];
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
};
|
|
599
|
+
NanoDb.prototype.transferData = function (destinationFilePath) {
|
|
600
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
601
|
+
var sourceData, destinationExists, destinationData, _a, _b, mergedData, error_11;
|
|
602
|
+
return __generator(this, function (_c) {
|
|
603
|
+
switch (_c.label) {
|
|
604
|
+
case 0:
|
|
605
|
+
_c.trys.push([0, 8, , 9]);
|
|
606
|
+
return [4 /*yield*/, this.dataPromise];
|
|
607
|
+
case 1:
|
|
608
|
+
sourceData = _c.sent();
|
|
609
|
+
return [4 /*yield*/, this.fileExists(destinationFilePath)];
|
|
610
|
+
case 2:
|
|
611
|
+
destinationExists = _c.sent();
|
|
612
|
+
destinationData = [];
|
|
613
|
+
if (!destinationExists) return [3 /*break*/, 4];
|
|
614
|
+
_b = (_a = JSON).parse;
|
|
615
|
+
return [4 /*yield*/, fs_1.promises.readFile(destinationFilePath, 'utf8')];
|
|
616
|
+
case 3:
|
|
617
|
+
destinationData = _b.apply(_a, [_c.sent()]);
|
|
618
|
+
return [3 /*break*/, 6];
|
|
619
|
+
case 4: return [4 /*yield*/, fs_1.promises.writeFile(destinationFilePath, '[]', 'utf8')];
|
|
620
|
+
case 5:
|
|
621
|
+
_c.sent();
|
|
622
|
+
_c.label = 6;
|
|
623
|
+
case 6:
|
|
624
|
+
mergedData = __spreadArray(__spreadArray([], destinationData, true), sourceData, true);
|
|
625
|
+
return [4 /*yield*/, fs_1.promises.writeFile(destinationFilePath, JSON.stringify(mergedData, null, 2), 'utf8')];
|
|
626
|
+
case 7:
|
|
627
|
+
_c.sent();
|
|
628
|
+
this.emit('dataTransferred', {
|
|
629
|
+
success: true,
|
|
630
|
+
message: 'Data transferred and added to existing data successfully.',
|
|
631
|
+
documentsCount: mergedData.length,
|
|
632
|
+
destinationFilePath: destinationFilePath,
|
|
633
|
+
fileSizeInBytes: 0,
|
|
634
|
+
});
|
|
635
|
+
return [2 /*return*/, {
|
|
636
|
+
success: true,
|
|
637
|
+
message: 'Data transferred and added to existing data successfully.',
|
|
638
|
+
documentsCount: mergedData.length,
|
|
639
|
+
destinationFilePath: destinationFilePath,
|
|
640
|
+
fileSizeInBytes: 0,
|
|
641
|
+
}];
|
|
642
|
+
case 8:
|
|
643
|
+
error_11 = _c.sent();
|
|
644
|
+
this.emit('transferError', {
|
|
645
|
+
success: false,
|
|
646
|
+
message: 'Error transferring and adding data.',
|
|
647
|
+
errorMessage: error_11.message,
|
|
648
|
+
documentsCount: 0,
|
|
649
|
+
fileSizeInBytes: 0,
|
|
650
|
+
});
|
|
651
|
+
return [2 /*return*/, {
|
|
652
|
+
success: false,
|
|
653
|
+
message: 'Error transferring and adding data.',
|
|
654
|
+
errorMessage: error_11.message,
|
|
655
|
+
documentsCount: 0,
|
|
656
|
+
fileSizeInBytes: 0,
|
|
657
|
+
}];
|
|
658
|
+
case 9: return [2 /*return*/];
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
};
|
|
663
|
+
NanoDb.prototype.fileExists = function (filePath) {
|
|
664
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
665
|
+
var _a;
|
|
666
|
+
return __generator(this, function (_b) {
|
|
667
|
+
switch (_b.label) {
|
|
668
|
+
case 0:
|
|
669
|
+
_b.trys.push([0, 2, , 3]);
|
|
670
|
+
return [4 /*yield*/, fs_1.promises.access(filePath)];
|
|
671
|
+
case 1:
|
|
672
|
+
_b.sent();
|
|
673
|
+
return [2 /*return*/, true];
|
|
674
|
+
case 2:
|
|
675
|
+
_a = _b.sent();
|
|
676
|
+
return [2 /*return*/, false];
|
|
677
|
+
case 3: return [2 /*return*/];
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
});
|
|
681
|
+
};
|
|
682
|
+
NanoDb.prototype.distinct = function (field) {
|
|
683
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
684
|
+
var data, distinctValues, error_12;
|
|
685
|
+
return __generator(this, function (_a) {
|
|
686
|
+
switch (_a.label) {
|
|
687
|
+
case 0:
|
|
688
|
+
_a.trys.push([0, 2, , 3]);
|
|
689
|
+
return [4 /*yield*/, this.dataPromise];
|
|
690
|
+
case 1:
|
|
691
|
+
data = _a.sent();
|
|
692
|
+
distinctValues = __spreadArray([], new Set(data.map(function (document) { return document[field]; })), true);
|
|
693
|
+
return [2 /*return*/, {
|
|
694
|
+
success: true,
|
|
695
|
+
message: 'Distinct values retrieved successfully.',
|
|
696
|
+
documentsCount: distinctValues.length,
|
|
697
|
+
distinctValues: distinctValues,
|
|
698
|
+
fileSizeInBytes: 0,
|
|
699
|
+
}];
|
|
700
|
+
case 2:
|
|
701
|
+
error_12 = _a.sent();
|
|
702
|
+
return [2 /*return*/, {
|
|
703
|
+
success: false,
|
|
704
|
+
message: 'Error retrieving distinct values.',
|
|
705
|
+
errorMessage: error_12.message,
|
|
706
|
+
documentsCount: 0,
|
|
707
|
+
distinctValues: [],
|
|
708
|
+
fileSizeInBytes: 0,
|
|
709
|
+
}];
|
|
710
|
+
case 3: return [2 /*return*/];
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
});
|
|
714
|
+
};
|
|
715
|
+
NanoDb.prototype.indexData = function (data) {
|
|
716
|
+
for (var field in data) {
|
|
717
|
+
if (!this.indexes.has(field)) {
|
|
718
|
+
this.indexes.set(field, new Map());
|
|
719
|
+
}
|
|
720
|
+
var fieldValue = data[field];
|
|
721
|
+
if (!this.indexes.get(field).has(fieldValue)) {
|
|
722
|
+
this.indexes.get(field).set(fieldValue, []);
|
|
723
|
+
}
|
|
724
|
+
this.indexes.get(field).get(fieldValue).push(data);
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
NanoDb.prototype.backup = function (filepath, format) {
|
|
728
|
+
if (format === void 0) { format = 'txt'; }
|
|
729
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
730
|
+
var data, serializedData, _a, zipBackupResult, gzipBackupResult, transferDataResult, error_13;
|
|
731
|
+
return __generator(this, function (_b) {
|
|
732
|
+
switch (_b.label) {
|
|
733
|
+
case 0:
|
|
734
|
+
_b.trys.push([0, 12, , 13]);
|
|
735
|
+
return [4 /*yield*/, this.dataPromise];
|
|
736
|
+
case 1:
|
|
737
|
+
data = _b.sent();
|
|
738
|
+
serializedData = void 0;
|
|
739
|
+
_a = format;
|
|
740
|
+
switch (_a) {
|
|
741
|
+
case 'txt': return [3 /*break*/, 2];
|
|
742
|
+
case 'zip': return [3 /*break*/, 3];
|
|
743
|
+
case 'gzip': return [3 /*break*/, 5];
|
|
744
|
+
case 'json': return [3 /*break*/, 7];
|
|
745
|
+
}
|
|
746
|
+
return [3 /*break*/, 9];
|
|
747
|
+
case 2:
|
|
748
|
+
serializedData = JSON.stringify(data, null, 2);
|
|
749
|
+
return [3 /*break*/, 10];
|
|
750
|
+
case 3: return [4 /*yield*/, this.createCompressedBackup(filepath, data, 'zip')];
|
|
751
|
+
case 4:
|
|
752
|
+
zipBackupResult = _b.sent();
|
|
753
|
+
this.emit('backupCreated', zipBackupResult);
|
|
754
|
+
return [2 /*return*/, zipBackupResult];
|
|
755
|
+
case 5: return [4 /*yield*/, this.createCompressedBackup(filepath, data, 'gzip')];
|
|
756
|
+
case 6:
|
|
757
|
+
gzipBackupResult = _b.sent();
|
|
758
|
+
this.emit('backupCreated', gzipBackupResult);
|
|
759
|
+
return [2 /*return*/, gzipBackupResult];
|
|
760
|
+
case 7: return [4 /*yield*/, this.transferData(filepath)];
|
|
761
|
+
case 8:
|
|
762
|
+
transferDataResult = _b.sent();
|
|
763
|
+
this.emit('backupCreated', transferDataResult);
|
|
764
|
+
return [2 /*return*/, transferDataResult];
|
|
765
|
+
case 9: return [2 /*return*/, {
|
|
766
|
+
success: false,
|
|
767
|
+
message: 'Unsupported backup format.',
|
|
768
|
+
documentsCount: 0,
|
|
769
|
+
fileSizeInBytes: 0,
|
|
770
|
+
}];
|
|
771
|
+
case 10: return [4 /*yield*/, fs_1.promises.writeFile(filepath, serializedData, 'utf8')];
|
|
772
|
+
case 11:
|
|
773
|
+
_b.sent();
|
|
774
|
+
this.emit('backupCreated', {
|
|
775
|
+
success: true,
|
|
776
|
+
message: "Backup created successfully in ".concat(format, " format."),
|
|
777
|
+
documentsCount: data.length,
|
|
778
|
+
fileSizeInBytes: Buffer.from(serializedData).length,
|
|
779
|
+
});
|
|
780
|
+
return [2 /*return*/, {
|
|
781
|
+
success: true,
|
|
782
|
+
message: "Backup created successfully in ".concat(format, " format."),
|
|
783
|
+
documentsCount: data.length,
|
|
784
|
+
fileSizeInBytes: Buffer.from(serializedData).length,
|
|
785
|
+
}];
|
|
786
|
+
case 12:
|
|
787
|
+
error_13 = _b.sent();
|
|
788
|
+
this.emit('backupError', {
|
|
789
|
+
success: false,
|
|
790
|
+
message: 'Error creating backup.',
|
|
791
|
+
errorMessage: error_13.message,
|
|
792
|
+
documentsCount: 0,
|
|
793
|
+
fileSizeInBytes: 0,
|
|
794
|
+
});
|
|
795
|
+
return [2 /*return*/, {
|
|
796
|
+
success: false,
|
|
797
|
+
message: 'Error creating backup.',
|
|
798
|
+
errorMessage: error_13.message,
|
|
799
|
+
documentsCount: 0,
|
|
800
|
+
fileSizeInBytes: 0,
|
|
801
|
+
}];
|
|
802
|
+
case 13: return [2 /*return*/];
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
});
|
|
806
|
+
};
|
|
807
|
+
NanoDb.prototype.createCompressedBackup = function (filepath, data, compression) {
|
|
808
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
809
|
+
var compressedData, _a, error_14;
|
|
810
|
+
return __generator(this, function (_b) {
|
|
811
|
+
switch (_b.label) {
|
|
812
|
+
case 0:
|
|
813
|
+
_b.trys.push([0, 8, , 9]);
|
|
814
|
+
compressedData = void 0;
|
|
815
|
+
_a = compression;
|
|
816
|
+
switch (_a) {
|
|
817
|
+
case 'zip': return [3 /*break*/, 1];
|
|
818
|
+
case 'gzip': return [3 /*break*/, 3];
|
|
819
|
+
}
|
|
820
|
+
return [3 /*break*/, 5];
|
|
821
|
+
case 1: return [4 /*yield*/, this.zipData(data)];
|
|
822
|
+
case 2:
|
|
823
|
+
compressedData = _b.sent();
|
|
824
|
+
return [3 /*break*/, 6];
|
|
825
|
+
case 3: return [4 /*yield*/, this.gzipData(data)];
|
|
826
|
+
case 4:
|
|
827
|
+
compressedData = _b.sent();
|
|
828
|
+
return [3 /*break*/, 6];
|
|
829
|
+
case 5: return [2 /*return*/, {
|
|
830
|
+
success: false,
|
|
831
|
+
message: 'Unsupported compression format.',
|
|
832
|
+
documentsCount: 0,
|
|
833
|
+
fileSizeInBytes: 0,
|
|
834
|
+
}];
|
|
835
|
+
case 6: return [4 /*yield*/, fs_1.promises.writeFile(filepath, compressedData)];
|
|
836
|
+
case 7:
|
|
837
|
+
_b.sent();
|
|
838
|
+
return [2 /*return*/, {
|
|
839
|
+
success: true,
|
|
840
|
+
message: "Backup created successfully in ".concat(compression, " format."),
|
|
841
|
+
documentsCount: data.length,
|
|
842
|
+
fileSizeInBytes: compressedData.length,
|
|
843
|
+
}];
|
|
844
|
+
case 8:
|
|
845
|
+
error_14 = _b.sent();
|
|
846
|
+
return [2 /*return*/, {
|
|
847
|
+
success: false,
|
|
848
|
+
message: 'Error creating compressed backup.',
|
|
849
|
+
errorMessage: error_14.message,
|
|
850
|
+
documentsCount: 0,
|
|
851
|
+
fileSizeInBytes: 0,
|
|
852
|
+
}];
|
|
853
|
+
case 9: return [2 /*return*/];
|
|
854
|
+
}
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
};
|
|
858
|
+
NanoDb.prototype.zipData = function (data) {
|
|
859
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
860
|
+
var serializedData;
|
|
861
|
+
return __generator(this, function (_a) {
|
|
862
|
+
serializedData = JSON.stringify(data, null, 2);
|
|
863
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
864
|
+
zlib.gzip(serializedData, function (err, buffer) {
|
|
865
|
+
if (err) {
|
|
866
|
+
reject(err);
|
|
867
|
+
}
|
|
868
|
+
else {
|
|
869
|
+
resolve(buffer);
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
})];
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
};
|
|
876
|
+
NanoDb.prototype.gzipData = function (data) {
|
|
877
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
878
|
+
var serializedData;
|
|
879
|
+
return __generator(this, function (_a) {
|
|
880
|
+
serializedData = JSON.stringify(data, null, 2);
|
|
881
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
882
|
+
zlib.gzip(serializedData, function (err, buffer) {
|
|
883
|
+
if (err) {
|
|
884
|
+
reject(err);
|
|
885
|
+
}
|
|
886
|
+
else {
|
|
887
|
+
resolve(buffer);
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
})];
|
|
891
|
+
});
|
|
892
|
+
});
|
|
893
|
+
};
|
|
894
|
+
NanoDb.prototype.loadEncryptedData = function () {
|
|
895
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
896
|
+
var encryptedData, decryptedData, err_2;
|
|
897
|
+
return __generator(this, function (_a) {
|
|
898
|
+
switch (_a.label) {
|
|
899
|
+
case 0:
|
|
900
|
+
_a.trys.push([0, 2, , 3]);
|
|
901
|
+
return [4 /*yield*/, fs_1.promises.readFile(this.filePath, 'utf8')];
|
|
902
|
+
case 1:
|
|
903
|
+
encryptedData = _a.sent();
|
|
904
|
+
decryptedData = this.decrypt(encryptedData);
|
|
905
|
+
return [2 /*return*/, JSON.parse(decryptedData)];
|
|
906
|
+
case 2:
|
|
907
|
+
err_2 = _a.sent();
|
|
908
|
+
return [2 /*return*/, []];
|
|
909
|
+
case 3: return [2 /*return*/];
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
});
|
|
913
|
+
};
|
|
914
|
+
NanoDb.prototype.encrypt = function (data) {
|
|
915
|
+
if (!this.encryptionKey) {
|
|
916
|
+
throw new Error('Encryption key is required for encryption.');
|
|
917
|
+
}
|
|
918
|
+
var cipher = crypto.createCipher('aes-256-cbc', Buffer.from(this.encryptionKey, 'utf8'));
|
|
919
|
+
var encrypted = cipher.update(data, 'utf8', 'hex');
|
|
920
|
+
encrypted += cipher.final('hex');
|
|
921
|
+
return encrypted;
|
|
922
|
+
};
|
|
923
|
+
NanoDb.prototype.decrypt = function (encryptedData) {
|
|
924
|
+
if (!this.encryptionKey) {
|
|
925
|
+
throw new Error('Encryption key is required for decryption.');
|
|
926
|
+
}
|
|
927
|
+
var decipher = crypto.createDecipher('aes-256-cbc', Buffer.from(this.encryptionKey, 'utf8'));
|
|
928
|
+
var decrypted = decipher.update(encryptedData, 'hex', 'utf8');
|
|
929
|
+
decrypted += decipher.final('utf8');
|
|
930
|
+
return decrypted;
|
|
931
|
+
};
|
|
932
|
+
NanoDb.prototype.nanoSize = function () {
|
|
933
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
934
|
+
function processDirectory(directoryPath) {
|
|
935
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
936
|
+
var files, _i, files_1, file, filePath, stats, error_16;
|
|
937
|
+
return __generator(this, function (_a) {
|
|
938
|
+
switch (_a.label) {
|
|
939
|
+
case 0:
|
|
940
|
+
_a.trys.push([0, 9, , 10]);
|
|
941
|
+
return [4 /*yield*/, fs.readdir(directoryPath)];
|
|
942
|
+
case 1:
|
|
943
|
+
files = _a.sent();
|
|
944
|
+
_i = 0, files_1 = files;
|
|
945
|
+
_a.label = 2;
|
|
946
|
+
case 2:
|
|
947
|
+
if (!(_i < files_1.length)) return [3 /*break*/, 8];
|
|
948
|
+
file = files_1[_i];
|
|
949
|
+
filePath = path.join(directoryPath, file);
|
|
950
|
+
return [4 /*yield*/, fs.stat(filePath)];
|
|
951
|
+
case 3:
|
|
952
|
+
stats = _a.sent();
|
|
953
|
+
if (!stats.isDirectory()) return [3 /*break*/, 6];
|
|
954
|
+
if (!!filePath.includes('node_modules')) return [3 /*break*/, 5];
|
|
955
|
+
return [4 /*yield*/, processDirectory(filePath)];
|
|
956
|
+
case 4:
|
|
957
|
+
_a.sent();
|
|
958
|
+
_a.label = 5;
|
|
959
|
+
case 5: return [3 /*break*/, 7];
|
|
960
|
+
case 6:
|
|
961
|
+
if (path.extname(filePath).toLowerCase() === '.json') {
|
|
962
|
+
totalSize_1 += stats.size;
|
|
963
|
+
}
|
|
964
|
+
_a.label = 7;
|
|
965
|
+
case 7:
|
|
966
|
+
_i++;
|
|
967
|
+
return [3 /*break*/, 2];
|
|
968
|
+
case 8: return [3 /*break*/, 10];
|
|
969
|
+
case 9:
|
|
970
|
+
error_16 = _a.sent();
|
|
971
|
+
console.error('Error reading directory:', error_16.message);
|
|
972
|
+
return [3 /*break*/, 10];
|
|
973
|
+
case 10: return [2 /*return*/];
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
var projectRoot, totalSize_1, error_15;
|
|
979
|
+
return __generator(this, function (_a) {
|
|
980
|
+
switch (_a.label) {
|
|
981
|
+
case 0:
|
|
982
|
+
projectRoot = process.cwd();
|
|
983
|
+
_a.label = 1;
|
|
984
|
+
case 1:
|
|
985
|
+
_a.trys.push([1, 3, , 4]);
|
|
986
|
+
totalSize_1 = 0;
|
|
987
|
+
return [4 /*yield*/, processDirectory(projectRoot)];
|
|
988
|
+
case 2:
|
|
989
|
+
_a.sent();
|
|
990
|
+
return [2 /*return*/, totalSize_1];
|
|
991
|
+
case 3:
|
|
992
|
+
error_15 = _a.sent();
|
|
993
|
+
console.error('Error calculating total size:', error_15.message);
|
|
994
|
+
return [2 /*return*/, 0];
|
|
995
|
+
case 4: return [2 /*return*/];
|
|
996
|
+
}
|
|
997
|
+
});
|
|
998
|
+
});
|
|
999
|
+
};
|
|
1000
|
+
NanoDb.prototype.replicate = function (fileToReplicate, replicatePath) {
|
|
1001
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1002
|
+
var sourceData, fileName, destinationFilePath, error_17;
|
|
1003
|
+
return __generator(this, function (_a) {
|
|
1004
|
+
switch (_a.label) {
|
|
1005
|
+
case 0:
|
|
1006
|
+
_a.trys.push([0, 3, , 4]);
|
|
1007
|
+
return [4 /*yield*/, fs_1.promises.readFile(fileToReplicate, 'utf8')];
|
|
1008
|
+
case 1:
|
|
1009
|
+
sourceData = _a.sent();
|
|
1010
|
+
fileName = path.basename(fileToReplicate);
|
|
1011
|
+
destinationFilePath = path.join(replicatePath, fileName);
|
|
1012
|
+
return [4 /*yield*/, fs_1.promises.writeFile(destinationFilePath, sourceData, 'utf8')];
|
|
1013
|
+
case 2:
|
|
1014
|
+
_a.sent();
|
|
1015
|
+
this.emit('dataReplicated', {
|
|
1016
|
+
success: true,
|
|
1017
|
+
message: 'Data replicated successfully.',
|
|
1018
|
+
documentsCount: 'Full',
|
|
1019
|
+
destinationFilePath: destinationFilePath,
|
|
1020
|
+
fileSizeInBytes: Buffer.from(sourceData).length,
|
|
1021
|
+
});
|
|
1022
|
+
return [2 /*return*/, {
|
|
1023
|
+
success: true,
|
|
1024
|
+
message: 'Data replicated successfully.',
|
|
1025
|
+
documentsCount: 'Full',
|
|
1026
|
+
destinationFilePath: destinationFilePath,
|
|
1027
|
+
fileSizeInBytes: Buffer.from(sourceData).length,
|
|
1028
|
+
}];
|
|
1029
|
+
case 3:
|
|
1030
|
+
error_17 = _a.sent();
|
|
1031
|
+
this.emit('replicationError', {
|
|
1032
|
+
success: false,
|
|
1033
|
+
message: 'Error replicating data.',
|
|
1034
|
+
errorMessage: error_17.message,
|
|
1035
|
+
documentsCount: 0,
|
|
1036
|
+
fileSizeInBytes: 0,
|
|
1037
|
+
});
|
|
1038
|
+
return [2 /*return*/, {
|
|
1039
|
+
success: false,
|
|
1040
|
+
message: 'Error replicating data.',
|
|
1041
|
+
errorMessage: error_17.message,
|
|
1042
|
+
documentsCount: 0,
|
|
1043
|
+
fileSizeInBytes: 0,
|
|
1044
|
+
}];
|
|
1045
|
+
case 4: return [2 /*return*/];
|
|
1046
|
+
}
|
|
1047
|
+
});
|
|
1048
|
+
});
|
|
1049
|
+
};
|
|
1050
|
+
NanoDb.prototype.aggregate = function (pipeline) {
|
|
1051
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1052
|
+
var data_2, result, error_18;
|
|
1053
|
+
return __generator(this, function (_a) {
|
|
1054
|
+
switch (_a.label) {
|
|
1055
|
+
case 0:
|
|
1056
|
+
_a.trys.push([0, 2, , 3]);
|
|
1057
|
+
return [4 /*yield*/, this.dataPromise];
|
|
1058
|
+
case 1:
|
|
1059
|
+
data_2 = _a.sent();
|
|
1060
|
+
result = pipeline.reduce(function (acc, stage) {
|
|
1061
|
+
if (stage.$match) {
|
|
1062
|
+
acc = acc.filter(function (document) { return (0, utils_1.matchesFilterQuery)(document, stage.$match); });
|
|
1063
|
+
}
|
|
1064
|
+
else if (stage.$group) {
|
|
1065
|
+
var groupedData_1 = new Map();
|
|
1066
|
+
acc.forEach(function (document) {
|
|
1067
|
+
var key = stage.$group._id || 'defaultGroup';
|
|
1068
|
+
var keyValue = String(document[key]);
|
|
1069
|
+
if (!groupedData_1.has(keyValue)) {
|
|
1070
|
+
groupedData_1.set(keyValue, []);
|
|
1071
|
+
}
|
|
1072
|
+
groupedData_1.get(keyValue).push(document);
|
|
1073
|
+
});
|
|
1074
|
+
acc = Array.from(groupedData_1, function (_a) {
|
|
1075
|
+
var _b;
|
|
1076
|
+
var key = _a[0], value = _a[1];
|
|
1077
|
+
return (_b = {}, _b[key] = value, _b);
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
else if (stage.$project) {
|
|
1081
|
+
acc = acc.map(function (document) {
|
|
1082
|
+
var projectedDocument = {};
|
|
1083
|
+
for (var field in stage.$project) {
|
|
1084
|
+
var fieldPath = stage.$project[field];
|
|
1085
|
+
var fieldValue = fieldPath.split('.').reduce(function (obj, key) { return obj[key]; }, document);
|
|
1086
|
+
projectedDocument[field] = fieldValue;
|
|
1087
|
+
}
|
|
1088
|
+
return projectedDocument;
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
else if (stage.$sort) {
|
|
1092
|
+
var sortBy_1 = stage.$sort.field;
|
|
1093
|
+
var sortOrder_2 = stage.$sort.order === 'desc' ? -1 : 1;
|
|
1094
|
+
acc.sort(function (a, b) { return (a[sortBy_1] > b[sortBy_1] ? sortOrder_2 : -sortOrder_2); });
|
|
1095
|
+
}
|
|
1096
|
+
else if (stage.$skip) {
|
|
1097
|
+
var skipCount = stage.$skip;
|
|
1098
|
+
acc = acc.slice(skipCount);
|
|
1099
|
+
}
|
|
1100
|
+
else if (stage.$limit) {
|
|
1101
|
+
var limitCount = stage.$limit;
|
|
1102
|
+
acc = acc.slice(0, limitCount);
|
|
1103
|
+
}
|
|
1104
|
+
else if (stage.$unwind) {
|
|
1105
|
+
var unwindField_1 = stage.$unwind;
|
|
1106
|
+
acc = acc.flatMap(function (document) {
|
|
1107
|
+
var unwindArray = document[unwindField_1] || [];
|
|
1108
|
+
return unwindArray.map(function (arrayElement) {
|
|
1109
|
+
var _a;
|
|
1110
|
+
return (__assign(__assign({}, document), (_a = {}, _a[unwindField_1] = arrayElement, _a)));
|
|
1111
|
+
});
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
else if (stage.$lookup) {
|
|
1115
|
+
var lookupField = stage.$lookup.from;
|
|
1116
|
+
var localField_1 = stage.$lookup.localField;
|
|
1117
|
+
var foreignField_1 = stage.$lookup.foreignField;
|
|
1118
|
+
var asField_1 = stage.$lookup.as;
|
|
1119
|
+
acc = acc.map(function (document) {
|
|
1120
|
+
var _a;
|
|
1121
|
+
var lookupValue = document[localField_1];
|
|
1122
|
+
var matchingDocuments = data_2.filter(function (lookupDoc) { return lookupDoc[foreignField_1] === lookupValue; });
|
|
1123
|
+
return __assign(__assign({}, document), (_a = {}, _a[asField_1] = matchingDocuments, _a));
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
else if (stage.$addFields) {
|
|
1127
|
+
var newFields_1 = stage.$addFields;
|
|
1128
|
+
acc = acc.map(function (document) {
|
|
1129
|
+
for (var field in newFields_1) {
|
|
1130
|
+
document[field] = newFields_1[field];
|
|
1131
|
+
}
|
|
1132
|
+
return document;
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
else if (stage.$replaceRoot) {
|
|
1136
|
+
var newRootField_1 = stage.$replaceRoot.newRoot;
|
|
1137
|
+
acc = acc.map(function (document) { return document[newRootField_1]; });
|
|
1138
|
+
}
|
|
1139
|
+
else if (stage.$count) {
|
|
1140
|
+
acc = [{ count: acc.length }];
|
|
1141
|
+
}
|
|
1142
|
+
return acc;
|
|
1143
|
+
}, data_2);
|
|
1144
|
+
return [2 /*return*/, {
|
|
1145
|
+
success: true,
|
|
1146
|
+
message: 'Aggregation completed successfully.',
|
|
1147
|
+
documentsCount: result.length,
|
|
1148
|
+
aggregatedResults: result,
|
|
1149
|
+
}];
|
|
1150
|
+
case 2:
|
|
1151
|
+
error_18 = _a.sent();
|
|
1152
|
+
return [2 /*return*/, {
|
|
1153
|
+
success: false,
|
|
1154
|
+
message: 'Error performing aggregation.',
|
|
1155
|
+
errorMessage: error_18.message,
|
|
1156
|
+
documentsCount: 0,
|
|
1157
|
+
aggregatedResults: [],
|
|
1158
|
+
}];
|
|
1159
|
+
case 3: return [2 /*return*/];
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
});
|
|
1163
|
+
};
|
|
1164
|
+
NanoDb.prototype.fullTextSearch = function (query) {
|
|
1165
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1166
|
+
var data, foundResults, error_19;
|
|
1167
|
+
return __generator(this, function (_a) {
|
|
1168
|
+
switch (_a.label) {
|
|
1169
|
+
case 0:
|
|
1170
|
+
_a.trys.push([0, 2, , 3]);
|
|
1171
|
+
return [4 /*yield*/, this.dataPromise];
|
|
1172
|
+
case 1:
|
|
1173
|
+
data = _a.sent();
|
|
1174
|
+
foundResults = data.filter(function (document) {
|
|
1175
|
+
for (var key in document) {
|
|
1176
|
+
var fieldValue = String(document[key]);
|
|
1177
|
+
if (fieldValue.toLowerCase().includes(query.toLowerCase())) {
|
|
1178
|
+
return true;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return false;
|
|
1182
|
+
});
|
|
1183
|
+
return [2 /*return*/, {
|
|
1184
|
+
success: true,
|
|
1185
|
+
message: 'Full-text search completed successfully.',
|
|
1186
|
+
documentsCount: foundResults.length,
|
|
1187
|
+
foundResults: foundResults,
|
|
1188
|
+
}];
|
|
1189
|
+
case 2:
|
|
1190
|
+
error_19 = _a.sent();
|
|
1191
|
+
return [2 /*return*/, {
|
|
1192
|
+
success: false,
|
|
1193
|
+
message: 'Error performing full-text search.',
|
|
1194
|
+
documentsCount: 0,
|
|
1195
|
+
foundResults: [],
|
|
1196
|
+
}];
|
|
1197
|
+
case 3: return [2 /*return*/];
|
|
1198
|
+
}
|
|
1199
|
+
});
|
|
1200
|
+
});
|
|
1201
|
+
};
|
|
1202
|
+
NanoDb.prototype.takeSnapshot = function (snapshotPathWithFileName) {
|
|
1203
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1204
|
+
var data, snapshotPathComponents, snapshotFileName, snapshotPath, snapshotFilePath, error_20;
|
|
1205
|
+
return __generator(this, function (_a) {
|
|
1206
|
+
switch (_a.label) {
|
|
1207
|
+
case 0:
|
|
1208
|
+
_a.trys.push([0, 3, , 4]);
|
|
1209
|
+
return [4 /*yield*/, this.dataPromise];
|
|
1210
|
+
case 1:
|
|
1211
|
+
data = _a.sent();
|
|
1212
|
+
snapshotPathComponents = snapshotPathWithFileName.split('/');
|
|
1213
|
+
snapshotFileName = snapshotPathComponents.pop() || "snapshot-".concat(Date.now(), ".json");
|
|
1214
|
+
snapshotPath = snapshotPathComponents.join('/');
|
|
1215
|
+
snapshotFilePath = "".concat(snapshotPath, "/").concat(snapshotFileName);
|
|
1216
|
+
return [4 /*yield*/, fs_1.promises.writeFile(snapshotFilePath, JSON.stringify(data, null, 2), 'utf8')];
|
|
1217
|
+
case 2:
|
|
1218
|
+
_a.sent();
|
|
1219
|
+
this.emit('snapshotTaken', {
|
|
1220
|
+
success: true,
|
|
1221
|
+
message: 'Snapshot taken successfully.',
|
|
1222
|
+
documentsCount: data.length,
|
|
1223
|
+
snapshotFilePath: snapshotFilePath,
|
|
1224
|
+
fileSizeInBytes: Buffer.from(JSON.stringify(data, null, 2)).length,
|
|
1225
|
+
});
|
|
1226
|
+
return [2 /*return*/, {
|
|
1227
|
+
success: true,
|
|
1228
|
+
message: 'Snapshot taken successfully.',
|
|
1229
|
+
documentsCount: data.length,
|
|
1230
|
+
snapshotFilePath: snapshotFilePath,
|
|
1231
|
+
fileSizeInBytes: Buffer.from(JSON.stringify(data, null, 2)).length,
|
|
1232
|
+
}];
|
|
1233
|
+
case 3:
|
|
1234
|
+
error_20 = _a.sent();
|
|
1235
|
+
this.emit('snapshotError', {
|
|
1236
|
+
success: false,
|
|
1237
|
+
message: 'Error taking snapshot.',
|
|
1238
|
+
errorMessage: error_20.message,
|
|
1239
|
+
documentsCount: 0,
|
|
1240
|
+
fileSizeInBytes: 0,
|
|
1241
|
+
});
|
|
1242
|
+
return [2 /*return*/, {
|
|
1243
|
+
success: false,
|
|
1244
|
+
message: 'Error taking snapshot.',
|
|
1245
|
+
errorMessage: error_20.message,
|
|
1246
|
+
documentsCount: 0,
|
|
1247
|
+
fileSizeInBytes: 0,
|
|
1248
|
+
}];
|
|
1249
|
+
case 4: return [2 /*return*/];
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
1252
|
+
});
|
|
1253
|
+
};
|
|
1254
|
+
NanoDb.prototype.restoreSnapshot = function (snapshotIndex, filePath) {
|
|
1255
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1256
|
+
var directoryPath, fileName, snapshotFiles, snapshotRegex_1, matchingSnapshots, snapshotFilePath, snapshotData, error_21;
|
|
1257
|
+
return __generator(this, function (_a) {
|
|
1258
|
+
switch (_a.label) {
|
|
1259
|
+
case 0:
|
|
1260
|
+
_a.trys.push([0, 4, , 5]);
|
|
1261
|
+
directoryPath = path.dirname(filePath);
|
|
1262
|
+
fileName = path.basename(filePath);
|
|
1263
|
+
return [4 /*yield*/, fs_1.promises.readdir(directoryPath)];
|
|
1264
|
+
case 1:
|
|
1265
|
+
snapshotFiles = _a.sent();
|
|
1266
|
+
snapshotRegex_1 = new RegExp("^".concat(fileName, "\\.snapshot-\\d+\\.json$"));
|
|
1267
|
+
matchingSnapshots = snapshotFiles.filter(function (file) { return snapshotRegex_1.test(file); });
|
|
1268
|
+
if (matchingSnapshots.length === 0) {
|
|
1269
|
+
return [2 /*return*/, {
|
|
1270
|
+
success: false,
|
|
1271
|
+
message: 'No snapshots found for restoration.',
|
|
1272
|
+
documentsCount: 0,
|
|
1273
|
+
}];
|
|
1274
|
+
}
|
|
1275
|
+
matchingSnapshots.sort();
|
|
1276
|
+
if (snapshotIndex < 0 || snapshotIndex >= matchingSnapshots.length) {
|
|
1277
|
+
return [2 /*return*/, {
|
|
1278
|
+
success: false,
|
|
1279
|
+
message: 'Invalid snapshot index.',
|
|
1280
|
+
documentsCount: 0,
|
|
1281
|
+
}];
|
|
1282
|
+
}
|
|
1283
|
+
snapshotFilePath = path.join(directoryPath, matchingSnapshots[snapshotIndex]);
|
|
1284
|
+
return [4 /*yield*/, fs_1.promises.readFile(snapshotFilePath, 'utf8')];
|
|
1285
|
+
case 2:
|
|
1286
|
+
snapshotData = _a.sent();
|
|
1287
|
+
return [4 /*yield*/, fs_1.promises.writeFile(filePath, snapshotData, 'utf8')];
|
|
1288
|
+
case 3:
|
|
1289
|
+
_a.sent();
|
|
1290
|
+
this.emit('snapshotRestored', {
|
|
1291
|
+
success: true,
|
|
1292
|
+
message: 'Snapshot restored successfully.',
|
|
1293
|
+
documentsCount: JSON.parse(snapshotData).length,
|
|
1294
|
+
snapshotFilePath: snapshotFilePath,
|
|
1295
|
+
});
|
|
1296
|
+
return [2 /*return*/, {
|
|
1297
|
+
success: true,
|
|
1298
|
+
message: 'Snapshot restored successfully.',
|
|
1299
|
+
documentsCount: JSON.parse(snapshotData).length,
|
|
1300
|
+
snapshotFilePath: snapshotFilePath,
|
|
1301
|
+
}];
|
|
1302
|
+
case 4:
|
|
1303
|
+
error_21 = _a.sent();
|
|
1304
|
+
return [2 /*return*/, {
|
|
1305
|
+
success: false,
|
|
1306
|
+
message: 'Error restoring snapshot.',
|
|
1307
|
+
errorMessage: error_21.message,
|
|
1308
|
+
documentsCount: 0,
|
|
1309
|
+
}];
|
|
1310
|
+
case 5: return [2 /*return*/];
|
|
1311
|
+
}
|
|
1312
|
+
});
|
|
1313
|
+
});
|
|
1314
|
+
};
|
|
1315
|
+
return NanoDb;
|
|
1316
|
+
}(events_1.EventEmitter));
|
|
1317
|
+
|
|
1318
|
+
exports.NanoDb = NanoDb;
|