tspace-mysql 1.3.6 → 1.3.7

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
@@ -412,7 +412,7 @@ class User extends Model {
412
412
  * this.useRegistry() // => build-in functions registry
413
413
  * this.useLoadRelationsInRegistry() // => auto generated results from relationship to results
414
414
  * this.useBuiltInRelationFunctions() // => build-in functions relationships to results
415
- * this.useSchema({
415
+ * this.useValidationSchema({
416
416
  * id : Number,
417
417
  * username : String
418
418
  * created_at : Date,
@@ -9,8 +9,8 @@ class ${model} extends Model {
9
9
  *
10
10
  * Assign setting global in your model
11
11
  * @useMethod
12
- *
13
- * this.useDebug() // => runing a uuid (universally unique identifier) when insert new data
12
+ * this.useUUID() // => runing a uuid (universally unique identifier) when insert new data
13
+ * this.useDebug() // debug raw query
14
14
  * this.usePrimaryKey('id')
15
15
  * this.useTimestamp({ createdAt : 'created_at' , updatedAt : 'updated_at' }) // runing a timestamp when insert or update
16
16
  * this.useSoftDelete()
@@ -0,0 +1,122 @@
1
+ import { Pagination } from '../Interface';
2
+ import { StateHandler } from '../StateHandler';
3
+ declare abstract class AbstractBuilder {
4
+ protected $setters: string[];
5
+ protected $utils: {
6
+ [key: string]: Function;
7
+ };
8
+ protected $constants: Function;
9
+ protected $state: StateHandler;
10
+ protected $pool: {
11
+ query: Function;
12
+ set: Function;
13
+ get: Function;
14
+ };
15
+ protected $logger: {
16
+ get: Function;
17
+ set: (value: string) => void;
18
+ reset: () => void;
19
+ check: (value: string) => boolean;
20
+ };
21
+ protected $attributes: {
22
+ [key: string]: any;
23
+ } | null;
24
+ abstract void(): this;
25
+ abstract debug(): this;
26
+ abstract dd(): this;
27
+ abstract select(...columns: string[]): this;
28
+ abstract distinct(...columns: string[]): this;
29
+ abstract whereNull(column: string): this;
30
+ abstract whereNotNull(column: string): this;
31
+ abstract where(column: string, operator: string, value: string): this;
32
+ abstract whereSensitive(column: string, operator: string, value: string): this;
33
+ abstract whereRaw(sql: string): this;
34
+ abstract whereId(id: number): this;
35
+ abstract whereUser(id: number): this;
36
+ abstract whereEmail(value: string): this;
37
+ abstract whereQuery(callback: Function): this;
38
+ abstract orWhere(column: string, operator: string, value: string): this;
39
+ abstract whereIn(column: string, arrayValues: Array<any>): this;
40
+ abstract orWhereIn(column: string, arrayValues: Array<any>): this;
41
+ abstract whereNotIn(column: string, arrayValues: Array<any>): this;
42
+ abstract whereSubQuery(column: string, subQuery: string): this;
43
+ abstract whereNotSubQuery(column: string, subQuery: string): this;
44
+ abstract orWhereSubQuery(column: string, subQuery: string): this;
45
+ abstract whereBetween(column: string, arrayValue: Array<any>): this;
46
+ abstract whereNotBetween(column: string, arrayValue: Array<any>): this;
47
+ abstract having(condition: string): this;
48
+ abstract havingRaw(condition: string): this;
49
+ abstract join(pk: string, fk: string): this;
50
+ abstract rightJoin(pk: string, fk: string): this;
51
+ abstract leftJoin(pk: string, fk: string): this;
52
+ abstract crossJoin(pk: string, fk: string): this;
53
+ abstract orderBy(column: string, order: string): this;
54
+ abstract orderByRaw(column: string, order: string): this;
55
+ abstract latest(...columns: Array<string>): this;
56
+ abstract latestRaw(...columns: Array<string>): this;
57
+ abstract oldest(...columns: Array<string>): this;
58
+ abstract oldestRaw(...columns: Array<string>): this;
59
+ abstract groupBy(...columns: string[]): this;
60
+ abstract groupByRaw(...columns: string[]): this;
61
+ abstract random(): this;
62
+ abstract inRandom(): this;
63
+ abstract limit(number: number): this;
64
+ abstract hidden(...columns: string[]): this;
65
+ abstract insert(objects: object): this;
66
+ abstract create(objects: object): this;
67
+ abstract update(objects: object): this;
68
+ abstract insertNotExists(objects: object): this;
69
+ abstract createNotExists(objects: object): this;
70
+ abstract insertOrUpdate(objects: object): this;
71
+ abstract createOrUpdate(objects: object): this;
72
+ abstract updateOrInsert(objects: object): this;
73
+ abstract updateOrCreate(objects: object): this;
74
+ abstract createMultiple(objects: object): this;
75
+ abstract insertMultiple(objects: object): this;
76
+ abstract except(...columns: string[]): this;
77
+ abstract only(...columns: string[]): this;
78
+ abstract drop(): Promise<any>;
79
+ abstract truncate(): Promise<any>;
80
+ abstract all(): Promise<Array<any>>;
81
+ abstract find(id: number): Promise<any>;
82
+ abstract pagination({ limit, page }: {
83
+ limit: number;
84
+ page: number;
85
+ }): Promise<Pagination>;
86
+ abstract paginate({ limit, page }: {
87
+ limit: number;
88
+ page: number;
89
+ }): Promise<Pagination>;
90
+ abstract first(): Promise<any>;
91
+ abstract firstOrError(message: string, options?: {
92
+ [key: string]: any;
93
+ }): Promise<any>;
94
+ abstract findOneOrError(message: string, options?: {
95
+ [key: string]: any;
96
+ }): Promise<any>;
97
+ abstract get(): Promise<any>;
98
+ abstract findOne(): Promise<any>;
99
+ abstract findMany(): Promise<any>;
100
+ abstract getGroupBy(column: string): Promise<any>;
101
+ abstract findManyGroupBy(column: string): Promise<any>;
102
+ abstract toArray(column: string): Promise<any>;
103
+ abstract toJSON(): Promise<any>;
104
+ abstract toSQL(): string;
105
+ abstract toString(): string;
106
+ abstract count(column: string): Promise<number>;
107
+ abstract sum(column: string): Promise<number>;
108
+ abstract avg(column: string): Promise<number>;
109
+ abstract max(column: string): Promise<number>;
110
+ abstract min(column: string): Promise<number>;
111
+ abstract rawQuery(sql: string): Promise<Array<any>>;
112
+ abstract delete(): Promise<boolean>;
113
+ abstract exists(): Promise<boolean>;
114
+ abstract save(): Promise<{
115
+ [key: string]: any;
116
+ } | Array<any> | null | undefined>;
117
+ abstract increment(column: string, value: number): Promise<any>;
118
+ abstract decrement(column: string, value: number): Promise<any>;
119
+ abstract faker(round: number): Promise<any>;
120
+ }
121
+ export { AbstractBuilder };
122
+ export default AbstractBuilder;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractBuilder = void 0;
4
+ const StateHandler_1 = require("../StateHandler");
5
+ class AbstractBuilder {
6
+ constructor() {
7
+ this.$setters = [
8
+ '$attributes',
9
+ '$logger',
10
+ '$utils',
11
+ '$constants',
12
+ '$pool',
13
+ '$state',
14
+ ];
15
+ this.$utils = {};
16
+ this.$constants = (name) => { };
17
+ this.$state = new StateHandler_1.StateHandler({});
18
+ this.$pool = {
19
+ query: (sql) => { },
20
+ set: (pool) => { },
21
+ get: () => { }
22
+ };
23
+ this.$logger = {
24
+ get: () => { },
25
+ set: (value) => { },
26
+ reset: () => { },
27
+ check: (value) => true || false
28
+ };
29
+ this.$attributes = null;
30
+ }
31
+ }
32
+ exports.AbstractBuilder = AbstractBuilder;
33
+ exports.default = AbstractBuilder;
@@ -0,0 +1,20 @@
1
+ import Builder from '../Builder';
2
+ import { Connection, ConnectionOptions } from '../Interface';
3
+ declare abstract class AbstractDB extends Builder {
4
+ abstract table(tableName: string): void;
5
+ abstract beginTransaction(): Promise<any>;
6
+ abstract makeObject(value: any): Record<string, any> | null;
7
+ abstract makeArray(value: any): Array<any>;
8
+ abstract generateUUID(): string;
9
+ abstract raw(sql: string): string;
10
+ abstract constants(constants?: string): string | {
11
+ [key: string]: any;
12
+ };
13
+ abstract caseUpdate(cases: {
14
+ when: string;
15
+ then: string;
16
+ }[], final?: string): string | [];
17
+ abstract getConnection(options: ConnectionOptions): Connection;
18
+ }
19
+ export { AbstractDB };
20
+ export default AbstractDB;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AbstractDB = void 0;
7
+ const Builder_1 = __importDefault(require("../Builder"));
8
+ class AbstractDB extends Builder_1.default {
9
+ }
10
+ exports.AbstractDB = AbstractDB;
11
+ exports.default = AbstractDB;
@@ -0,0 +1,45 @@
1
+ import { Relation, RelationQuery } from '../Interface';
2
+ import Builder from '../Builder';
3
+ declare abstract class AbstractModel extends Builder {
4
+ protected abstract useUUID(): this;
5
+ protected abstract usePrimaryKey(primaryKey: string): this;
6
+ protected abstract useRegistry(): this;
7
+ protected abstract useDebug(): this;
8
+ protected abstract useTable(table: string): this;
9
+ protected abstract useTablePlural(): this;
10
+ protected abstract useTableSingular(): this;
11
+ protected abstract useTimestamp(): this;
12
+ protected abstract useSoftDelete(): this;
13
+ protected abstract usePattern(pattern: string): this;
14
+ protected abstract useLoadRelationsInRegistry(): this;
15
+ protected abstract useBuiltInRelationFunctions(): this;
16
+ protected abstract define(): void;
17
+ protected abstract hasOne({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
18
+ protected abstract hasMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
19
+ protected abstract belongsTo({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
20
+ protected abstract belongsToMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
21
+ protected abstract buildMethodRelation(name: string, callback?: Function): this;
22
+ protected abstract hasOneBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
23
+ protected abstract hasManyBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
24
+ protected abstract belongsToBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
25
+ protected abstract belongsToManyBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
26
+ abstract ignoreSoftDelete(): this;
27
+ abstract disableSoftDelete(): this;
28
+ abstract registry(func: {
29
+ [key: string]: Function;
30
+ }): this;
31
+ abstract onlyTrashed(): Promise<any>;
32
+ abstract trashed(): Promise<any>;
33
+ abstract restore(): Promise<any>;
34
+ abstract with(...nameRelations: string[]): this;
35
+ abstract withQuery(nameRelations: string, callback: Function): this;
36
+ abstract withExists(...nameRelations: string[]): this;
37
+ abstract withAndTrashed(...nameRelations: string[]): this;
38
+ abstract has(...nameRelations: string[]): this;
39
+ abstract relations(...nameRelations: string[]): this;
40
+ abstract relationQuery(nameRelations: string, callback: Function): this;
41
+ abstract relationsExists(...nameRelations: string[]): this;
42
+ abstract relationsAndTrashed(...nameRelations: string[]): this;
43
+ }
44
+ export { AbstractModel };
45
+ export default AbstractModel;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AbstractModel = void 0;
7
+ const Builder_1 = __importDefault(require("../Builder"));
8
+ class AbstractModel extends Builder_1.default {
9
+ }
10
+ exports.AbstractModel = AbstractModel;
11
+ exports.default = AbstractModel;
@@ -1,3 +1,17 @@
1
+ /**
2
+ * make schema for table with Blueprint
3
+ * @example
4
+ * import { Schema , Blueprint } from 'tspace-mysql'
5
+ * await new Schema().table('persos1',{
6
+ * id : new Blueprint().int().notNull().primary().autoIncrement(),
7
+ * name : new Blueprint().varchar(255).default('my name ...'),
8
+ * email : new Blueprint().varchar(255).unique(),
9
+ * verify : new Blueprint().tinyInt(1).notNull(),
10
+ * created_at : new Blueprint().timestamp().null(),
11
+ * updated_at : new Blueprint().timestamp().null(),
12
+ * deleted_at : new Blueprint().timestamp().null()
13
+ * })
14
+ */
1
15
  declare class Blueprint {
2
16
  protected type: string;
3
17
  protected attrbuites: Array<string>;
@@ -115,10 +129,10 @@ declare class Blueprint {
115
129
  primary(): this;
116
130
  /**
117
131
  * Assign attrbuites 'default' in table
118
- * @param {string | number} n default value
132
+ * @param {string | number} value default value
119
133
  * @return {this} this
120
134
  */
121
- default(n: string | number): this;
135
+ default(value: string | number): this;
122
136
  /**
123
137
  * Assign attrbuites 'default currentTimestamp' in table
124
138
  * @return {this} this
@@ -1,6 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Blueprint = void 0;
4
+ /**
5
+ * make schema for table with Blueprint
6
+ * @example
7
+ * import { Schema , Blueprint } from 'tspace-mysql'
8
+ * await new Schema().table('persos1',{
9
+ * id : new Blueprint().int().notNull().primary().autoIncrement(),
10
+ * name : new Blueprint().varchar(255).default('my name ...'),
11
+ * email : new Blueprint().varchar(255).unique(),
12
+ * verify : new Blueprint().tinyInt(1).notNull(),
13
+ * created_at : new Blueprint().timestamp().null(),
14
+ * updated_at : new Blueprint().timestamp().null(),
15
+ * deleted_at : new Blueprint().timestamp().null()
16
+ * })
17
+ */
4
18
  class Blueprint {
5
19
  constructor() {
6
20
  this.type = '';
@@ -190,11 +204,11 @@ class Blueprint {
190
204
  }
191
205
  /**
192
206
  * Assign attrbuites 'default' in table
193
- * @param {string | number} n default value
207
+ * @param {string | number} value default value
194
208
  * @return {this} this
195
209
  */
196
- default(n) {
197
- this._addAssignAttrbuite(`DEFAULT '${n}'`);
210
+ default(value) {
211
+ this._addAssignAttrbuite(`DEFAULT '${value}'`);
198
212
  return this;
199
213
  }
200
214
  /**
@@ -1,4 +1,4 @@
1
- import { AbstractBuilder } from './AbstractBuilder';
1
+ import { AbstractBuilder } from './Abstract/AbstractBuilder';
2
2
  import { Pagination, Backup, ConnectionOptions, BackupToFile, Connection, ConnectionTransaction } from './Interface';
3
3
  declare class Builder extends AbstractBuilder {
4
4
  constructor();
@@ -26,7 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Builder = void 0;
27
27
  const fs_1 = __importDefault(require("fs"));
28
28
  const sql_formatter_1 = require("sql-formatter");
29
- const AbstractBuilder_1 = require("./AbstractBuilder");
29
+ const AbstractBuilder_1 = require("./Abstract/AbstractBuilder");
30
30
  const utils_1 = require("../utils");
31
31
  const constants_1 = require("../constants");
32
32
  const DB_1 = require("./DB");
@@ -52,7 +52,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
52
52
  * @return {this} this
53
53
  */
54
54
  except(...columns) {
55
- this.$state.set('EXCEPT', columns.length ? columns : ['id']);
55
+ this.$state.set('EXCEPT', columns.length ? columns : []);
56
56
  return this;
57
57
  }
58
58
  /**
@@ -1343,7 +1343,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1343
1343
  ].join(' ');
1344
1344
  const rawColumns = yield this.queryStatement(sql);
1345
1345
  const columns = rawColumns.map((column) => column.Field);
1346
- const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
1346
+ const removeExcept = columns.filter((column) => {
1347
+ const excepts = this.$state.get('EXCEPT');
1348
+ return excepts.every((except) => except !== column);
1349
+ });
1347
1350
  return removeExcept;
1348
1351
  });
1349
1352
  }
@@ -2162,6 +2165,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2162
2165
  tabWidth: 2,
2163
2166
  linesBetweenQueries: 1,
2164
2167
  }));
2168
+ // fs.writeFileSync(filePath, [...sql,'COMMIT;'].join('\n'))
2169
+ console.log(sql);
2165
2170
  return;
2166
2171
  });
2167
2172
  }
@@ -1,4 +1,4 @@
1
- import { AbstractDB } from './AbstractDB';
1
+ import { AbstractDB } from './Abstract/AbstractDB';
2
2
  import { Connection, ConnectionOptions, ConnectionTransaction } from './Interface';
3
3
  /**
4
4
  * Assign table name
@@ -24,7 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.DB = void 0;
27
- const AbstractDB_1 = require("./AbstractDB");
27
+ const AbstractDB_1 = require("./Abstract/AbstractDB");
28
28
  const ProxyHandler_1 = require("./ProxyHandler");
29
29
  const connection_1 = require("../connection");
30
30
  const StateHandler_1 = __importDefault(require("./StateHandler"));
@@ -1,4 +1,4 @@
1
- import { AbstractModel } from './AbstractModel';
1
+ import { AbstractModel } from './Abstract/AbstractModel';
2
2
  import { Relation, Pagination, RelationQuery } from './Interface';
3
3
  declare class Model extends AbstractModel {
4
4
  constructor();
@@ -211,7 +211,7 @@ declare class Model extends AbstractModel {
211
211
  * @example
212
212
  * class User extends Model {
213
213
  * constructor() {
214
- * this.useSchema({
214
+ * this.useValidationSchema({
215
215
  * id : Number,
216
216
  * email : String,
217
217
  * name : String,
@@ -221,7 +221,7 @@ declare class Model extends AbstractModel {
221
221
  * }
222
222
  * @return {this} this
223
223
  */
224
- protected useSchema(schema: Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
224
+ protected useValidationSchema(schema: Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
225
225
  /**
226
226
  * Assign hook function when execute returned results to callback function
227
227
  * @param {Array<Function>} arrayFunctions functions for callback result
@@ -16,7 +16,7 @@ exports.Model = void 0;
16
16
  const pluralize_1 = __importDefault(require("pluralize"));
17
17
  const DB_1 = require("./DB");
18
18
  const Schema_1 = require("./Schema");
19
- const AbstractModel_1 = require("./AbstractModel");
19
+ const AbstractModel_1 = require("./Abstract/AbstractModel");
20
20
  const ProxyHandler_1 = require("./ProxyHandler");
21
21
  const StateHandler_1 = require("./StateHandler");
22
22
  class Model extends AbstractModel_1.AbstractModel {
@@ -301,7 +301,7 @@ class Model extends AbstractModel_1.AbstractModel {
301
301
  * @example
302
302
  * class User extends Model {
303
303
  * constructor() {
304
- * this.useSchema({
304
+ * this.useValidationSchema({
305
305
  * id : Number,
306
306
  * email : String,
307
307
  * name : String,
@@ -311,7 +311,7 @@ class Model extends AbstractModel_1.AbstractModel {
311
311
  * }
312
312
  * @return {this} this
313
313
  */
314
- useSchema(schema) {
314
+ useValidationSchema(schema) {
315
315
  this.$state.set('SCHEMA', schema);
316
316
  return this;
317
317
  }
@@ -342,7 +342,10 @@ class Model extends AbstractModel_1.AbstractModel {
342
342
  return __awaiter(this, void 0, void 0, function* () {
343
343
  if (this.$state.get('SCHEMA')) {
344
344
  const columns = Object.keys(this.$state.get('SCHEMA'));
345
- const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
345
+ const removeExcept = columns.filter((column) => {
346
+ const excepts = this.$state.get('EXCEPT');
347
+ return excepts.every((except) => except !== column);
348
+ });
346
349
  return removeExcept;
347
350
  }
348
351
  const rawColumns = yield this.queryStatement([
@@ -352,7 +355,10 @@ class Model extends AbstractModel_1.AbstractModel {
352
355
  `${this.$state.get('TABLE_NAME')}`
353
356
  ].join(' '));
354
357
  const columns = rawColumns.map((column) => column.Field);
355
- const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
358
+ const removeExcept = columns.filter((column) => {
359
+ const excepts = this.$state.get('EXCEPT');
360
+ return excepts.every((except) => except !== column);
361
+ });
356
362
  return removeExcept;
357
363
  });
358
364
  }
@@ -1,13 +1,7 @@
1
1
  import { Builder } from "./Builder";
2
2
  declare class Schema extends Builder {
3
- table: (table: string, schemas: Record<string, {
4
- type: string;
5
- attrbuites: string[];
6
- }>) => Promise<void>;
7
- createTable: (table: string, schemas: Record<string, {
8
- type: string;
9
- attrbuites: string[];
10
- }>) => Promise<any>;
3
+ table: (table: string, schemas: Record<string, any>) => Promise<void>;
4
+ createTable: (table: string, schemas: Record<string, any>) => Promise<any>;
11
5
  }
12
6
  export { Schema };
13
7
  export default Schema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",