tspace-mysql 1.4.8 → 1.4.9
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 +107 -45
- package/dist/lib/Interface.d.ts +16 -1
- package/dist/lib/tspace/Abstracts/AbstractBuilder.d.ts +1 -1
- package/dist/lib/tspace/Abstracts/AbstractModel.d.ts +19 -1
- package/dist/lib/tspace/Blueprint.d.ts +8 -4
- package/dist/lib/tspace/Blueprint.js +30 -18
- package/dist/lib/tspace/Builder.d.ts +4 -4
- package/dist/lib/tspace/Builder.js +8 -34
- package/dist/lib/tspace/Decorator.d.ts +20 -0
- package/dist/lib/tspace/Decorator.js +166 -0
- package/dist/lib/tspace/Handlers/Relation.js +8 -9
- package/dist/lib/tspace/Handlers/State.js +3 -2
- package/dist/lib/tspace/Model.d.ts +205 -2
- package/dist/lib/tspace/Model.js +766 -40
- package/dist/lib/tspace/Schema.js +1 -3
- package/dist/lib/tspace/index.d.ts +2 -0
- package/dist/lib/tspace/index.js +17 -1
- package/dist/lib/utils/index.js +1 -0
- package/dist/tests/01-Pool.test.d.ts +1 -0
- package/dist/tests/01-Pool.test.js +45 -0
- package/dist/tests/02-DB.test.d.ts +1 -0
- package/dist/tests/02-DB.test.js +109 -0
- package/dist/tests/03-Model.test.d.ts +1 -0
- package/dist/tests/03-Model.test.js +73 -0
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com)
|
|
4
4
|
[](https://www.npmjs.com)
|
|
5
5
|
|
|
6
|
-
tspace-mysql is an ORM
|
|
7
|
-
Its always support the latest TypeScript and JavaScript features and provide additional features that help you to develop.
|
|
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.
|
|
8
7
|
|
|
9
8
|
## Install
|
|
10
9
|
|
|
@@ -29,6 +28,7 @@ npm install tspace-mysql --save
|
|
|
29
28
|
- [Deeply Nested Relations](#deeply-nested-relations)
|
|
30
29
|
- [Relation Exists](#relation-exists)
|
|
31
30
|
- [Built in Relation Functions](#built-in-relation-functions)
|
|
31
|
+
- [Decorator](#decorator)
|
|
32
32
|
- [Schema Model](#schema-model)
|
|
33
33
|
- [Validation](#validation)
|
|
34
34
|
- [Sync](#sync)
|
|
@@ -43,7 +43,7 @@ npm install tspace-mysql --save
|
|
|
43
43
|
- [Blueprint](#blueprint)
|
|
44
44
|
|
|
45
45
|
## Configuration
|
|
46
|
-
|
|
46
|
+
To establish a connection, the recommended method for creating your environment variables is by using a '.env' file. using the following:
|
|
47
47
|
```js
|
|
48
48
|
DB_HOST = localhost
|
|
49
49
|
DB_PORT = 3306
|
|
@@ -59,7 +59,7 @@ DB_DATABASE = database
|
|
|
59
59
|
* DB_DATE_STRINGS = true
|
|
60
60
|
*/
|
|
61
61
|
```
|
|
62
|
-
|
|
62
|
+
You can also create a file named 'db.tspace' to configure the connection. using the following:
|
|
63
63
|
```js
|
|
64
64
|
source db {
|
|
65
65
|
host = localhost
|
|
@@ -77,7 +77,7 @@ source db {
|
|
|
77
77
|
|
|
78
78
|
```
|
|
79
79
|
## Running Queries
|
|
80
|
-
Once you have configured your database connection, you
|
|
80
|
+
Once you have configured your database connection, you can execute queries using the following:
|
|
81
81
|
```js
|
|
82
82
|
+-------------+--------------+----------------------------+
|
|
83
83
|
| table users |
|
|
@@ -192,6 +192,19 @@ const hookResult = (result) => console.log('hook!! result => ',result)
|
|
|
192
192
|
const user = await new DB('users').where('id',1).hook(hookResult).findOne()
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
+
Running A Faker
|
|
196
|
+
```js
|
|
197
|
+
|
|
198
|
+
await new DB('users').faker(5)
|
|
199
|
+
// custom some columns
|
|
200
|
+
await new DB('users').faker(5 , (row , index) => {
|
|
201
|
+
return {
|
|
202
|
+
...row,
|
|
203
|
+
custom : 'custom' + index
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
```
|
|
207
|
+
|
|
195
208
|
Running A Insert Query
|
|
196
209
|
```js
|
|
197
210
|
const user = await new DB('users')
|
|
@@ -203,16 +216,6 @@ const user = await new DB('users')
|
|
|
203
216
|
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
204
217
|
|
|
205
218
|
+--------------------------------------------------------------------------+
|
|
206
|
-
|
|
207
|
-
const reposity = new DB('users')
|
|
208
|
-
reposity.name = 'tspace4'
|
|
209
|
-
reposity.email = 'tspace4@gmail.com'
|
|
210
|
-
|
|
211
|
-
await reposity.save()
|
|
212
|
-
|
|
213
|
-
const { result } = reposity
|
|
214
|
-
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
215
|
-
|
|
216
219
|
const users = await new DB('users')
|
|
217
220
|
.createMultiple([
|
|
218
221
|
{
|
|
@@ -271,18 +274,6 @@ const user = await new DB('users')
|
|
|
271
274
|
.save()
|
|
272
275
|
// UPDATE `users` SET `name` = CASE WHEN (`name` = "" OR `name` IS NULL) THEN "tspace1**" ELSE `name` END,`email` = 'tspace1@gmail.com' WHERE `users`.`id` = '1' LIMIT 1;
|
|
273
276
|
|
|
274
|
-
|
|
275
|
-
+--------------------------------------------------------------------------+
|
|
276
|
-
|
|
277
|
-
const reposity = new DB('users').where('id',1)
|
|
278
|
-
reposity.name = 'tspace1++'
|
|
279
|
-
reposity.email = 'tspace1++@gmail.com'
|
|
280
|
-
|
|
281
|
-
await reposity.save()
|
|
282
|
-
// UPDATE `users` SET `name` = 'tspace1**',`email` = 'tspace1@gmail.com' WHERE `users`.`id` = '1' LIMIT 1;
|
|
283
|
-
const { result } = reposity
|
|
284
|
-
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
285
|
-
|
|
286
277
|
```
|
|
287
278
|
Running A Update Or Created Query
|
|
288
279
|
```js
|
|
@@ -304,7 +295,7 @@ const deleted = await new DB('users').where('id',1).delete()
|
|
|
304
295
|
```
|
|
305
296
|
## Database Transactions
|
|
306
297
|
|
|
307
|
-
Within a
|
|
298
|
+
Within a database transaction, you can utilize the following:
|
|
308
299
|
|
|
309
300
|
```js
|
|
310
301
|
const connection = await new DB().beginTransaction()
|
|
@@ -364,7 +355,7 @@ try {
|
|
|
364
355
|
|
|
365
356
|
```
|
|
366
357
|
## Connection
|
|
367
|
-
When establishing a connection, you
|
|
358
|
+
When establishing a connection, you can specify options as follows:
|
|
368
359
|
```js
|
|
369
360
|
const connection = await new DB().getConnection({
|
|
370
361
|
host: 'localhost',
|
|
@@ -381,7 +372,7 @@ const users = await new DB('users')
|
|
|
381
372
|
```
|
|
382
373
|
|
|
383
374
|
## Backup
|
|
384
|
-
|
|
375
|
+
To backup a database, you can perform the following steps:
|
|
385
376
|
```js
|
|
386
377
|
/**
|
|
387
378
|
*
|
|
@@ -418,8 +409,7 @@ const backupToFile = await new DB().backupToFile({
|
|
|
418
409
|
```
|
|
419
410
|
|
|
420
411
|
## Generating Model Classes
|
|
421
|
-
To get started,
|
|
422
|
-
you may use the make:model command to generate a new model:
|
|
412
|
+
To get started, install the 'tspace-mysql' package globally using the following npm command:
|
|
423
413
|
|
|
424
414
|
```js
|
|
425
415
|
/**
|
|
@@ -512,8 +502,8 @@ export { User }
|
|
|
512
502
|
export default User
|
|
513
503
|
```
|
|
514
504
|
## Relationships
|
|
515
|
-
Relationships are defined as methods on your Model classes
|
|
516
|
-
Let's
|
|
505
|
+
Relationships are defined as methods on your Model classes.
|
|
506
|
+
Let's example a basic relationship:
|
|
517
507
|
|
|
518
508
|
## One To One
|
|
519
509
|
A one-to-one relationship is used to define relationships where a single model is the parent to one child models
|
|
@@ -659,8 +649,8 @@ const userUsingFunction = await new User().roles().findOne()
|
|
|
659
649
|
```
|
|
660
650
|
|
|
661
651
|
## Deeply Nested Relations
|
|
662
|
-
Relationships can
|
|
663
|
-
|
|
652
|
+
Relationships can involve deep connections.
|
|
653
|
+
Let's example of a deep relationship:
|
|
664
654
|
```js
|
|
665
655
|
import { Model } from 'tspace-mysql'
|
|
666
656
|
|
|
@@ -735,8 +725,8 @@ await new User()
|
|
|
735
725
|
|
|
736
726
|
```
|
|
737
727
|
## Relation Exists
|
|
738
|
-
Relationships can return only
|
|
739
|
-
|
|
728
|
+
Relationships can return results only if they are not empty in relations, considering soft deletes.
|
|
729
|
+
Let's illustrate this with an example of an existence check in relations:
|
|
740
730
|
```js
|
|
741
731
|
+-------------+--------------+----------------------------+--------------------+
|
|
742
732
|
| table users | |
|
|
@@ -832,8 +822,7 @@ await new User().relationsExists('posts').findMany()
|
|
|
832
822
|
|
|
833
823
|
```
|
|
834
824
|
## Built in Relation Functions
|
|
835
|
-
|
|
836
|
-
let's example a built in function :
|
|
825
|
+
Certainly, let's illustrate the use of a built-in function in the results of relationships:
|
|
837
826
|
```js
|
|
838
827
|
import { Model } from 'tspace-mysql'
|
|
839
828
|
|
|
@@ -871,16 +860,89 @@ for (const post of posts) {
|
|
|
871
860
|
const comments = await post.$comments()
|
|
872
861
|
}
|
|
873
862
|
|
|
863
|
+
```
|
|
864
|
+
|
|
865
|
+
## Decorator
|
|
866
|
+
Decorators can be used in a Model.
|
|
867
|
+
Let's illustrate this with an example of a decorator:
|
|
868
|
+
```js
|
|
869
|
+
|
|
870
|
+
import {
|
|
871
|
+
Blueprint,
|
|
872
|
+
Model ,
|
|
873
|
+
Table ,TableSingular, TablePlural,
|
|
874
|
+
UUID, SoftDelete, Timestamp,
|
|
875
|
+
Column, Pattern, Validate,
|
|
876
|
+
HasMany, HasOne, BelongsTo, BelongsToMany
|
|
877
|
+
|
|
878
|
+
} from 'tspace-mysql'
|
|
879
|
+
import { Post } from './Post'
|
|
880
|
+
import { PostUser } from './PostUser'
|
|
881
|
+
|
|
882
|
+
@Pattern('camelCase')
|
|
883
|
+
@UUID()
|
|
884
|
+
@SoftDelete()
|
|
885
|
+
@Timestamp()
|
|
886
|
+
@Table('users')
|
|
887
|
+
class User extends Model {
|
|
888
|
+
|
|
889
|
+
@Column(() => new Blueprint().int().notNull().primary().autoIncrement())
|
|
890
|
+
public id!: number
|
|
891
|
+
|
|
892
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
893
|
+
public uuid!: string
|
|
894
|
+
|
|
895
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
896
|
+
@Validate({
|
|
897
|
+
type : String,
|
|
898
|
+
require : true,
|
|
899
|
+
length : 50,
|
|
900
|
+
match: /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
901
|
+
unique : true,
|
|
902
|
+
fn : (email : string) => /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email)
|
|
903
|
+
})
|
|
904
|
+
public email!: string
|
|
905
|
+
|
|
906
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
907
|
+
public name !: string
|
|
908
|
+
|
|
909
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
910
|
+
public username !: string
|
|
911
|
+
|
|
912
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
913
|
+
public password !: string
|
|
914
|
+
|
|
915
|
+
@Column(() => new Blueprint().timestamp().null())
|
|
916
|
+
public createdAt!: Date
|
|
917
|
+
|
|
918
|
+
@Column(() => new Blueprint().timestamp().null())
|
|
919
|
+
public updatedAt!: Date
|
|
920
|
+
|
|
921
|
+
@Column(() => new Blueprint().timestamp().null())
|
|
922
|
+
public deletedAt!: Date
|
|
923
|
+
|
|
924
|
+
@HasMany({ model : Post })
|
|
925
|
+
public posts!: Post[]
|
|
926
|
+
|
|
927
|
+
@HasOne({ model : Post })
|
|
928
|
+
public post!: Post
|
|
929
|
+
|
|
930
|
+
@BelongsToMany({ model : Post , modelPivot : PostUser })
|
|
931
|
+
public users!: PostUser[]
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
export { User }
|
|
935
|
+
export default User
|
|
936
|
+
|
|
874
937
|
```
|
|
875
938
|
## Schema Model
|
|
876
|
-
Define the schema of Model
|
|
877
|
-
let's example a validator model:
|
|
939
|
+
Define the schema of a Model
|
|
878
940
|
|
|
879
941
|
## Validation
|
|
880
942
|
Validate the schema of Model
|
|
881
943
|
let's example a validator model:
|
|
882
944
|
```js
|
|
883
|
-
|
|
945
|
+
import { Model , Blueprint , Column } from 'tspace-mysql'
|
|
884
946
|
class User extends Model {
|
|
885
947
|
constructor(){
|
|
886
948
|
super()
|
|
@@ -900,7 +962,7 @@ class User extends Model {
|
|
|
900
962
|
uuid : Number,
|
|
901
963
|
name : {
|
|
902
964
|
type : String,
|
|
903
|
-
length : 191
|
|
965
|
+
length : 191,
|
|
904
966
|
require : true,
|
|
905
967
|
json : true
|
|
906
968
|
},
|
|
@@ -910,7 +972,7 @@ class User extends Model {
|
|
|
910
972
|
length : 191,
|
|
911
973
|
match: /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
912
974
|
unique : true,
|
|
913
|
-
fn : (email : string) =>
|
|
975
|
+
fn : (email : string) => /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email)
|
|
914
976
|
},
|
|
915
977
|
createdAt : Date,
|
|
916
978
|
updatedAt : Date,
|
package/dist/lib/Interface.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export interface Relation {
|
|
|
19
19
|
export interface RelationQuery {
|
|
20
20
|
name?: string;
|
|
21
21
|
model: new () => Model;
|
|
22
|
-
as?: string;
|
|
22
|
+
as?: string | undefined;
|
|
23
23
|
localKey?: string | undefined;
|
|
24
24
|
foreignKey?: string | undefined;
|
|
25
25
|
freezeTable?: string | undefined;
|
|
@@ -29,6 +29,7 @@ export interface RelationQuery {
|
|
|
29
29
|
exists?: boolean | undefined;
|
|
30
30
|
all?: boolean | undefined;
|
|
31
31
|
trashed?: boolean | undefined;
|
|
32
|
+
count?: boolean | undefined;
|
|
32
33
|
oldVersion?: boolean | undefined;
|
|
33
34
|
modelPivot?: new () => Model | undefined;
|
|
34
35
|
}
|
|
@@ -142,3 +143,17 @@ export type ValidateSchema = null | Record<string, NumberConstructor | StringCon
|
|
|
142
143
|
json?: boolean;
|
|
143
144
|
fn?: Function;
|
|
144
145
|
}>;
|
|
146
|
+
export type ValidateSchemaDecorator = NumberConstructor | StringConstructor | DateConstructor | BooleanConstructor | {
|
|
147
|
+
type: NumberConstructor | StringConstructor | DateConstructor | BooleanConstructor;
|
|
148
|
+
require?: boolean;
|
|
149
|
+
match?: RegExp;
|
|
150
|
+
length?: number;
|
|
151
|
+
minLength?: number;
|
|
152
|
+
maxLength?: number;
|
|
153
|
+
min?: number;
|
|
154
|
+
max?: number;
|
|
155
|
+
enum?: string[] | number[] | boolean[];
|
|
156
|
+
unique?: boolean;
|
|
157
|
+
json?: boolean;
|
|
158
|
+
fn?: Function;
|
|
159
|
+
};
|
|
@@ -117,7 +117,7 @@ declare abstract class AbstractBuilder {
|
|
|
117
117
|
abstract save(): Promise<Record<string, any> | any[] | null | undefined>;
|
|
118
118
|
abstract increment(column: string, value: number): Promise<number>;
|
|
119
119
|
abstract decrement(column: string, value: number): Promise<number>;
|
|
120
|
-
abstract faker(round: number): Promise<
|
|
120
|
+
abstract faker(round: number, cb?: Function): Promise<Record<string, any>[]>;
|
|
121
121
|
}
|
|
122
122
|
export { AbstractBuilder };
|
|
123
123
|
export default AbstractBuilder;
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import { Relation, RelationQuery } from '../../Interface';
|
|
1
|
+
import { Pattern, Relation, RelationQuery, ValidateSchema } from '../../Interface';
|
|
2
|
+
import { Blueprint } from '../Blueprint';
|
|
2
3
|
import { Builder } from '../Builder';
|
|
3
4
|
import { RelationHandler } from '../Handlers/Relation';
|
|
4
5
|
declare abstract class AbstractModel extends Builder {
|
|
5
6
|
protected $relation: RelationHandler | undefined;
|
|
7
|
+
protected $schema: Record<string, Blueprint> | undefined;
|
|
8
|
+
protected $validateSchema: ValidateSchema | undefined;
|
|
9
|
+
protected $table: string | undefined;
|
|
10
|
+
protected $pattern: Pattern | undefined;
|
|
11
|
+
protected $hasMany: RelationQuery[] | undefined;
|
|
12
|
+
protected $hasOne: RelationQuery[] | undefined;
|
|
13
|
+
protected $belongsTo: RelationQuery[] | undefined;
|
|
14
|
+
protected $belongsToMany: RelationQuery[] | undefined;
|
|
15
|
+
protected $uuid: boolean | undefined;
|
|
16
|
+
protected $timestamp: boolean | undefined;
|
|
17
|
+
protected $softDelete: boolean | undefined;
|
|
18
|
+
protected $uuidColumn: string | undefined;
|
|
19
|
+
protected $timestampColumns: {
|
|
20
|
+
createdAt: string;
|
|
21
|
+
updatedAt: string;
|
|
22
|
+
} | undefined;
|
|
23
|
+
protected $softDeleteColumn: string | undefined;
|
|
6
24
|
protected abstract useUUID(): this;
|
|
7
25
|
protected abstract usePrimaryKey(primaryKey: string): this;
|
|
8
26
|
protected abstract useRegistry(): this;
|
|
@@ -15,10 +15,10 @@ import { Model } from "./Model";
|
|
|
15
15
|
* })
|
|
16
16
|
*/
|
|
17
17
|
declare class Blueprint {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
private _type;
|
|
19
|
+
private _attributes;
|
|
20
|
+
private _foreignKey;
|
|
21
|
+
private _valueType;
|
|
22
22
|
/**
|
|
23
23
|
* Assign type 'int' in table
|
|
24
24
|
* @return {this} this
|
|
@@ -210,6 +210,10 @@ declare class Blueprint {
|
|
|
210
210
|
onDelete?: 'CASCADE' | 'NO ACTION' | 'RESTRICT' | 'SET NULL';
|
|
211
211
|
onUpdate?: 'CASCADE' | 'NO ACTION' | 'RESTRICT' | 'SET NULL';
|
|
212
212
|
}): this;
|
|
213
|
+
get type(): string;
|
|
214
|
+
get attributes(): string[];
|
|
215
|
+
get foreignKey(): Record<string, any> | null;
|
|
216
|
+
get valueType(): NumberConstructor | StringConstructor | DateConstructor;
|
|
213
217
|
private _addAssignType;
|
|
214
218
|
private _addAssignAttribute;
|
|
215
219
|
}
|
|
@@ -18,10 +18,10 @@ exports.Blueprint = void 0;
|
|
|
18
18
|
*/
|
|
19
19
|
class Blueprint {
|
|
20
20
|
constructor() {
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
21
|
+
this._type = 'INT';
|
|
22
|
+
this._attributes = [];
|
|
23
|
+
this._foreignKey = null;
|
|
24
|
+
this._valueType = String;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Assign type 'int' in table
|
|
@@ -29,7 +29,7 @@ class Blueprint {
|
|
|
29
29
|
*/
|
|
30
30
|
int(_) {
|
|
31
31
|
this._addAssignType('INT');
|
|
32
|
-
this.
|
|
32
|
+
this._valueType = Number;
|
|
33
33
|
return this;
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
@@ -39,7 +39,7 @@ class Blueprint {
|
|
|
39
39
|
*/
|
|
40
40
|
tinyInt(number = 1) {
|
|
41
41
|
this._addAssignType(`TINYINT(${number})`);
|
|
42
|
-
this.
|
|
42
|
+
this._valueType = Number;
|
|
43
43
|
return this;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
@@ -49,7 +49,7 @@ class Blueprint {
|
|
|
49
49
|
*/
|
|
50
50
|
tinyint(number = 1) {
|
|
51
51
|
this._addAssignType(`TINYINT(${number})`);
|
|
52
|
-
this.
|
|
52
|
+
this._valueType = Number;
|
|
53
53
|
return this;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
@@ -59,7 +59,7 @@ class Blueprint {
|
|
|
59
59
|
*/
|
|
60
60
|
bigInt(number = 10) {
|
|
61
61
|
this._addAssignType(`BIGINT(${number})`);
|
|
62
|
-
this.
|
|
62
|
+
this._valueType = Number;
|
|
63
63
|
return this;
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
@@ -69,7 +69,7 @@ class Blueprint {
|
|
|
69
69
|
*/
|
|
70
70
|
bigint(number = 10) {
|
|
71
71
|
this._addAssignType(`BIGINT(${number})`);
|
|
72
|
-
this.
|
|
72
|
+
this._valueType = Number;
|
|
73
73
|
return this;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
@@ -79,7 +79,7 @@ class Blueprint {
|
|
|
79
79
|
* @return {this} this
|
|
80
80
|
*/
|
|
81
81
|
double(length = 0, decimal = 0) {
|
|
82
|
-
this.
|
|
82
|
+
this._valueType = Number;
|
|
83
83
|
if (!length || !decimal) {
|
|
84
84
|
this._addAssignType(`DOUBLE`);
|
|
85
85
|
return this;
|
|
@@ -94,7 +94,7 @@ class Blueprint {
|
|
|
94
94
|
* @return {this} this
|
|
95
95
|
*/
|
|
96
96
|
float(length = 0, decimal = 0) {
|
|
97
|
-
this.
|
|
97
|
+
this._valueType = Number;
|
|
98
98
|
if (!length || !decimal) {
|
|
99
99
|
this._addAssignType(`FLOAT`);
|
|
100
100
|
return this;
|
|
@@ -204,7 +204,7 @@ class Blueprint {
|
|
|
204
204
|
*/
|
|
205
205
|
date() {
|
|
206
206
|
this._addAssignType(`DATE`);
|
|
207
|
-
this.
|
|
207
|
+
this._valueType = Date;
|
|
208
208
|
return this;
|
|
209
209
|
}
|
|
210
210
|
/**
|
|
@@ -213,7 +213,7 @@ class Blueprint {
|
|
|
213
213
|
*/
|
|
214
214
|
dateTime() {
|
|
215
215
|
this._addAssignType(`DATETIME`);
|
|
216
|
-
this.
|
|
216
|
+
this._valueType = Date;
|
|
217
217
|
return this;
|
|
218
218
|
}
|
|
219
219
|
/**
|
|
@@ -222,7 +222,7 @@ class Blueprint {
|
|
|
222
222
|
*/
|
|
223
223
|
datetime() {
|
|
224
224
|
this._addAssignType(`DATETIME`);
|
|
225
|
-
this.
|
|
225
|
+
this._valueType = Date;
|
|
226
226
|
return this;
|
|
227
227
|
}
|
|
228
228
|
/**
|
|
@@ -231,7 +231,7 @@ class Blueprint {
|
|
|
231
231
|
*/
|
|
232
232
|
timestamp() {
|
|
233
233
|
this._addAssignType(`TIMESTAMP`);
|
|
234
|
-
this.
|
|
234
|
+
this._valueType = Date;
|
|
235
235
|
return this;
|
|
236
236
|
}
|
|
237
237
|
/**
|
|
@@ -326,7 +326,7 @@ class Blueprint {
|
|
|
326
326
|
* @return {this} this
|
|
327
327
|
*/
|
|
328
328
|
foreign({ references, on, onDelete, onUpdate }) {
|
|
329
|
-
this.
|
|
329
|
+
this._foreignKey = {
|
|
330
330
|
references: references == null ? 'id' : references,
|
|
331
331
|
on: typeof on === 'string' ? on : new on(),
|
|
332
332
|
onDelete: onDelete == null ? 'CASCADE' : onDelete,
|
|
@@ -334,12 +334,24 @@ class Blueprint {
|
|
|
334
334
|
};
|
|
335
335
|
return this;
|
|
336
336
|
}
|
|
337
|
+
get type() {
|
|
338
|
+
return this._type;
|
|
339
|
+
}
|
|
340
|
+
get attributes() {
|
|
341
|
+
return this._attributes;
|
|
342
|
+
}
|
|
343
|
+
get foreignKey() {
|
|
344
|
+
return this._foreignKey;
|
|
345
|
+
}
|
|
346
|
+
get valueType() {
|
|
347
|
+
return this._valueType;
|
|
348
|
+
}
|
|
337
349
|
_addAssignType(type) {
|
|
338
|
-
this.
|
|
350
|
+
this._type = type;
|
|
339
351
|
return this;
|
|
340
352
|
}
|
|
341
353
|
_addAssignAttribute(attribute) {
|
|
342
|
-
this.
|
|
354
|
+
this._attributes = [...this.attributes, attribute];
|
|
343
355
|
return this;
|
|
344
356
|
}
|
|
345
357
|
}
|
|
@@ -1265,7 +1265,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1265
1265
|
* @param {number} rows number of rows
|
|
1266
1266
|
* @return {promise<any>}
|
|
1267
1267
|
*/
|
|
1268
|
-
faker(rows?:
|
|
1268
|
+
faker(rows: number, cb?: Function): Promise<Record<string, any>[]>;
|
|
1269
1269
|
/**
|
|
1270
1270
|
*
|
|
1271
1271
|
* truncate of table
|
|
@@ -1318,7 +1318,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1318
1318
|
}): Promise<any>;
|
|
1319
1319
|
private _insertNotExists;
|
|
1320
1320
|
private _insert;
|
|
1321
|
-
|
|
1321
|
+
protected _checkValueHasRaw(value: any): string;
|
|
1322
1322
|
private _insertMultiple;
|
|
1323
1323
|
private _insertOrSelect;
|
|
1324
1324
|
private _updateOrInsert;
|
|
@@ -1327,8 +1327,8 @@ declare class Builder extends AbstractBuilder {
|
|
|
1327
1327
|
private _queryUpdate;
|
|
1328
1328
|
private _queryInsert;
|
|
1329
1329
|
private _queryInsertMultiple;
|
|
1330
|
-
|
|
1331
|
-
|
|
1330
|
+
protected _valueAndOperator(value: string, operator: string, useDefault?: boolean): string[];
|
|
1331
|
+
protected _valueTrueFalse(value: any): any;
|
|
1332
1332
|
private _initialConnection;
|
|
1333
1333
|
}
|
|
1334
1334
|
export { Builder };
|
|
@@ -60,11 +60,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
60
60
|
let select = columns.map((column) => {
|
|
61
61
|
if (column === '*' || (column.includes('*') && /\./.test(column)))
|
|
62
62
|
return column;
|
|
63
|
-
if (/\./.test(column))
|
|
64
|
-
return this.bindColumn(column);
|
|
65
63
|
if (column.includes(this.$constants('RAW')))
|
|
66
64
|
return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
|
|
67
|
-
return
|
|
65
|
+
return this.bindColumn(column);
|
|
68
66
|
});
|
|
69
67
|
select = [...this._getState('SELECT'), ...select];
|
|
70
68
|
if (this._getState('DISTINCT') && select.length) {
|
|
@@ -1848,9 +1846,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1848
1846
|
*/
|
|
1849
1847
|
bindColumn(column) {
|
|
1850
1848
|
if (!/\./.test(column))
|
|
1851
|
-
return
|
|
1849
|
+
return `\`${this.getTableName().replace(/`/g, '')}\`.\`${column.replace(/`/g, '')}\``;
|
|
1852
1850
|
const [table, c] = column.split('.');
|
|
1853
|
-
return `\`${table}\`.\`${c}\``;
|
|
1851
|
+
return `\`${table.replace(/`/g, '')}\`.\`${c.replace(/`/g, '')}\``;
|
|
1854
1852
|
}
|
|
1855
1853
|
/**
|
|
1856
1854
|
* The 'debug' method is used to console.log raw SQL query that would be executed by a query builder
|
|
@@ -2599,29 +2597,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2599
2597
|
*/
|
|
2600
2598
|
save() {
|
|
2601
2599
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2602
|
-
const attributes = this.$attributes;
|
|
2603
|
-
if (attributes != null) {
|
|
2604
|
-
while (true) {
|
|
2605
|
-
if (!this._getState('where').length) {
|
|
2606
|
-
const query = this._queryInsert(attributes);
|
|
2607
|
-
this._setState('INSERT', [
|
|
2608
|
-
`${this.$constants('INSERT')}`,
|
|
2609
|
-
`${this._getState('TABLE_NAME')}`,
|
|
2610
|
-
`${query}`
|
|
2611
|
-
].join(' '));
|
|
2612
|
-
this._setState('SAVE', 'INSERT');
|
|
2613
|
-
break;
|
|
2614
|
-
}
|
|
2615
|
-
const query = this._queryUpdate(attributes);
|
|
2616
|
-
this._setState('UPDATE', [
|
|
2617
|
-
`${this.$constants('UPDATE')}`,
|
|
2618
|
-
`${this._getState('TABLE_NAME')}`,
|
|
2619
|
-
`${query}`
|
|
2620
|
-
].join(' '));
|
|
2621
|
-
this._setState('SAVE', 'UPDATE');
|
|
2622
|
-
break;
|
|
2623
|
-
}
|
|
2624
|
-
}
|
|
2625
2600
|
switch (this._getState('SAVE')) {
|
|
2626
2601
|
case 'INSERT_MULTIPLE': return yield this._insertMultiple();
|
|
2627
2602
|
case 'INSERT': return yield this._insert();
|
|
@@ -2988,7 +2963,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2988
2963
|
* @param {number} rows number of rows
|
|
2989
2964
|
* @return {promise<any>}
|
|
2990
2965
|
*/
|
|
2991
|
-
faker(rows
|
|
2966
|
+
faker(rows, cb) {
|
|
2992
2967
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2993
2968
|
let data = [];
|
|
2994
2969
|
const sql = [
|
|
@@ -3011,6 +2986,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3011
2986
|
continue;
|
|
3012
2987
|
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
|
|
3013
2988
|
}
|
|
2989
|
+
if (cb) {
|
|
2990
|
+
data = [...data, cb(columnAndValue, row)];
|
|
2991
|
+
continue;
|
|
2992
|
+
}
|
|
3014
2993
|
data = [...data, columnAndValue];
|
|
3015
2994
|
}
|
|
3016
2995
|
return yield this.createMultiple(data).save();
|
|
@@ -3283,13 +3262,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3283
3262
|
sql: this._queryBuilder().insert(),
|
|
3284
3263
|
returnId: true
|
|
3285
3264
|
});
|
|
3286
|
-
console.log({
|
|
3287
|
-
result,
|
|
3288
|
-
id
|
|
3289
|
-
});
|
|
3290
3265
|
if (this._getState('VOID') || !result)
|
|
3291
3266
|
return this._resultHandler(undefined);
|
|
3292
|
-
console.log('hi');
|
|
3293
3267
|
const arrayId = [...Array(result)].map((_, i) => i + id);
|
|
3294
3268
|
const sql = new Builder()
|
|
3295
3269
|
.copyBuilder(this, { select: true })
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RelationQuery, ValidateSchemaDecorator } from "../Interface";
|
|
2
|
+
import { Blueprint } from "./Blueprint";
|
|
3
|
+
export declare const Table: (name: string) => (constructor: Function) => void;
|
|
4
|
+
export declare const TableSingular: () => (constructor: Function) => void;
|
|
5
|
+
export declare const TablePlural: () => (constructor: Function) => void;
|
|
6
|
+
export declare const Column: (blueprint: () => Blueprint) => (target: any, key: string) => void;
|
|
7
|
+
export declare const Validate: (validate: ValidateSchemaDecorator) => (target: any, key: string) => void;
|
|
8
|
+
export declare const UUID: (column?: string) => (constructor: Function) => void;
|
|
9
|
+
export declare const Timestamp: (timestampColumns?: {
|
|
10
|
+
createdAt: string;
|
|
11
|
+
updatedAt: string;
|
|
12
|
+
}) => (constructor: Function) => void;
|
|
13
|
+
export declare const SoftDelete: (column?: string) => (constructor: Function) => void;
|
|
14
|
+
export declare const Pattern: (pattern: 'camelCase' | 'snake_case') => (constructor: Function) => void;
|
|
15
|
+
export declare const CamelCase: () => (constructor: Function) => void;
|
|
16
|
+
export declare const SnakeCase: () => (constructor: Function) => void;
|
|
17
|
+
export declare const HasOne: ({ name, as, model, localKey, foreignKey, freezeTable }: RelationQuery) => (target: any, key: string) => void;
|
|
18
|
+
export declare const HasMany: ({ name, as, model, localKey, foreignKey, freezeTable }: RelationQuery) => (target: any, key: string) => void;
|
|
19
|
+
export declare const BelongsTo: ({ name, as, model, localKey, foreignKey, freezeTable }: RelationQuery) => (target: any, key: string) => void;
|
|
20
|
+
export declare const BelongsToMany: ({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }: RelationQuery) => (target: any, key: string) => void;
|