tspace-mysql 1.0.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/License +19 -0
- package/README.md +375 -0
- package/dist/cli/index.js +37 -0
- package/dist/cli/migrate/make.js +62 -0
- package/dist/cli/models/make.js +52 -0
- package/dist/cli/models/model.js +6 -0
- package/dist/cli/tables/make.js +32 -0
- package/dist/cli/tables/table.js +6 -0
- package/dist/lib/config/env.js +21 -0
- package/dist/lib/connections/index.js +42 -0
- package/dist/lib/connections/options.js +32 -0
- package/dist/lib/index.js +32 -0
- package/dist/lib/tspace/AbstractDB.js +29 -0
- package/dist/lib/tspace/AbstractDatabase.js +44 -0
- package/dist/lib/tspace/AbstractModel.js +29 -0
- package/dist/lib/tspace/DB.js +247 -0
- package/dist/lib/tspace/Database.js +1450 -0
- package/dist/lib/tspace/Interface.js +2 -0
- package/dist/lib/tspace/Logger.js +31 -0
- package/dist/lib/tspace/Model.js +2066 -0
- package/dist/lib/tspace/ProxyHandler.js +39 -0
- package/dist/lib/tspace/Schema.js +250 -0
- package/dist/lib/tspace/index.js +19 -0
- package/dist/lib/utils/constant.js +71 -0
- package/dist/lib/utils/index.js +297 -0
- package/package.json +52 -0
package/License
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2021 Thanathip Srisawatpattana
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# tspace-mysql
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com)
|
|
4
|
+
[](https://www.npmjs.com)
|
|
5
|
+
|
|
6
|
+
Query builder object relation mapping
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
Install with [npm](https://www.npmjs.com/):
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install tspace-mysql --save
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
## Setup
|
|
17
|
+
.env
|
|
18
|
+
```js
|
|
19
|
+
DB_HOST = localhost
|
|
20
|
+
DB_PORT = 3306
|
|
21
|
+
DB_USERNAME = root
|
|
22
|
+
DB_PASSWORD = password
|
|
23
|
+
DB_DATABASE = database
|
|
24
|
+
```
|
|
25
|
+
## Getting Started
|
|
26
|
+
queries data
|
|
27
|
+
```js
|
|
28
|
+
import { DB } from 'tspace-mysql'
|
|
29
|
+
(async () => {
|
|
30
|
+
await new DB().raw('SELECT * FROM users')
|
|
31
|
+
await new DB().table('users').findMany()
|
|
32
|
+
await new DB().table('users').select('id').where('id',1).findOne()
|
|
33
|
+
await new DB().table('users').select('id').whereIn('id',[1,2,3]).findMany()
|
|
34
|
+
})()
|
|
35
|
+
```
|
|
36
|
+
create data
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { DB } from 'tspace-mysql'
|
|
40
|
+
(async () => {
|
|
41
|
+
*ex pattern 1
|
|
42
|
+
const user = await new DB().table('users').create({
|
|
43
|
+
name : 'tspace',
|
|
44
|
+
email : 'tspace@gmail.com'
|
|
45
|
+
}).save()
|
|
46
|
+
|
|
47
|
+
console.log(user)
|
|
48
|
+
|
|
49
|
+
*ex pattern 2
|
|
50
|
+
const u = new DB().table('users')
|
|
51
|
+
u.name = 'tspace'
|
|
52
|
+
u.email = 'tspace@gmail.com'
|
|
53
|
+
await u.save()
|
|
54
|
+
const { result : user } = u
|
|
55
|
+
/* or const user = await u.save() */
|
|
56
|
+
console.log(user)
|
|
57
|
+
})()
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
update data
|
|
61
|
+
```js
|
|
62
|
+
import { DB } from 'tspace-mysql'
|
|
63
|
+
(async () => {
|
|
64
|
+
*ex pattern 1
|
|
65
|
+
const user = await new DB().table('users').update({
|
|
66
|
+
name : 'tspace',
|
|
67
|
+
email : 'tspace@gmail.com'
|
|
68
|
+
})
|
|
69
|
+
.where('id',1)
|
|
70
|
+
.save()
|
|
71
|
+
|
|
72
|
+
console.log(user)
|
|
73
|
+
|
|
74
|
+
*ex pattern 2
|
|
75
|
+
const u = new DB().table('users').where('id',1)
|
|
76
|
+
u.name = 'tspace'
|
|
77
|
+
u.email = 'tspace@gmail.com'
|
|
78
|
+
await u.save()
|
|
79
|
+
const { result : user } = u
|
|
80
|
+
/* or const user = await u.save() */
|
|
81
|
+
console.log(user)
|
|
82
|
+
})()
|
|
83
|
+
```
|
|
84
|
+
delete data
|
|
85
|
+
```js
|
|
86
|
+
import { DB } from 'tspace-mysql'
|
|
87
|
+
(async () => {
|
|
88
|
+
await new User().where('id',1).delete()
|
|
89
|
+
})()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Transactions & Rollback
|
|
93
|
+
```js
|
|
94
|
+
import { DB } from 'tspace-mysql'
|
|
95
|
+
(async () => {
|
|
96
|
+
const transaction = await new DB().beginTransaction()
|
|
97
|
+
try {
|
|
98
|
+
const user = await new User().create({
|
|
99
|
+
name : 'tspace',
|
|
100
|
+
email : 'tspace@gmail.com'
|
|
101
|
+
})
|
|
102
|
+
.save(transaction)
|
|
103
|
+
|
|
104
|
+
const posts = await new Post().createMultiple([
|
|
105
|
+
{
|
|
106
|
+
user_id : user.id,
|
|
107
|
+
title : 'tspace post'
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
user_id : user.id,
|
|
111
|
+
title : 'tspace post second'
|
|
112
|
+
}
|
|
113
|
+
])
|
|
114
|
+
.save(transaction)
|
|
115
|
+
|
|
116
|
+
throw new Error('try to transaction')
|
|
117
|
+
|
|
118
|
+
} catch (err) {
|
|
119
|
+
const rollback = await transaction.rollback()
|
|
120
|
+
console.log(rollback ,'rollback !')
|
|
121
|
+
}
|
|
122
|
+
})()
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Model Conventions
|
|
126
|
+
basic model class and discuss some relations:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
import { Model } from 'tspace-mysql'
|
|
130
|
+
import Post from '../Post'
|
|
131
|
+
import Comment from '../Comment'
|
|
132
|
+
import User from '../User'
|
|
133
|
+
|
|
134
|
+
Folder directory example
|
|
135
|
+
- App
|
|
136
|
+
- Model
|
|
137
|
+
Post.ts
|
|
138
|
+
User.ts
|
|
139
|
+
Comment.ts
|
|
140
|
+
|
|
141
|
+
(async () => {
|
|
142
|
+
const users = await new User()
|
|
143
|
+
.with('posts','comments') /* relations -> hasMany: posts & comments */
|
|
144
|
+
.withQuery('posts', (query) => query.with('user')) /* relation -> belongsTo: post by user */
|
|
145
|
+
.withQuery('comments', (query) => query.with('user','post')) /* relation -> belongsTo: comment by user? & comment in post? */
|
|
146
|
+
.findMany()
|
|
147
|
+
|
|
148
|
+
console.log(users)
|
|
149
|
+
})()
|
|
150
|
+
```
|
|
151
|
+
*User.ts
|
|
152
|
+
```js
|
|
153
|
+
import { Model } from 'tspace-mysql'
|
|
154
|
+
class User extends Model {
|
|
155
|
+
constructor(){
|
|
156
|
+
super()
|
|
157
|
+
this.hasMany({name : 'posts', model: Post })
|
|
158
|
+
this.hasMany({name : 'comments', model: Comment })
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export default User
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
*Post.ts
|
|
165
|
+
```js
|
|
166
|
+
import { Model } from 'tspace-mysql'
|
|
167
|
+
|
|
168
|
+
class Post extends Model {
|
|
169
|
+
constructor(){
|
|
170
|
+
super()
|
|
171
|
+
this.belongsTo({name : 'user', model: User })
|
|
172
|
+
this.hasMany({ name : 'comments' , model : Comment })
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export default Post
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
*Comment.ts
|
|
179
|
+
```js
|
|
180
|
+
import { Model } from 'tspace-mysql'
|
|
181
|
+
|
|
182
|
+
class Comment extends Model {
|
|
183
|
+
constructor(){
|
|
184
|
+
super()
|
|
185
|
+
this.belongsTo({name : 'user', model: User })
|
|
186
|
+
this.belongsTo({name : 'post', model: Post })
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export default Comment
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Method chaining
|
|
193
|
+
method chaining for queries
|
|
194
|
+
```js
|
|
195
|
+
where(column , operator , value)
|
|
196
|
+
whereSensitive(column , operator , value)
|
|
197
|
+
whereId(id)
|
|
198
|
+
whereUser(userId)
|
|
199
|
+
whereEmail(value)
|
|
200
|
+
orWhere(column , operator , value)
|
|
201
|
+
whereIn(column , [])
|
|
202
|
+
whereNotIn(column , [])
|
|
203
|
+
whereNull(column)
|
|
204
|
+
whereNotNull(column)
|
|
205
|
+
whereBetween (column , [value1 , value2])
|
|
206
|
+
whereSubQuery(colmn , rawSQL)
|
|
207
|
+
|
|
208
|
+
select(column1 ,column2 ,...N)
|
|
209
|
+
except(column1 ,column2 ,...N)
|
|
210
|
+
only(column1 ,column2 ,...N)
|
|
211
|
+
hidden(column1 ,column2 ,...N)
|
|
212
|
+
join (primary key , table.foreign key)
|
|
213
|
+
rightJoin (primary key , table.foreign key)
|
|
214
|
+
leftJoin (primary key , table.foreign key)
|
|
215
|
+
limit (limit)
|
|
216
|
+
orderBy (column ,'ASC' || 'DSCE')
|
|
217
|
+
having (condition)
|
|
218
|
+
latest (column)
|
|
219
|
+
oldest (column)
|
|
220
|
+
groupBy (column)
|
|
221
|
+
|
|
222
|
+
insert(objects)
|
|
223
|
+
create(objects)
|
|
224
|
+
createMultiple(array objects)
|
|
225
|
+
update (objects)
|
|
226
|
+
insertNotExists(objects)
|
|
227
|
+
createNotExists(objects)
|
|
228
|
+
updateOrInsert (objects)
|
|
229
|
+
updateOrCreate (objects)
|
|
230
|
+
/**
|
|
231
|
+
* relationship
|
|
232
|
+
*
|
|
233
|
+
* @Relation setup name in model
|
|
234
|
+
*/
|
|
235
|
+
with(name1 , name2,...nameN)
|
|
236
|
+
withExists(name1 , name2,...nameN)
|
|
237
|
+
withQuery('relation registry',(callback queries))
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* queries statements
|
|
241
|
+
*
|
|
242
|
+
* @exec statements
|
|
243
|
+
*/
|
|
244
|
+
findMany()
|
|
245
|
+
findOne()
|
|
246
|
+
find(id)
|
|
247
|
+
first()
|
|
248
|
+
get()
|
|
249
|
+
delelte()
|
|
250
|
+
exists ()
|
|
251
|
+
onlyTrashed()
|
|
252
|
+
toSQL()
|
|
253
|
+
toString()
|
|
254
|
+
toJSON()
|
|
255
|
+
toArray(column)
|
|
256
|
+
count(column)
|
|
257
|
+
sum(column)
|
|
258
|
+
avg(column)
|
|
259
|
+
max(column)
|
|
260
|
+
min(column)
|
|
261
|
+
pagination({ limit , page })
|
|
262
|
+
save() /*for action statements insert update or delete */
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Cli
|
|
266
|
+
npm install tspace-mysql -g
|
|
267
|
+
```js
|
|
268
|
+
|
|
269
|
+
tspace-mysql make:model <folder/name model> --m --f=... --name=.... --js
|
|
270
|
+
* optional
|
|
271
|
+
--m /* created table for migrate in <FOLDER/migrations> */
|
|
272
|
+
--f=folder/...folder /* created table for migrate in <CUSTOM FOLDER> default <FOLDER/migrations> */
|
|
273
|
+
--js /* extension .js default .ts */
|
|
274
|
+
--name=NAME /* class name default <NAME> in input cli */
|
|
275
|
+
|
|
276
|
+
tspace-mysql make:table <folder> --name=....
|
|
277
|
+
* required
|
|
278
|
+
--name=TABLE_NAME /* created table for migrate in <folder> */
|
|
279
|
+
* optional
|
|
280
|
+
--js /* extension .js default .ts */
|
|
281
|
+
|
|
282
|
+
tspace-mysql migrate <folder> --js
|
|
283
|
+
* optional
|
|
284
|
+
--js /* extension .js default .ts */
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
tspace-mysql make:model App/Models/User --m
|
|
288
|
+
```js
|
|
289
|
+
/* Ex folder
|
|
290
|
+
- node_modules
|
|
291
|
+
- App
|
|
292
|
+
- Models
|
|
293
|
+
User.ts
|
|
294
|
+
*/
|
|
295
|
+
|
|
296
|
+
/* in App/Models/User.ts */
|
|
297
|
+
import { Model } from 'tspace-mysql'
|
|
298
|
+
class User extends Model{
|
|
299
|
+
constructor(){
|
|
300
|
+
super()
|
|
301
|
+
this.useDebug() /* default false *debug sql */
|
|
302
|
+
this.useTimestamp() /* default false * case created_at & updated_at*/
|
|
303
|
+
this.useSoftDelete() /* default false * case where deleted_at is null */
|
|
304
|
+
this.useTable('users') /* default users */
|
|
305
|
+
this.usePattern('camelCase') /* default snake_case */
|
|
306
|
+
this.useUUID()
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
export default User
|
|
310
|
+
```
|
|
311
|
+
tspace-mysql make:table App/Models/Migrations --name=users
|
|
312
|
+
```js
|
|
313
|
+
/* Ex folder
|
|
314
|
+
- node_modules
|
|
315
|
+
- App
|
|
316
|
+
- Models
|
|
317
|
+
- migrations
|
|
318
|
+
create_users_table.ts
|
|
319
|
+
User.ts
|
|
320
|
+
*/
|
|
321
|
+
/* in App/Models/Migrations/create_users_table.ts */
|
|
322
|
+
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
323
|
+
(async () => {
|
|
324
|
+
await new Schema().table('users',{
|
|
325
|
+
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
326
|
+
name : new Blueprint().varchar(120).default('my name'),
|
|
327
|
+
email : new Blueprint().varchar(255).unique(),
|
|
328
|
+
email_verify : new Blueprint().tinyInt(),
|
|
329
|
+
password : new Blueprint().varchar(255),
|
|
330
|
+
})
|
|
331
|
+
/**
|
|
332
|
+
*
|
|
333
|
+
* @Faker data
|
|
334
|
+
* await new DB().table('users').faker(5)
|
|
335
|
+
*/
|
|
336
|
+
})()
|
|
337
|
+
```
|
|
338
|
+
tspace-mysql migrate App/Models/Migrations
|
|
339
|
+
/* migrate all table in folder into database */
|
|
340
|
+
```js
|
|
341
|
+
* Blueprint method
|
|
342
|
+
/**
|
|
343
|
+
*
|
|
344
|
+
* @Types
|
|
345
|
+
*
|
|
346
|
+
*/
|
|
347
|
+
int ()
|
|
348
|
+
tinyInt (number)
|
|
349
|
+
bigInt (number)
|
|
350
|
+
double ()
|
|
351
|
+
float ()
|
|
352
|
+
varchar (number)
|
|
353
|
+
char (number)
|
|
354
|
+
longText()
|
|
355
|
+
mediumText()
|
|
356
|
+
tinyText()
|
|
357
|
+
text()
|
|
358
|
+
enum(...n)
|
|
359
|
+
date()
|
|
360
|
+
dateTime()
|
|
361
|
+
timestamp ()
|
|
362
|
+
/**
|
|
363
|
+
*
|
|
364
|
+
* @Attrbuites
|
|
365
|
+
*
|
|
366
|
+
*/
|
|
367
|
+
unsigned ()
|
|
368
|
+
unique ()
|
|
369
|
+
null ()
|
|
370
|
+
notNull ()
|
|
371
|
+
primary()
|
|
372
|
+
default (string)
|
|
373
|
+
defaultTimestamp ()
|
|
374
|
+
autoIncrement ()
|
|
375
|
+
```
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
var fs_1 = __importDefault(require("fs"));
|
|
9
|
+
var make_1 = __importDefault(require("./models/make"));
|
|
10
|
+
var make_2 = __importDefault(require("./tables/make"));
|
|
11
|
+
var make_3 = __importDefault(require("./migrate/make"));
|
|
12
|
+
var commands = {
|
|
13
|
+
'make:model': make_1.default,
|
|
14
|
+
'make:table': make_2.default,
|
|
15
|
+
'migrate': make_3.default
|
|
16
|
+
};
|
|
17
|
+
try {
|
|
18
|
+
var name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(function (data) { return data === null || data === void 0 ? void 0 : data.includes('--name='); })) === null || _b === void 0 ? void 0 : _b.replace('--name=', '')) !== null && _c !== void 0 ? _c : null;
|
|
19
|
+
var migrate = (_e = (_d = process.argv.slice(2)) === null || _d === void 0 ? void 0 : _d.includes('--m')) !== null && _e !== void 0 ? _e : false;
|
|
20
|
+
var migrateFolder = (_h = (_g = (_f = process.argv.slice(2)) === null || _f === void 0 ? void 0 : _f.find(function (data) { return data === null || data === void 0 ? void 0 : data.includes('--f='); })) === null || _g === void 0 ? void 0 : _g.replace('--f=', '/')) !== null && _h !== void 0 ? _h : null;
|
|
21
|
+
var path = (_j = process.argv.slice(3)) === null || _j === void 0 ? void 0 : _j.shift();
|
|
22
|
+
var type = ((_l = (_k = process.argv.slice(2)) === null || _k === void 0 ? void 0 : _k.includes('--js')) !== null && _l !== void 0 ? _l : false) ? '.js' : '.ts';
|
|
23
|
+
var cmd = {
|
|
24
|
+
name: name,
|
|
25
|
+
path: path,
|
|
26
|
+
migrate: migrate,
|
|
27
|
+
migrateFolder: migrateFolder,
|
|
28
|
+
type: type,
|
|
29
|
+
cwd: process.cwd(),
|
|
30
|
+
fs: fs_1.default,
|
|
31
|
+
npm: 'tspace-mysql'
|
|
32
|
+
};
|
|
33
|
+
commands[process.argv[2]](cmd);
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
console.log("\n use make:model <folder/...folder/model name> --m\n use make:table <folder/folder/...folder> --name=<name>\n use migrate <folder/folder/...folder> \n");
|
|
37
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var child_process_1 = require("child_process");
|
|
15
|
+
exports.default = (function (_ref) {
|
|
16
|
+
var e_1, _a;
|
|
17
|
+
var _b, _c, _d;
|
|
18
|
+
var path = _ref.path, type = _ref.type, cwd = _ref.cwd, fs = _ref.fs;
|
|
19
|
+
var split = path.split('/') || path;
|
|
20
|
+
var f = split.join('/');
|
|
21
|
+
try {
|
|
22
|
+
fs.accessSync(cwd + ("/" + f), fs.F_OK, {
|
|
23
|
+
recursive: true
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
fs.mkdirSync(cwd + ("/" + f), {
|
|
28
|
+
recursive: true
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
var dir = cwd + "/" + f;
|
|
33
|
+
var files = (_b = fs.readdirSync(dir)) !== null && _b !== void 0 ? _b : [];
|
|
34
|
+
if (!(files === null || files === void 0 ? void 0 : files.length))
|
|
35
|
+
console.log('this folder is empty');
|
|
36
|
+
var cmd = type === '.js' ? 'node' : 'ts-node';
|
|
37
|
+
try {
|
|
38
|
+
for (var files_1 = __values(files), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) {
|
|
39
|
+
var t = files_1_1.value;
|
|
40
|
+
var run = (0, child_process_1.exec)(cmd + " " + dir + "/" + t);
|
|
41
|
+
(_c = run === null || run === void 0 ? void 0 : run.stdout) === null || _c === void 0 ? void 0 : _c.on('data', function (data) {
|
|
42
|
+
if (data)
|
|
43
|
+
console.log(data);
|
|
44
|
+
});
|
|
45
|
+
(_d = run === null || run === void 0 ? void 0 : run.stderr) === null || _d === void 0 ? void 0 : _d.on('data', function (err) {
|
|
46
|
+
if (err)
|
|
47
|
+
console.error(err);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
52
|
+
finally {
|
|
53
|
+
try {
|
|
54
|
+
if (files_1_1 && !files_1_1.done && (_a = files_1.return)) _a.call(files_1);
|
|
55
|
+
}
|
|
56
|
+
finally { if (e_1) throw e_1.error; }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
console.log(err.message);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var model_1 = __importDefault(require("./model"));
|
|
7
|
+
var table_1 = __importDefault(require("../tables/table"));
|
|
8
|
+
var utils_1 = __importDefault(require("../../lib/utils"));
|
|
9
|
+
exports.default = (function (_ref) {
|
|
10
|
+
var path = _ref.path, migrate = _ref.migrate, migrateFolder = _ref.migrateFolder, type = _ref.type, name = _ref.name, cwd = _ref.cwd, fs = _ref.fs, npm = _ref.npm;
|
|
11
|
+
var split = path.split('/') || path;
|
|
12
|
+
var model = split.pop();
|
|
13
|
+
var f = split.join('/');
|
|
14
|
+
try {
|
|
15
|
+
fs.accessSync(cwd + ("/" + f), fs.F_OK, {
|
|
16
|
+
recursive: true
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
fs.mkdirSync(cwd + ("/" + f), {
|
|
21
|
+
recursive: true
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
var folder = cwd + "/" + f + "/" + model + type;
|
|
25
|
+
var data = (0, model_1.default)(name || model, npm);
|
|
26
|
+
fs.writeFile(folder, data, function (err) {
|
|
27
|
+
if (err)
|
|
28
|
+
throw err.message;
|
|
29
|
+
});
|
|
30
|
+
console.log("Model : '" + model + "' created successfully");
|
|
31
|
+
if (migrate) {
|
|
32
|
+
var tableName = utils_1.default.tableName(name || model);
|
|
33
|
+
var folder_1 = migrateFolder !== null && migrateFolder !== void 0 ? migrateFolder : "/" + f + "/Migrations";
|
|
34
|
+
try {
|
|
35
|
+
fs.accessSync(cwd + folder_1, fs.F_OK, {
|
|
36
|
+
recursive: true
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
fs.mkdirSync(cwd + folder_1, {
|
|
41
|
+
recursive: true
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
var folderMigrate = cwd + "/" + folder_1 + "/create_" + tableName + "_table" + type;
|
|
45
|
+
var table = (0, table_1.default)(tableName, npm);
|
|
46
|
+
fs.writeFile(folderMigrate, table, function (err) {
|
|
47
|
+
if (err)
|
|
48
|
+
throw err;
|
|
49
|
+
});
|
|
50
|
+
console.log("Migration : '" + tableName + "' created successfully");
|
|
51
|
+
}
|
|
52
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var Model = function (model, npm) {
|
|
4
|
+
return "import { Model } from '" + npm + "'\nclass " + model + " extends Model {\n constructor(){\n super()\n this.useTimestamp()\n }\n}\nexport { " + model + " }\nexport default " + model + "\n";
|
|
5
|
+
};
|
|
6
|
+
exports.default = Model;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var table_1 = __importDefault(require("./table"));
|
|
7
|
+
exports.default = (function (_ref) {
|
|
8
|
+
var path = _ref.path, name = _ref.name, type = _ref.type, cwd = _ref.cwd, fs = _ref.fs, npm = _ref.npm;
|
|
9
|
+
var split = path.split('/') || path;
|
|
10
|
+
var f = split.join('/');
|
|
11
|
+
if (name == null)
|
|
12
|
+
console.log("use " + npm + " make:table FOLDER/FOLDER --name=tableName");
|
|
13
|
+
else {
|
|
14
|
+
try {
|
|
15
|
+
fs.accessSync(cwd + ("/" + f), fs.F_OK, {
|
|
16
|
+
recursive: true
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
fs.mkdirSync(cwd + ("/" + f), {
|
|
21
|
+
recursive: true
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
var folderMigrate = cwd + "/" + f + "/create_" + name + "_table" + type;
|
|
25
|
+
var table = (0, table_1.default)(name, npm);
|
|
26
|
+
fs.writeFile(folderMigrate, table, function (err) {
|
|
27
|
+
if (err)
|
|
28
|
+
console.log(err.message);
|
|
29
|
+
});
|
|
30
|
+
console.log("Migration : " + name + " created successfully");
|
|
31
|
+
}
|
|
32
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var Table = function (model, npm) {
|
|
4
|
+
return "import { Schema , Blueprint , DB } from '" + npm + "'\n(async () => {\n await new Schema().table('" + model + "',{ \n id : new Blueprint().int().notNull().primary().autoIncrement(),\n name : new Blueprint().varchar(120).default('my name'),\n email : new Blueprint().varchar(120).unique(),\n email_verify : new Blueprint().tinyInt(),\n password : new Blueprint().varchar(120),\n birthdate : new Blueprint().date()\n })\n\n /**\n * \n * @Faker data\n * await new DB().table('" + model + "').faker(5)\n */\n})()\n";
|
|
5
|
+
};
|
|
6
|
+
exports.default = Table;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
var _a;
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
var dotenv_1 = __importDefault(require("dotenv"));
|
|
8
|
+
var path_1 = __importDefault(require("path"));
|
|
9
|
+
var NODE_ENV = (_a = process.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV;
|
|
10
|
+
var pathEnv = path_1.default.join(path_1.default.resolve(), ".env." + NODE_ENV);
|
|
11
|
+
if (NODE_ENV == null)
|
|
12
|
+
pathEnv = path_1.default.join(path_1.default.resolve(), ".env");
|
|
13
|
+
dotenv_1.default.config({ path: pathEnv });
|
|
14
|
+
var env = Object.freeze({
|
|
15
|
+
DB_HOST: process.env.DB_HOST,
|
|
16
|
+
DB_PORT: process.env.DB_PORT,
|
|
17
|
+
DB_USERNAME: process.env.DB_USERNAME,
|
|
18
|
+
DB_PASSWORD: process.env.DB_PASSWORD,
|
|
19
|
+
DB_DATABASE: process.env.DB_DATABASE
|
|
20
|
+
});
|
|
21
|
+
exports.default = env;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
var util_1 = require("util");
|
|
23
|
+
var mysql_1 = require("mysql");
|
|
24
|
+
var options_1 = __importDefault(require("./options"));
|
|
25
|
+
var pool = (0, mysql_1.createPool)(options_1.default);
|
|
26
|
+
var optionIsNull = Object === null || Object === void 0 ? void 0 : Object.entries(options_1.default).some(function (_a) {
|
|
27
|
+
var _b = __read(_a, 2), _ = _b[0], option = _b[1];
|
|
28
|
+
return option == null;
|
|
29
|
+
});
|
|
30
|
+
var messsage = optionIsNull ?
|
|
31
|
+
"Can't get optionuration in environment !" :
|
|
32
|
+
"Connection lost to database !";
|
|
33
|
+
pool.getConnection(function (err, connection) {
|
|
34
|
+
if (err) {
|
|
35
|
+
console.log("\u001B[1m\u001B[31m\n " + messsage + " \u001B[0m\n ------------------------------- \u001B[33m\n DB_HOST : " + (options_1.default === null || options_1.default === void 0 ? void 0 : options_1.default.host) + " \n DB_PORT : " + (options_1.default === null || options_1.default === void 0 ? void 0 : options_1.default.port) + " \n DB_USERNAME : " + options_1.default.user + " \n DB_PASSWORD : " + options_1.default.password + " \n DB_DATABASE : " + options_1.default.database + " \u001B[0m \n -------------------------------\n ");
|
|
36
|
+
}
|
|
37
|
+
if (connection)
|
|
38
|
+
connection.release();
|
|
39
|
+
return;
|
|
40
|
+
});
|
|
41
|
+
pool.query = (0, util_1.promisify)(pool.query);
|
|
42
|
+
exports.default = pool;
|
|
@@ -0,0 +1,32 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
var env_1 = __importDefault(require("../config/env"));
|
|
18
|
+
var defaultOptions = {
|
|
19
|
+
connectionLimit: 10,
|
|
20
|
+
dateStrings: true,
|
|
21
|
+
waitForConnections: false,
|
|
22
|
+
charset: 'utf8mb4'
|
|
23
|
+
};
|
|
24
|
+
var options = {
|
|
25
|
+
host: env_1.default.DB_HOST,
|
|
26
|
+
database: env_1.default.DB_DATABASE,
|
|
27
|
+
port: env_1.default.DB_PORT,
|
|
28
|
+
user: env_1.default.DB_USERNAME,
|
|
29
|
+
password: env_1.default.DB_PASSWORD
|
|
30
|
+
};
|
|
31
|
+
var configs = __assign(__assign({}, defaultOptions), options);
|
|
32
|
+
exports.default = configs;
|