tspace-mysql 1.5.4 → 1.5.6
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 +106 -67
- package/build/lib/Interface.d.ts +18 -6
- package/build/lib/constants/index.js +1 -0
- package/build/lib/core/Abstracts/AbstractModel.d.ts +1 -1
- package/build/lib/core/Builder.d.ts +10 -7
- package/build/lib/core/Builder.js +82 -21
- package/build/lib/core/DB.d.ts +25 -2
- package/build/lib/core/DB.js +73 -7
- package/build/lib/core/Handlers/Relation.js +28 -17
- package/build/lib/core/Handlers/State.js +35 -3
- package/build/lib/core/Model.d.ts +37 -30
- package/build/lib/core/Model.js +76 -22
- package/build/lib/core/Schema.js +8 -8
- package/build/lib/core/Type.d.ts +13 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ npm install tspace-mysql -g
|
|
|
19
19
|
- [Configuration](#configuration)
|
|
20
20
|
- [Query Builder](#query-builder)
|
|
21
21
|
- [Returning Results](#returning-results)
|
|
22
|
+
- [Query Statement](#query-statements)
|
|
22
23
|
- [Select Statements](#select-statements)
|
|
23
24
|
- [Raw Expressions](#raw-expressions)
|
|
24
25
|
- [Ordering, Grouping, Limit and Offset](#ordering-grouping-limit-and-offset)
|
|
@@ -32,6 +33,7 @@ npm install tspace-mysql -g
|
|
|
32
33
|
- [Basic Where Clauses](#basic-where-clauses)
|
|
33
34
|
- [Where Clauses](#where-clauses)
|
|
34
35
|
- [Or Where Clauses](#or-where-clauses)
|
|
36
|
+
- [Where Object Clauses](#where-object-clauses)
|
|
35
37
|
- [JSON Where Clauses](#json-where-clauses)
|
|
36
38
|
- [Additional Where Clauses](#additional-where-clauses)
|
|
37
39
|
- [Logical Grouping](#logical-grouping)
|
|
@@ -207,6 +209,19 @@ const users = await new DB("users").makeUpdateStatement() // query string for up
|
|
|
207
209
|
const users = await new DB("users").makeDeleteStatement() // query string for delete statement
|
|
208
210
|
|
|
209
211
|
const users = await new DB("users").makeCreateTableStatement() // query string for create table statement
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Query Statements
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
const query = await DB.query(
|
|
219
|
+
"SELECT * FROM users WHERE id = :id AND email IS :email AND name IN :username", {
|
|
220
|
+
id : 1,
|
|
221
|
+
email : null,
|
|
222
|
+
username : ['name1','name2']
|
|
223
|
+
})
|
|
224
|
+
// SELECT * FROM users WHERE id = '1' AND email IS NULL AND username in ('name1','name2');
|
|
210
225
|
```
|
|
211
226
|
|
|
212
227
|
## Select Statements
|
|
@@ -386,6 +401,30 @@ const users = await new DB("users")
|
|
|
386
401
|
// ( `users`.`id` <> '2' AND `users`.`username` = 'try to find' AND `users`.`email` = 'find@example.com');
|
|
387
402
|
```
|
|
388
403
|
|
|
404
|
+
### Where Object Clauses
|
|
405
|
+
|
|
406
|
+
```js
|
|
407
|
+
const whereObject = await new DB("users")
|
|
408
|
+
.whereObject({
|
|
409
|
+
id : DB.op('eq',1),
|
|
410
|
+
username : DB.op('in',['user1','user2']),
|
|
411
|
+
name : DB.op('like','%value%')
|
|
412
|
+
})
|
|
413
|
+
.findMany();
|
|
414
|
+
|
|
415
|
+
// You can also use .where as well.
|
|
416
|
+
const where = await new DB("users")
|
|
417
|
+
.where({
|
|
418
|
+
id : DB.op('eq',1),
|
|
419
|
+
username : DB.op('in',['user1','user2']),
|
|
420
|
+
name : DB.op('like','%value%')
|
|
421
|
+
})
|
|
422
|
+
.findMany();
|
|
423
|
+
|
|
424
|
+
// SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` IN ('user1','user2') AND `users`.`name` LIKE '%value%';
|
|
425
|
+
|
|
426
|
+
```
|
|
427
|
+
|
|
389
428
|
### JSON Where Clauses
|
|
390
429
|
|
|
391
430
|
```js
|
|
@@ -1085,7 +1124,7 @@ tspace-mysql make:model <model name> --dir=< directory >
|
|
|
1085
1124
|
|
|
1086
1125
|
## Model Conventions
|
|
1087
1126
|
|
|
1088
|
-
|
|
1127
|
+
Your database schema using models. These models represent tables in the database
|
|
1089
1128
|
Let's example a basic model class:
|
|
1090
1129
|
|
|
1091
1130
|
```js
|
|
@@ -1181,7 +1220,7 @@ class User extends Model {
|
|
|
1181
1220
|
super()
|
|
1182
1221
|
// By default, the model knows that the table name for this User is 'users'
|
|
1183
1222
|
|
|
1184
|
-
this.useTable('
|
|
1223
|
+
this.useTable('fix_table') // fixtable
|
|
1185
1224
|
this.useTablePlural() // users
|
|
1186
1225
|
this.useTableSingular() // user
|
|
1187
1226
|
}
|
|
@@ -1201,7 +1240,6 @@ class UserPhone extends Model {
|
|
|
1201
1240
|
// The table name is user_phones
|
|
1202
1241
|
this.useSnakeCase()
|
|
1203
1242
|
|
|
1204
|
-
|
|
1205
1243
|
this.useCamelCase()
|
|
1206
1244
|
// The table name is userPhones
|
|
1207
1245
|
}
|
|
@@ -1220,10 +1258,10 @@ const userPhone = await new UserPhone().where('user_id',1).findOne()
|
|
|
1220
1258
|
|
|
1221
1259
|
import { Model } from 'tspace-mysql'
|
|
1222
1260
|
class User extends Model {
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1261
|
+
constructor() {
|
|
1262
|
+
super()
|
|
1263
|
+
this.useUUID() // insert uuid when creating
|
|
1264
|
+
}
|
|
1227
1265
|
}
|
|
1228
1266
|
|
|
1229
1267
|
```
|
|
@@ -1234,21 +1272,21 @@ class User extends Model {
|
|
|
1234
1272
|
|
|
1235
1273
|
import { Model } from 'tspace-mysql'
|
|
1236
1274
|
class User extends Model {
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1275
|
+
constructor() {
|
|
1276
|
+
super()
|
|
1277
|
+
// insert created_at and updated_at when creating
|
|
1278
|
+
// update updated_at when updating
|
|
1279
|
+
// 'created_at' and 'updated_at' still relate to pettern the model
|
|
1280
|
+
// this.useCamelCase() will covert 'created_at' to 'createdAt' and 'updated_at' to 'updatedAt'
|
|
1281
|
+
this.useTimestamp()
|
|
1282
|
+
|
|
1283
|
+
// custom the columns
|
|
1284
|
+
this.useTimestamp({
|
|
1285
|
+
createdAt : 'createdAtCustom',
|
|
1286
|
+
updatedAt : 'updatedAtCustom'
|
|
1287
|
+
})
|
|
1250
1288
|
|
|
1251
|
-
|
|
1289
|
+
}
|
|
1252
1290
|
}
|
|
1253
1291
|
|
|
1254
1292
|
```
|
|
@@ -1352,8 +1390,8 @@ class User extends Model {
|
|
|
1352
1390
|
super()
|
|
1353
1391
|
this.useSoftDelete() // All query will be where 'deleted_at' is null
|
|
1354
1392
|
|
|
1355
|
-
//
|
|
1356
|
-
//
|
|
1393
|
+
// You can also use patterns camelCase to covert the 'deleted_at' to 'deletedAt'
|
|
1394
|
+
// You can also customize the column 'deleted_at'
|
|
1357
1395
|
this.useSoftDelete('deletedAtCustom')
|
|
1358
1396
|
}
|
|
1359
1397
|
}
|
|
@@ -1400,7 +1438,7 @@ export default User
|
|
|
1400
1438
|
+--------------------------------------------------------------------------+
|
|
1401
1439
|
|
|
1402
1440
|
import User from '../User'
|
|
1403
|
-
const user = await new User().relations('phone').findOne() // can use the method .with('roles')
|
|
1441
|
+
const user = await new User().relations('phone').findOne() // You can also use the method .with('roles').
|
|
1404
1442
|
// user?.phone => {...}
|
|
1405
1443
|
const userUsingFunction = await new User().phone().findOne()
|
|
1406
1444
|
// userUsingFunction?.phone => {...}
|
|
@@ -1983,7 +2021,7 @@ await Schema.sync(`/src/Models`, {
|
|
|
1983
2021
|
changed: true,
|
|
1984
2022
|
});
|
|
1985
2023
|
|
|
1986
|
-
// also
|
|
2024
|
+
// You can also synchronize using the Model.
|
|
1987
2025
|
await new User().sync({ force: true, foreign: true, changed: true });
|
|
1988
2026
|
```
|
|
1989
2027
|
|
|
@@ -2007,11 +2045,11 @@ const schemaUser = {
|
|
|
2007
2045
|
updatedAt :new Blueprint().timestamp().null()
|
|
2008
2046
|
}
|
|
2009
2047
|
|
|
2010
|
-
type
|
|
2048
|
+
type TSchemaUser = SchemaType<typeof schemaUser>
|
|
2011
2049
|
|
|
2012
|
-
//
|
|
2050
|
+
// You can also reassign the type for the schema
|
|
2013
2051
|
/**
|
|
2014
|
-
type
|
|
2052
|
+
type TSchemaUser = SchemaType<typeof schemaUser , {
|
|
2015
2053
|
id : number,
|
|
2016
2054
|
uuid : string,
|
|
2017
2055
|
...
|
|
@@ -2019,7 +2057,7 @@ type SchemaUserType = SchemaType<typeof schemaUser>
|
|
|
2019
2057
|
|
|
2020
2058
|
*/
|
|
2021
2059
|
|
|
2022
|
-
class User extends Model<
|
|
2060
|
+
class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate the type for the Model.
|
|
2023
2061
|
constructor() {
|
|
2024
2062
|
super()
|
|
2025
2063
|
this.useSchema(schemaUser)
|
|
@@ -2028,7 +2066,7 @@ class User extends Model<SchemaUserType> { // add this type for the Model
|
|
|
2028
2066
|
}
|
|
2029
2067
|
}
|
|
2030
2068
|
|
|
2031
|
-
export { User ,
|
|
2069
|
+
export { User , TSchemaUser }
|
|
2032
2070
|
export default User
|
|
2033
2071
|
|
|
2034
2072
|
+--------------------------------------------------------------------------+
|
|
@@ -2045,9 +2083,9 @@ const schemaPhone = {
|
|
|
2045
2083
|
updatedAt :new Blueprint().timestamp().null()
|
|
2046
2084
|
}
|
|
2047
2085
|
|
|
2048
|
-
type
|
|
2086
|
+
type TSchemaPhone = SchemaType<typeof schemaPhone>
|
|
2049
2087
|
|
|
2050
|
-
class Phone extends Model<
|
|
2088
|
+
class Phone extends Model<TSchemaPhone> {
|
|
2051
2089
|
constructor() {
|
|
2052
2090
|
super()
|
|
2053
2091
|
this.useSchema(schemaPhone)
|
|
@@ -2055,7 +2093,7 @@ class Phone extends Model<SchemaPhoneType> {
|
|
|
2055
2093
|
}
|
|
2056
2094
|
}
|
|
2057
2095
|
|
|
2058
|
-
export { Phone ,
|
|
2096
|
+
export { Phone , TSchemaPhone }
|
|
2059
2097
|
export default Phone
|
|
2060
2098
|
|
|
2061
2099
|
+--------------------------------------------------------------------------+
|
|
@@ -2064,8 +2102,8 @@ export default Phone
|
|
|
2064
2102
|
### Safety Select
|
|
2065
2103
|
|
|
2066
2104
|
```js
|
|
2067
|
-
import { User ,
|
|
2068
|
-
import { Phone,
|
|
2105
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2106
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2069
2107
|
|
|
2070
2108
|
const users = await new User().select('id','username').findMany() ✅
|
|
2071
2109
|
const users = await new User().select('idx','username').findMany() ❌
|
|
@@ -2079,8 +2117,8 @@ const users = await new User().except('idx','username').findMany() ❌
|
|
|
2079
2117
|
|
|
2080
2118
|
```js
|
|
2081
2119
|
|
|
2082
|
-
import { User ,
|
|
2083
|
-
import { Phone,
|
|
2120
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2121
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2084
2122
|
|
|
2085
2123
|
const users = await new User().orderBy('id','DESC').findMany() ✅
|
|
2086
2124
|
const users = await new User().orderBy('idx','DESC').findMany() ❌
|
|
@@ -2096,8 +2134,8 @@ const users = await new User().oldest('idx').findMany() ❌
|
|
|
2096
2134
|
### Safety GroupBy
|
|
2097
2135
|
|
|
2098
2136
|
```js
|
|
2099
|
-
import { User ,
|
|
2100
|
-
import { Phone,
|
|
2137
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2138
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2101
2139
|
|
|
2102
2140
|
const users = await new User().groupBy('id').findMany() ✅
|
|
2103
2141
|
const users = await new User().groupBy('idx').findMany() ❌
|
|
@@ -2107,8 +2145,8 @@ const users = await new User().groupBy('idx').findMany() ❌
|
|
|
2107
2145
|
### Safety Where
|
|
2108
2146
|
|
|
2109
2147
|
```js
|
|
2110
|
-
import { User ,
|
|
2111
|
-
import { Phone,
|
|
2148
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2149
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2112
2150
|
|
|
2113
2151
|
const users = await new User().where('id',1).findMany() ✅
|
|
2114
2152
|
const users = await new User().where('idxx',1).findMany() ❌
|
|
@@ -2145,8 +2183,8 @@ const users = await new User()
|
|
|
2145
2183
|
### Safety Insert
|
|
2146
2184
|
|
|
2147
2185
|
```js
|
|
2148
|
-
import { User ,
|
|
2149
|
-
import { Phone,
|
|
2186
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2187
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2150
2188
|
|
|
2151
2189
|
const users = await new User().create({ id : 10 }).save() ✅
|
|
2152
2190
|
|
|
@@ -2157,8 +2195,8 @@ const users = await new User().create({ idx : 10 }).save() ❌
|
|
|
2157
2195
|
### Safety Update
|
|
2158
2196
|
|
|
2159
2197
|
```js
|
|
2160
|
-
import { User ,
|
|
2161
|
-
import { Phone,
|
|
2198
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2199
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2162
2200
|
|
|
2163
2201
|
const users = await new User().update({ id : 10 }).where('id',1).save() ✅
|
|
2164
2202
|
const users = await new User().update({ id : 10 }).where('idx',1).save() ❌
|
|
@@ -2169,8 +2207,8 @@ const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
|
|
|
2169
2207
|
### Safety Delete
|
|
2170
2208
|
|
|
2171
2209
|
```js
|
|
2172
|
-
import { User ,
|
|
2173
|
-
import { Phone,
|
|
2210
|
+
import { User , TSchemaUser } from './User.ts'
|
|
2211
|
+
import { Phone, TSchemaPhone } from './Phone.ts'
|
|
2174
2212
|
|
|
2175
2213
|
const users = await new User().where('id',1).delete() ✅
|
|
2176
2214
|
const users = await new User().where('idx',1).delete() ❌
|
|
@@ -2192,7 +2230,7 @@ import { Phone } from './Phone.ts'
|
|
|
2192
2230
|
user.phones ❌
|
|
2193
2231
|
}
|
|
2194
2232
|
|
|
2195
|
-
// You can also specify the type for the results
|
|
2233
|
+
// You can also specify the type for the results.
|
|
2196
2234
|
// bad 👎👎👎
|
|
2197
2235
|
const users = await new User()
|
|
2198
2236
|
.relations('phone','phones')
|
|
@@ -2210,15 +2248,15 @@ import { Phone } from './Phone.ts'
|
|
|
2210
2248
|
// good 👍👍👍
|
|
2211
2249
|
const users = await new User()
|
|
2212
2250
|
.relations('phone','phones')
|
|
2213
|
-
.findMany<{ phone :
|
|
2251
|
+
.findMany<{ phone : TSchemaPhone , phones : TSchemaPhone[] }>()
|
|
2214
2252
|
|
|
2215
2253
|
for(const user of users) {
|
|
2216
2254
|
user.phone ✅
|
|
2217
2255
|
user.phones ✅
|
|
2218
|
-
user.phone
|
|
2219
|
-
user.phone
|
|
2220
|
-
user.phones.map(phone => phone
|
|
2221
|
-
user.phones.map(phone => phone
|
|
2256
|
+
user.phone?.id ✅
|
|
2257
|
+
user.phone?.idx ❌
|
|
2258
|
+
user.phones.map(phone => phone?.id) ✅
|
|
2259
|
+
user.phones.map(phone => phone?.idx) ❌
|
|
2222
2260
|
}
|
|
2223
2261
|
|
|
2224
2262
|
+--------------------------------------------------------------------------+
|
|
@@ -2228,14 +2266,14 @@ for(const user of users) {
|
|
|
2228
2266
|
.relations('phone','phones')
|
|
2229
2267
|
.relationQuery('phone' , (query : Phone) => query.relations('user'))
|
|
2230
2268
|
.relationQuery('phones' , (query : Phone) => query.relations('user'))
|
|
2231
|
-
.findMany<{ phone :
|
|
2269
|
+
.findMany<{ phone : TSchemaPhone , phones : TSchemaPhone[] }>()
|
|
2232
2270
|
|
|
2233
2271
|
for(const user of users) {
|
|
2234
2272
|
user.phone.user ❌
|
|
2235
2273
|
user.phones.map(phone =>phone.user) ❌
|
|
2236
2274
|
}
|
|
2237
2275
|
|
|
2238
|
-
// You can also specify the type for the results
|
|
2276
|
+
// You can also specify the type for the results.
|
|
2239
2277
|
// bad 👎👎👎
|
|
2240
2278
|
const users = await new User()
|
|
2241
2279
|
.relations('phone','phones')
|
|
@@ -2256,8 +2294,8 @@ for(const user of users) {
|
|
|
2256
2294
|
.relationQuery('phone' , (query : Phone) => query.relations('user'))
|
|
2257
2295
|
.relationQuery('phones' , (query : Phone) => query.relations('user'))
|
|
2258
2296
|
.findMany<{
|
|
2259
|
-
phone : Partial<
|
|
2260
|
-
phones : (Partial<
|
|
2297
|
+
phone : Partial<TSchemaPhone> & { user : TSchemaUser};
|
|
2298
|
+
phones : (Partial<TSchemaPhone> & { user : TSchemaUser})[];
|
|
2261
2299
|
}>()
|
|
2262
2300
|
|
|
2263
2301
|
for(const user of users) {
|
|
@@ -2273,7 +2311,7 @@ for(const user of users) {
|
|
|
2273
2311
|
// If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
|
|
2274
2312
|
|
|
2275
2313
|
import { Model , Blueprint , SchemaType , RelationType } from 'tspace-mysql'
|
|
2276
|
-
import { Phone ,
|
|
2314
|
+
import { Phone , TSchemaPhone } from '../Phone'
|
|
2277
2315
|
|
|
2278
2316
|
const schemaUser = {
|
|
2279
2317
|
id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
@@ -2286,14 +2324,15 @@ const schemaUser = {
|
|
|
2286
2324
|
updatedAt :new Blueprint().timestamp().null()
|
|
2287
2325
|
}
|
|
2288
2326
|
|
|
2289
|
-
type
|
|
2327
|
+
type TSchemaUser = SchemaType<typeof schemaUser>
|
|
2290
2328
|
|
|
2291
2329
|
type RelationUserType = RelationType<{
|
|
2292
|
-
|
|
2293
|
-
|
|
2330
|
+
phones : TSchemaPhone[]
|
|
2331
|
+
phone : TSchemaPhone
|
|
2294
2332
|
}>
|
|
2295
2333
|
|
|
2296
|
-
|
|
2334
|
+
// Add this '<TSchemaUser, RelationUserType>' to activate the type for the Model.
|
|
2335
|
+
class User extends Model<TSchemaUser, RelationUserType> {
|
|
2297
2336
|
constructor() {
|
|
2298
2337
|
super()
|
|
2299
2338
|
this.useSchema(schemaUser)
|
|
@@ -2304,7 +2343,7 @@ class User extends Model<SchemaUserType, RelationUserType> {
|
|
|
2304
2343
|
}
|
|
2305
2344
|
}
|
|
2306
2345
|
|
|
2307
|
-
export { User ,
|
|
2346
|
+
export { User , TSchemaUser }
|
|
2308
2347
|
|
|
2309
2348
|
+--------------------------------------------------------------------------+
|
|
2310
2349
|
|
|
@@ -2320,13 +2359,13 @@ const schemaPhone = {
|
|
|
2320
2359
|
updatedAt :new Blueprint().timestamp().null()
|
|
2321
2360
|
}
|
|
2322
2361
|
|
|
2323
|
-
type
|
|
2362
|
+
type TSchemaPhone = SchemaType<typeof schemaPhone>
|
|
2324
2363
|
|
|
2325
2364
|
type RelationPhoneType = RelationType<{
|
|
2326
|
-
|
|
2365
|
+
user : TSchemaUser[]
|
|
2327
2366
|
}>
|
|
2328
2367
|
|
|
2329
|
-
class Phone extends Model<
|
|
2368
|
+
class Phone extends Model<TSchemaPhone,RelationPhoneType> {
|
|
2330
2369
|
constructor() {
|
|
2331
2370
|
super()
|
|
2332
2371
|
this.useSchema(schemaPhone)
|
|
@@ -2335,7 +2374,7 @@ class Phone extends Model<SchemaPhoneType,RelationPhoneType> {
|
|
|
2335
2374
|
}
|
|
2336
2375
|
}
|
|
2337
2376
|
|
|
2338
|
-
export { Phone ,
|
|
2377
|
+
export { Phone , TSchemaPhone }
|
|
2339
2378
|
|
|
2340
2379
|
+--------------------------------------------------------------------------+
|
|
2341
2380
|
|
package/build/lib/Interface.d.ts
CHANGED
|
@@ -167,17 +167,29 @@ export interface GlobalSetting {
|
|
|
167
167
|
logger?: boolean;
|
|
168
168
|
}
|
|
169
169
|
export interface Operator {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
170
|
+
eq: string;
|
|
171
|
+
notEq: string;
|
|
172
|
+
more: string;
|
|
173
|
+
less: string;
|
|
174
|
+
moreOrEq: string;
|
|
175
|
+
lessOrEq: string;
|
|
176
176
|
like: string;
|
|
177
177
|
notLike: string;
|
|
178
178
|
in: string;
|
|
179
179
|
notIn: string;
|
|
180
180
|
isNull: string;
|
|
181
181
|
isNotNull: string;
|
|
182
|
+
'|eq': string;
|
|
183
|
+
'|notEq': string;
|
|
184
|
+
'|more': string;
|
|
185
|
+
'|less': string;
|
|
186
|
+
'|moreOrEq': string;
|
|
187
|
+
'|lessOrEq': string;
|
|
188
|
+
'|like': string;
|
|
189
|
+
'|notLike': string;
|
|
190
|
+
'|in': string;
|
|
191
|
+
'|notIn': string;
|
|
192
|
+
'|isNull': string;
|
|
193
|
+
'|isNotNull': string;
|
|
182
194
|
}
|
|
183
195
|
export type PoolEvent = 'CONNECTION' | 'RELEASE' | 'QUERY' | 'SLOW_QUERY' | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE';
|
|
@@ -28,7 +28,7 @@ declare abstract class AbstractModel<T, R> extends Builder {
|
|
|
28
28
|
updated: Function;
|
|
29
29
|
deleted: Function;
|
|
30
30
|
}) | undefined;
|
|
31
|
-
protected abstract column<K extends keyof T
|
|
31
|
+
protected abstract column<K extends keyof T | `${string}.${string}`>(key: K): K;
|
|
32
32
|
protected abstract useUUID(): this;
|
|
33
33
|
protected abstract usePrimaryKey(primaryKey: string): this;
|
|
34
34
|
protected abstract useRegistry(): this;
|
|
@@ -2,6 +2,12 @@ import { AbstractBuilder } from './Abstracts/AbstractBuilder';
|
|
|
2
2
|
import { Pagination, ConnectionOptions, Connection, ConnectionTransaction, NonEmptyArray } from '../Interface';
|
|
3
3
|
declare class Builder extends AbstractBuilder {
|
|
4
4
|
constructor();
|
|
5
|
+
/**
|
|
6
|
+
* The 'instance' method is used get instance.
|
|
7
|
+
* @static
|
|
8
|
+
* @return {Builder} instance of the Builder
|
|
9
|
+
*/
|
|
10
|
+
static get instance(): Builder;
|
|
5
11
|
/**
|
|
6
12
|
* The 'distinct' method is used to apply the DISTINCT keyword to a database query.
|
|
7
13
|
*
|
|
@@ -976,13 +982,6 @@ declare class Builder extends AbstractBuilder {
|
|
|
976
982
|
* @return {promise<any>}
|
|
977
983
|
*/
|
|
978
984
|
rawQuery(sql: string): Promise<any>;
|
|
979
|
-
/**
|
|
980
|
-
* This 'query' method is used to execute sql statement
|
|
981
|
-
*
|
|
982
|
-
* @param {string} sql
|
|
983
|
-
* @return {promise<any>}
|
|
984
|
-
*/
|
|
985
|
-
query(sql: string): Promise<any>;
|
|
986
985
|
/**
|
|
987
986
|
*
|
|
988
987
|
* plus value then update
|
|
@@ -1343,6 +1342,10 @@ declare class Builder extends AbstractBuilder {
|
|
|
1343
1342
|
private _insertNotExists;
|
|
1344
1343
|
private _insert;
|
|
1345
1344
|
protected _checkValueHasRaw(value: any): string;
|
|
1345
|
+
protected _checkValueHasOp(str: string): {
|
|
1346
|
+
op: string;
|
|
1347
|
+
value: string;
|
|
1348
|
+
} | null;
|
|
1346
1349
|
private _insertMultiple;
|
|
1347
1350
|
private _insertOrSelect;
|
|
1348
1351
|
private _updateOrInsert;
|
|
@@ -26,11 +26,20 @@ const utils_1 = require("../utils");
|
|
|
26
26
|
const constants_1 = require("../constants");
|
|
27
27
|
const DB_1 = require("./DB");
|
|
28
28
|
const connection_1 = require("../connection");
|
|
29
|
+
const State_1 = require("./Handlers/State");
|
|
29
30
|
class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
30
31
|
constructor() {
|
|
31
32
|
super();
|
|
32
33
|
this._initialConnection();
|
|
33
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* The 'instance' method is used get instance.
|
|
37
|
+
* @static
|
|
38
|
+
* @return {Builder} instance of the Builder
|
|
39
|
+
*/
|
|
40
|
+
static get instance() {
|
|
41
|
+
return new this();
|
|
42
|
+
}
|
|
34
43
|
/**
|
|
35
44
|
* The 'distinct' method is used to apply the DISTINCT keyword to a database query.
|
|
36
45
|
*
|
|
@@ -350,15 +359,60 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
350
359
|
for (const column in columns) {
|
|
351
360
|
const operator = '=';
|
|
352
361
|
const value = this.$utils.escape(columns[column]);
|
|
353
|
-
this
|
|
354
|
-
|
|
355
|
-
[
|
|
356
|
-
this.$state.get('WHERE')
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
+
const useOp = this._checkValueHasOp(value);
|
|
363
|
+
if (useOp == null) {
|
|
364
|
+
this.$state.set('WHERE', [
|
|
365
|
+
...this.$state.get('WHERE'),
|
|
366
|
+
[
|
|
367
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
368
|
+
`${this.bindColumn(String(column))}`,
|
|
369
|
+
`${operator}`,
|
|
370
|
+
`${this._checkValueHasRaw(value)}`
|
|
371
|
+
].join(' ')
|
|
372
|
+
]);
|
|
373
|
+
return this;
|
|
374
|
+
}
|
|
375
|
+
switch (useOp.op) {
|
|
376
|
+
case 'IN': {
|
|
377
|
+
this.whereIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(','));
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
case '|IN': {
|
|
381
|
+
this.orWhereIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(','));
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
case 'NOT IN': {
|
|
385
|
+
this.whereNotIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(','));
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
case '|NOT IN': {
|
|
389
|
+
this.orWhereNotIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(','));
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
case 'IS NULL': {
|
|
393
|
+
this.whereNull(column);
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
case '|IS NULL': {
|
|
397
|
+
this.orWhereNull(column);
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case 'IS NOT NULL': {
|
|
401
|
+
this.whereNotNull(column);
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
case '|IS NOT NULL': {
|
|
405
|
+
this.orWhereNotNull(column);
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
default: {
|
|
409
|
+
if (useOp.op.includes('|')) {
|
|
410
|
+
this.orWhere(column, useOp.op.replace('|', ''), useOp.value);
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
this.where(column, useOp.op, useOp.value);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
362
416
|
}
|
|
363
417
|
return this;
|
|
364
418
|
}
|
|
@@ -2155,17 +2209,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2155
2209
|
return yield this._queryStatement(sql);
|
|
2156
2210
|
});
|
|
2157
2211
|
}
|
|
2158
|
-
/**
|
|
2159
|
-
* This 'query' method is used to execute sql statement
|
|
2160
|
-
*
|
|
2161
|
-
* @param {string} sql
|
|
2162
|
-
* @return {promise<any>}
|
|
2163
|
-
*/
|
|
2164
|
-
query(sql) {
|
|
2165
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
2166
|
-
return yield this._queryStatement(sql);
|
|
2167
|
-
});
|
|
2168
|
-
}
|
|
2169
2212
|
/**
|
|
2170
2213
|
*
|
|
2171
2214
|
* plus value then update
|
|
@@ -3355,9 +3398,26 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3355
3398
|
});
|
|
3356
3399
|
}
|
|
3357
3400
|
_checkValueHasRaw(value) {
|
|
3358
|
-
|
|
3401
|
+
const detectedValue = typeof value === 'string' && value.startsWith(this.$constants('RAW'))
|
|
3359
3402
|
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
|
|
3360
3403
|
: `'${this.$utils.covertBooleanToNumber(value)}'`;
|
|
3404
|
+
return detectedValue;
|
|
3405
|
+
}
|
|
3406
|
+
_checkValueHasOp(str) {
|
|
3407
|
+
var _a;
|
|
3408
|
+
if (!str.includes(this.$constants('OP')) || !str.includes(this.$constants('VALUE'))) {
|
|
3409
|
+
return null;
|
|
3410
|
+
}
|
|
3411
|
+
const opRegex = new RegExp(`\\${this.$constants('OP')}\\(([^)]+)\\)`);
|
|
3412
|
+
const valueRegex = new RegExp(`\\${this.$constants('VALUE')}\\(([^)]+)\\)`);
|
|
3413
|
+
const opMatch = str.match(opRegex);
|
|
3414
|
+
const valueMatch = str.match(valueRegex);
|
|
3415
|
+
const op = opMatch ? opMatch[1] : '';
|
|
3416
|
+
const value = valueMatch ? valueMatch[1] : '';
|
|
3417
|
+
return {
|
|
3418
|
+
op: op.replace(this.$constants('OP'), ''),
|
|
3419
|
+
value: (_a = value === null || value === void 0 ? void 0 : value.replace(this.$constants('VALUE'), '')) !== null && _a !== void 0 ? _a : ''
|
|
3420
|
+
};
|
|
3361
3421
|
}
|
|
3362
3422
|
_insertMultiple() {
|
|
3363
3423
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -3597,6 +3657,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3597
3657
|
}
|
|
3598
3658
|
};
|
|
3599
3659
|
})();
|
|
3660
|
+
this.$state = new State_1.StateHandler('default');
|
|
3600
3661
|
this.$logger = (() => {
|
|
3601
3662
|
let logger = [];
|
|
3602
3663
|
return {
|