tspace-mysql 1.4.7 → 1.4.8
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 +24 -7
- package/dist/lib/{tspace/Interface.d.ts → Interface.d.ts} +2 -1
- package/dist/lib/connection/index.d.ts +1 -1
- package/dist/lib/constants/index.js +3 -3
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractBuilder.d.ts +2 -2
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractBuilder.js +2 -2
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.d.ts +8 -2
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.d.ts +4 -2
- package/dist/lib/tspace/Blueprint.d.ts +11 -1
- package/dist/lib/tspace/Blueprint.js +11 -1
- package/dist/lib/tspace/Builder.d.ts +22 -7
- package/dist/lib/tspace/Builder.js +384 -310
- package/dist/lib/tspace/DB.d.ts +42 -2
- package/dist/lib/tspace/DB.js +61 -5
- package/dist/lib/tspace/{ProxyHandler.js → Handlers/Proxy.js} +1 -1
- package/dist/lib/tspace/{RelationHandler.d.ts → Handlers/Relation.d.ts} +4 -4
- package/dist/lib/tspace/{RelationHandler.js → Handlers/Relation.js} +95 -19
- package/dist/lib/tspace/Model.d.ts +71 -10
- package/dist/lib/tspace/Model.js +228 -64
- package/dist/lib/tspace/Schema.js +4 -5
- package/dist/lib/tspace/index.d.ts +2 -0
- package/dist/lib/tspace/index.js +3 -1
- package/dist/lib/utils/index.d.ts +1 -0
- package/dist/lib/utils/index.js +8 -0
- package/package.json +1 -1
- /package/dist/lib/{tspace/Interface.js → Interface.js} +0 -0
- /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.js +0 -0
- /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.js +0 -0
- /package/dist/lib/tspace/{ProxyHandler.d.ts → Handlers/Proxy.d.ts} +0 -0
- /package/dist/lib/tspace/{StateHandler.d.ts → Handlers/State.d.ts} +0 -0
- /package/dist/lib/tspace/{StateHandler.js → Handlers/State.js} +0 -0
package/dist/lib/tspace/DB.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AbstractDB } from './
|
|
2
|
-
import { Connection, ConnectionOptions, ConnectionTransaction } from '
|
|
1
|
+
import { AbstractDB } from './Abstracts/AbstractDB';
|
|
2
|
+
import { Connection, ConnectionOptions, ConnectionTransaction } from '../Interface';
|
|
3
3
|
/**
|
|
4
4
|
* 'DB' class is a component of the database system
|
|
5
5
|
* @param {string?} table table name
|
|
@@ -95,6 +95,46 @@ declare class DB extends AbstractDB {
|
|
|
95
95
|
* @return {string} string
|
|
96
96
|
*/
|
|
97
97
|
static generateUUID(): string;
|
|
98
|
+
/**
|
|
99
|
+
* The 'snakeCase' methid is used to covert value to snakeCase pattern.
|
|
100
|
+
* @return {string} string
|
|
101
|
+
*/
|
|
102
|
+
snakeCase(value: string): string;
|
|
103
|
+
/**
|
|
104
|
+
* The 'snakeCase' methid is used to covert value to snake_case pattern.
|
|
105
|
+
* @return {string} string
|
|
106
|
+
*/
|
|
107
|
+
static snakeCase(value: string): string;
|
|
108
|
+
/**
|
|
109
|
+
* The 'camelCase' methid is used to covert value to camelCase pattern.
|
|
110
|
+
* @return {string} string
|
|
111
|
+
*/
|
|
112
|
+
camelCase(value: string): string;
|
|
113
|
+
/**
|
|
114
|
+
* The 'camelCase' methid is used to covert value to camelCase pattern.
|
|
115
|
+
* @return {string} string
|
|
116
|
+
*/
|
|
117
|
+
static camelCase(value: string): string;
|
|
118
|
+
/**
|
|
119
|
+
* The 'escape' methid is used to escaping SQL injections.
|
|
120
|
+
* @return {string} string
|
|
121
|
+
*/
|
|
122
|
+
escape(value: string): string;
|
|
123
|
+
/**
|
|
124
|
+
* The 'escape' methid is used to escaping SQL injections.
|
|
125
|
+
* @return {string} string
|
|
126
|
+
*/
|
|
127
|
+
static escape(value: string): string;
|
|
128
|
+
/**
|
|
129
|
+
* The 'escapeXSS' methid is used to escaping XSS characters.
|
|
130
|
+
* @return {string} string
|
|
131
|
+
*/
|
|
132
|
+
escapeXSS(value: string): string;
|
|
133
|
+
/**
|
|
134
|
+
* The 'escapeXSS' methid is used to escaping XSS characters.
|
|
135
|
+
* @return {string} string
|
|
136
|
+
*/
|
|
137
|
+
static escapeXSS(value: string): string;
|
|
98
138
|
/**
|
|
99
139
|
* The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'.
|
|
100
140
|
* @param {string} sql
|
package/dist/lib/tspace/DB.js
CHANGED
|
@@ -24,10 +24,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.DB = void 0;
|
|
27
|
-
const AbstractDB_1 = require("./
|
|
28
|
-
const
|
|
27
|
+
const AbstractDB_1 = require("./Abstracts/AbstractDB");
|
|
28
|
+
const Proxy_1 = require("./Handlers/Proxy");
|
|
29
29
|
const connection_1 = require("../connection");
|
|
30
|
-
const
|
|
30
|
+
const State_1 = __importDefault(require("./Handlers/State"));
|
|
31
31
|
/**
|
|
32
32
|
* 'DB' class is a component of the database system
|
|
33
33
|
* @param {string?} table table name
|
|
@@ -40,7 +40,7 @@ class DB extends AbstractDB_1.AbstractDB {
|
|
|
40
40
|
this._initialDB();
|
|
41
41
|
if (table)
|
|
42
42
|
this.table(table);
|
|
43
|
-
return new Proxy(this,
|
|
43
|
+
return new Proxy(this, Proxy_1.proxyHandler);
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* The 'table' method is used to define the table name.
|
|
@@ -178,6 +178,62 @@ class DB extends AbstractDB_1.AbstractDB {
|
|
|
178
178
|
static generateUUID() {
|
|
179
179
|
return new this().generateUUID();
|
|
180
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* The 'snakeCase' methid is used to covert value to snakeCase pattern.
|
|
183
|
+
* @return {string} string
|
|
184
|
+
*/
|
|
185
|
+
snakeCase(value) {
|
|
186
|
+
return this.$utils.snakeCase(value);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The 'snakeCase' methid is used to covert value to snake_case pattern.
|
|
190
|
+
* @return {string} string
|
|
191
|
+
*/
|
|
192
|
+
static snakeCase(value) {
|
|
193
|
+
return new this().$utils.snakeCase(value);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The 'camelCase' methid is used to covert value to camelCase pattern.
|
|
197
|
+
* @return {string} string
|
|
198
|
+
*/
|
|
199
|
+
camelCase(value) {
|
|
200
|
+
return this.$utils.camelCase(value);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The 'camelCase' methid is used to covert value to camelCase pattern.
|
|
204
|
+
* @return {string} string
|
|
205
|
+
*/
|
|
206
|
+
static camelCase(value) {
|
|
207
|
+
return new this().$utils.camelCase(value);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The 'escape' methid is used to escaping SQL injections.
|
|
211
|
+
* @return {string} string
|
|
212
|
+
*/
|
|
213
|
+
escape(value) {
|
|
214
|
+
return this.$utils.escape(value);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The 'escape' methid is used to escaping SQL injections.
|
|
218
|
+
* @return {string} string
|
|
219
|
+
*/
|
|
220
|
+
static escape(value) {
|
|
221
|
+
return new this().$utils.escape(value);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The 'escapeXSS' methid is used to escaping XSS characters.
|
|
225
|
+
* @return {string} string
|
|
226
|
+
*/
|
|
227
|
+
escapeXSS(value) {
|
|
228
|
+
return this.$utils.escapeXSS(value);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The 'escapeXSS' methid is used to escaping XSS characters.
|
|
232
|
+
* @return {string} string
|
|
233
|
+
*/
|
|
234
|
+
static escapeXSS(value) {
|
|
235
|
+
return new this().$utils.escapeXSS(value);
|
|
236
|
+
}
|
|
181
237
|
/**
|
|
182
238
|
* The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'.
|
|
183
239
|
* @param {string} sql
|
|
@@ -276,7 +332,7 @@ class DB extends AbstractDB_1.AbstractDB {
|
|
|
276
332
|
});
|
|
277
333
|
}
|
|
278
334
|
_initialDB() {
|
|
279
|
-
this.$state = new
|
|
335
|
+
this.$state = new State_1.default(this.$constants('DB'));
|
|
280
336
|
return this;
|
|
281
337
|
}
|
|
282
338
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Relation, RelationQuery } from "
|
|
2
|
-
import { Model } from "
|
|
1
|
+
import { Relation, RelationQuery } from "../../Interface";
|
|
2
|
+
import { Model } from "../Model";
|
|
3
3
|
declare class RelationHandler {
|
|
4
4
|
private MODEL;
|
|
5
5
|
private $constants;
|
|
@@ -7,7 +7,7 @@ declare class RelationHandler {
|
|
|
7
7
|
constructor(model: Model);
|
|
8
8
|
load(parents: Record<string, any>[], relation: Relation): Promise<any[]>;
|
|
9
9
|
loadExists(): string;
|
|
10
|
-
apply(nameRelations: string[], type: 'all' | 'exists' | 'trashed' | 'default'): Relation[];
|
|
10
|
+
apply(nameRelations: string[], type: 'all' | 'exists' | 'trashed' | 'count' | 'default'): Relation[];
|
|
11
11
|
callback(nameRelation: string, cb: Function): void;
|
|
12
12
|
hasOne({ name, as, model, localKey, foreignKey, freezeTable }: Relation): void;
|
|
13
13
|
hasMany({ name, as, model, localKey, foreignKey, freezeTable }: Relation): void;
|
|
@@ -23,7 +23,7 @@ declare class RelationHandler {
|
|
|
23
23
|
private _relationMapData;
|
|
24
24
|
private _belongsToMany;
|
|
25
25
|
private _valueInRelation;
|
|
26
|
-
|
|
26
|
+
protected _valuePattern(value: string): string;
|
|
27
27
|
private _assertError;
|
|
28
28
|
protected _getState(key: string): any;
|
|
29
29
|
protected _setState(key: string, value: any): void;
|
|
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.RelationHandler = void 0;
|
|
16
16
|
const pluralize_1 = __importDefault(require("pluralize"));
|
|
17
|
-
const Model_1 = require("
|
|
17
|
+
const Model_1 = require("../Model");
|
|
18
18
|
class RelationHandler {
|
|
19
19
|
constructor(model) {
|
|
20
20
|
this.MODEL = model;
|
|
@@ -44,6 +44,19 @@ class RelationHandler {
|
|
|
44
44
|
return [];
|
|
45
45
|
const query = relation.query;
|
|
46
46
|
this._assertError(query == null, `Unknown callback query in [Relation : ${relation.name}]`);
|
|
47
|
+
if (relation.count) {
|
|
48
|
+
const results = yield query
|
|
49
|
+
.whereIn(foreignKey, parentIds)
|
|
50
|
+
.select(foreignKey)
|
|
51
|
+
.selectRaw(`${this.$constants('COUNT')}(${foreignKey}) ${this.$constants('AS')} \`aggregate\``)
|
|
52
|
+
.debug(this._getState('DEBUG'))
|
|
53
|
+
.when(relation.trashed, (query) => query.onlyTrashed())
|
|
54
|
+
.when(relation.all, (query) => query.disableSoftDelete())
|
|
55
|
+
.bind(this.MODEL['$pool'].get())
|
|
56
|
+
.groupBy(foreignKey)
|
|
57
|
+
.get();
|
|
58
|
+
return this._relationMapData(parents, results, relation);
|
|
59
|
+
}
|
|
47
60
|
const results = yield query
|
|
48
61
|
.whereIn(foreignKey, parentIds)
|
|
49
62
|
.debug(this._getState('DEBUG'))
|
|
@@ -70,6 +83,8 @@ class RelationHandler {
|
|
|
70
83
|
const cloneRelations = clone['_getState']('RELATIONS');
|
|
71
84
|
if (cloneRelations.length) {
|
|
72
85
|
for (const r of cloneRelations) {
|
|
86
|
+
if (!r.exists)
|
|
87
|
+
continue;
|
|
73
88
|
if (r.query == null)
|
|
74
89
|
continue;
|
|
75
90
|
const sql = (_c = (_b = clone['$relation']) === null || _b === void 0 ? void 0 : _b._handleRelationsExists(r)) !== null && _c !== void 0 ? _c : '';
|
|
@@ -82,21 +97,21 @@ class RelationHandler {
|
|
|
82
97
|
: new Model_1.Model().table(String(pivot));
|
|
83
98
|
const sql = clone
|
|
84
99
|
.bind(this.MODEL['$pool'].get())
|
|
85
|
-
.
|
|
86
|
-
.whereReference(`\`${query.getTableName()}\`.\`${foreignKey}\``, `\`${
|
|
100
|
+
.selectRaw("1")
|
|
101
|
+
.whereReference(`\`${query.getTableName()}\`.\`${foreignKey}\``, `\`${thisPivot.getTableName()}\`.\`${localKey}\``)
|
|
87
102
|
.toString();
|
|
88
103
|
thisPivot.whereExists(sql);
|
|
89
104
|
const sqlPivot = thisPivot
|
|
90
105
|
.bind(this.MODEL['$pool'].get())
|
|
91
|
-
.
|
|
92
|
-
.whereReference(`\`${this.MODEL['getTableName']()}\`.\`${foreignKey}\``, `\`${
|
|
106
|
+
.selectRaw("1")
|
|
107
|
+
.whereReference(`\`${this.MODEL['getTableName']()}\`.\`${foreignKey}\``, `\`${thisPivot.getTableName()}\`.\`${this._valuePattern([pluralize_1.default.singular(this.MODEL['getTableName']()), foreignKey].join("_"))}\``)
|
|
93
108
|
.toString();
|
|
94
109
|
this.MODEL['whereExists'](sqlPivot);
|
|
95
110
|
continue;
|
|
96
111
|
}
|
|
97
112
|
const sql = clone
|
|
98
113
|
.bind(this.MODEL['$pool'].get())
|
|
99
|
-
.
|
|
114
|
+
.selectRaw("1")
|
|
100
115
|
.whereReference(`\`${this.MODEL['getTableName']()}\`.\`${localKey}\``, `\`${query.getTableName()}\`.\`${foreignKey}\``)
|
|
101
116
|
.toString();
|
|
102
117
|
this.MODEL['whereExists'](sql);
|
|
@@ -284,7 +299,7 @@ class RelationHandler {
|
|
|
284
299
|
return;
|
|
285
300
|
}
|
|
286
301
|
_handleRelationsExists(relation) {
|
|
287
|
-
var _a, _b, _c;
|
|
302
|
+
var _a, _b, _c, _d;
|
|
288
303
|
this._assertError(!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length), `unknown [relation]`);
|
|
289
304
|
const { localKey, foreignKey } = this._valueInRelation(relation);
|
|
290
305
|
const query = relation.query;
|
|
@@ -293,16 +308,46 @@ class RelationHandler {
|
|
|
293
308
|
const cloneRelations = clone['_getState']('RELATIONS');
|
|
294
309
|
if (cloneRelations.length) {
|
|
295
310
|
for (const r of cloneRelations) {
|
|
311
|
+
if (!r.exists)
|
|
312
|
+
continue;
|
|
296
313
|
if (r.query == null)
|
|
297
314
|
continue;
|
|
298
|
-
|
|
315
|
+
if (r.relation === this.$constants('RELATIONSHIP').belongsToMany) {
|
|
316
|
+
const data = (_b = clone['$relation']) === null || _b === void 0 ? void 0 : _b._valueInRelation(r);
|
|
317
|
+
if (data == null)
|
|
318
|
+
continue;
|
|
319
|
+
const { modelPivot, pivot, foreignKey } = data;
|
|
320
|
+
const thisPivot = modelPivot == null && pivot == null
|
|
321
|
+
? new Model_1.Model().table(`${this._valuePattern([
|
|
322
|
+
pluralize_1.default.singular(clone.getTableName()),
|
|
323
|
+
pluralize_1.default.singular(this.MODEL['getTableName']())
|
|
324
|
+
].join("_"))}`)
|
|
325
|
+
: modelPivot ? new modelPivot : new Model_1.Model().table(`${pivot}`);
|
|
326
|
+
const sql = clone
|
|
327
|
+
.bind(this.MODEL['$pool'].get())
|
|
328
|
+
.selectRaw("1")
|
|
329
|
+
.whereReference(`\`${clone.getTableName()}\`.\`${foreignKey}\``, `\`${thisPivot.getTableName()}\`.\`${this._valuePattern([
|
|
330
|
+
pluralize_1.default.singular(clone.getTableName()),
|
|
331
|
+
localKey
|
|
332
|
+
].join('_'))}\``)
|
|
333
|
+
.toString();
|
|
334
|
+
thisPivot.whereExists(sql);
|
|
335
|
+
const sqlPivot = thisPivot
|
|
336
|
+
.bind(this.MODEL['$pool'].get())
|
|
337
|
+
.selectRaw("1")
|
|
338
|
+
.whereReference(`\`${this.MODEL['getTableName']()}\`.\`${foreignKey}\``, `\`${thisPivot.getTableName()}\`.\`${this._valuePattern([pluralize_1.default.singular(this.MODEL['getTableName']()), foreignKey].join("_"))}\``)
|
|
339
|
+
.toString();
|
|
340
|
+
clone.whereExists(sqlPivot);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const sql = (_d = (_c = clone['$relation']) === null || _c === void 0 ? void 0 : _c._handleRelationsExists(r)) !== null && _d !== void 0 ? _d : '';
|
|
299
344
|
clone.whereExists(sql);
|
|
300
345
|
}
|
|
301
346
|
}
|
|
302
347
|
const sql = clone
|
|
303
348
|
.bind(this.MODEL['$pool'].get())
|
|
304
|
-
.
|
|
305
|
-
.whereReference(`\`${this.MODEL['getTableName']()}\`.\`${localKey}\``, `\`${
|
|
349
|
+
.selectRaw("1")
|
|
350
|
+
.whereReference(`\`${this.MODEL['getTableName']()}\`.\`${localKey}\``, `\`${clone.getTableName()}\`.\`${foreignKey}\``)
|
|
306
351
|
.toString();
|
|
307
352
|
return sql;
|
|
308
353
|
}
|
|
@@ -319,8 +364,9 @@ class RelationHandler {
|
|
|
319
364
|
const functionName = [...this.$logger.get()][this.$logger.get().length - 2];
|
|
320
365
|
return functionName.replace(/([A-Z])/g, (str) => `_${str.toLowerCase()}`);
|
|
321
366
|
}
|
|
322
|
-
_relationMapData(dataParents, dataChilds,
|
|
323
|
-
|
|
367
|
+
_relationMapData(dataParents, dataChilds, r) {
|
|
368
|
+
var _a;
|
|
369
|
+
const { name, as, relation, localKey, foreignKey } = this._valueInRelation(r);
|
|
324
370
|
const keyRelation = as !== null && as !== void 0 ? as : name;
|
|
325
371
|
for (const dataParent of dataParents) {
|
|
326
372
|
const relationIsHasOneOrBelongsTo = [
|
|
@@ -329,11 +375,15 @@ class RelationHandler {
|
|
|
329
375
|
].some(r => r === relation);
|
|
330
376
|
dataParent[keyRelation] = [];
|
|
331
377
|
if (relationIsHasOneOrBelongsTo)
|
|
332
|
-
dataParent[keyRelation] = null;
|
|
378
|
+
dataParent[keyRelation] = r.count ? 0 : null;
|
|
333
379
|
if (!dataChilds.length)
|
|
334
380
|
continue;
|
|
335
381
|
for (const dataChild of dataChilds) {
|
|
336
382
|
if (dataChild[foreignKey] === dataParent[localKey]) {
|
|
383
|
+
if (r.count) {
|
|
384
|
+
dataParent[keyRelation] = (_a = dataChild === null || dataChild === void 0 ? void 0 : dataChild.aggregate) !== null && _a !== void 0 ? _a : 0;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
337
387
|
const relationIsHasOneOrBelongsTo = [
|
|
338
388
|
this.$constants('RELATIONSHIP').hasOne,
|
|
339
389
|
this.$constants('RELATIONSHIP').belongsTo
|
|
@@ -351,7 +401,7 @@ class RelationHandler {
|
|
|
351
401
|
return dataParents;
|
|
352
402
|
}
|
|
353
403
|
_belongsToMany(parents, relation) {
|
|
354
|
-
var _a;
|
|
404
|
+
var _a, _b;
|
|
355
405
|
return __awaiter(this, void 0, void 0, function* () {
|
|
356
406
|
const { name, foreignKey, localKey, pivot, oldVersion, modelPivot } = this._valueInRelation(relation);
|
|
357
407
|
const localKeyId = parents.map((parent) => {
|
|
@@ -371,23 +421,49 @@ class RelationHandler {
|
|
|
371
421
|
const pivotTable = String(((_a = relation.pivot) !== null && _a !== void 0 ? _a : pivot));
|
|
372
422
|
const sqlPivotExists = new Model_1.Model()
|
|
373
423
|
.copyModel(modelRelation)
|
|
374
|
-
.
|
|
424
|
+
.selectRaw("1")
|
|
375
425
|
.whereReference(`\`${modelRelation.getTableName()}\`.\`${foreignKey}\``, `\`${pivotTable}\`.\`${localKey}\``)
|
|
376
426
|
.toString();
|
|
377
427
|
const queryPivot = modelPivot
|
|
378
428
|
? new modelPivot()
|
|
379
429
|
: new Model_1.Model().table(pivotTable);
|
|
380
|
-
|
|
430
|
+
if (relation.count) {
|
|
431
|
+
const pivotResults = yield queryPivot
|
|
432
|
+
.whereIn(localKeyPivotTable, mainResultIds)
|
|
433
|
+
.select(localKeyPivotTable)
|
|
434
|
+
.selectRaw(`${this.$constants('COUNT')}(${localKeyPivotTable}) ${this.$constants('AS')} \`aggregate\``)
|
|
435
|
+
.when(relation.exists, (query) => query.whereExists(sqlPivotExists))
|
|
436
|
+
.when(relation.trashed, (query) => query.onlyTrashed())
|
|
437
|
+
.when(relation.all, (query) => query.disableSoftDelete())
|
|
438
|
+
.groupBy(localKeyPivotTable)
|
|
439
|
+
.bind(this.MODEL['$pool'].get())
|
|
440
|
+
.debug(this._getState('DEBUG'))
|
|
441
|
+
.get();
|
|
442
|
+
for (const parent of parents) {
|
|
443
|
+
if (parent[name] == null)
|
|
444
|
+
parent[name] = 0;
|
|
445
|
+
for (const pivotResult of pivotResults) {
|
|
446
|
+
if (pivotResult[localKeyPivotTable] !== parent[foreignKey])
|
|
447
|
+
continue;
|
|
448
|
+
parent[name] = (_b = pivotResult.aggregate) !== null && _b !== void 0 ? _b : 0;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (this._getState('HIDDEN').length)
|
|
452
|
+
this.MODEL['_hiddenColumnModel'](parents);
|
|
453
|
+
return parents;
|
|
454
|
+
}
|
|
455
|
+
const pivotResults = yield queryPivot
|
|
381
456
|
.whereIn(localKeyPivotTable, mainResultIds)
|
|
382
457
|
.when(relation.exists, (query) => query.whereExists(sqlPivotExists))
|
|
383
458
|
.when(relation.trashed, (query) => query.onlyTrashed())
|
|
384
459
|
.when(relation.all, (query) => query.disableSoftDelete())
|
|
385
|
-
.
|
|
386
|
-
|
|
460
|
+
.bind(this.MODEL['$pool'].get())
|
|
461
|
+
.debug(this._getState('DEBUG'))
|
|
462
|
+
.get();
|
|
387
463
|
const relationIds = Array.from(new Set(pivotResults
|
|
388
464
|
.map((pivotResult) => pivotResult[relationForeignKey])
|
|
389
465
|
.filter((d) => d != null)));
|
|
390
|
-
const relationResults = yield this.MODEL
|
|
466
|
+
const relationResults = yield this.MODEL.rawQuery(modelRelation
|
|
391
467
|
.whereIn(mainlocalKey, relationIds)
|
|
392
468
|
.when(relation.trashed, (query) => query.disableSoftDelete())
|
|
393
469
|
.toString());
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { AbstractModel } from './
|
|
2
|
-
import {
|
|
1
|
+
import { AbstractModel } from './Abstracts/AbstractModel';
|
|
2
|
+
import { Blueprint } from './Blueprint';
|
|
3
|
+
import { Relation, Pagination, RelationQuery, ValidateSchema } from '../Interface';
|
|
3
4
|
/**
|
|
4
5
|
*
|
|
5
6
|
* 'Model' class is a representation of a database table
|
|
@@ -92,7 +93,7 @@ declare class Model extends AbstractModel {
|
|
|
92
93
|
* }
|
|
93
94
|
* @return {this} this
|
|
94
95
|
*/
|
|
95
|
-
protected useSchema(schema: Record<string,
|
|
96
|
+
protected useSchema(schema: Record<string, Blueprint>): this;
|
|
96
97
|
/**
|
|
97
98
|
*
|
|
98
99
|
* The "useRegistry" method is used to define Function to results.
|
|
@@ -105,7 +106,6 @@ declare class Model extends AbstractModel {
|
|
|
105
106
|
* this.useRegistry()
|
|
106
107
|
* }
|
|
107
108
|
* }
|
|
108
|
-
|
|
109
109
|
*/
|
|
110
110
|
protected useRegistry(): this;
|
|
111
111
|
/**
|
|
@@ -176,7 +176,27 @@ declare class Model extends AbstractModel {
|
|
|
176
176
|
* }
|
|
177
177
|
*/
|
|
178
178
|
protected usePattern(pattern: "snake_case" | "camelCase"): this;
|
|
179
|
+
/**
|
|
180
|
+
* The "useCamelCase" method is used to assign pattern camelCase.
|
|
181
|
+
* @return {this} this
|
|
182
|
+
* @example
|
|
183
|
+
* class User extends Model {
|
|
184
|
+
* constructor() {
|
|
185
|
+
* this.useCamelCase()
|
|
186
|
+
* }
|
|
187
|
+
* }
|
|
188
|
+
*/
|
|
179
189
|
protected useCamelCase(): this;
|
|
190
|
+
/**
|
|
191
|
+
* The "SnakeCase" method is used to assign pattern snake_case.
|
|
192
|
+
* @return {this} this
|
|
193
|
+
* @example
|
|
194
|
+
* class User extends Model {
|
|
195
|
+
* constructor() {
|
|
196
|
+
* this.SnakeCase()
|
|
197
|
+
* }
|
|
198
|
+
* }
|
|
199
|
+
*/
|
|
180
200
|
protected useSnakeCase(): this;
|
|
181
201
|
/**
|
|
182
202
|
* The "useSoftDelete" refer to a feature that allows you to "soft delete" records from a database table instead of permanently deleting them.
|
|
@@ -331,7 +351,7 @@ declare class Model extends AbstractModel {
|
|
|
331
351
|
* @override
|
|
332
352
|
* @return {promise<string>} string
|
|
333
353
|
*/
|
|
334
|
-
protected
|
|
354
|
+
protected exceptColumns(): Promise<string[]>;
|
|
335
355
|
/**
|
|
336
356
|
* Build method for relation in model
|
|
337
357
|
* @param {string} name name relation registry in your model
|
|
@@ -339,6 +359,36 @@ declare class Model extends AbstractModel {
|
|
|
339
359
|
* @return {this} this
|
|
340
360
|
*/
|
|
341
361
|
protected buildMethodRelation(name: string, callback?: Function): this;
|
|
362
|
+
/**
|
|
363
|
+
*
|
|
364
|
+
* The 'makeSelectStatement' method is used to make select statement.
|
|
365
|
+
* @return {Promise<string>} string
|
|
366
|
+
*/
|
|
367
|
+
makeSelectStatement(): Promise<string>;
|
|
368
|
+
/**
|
|
369
|
+
*
|
|
370
|
+
* The 'makeInsertStatement' method is used to make insert table statement.
|
|
371
|
+
* @return {Promise<string>} string
|
|
372
|
+
*/
|
|
373
|
+
makeInsertStatement(): Promise<string>;
|
|
374
|
+
/**
|
|
375
|
+
*
|
|
376
|
+
* The 'makeUpdateStatement' method is used to make update table statement.
|
|
377
|
+
* @return {Promise<string>} string
|
|
378
|
+
*/
|
|
379
|
+
makeUpdateStatement(): Promise<string>;
|
|
380
|
+
/**
|
|
381
|
+
*
|
|
382
|
+
* The 'makeDeleteStatement' method is used to make delete statement.
|
|
383
|
+
* @return {Promise<string>} string
|
|
384
|
+
*/
|
|
385
|
+
makeDeleteStatement(): Promise<string>;
|
|
386
|
+
/**
|
|
387
|
+
*
|
|
388
|
+
* The 'makeCreateStatement' method is used to make create table statement.
|
|
389
|
+
* @return {Promise<string>} string
|
|
390
|
+
*/
|
|
391
|
+
makeCreateStatement(): Promise<string>;
|
|
342
392
|
/**
|
|
343
393
|
*
|
|
344
394
|
* Clone instance of model
|
|
@@ -446,6 +496,15 @@ declare class Model extends AbstractModel {
|
|
|
446
496
|
* @return {this} this
|
|
447
497
|
*/
|
|
448
498
|
withAll(...nameRelations: string[]): this;
|
|
499
|
+
/**
|
|
500
|
+
* The 'withAll' method is used to eager load related (relations) data when retrieving records from a database.
|
|
501
|
+
*
|
|
502
|
+
* Eager loading allows you to retrieve a primary model and its related models in a more efficient
|
|
503
|
+
* It's method ignore soft delete
|
|
504
|
+
* @param {...string} nameRelations if data exists return blank
|
|
505
|
+
* @return {this} this
|
|
506
|
+
*/
|
|
507
|
+
withCount(...nameRelations: string[]): this;
|
|
449
508
|
/**
|
|
450
509
|
* The 'withTrashed' method is used to eager load related (relations) data when retrieving records from a database.
|
|
451
510
|
*
|
|
@@ -986,13 +1045,14 @@ declare class Model extends AbstractModel {
|
|
|
986
1045
|
* @return {this} this this
|
|
987
1046
|
*/
|
|
988
1047
|
insertNotExists(data: Record<string, string | number | boolean | null | undefined>): this;
|
|
1048
|
+
getSchemaModel(): Record<string, any> | null;
|
|
1049
|
+
validation(schema?: ValidateSchema): this;
|
|
989
1050
|
/**
|
|
990
|
-
*
|
|
991
|
-
*
|
|
992
|
-
* @return {
|
|
1051
|
+
* The 'bindPattern' method is used to covert column relate with pattern
|
|
1052
|
+
* @param {string} column
|
|
1053
|
+
* @return {string} return table.column
|
|
993
1054
|
*/
|
|
994
|
-
|
|
995
|
-
getSchemaModel(): Record<string, any> | null;
|
|
1055
|
+
bindPattern(column: string): string;
|
|
996
1056
|
/**
|
|
997
1057
|
* @override Method
|
|
998
1058
|
* @return {Promise<Record<string,any> | any[] | null | undefined>}
|
|
@@ -1020,6 +1080,7 @@ declare class Model extends AbstractModel {
|
|
|
1020
1080
|
insert: () => string;
|
|
1021
1081
|
update: () => string;
|
|
1022
1082
|
delete: () => string;
|
|
1083
|
+
where: () => string | null;
|
|
1023
1084
|
any: () => string;
|
|
1024
1085
|
};
|
|
1025
1086
|
private _showOnly;
|