tspace-mysql 1.8.2 → 1.8.4
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 +443 -68
- package/build/cli/generate/model.js +3 -3
- package/build/cli/generate/model.js.map +1 -1
- package/build/lib/constants/index.d.ts +7 -1
- package/build/lib/constants/index.js +7 -1
- package/build/lib/constants/index.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractDB.d.ts +2 -2
- package/build/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractView.d.ts +10 -0
- package/build/lib/core/Abstracts/AbstractView.js +9 -0
- package/build/lib/core/Abstracts/AbstractView.js.map +1 -0
- package/build/lib/core/Blueprint.d.ts +24 -4
- package/build/lib/core/Blueprint.js +78 -87
- package/build/lib/core/Blueprint.js.map +1 -1
- package/build/lib/core/Builder.d.ts +39 -30
- package/build/lib/core/Builder.js +1094 -1044
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/Cache/DBCache.js +15 -16
- package/build/lib/core/Cache/DBCache.js.map +1 -1
- package/build/lib/core/DB.d.ts +38 -38
- package/build/lib/core/DB.js +119 -115
- package/build/lib/core/DB.js.map +1 -1
- package/build/lib/core/Decorator.d.ts +1 -1
- package/build/lib/core/Decorator.js.map +1 -1
- package/build/lib/core/Handlers/Relation.d.ts +2 -1
- package/build/lib/core/Handlers/Relation.js +60 -15
- package/build/lib/core/Handlers/Relation.js.map +1 -1
- package/build/lib/core/Handlers/State.d.ts +1 -0
- package/build/lib/core/Handlers/State.js +1 -1
- package/build/lib/core/Handlers/State.js.map +1 -1
- package/build/lib/core/JoinModel.d.ts +1 -1
- package/build/lib/core/JoinModel.js.map +1 -1
- package/build/lib/core/Meta.d.ts +55 -0
- package/build/lib/core/Meta.js +175 -0
- package/build/lib/core/Meta.js.map +1 -0
- package/build/lib/core/Model.d.ts +158 -94
- package/build/lib/core/Model.js +1586 -1203
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/core/Nest/index.d.ts +3 -1
- package/build/lib/core/Nest/index.js +4 -2
- package/build/lib/core/Nest/index.js.map +1 -1
- package/build/lib/{connection/index.d.ts → core/Pool.d.ts} +2 -1
- package/build/lib/{connection/index.js → core/Pool.js} +74 -6
- package/build/lib/core/Pool.js.map +1 -0
- package/build/lib/core/Repository.d.ts +16 -16
- package/build/lib/core/Repository.js +73 -45
- package/build/lib/core/Repository.js.map +1 -1
- package/build/lib/core/Schema.d.ts +2 -2
- package/build/lib/core/Schema.js +113 -78
- package/build/lib/core/Schema.js.map +1 -1
- package/build/lib/core/SqlLike.js.map +1 -1
- package/build/lib/core/StoredProcedure.d.ts +120 -0
- package/build/lib/core/StoredProcedure.js +161 -0
- package/build/lib/core/StoredProcedure.js.map +1 -0
- package/build/lib/core/UtilityTypes.d.ts +25 -44
- package/build/lib/core/View.d.ts +96 -0
- package/build/lib/core/View.js +141 -0
- package/build/lib/core/View.js.map +1 -0
- package/build/lib/core/index.d.ts +17 -12
- package/build/lib/core/index.js +9 -4
- package/build/lib/core/index.js.map +1 -1
- package/build/lib/types/index.d.ts +23 -3
- package/build/lib/utils/index.js +5 -2
- package/build/lib/utils/index.js.map +1 -1
- package/build/tests/01-Pool.test.js +0 -10
- package/build/tests/01-Pool.test.js.map +1 -1
- package/build/tests/04-Model-pattern.test.js +1 -1
- package/build/tests/04-Model-pattern.test.js.map +1 -1
- package/build/tests/05-View.test.d.ts +1 -0
- package/build/tests/05-View.test.js +83 -0
- package/build/tests/05-View.test.js.map +1 -0
- package/build/tests/06-Meta.test.d.ts +1 -0
- package/build/tests/06-Meta.test.js +103 -0
- package/build/tests/06-Meta.test.js.map +1 -0
- package/build/tests/07-Virtual-column.test.d.ts +1 -0
- package/build/tests/07-Virtual-column.test.js +160 -0
- package/build/tests/07-Virtual-column.test.js.map +1 -0
- package/build/tests/schema-spec.js +25 -25
- package/build/tests/schema-spec.js.map +1 -1
- package/package.json +1 -1
- package/build/lib/connection/index.js.map +0 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { TStoreProcedure } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* 'StoredProcedure' is a predefined set of SQL statements stored in the database that you can call (execute) by name.
|
|
5
|
+
* @generic {Type} TS
|
|
6
|
+
* @generic {Type} TR
|
|
7
|
+
* @example
|
|
8
|
+
import { StoredProcedure } from 'tspace-mysql'
|
|
9
|
+
|
|
10
|
+
type T = {
|
|
11
|
+
AddUser: {
|
|
12
|
+
params: {
|
|
13
|
+
name : string;
|
|
14
|
+
email: string;
|
|
15
|
+
} | [string,string];
|
|
16
|
+
result: {
|
|
17
|
+
fieldCount: number;
|
|
18
|
+
affectedRows: number;
|
|
19
|
+
insertId: number;
|
|
20
|
+
info: string;
|
|
21
|
+
serverStatus: number;
|
|
22
|
+
warningStatus: number;
|
|
23
|
+
changedRows: number;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
GetUser: {
|
|
27
|
+
params: [number];
|
|
28
|
+
result: any[]
|
|
29
|
+
},
|
|
30
|
+
GetUsers: {
|
|
31
|
+
params: [];
|
|
32
|
+
result: any[]
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
class MyStore extends StoredProcedure<T> {
|
|
37
|
+
protected boot(): void {
|
|
38
|
+
|
|
39
|
+
this.createProcedure({
|
|
40
|
+
name: 'AddUser',
|
|
41
|
+
expression: `
|
|
42
|
+
CREATE PROCEDURE AddUser(IN name VARCHAR(255), IN email VARCHAR(255))
|
|
43
|
+
BEGIN
|
|
44
|
+
INSERT INTO users (name, email) VALUES (name, email);
|
|
45
|
+
END;
|
|
46
|
+
`,
|
|
47
|
+
synchronize: true
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
this.createProcedure({
|
|
51
|
+
name: 'GetUsers',
|
|
52
|
+
expression: `
|
|
53
|
+
CREATE PROCEDURE GetUsers()
|
|
54
|
+
BEGIN
|
|
55
|
+
SELECT * FROM users LIMIT 5;
|
|
56
|
+
END;
|
|
57
|
+
`,
|
|
58
|
+
synchronize: true
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.createProcedure({
|
|
62
|
+
name: 'GetUser',
|
|
63
|
+
expression: `
|
|
64
|
+
CREATE PROCEDURE GetUser(IN userId INT)
|
|
65
|
+
BEGIN
|
|
66
|
+
SELECT * FROM users WHERE id = userId LIMIT 1;
|
|
67
|
+
END;
|
|
68
|
+
`,
|
|
69
|
+
synchronize: true
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const storeProcedure = new MyStoredProcedure()
|
|
75
|
+
|
|
76
|
+
storeProcedure.call('AddUser', { name : 'tspace-mysql' , email : 'tspace-mysql@example.com'})
|
|
77
|
+
.then(r => console.log(r))
|
|
78
|
+
.catch(e => console.log(e))
|
|
79
|
+
|
|
80
|
+
storeProcedure.call('GetUser',[1])
|
|
81
|
+
.then(r => console.log(r))
|
|
82
|
+
.catch(e => console.log(e))
|
|
83
|
+
|
|
84
|
+
storeProcedure.call('GetUsers',[])
|
|
85
|
+
.then(r => console.log(r))
|
|
86
|
+
.catch(e => console.log(e))
|
|
87
|
+
*/
|
|
88
|
+
declare class StoredProcedure<T extends Record<string, TStoreProcedure>> {
|
|
89
|
+
private APPLY_STORED;
|
|
90
|
+
constructor();
|
|
91
|
+
/**
|
|
92
|
+
* The 'boot' method is a special method that you can define within a store procedure.
|
|
93
|
+
* @returns {void} void
|
|
94
|
+
*/
|
|
95
|
+
protected boot(): void;
|
|
96
|
+
/**
|
|
97
|
+
* The 'createProcedure' method is used create store procedure.
|
|
98
|
+
*
|
|
99
|
+
* @param {object} object { name, expression, synchronize }
|
|
100
|
+
* @property {string?} object.name
|
|
101
|
+
* @property {string} object.expression
|
|
102
|
+
* @property {boolean} object.synchronize
|
|
103
|
+
* @returns {void}
|
|
104
|
+
*/
|
|
105
|
+
protected createProcedure({ name, expression, synchronize }: {
|
|
106
|
+
name: keyof T & string;
|
|
107
|
+
expression: string;
|
|
108
|
+
synchronize?: boolean;
|
|
109
|
+
}): void;
|
|
110
|
+
/**
|
|
111
|
+
* The 'call' method is used create store procedure.
|
|
112
|
+
*
|
|
113
|
+
* @param {string} name
|
|
114
|
+
* @param {object|array} params
|
|
115
|
+
* @returns {Promise<any>}
|
|
116
|
+
*/
|
|
117
|
+
call<K extends keyof T>(name: K, params: T[K]['params']): Promise<T[K]['result']>;
|
|
118
|
+
}
|
|
119
|
+
export { StoredProcedure };
|
|
120
|
+
export default StoredProcedure;
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.StoredProcedure = void 0;
|
|
13
|
+
const DB_1 = require("./DB");
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* 'StoredProcedure' is a predefined set of SQL statements stored in the database that you can call (execute) by name.
|
|
17
|
+
* @generic {Type} TS
|
|
18
|
+
* @generic {Type} TR
|
|
19
|
+
* @example
|
|
20
|
+
import { StoredProcedure } from 'tspace-mysql'
|
|
21
|
+
|
|
22
|
+
type T = {
|
|
23
|
+
AddUser: {
|
|
24
|
+
params: {
|
|
25
|
+
name : string;
|
|
26
|
+
email: string;
|
|
27
|
+
} | [string,string];
|
|
28
|
+
result: {
|
|
29
|
+
fieldCount: number;
|
|
30
|
+
affectedRows: number;
|
|
31
|
+
insertId: number;
|
|
32
|
+
info: string;
|
|
33
|
+
serverStatus: number;
|
|
34
|
+
warningStatus: number;
|
|
35
|
+
changedRows: number;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
GetUser: {
|
|
39
|
+
params: [number];
|
|
40
|
+
result: any[]
|
|
41
|
+
},
|
|
42
|
+
GetUsers: {
|
|
43
|
+
params: [];
|
|
44
|
+
result: any[]
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
class MyStore extends StoredProcedure<T> {
|
|
49
|
+
protected boot(): void {
|
|
50
|
+
|
|
51
|
+
this.createProcedure({
|
|
52
|
+
name: 'AddUser',
|
|
53
|
+
expression: `
|
|
54
|
+
CREATE PROCEDURE AddUser(IN name VARCHAR(255), IN email VARCHAR(255))
|
|
55
|
+
BEGIN
|
|
56
|
+
INSERT INTO users (name, email) VALUES (name, email);
|
|
57
|
+
END;
|
|
58
|
+
`,
|
|
59
|
+
synchronize: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
this.createProcedure({
|
|
63
|
+
name: 'GetUsers',
|
|
64
|
+
expression: `
|
|
65
|
+
CREATE PROCEDURE GetUsers()
|
|
66
|
+
BEGIN
|
|
67
|
+
SELECT * FROM users LIMIT 5;
|
|
68
|
+
END;
|
|
69
|
+
`,
|
|
70
|
+
synchronize: true
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
this.createProcedure({
|
|
74
|
+
name: 'GetUser',
|
|
75
|
+
expression: `
|
|
76
|
+
CREATE PROCEDURE GetUser(IN userId INT)
|
|
77
|
+
BEGIN
|
|
78
|
+
SELECT * FROM users WHERE id = userId LIMIT 1;
|
|
79
|
+
END;
|
|
80
|
+
`,
|
|
81
|
+
synchronize: true
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const storeProcedure = new MyStoredProcedure()
|
|
87
|
+
|
|
88
|
+
storeProcedure.call('AddUser', { name : 'tspace-mysql' , email : 'tspace-mysql@example.com'})
|
|
89
|
+
.then(r => console.log(r))
|
|
90
|
+
.catch(e => console.log(e))
|
|
91
|
+
|
|
92
|
+
storeProcedure.call('GetUser',[1])
|
|
93
|
+
.then(r => console.log(r))
|
|
94
|
+
.catch(e => console.log(e))
|
|
95
|
+
|
|
96
|
+
storeProcedure.call('GetUsers',[])
|
|
97
|
+
.then(r => console.log(r))
|
|
98
|
+
.catch(e => console.log(e))
|
|
99
|
+
*/
|
|
100
|
+
class StoredProcedure {
|
|
101
|
+
constructor() {
|
|
102
|
+
this.APPLY_STORED = new Map();
|
|
103
|
+
this.boot();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The 'boot' method is a special method that you can define within a store procedure.
|
|
107
|
+
* @returns {void} void
|
|
108
|
+
*/
|
|
109
|
+
boot() { }
|
|
110
|
+
/**
|
|
111
|
+
* The 'createProcedure' method is used create store procedure.
|
|
112
|
+
*
|
|
113
|
+
* @param {object} object { name, expression, synchronize }
|
|
114
|
+
* @property {string?} object.name
|
|
115
|
+
* @property {string} object.expression
|
|
116
|
+
* @property {boolean} object.synchronize
|
|
117
|
+
* @returns {void}
|
|
118
|
+
*/
|
|
119
|
+
createProcedure({ name, expression, synchronize }) {
|
|
120
|
+
const fn = () => __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
if (synchronize) {
|
|
122
|
+
yield DB_1.DB.query(`DROP PROCEDURE IF EXISTS ${name}`);
|
|
123
|
+
}
|
|
124
|
+
yield DB_1.DB.query(expression)
|
|
125
|
+
.catch(err => {
|
|
126
|
+
const message = err.message;
|
|
127
|
+
const exists = String(message).includes(`PROCEDURE ${name} already exists`);
|
|
128
|
+
if (exists)
|
|
129
|
+
return;
|
|
130
|
+
console.log(`\n\x1b[31mFAIL QUERY:\x1b[0m \x1b[33m${expression.trim()};\x1b[0m`);
|
|
131
|
+
throw err;
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
this.APPLY_STORED.set(name, fn);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The 'call' method is used create store procedure.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} name
|
|
140
|
+
* @param {object|array} params
|
|
141
|
+
* @returns {Promise<any>}
|
|
142
|
+
*/
|
|
143
|
+
call(name, params) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
const storeProcedure = this.APPLY_STORED.get(name);
|
|
146
|
+
if (!storeProcedure)
|
|
147
|
+
throw new Error(`Stored procedure "${String(name)}" not registered.`);
|
|
148
|
+
yield storeProcedure();
|
|
149
|
+
const placeholders = Object.keys(params).map(key => `:${key}`);
|
|
150
|
+
const sql = `CALL ${String(name)}(${placeholders.join(', ')})`;
|
|
151
|
+
const result = yield DB_1.DB.query(sql, params);
|
|
152
|
+
if (Array.isArray(result) && Array.isArray(result[0])) {
|
|
153
|
+
return result[0];
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
exports.StoredProcedure = StoredProcedure;
|
|
160
|
+
exports.default = StoredProcedure;
|
|
161
|
+
//# sourceMappingURL=StoredProcedure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StoredProcedure.js","sourceRoot":"","sources":["../../../src/lib/core/StoredProcedure.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAA2B;AAK3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqFG;AACH,MAAM,eAAe;IAIjB;QAFQ,iBAAY,GAAqC,IAAI,GAAG,EAAE,CAAC;QAG/D,IAAI,CAAC,IAAI,EAAE,CAAA;IAC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AI,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;YAE/D,MAAM,MAAM,GAAG,MAAM,OAAE,CAAC,KAAK,CAAC,GAAG,EAAC,MAAM,CAAC,CAAC;YAE1C,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpD,OAAO,MAAM,CAAC,CAAC,CAAmB,CAAC;YACvC,CAAC;YAED,OAAO,MAAwB,CAAC;QACpC,CAAC;KAAA;CACJ;AAEQ,0CAAe;AACxB,kBAAe,eAAe,CAAC"}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { TPagination, TRelationResults, TRepositoryRequest } from "../types";
|
|
2
1
|
import { Blueprint } from "./Blueprint";
|
|
3
2
|
import { Model } from "./Model";
|
|
4
3
|
import { Repository } from "./Repository";
|
|
4
|
+
import type { TPagination, TRelationResults, TRepositoryRequest } from "../types";
|
|
5
5
|
/**
|
|
6
6
|
* The 'TSchemaStrict' type is used to specify the type of the schema.
|
|
7
7
|
*
|
|
8
8
|
* @param {type} T typeof the schema
|
|
9
9
|
* @param {type} S override type in the schema
|
|
10
10
|
* @example
|
|
11
|
-
* import { Blueprint
|
|
11
|
+
* import { Blueprint, TSchema, Model, Schema } from 'tspace-mysql';
|
|
12
12
|
* const schemaUser = {
|
|
13
13
|
* id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
14
14
|
* uuid :new Blueprint().varchar(50).null(),
|
|
@@ -212,24 +212,6 @@ export type RelationType<R> = {
|
|
|
212
212
|
*
|
|
213
213
|
*/
|
|
214
214
|
export type TSchemaModel<M extends Model> = ReturnType<M['typeOfSchema']>;
|
|
215
|
-
/**
|
|
216
|
-
* The 'SchemaModelType' type is used to get type of schema in the model
|
|
217
|
-
* @generic {Model} M Model
|
|
218
|
-
* @example
|
|
219
|
-
* import { TSchemaModel } from 'tspace-mysql'
|
|
220
|
-
* import { User } from '../Models/User'
|
|
221
|
-
*
|
|
222
|
-
* type TSchemaModelOfUser = TSchemaModel<User>
|
|
223
|
-
* // now you can see the schema like that
|
|
224
|
-
* {
|
|
225
|
-
* id : number,
|
|
226
|
-
* uuid : string | null,
|
|
227
|
-
* name : string
|
|
228
|
-
* // ....
|
|
229
|
-
* }
|
|
230
|
-
*
|
|
231
|
-
*/
|
|
232
|
-
export type SchemaModelType<M extends Model> = ReturnType<M['typeOfSchema']>;
|
|
233
215
|
/**
|
|
234
216
|
* The 'TRelationModel' type is used to get type of schema in the model
|
|
235
217
|
* @generic {Model} M Model
|
|
@@ -247,30 +229,7 @@ export type SchemaModelType<M extends Model> = ReturnType<M['typeOfSchema']>;
|
|
|
247
229
|
* }
|
|
248
230
|
*
|
|
249
231
|
*/
|
|
250
|
-
export type RelationModelType<M extends Model> = ReturnType<M['typeOfRelation']>;
|
|
251
|
-
/**
|
|
252
|
-
* The 'RelationModelType' type is used to get type of schema in the model
|
|
253
|
-
* @generic {Model} M Model
|
|
254
|
-
* @example
|
|
255
|
-
* import { RelationModelType } from 'tspace-mysql'
|
|
256
|
-
* import { User } from '../Models/User
|
|
257
|
-
*
|
|
258
|
-
* type TRelationModelOfUser = TRelationModel<User>
|
|
259
|
-
*
|
|
260
|
-
* {
|
|
261
|
-
* phone : {
|
|
262
|
-
* id : number,
|
|
263
|
-
* // ....
|
|
264
|
-
* }
|
|
265
|
-
* }
|
|
266
|
-
*
|
|
267
|
-
*/
|
|
268
232
|
export type TRelationModel<M extends Model> = ReturnType<M['typeOfRelation']>;
|
|
269
|
-
/**
|
|
270
|
-
* The 'TRepository' type is used to get type of repository
|
|
271
|
-
* @generic {Model} M Model
|
|
272
|
-
*/
|
|
273
|
-
export type TRepository<M extends Model> = TRepositoryRequest<TSchemaModel<M>, TRelationModel<M>>;
|
|
274
233
|
/**
|
|
275
234
|
* The 'TResult' type is used to get type of result from model
|
|
276
235
|
* @generic {Model} M Model
|
|
@@ -281,11 +240,16 @@ export type TResult<M extends Model> = TRelationResults<TRelationModel<M>> & TSc
|
|
|
281
240
|
* @generic {Model} M Model
|
|
282
241
|
*/
|
|
283
242
|
export type TResultPaginate<M extends Model> = TPagination<TRelationResults<TRelationModel<M>> & TSchemaModel<M>>;
|
|
243
|
+
/**
|
|
244
|
+
* The 'TRepository' type is used to get type of repository
|
|
245
|
+
* @generic {Model} M Model
|
|
246
|
+
*/
|
|
247
|
+
export type TRepository<M extends Model> = TRepositoryRequest<TSchemaModel<M>, TRelationModel<M>>;
|
|
284
248
|
/**
|
|
285
249
|
* The 'TypeOfRepository' type is used to return typeof repository
|
|
286
250
|
* @generic {Model} M Model
|
|
287
251
|
*/
|
|
288
|
-
export type
|
|
252
|
+
export type TRepositoryTypeOf<M extends Model> = ReturnType<typeof Repository<M>>;
|
|
289
253
|
/**
|
|
290
254
|
* The 'TSchemaKeyOf' type is used to get keyof type TSchemaModel<Model>
|
|
291
255
|
* @generic {Model} M Model
|
|
@@ -295,3 +259,20 @@ export type TSchemaKeyOf<M extends Model, T = TSchemaModel<M>> = keyof {
|
|
|
295
259
|
} extends never ? string : keyof {
|
|
296
260
|
[K in keyof T as string extends K ? never : K]: T[K];
|
|
297
261
|
};
|
|
262
|
+
export declare namespace T {
|
|
263
|
+
type Schema<T, S = {}> = TSchema<T, S>;
|
|
264
|
+
type SchemaStrict<T, S = {}> = TSchemaStrict<T, S>;
|
|
265
|
+
type SchemaModel<M extends Model> = TSchemaModel<M>;
|
|
266
|
+
type KeyOf<M extends Model> = TSchemaKeyOf<M>;
|
|
267
|
+
type Column<M extends Model> = TSchemaKeyOf<M>;
|
|
268
|
+
type Relation<R> = TRelation<R>;
|
|
269
|
+
type RelationModel<M extends Model> = TRelationModel<M>;
|
|
270
|
+
type Repository<M extends Model> = TRepository<M>;
|
|
271
|
+
type RepositoryTypeOf<M extends Model> = TRepositoryTypeOf<M>;
|
|
272
|
+
type Result<M extends Model, Opts extends {
|
|
273
|
+
paginate?: boolean;
|
|
274
|
+
} = {}> = Opts['paginate'] extends true ? TResultPaginate<M> : TResult<M>;
|
|
275
|
+
type Results<M extends Model, Opts extends {
|
|
276
|
+
paginate?: boolean;
|
|
277
|
+
} = {}> = Opts['paginate'] extends true ? TResultPaginate<M> : TResult<M>;
|
|
278
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { AbstractView } from "./Abstracts/AbstractView";
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* 'View' class represents a database view, similar to a table, but based on a stored SQL query.
|
|
5
|
+
* @generic {Type} TS
|
|
6
|
+
* @generic {Type} TR
|
|
7
|
+
* @example
|
|
8
|
+
import { type T, Blueprint, Model , View , Meta } from 'tspace-mysql'
|
|
9
|
+
|
|
10
|
+
const schemaUser = {
|
|
11
|
+
id: Blueprint.int().notNull().primary().autoIncrement(),
|
|
12
|
+
uuid: Blueprint.varchar(50).null().index(),
|
|
13
|
+
name: Blueprint.varchar(191).notNull(),
|
|
14
|
+
email: Blueprint.varchar(191).notNull()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type TUser = T.Schema<typeof schemaUser>
|
|
18
|
+
|
|
19
|
+
class User extends Model<TUser> {
|
|
20
|
+
protected boot(): void {
|
|
21
|
+
this.useCamelCase()
|
|
22
|
+
this.useSchema(schemaUser)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const schemaPost = {
|
|
27
|
+
id: Blueprint.int().notNull().primary().autoIncrement(),
|
|
28
|
+
uuid: Blueprint.varchar(50).null().index(),
|
|
29
|
+
userId :Blueprint.int().notnull(),
|
|
30
|
+
title: Blueprint.varchar(191).notNull(),
|
|
31
|
+
content: Blueprint.varchar(191).notNull()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type TPost = T.Schema<typeof schemaPost>
|
|
35
|
+
|
|
36
|
+
class Post extends Model<TPost> {
|
|
37
|
+
protected boot(): void {
|
|
38
|
+
this.useSchema(schemaPost)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const schemaUserPostCountView = {
|
|
43
|
+
id :Blueprint.int().notNull().primary().autoIncrement(),
|
|
44
|
+
userId :Blueprint.int().notnull(),
|
|
45
|
+
name :Blueprint.varchar(255).null(),
|
|
46
|
+
postCount : Blueprint.int().notnull()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type TSUserPostCountView = T.Schema<typeof schemaUserPostCountView>
|
|
50
|
+
type TRUserPostCountView = T.Relation<{
|
|
51
|
+
user: User
|
|
52
|
+
}>
|
|
53
|
+
|
|
54
|
+
class UserPostCountView extends View<TSUserPostCountView,TRUserPostCountView> {
|
|
55
|
+
protected boot(): void {
|
|
56
|
+
this.useCamelCase()
|
|
57
|
+
this.useSchema(schemaUserPostCountView)
|
|
58
|
+
const metaUser = Meta(User)
|
|
59
|
+
const metaPost = Meta(Post)
|
|
60
|
+
|
|
61
|
+
this.createView({
|
|
62
|
+
synchronize: true,
|
|
63
|
+
expression : new User()
|
|
64
|
+
.selectRaw(`ROW_NUMBER() OVER (ORDER BY ${metaUser.columnRef('id')}) AS id`)
|
|
65
|
+
.selectRaw(`${metaUser.columnRef('id')} AS userId`)
|
|
66
|
+
.selectRaw(metaUser.columnRef('name'))
|
|
67
|
+
.select(metaUser.columnRef('email'))
|
|
68
|
+
.selectRaw(`COUNT(${metaPost.columnRef('id')}) AS postCount`)
|
|
69
|
+
.leftJoin(metaUser.columnRef('id'),metaPost.columnRef('userId'))
|
|
70
|
+
.groupBy(metaUser.columnRef('id'))
|
|
71
|
+
.groupBy(metaUser.columnRef('name'))
|
|
72
|
+
.toString()
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
this.belongsTo({ name : 'user' , model : User })
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
*/
|
|
79
|
+
declare class View<TS extends Record<string, any> = any, TR = unknown> extends AbstractView<TS, TR> {
|
|
80
|
+
/**
|
|
81
|
+
* The 'createView' method is used create table and stored SQL query.
|
|
82
|
+
*
|
|
83
|
+
* @param {object} object { name, expression, synchronize }
|
|
84
|
+
* @property {string?} object.name
|
|
85
|
+
* @property {string} object.expression
|
|
86
|
+
* @property {boolean} object.synchronize
|
|
87
|
+
* @returns {void}
|
|
88
|
+
*/
|
|
89
|
+
protected createView({ name, expression, synchronize }: {
|
|
90
|
+
name?: string;
|
|
91
|
+
expression: string;
|
|
92
|
+
synchronize?: boolean;
|
|
93
|
+
}): void;
|
|
94
|
+
}
|
|
95
|
+
export { View };
|
|
96
|
+
export default View;
|
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.View = void 0;
|
|
13
|
+
const DB_1 = require("./DB");
|
|
14
|
+
const AbstractView_1 = require("./Abstracts/AbstractView");
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* 'View' class represents a database view, similar to a table, but based on a stored SQL query.
|
|
18
|
+
* @generic {Type} TS
|
|
19
|
+
* @generic {Type} TR
|
|
20
|
+
* @example
|
|
21
|
+
import { type T, Blueprint, Model , View , Meta } from 'tspace-mysql'
|
|
22
|
+
|
|
23
|
+
const schemaUser = {
|
|
24
|
+
id: Blueprint.int().notNull().primary().autoIncrement(),
|
|
25
|
+
uuid: Blueprint.varchar(50).null().index(),
|
|
26
|
+
name: Blueprint.varchar(191).notNull(),
|
|
27
|
+
email: Blueprint.varchar(191).notNull()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type TUser = T.Schema<typeof schemaUser>
|
|
31
|
+
|
|
32
|
+
class User extends Model<TUser> {
|
|
33
|
+
protected boot(): void {
|
|
34
|
+
this.useCamelCase()
|
|
35
|
+
this.useSchema(schemaUser)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const schemaPost = {
|
|
40
|
+
id: Blueprint.int().notNull().primary().autoIncrement(),
|
|
41
|
+
uuid: Blueprint.varchar(50).null().index(),
|
|
42
|
+
userId :Blueprint.int().notnull(),
|
|
43
|
+
title: Blueprint.varchar(191).notNull(),
|
|
44
|
+
content: Blueprint.varchar(191).notNull()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type TPost = T.Schema<typeof schemaPost>
|
|
48
|
+
|
|
49
|
+
class Post extends Model<TPost> {
|
|
50
|
+
protected boot(): void {
|
|
51
|
+
this.useSchema(schemaPost)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const schemaUserPostCountView = {
|
|
56
|
+
id :Blueprint.int().notNull().primary().autoIncrement(),
|
|
57
|
+
userId :Blueprint.int().notnull(),
|
|
58
|
+
name :Blueprint.varchar(255).null(),
|
|
59
|
+
postCount : Blueprint.int().notnull()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type TSUserPostCountView = T.Schema<typeof schemaUserPostCountView>
|
|
63
|
+
type TRUserPostCountView = T.Relation<{
|
|
64
|
+
user: User
|
|
65
|
+
}>
|
|
66
|
+
|
|
67
|
+
class UserPostCountView extends View<TSUserPostCountView,TRUserPostCountView> {
|
|
68
|
+
protected boot(): void {
|
|
69
|
+
this.useCamelCase()
|
|
70
|
+
this.useSchema(schemaUserPostCountView)
|
|
71
|
+
const metaUser = Meta(User)
|
|
72
|
+
const metaPost = Meta(Post)
|
|
73
|
+
|
|
74
|
+
this.createView({
|
|
75
|
+
synchronize: true,
|
|
76
|
+
expression : new User()
|
|
77
|
+
.selectRaw(`ROW_NUMBER() OVER (ORDER BY ${metaUser.columnRef('id')}) AS id`)
|
|
78
|
+
.selectRaw(`${metaUser.columnRef('id')} AS userId`)
|
|
79
|
+
.selectRaw(metaUser.columnRef('name'))
|
|
80
|
+
.select(metaUser.columnRef('email'))
|
|
81
|
+
.selectRaw(`COUNT(${metaPost.columnRef('id')}) AS postCount`)
|
|
82
|
+
.leftJoin(metaUser.columnRef('id'),metaPost.columnRef('userId'))
|
|
83
|
+
.groupBy(metaUser.columnRef('id'))
|
|
84
|
+
.groupBy(metaUser.columnRef('name'))
|
|
85
|
+
.toString()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
this.belongsTo({ name : 'user' , model : User })
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
*/
|
|
92
|
+
class View extends AbstractView_1.AbstractView {
|
|
93
|
+
/**
|
|
94
|
+
* The 'createView' method is used create table and stored SQL query.
|
|
95
|
+
*
|
|
96
|
+
* @param {object} object { name, expression, synchronize }
|
|
97
|
+
* @property {string?} object.name
|
|
98
|
+
* @property {string} object.expression
|
|
99
|
+
* @property {boolean} object.synchronize
|
|
100
|
+
* @returns {void}
|
|
101
|
+
*/
|
|
102
|
+
createView({ name, expression, synchronize }) {
|
|
103
|
+
const baseViewName = this._classToTableName();
|
|
104
|
+
const patternViewName = this._valuePattern(baseViewName);
|
|
105
|
+
const viewName = name !== null && name !== void 0 ? name : patternViewName;
|
|
106
|
+
let sql = expression;
|
|
107
|
+
const fn = () => __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
try {
|
|
109
|
+
if (synchronize) {
|
|
110
|
+
const dropViewSQL = `${this.$constants('DROP_VIEW')} ${viewName} `;
|
|
111
|
+
yield DB_1.DB
|
|
112
|
+
.query(dropViewSQL)
|
|
113
|
+
.catch(err => {
|
|
114
|
+
sql = dropViewSQL;
|
|
115
|
+
throw err;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const createViewSQL = `${this.$constants('CREATE_VIEW')} ${viewName} ${this.$constants('AS')} ${expression}`;
|
|
119
|
+
yield DB_1.DB
|
|
120
|
+
.query(createViewSQL)
|
|
121
|
+
.catch(err => {
|
|
122
|
+
sql = createViewSQL;
|
|
123
|
+
throw err;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
const message = err.message;
|
|
128
|
+
const exists = String(message).includes(`Table '${viewName}' already exists`);
|
|
129
|
+
if (exists)
|
|
130
|
+
return;
|
|
131
|
+
console.log(`\n\x1b[31mFAIL QUERY:\x1b[0m \x1b[33m${sql.trim()};\x1b[0m`);
|
|
132
|
+
throw err;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
this.table(viewName);
|
|
136
|
+
this.useMiddleware(fn);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.View = View;
|
|
140
|
+
exports.default = View;
|
|
141
|
+
//# sourceMappingURL=View.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"View.js","sourceRoot":"","sources":["../../../src/lib/core/View.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAAoC;AACpC,2DAAwD;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4EG;AACH,MAAM,IAAwD,SAAQ,2BAAmB;IAErF;;;;;;;;OAQG;IACO,UAAU,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAIlD;QACE,MAAM,YAAY,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC9C,MAAM,eAAe,GAAG,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;QACzD,MAAM,QAAQ,GAAG,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,eAAe,CAAC;QACzC,IAAI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 +1,25 @@
|
|
|
1
|
-
import DB from
|
|
2
|
-
import Model from
|
|
3
|
-
import Schema from
|
|
4
|
-
import Blueprint from
|
|
5
|
-
import Pool from
|
|
6
|
-
import sql from
|
|
1
|
+
import DB from "./DB";
|
|
2
|
+
import Model from "./Model";
|
|
3
|
+
import Schema from "./Schema";
|
|
4
|
+
import Blueprint from "./Blueprint";
|
|
5
|
+
import Pool from "./Pool";
|
|
6
|
+
import sql from "./SqlLike";
|
|
7
|
+
import Meta from "./Meta";
|
|
8
|
+
import View from './View';
|
|
9
|
+
export { View };
|
|
10
|
+
export { Meta };
|
|
7
11
|
export { sql };
|
|
8
12
|
export { DB };
|
|
9
13
|
export { Model };
|
|
10
14
|
export { Blueprint };
|
|
11
15
|
export { Pool };
|
|
12
|
-
export * from
|
|
13
|
-
export * from
|
|
14
|
-
export * from
|
|
15
|
-
export * from
|
|
16
|
-
export * from
|
|
17
|
-
export * from
|
|
16
|
+
export * from "./Decorator";
|
|
17
|
+
export * from "./Schema";
|
|
18
|
+
export * from "./UtilityTypes";
|
|
19
|
+
export * from "./Repository";
|
|
20
|
+
export * from "./Operator";
|
|
21
|
+
export * from "./Nest";
|
|
22
|
+
export * from './StoredProcedure';
|
|
18
23
|
declare const _default: {
|
|
19
24
|
DB: typeof DB;
|
|
20
25
|
Model: typeof Model;
|