couchset 0.0.0 → 0.0.5
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/LICENSE +21 -0
- package/README.md +231 -0
- package/dist/automate/index.d.ts +4 -0
- package/dist/automate/index.js +17 -0
- package/dist/automate/index.js.map +1 -0
- package/dist/automate/middlewares/index.d.ts +1 -0
- package/dist/automate/middlewares/index.js +14 -0
- package/dist/automate/middlewares/index.js.map +1 -0
- package/dist/automate/middlewares/isAuth.d.ts +7 -0
- package/dist/automate/middlewares/isAuth.js +28 -0
- package/dist/automate/middlewares/isAuth.js.map +1 -0
- package/dist/automate/model/client.d.ts +12 -0
- package/dist/automate/model/client.js +33 -0
- package/dist/automate/model/client.js.map +1 -0
- package/dist/automate/model/index.d.ts +44 -0
- package/dist/automate/model/index.js +304 -0
- package/dist/automate/model/index.js.map +1 -0
- package/dist/automate/morpheus/index.d.ts +14 -0
- package/dist/automate/morpheus/index.js +171 -0
- package/dist/automate/morpheus/index.js.map +1 -0
- package/dist/automate/writer/index.d.ts +10 -0
- package/dist/automate/writer/index.js +72 -0
- package/dist/automate/writer/index.js.map +1 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/connection.d.ts +36 -0
- package/dist/connection.js +129 -0
- package/dist/connection.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/model/index.d.ts +107 -0
- package/dist/model/index.js +329 -0
- package/dist/model/index.js.map +1 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +14 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/pagination/pagination.d.ts +19 -0
- package/dist/pagination/pagination.js +97 -0
- package/dist/pagination/pagination.js.map +1 -0
- package/dist/query/base-query.d.ts +17 -0
- package/dist/query/base-query.js +15 -0
- package/dist/query/base-query.js.map +1 -0
- package/dist/query/exceptions.d.ts +24 -0
- package/dist/query/exceptions.js +90 -0
- package/dist/query/exceptions.js.map +1 -0
- package/dist/query/helpers/builders.d.ts +64 -0
- package/dist/query/helpers/builders.js +415 -0
- package/dist/query/helpers/builders.js.map +1 -0
- package/dist/query/helpers/dictionary.d.ts +45 -0
- package/dist/query/helpers/dictionary.js +105 -0
- package/dist/query/helpers/dictionary.js.map +1 -0
- package/dist/query/helpers/index.d.ts +3 -0
- package/dist/query/helpers/index.js +16 -0
- package/dist/query/helpers/index.js.map +1 -0
- package/dist/query/helpers/reservedWords.d.ts +1 -0
- package/dist/query/helpers/reservedWords.js +200 -0
- package/dist/query/helpers/reservedWords.js.map +1 -0
- package/dist/query/index.d.ts +5 -0
- package/dist/query/index.js +27 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/interface/query.types.d.ts +236 -0
- package/dist/query/interface/query.types.js +3 -0
- package/dist/query/interface/query.types.js.map +1 -0
- package/dist/query/query.cluster.d.ts +14 -0
- package/dist/query/query.cluster.js +80 -0
- package/dist/query/query.cluster.js.map +1 -0
- package/dist/query/query.d.ts +321 -0
- package/dist/query/query.js +457 -0
- package/dist/query/query.js.map +1 -0
- package/dist/query/utils.d.ts +8 -0
- package/dist/query/utils.js +56 -0
- package/dist/query/utils.js.map +1 -0
- package/dist/search/customQuery.d.ts +17 -0
- package/dist/search/customQuery.js +83 -0
- package/dist/search/customQuery.js.map +1 -0
- package/dist/search/index.d.ts +1 -0
- package/dist/search/index.js +14 -0
- package/dist/search/index.js.map +1 -0
- package/dist/shared/ContextType.d.ts +27 -0
- package/dist/shared/ContextType.js +55 -0
- package/dist/shared/ContextType.js.map +1 -0
- package/dist/shared/common.model.d.ts +37 -0
- package/dist/shared/common.model.js +148 -0
- package/dist/shared/common.model.js.map +1 -0
- package/dist/shared/index.d.ts +2 -0
- package/dist/shared/index.js +15 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/utils/awaitTo.d.ts +17 -0
- package/dist/utils/awaitTo.js +31 -0
- package/dist/utils/awaitTo.js.map +1 -0
- package/dist/utils/date.d.ts +11 -0
- package/dist/utils/date.js +21 -0
- package/dist/utils/date.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/log.d.ts +9 -0
- package/dist/utils/log.js +19 -0
- package/dist/utils/log.js.map +1 -0
- package/dist/utils/text.utils.d.ts +14 -0
- package/dist/utils/text.utils.js +45 -0
- package/dist/utils/text.utils.js.map +1 -0
- package/dist/utils/utils.schema.d.ts +6 -0
- package/dist/utils/utils.schema.js +33 -0
- package/dist/utils/utils.schema.js.map +1 -0
- package/dist/uuid.d.ts +1 -0
- package/dist/uuid.js +9 -0
- package/dist/uuid.js.map +1 -0
- package/docs/couch.png +0 -0
- package/docs/couchset.png +0 -0
- package/docs/couchset_logo.png +0 -0
- package/package.json +108 -30
- package/register/index.js +1 -0
- package/register/transpile-only.js +3 -0
- package/register/type-check.js +3 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (_) try {
|
|
29
|
+
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;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.Model = void 0;
|
|
54
|
+
var connection_1 = __importDefault(require("../connection"));
|
|
55
|
+
var pagination_1 = require("../pagination");
|
|
56
|
+
var uuid_1 = require("../uuid");
|
|
57
|
+
var utils_1 = require("../utils");
|
|
58
|
+
var search_1 = require("../search");
|
|
59
|
+
var automate_1 = require("../automate");
|
|
60
|
+
var Model = /** @class */ (function () {
|
|
61
|
+
function Model(name, options) {
|
|
62
|
+
this.scope = '_default';
|
|
63
|
+
this.schema = {
|
|
64
|
+
createdAt: 'date',
|
|
65
|
+
updatedAt: 'date',
|
|
66
|
+
};
|
|
67
|
+
this.graphqlType = null;
|
|
68
|
+
// this.collection = CouchbaseConnection.Instance.getCollection();
|
|
69
|
+
this.collectionName = name;
|
|
70
|
+
if (options) {
|
|
71
|
+
this.graphqlType = (options && options.graphqlType) || null;
|
|
72
|
+
this.scope = (options && options.scope) || '_default';
|
|
73
|
+
this.schema = __assign(__assign({}, ((options && options.schema) || {})), { createdAt: 'date', updatedAt: 'date' });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Set setGraphqlType
|
|
78
|
+
* setGraphqlType
|
|
79
|
+
*/
|
|
80
|
+
Model.prototype.setGraphqlType = function (gqlType) {
|
|
81
|
+
this.graphqlType = gqlType;
|
|
82
|
+
return this;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Refresh and get default collection from CouchbaseConnection
|
|
86
|
+
* Because CouchbaseConnection is a singleton, sometimes it might be undefined depending when model was created
|
|
87
|
+
* So we have to call it from all model methods
|
|
88
|
+
* to avoid error `Cannot read property 'defaultCollection' of null`
|
|
89
|
+
*/
|
|
90
|
+
Model.prototype.fresh = function () {
|
|
91
|
+
// if couchbase connection has been init
|
|
92
|
+
if (connection_1.default.Instance.bucketName) {
|
|
93
|
+
this.collection = connection_1.default.Instance.getCollection();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
/** Get this collection
|
|
97
|
+
* getCollection
|
|
98
|
+
*/
|
|
99
|
+
Model.prototype.getBucketName = function () {
|
|
100
|
+
return connection_1.default.Instance.bucketName;
|
|
101
|
+
};
|
|
102
|
+
/** Get this collection
|
|
103
|
+
* getCollection
|
|
104
|
+
*/
|
|
105
|
+
Model.prototype.couchbaseConnection = function () {
|
|
106
|
+
return connection_1.default.Instance;
|
|
107
|
+
};
|
|
108
|
+
/** Get this collection
|
|
109
|
+
* getCollection
|
|
110
|
+
*/
|
|
111
|
+
Model.prototype.getCollection = function () {
|
|
112
|
+
this.fresh();
|
|
113
|
+
return this.collection;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* create
|
|
117
|
+
*/
|
|
118
|
+
Model.prototype.create = function (data) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
120
|
+
var id, createdData, error_1;
|
|
121
|
+
return __generator(this, function (_a) {
|
|
122
|
+
switch (_a.label) {
|
|
123
|
+
case 0:
|
|
124
|
+
this.fresh();
|
|
125
|
+
id = (0, uuid_1.generateUUID)();
|
|
126
|
+
createdData = __assign(__assign({ id: id }, data), { createdAt: new Date(), updatedAt: new Date(), _type: this.collectionName, _scope: this.scope });
|
|
127
|
+
_a.label = 1;
|
|
128
|
+
case 1:
|
|
129
|
+
_a.trys.push([1, 3, , 4]);
|
|
130
|
+
return [4 /*yield*/, this.collection.upsert(createdData.id, createdData)];
|
|
131
|
+
case 2:
|
|
132
|
+
_a.sent();
|
|
133
|
+
return [2 /*return*/, (0, utils_1.parseSchema)(this.schema, createdData)];
|
|
134
|
+
case 3:
|
|
135
|
+
error_1 = _a.sent();
|
|
136
|
+
throw error_1;
|
|
137
|
+
case 4: return [2 /*return*/];
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* findById
|
|
144
|
+
*/
|
|
145
|
+
Model.prototype.findById = function (id) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
147
|
+
var data, error_2;
|
|
148
|
+
return __generator(this, function (_a) {
|
|
149
|
+
switch (_a.label) {
|
|
150
|
+
case 0:
|
|
151
|
+
this.fresh();
|
|
152
|
+
_a.label = 1;
|
|
153
|
+
case 1:
|
|
154
|
+
_a.trys.push([1, 3, , 4]);
|
|
155
|
+
return [4 /*yield*/, this.collection.get(id)];
|
|
156
|
+
case 2:
|
|
157
|
+
data = _a.sent();
|
|
158
|
+
return [2 /*return*/, (0, utils_1.parseSchema)(this.schema, data.content)];
|
|
159
|
+
case 3:
|
|
160
|
+
error_2 = _a.sent();
|
|
161
|
+
throw error_2;
|
|
162
|
+
case 4: return [2 /*return*/];
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* update
|
|
169
|
+
*/
|
|
170
|
+
Model.prototype.updateById = function (id, data) {
|
|
171
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
172
|
+
var updatedDocument, error_3;
|
|
173
|
+
return __generator(this, function (_a) {
|
|
174
|
+
switch (_a.label) {
|
|
175
|
+
case 0:
|
|
176
|
+
this.fresh();
|
|
177
|
+
updatedDocument = __assign(__assign({}, data), { id: id, updatedAt: new Date(), _type: this.collectionName, _scope: this.scope });
|
|
178
|
+
_a.label = 1;
|
|
179
|
+
case 1:
|
|
180
|
+
_a.trys.push([1, 3, , 4]);
|
|
181
|
+
return [4 /*yield*/, this.collection.replace(id, updatedDocument)];
|
|
182
|
+
case 2:
|
|
183
|
+
_a.sent();
|
|
184
|
+
return [2 /*return*/, (0, utils_1.parseSchema)(this.schema, updatedDocument)];
|
|
185
|
+
case 3:
|
|
186
|
+
error_3 = _a.sent();
|
|
187
|
+
throw error_3;
|
|
188
|
+
case 4: return [2 /*return*/];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* save
|
|
195
|
+
*/
|
|
196
|
+
Model.prototype.save = function (data) {
|
|
197
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
198
|
+
var id, updatedDocument, error_4;
|
|
199
|
+
return __generator(this, function (_a) {
|
|
200
|
+
switch (_a.label) {
|
|
201
|
+
case 0:
|
|
202
|
+
this.fresh();
|
|
203
|
+
id = data && data.id;
|
|
204
|
+
updatedDocument = __assign(__assign({}, data), { id: id, updatedAt: new Date(), _type: this.collectionName, _scope: this.scope });
|
|
205
|
+
_a.label = 1;
|
|
206
|
+
case 1:
|
|
207
|
+
_a.trys.push([1, 3, , 4]);
|
|
208
|
+
if (!id) {
|
|
209
|
+
throw new Error('document must have id');
|
|
210
|
+
}
|
|
211
|
+
return [4 /*yield*/, this.collection.replace(id, updatedDocument)];
|
|
212
|
+
case 2:
|
|
213
|
+
_a.sent();
|
|
214
|
+
return [2 /*return*/, (0, utils_1.parseSchema)(this.schema, updatedDocument)];
|
|
215
|
+
case 3:
|
|
216
|
+
error_4 = _a.sent();
|
|
217
|
+
console.error(error_4);
|
|
218
|
+
throw error_4;
|
|
219
|
+
case 4: return [2 /*return*/];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
Model.prototype.delete = function (id) {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
226
|
+
var error_5;
|
|
227
|
+
return __generator(this, function (_a) {
|
|
228
|
+
switch (_a.label) {
|
|
229
|
+
case 0:
|
|
230
|
+
this.fresh();
|
|
231
|
+
_a.label = 1;
|
|
232
|
+
case 1:
|
|
233
|
+
_a.trys.push([1, 3, , 4]);
|
|
234
|
+
return [4 /*yield*/, this.collection.remove(id)];
|
|
235
|
+
case 2:
|
|
236
|
+
_a.sent();
|
|
237
|
+
return [2 /*return*/, true];
|
|
238
|
+
case 3:
|
|
239
|
+
error_5 = _a.sent();
|
|
240
|
+
throw error_5;
|
|
241
|
+
case 4: return [2 /*return*/];
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Pagination
|
|
248
|
+
*
|
|
249
|
+
select = ['id', 'createdAt']
|
|
250
|
+
where = {
|
|
251
|
+
where: { owner: { $eq: "stoqey" }, _type: { $eq: "Trade" } },
|
|
252
|
+
},
|
|
253
|
+
page = 0,
|
|
254
|
+
limit = 10,
|
|
255
|
+
orderBy = { createdAt: "DESC" },
|
|
256
|
+
* @param args PaginationArgs
|
|
257
|
+
*/
|
|
258
|
+
Model.prototype.pagination = function (_a) {
|
|
259
|
+
var select = _a.select, where = _a.where, orderBy = _a.orderBy, limit = _a.limit, page = _a.page, _b = _a.customQuery, customQuery = _b === void 0 ? {} : _b;
|
|
260
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
261
|
+
var whereEx, bucketName, rows;
|
|
262
|
+
var _this = this;
|
|
263
|
+
return __generator(this, function (_c) {
|
|
264
|
+
switch (_c.label) {
|
|
265
|
+
case 0:
|
|
266
|
+
this.fresh(); // refresh
|
|
267
|
+
whereEx = { _type: { $eq: this.collectionName } };
|
|
268
|
+
if (where) {
|
|
269
|
+
whereEx = __assign(__assign({}, whereEx), where);
|
|
270
|
+
}
|
|
271
|
+
bucketName = connection_1.default.Instance.bucketName;
|
|
272
|
+
return [4 /*yield*/, (0, pagination_1.Pagination)({
|
|
273
|
+
bucketName: bucketName,
|
|
274
|
+
select: select,
|
|
275
|
+
where: __assign({ where: whereEx }, customQuery),
|
|
276
|
+
limit: limit,
|
|
277
|
+
page: page,
|
|
278
|
+
orderBy: orderBy,
|
|
279
|
+
})];
|
|
280
|
+
case 1:
|
|
281
|
+
rows = _c.sent();
|
|
282
|
+
return [2 /*return*/, rows.map(function (r) { return (0, utils_1.parseSchema)(_this.schema, r); })];
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Run a custom query with model parsing
|
|
289
|
+
* @param { select, query }
|
|
290
|
+
* @returns
|
|
291
|
+
*/
|
|
292
|
+
Model.prototype.customQuery = function (_a) {
|
|
293
|
+
var params = _a.params, limit = _a.limit, query = _a.query;
|
|
294
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
295
|
+
var response;
|
|
296
|
+
return __generator(this, function (_b) {
|
|
297
|
+
switch (_b.label) {
|
|
298
|
+
case 0:
|
|
299
|
+
this.fresh(); // refresh
|
|
300
|
+
return [4 /*yield*/, (0, search_1.CustomQuery)({
|
|
301
|
+
limit: limit,
|
|
302
|
+
params: params,
|
|
303
|
+
query: query,
|
|
304
|
+
})];
|
|
305
|
+
case 1:
|
|
306
|
+
response = _b.sent();
|
|
307
|
+
return [2 /*return*/, response];
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
Model.prototype.parse = function (data) {
|
|
313
|
+
this.fresh(); // refresh
|
|
314
|
+
return (0, utils_1.parseSchema)(this.schema, data);
|
|
315
|
+
};
|
|
316
|
+
/**
|
|
317
|
+
*
|
|
318
|
+
* @param args AutomaticModelOptions
|
|
319
|
+
* @returns
|
|
320
|
+
*/
|
|
321
|
+
Model.prototype.automate = function (args) {
|
|
322
|
+
this.fresh(); // refresh
|
|
323
|
+
return (0, automate_1.automateImplementation)(this.graphqlType, __assign({ model: this }, args));
|
|
324
|
+
};
|
|
325
|
+
return Model;
|
|
326
|
+
}());
|
|
327
|
+
exports.Model = Model;
|
|
328
|
+
exports.default = Model;
|
|
329
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../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CAAC,CAAC,UAAU;QACxB,OAAO,IAAA,mBAAW,EAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,wBAAQ,GAAf,UAAgB,IAAsC;QAClD,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,UAAU;QACxB,OAAO,IAAA,iCAAsB,EAAC,IAAI,CAAC,WAAW,aAAG,KAAK,EAAE,IAAI,IAAK,IAAI,EAAE,CAAC;IAC5E,CAAC;IACL,YAAC;AAAD,CAAC,AA/PD,IA+PC;AA/PY,sBAAK;AAiQlB,kBAAe,KAAK,CAAC","sourcesContent":["import {Collection} from 'couchbase';\n\nimport CouchbaseConnection from '../connection';\nimport {Pagination} from '../pagination';\nimport {generateUUID} from '../uuid';\nimport {parseSchema, SchemaTypes} from '../utils';\nimport {CustomQuery, CustomQueryPagination} from '../search';\nimport {automateImplementation, AutomaticMethodOptions} from '../automate';\n\nexport interface AutoModelFields {\n id: string;\n createdAt: Date;\n updatedAt?: Date;\n deleted?: Date;\n _type: string; // type for models/collections\n _scope: string; // scope for collections\n}\n\ninterface ModalOptions {\n scope?: string;\n schema?: Record<string, SchemaTypes>;\n graphqlType?: any;\n}\n\nexport class Model {\n collection: Collection;\n collectionName: string;\n scope = '_default';\n schema: Record<string, SchemaTypes> = {\n createdAt: 'date',\n updatedAt: 'date',\n };\n graphqlType = null;\n\n constructor(name: string, options?: ModalOptions) {\n // this.collection = CouchbaseConnection.Instance.getCollection();\n this.collectionName = name;\n if (options) {\n this.graphqlType = (options && options.graphqlType) || null;\n this.scope = (options && options.scope) || '_default';\n this.schema = {\n ...((options && options.schema) || {}),\n createdAt: 'date',\n updatedAt: 'date',\n };\n }\n }\n\n /**\n * Set setGraphqlType\n * setGraphqlType\n */\n public setGraphqlType(gqlType: any): Model {\n this.graphqlType = gqlType;\n return this;\n }\n\n /**\n * Refresh and get default collection from CouchbaseConnection\n * Because CouchbaseConnection is a singleton, sometimes it might be undefined depending when model was created\n * So we have to call it from all model methods\n * to avoid error `Cannot read property 'defaultCollection' of null`\n */\n public fresh(): void {\n // if couchbase connection has been init\n if (CouchbaseConnection.Instance.bucketName) {\n this.collection = CouchbaseConnection.Instance.getCollection();\n }\n }\n\n /** Get this collection\n * getCollection\n */\n public getBucketName(): string {\n return CouchbaseConnection.Instance.bucketName;\n }\n\n /** Get this collection\n * getCollection\n */\n public couchbaseConnection(): CouchbaseConnection {\n return CouchbaseConnection.Instance;\n }\n\n /** Get this collection\n * getCollection\n */\n public getCollection(): Collection {\n this.fresh();\n return this.collection;\n }\n\n /**\n * create\n */\n public async create<T>(data: T): Promise<T & AutoModelFields> {\n this.fresh();\n const id = generateUUID();\n const createdData = {\n id, // let id be overriden\n ...data,\n createdAt: new Date(),\n updatedAt: new Date(), // same as created at\n _type: this.collectionName,\n _scope: this.scope,\n };\n\n try {\n await this.collection.upsert(createdData.id, createdData);\n return parseSchema(this.schema, createdData);\n } catch (error) {\n throw error;\n }\n }\n\n /**\n * findById\n */\n public async findById(id: string): Promise<any & AutoModelFields> {\n this.fresh();\n try {\n const data = await this.collection.get(id);\n return parseSchema(this.schema, data.content);\n } catch (error) {\n throw error;\n }\n }\n\n /**\n * update\n */\n public async updateById<T>(id: string, data: T): Promise<T> {\n this.fresh();\n const updatedDocument = {\n ...data,\n id,\n updatedAt: new Date(),\n _type: this.collectionName, // type and scope must be defined\n _scope: this.scope,\n };\n\n try {\n await this.collection.replace(id, updatedDocument);\n return parseSchema(this.schema, updatedDocument);\n } catch (error) {\n throw error;\n }\n }\n\n /**\n * save\n */\n public async save<T>(data: T & {id: string}): Promise<T> {\n this.fresh();\n\n const id = data && data.id;\n\n const updatedDocument = {\n ...data,\n id, // id's must match\n updatedAt: new Date(),\n _type: this.collectionName, // type and scope must be defined\n _scope: this.scope,\n };\n\n try {\n if (!id) {\n throw new Error('document must have id');\n }\n await this.collection.replace(id, updatedDocument);\n return parseSchema(this.schema, updatedDocument);\n } catch (error) {\n console.error(error);\n throw error;\n }\n }\n\n public async delete(id: string): Promise<boolean> {\n this.fresh();\n try {\n await this.collection.remove(id);\n return true;\n } catch (error) {\n throw error;\n }\n }\n\n /**\n * Pagination\n * \n select = ['id', 'createdAt']\n where = {\n where: { owner: { $eq: \"stoqey\" }, _type: { $eq: \"Trade\" } },\n },\n page = 0,\n limit = 10,\n orderBy = { createdAt: \"DESC\" },\n * @param args PaginationArgs\n */\n public async pagination({\n select,\n where,\n orderBy,\n limit,\n page,\n customQuery = {},\n }: {\n select?: any[] | string;\n where?: any;\n orderBy?: any;\n limit?: number;\n page?: number;\n customQuery?: any; // can be $and or any other valid quries\n }): Promise<any[]> {\n this.fresh(); // refresh\n // Where begins here\n let whereEx = {_type: {$eq: this.collectionName}};\n\n if (where) {\n whereEx = {\n ...whereEx,\n ...where,\n };\n }\n\n const bucketName = CouchbaseConnection.Instance.bucketName;\n\n const rows = await Pagination({\n bucketName,\n select,\n where: {where: whereEx, ...customQuery},\n limit,\n page,\n orderBy,\n });\n\n return rows.map((r) => parseSchema(this.schema, r));\n }\n\n /**\n * Run a custom query with model parsing\n * @param { select, query }\n * @returns\n */\n public async customQuery<T>({\n params,\n limit,\n query,\n }: {\n params: any;\n limit: number;\n query: string;\n }): Promise<[T[], CustomQueryPagination]> {\n this.fresh(); // refresh\n // Where begins here\n\n const response = await CustomQuery<T>({\n limit,\n params,\n query,\n });\n\n return response;\n }\n\n public parse<T>(data: T): T {\n this.fresh(); // refresh\n return parseSchema(this.schema, data);\n }\n\n /**\n *\n * @param args AutomaticModelOptions\n * @returns\n */\n public automate(args?: Partial<AutomaticMethodOptions>) {\n this.fresh(); // refresh\n return automateImplementation(this.graphqlType, {model: this, ...args});\n }\n}\n\nexport default Model;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './pagination';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./pagination"), exports);
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/pagination/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6B","sourcesContent":["export * from './pagination';\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface PaginationArgs {
|
|
2
|
+
bucketName: string;
|
|
3
|
+
select?: any[] | string;
|
|
4
|
+
where: any;
|
|
5
|
+
page: number;
|
|
6
|
+
limit: number;
|
|
7
|
+
orderBy?: any;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Common pagination
|
|
11
|
+
* where = {
|
|
12
|
+
where: { owner: { $eq: "stoqey" }, _type: { $eq: "Trade" } },
|
|
13
|
+
},
|
|
14
|
+
page = 0,
|
|
15
|
+
limit = 10,
|
|
16
|
+
orderBy = { createdAt: "DESC" },
|
|
17
|
+
* @param args PaginationArgs
|
|
18
|
+
*/
|
|
19
|
+
export declare const Pagination: (args: PaginationArgs) => Promise<any[]>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.Pagination = void 0;
|
|
43
|
+
var query_1 = require("../query");
|
|
44
|
+
var connection_1 = __importDefault(require("../connection"));
|
|
45
|
+
/**
|
|
46
|
+
* Common pagination
|
|
47
|
+
* where = {
|
|
48
|
+
where: { owner: { $eq: "stoqey" }, _type: { $eq: "Trade" } },
|
|
49
|
+
},
|
|
50
|
+
page = 0,
|
|
51
|
+
limit = 10,
|
|
52
|
+
orderBy = { createdAt: "DESC" },
|
|
53
|
+
* @param args PaginationArgs
|
|
54
|
+
*/
|
|
55
|
+
var Pagination = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
56
|
+
var _a, bucketName, _b,
|
|
57
|
+
// select: ogSelected = '*',
|
|
58
|
+
where, _c, page, _d, limit, _e, orderBy, cluster, select, offset, query, rows, completedRows, error_1;
|
|
59
|
+
return __generator(this, function (_f) {
|
|
60
|
+
switch (_f.label) {
|
|
61
|
+
case 0:
|
|
62
|
+
_a = args.bucketName, bucketName = _a === void 0 ? '_default' : _a, _b = args.where, where = _b === void 0 ? {
|
|
63
|
+
// where: {owner: {$eq: 'stoqey'}, _type: {$eq: 'Trade'}},
|
|
64
|
+
} : _b, _c = args.page, page = _c === void 0 ? 0 : _c, _d = args.limit, limit = _d === void 0 ? 10 : _d, _e = args.orderBy, orderBy = _e === void 0 ? { createdAt: 'DESC' } : _e;
|
|
65
|
+
cluster = connection_1.default.Instance.cluster;
|
|
66
|
+
select = args.select || '*';
|
|
67
|
+
if (Array.isArray(select)) {
|
|
68
|
+
select = select.map(function (i) { return ({ $field: i }); });
|
|
69
|
+
}
|
|
70
|
+
offset = page * limit;
|
|
71
|
+
_f.label = 1;
|
|
72
|
+
case 1:
|
|
73
|
+
_f.trys.push([1, 3, , 4]);
|
|
74
|
+
query = new query_1.QueryBuilder(where, bucketName)
|
|
75
|
+
.select(select)
|
|
76
|
+
.limit(limit)
|
|
77
|
+
.offset(offset)
|
|
78
|
+
.orderBy(orderBy)
|
|
79
|
+
.build();
|
|
80
|
+
console.log('query', query);
|
|
81
|
+
return [4 /*yield*/, cluster.query(query)];
|
|
82
|
+
case 2:
|
|
83
|
+
rows = (_f.sent()).rows;
|
|
84
|
+
completedRows = rows.map(function (r) {
|
|
85
|
+
return select === '*' ? r[bucketName] : r;
|
|
86
|
+
});
|
|
87
|
+
return [2 /*return*/, completedRows];
|
|
88
|
+
case 3:
|
|
89
|
+
error_1 = _f.sent();
|
|
90
|
+
console.error('error running pagination', error_1);
|
|
91
|
+
return [2 /*return*/, []];
|
|
92
|
+
case 4: return [2 /*return*/];
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}); };
|
|
96
|
+
exports.Pagination = Pagination;
|
|
97
|
+
//# sourceMappingURL=pagination.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pagination.js","sourceRoot":"","sources":["../../src/pagination/pagination.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kCAAsC;AACtC,6DAAgD;AAWhD;;;;;;;;;GASG;AACI,IAAM,UAAU,GAAG,UAAO,IAAoB;;;;;;;gBAE7C,KAQA,IAAI,WARmB,EAAvB,UAAU,mBAAG,UAAU,KAAA,EAEvB,KAMA,IAAI,MAJH,EAFD,KAAK,mBAAG;gBACJ,0DA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jD,sBAAO,EAAE,EAAC;;;;KAEjB,CAAC;AA1CW,QAAA,UAAU,cA0CrB","sourcesContent":["import {QueryBuilder} from '../query';\nimport CouchbaseConnection from '../connection';\n\nexport interface PaginationArgs {\n bucketName: string;\n select?: any[] | string;\n where: any;\n page: number;\n limit: number;\n orderBy?: any;\n}\n\n/**\n * Common pagination\n * where = {\n where: { owner: { $eq: \"stoqey\" }, _type: { $eq: \"Trade\" } },\n },\n page = 0,\n limit = 10,\n orderBy = { createdAt: \"DESC\" },\n * @param args PaginationArgs\n */\nexport const Pagination = async (args: PaginationArgs): Promise<any[]> => {\n const {\n bucketName = '_default',\n // select: ogSelected = '*',\n where = {\n // where: {owner: {$eq: 'stoqey'}, _type: {$eq: 'Trade'}},\n },\n page = 0,\n limit = 10,\n orderBy = {createdAt: 'DESC'},\n } = args;\n\n const cluster = CouchbaseConnection.Instance.cluster;\n\n let select = args.select || '*';\n if (Array.isArray(select)) {\n select = select.map((i) => ({$field: i}));\n }\n\n const offset = page * limit;\n\n try {\n const query = new QueryBuilder(where, bucketName)\n .select(select)\n .limit(limit)\n .offset(offset)\n .orderBy(orderBy)\n .build();\n\n console.log('query', query);\n\n const {rows} = await cluster.query(query);\n\n const completedRows = rows.map((r: any) => {\n return select === '*' ? r[bucketName] : r;\n });\n\n return completedRows;\n } catch (error) {\n console.error('error running pagination', error);\n return [];\n }\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { IConditionExpr, ILetExpr, ISelectType, LogicalWhereExpr, SortType } from './interface/query.types';
|
|
2
|
+
/**
|
|
3
|
+
* Basic definition of Query Class
|
|
4
|
+
* */
|
|
5
|
+
export declare abstract class BaseQuery {
|
|
6
|
+
protected _conditions: IConditionExpr;
|
|
7
|
+
protected _collection: string;
|
|
8
|
+
protected constructor(_conditions: IConditionExpr, _collection: string);
|
|
9
|
+
abstract select(value?: ISelectType[] | string | undefined): BaseQuery;
|
|
10
|
+
abstract where(value: LogicalWhereExpr): BaseQuery;
|
|
11
|
+
abstract orderBy(value: Record<string, SortType>): BaseQuery;
|
|
12
|
+
abstract limit(value: number): BaseQuery;
|
|
13
|
+
abstract offset(value: number): BaseQuery;
|
|
14
|
+
abstract let(value: [ILetExpr]): BaseQuery;
|
|
15
|
+
abstract useKeys(value: [string]): BaseQuery;
|
|
16
|
+
abstract build(): string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseQuery = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Basic definition of Query Class
|
|
6
|
+
* */
|
|
7
|
+
var BaseQuery = /** @class */ (function () {
|
|
8
|
+
function BaseQuery(_conditions, _collection) {
|
|
9
|
+
this._conditions = _conditions;
|
|
10
|
+
this._collection = _collection;
|
|
11
|
+
}
|
|
12
|
+
return BaseQuery;
|
|
13
|
+
}());
|
|
14
|
+
exports.BaseQuery = BaseQuery;
|
|
15
|
+
//# sourceMappingURL=base-query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-query.js","sourceRoot":"","sources":["../../src/query/base-query.ts"],"names":[],"mappings":";;;AAQA;;KAEK;AACL;IACI,mBAAgC,WAA2B,EAAY,WAAmB;QAA1D,gBAAW,GAAX,WAAW,CAAgB;QAAY,gBAAW,GAAX,WAAW,CAAQ;IAAG,CAAC;IAUlG,gBAAC;AAAD,CAAC,AAXD,IAWC;AAXqB,8BAAS","sourcesContent":["import {\n IConditionExpr,\n ILetExpr,\n ISelectType,\n LogicalWhereExpr,\n SortType,\n} from './interface/query.types';\n\n/**\n * Basic definition of Query Class\n * */\nexport abstract class BaseQuery {\n protected constructor(protected _conditions: IConditionExpr, protected _collection: string) {}\n\n abstract select(value?: ISelectType[] | string | undefined): BaseQuery;\n abstract where(value: LogicalWhereExpr): BaseQuery;\n abstract orderBy(value: Record<string, SortType>): BaseQuery;\n abstract limit(value: number): BaseQuery;\n abstract offset(value: number): BaseQuery;\n abstract let(value: [ILetExpr]): BaseQuery;\n abstract useKeys(value: [string]): BaseQuery;\n abstract build(): string;\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class SelectClauseException extends Error {
|
|
2
|
+
constructor();
|
|
3
|
+
}
|
|
4
|
+
export declare class WhereClauseException extends Error {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
export declare class InWithinOperatorExceptions extends WhereClauseException {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
export declare class MultipleQueryTypesException extends Error {
|
|
11
|
+
constructor(type1: string, type2: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class QueryOperatorNotFoundException extends WhereClauseException {
|
|
14
|
+
constructor(operator: string);
|
|
15
|
+
}
|
|
16
|
+
export declare class QueryGroupByParamsException extends WhereClauseException {
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
|
19
|
+
export declare class IndexParamsOnExceptions extends Error {
|
|
20
|
+
constructor(clause: string[]);
|
|
21
|
+
}
|
|
22
|
+
export declare class IndexParamsUsingGSIExceptions extends Error {
|
|
23
|
+
constructor(clause: string[]);
|
|
24
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.IndexParamsUsingGSIExceptions = exports.IndexParamsOnExceptions = exports.QueryGroupByParamsException = exports.QueryOperatorNotFoundException = exports.MultipleQueryTypesException = exports.InWithinOperatorExceptions = exports.WhereClauseException = exports.SelectClauseException = void 0;
|
|
19
|
+
var SelectClauseException = /** @class */ (function (_super) {
|
|
20
|
+
__extends(SelectClauseException, _super);
|
|
21
|
+
function SelectClauseException() {
|
|
22
|
+
// todo update message
|
|
23
|
+
return _super.call(this, 'The SELECT clause does not have the proper structure') || this;
|
|
24
|
+
}
|
|
25
|
+
return SelectClauseException;
|
|
26
|
+
}(Error));
|
|
27
|
+
exports.SelectClauseException = SelectClauseException;
|
|
28
|
+
var WhereClauseException = /** @class */ (function (_super) {
|
|
29
|
+
__extends(WhereClauseException, _super);
|
|
30
|
+
function WhereClauseException() {
|
|
31
|
+
return _super.call(this, 'The WHERE clause does not have the proper structure') || this;
|
|
32
|
+
}
|
|
33
|
+
return WhereClauseException;
|
|
34
|
+
}(Error));
|
|
35
|
+
exports.WhereClauseException = WhereClauseException;
|
|
36
|
+
var InWithinOperatorExceptions = /** @class */ (function (_super) {
|
|
37
|
+
__extends(InWithinOperatorExceptions, _super);
|
|
38
|
+
function InWithinOperatorExceptions() {
|
|
39
|
+
var _this = _super.call(this) || this;
|
|
40
|
+
_this.message = "The search_expr, target_expr and operator (IN | WITHIN) values are required for collection operators.";
|
|
41
|
+
return _this;
|
|
42
|
+
}
|
|
43
|
+
return InWithinOperatorExceptions;
|
|
44
|
+
}(WhereClauseException));
|
|
45
|
+
exports.InWithinOperatorExceptions = InWithinOperatorExceptions;
|
|
46
|
+
var MultipleQueryTypesException = /** @class */ (function (_super) {
|
|
47
|
+
__extends(MultipleQueryTypesException, _super);
|
|
48
|
+
function MultipleQueryTypesException(type1, type2) {
|
|
49
|
+
return _super.call(this, "Cannot combine multiple query types (ex: " + type1 + " with " + type2) || this;
|
|
50
|
+
}
|
|
51
|
+
return MultipleQueryTypesException;
|
|
52
|
+
}(Error));
|
|
53
|
+
exports.MultipleQueryTypesException = MultipleQueryTypesException;
|
|
54
|
+
var QueryOperatorNotFoundException = /** @class */ (function (_super) {
|
|
55
|
+
__extends(QueryOperatorNotFoundException, _super);
|
|
56
|
+
function QueryOperatorNotFoundException(operator) {
|
|
57
|
+
var _this = _super.call(this) || this;
|
|
58
|
+
_this.message = "Operator not found: " + operator;
|
|
59
|
+
return _this;
|
|
60
|
+
}
|
|
61
|
+
return QueryOperatorNotFoundException;
|
|
62
|
+
}(WhereClauseException));
|
|
63
|
+
exports.QueryOperatorNotFoundException = QueryOperatorNotFoundException;
|
|
64
|
+
var QueryGroupByParamsException = /** @class */ (function (_super) {
|
|
65
|
+
__extends(QueryGroupByParamsException, _super);
|
|
66
|
+
function QueryGroupByParamsException() {
|
|
67
|
+
var _this = _super.call(this) || this;
|
|
68
|
+
_this.message = "The GROUP BY clause must be defined to use the LETTING and HAVING clauses";
|
|
69
|
+
return _this;
|
|
70
|
+
}
|
|
71
|
+
return QueryGroupByParamsException;
|
|
72
|
+
}(WhereClauseException));
|
|
73
|
+
exports.QueryGroupByParamsException = QueryGroupByParamsException;
|
|
74
|
+
var IndexParamsOnExceptions = /** @class */ (function (_super) {
|
|
75
|
+
__extends(IndexParamsOnExceptions, _super);
|
|
76
|
+
function IndexParamsOnExceptions(clause) {
|
|
77
|
+
return _super.call(this, "The ON parameter can only be applied in the following clauses: " + JSON.stringify(clause)) || this;
|
|
78
|
+
}
|
|
79
|
+
return IndexParamsOnExceptions;
|
|
80
|
+
}(Error));
|
|
81
|
+
exports.IndexParamsOnExceptions = IndexParamsOnExceptions;
|
|
82
|
+
var IndexParamsUsingGSIExceptions = /** @class */ (function (_super) {
|
|
83
|
+
__extends(IndexParamsUsingGSIExceptions, _super);
|
|
84
|
+
function IndexParamsUsingGSIExceptions(clause) {
|
|
85
|
+
return _super.call(this, "The USING GSI parameter can only be applied in the following clauses: " + JSON.stringify(clause)) || this;
|
|
86
|
+
}
|
|
87
|
+
return IndexParamsUsingGSIExceptions;
|
|
88
|
+
}(Error));
|
|
89
|
+
exports.IndexParamsUsingGSIExceptions = IndexParamsUsingGSIExceptions;
|
|
90
|
+
//# sourceMappingURL=exceptions.js.map
|