tspace-mysql 1.8.2 → 1.8.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 CHANGED
@@ -120,6 +120,7 @@ npm install tspace-mysql -g
120
120
  - [Safety Update](#safety-update)
121
121
  - [Safety Delete](#safety-delete)
122
122
  - [Safety Relationships](#safety-relationships)
123
+ - [Safety Result](#safety-result)
123
124
  - [Repository](#repository)
124
125
  - [Repository Select Statements](#repository-select-statements)
125
126
  - [Repository Insert Statements](#repository-insert-statements)
@@ -1229,7 +1230,35 @@ try {
1229
1230
  *
1230
1231
  * @commit commit transaction to database
1231
1232
  */
1233
+ // After your use commit if use same connection for actions this transction will auto commit
1232
1234
  await connection.commit();
1235
+
1236
+ // If you need to start a new transaction again, just use wait connection.startTransaction();
1237
+
1238
+ const postsAfterCommited = await new Post()
1239
+ .createMultiple([
1240
+ {
1241
+ user_id: user.id,
1242
+ title: `tspace-post1`,
1243
+ },
1244
+ {
1245
+ user_id: user.id,
1246
+ title: `tspace-post2`,
1247
+ },
1248
+ {
1249
+ user_id: user.id,
1250
+ title: `tspace-post3`,
1251
+ },
1252
+ ])
1253
+ // Using this connection now will auto-commit to the database.
1254
+ .bind(connection) // If you need to perform additional operations, use await connection.startTransaction(); again.
1255
+ .save();
1256
+
1257
+
1258
+ // Do not perform any operations with this connection.
1259
+ // The transaction has already been committed, and the connection is closed.
1260
+ // Just ensure everything is handled at the end of the transaction.
1261
+ await connection.end();
1233
1262
 
1234
1263
  } catch (err) {
1235
1264
  /**
@@ -3045,6 +3074,30 @@ const users = await new User()
3045
3074
 
3046
3075
  ```
3047
3076
 
3077
+ ## Safety Result
3078
+ ```js
3079
+ import type { TResult } from 'tspace-mysql'
3080
+
3081
+ const fError = async () : Promise<TResult<User>[]> => {
3082
+
3083
+ const users = [{
3084
+ id : 1,
3085
+ uuid: "12d4f08a-a20d-4f41-abac-81391e135d60",
3086
+ email: "tspace@example.com"
3087
+ }]
3088
+
3089
+ return users // ❌
3090
+ }
3091
+
3092
+ const fCorrect = async () : Promise<TResult<User>[]> => {
3093
+
3094
+ const users = await new User().findMany()
3095
+
3096
+ return users // ✅
3097
+ }
3098
+
3099
+ ```
3100
+
3048
3101
  ## Repository
3049
3102
  ```js
3050
3103
  Repository is a mechanism that encapsulates all database operations related to a specific model.
@@ -3248,8 +3301,12 @@ try {
3248
3301
  transaction
3249
3302
  })
3250
3303
 
3304
+ // after your use commit if use same transction for actions this transction will auto commit
3251
3305
  await transaction.commit()
3252
3306
 
3307
+ // ensure the nothing with transction just use end of transction
3308
+ await transaction.end();
3309
+
3253
3310
  } catch (err) {
3254
3311
 
3255
3312
  await transaction.rollback()
@@ -3266,28 +3323,36 @@ import { Phone } from '../Models/Phone'
3266
3323
  const userRepository = Repository(User)
3267
3324
 
3268
3325
  const userHasPhones = await userRepository.findOne({
3269
- select : ['*'],
3326
+ select : {
3327
+ id : true,
3328
+ name : true,
3329
+ username : true,
3330
+ phone : {
3331
+ id : true,
3332
+ user_id : true,
3333
+ name: true
3334
+ }
3335
+ },
3270
3336
  where : {
3271
3337
  id: 1
3272
3338
  },
3273
- relations : ['phone'],
3274
- relationQuery:{
3275
- name : 'phone',
3276
- callback: () : TRepository<Phone> => ({ // add type for the callback know to check type of the model
3277
- select: ['id', 'userId', 'name'],
3278
- relations : ['user']
3279
- })
3339
+ relations: {
3340
+ phone: {
3341
+ user : true
3342
+ }
3280
3343
  }
3281
- })
3344
+ });
3282
3345
 
3283
3346
  const phoneRepository = Repository(Phone)
3284
3347
 
3285
3348
  const phoneBelongUser = await phoneRepository.findOne({
3286
- select : ['*'],
3349
+ select : '*',
3287
3350
  where : {
3288
3351
  id: 1
3289
3352
  },
3290
- relations : ['user']
3353
+ relations : {
3354
+ user : true
3355
+ }
3291
3356
  })
3292
3357
 
3293
3358
  ```
@@ -1,7 +1,7 @@
1
1
  import Builder from '../Builder';
2
- import { TConnection, TConnectionOptions } from '../../types';
2
+ import { TConnection, TConnectionOptions, TConnectionTransaction } from '../../types';
3
3
  declare abstract class AbstractDB extends Builder {
4
- abstract beginTransaction(): Promise<any>;
4
+ abstract beginTransaction(): Promise<TConnectionTransaction>;
5
5
  abstract generateUUID(): string;
6
6
  abstract raw(sql: string): string;
7
7
  abstract escape(sql: string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"AbstractDB.js","sourceRoot":"","sources":["../../../../src/lib/core/Abstracts/AbstractDB.ts"],"names":[],"mappings":";;;;;;AAAA,yDAAqC;AAMrC,MAAe,UAAW,SAAQ,iBAAO;CAcxC;AAEQ,gCAAU;AACnB,kBAAe,UAAU,CAAA"}
1
+ {"version":3,"file":"AbstractDB.js","sourceRoot":"","sources":["../../../../src/lib/core/Abstracts/AbstractDB.ts"],"names":[],"mappings":";;;;;;AAAA,yDAAqC;AAOrC,MAAe,UAAW,SAAQ,iBAAO;CAcxC;AAEQ,gCAAU;AACnB,kBAAe,UAAU,CAAA"}
@@ -1,6 +1,6 @@
1
- import { AbstractBuilder } from './Abstracts/AbstractBuilder';
2
- import { Join } from './Join';
3
- import { TPagination, TConnectionOptions, TConnection, TConnectionTransaction } from '../types';
1
+ import { AbstractBuilder } from "./Abstracts/AbstractBuilder";
2
+ import { Join } from "./Join";
3
+ import { TPagination, TConnectionOptions, TConnection, TConnectionTransaction } from "../types";
4
4
  declare class Builder extends AbstractBuilder {
5
5
  constructor();
6
6
  /**
@@ -145,7 +145,7 @@ declare class Builder extends AbstractBuilder {
145
145
  * @param {string} type - The types 'object' | 'array'
146
146
  * @returns {this} this
147
147
  */
148
- returnType(type: 'object' | 'array'): this;
148
+ returnType(type: "object" | "array"): this;
149
149
  /**
150
150
  * The 'pluck' method is used to retrieve the value of a single column from the first result of a query.
151
151
  *
@@ -685,7 +685,7 @@ declare class Builder extends AbstractBuilder {
685
685
  * .get()
686
686
  * @returns {this}
687
687
  */
688
- joinSubQuery({ localKey, foreignKey, sql }: {
688
+ joinSubQuery({ localKey, foreignKey, sql, }: {
689
689
  localKey: string;
690
690
  foreignKey: string;
691
691
  sql: string;
@@ -698,7 +698,7 @@ declare class Builder extends AbstractBuilder {
698
698
  * @param {string?} order by default order = 'asc' but you can used 'asc' or 'desc'
699
699
  * @returns {this}
700
700
  */
701
- orderBy(column: string, order?: 'ASC' | 'asc' | 'DESC' | 'desc'): this;
701
+ orderBy(column: string, order?: "ASC" | "asc" | "DESC" | "desc"): this;
702
702
  /**
703
703
  * The 'orderByRaw' method is used to specify the order in which the results of a database query should be sorted.
704
704
  *
@@ -709,7 +709,7 @@ declare class Builder extends AbstractBuilder {
709
709
  * @param {string?} order [order=asc] asc, desc
710
710
  * @returns {this}
711
711
  */
712
- orderByRaw(column: string, order?: 'ASC' | 'asc' | 'DESC' | 'desc'): this;
712
+ orderByRaw(column: string, order?: "ASC" | "asc" | "DESC" | "desc"): this;
713
713
  /**
714
714
  * The 'random' method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
715
715
  *
@@ -1027,7 +1027,7 @@ declare class Builder extends AbstractBuilder {
1027
1027
  *
1028
1028
  * This method is particularly useful for debugging and understanding the SQL queries generated by your application.
1029
1029
  * @returns {string}
1030
- */
1030
+ */
1031
1031
  toRawSQL(): string;
1032
1032
  /**
1033
1033
  * The 'getTableName' method is used to get table name
@@ -1066,13 +1066,13 @@ declare class Builder extends AbstractBuilder {
1066
1066
  * The 'hook' method is used function when execute returns a result to callback function
1067
1067
  * @param {Function} func function for callback result
1068
1068
  * @returns {this}
1069
- */
1069
+ */
1070
1070
  hook(func: Function): this;
1071
1071
  /**
1072
1072
  * The 'before' method is used function when execute returns a result to callback function
1073
1073
  * @param {Function} func function for callback result
1074
1074
  * @returns {this}
1075
- */
1075
+ */
1076
1076
  before(func: Function): this;
1077
1077
  /**
1078
1078
  *
@@ -1338,20 +1338,20 @@ declare class Builder extends AbstractBuilder {
1338
1338
  */
1339
1339
  getGroupBy(column: string): Promise<Map<string | number, any[]>>;
1340
1340
  /**
1341
- * The 'findGroupBy' method is used to execute a database query and retrieve the result set that matches the query conditions.
1342
- *
1343
- * It retrieves multiple records from a database table based on the criteria specified in the query.
1344
- *
1345
- * It returns record to new Map
1346
- * @param {string} column
1347
- * @example
1348
- * const results = await new DB('posts')
1349
- * .findGroupBy('user_id')
1350
- *
1351
- * // you can find with user id in the results
1352
- * const postsByUserId1 = results.get(1)
1353
- * @returns {promise<Array>}
1354
- */
1341
+ * The 'findGroupBy' method is used to execute a database query and retrieve the result set that matches the query conditions.
1342
+ *
1343
+ * It retrieves multiple records from a database table based on the criteria specified in the query.
1344
+ *
1345
+ * It returns record to new Map
1346
+ * @param {string} column
1347
+ * @example
1348
+ * const results = await new DB('posts')
1349
+ * .findGroupBy('user_id')
1350
+ *
1351
+ * // you can find with user id in the results
1352
+ * const postsByUserId1 = results.get(1)
1353
+ * @returns {promise<Array>}
1354
+ */
1355
1355
  findGroupBy(column: string): Promise<Map<string | number, any[]>>;
1356
1356
  /**
1357
1357
  * The 'save' method is used to persist a new 'Model' or new 'DB' instance or update an existing model instance in the database.
@@ -1444,7 +1444,7 @@ declare class Builder extends AbstractBuilder {
1444
1444
  * @property {boolean} option.force
1445
1445
  * @returns {promise<boolean>}
1446
1446
  */
1447
- truncate({ force }?: {
1447
+ truncate({ force, }?: {
1448
1448
  force?: boolean;
1449
1449
  }): Promise<boolean>;
1450
1450
  /**
@@ -1493,7 +1493,7 @@ declare class Builder extends AbstractBuilder {
1493
1493
  protected _resultHandlerExists(data: any): any;
1494
1494
  whereReference(tableAndLocalKey: string, tableAndForeignKey?: string): this;
1495
1495
  protected _queryStatement(sql: string): Promise<any[]>;
1496
- protected _actionStatement({ sql, returnId }: {
1496
+ protected _actionStatement({ sql, returnId, }: {
1497
1497
  sql: string;
1498
1498
  returnId?: boolean;
1499
1499
  }): Promise<any>;