tspace-mysql 1.5.1 → 1.5.3
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 +108 -6
- package/build/lib/Interface.d.ts +4 -4
- package/build/lib/connection/options.js +1 -1
- package/build/lib/constants/index.d.ts +1 -3
- package/build/lib/constants/index.js +0 -96
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.js +1 -4
- package/build/lib/core/Abstracts/AbstractModel.d.ts +14 -13
- package/build/lib/core/Builder.d.ts +1 -1
- package/build/lib/core/Builder.js +79 -56
- package/build/lib/core/DB.js +33 -33
- package/build/lib/core/Handlers/Relation.d.ts +2 -2
- package/build/lib/core/Handlers/Relation.js +2 -2
- package/build/lib/core/Handlers/State.d.ts +2 -2
- package/build/lib/core/Handlers/State.js +126 -12
- package/build/lib/core/Model.d.ts +78 -72
- package/build/lib/core/Model.js +102 -56
- package/build/lib/core/Schema.d.ts +1 -4
- package/build/lib/core/Schema.js +15 -15
- package/build/lib/core/Type.d.ts +60 -0
- package/build/lib/core/Type.js +2 -0
- package/build/lib/core/index.d.ts +1 -0
- package/build/lib/core/index.js +1 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2039,6 +2039,7 @@ import { User } from './User.ts'
|
|
|
2039
2039
|
const schemaPhone = {
|
|
2040
2040
|
id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
2041
2041
|
uuid :new Blueprint().varchar(50).null(),
|
|
2042
|
+
userId : new Blueprint().int().notNull(),
|
|
2042
2043
|
number :new Blueprint().varchar(50).notNull(),
|
|
2043
2044
|
createdAt :new Blueprint().timestamp().null(),
|
|
2044
2045
|
updatedAt :new Blueprint().timestamp().null()
|
|
@@ -2179,11 +2180,8 @@ const users = await new User().where('idx',1).delete() ❌
|
|
|
2179
2180
|
### Safety Relationships
|
|
2180
2181
|
|
|
2181
2182
|
```js
|
|
2182
|
-
|
|
2183
|
-
import {
|
|
2184
|
-
import { Phone, schemaPhoneType } from './Phone.ts'
|
|
2185
|
-
|
|
2186
|
-
+--------------------------------------------------------------------------+
|
|
2183
|
+
import { User } from './User.ts'
|
|
2184
|
+
import { Phone } from './Phone.ts'
|
|
2187
2185
|
// Case #1 : Relationship with 2 relations 'phone' and 'phones'
|
|
2188
2186
|
const users = await new User()
|
|
2189
2187
|
.relations('phone','phones')
|
|
@@ -2225,7 +2223,7 @@ for(const user of users) {
|
|
|
2225
2223
|
|
|
2226
2224
|
+--------------------------------------------------------------------------+
|
|
2227
2225
|
|
|
2228
|
-
// Case #2 :
|
|
2226
|
+
// Case #2 : There is a relationship between two entities, 'phone' and 'phones', both of which are related to the 'user' entity through nested relations
|
|
2229
2227
|
const users = await new User()
|
|
2230
2228
|
.relations('phone','phones')
|
|
2231
2229
|
.relationQuery('phone' , (query : Phone) => query.relations('user'))
|
|
@@ -2272,6 +2270,110 @@ for(const user of users) {
|
|
|
2272
2270
|
}
|
|
2273
2271
|
|
|
2274
2272
|
+--------------------------------------------------------------------------+
|
|
2273
|
+
// If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
|
|
2274
|
+
|
|
2275
|
+
import { Model , Blueprint , SchemaType , RelationType } from 'tspace-mysql'
|
|
2276
|
+
import { Phone , SchemaPhoneType } from '../Phone'
|
|
2277
|
+
|
|
2278
|
+
const schemaUser = {
|
|
2279
|
+
id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
2280
|
+
uuid :new Blueprint().varchar(50).null(),
|
|
2281
|
+
email :new Blueprint().varchar(50).null(),
|
|
2282
|
+
name :new Blueprint().varchar(255).null(),
|
|
2283
|
+
username : new Blueprint().varchar(255).null(),
|
|
2284
|
+
password : new Blueprint().varchar(255).null(),
|
|
2285
|
+
createdAt :new Blueprint().timestamp().null(),
|
|
2286
|
+
updatedAt :new Blueprint().timestamp().null()
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
type SchemaUserType = SchemaType<typeof schemaUser>
|
|
2290
|
+
|
|
2291
|
+
type RelationUserType = RelationType<{
|
|
2292
|
+
phones : SchemaPhoneType[]
|
|
2293
|
+
phone : SchemaPhoneType
|
|
2294
|
+
}>
|
|
2295
|
+
|
|
2296
|
+
class User extends Model<SchemaUserType, RelationUserType> {
|
|
2297
|
+
constructor() {
|
|
2298
|
+
super()
|
|
2299
|
+
this.useSchema(schemaUser)
|
|
2300
|
+
this.hasOne({ model : Phone, name : 'phonex' }) ❌
|
|
2301
|
+
this.hasMany({ model : Phone, name : 'phonesx' }) ❌
|
|
2302
|
+
this.hasOne({ model : Phone, name : 'phone' }) ✅
|
|
2303
|
+
this.hasMany({ model : Phone, name : 'phones' }) ✅
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
export { User , SchemaUserType }
|
|
2308
|
+
|
|
2309
|
+
+--------------------------------------------------------------------------+
|
|
2310
|
+
|
|
2311
|
+
// in file Phone.ts
|
|
2312
|
+
import { Model , Blueprint , SchemaType , RelationType} from 'tspace-mysql'
|
|
2313
|
+
import { User } from './User.ts'
|
|
2314
|
+
const schemaPhone = {
|
|
2315
|
+
id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
2316
|
+
uuid :new Blueprint().varchar(50).null(),
|
|
2317
|
+
userId : new Blueprint().int().notNull(),
|
|
2318
|
+
number :new Blueprint().varchar(50).notNull(),
|
|
2319
|
+
createdAt :new Blueprint().timestamp().null(),
|
|
2320
|
+
updatedAt :new Blueprint().timestamp().null()
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
type SchemaPhoneType = SchemaType<typeof schemaPhone>
|
|
2324
|
+
|
|
2325
|
+
type RelationPhoneType = RelationType<{
|
|
2326
|
+
user : SchemaPhoneType[]
|
|
2327
|
+
}>
|
|
2328
|
+
|
|
2329
|
+
class Phone extends Model<SchemaPhoneType,RelationPhoneType> {
|
|
2330
|
+
constructor() {
|
|
2331
|
+
super()
|
|
2332
|
+
this.useSchema(schemaPhone)
|
|
2333
|
+
this.useBelongsTo({ model : User, name : 'userx'}) ❌
|
|
2334
|
+
this.useBelongsTo({ model : User, name : 'user'}) ✅
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
export { Phone , SchemaPhoneType }
|
|
2339
|
+
|
|
2340
|
+
+--------------------------------------------------------------------------+
|
|
2341
|
+
|
|
2342
|
+
const users = await new User()
|
|
2343
|
+
.relations('phonex','phonesx') ❌
|
|
2344
|
+
.relationQuery('phonex' ❌ , (query : Phone) => query.relations('user')) ✅
|
|
2345
|
+
.relationQuery('phonesx' ❌ , (query : Phone) => query.relations('user')) ✅
|
|
2346
|
+
.findMany()
|
|
2347
|
+
|
|
2348
|
+
const users = await new User()
|
|
2349
|
+
.relations('phone','phones') ✅
|
|
2350
|
+
.relationQuery('phonex' ❌ , (query : Phone) => query.relations('user')) ✅
|
|
2351
|
+
.relationQuery('phonesx' ❌ , (query : Phone) => query.relations('user')) ✅
|
|
2352
|
+
.findMany()
|
|
2353
|
+
|
|
2354
|
+
const users = await new User()
|
|
2355
|
+
.relations('phone','phones')
|
|
2356
|
+
.relationQuery('phone' , (query : Phone) => query.relations('userx')) ❌
|
|
2357
|
+
.relationQuery('phones' , (query : Phone) => query.relations('userx')) ❌
|
|
2358
|
+
.findMany()
|
|
2359
|
+
|
|
2360
|
+
const users = await new User()
|
|
2361
|
+
.relations('phone','phones') ✅
|
|
2362
|
+
.relationQuery('phone' ✅ , (query : Phone) => query.relations('user')) ✅
|
|
2363
|
+
.relationQuery('phones'✅ , (query : Phone) => query.relations('user')) ✅
|
|
2364
|
+
.findMany()
|
|
2365
|
+
|
|
2366
|
+
for(const user of users) {
|
|
2367
|
+
user.phone.user ❌
|
|
2368
|
+
user.phone?.user ✅
|
|
2369
|
+
user.phone?.user.id ✅
|
|
2370
|
+
user.phone?.userx ❌
|
|
2371
|
+
user.phone?.user.idx ❌
|
|
2372
|
+
user.phones.map(phone =>phone?.user.id) ❌
|
|
2373
|
+
user.phones?.map(phone =>phone?.user.id) ✅
|
|
2374
|
+
user.phones?.map(phone =>phone?.user.idx) ❌
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2275
2377
|
```
|
|
2276
2378
|
|
|
2277
2379
|
## Blueprint
|
package/build/lib/Interface.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Model from "./core/Model";
|
|
2
|
-
export interface Relation {
|
|
3
|
-
name:
|
|
2
|
+
export interface Relation<K = any> {
|
|
3
|
+
name: K extends void ? never : K;
|
|
4
4
|
model: new () => Model;
|
|
5
5
|
as?: string | undefined;
|
|
6
6
|
localKey?: string | undefined;
|
|
@@ -16,8 +16,8 @@ export interface Relation {
|
|
|
16
16
|
oldVersion?: boolean | undefined;
|
|
17
17
|
modelPivot?: new () => Model | undefined;
|
|
18
18
|
}
|
|
19
|
-
export interface RelationQuery {
|
|
20
|
-
name?:
|
|
19
|
+
export interface RelationQuery<K = any> {
|
|
20
|
+
name?: K extends void ? never : K;
|
|
21
21
|
model: new () => Model;
|
|
22
22
|
as?: string | undefined;
|
|
23
23
|
localKey?: string | undefined;
|
|
@@ -21,7 +21,7 @@ const environment = () => {
|
|
|
21
21
|
dotenv_1.default.config({ path: environment() });
|
|
22
22
|
const ENV = process.env;
|
|
23
23
|
const env = {
|
|
24
|
-
HOST: ENV.DB_HOST || ENV.TSPACE_HOST,
|
|
24
|
+
HOST: ENV.DB_HOST || ENV.TSPACE_HOST || 'localhost',
|
|
25
25
|
PORT: ENV.DB_PORT || ENV.TSPACE_PORT || 3306,
|
|
26
26
|
USERNAME: ENV.DB_USERNAME || ENV.TSPACE_USERNAME || ENV.DB_USER,
|
|
27
27
|
PASSWORD: ENV.DB_PASSWORD || ENV.TSPACE_PASSWORD || '',
|
|
@@ -90,102 +90,6 @@ const CONSTANTS = Object.freeze({
|
|
|
90
90
|
PATTERN: {
|
|
91
91
|
snake_case: 'snake_case',
|
|
92
92
|
camelCase: 'camelCase'
|
|
93
|
-
},
|
|
94
|
-
DEFAULT: {
|
|
95
|
-
DEBUG: false,
|
|
96
|
-
},
|
|
97
|
-
DB: {
|
|
98
|
-
PRIMARY_KEY: 'id',
|
|
99
|
-
VOID: false,
|
|
100
|
-
RESULT: null,
|
|
101
|
-
DISTINCT: false,
|
|
102
|
-
PLUCK: '',
|
|
103
|
-
SAVE: '',
|
|
104
|
-
DELETE: '',
|
|
105
|
-
UPDATE: '',
|
|
106
|
-
INSERT: '',
|
|
107
|
-
SELECT: [],
|
|
108
|
-
ONLY: [],
|
|
109
|
-
EXCEPTS: [],
|
|
110
|
-
CHUNK: 0,
|
|
111
|
-
COUNT: '',
|
|
112
|
-
FROM: 'FROM',
|
|
113
|
-
JOIN: [],
|
|
114
|
-
WHERE: [],
|
|
115
|
-
GROUP_BY: '',
|
|
116
|
-
ORDER_BY: [],
|
|
117
|
-
LIMIT: '',
|
|
118
|
-
OFFSET: '',
|
|
119
|
-
HAVING: '',
|
|
120
|
-
TABLE_NAME: '',
|
|
121
|
-
UUID_CUSTOM: '',
|
|
122
|
-
HIDDEN: [],
|
|
123
|
-
DEBUG: false,
|
|
124
|
-
UUID: false,
|
|
125
|
-
PAGE: 1,
|
|
126
|
-
PER_PAGE: 1,
|
|
127
|
-
HOOKS: [],
|
|
128
|
-
RETURN_TYPE: null
|
|
129
|
-
},
|
|
130
|
-
MODEL: {
|
|
131
|
-
MODEL_NAME: 'MODEL',
|
|
132
|
-
PRIMARY_KEY: 'id',
|
|
133
|
-
VOID: false,
|
|
134
|
-
SELECT: [],
|
|
135
|
-
DELETE: '',
|
|
136
|
-
UPDATE: '',
|
|
137
|
-
INSERT: '',
|
|
138
|
-
ONLY: [],
|
|
139
|
-
EXCEPTS: [],
|
|
140
|
-
CHUNK: 0,
|
|
141
|
-
COUNT: '',
|
|
142
|
-
FROM: 'FROM',
|
|
143
|
-
JOIN: [],
|
|
144
|
-
WHERE: [],
|
|
145
|
-
GROUP_BY: '',
|
|
146
|
-
ORDER_BY: [],
|
|
147
|
-
LIMIT: '',
|
|
148
|
-
OFFSET: '',
|
|
149
|
-
HAVING: '',
|
|
150
|
-
TABLE_NAME: '',
|
|
151
|
-
UUID_FORMAT: 'uuid',
|
|
152
|
-
HIDDEN: [],
|
|
153
|
-
DEBUG: false,
|
|
154
|
-
UUID: false,
|
|
155
|
-
SOFT_DELETE: false,
|
|
156
|
-
SOFT_DELETE_FORMAT: 'deleted_at',
|
|
157
|
-
SOFT_DELETE_RELATIONS: false,
|
|
158
|
-
PAGE: 1,
|
|
159
|
-
PER_PAGE: 1,
|
|
160
|
-
REGISTRY: {},
|
|
161
|
-
RESULT: null,
|
|
162
|
-
PATTERN: 'snake_case',
|
|
163
|
-
DISTINCT: false,
|
|
164
|
-
PLUCK: '',
|
|
165
|
-
SAVE: '',
|
|
166
|
-
HOOKS: [],
|
|
167
|
-
RELATION: [],
|
|
168
|
-
RELATIONS: [],
|
|
169
|
-
RELATIONS_TRASHED: false,
|
|
170
|
-
RELATIONS_EXISTS: false,
|
|
171
|
-
RELATIONS_EXISTS_NOT_ID: [],
|
|
172
|
-
TIMESTAMP: false,
|
|
173
|
-
TIMESTAMP_FORMAT: {
|
|
174
|
-
CREATED_AT: 'created_at',
|
|
175
|
-
UPDATED_AT: 'updated_at'
|
|
176
|
-
},
|
|
177
|
-
LOGGER: false,
|
|
178
|
-
LOGGER_OPTIONS: null,
|
|
179
|
-
TABLE_LOGGER: '$loggers',
|
|
180
|
-
VALIDATE_SCHEMA: false,
|
|
181
|
-
VALIDATE_SCHEMA_DEFINED: null,
|
|
182
|
-
FUNCTION_RELATION: false,
|
|
183
|
-
SCHEMA_TABLE: null,
|
|
184
|
-
RETRY: 0,
|
|
185
|
-
OBSERVER: null,
|
|
186
|
-
DATA: null,
|
|
187
|
-
BEFORE_CREATING_TABLE: null,
|
|
188
|
-
RETURN_TYPE: null
|
|
189
93
|
}
|
|
190
94
|
});
|
|
191
95
|
exports.CONSTANTS = CONSTANTS;
|
|
@@ -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, cb?: Function): Promise<
|
|
120
|
+
abstract faker(round: number, cb?: Function): Promise<void>;
|
|
121
121
|
}
|
|
122
122
|
export { AbstractBuilder };
|
|
123
123
|
export default AbstractBuilder;
|
|
@@ -15,10 +15,7 @@ class AbstractBuilder {
|
|
|
15
15
|
];
|
|
16
16
|
this.$utils = {};
|
|
17
17
|
this.$constants = (name) => { };
|
|
18
|
-
this.$state = new State_1.StateHandler(
|
|
19
|
-
state: {},
|
|
20
|
-
constants: {}
|
|
21
|
-
});
|
|
18
|
+
this.$state = new State_1.StateHandler('default');
|
|
22
19
|
this.$pool = {
|
|
23
20
|
query: (sql) => { },
|
|
24
21
|
set: (pool) => { },
|
|
@@ -2,7 +2,8 @@ import { Pattern, Relation, RelationQuery, ValidateSchema } from '../../Interfac
|
|
|
2
2
|
import { Blueprint } from '../Blueprint';
|
|
3
3
|
import { Builder } from '../Builder';
|
|
4
4
|
import { RelationHandler } from '../Handlers/Relation';
|
|
5
|
-
|
|
5
|
+
import { Model } from '../Model';
|
|
6
|
+
declare abstract class AbstractModel<T, R> extends Builder {
|
|
6
7
|
protected $relation: RelationHandler | undefined;
|
|
7
8
|
protected $schema: Record<string, Blueprint> | undefined;
|
|
8
9
|
protected $validateSchema: ValidateSchema | undefined;
|
|
@@ -48,7 +49,7 @@ declare abstract class AbstractModel<T> extends Builder {
|
|
|
48
49
|
protected abstract hasMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
49
50
|
protected abstract belongsTo({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
50
51
|
protected abstract belongsToMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
51
|
-
protected abstract buildMethodRelation(name:
|
|
52
|
+
protected abstract buildMethodRelation<K extends keyof R>(name: K, callback?: Function): this;
|
|
52
53
|
protected abstract hasOneBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
53
54
|
protected abstract hasManyBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
54
55
|
protected abstract belongsToBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
@@ -59,17 +60,17 @@ declare abstract class AbstractModel<T> extends Builder {
|
|
|
59
60
|
abstract onlyTrashed(): this;
|
|
60
61
|
abstract trashed(): this;
|
|
61
62
|
abstract restore(): Promise<any[]>;
|
|
62
|
-
abstract with(...nameRelations:
|
|
63
|
-
abstract withQuery(nameRelations:
|
|
64
|
-
abstract withExists(...nameRelations:
|
|
65
|
-
abstract withTrashed(...nameRelations:
|
|
66
|
-
abstract withAll(...nameRelations:
|
|
67
|
-
abstract has(...nameRelations:
|
|
68
|
-
abstract relations(...nameRelations:
|
|
69
|
-
abstract relationQuery(nameRelations:
|
|
70
|
-
abstract relationsExists(...nameRelations:
|
|
71
|
-
abstract relationsAll(...nameRelations:
|
|
72
|
-
abstract relationsTrashed(...nameRelations:
|
|
63
|
+
abstract with<K extends keyof R>(...nameRelations: K[]): this;
|
|
64
|
+
abstract withQuery<K extends keyof R, TModel extends Model>(nameRelations: K, callback: (query: TModel) => TModel): this;
|
|
65
|
+
abstract withExists<K extends keyof R>(...nameRelations: K[]): this;
|
|
66
|
+
abstract withTrashed<K extends keyof R>(...nameRelations: K[]): this;
|
|
67
|
+
abstract withAll<K extends keyof R>(...nameRelations: K[]): this;
|
|
68
|
+
abstract has<K extends keyof R>(...nameRelations: K[]): this;
|
|
69
|
+
abstract relations<K extends keyof R>(...nameRelations: K[]): this;
|
|
70
|
+
abstract relationQuery<K extends keyof R, TModel extends Model>(nameRelations: K, callback: (query: TModel) => TModel): this;
|
|
71
|
+
abstract relationsExists<K extends keyof R>(...nameRelations: K[]): this;
|
|
72
|
+
abstract relationsAll<K extends keyof R>(...nameRelations: K[]): this;
|
|
73
|
+
abstract relationsTrashed<K extends keyof R>(...nameRelations: K[]): this;
|
|
73
74
|
}
|
|
74
75
|
export { AbstractModel };
|
|
75
76
|
export default AbstractModel;
|
|
@@ -1289,7 +1289,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1289
1289
|
* @param {number} rows number of rows
|
|
1290
1290
|
* @return {promise<any>}
|
|
1291
1291
|
*/
|
|
1292
|
-
faker(rows: number, cb?: Function): Promise<
|
|
1292
|
+
faker(rows: number, cb?: Function): Promise<void>;
|
|
1293
1293
|
/**
|
|
1294
1294
|
*
|
|
1295
1295
|
* truncate of table
|