tspace-mysql 1.3.8 → 1.4.0
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 +50 -40
- package/dist/cli/dump/db.js +23 -10
- package/dist/cli/dump/table.d.ts +4 -0
- package/dist/cli/dump/table.js +38 -0
- package/dist/cli/generate/make.js +27 -10
- package/dist/cli/generate/model.d.ts +1 -1
- package/dist/cli/generate/model.js +23 -16
- package/dist/cli/index.js +21 -5
- package/dist/cli/migrate/make.d.ts +1 -1
- package/dist/cli/migrate/make.js +2 -2
- package/dist/cli/models/make.js +2 -2
- package/dist/cli/models/model.js +1 -14
- package/dist/cli/query/index.js +4 -1
- package/dist/lib/connection/options.js +16 -15
- package/dist/lib/constants/index.js +6 -2
- package/dist/lib/tspace/Abstract/AbstractDB.d.ts +1 -1
- package/dist/lib/tspace/Blueprint.d.ts +21 -16
- package/dist/lib/tspace/Blueprint.js +44 -27
- package/dist/lib/tspace/Builder.d.ts +52 -2
- package/dist/lib/tspace/Builder.js +152 -3
- package/dist/lib/tspace/DB.d.ts +12 -12
- package/dist/lib/tspace/DB.js +42 -29
- package/dist/lib/tspace/Interface.d.ts +12 -0
- package/dist/lib/tspace/Model.d.ts +18 -10
- package/dist/lib/tspace/Model.js +85 -74
- package/dist/lib/tspace/Schema.d.ts +1 -1
- package/dist/lib/tspace/Schema.js +7 -7
- package/package.json +1 -1
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @example
|
|
4
4
|
* import { Schema , Blueprint } from 'tspace-mysql'
|
|
5
5
|
* await new Schema().table('persos1',{
|
|
6
|
-
* id
|
|
7
|
-
* name
|
|
8
|
-
* email
|
|
9
|
-
* verify
|
|
6
|
+
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
7
|
+
* name : new Blueprint().varchar(255).default('my name ...'),
|
|
8
|
+
* email : new Blueprint().varchar(255).unique(),
|
|
9
|
+
* verify : new Blueprint().tinyInt(1).notNull(),
|
|
10
10
|
* created_at : new Blueprint().timestamp().null(),
|
|
11
11
|
* updated_at : new Blueprint().timestamp().null(),
|
|
12
12
|
* deleted_at : new Blueprint().timestamp().null()
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
*/
|
|
15
15
|
declare class Blueprint {
|
|
16
16
|
protected type: string;
|
|
17
|
-
protected
|
|
17
|
+
protected attributes: Array<string>;
|
|
18
|
+
protected valueType: NumberConstructor | StringConstructor | DateConstructor;
|
|
18
19
|
/**
|
|
19
20
|
* Assign type 'int' in table
|
|
20
21
|
* @return {this} this
|
|
@@ -48,7 +49,7 @@ declare class Blueprint {
|
|
|
48
49
|
float(length?: number, decimal?: number): this;
|
|
49
50
|
/**
|
|
50
51
|
* Assign type 'VARCHAR' in table
|
|
51
|
-
* @param {number} length [length =
|
|
52
|
+
* @param {number} length [length = 191] length of string
|
|
52
53
|
* @return {this} this
|
|
53
54
|
*/
|
|
54
55
|
varchar(length?: number): this;
|
|
@@ -65,7 +66,6 @@ declare class Blueprint {
|
|
|
65
66
|
longText(): this;
|
|
66
67
|
/**
|
|
67
68
|
* Assign type 'MEDIUMTEXT' in table
|
|
68
|
-
* @param {number} length [length = 1] length of string
|
|
69
69
|
* @return {this} this
|
|
70
70
|
*/
|
|
71
71
|
mediumText(): this;
|
|
@@ -97,54 +97,59 @@ declare class Blueprint {
|
|
|
97
97
|
* @return {this} this
|
|
98
98
|
*/
|
|
99
99
|
dateTime(): this;
|
|
100
|
+
/**
|
|
101
|
+
* Assign type 'DATETIME' in table
|
|
102
|
+
* @return {this} this
|
|
103
|
+
*/
|
|
104
|
+
datetime(): this;
|
|
100
105
|
/**
|
|
101
106
|
* Assign type 'TIMESTAMP' in table
|
|
102
107
|
* @return {this} this
|
|
103
108
|
*/
|
|
104
109
|
timestamp(): this;
|
|
105
110
|
/**
|
|
106
|
-
* Assign
|
|
111
|
+
* Assign attributes 'UNSIGNED' in table
|
|
107
112
|
* @return {this} this
|
|
108
113
|
*/
|
|
109
114
|
unsigned(): this;
|
|
110
115
|
/**
|
|
111
|
-
* Assign
|
|
116
|
+
* Assign attributes 'UNIQUE' in table
|
|
112
117
|
* @return {this} this
|
|
113
118
|
*/
|
|
114
119
|
unique(): this;
|
|
115
120
|
/**
|
|
116
|
-
* Assign
|
|
121
|
+
* Assign attributes 'NULL' in table
|
|
117
122
|
* @return {this} this
|
|
118
123
|
*/
|
|
119
124
|
null(): this;
|
|
120
125
|
/**
|
|
121
|
-
* Assign
|
|
126
|
+
* Assign attributes 'NOT NULL' in table
|
|
122
127
|
* @return {this} this
|
|
123
128
|
*/
|
|
124
129
|
notNull(): this;
|
|
125
130
|
/**
|
|
126
|
-
* Assign
|
|
131
|
+
* Assign attributes 'PRIMARY KEY' in table
|
|
127
132
|
* @return {this} this
|
|
128
133
|
*/
|
|
129
134
|
primary(): this;
|
|
130
135
|
/**
|
|
131
|
-
* Assign
|
|
136
|
+
* Assign attributes 'default' in table
|
|
132
137
|
* @param {string | number} value default value
|
|
133
138
|
* @return {this} this
|
|
134
139
|
*/
|
|
135
140
|
default(value: string | number): this;
|
|
136
141
|
/**
|
|
137
|
-
* Assign
|
|
142
|
+
* Assign attributes 'default currentTimestamp' in table
|
|
138
143
|
* @return {this} this
|
|
139
144
|
*/
|
|
140
145
|
currentTimestamp(): this;
|
|
141
146
|
/**
|
|
142
|
-
* Assign
|
|
147
|
+
* Assign attributes 'autoIncrement' in table
|
|
143
148
|
* @return {this} this
|
|
144
149
|
*/
|
|
145
150
|
autoIncrement(): this;
|
|
146
151
|
private _addAssignType;
|
|
147
|
-
private
|
|
152
|
+
private _addAssignAttribute;
|
|
148
153
|
}
|
|
149
154
|
export { Blueprint };
|
|
150
155
|
export default Blueprint;
|
|
@@ -6,10 +6,10 @@ exports.Blueprint = void 0;
|
|
|
6
6
|
* @example
|
|
7
7
|
* import { Schema , Blueprint } from 'tspace-mysql'
|
|
8
8
|
* await new Schema().table('persos1',{
|
|
9
|
-
* id
|
|
10
|
-
* name
|
|
11
|
-
* email
|
|
12
|
-
* verify
|
|
9
|
+
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
10
|
+
* name : new Blueprint().varchar(255).default('my name ...'),
|
|
11
|
+
* email : new Blueprint().varchar(255).unique(),
|
|
12
|
+
* verify : new Blueprint().tinyInt(1).notNull(),
|
|
13
13
|
* created_at : new Blueprint().timestamp().null(),
|
|
14
14
|
* updated_at : new Blueprint().timestamp().null(),
|
|
15
15
|
* deleted_at : new Blueprint().timestamp().null()
|
|
@@ -18,7 +18,8 @@ exports.Blueprint = void 0;
|
|
|
18
18
|
class Blueprint {
|
|
19
19
|
constructor() {
|
|
20
20
|
this.type = '';
|
|
21
|
-
this.
|
|
21
|
+
this.attributes = [];
|
|
22
|
+
this.valueType = String;
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
25
|
* Assign type 'int' in table
|
|
@@ -26,6 +27,7 @@ class Blueprint {
|
|
|
26
27
|
*/
|
|
27
28
|
int() {
|
|
28
29
|
this._addAssignType('INT');
|
|
30
|
+
this.valueType = Number;
|
|
29
31
|
return this;
|
|
30
32
|
}
|
|
31
33
|
/**
|
|
@@ -35,6 +37,7 @@ class Blueprint {
|
|
|
35
37
|
*/
|
|
36
38
|
tinyInt(number = 1) {
|
|
37
39
|
this._addAssignType(`TINYINT(${number})`);
|
|
40
|
+
this.valueType = Number;
|
|
38
41
|
return this;
|
|
39
42
|
}
|
|
40
43
|
/**
|
|
@@ -44,6 +47,7 @@ class Blueprint {
|
|
|
44
47
|
*/
|
|
45
48
|
bigInt(number = 10) {
|
|
46
49
|
this._addAssignType(`BIGINT(${number})`);
|
|
50
|
+
this.valueType = Number;
|
|
47
51
|
return this;
|
|
48
52
|
}
|
|
49
53
|
/**
|
|
@@ -53,6 +57,7 @@ class Blueprint {
|
|
|
53
57
|
* @return {this} this
|
|
54
58
|
*/
|
|
55
59
|
double(length = 0, decimal = 0) {
|
|
60
|
+
this.valueType = Number;
|
|
56
61
|
if (!length || !decimal) {
|
|
57
62
|
this._addAssignType(`DOUBLE`);
|
|
58
63
|
return this;
|
|
@@ -67,6 +72,7 @@ class Blueprint {
|
|
|
67
72
|
* @return {this} this
|
|
68
73
|
*/
|
|
69
74
|
float(length = 0, decimal = 0) {
|
|
75
|
+
this.valueType = Number;
|
|
70
76
|
if (!length || !decimal) {
|
|
71
77
|
this._addAssignType(`FLOAT`);
|
|
72
78
|
return this;
|
|
@@ -76,10 +82,10 @@ class Blueprint {
|
|
|
76
82
|
}
|
|
77
83
|
/**
|
|
78
84
|
* Assign type 'VARCHAR' in table
|
|
79
|
-
* @param {number} length [length =
|
|
85
|
+
* @param {number} length [length = 191] length of string
|
|
80
86
|
* @return {this} this
|
|
81
87
|
*/
|
|
82
|
-
varchar(length =
|
|
88
|
+
varchar(length = 191) {
|
|
83
89
|
if (length > 255)
|
|
84
90
|
length = 255;
|
|
85
91
|
this._addAssignType(`VARCHAR(${length})`);
|
|
@@ -104,7 +110,6 @@ class Blueprint {
|
|
|
104
110
|
}
|
|
105
111
|
/**
|
|
106
112
|
* Assign type 'MEDIUMTEXT' in table
|
|
107
|
-
* @param {number} length [length = 1] length of string
|
|
108
113
|
* @return {this} this
|
|
109
114
|
*/
|
|
110
115
|
mediumText() {
|
|
@@ -135,7 +140,7 @@ class Blueprint {
|
|
|
135
140
|
* @return {this} this
|
|
136
141
|
*/
|
|
137
142
|
enum(...enums) {
|
|
138
|
-
this._addAssignType(`ENUM('${
|
|
143
|
+
this._addAssignType(`ENUM(${enums.map(e => `'${e.replace(/'/g, '')}'`)})`);
|
|
139
144
|
return this;
|
|
140
145
|
}
|
|
141
146
|
/**
|
|
@@ -144,6 +149,7 @@ class Blueprint {
|
|
|
144
149
|
*/
|
|
145
150
|
date() {
|
|
146
151
|
this._addAssignType(`DATE`);
|
|
152
|
+
this.valueType = Date;
|
|
147
153
|
return this;
|
|
148
154
|
}
|
|
149
155
|
/**
|
|
@@ -152,6 +158,16 @@ class Blueprint {
|
|
|
152
158
|
*/
|
|
153
159
|
dateTime() {
|
|
154
160
|
this._addAssignType(`DATETIME`);
|
|
161
|
+
this.valueType = Date;
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Assign type 'DATETIME' in table
|
|
166
|
+
* @return {this} this
|
|
167
|
+
*/
|
|
168
|
+
datetime() {
|
|
169
|
+
this._addAssignType(`DATETIME`);
|
|
170
|
+
this.valueType = Date;
|
|
155
171
|
return this;
|
|
156
172
|
}
|
|
157
173
|
/**
|
|
@@ -160,71 +176,72 @@ class Blueprint {
|
|
|
160
176
|
*/
|
|
161
177
|
timestamp() {
|
|
162
178
|
this._addAssignType(`TIMESTAMP`);
|
|
179
|
+
this.valueType = Date;
|
|
163
180
|
return this;
|
|
164
181
|
}
|
|
165
182
|
/**
|
|
166
|
-
* Assign
|
|
183
|
+
* Assign attributes 'UNSIGNED' in table
|
|
167
184
|
* @return {this} this
|
|
168
185
|
*/
|
|
169
186
|
unsigned() {
|
|
170
|
-
this.
|
|
187
|
+
this._addAssignAttribute(`UNSIGNED`);
|
|
171
188
|
return this;
|
|
172
189
|
}
|
|
173
190
|
/**
|
|
174
|
-
* Assign
|
|
191
|
+
* Assign attributes 'UNIQUE' in table
|
|
175
192
|
* @return {this} this
|
|
176
193
|
*/
|
|
177
194
|
unique() {
|
|
178
|
-
this.
|
|
195
|
+
this._addAssignAttribute(`UNIQUE`);
|
|
179
196
|
return this;
|
|
180
197
|
}
|
|
181
198
|
/**
|
|
182
|
-
* Assign
|
|
199
|
+
* Assign attributes 'NULL' in table
|
|
183
200
|
* @return {this} this
|
|
184
201
|
*/
|
|
185
202
|
null() {
|
|
186
|
-
this.
|
|
203
|
+
this._addAssignAttribute(`NULL`);
|
|
187
204
|
return this;
|
|
188
205
|
}
|
|
189
206
|
/**
|
|
190
|
-
* Assign
|
|
207
|
+
* Assign attributes 'NOT NULL' in table
|
|
191
208
|
* @return {this} this
|
|
192
209
|
*/
|
|
193
210
|
notNull() {
|
|
194
|
-
this.
|
|
211
|
+
this._addAssignAttribute(`NOT NULL`);
|
|
195
212
|
return this;
|
|
196
213
|
}
|
|
197
214
|
/**
|
|
198
|
-
* Assign
|
|
215
|
+
* Assign attributes 'PRIMARY KEY' in table
|
|
199
216
|
* @return {this} this
|
|
200
217
|
*/
|
|
201
218
|
primary() {
|
|
202
|
-
this.
|
|
219
|
+
this._addAssignAttribute(`PRIMARY KEY`);
|
|
203
220
|
return this;
|
|
204
221
|
}
|
|
205
222
|
/**
|
|
206
|
-
* Assign
|
|
223
|
+
* Assign attributes 'default' in table
|
|
207
224
|
* @param {string | number} value default value
|
|
208
225
|
* @return {this} this
|
|
209
226
|
*/
|
|
210
227
|
default(value) {
|
|
211
|
-
this.
|
|
228
|
+
this._addAssignAttribute(`DEFAULT '${value}'`);
|
|
212
229
|
return this;
|
|
213
230
|
}
|
|
214
231
|
/**
|
|
215
|
-
* Assign
|
|
232
|
+
* Assign attributes 'default currentTimestamp' in table
|
|
216
233
|
* @return {this} this
|
|
217
234
|
*/
|
|
218
235
|
currentTimestamp() {
|
|
219
|
-
this.
|
|
236
|
+
this._addAssignAttribute(`DEFAULT CURRENT_TIMESTAMP`);
|
|
220
237
|
return this;
|
|
221
238
|
}
|
|
222
239
|
/**
|
|
223
|
-
* Assign
|
|
240
|
+
* Assign attributes 'autoIncrement' in table
|
|
224
241
|
* @return {this} this
|
|
225
242
|
*/
|
|
226
243
|
autoIncrement() {
|
|
227
|
-
this.
|
|
244
|
+
this._addAssignAttribute(`AUTO_INCREMENT`);
|
|
228
245
|
return this;
|
|
229
246
|
}
|
|
230
247
|
_addAssignType(type) {
|
|
@@ -233,8 +250,8 @@ class Blueprint {
|
|
|
233
250
|
this.type = type;
|
|
234
251
|
return this;
|
|
235
252
|
}
|
|
236
|
-
|
|
237
|
-
this.
|
|
253
|
+
_addAssignAttribute(Attribute) {
|
|
254
|
+
this.attributes = [...this.attributes, Attribute];
|
|
238
255
|
return this;
|
|
239
256
|
}
|
|
240
257
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbstractBuilder } from './Abstract/AbstractBuilder';
|
|
2
|
-
import { Pagination, Backup, ConnectionOptions, BackupToFile, Connection, ConnectionTransaction } from './Interface';
|
|
2
|
+
import { Pagination, Backup, ConnectionOptions, BackupToFile, Connection, ConnectionTransaction, BackupTableToFile } from './Interface';
|
|
3
3
|
declare class Builder extends AbstractBuilder {
|
|
4
4
|
constructor();
|
|
5
5
|
/**
|
|
@@ -783,6 +783,12 @@ declare class Builder extends AbstractBuilder {
|
|
|
783
783
|
* @return {Promise<Array>}
|
|
784
784
|
*/
|
|
785
785
|
showSchemas(table?: string): Promise<Array<string>>;
|
|
786
|
+
/**
|
|
787
|
+
*
|
|
788
|
+
* get schema from table
|
|
789
|
+
* @return {this} this this
|
|
790
|
+
*/
|
|
791
|
+
getSchema(): Promise<any>;
|
|
786
792
|
/**
|
|
787
793
|
*
|
|
788
794
|
* show values in table
|
|
@@ -817,10 +823,54 @@ declare class Builder extends AbstractBuilder {
|
|
|
817
823
|
* @param {string} backup.connection.database
|
|
818
824
|
* @param {string} backup.connection.username
|
|
819
825
|
* @param {string} backup.connection.password
|
|
820
|
-
|
|
821
826
|
* @return {Promise<boolean>}
|
|
822
827
|
*/
|
|
823
828
|
backupToFile({ filePath, database, connection }: BackupToFile): Promise<void>;
|
|
829
|
+
/**
|
|
830
|
+
*
|
|
831
|
+
* backup database intro file
|
|
832
|
+
* @param {Object} backupOptions
|
|
833
|
+
* @param {string} backup.database
|
|
834
|
+
* @param {object?} backup.filePath
|
|
835
|
+
* @param {object?} backup.connection
|
|
836
|
+
* @param {string} backup.connection.host
|
|
837
|
+
* @param {number} backup.connection.port
|
|
838
|
+
* @param {string} backup.connection.database
|
|
839
|
+
* @param {string} backup.connection.username
|
|
840
|
+
* @param {string} backup.connection.password
|
|
841
|
+
* @return {Promise<boolean>}
|
|
842
|
+
*/
|
|
843
|
+
backupSchemaToFile({ filePath, database, connection }: BackupToFile): Promise<void>;
|
|
844
|
+
/**
|
|
845
|
+
*
|
|
846
|
+
* backup table intro file
|
|
847
|
+
* @param {Object} backupOptions
|
|
848
|
+
* @param {string} backup.table
|
|
849
|
+
* @param {object?} backup.filePath
|
|
850
|
+
* @param {object?} backup.connection
|
|
851
|
+
* @param {string} backup.connection.host
|
|
852
|
+
* @param {number} backup.connection.port
|
|
853
|
+
* @param {string} backup.connection.database
|
|
854
|
+
* @param {string} backup.connection.username
|
|
855
|
+
* @param {string} backup.connection.password
|
|
856
|
+
* @return {Promise<boolean>}
|
|
857
|
+
*/
|
|
858
|
+
backupTableToFile({ filePath, table, connection }: BackupTableToFile): Promise<void>;
|
|
859
|
+
/**
|
|
860
|
+
*
|
|
861
|
+
* backup table only schema intro file
|
|
862
|
+
* @param {Object} backupOptions
|
|
863
|
+
* @param {string} backup.table
|
|
864
|
+
* @param {object?} backup.filePath
|
|
865
|
+
* @param {object?} backup.connection
|
|
866
|
+
* @param {string} backup.connection.host
|
|
867
|
+
* @param {number} backup.connection.port
|
|
868
|
+
* @param {string} backup.connection.database
|
|
869
|
+
* @param {string} backup.connection.username
|
|
870
|
+
* @param {string} backup.connection.password
|
|
871
|
+
* @return {Promise<boolean>}
|
|
872
|
+
*/
|
|
873
|
+
backupTableSchemaToFile({ filePath, table, connection }: BackupTableToFile): Promise<void>;
|
|
824
874
|
/**
|
|
825
875
|
*
|
|
826
876
|
* fake data
|
|
@@ -2077,6 +2077,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2077
2077
|
});
|
|
2078
2078
|
});
|
|
2079
2079
|
}
|
|
2080
|
+
/**
|
|
2081
|
+
*
|
|
2082
|
+
* get schema from table
|
|
2083
|
+
* @return {this} this this
|
|
2084
|
+
*/
|
|
2085
|
+
getSchema() {
|
|
2086
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2087
|
+
const sql = [
|
|
2088
|
+
`${this.$constants('SHOW')}`,
|
|
2089
|
+
`${this.$constants('COLUMNS')}`,
|
|
2090
|
+
`${this.$constants('FROM')}`,
|
|
2091
|
+
`\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
|
|
2092
|
+
].join(' ');
|
|
2093
|
+
return yield this.queryStatement(sql);
|
|
2094
|
+
});
|
|
2095
|
+
}
|
|
2080
2096
|
/**
|
|
2081
2097
|
*
|
|
2082
2098
|
* show values in table
|
|
@@ -2170,7 +2186,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2170
2186
|
* @param {string} backup.connection.database
|
|
2171
2187
|
* @param {string} backup.connection.username
|
|
2172
2188
|
* @param {string} backup.connection.password
|
|
2173
|
-
|
|
2174
2189
|
* @return {Promise<boolean>}
|
|
2175
2190
|
*/
|
|
2176
2191
|
backupToFile({ filePath, database, connection }) {
|
|
@@ -2226,8 +2241,142 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2226
2241
|
tabWidth: 2,
|
|
2227
2242
|
linesBetweenQueries: 1,
|
|
2228
2243
|
}));
|
|
2229
|
-
|
|
2230
|
-
|
|
2244
|
+
return;
|
|
2245
|
+
});
|
|
2246
|
+
}
|
|
2247
|
+
/**
|
|
2248
|
+
*
|
|
2249
|
+
* backup database intro file
|
|
2250
|
+
* @param {Object} backupOptions
|
|
2251
|
+
* @param {string} backup.database
|
|
2252
|
+
* @param {object?} backup.filePath
|
|
2253
|
+
* @param {object?} backup.connection
|
|
2254
|
+
* @param {string} backup.connection.host
|
|
2255
|
+
* @param {number} backup.connection.port
|
|
2256
|
+
* @param {string} backup.connection.database
|
|
2257
|
+
* @param {string} backup.connection.username
|
|
2258
|
+
* @param {string} backup.connection.password
|
|
2259
|
+
* @return {Promise<boolean>}
|
|
2260
|
+
*/
|
|
2261
|
+
backupSchemaToFile({ filePath, database, connection }) {
|
|
2262
|
+
var _a, _b;
|
|
2263
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2264
|
+
const tables = yield this.queryStatement(this.$constants('SHOW_TABLES'));
|
|
2265
|
+
let backup = [];
|
|
2266
|
+
for (const t of tables) {
|
|
2267
|
+
const table = String((_a = Object.values(t)) === null || _a === void 0 ? void 0 : _a.shift());
|
|
2268
|
+
const schemas = yield this.showSchemas(table);
|
|
2269
|
+
const createTableSQL = [
|
|
2270
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
2271
|
+
`\`${table}\``,
|
|
2272
|
+
`(${schemas.join(',')})`,
|
|
2273
|
+
`${this.$constants('ENGINE')};`,
|
|
2274
|
+
];
|
|
2275
|
+
backup = [
|
|
2276
|
+
...backup,
|
|
2277
|
+
{
|
|
2278
|
+
table: createTableSQL.join(' ')
|
|
2279
|
+
}
|
|
2280
|
+
];
|
|
2281
|
+
}
|
|
2282
|
+
if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
|
|
2283
|
+
this.connection(connection);
|
|
2284
|
+
let sql = [
|
|
2285
|
+
`SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";`,
|
|
2286
|
+
`START TRANSACTION;`,
|
|
2287
|
+
`SET time_zone = "+00:00";`,
|
|
2288
|
+
`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\`;`,
|
|
2289
|
+
`USE \`${database}\`;`
|
|
2290
|
+
];
|
|
2291
|
+
for (const b of backup) {
|
|
2292
|
+
sql = [...sql, b.table];
|
|
2293
|
+
}
|
|
2294
|
+
fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)([...sql, 'COMMIT;'].join('\n'), {
|
|
2295
|
+
language: 'spark',
|
|
2296
|
+
tabWidth: 2,
|
|
2297
|
+
linesBetweenQueries: 1,
|
|
2298
|
+
}));
|
|
2299
|
+
return;
|
|
2300
|
+
});
|
|
2301
|
+
}
|
|
2302
|
+
/**
|
|
2303
|
+
*
|
|
2304
|
+
* backup table intro file
|
|
2305
|
+
* @param {Object} backupOptions
|
|
2306
|
+
* @param {string} backup.table
|
|
2307
|
+
* @param {object?} backup.filePath
|
|
2308
|
+
* @param {object?} backup.connection
|
|
2309
|
+
* @param {string} backup.connection.host
|
|
2310
|
+
* @param {number} backup.connection.port
|
|
2311
|
+
* @param {string} backup.connection.database
|
|
2312
|
+
* @param {string} backup.connection.username
|
|
2313
|
+
* @param {string} backup.connection.password
|
|
2314
|
+
* @return {Promise<boolean>}
|
|
2315
|
+
*/
|
|
2316
|
+
backupTableToFile({ filePath, table, connection }) {
|
|
2317
|
+
var _a;
|
|
2318
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2319
|
+
const schemas = yield this.showSchemas(table);
|
|
2320
|
+
const createTableSQL = [
|
|
2321
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
2322
|
+
`\`${table}\``,
|
|
2323
|
+
`(${schemas.join(',')})`,
|
|
2324
|
+
`${this.$constants('ENGINE')};`,
|
|
2325
|
+
];
|
|
2326
|
+
const values = yield this.showValues(table);
|
|
2327
|
+
let valueSQL = [];
|
|
2328
|
+
if (values.length) {
|
|
2329
|
+
const columns = yield this.showColumns(table);
|
|
2330
|
+
valueSQL = [
|
|
2331
|
+
`${this.$constants('INSERT')}`,
|
|
2332
|
+
`\`${table}\``,
|
|
2333
|
+
`(${columns.map((column) => `\`${column}\``).join(',')})`,
|
|
2334
|
+
`${this.$constants('VALUES')} ${values.join(',')};`
|
|
2335
|
+
];
|
|
2336
|
+
}
|
|
2337
|
+
const sql = [createTableSQL.join(' '), valueSQL.join(' ')];
|
|
2338
|
+
if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
|
|
2339
|
+
this.connection(connection);
|
|
2340
|
+
fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)(sql.join('\n'), {
|
|
2341
|
+
language: 'spark',
|
|
2342
|
+
tabWidth: 2,
|
|
2343
|
+
linesBetweenQueries: 1,
|
|
2344
|
+
}));
|
|
2345
|
+
return;
|
|
2346
|
+
});
|
|
2347
|
+
}
|
|
2348
|
+
/**
|
|
2349
|
+
*
|
|
2350
|
+
* backup table only schema intro file
|
|
2351
|
+
* @param {Object} backupOptions
|
|
2352
|
+
* @param {string} backup.table
|
|
2353
|
+
* @param {object?} backup.filePath
|
|
2354
|
+
* @param {object?} backup.connection
|
|
2355
|
+
* @param {string} backup.connection.host
|
|
2356
|
+
* @param {number} backup.connection.port
|
|
2357
|
+
* @param {string} backup.connection.database
|
|
2358
|
+
* @param {string} backup.connection.username
|
|
2359
|
+
* @param {string} backup.connection.password
|
|
2360
|
+
* @return {Promise<boolean>}
|
|
2361
|
+
*/
|
|
2362
|
+
backupTableSchemaToFile({ filePath, table, connection }) {
|
|
2363
|
+
var _a;
|
|
2364
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2365
|
+
const schemas = yield this.showSchemas(table);
|
|
2366
|
+
const createTableSQL = [
|
|
2367
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
2368
|
+
`\`${table}\``,
|
|
2369
|
+
`(${schemas.join(',')})`,
|
|
2370
|
+
`${this.$constants('ENGINE')};`,
|
|
2371
|
+
];
|
|
2372
|
+
const sql = [createTableSQL.join(' ')];
|
|
2373
|
+
if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
|
|
2374
|
+
this.connection(connection);
|
|
2375
|
+
fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)(sql.join('\n'), {
|
|
2376
|
+
language: 'spark',
|
|
2377
|
+
tabWidth: 2,
|
|
2378
|
+
linesBetweenQueries: 1,
|
|
2379
|
+
}));
|
|
2231
2380
|
return;
|
|
2232
2381
|
});
|
|
2233
2382
|
}
|
package/dist/lib/tspace/DB.d.ts
CHANGED
|
@@ -63,7 +63,17 @@ declare class DB extends AbstractDB {
|
|
|
63
63
|
* @param {string} option.password
|
|
64
64
|
* @return {Connection}
|
|
65
65
|
*/
|
|
66
|
-
getConnection(options
|
|
66
|
+
getConnection(options?: ConnectionOptions): Promise<Connection>;
|
|
67
|
+
/**
|
|
68
|
+
* Get a connection
|
|
69
|
+
* @return {ConnectionTransaction} object - Connection for the transaction
|
|
70
|
+
* @type {object} connection
|
|
71
|
+
* @property {function} connection.query - execute query sql then release connection to pool
|
|
72
|
+
* @property {function} connection.startTransaction - start transaction of query
|
|
73
|
+
* @property {function} connection.commit - commit transaction of query
|
|
74
|
+
* @property {function} connection.rollback - rollback transaction of query
|
|
75
|
+
*/
|
|
76
|
+
beginTransaction(): Promise<ConnectionTransaction>;
|
|
67
77
|
/**
|
|
68
78
|
* Covert result to array
|
|
69
79
|
* @param {any} result table name
|
|
@@ -76,16 +86,6 @@ declare class DB extends AbstractDB {
|
|
|
76
86
|
* @return {Record | null} object | null
|
|
77
87
|
*/
|
|
78
88
|
static makeObject(result: any): Record<string, any> | null;
|
|
79
|
-
/**
|
|
80
|
-
* Get a connection
|
|
81
|
-
* @return {ConnectionTransaction} object - Connection for the transaction
|
|
82
|
-
* @type {object} connection
|
|
83
|
-
* @property {function} connection.query - execute query sql then release connection to pool
|
|
84
|
-
* @property {function} connection.startTransaction - start transaction of query
|
|
85
|
-
* @property {function} connection.commit - commit transaction of query
|
|
86
|
-
* @property {function} connection.rollback - rollback transaction of query
|
|
87
|
-
*/
|
|
88
|
-
beginTransaction(): Promise<ConnectionTransaction>;
|
|
89
89
|
/**
|
|
90
90
|
* Assign table name
|
|
91
91
|
* @static
|
|
@@ -147,7 +147,7 @@ declare class DB extends AbstractDB {
|
|
|
147
147
|
* @param {string} option.password
|
|
148
148
|
* @return {Connection}
|
|
149
149
|
*/
|
|
150
|
-
static getConnection(options: ConnectionOptions): Connection
|
|
150
|
+
static getConnection(options: ConnectionOptions): Promise<Connection>;
|
|
151
151
|
private _typeOf;
|
|
152
152
|
private _initialDB;
|
|
153
153
|
}
|