tspace-mysql 1.7.9 → 1.8.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 +44 -14
- package/build/lib/connection/index.d.ts +0 -1
- package/build/lib/connection/index.js +12 -31
- package/build/lib/connection/index.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractModel.d.ts +15 -15
- package/build/lib/core/Abstracts/AbstractModel.js.map +1 -1
- package/build/lib/core/Builder.d.ts +1 -1
- package/build/lib/core/Builder.js +10 -7
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/Handlers/Relation.d.ts +1 -0
- package/build/lib/core/Handlers/Relation.js +20 -0
- package/build/lib/core/Handlers/Relation.js.map +1 -1
- package/build/lib/core/Handlers/State.d.ts +1 -1
- package/build/lib/core/Handlers/State.js +1 -1
- package/build/lib/core/Handlers/State.js.map +1 -1
- package/build/lib/core/Model.d.ts +41 -23
- package/build/lib/core/Model.js +72 -30
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/core/Operator.d.ts +2 -0
- package/build/lib/core/Operator.js +4 -1
- package/build/lib/core/Operator.js.map +1 -1
- package/build/lib/core/UtilityTypes.d.ts +11 -0
- package/build/lib/types.d.ts +15 -9
- package/build/lib/utils/index.d.ts +1 -0
- package/build/lib/utils/index.js +8 -1
- package/build/lib/utils/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,26 @@
|
|
|
5
5
|
|
|
6
6
|
tspace-mysql is an Object-Relational Mapping (ORM) tool designed to run seamlessly in Node.js and is fully compatible with TypeScript. It consistently supports the latest features in both TypeScript and JavaScript, providing additional functionalities to enhance your development experience.
|
|
7
7
|
|
|
8
|
+
## Feature
|
|
9
|
+
|
|
10
|
+
| **Feature** | **Description** |
|
|
11
|
+
|-------------------------------|----------------------------------------------------------------------------------------------------------|
|
|
12
|
+
| **Query Builder** | Create flexible queries like `SELECT`, `INSERT`, `UPDATE`, and `DELETE`. You can also use raw SQL. |
|
|
13
|
+
| **Join Clauses** | Use `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, and `CROSS JOIN` to combine data from multiple tables. |
|
|
14
|
+
| **Model** | Provides a way to interact with database records as objects in code. You can perform create, read, update, and delete (CRUD) operations. Models also support soft deletes and relationship methods. |
|
|
15
|
+
| **Schema** | Allows you to define and manage the structure of MySQL tables, including data types and relationships. Supports migrations and validation. |
|
|
16
|
+
| **Validation** | Automatically checks data against defined rules before saving it to the database, ensuring data integrity and correctness. |
|
|
17
|
+
| **Sync** | Synchronizes the model structure with the database, updating the schema to match the model definitions automatically. |
|
|
18
|
+
| **Soft Deletes** | Marks records as deleted without removing them from the database. This allows for recovery and auditing later. |
|
|
19
|
+
| **Relationships** | Set up connections between models, such as one-to-one, one-to-many, belongs-to, and many-to-many. Supports nested relationships and checks. |
|
|
20
|
+
| **Type Safety** | Ensures that queries are safer by checking the types of statements like `SELECT`, `ORDER BY`, `GROUP BY`, and `WHERE`. |
|
|
21
|
+
| **Repository** | Follows a pattern for managing database operations like `SELECT`, `INSERT`, `UPDATE`, and `DELETE`. It helps keep the code organized. |
|
|
22
|
+
| **Decorators** | Use decorators to add extra functionality or information to model classes and methods, making the code easier to read. |
|
|
23
|
+
| **Caching** | Improves performance by storing frequently requested data. Supports in-memory caching (like memory DB) and Redis for distributed caching. |
|
|
24
|
+
| **Migrations** | Use CLI commands to create models, make migrations, and apply changes to the database structure. |
|
|
25
|
+
| **Blueprints** | Create a clear layout of the database structure and how models and tables relate to each other. |
|
|
26
|
+
| **CLI** | A Command Line Interface for managing models, running migrations, executing queries, and performing other tasks using commands (like `make:model`, `migrate`, and `query`). |
|
|
27
|
+
|
|
8
28
|
## Install
|
|
9
29
|
|
|
10
30
|
Install with [npm](https://www.npmjs.com/):
|
|
@@ -939,7 +959,7 @@ const usersUnsetWhereStatement = await userInstance.unset({ select : true, where
|
|
|
939
959
|
```js
|
|
940
960
|
|
|
941
961
|
const user = await new User()
|
|
942
|
-
.CTEs('
|
|
962
|
+
.CTEs('z', (query) => {
|
|
943
963
|
return query
|
|
944
964
|
.from('posts')
|
|
945
965
|
})
|
|
@@ -947,9 +967,9 @@ const user = await new User()
|
|
|
947
967
|
return query
|
|
948
968
|
.from('post_user')
|
|
949
969
|
})
|
|
950
|
-
.select('users.*','x.*','
|
|
970
|
+
.select('users.*','x.*','z.*')
|
|
951
971
|
.join('users.id','x.user_id')
|
|
952
|
-
.join('users.id','
|
|
972
|
+
.join('users.id','z.user_id')
|
|
953
973
|
.findOne()
|
|
954
974
|
|
|
955
975
|
// WITH z AS (SELECT posts.* FROM `posts`),
|
|
@@ -2458,7 +2478,7 @@ class User extends Model {
|
|
|
2458
2478
|
this.hasMany({ name: "posts", model: Post });
|
|
2459
2479
|
|
|
2460
2480
|
// if you need to initialize data when creating the table, you can use the following.
|
|
2461
|
-
this.
|
|
2481
|
+
this.whenCreatingTable(async () => {
|
|
2462
2482
|
return await new User()
|
|
2463
2483
|
.create({
|
|
2464
2484
|
...columns,
|
|
@@ -2545,7 +2565,7 @@ Type safety still works when you add additional types to your model, using the f
|
|
|
2545
2565
|
|
|
2546
2566
|
```js
|
|
2547
2567
|
// in file User.ts
|
|
2548
|
-
import { Model , Blueprint , TSchema } from 'tspace-mysql'
|
|
2568
|
+
import { Model , Blueprint , TSchema , TSchemaStatic } from 'tspace-mysql'
|
|
2549
2569
|
import Phone from '../Phone'
|
|
2550
2570
|
|
|
2551
2571
|
const schemaUser = {
|
|
@@ -2559,7 +2579,11 @@ const schemaUser = {
|
|
|
2559
2579
|
updatedAt :Blueprint.timestamp().null()
|
|
2560
2580
|
}
|
|
2561
2581
|
|
|
2562
|
-
type TSchemaUser =
|
|
2582
|
+
type TSchemaUser = TSchemaStatic<typeof schemaUser>
|
|
2583
|
+
// TSchemaUser = TSchema<typeof schemaUser>
|
|
2584
|
+
|
|
2585
|
+
// TSchema allowed to set any new keys without in the schema to results
|
|
2586
|
+
// TSchemaStatic not allowed to set any new keys without in the schema to results
|
|
2563
2587
|
|
|
2564
2588
|
class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate the type for the Model.
|
|
2565
2589
|
constructor() {
|
|
@@ -2576,7 +2600,7 @@ export default User
|
|
|
2576
2600
|
+--------------------------------------------------------------------------+
|
|
2577
2601
|
|
|
2578
2602
|
// in file Phone.ts
|
|
2579
|
-
import { Model , Blueprint , TSchema } from 'tspace-mysql'
|
|
2603
|
+
import { Model , Blueprint , TSchema , TSchemaStatic } from 'tspace-mysql'
|
|
2580
2604
|
import { User } from './User.ts'
|
|
2581
2605
|
const schemaPhone = {
|
|
2582
2606
|
id :Blueprint.int().notNull().primary().autoIncrement(),
|
|
@@ -2587,7 +2611,7 @@ const schemaPhone = {
|
|
|
2587
2611
|
updatedAt :Blueprint.timestamp().null()
|
|
2588
2612
|
}
|
|
2589
2613
|
|
|
2590
|
-
type TSchemaPhone =
|
|
2614
|
+
type TSchemaPhone = TSchemaStatic<typeof schemaPhone>
|
|
2591
2615
|
|
|
2592
2616
|
class Phone extends Model<TSchemaPhone> {
|
|
2593
2617
|
constructor() {
|
|
@@ -2609,12 +2633,18 @@ export default Phone
|
|
|
2609
2633
|
import { User } from './User.ts'
|
|
2610
2634
|
import { Phone } from './Phone.ts'
|
|
2611
2635
|
|
|
2612
|
-
const
|
|
2613
|
-
const
|
|
2636
|
+
const user = await new User().select('id','username').findOne() ✅
|
|
2637
|
+
const user = await new User().select('idx','username').findOne() ❌
|
|
2614
2638
|
|
|
2615
|
-
const
|
|
2616
|
-
const
|
|
2639
|
+
const user = await new User().except('id','username').findOne() ✅
|
|
2640
|
+
const user = await new User().except('idx','username').findOne() ❌
|
|
2617
2641
|
|
|
2642
|
+
// TSchemaStatic not allowed to set any new keys without in the schema to results
|
|
2643
|
+
user.withoutSchema = 1 ✅ // TSchema<User>
|
|
2644
|
+
user.withoutSchema = 1 ❌ // TSchemaStatic<User>
|
|
2645
|
+
// But can you make like this for cases
|
|
2646
|
+
const user = await new User().except('idx','username').findOne<{ withoutSchema : number }>()
|
|
2647
|
+
user.withoutSchema = 1 ✅
|
|
2618
2648
|
```
|
|
2619
2649
|
|
|
2620
2650
|
### Safety OrderBy
|
|
@@ -2818,7 +2848,7 @@ for(const user of users) {
|
|
|
2818
2848
|
+--------------------------------------------------------------------------+
|
|
2819
2849
|
// If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
|
|
2820
2850
|
|
|
2821
|
-
import { Model , Blueprint , TSchema , TRelation } from 'tspace-mysql'
|
|
2851
|
+
import { Model , Blueprint , TSchema , TSchemaStatic , TRelation } from 'tspace-mysql'
|
|
2822
2852
|
import { Phone } from '../Phone'
|
|
2823
2853
|
|
|
2824
2854
|
const schemaUser = {
|
|
@@ -2832,7 +2862,7 @@ const schemaUser = {
|
|
|
2832
2862
|
updatedAt :Blueprint.timestamp().null()
|
|
2833
2863
|
}
|
|
2834
2864
|
|
|
2835
|
-
type TSchemaUser =
|
|
2865
|
+
type TSchemaUser = TSchemaStatic<typeof schemaUser>
|
|
2836
2866
|
|
|
2837
2867
|
type TRelationUser = TRelation<{
|
|
2838
2868
|
phones : Phone[]
|
|
@@ -97,39 +97,37 @@ class PoolConnection extends events_1.EventEmitter {
|
|
|
97
97
|
});
|
|
98
98
|
},
|
|
99
99
|
connection: () => {
|
|
100
|
-
const transaction = this._transaction();
|
|
101
100
|
return new Promise((resolve, reject) => {
|
|
102
101
|
pool.getConnection((err, connection) => {
|
|
103
102
|
if (err)
|
|
104
103
|
return reject(err);
|
|
105
104
|
const query = (sql) => {
|
|
106
105
|
const start = Date.now();
|
|
107
|
-
return new Promise((
|
|
108
|
-
if (!transaction.state()) {
|
|
109
|
-
return reject(new Error('The transaction has either been closed or has not started.'));
|
|
110
|
-
}
|
|
106
|
+
return new Promise((resolveQ, rejectQ) => {
|
|
111
107
|
connection.query(sql, (err, results) => {
|
|
112
108
|
connection.release();
|
|
113
|
-
if (err)
|
|
114
|
-
return
|
|
109
|
+
if (err) {
|
|
110
|
+
return rejectQ(err);
|
|
111
|
+
}
|
|
115
112
|
this._detectEventQuery({ start, sql, results });
|
|
116
|
-
return
|
|
113
|
+
return resolveQ(results);
|
|
117
114
|
});
|
|
118
115
|
});
|
|
119
116
|
};
|
|
120
117
|
const startTransaction = () => __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
// don't use await as it blocks all connection pools. and ignore the .bind(...) method.
|
|
119
|
+
query('START TRANSACTION')
|
|
120
|
+
.catch(err => reject(err));
|
|
123
121
|
return;
|
|
124
122
|
});
|
|
125
123
|
const commit = () => __awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
yield query('COMMIT')
|
|
127
|
-
|
|
124
|
+
yield query('COMMIT')
|
|
125
|
+
.catch(err => reject(err));
|
|
128
126
|
return;
|
|
129
127
|
});
|
|
130
128
|
const rollback = () => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
yield query('ROLLBACK')
|
|
132
|
-
|
|
129
|
+
yield query('ROLLBACK')
|
|
130
|
+
.catch(err => reject(err));
|
|
133
131
|
return;
|
|
134
132
|
});
|
|
135
133
|
return resolve({
|
|
@@ -144,23 +142,6 @@ class PoolConnection extends events_1.EventEmitter {
|
|
|
144
142
|
}
|
|
145
143
|
};
|
|
146
144
|
}
|
|
147
|
-
_transaction() {
|
|
148
|
-
class Transaction {
|
|
149
|
-
constructor() {
|
|
150
|
-
this.on = false;
|
|
151
|
-
}
|
|
152
|
-
state() {
|
|
153
|
-
return this.on;
|
|
154
|
-
}
|
|
155
|
-
start() {
|
|
156
|
-
this.on = true;
|
|
157
|
-
}
|
|
158
|
-
close() {
|
|
159
|
-
this.on = false;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return new Transaction();
|
|
163
|
-
}
|
|
164
145
|
_detectEventQuery({ start, sql, results }) {
|
|
165
146
|
const duration = Date.now() - start;
|
|
166
147
|
if (duration > 1000 * 10) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/connection/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAiC;AACjC,gDAAmC;AACnC,mCAAqC;AACrC,mCAKe;AAEf,sDAEmB;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/connection/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAiC;AACjC,gDAAmC;AACnC,mCAAqC;AACrC,mCAKe;AAEf,sDAEmB;AAyXV,uGA1XL,gCAAsB,OA0XK;AAjX/B,MAAa,cAAe,SAAQ,qBAAY;IAI5C;;;OAGG;IACH,YAAY,OAAmB;QAC3B,KAAK,EAAE,CAAA;QAPH,YAAO,GAAG,IAAI,CAAC,YAAY,EAAE,CAAA;QAQjC,IAAG,OAAO,EAAE,CAAC;YACT,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,CAClB,MAAM,CAAC,OAAO,iCACP,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,GAChC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,EACxC,CACL,CAAA;QACL,CAAC;IACL,CAAC;IACD;;;;;;OAMG;IACI,IAAI;QAEP,MAAM,IAAI,GAAW,IAAA,mBAAU,EAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;QAEjE;;;;WAIG;QACH,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;QAEzB,IAAI,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,UAAU,EAAE,EAAE;YAC9B,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,UAAU,CAAC,CAAA;QACpC,CAAC,CAAC,CAAA;QAEF,OAAO;YACH,EAAE,EAAG,CAAC,KAAkB,EAAG,IAAI,EAAE,EAAE;gBAC/B,OAAO,IAAI,CA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B,CAAC;CACJ;AArWD,wCAqWC;AAED;;;;;;;GAOG;AACH,MAAM,IAAI,GAAG,IAAI,cAAc,EAAE,CAAC,IAAI,EAAE,CAAA;AAGvB,oBAAI;AACrB,kBAAe,IAAI,CAAA"}
|
|
@@ -2,7 +2,7 @@ import { Blueprint } from '../Blueprint';
|
|
|
2
2
|
import { Builder } from '../Builder';
|
|
3
3
|
import { RelationHandler } from '../Handlers/Relation';
|
|
4
4
|
import { Model } from '../Model';
|
|
5
|
-
import type { TPattern, TRelationOptions, TRelationQueryOptions, TValidateSchema, TCache } from '../../types';
|
|
5
|
+
import type { TPattern, TRelationOptions, TRelationQueryOptions, TValidateSchema, TCache, TRelationKeys } from '../../types';
|
|
6
6
|
declare abstract class AbstractModel<T, R> extends Builder {
|
|
7
7
|
protected $cache: TCache;
|
|
8
8
|
protected $relation: RelationHandler;
|
|
@@ -49,7 +49,7 @@ declare abstract class AbstractModel<T, R> extends Builder {
|
|
|
49
49
|
protected abstract hasMany({ name, model, localKey, foreignKey, freezeTable, as }: TRelationOptions): this;
|
|
50
50
|
protected abstract belongsTo({ name, model, localKey, foreignKey, freezeTable, as }: TRelationOptions): this;
|
|
51
51
|
protected abstract belongsToMany({ name, model, localKey, foreignKey, freezeTable, as }: TRelationOptions): this;
|
|
52
|
-
protected abstract buildMethodRelation<K extends R extends object ?
|
|
52
|
+
protected abstract buildMethodRelation<K extends R extends object ? TRelationKeys<R> : string>(name: K, callback?: Function): this;
|
|
53
53
|
protected abstract hasOneBuilder({ name, model, localKey, foreignKey, freezeTable, as }: TRelationQueryOptions, callback: Function): this;
|
|
54
54
|
protected abstract hasManyBuilder({ name, model, localKey, foreignKey, freezeTable, as }: TRelationQueryOptions, callback: Function): this;
|
|
55
55
|
protected abstract belongsToBuilder({ name, model, localKey, foreignKey, freezeTable, as }: TRelationQueryOptions, callback: Function): this;
|
|
@@ -60,19 +60,19 @@ declare abstract class AbstractModel<T, R> extends Builder {
|
|
|
60
60
|
abstract onlyTrashed(): this;
|
|
61
61
|
abstract trashed(): this;
|
|
62
62
|
abstract restore(): Promise<T[]>;
|
|
63
|
-
abstract with<K extends R extends object ?
|
|
64
|
-
abstract withQuery<K extends R extends object ?
|
|
65
|
-
abstract withExists<K extends R extends object ?
|
|
66
|
-
abstract withTrashed<K extends R extends object ?
|
|
67
|
-
abstract withAll<K extends R extends object ?
|
|
68
|
-
abstract withCount<K extends R extends object ?
|
|
69
|
-
abstract has<K extends R extends object ?
|
|
70
|
-
abstract relations<K extends R extends object ?
|
|
71
|
-
abstract relationQuery<K extends R extends object ?
|
|
72
|
-
abstract relationsExists<K extends R extends object ?
|
|
73
|
-
abstract relationsAll<K extends R extends object ?
|
|
74
|
-
abstract relationsCount<K extends R extends object ?
|
|
75
|
-
abstract relationsTrashed<K extends R extends object ?
|
|
63
|
+
abstract with<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
64
|
+
abstract withQuery<K extends R extends object ? TRelationKeys<R> : string, TModel extends Model>(nameRelations: K, callback: (query: TModel) => TModel): this;
|
|
65
|
+
abstract withExists<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
66
|
+
abstract withTrashed<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
67
|
+
abstract withAll<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
68
|
+
abstract withCount<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
69
|
+
abstract has<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
70
|
+
abstract relations<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
71
|
+
abstract relationQuery<K extends R extends object ? TRelationKeys<R> : string, TModel extends Model>(nameRelations: K, callback: (query: TModel) => TModel): this;
|
|
72
|
+
abstract relationsExists<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
73
|
+
abstract relationsAll<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
74
|
+
abstract relationsCount<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
75
|
+
abstract relationsTrashed<K extends R extends object ? TRelationKeys<R> : string>(...nameRelations: K[]): this;
|
|
76
76
|
}
|
|
77
77
|
export { AbstractModel };
|
|
78
78
|
export default AbstractModel;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractModel.js","sourceRoot":"","sources":["../../../../src/lib/core/Abstracts/AbstractModel.ts"],"names":[],"mappings":";;;AACA,wCAA6C;
|
|
1
|
+
{"version":3,"file":"AbstractModel.js","sourceRoot":"","sources":["../../../../src/lib/core/Abstracts/AbstractModel.ts"],"names":[],"mappings":";;;AACA,wCAA6C;AAY7C,MAAe,aAAmB,SAAQ,iBAAO;CA2EhD;AAEQ,sCAAa;AACtB,kBAAe,aAAa,CAAA"}
|
|
@@ -1501,7 +1501,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1501
1501
|
private _queryUpdate;
|
|
1502
1502
|
private _queryInsert;
|
|
1503
1503
|
private _queryInsertMultiple;
|
|
1504
|
-
protected _valueAndOperator(value: string, operator: string, useDefault?: boolean):
|
|
1504
|
+
protected _valueAndOperator(value: string, operator: string, useDefault?: boolean): any[];
|
|
1505
1505
|
private _handleJoin;
|
|
1506
1506
|
private _initialConnection;
|
|
1507
1507
|
}
|
|
@@ -777,7 +777,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
777
777
|
*/
|
|
778
778
|
whereIn(column, array) {
|
|
779
779
|
if (!Array.isArray(array))
|
|
780
|
-
|
|
780
|
+
array = [array];
|
|
781
781
|
const values = array.length
|
|
782
782
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
783
783
|
: this.$constants(this.$constants('NULL'));
|
|
@@ -802,7 +802,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
802
802
|
*/
|
|
803
803
|
orWhereIn(column, array) {
|
|
804
804
|
if (!Array.isArray(array))
|
|
805
|
-
|
|
805
|
+
array = [array];
|
|
806
806
|
const values = array.length
|
|
807
807
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
808
808
|
: this.$constants(this.$constants('NULL'));
|
|
@@ -827,7 +827,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
827
827
|
*/
|
|
828
828
|
whereNotIn(column, array) {
|
|
829
829
|
if (!Array.isArray(array))
|
|
830
|
-
|
|
830
|
+
array = [array];
|
|
831
831
|
if (!array.length)
|
|
832
832
|
return this;
|
|
833
833
|
const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
|
|
@@ -852,7 +852,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
852
852
|
*/
|
|
853
853
|
orWhereNotIn(column, array) {
|
|
854
854
|
if (!Array.isArray(array))
|
|
855
|
-
|
|
855
|
+
array = [array];
|
|
856
856
|
if (!array.length)
|
|
857
857
|
return this;
|
|
858
858
|
const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
|
|
@@ -1854,6 +1854,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1854
1854
|
* @returns {this}
|
|
1855
1855
|
*/
|
|
1856
1856
|
limit(number = 1) {
|
|
1857
|
+
number = this.$utils.softNumber(number);
|
|
1857
1858
|
if (number === -1)
|
|
1858
1859
|
number = Math.pow(2, 31) - 1; // int 32 bit
|
|
1859
1860
|
if (number < 0 || number === -0)
|
|
@@ -1879,6 +1880,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1879
1880
|
* @returns {this}
|
|
1880
1881
|
*/
|
|
1881
1882
|
offset(number = 1) {
|
|
1883
|
+
number = this.$utils.softNumber(number);
|
|
1882
1884
|
if (number < 0 || number === -0)
|
|
1883
1885
|
number = 0;
|
|
1884
1886
|
this.$state.set('OFFSET', `${this.$constants('OFFSET')} ${number}`);
|
|
@@ -3084,9 +3086,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3084
3086
|
return __awaiter(this, arguments, void 0, function* ({ waitMs = 0 } = {}) {
|
|
3085
3087
|
this.$state.set('AFTER_SAVE', waitMs);
|
|
3086
3088
|
switch (this.$state.get('SAVE')) {
|
|
3087
|
-
case 'INSERT_MULTIPLE': return yield this._insertMultiple();
|
|
3088
3089
|
case 'INSERT': return yield this._insert();
|
|
3089
3090
|
case 'UPDATE': return yield this._update();
|
|
3091
|
+
case 'INSERT_MULTIPLE': return yield this._insertMultiple();
|
|
3090
3092
|
case 'INSERT_NOT_EXISTS': return yield this._insertNotExists();
|
|
3091
3093
|
case 'UPDATE_OR_INSERT': return yield this._updateOrInsert();
|
|
3092
3094
|
case 'INSERT_OR_SELECT': return yield this._insertOrSelect();
|
|
@@ -3937,8 +3939,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3937
3939
|
_valueAndOperator(value, operator, useDefault = false) {
|
|
3938
3940
|
if (useDefault)
|
|
3939
3941
|
return [operator, '='];
|
|
3940
|
-
if (operator == null)
|
|
3941
|
-
|
|
3942
|
+
if (operator == null) {
|
|
3943
|
+
return [[], '='];
|
|
3944
|
+
}
|
|
3942
3945
|
if (operator.toUpperCase() === this.$constants('LIKE')) {
|
|
3943
3946
|
operator = operator.toUpperCase();
|
|
3944
3947
|
}
|