@zuzjs/orm 0.1.1 → 0.1.2
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/dist/bin.js +16 -29
- package/dist/core/cli.d.ts +5 -0
- package/dist/core/cli.js +78 -0
- package/dist/drivers/mysql/index.js +4 -0
- package/dist/drivers/queryBuilder.d.ts +91 -0
- package/dist/drivers/queryBuilder.js +91 -0
- package/dist/index.d.ts +65 -1
- package/dist/index.js +65 -1
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -4,36 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const picocolors_1 = __importDefault(require("picocolors"));
|
|
9
7
|
const commander_1 = require("commander");
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
8
|
+
const cli_js_1 = require("./core/cli.js");
|
|
9
|
+
const picocolors_1 = __importDefault(require("picocolors"));
|
|
10
|
+
const version = `0.1.2`;
|
|
11
|
+
commander_1.program
|
|
12
|
+
.name("zorm")
|
|
13
|
+
.description("ZuzORM is a lightweight ORM wrapper around TypeORM with support for MySQL.");
|
|
14
|
+
/**Version */
|
|
14
15
|
commander_1.program
|
|
15
16
|
.option(`-v, --version`)
|
|
16
|
-
.
|
|
17
|
-
.
|
|
18
|
-
|
|
19
|
-
const { version, connection, dist: destination } = commander_1.program.opts();
|
|
20
|
-
if (version) {
|
|
21
|
-
console.log(`ZuzORM v0.1.1`);
|
|
17
|
+
.description("Displays current version of ZuzORM")
|
|
18
|
+
.action(() => {
|
|
19
|
+
console.log(picocolors_1.default.cyan(`ZuzORM v${version}`));
|
|
22
20
|
process.exit(1);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
if (connection.startsWith(`mysql`)) {
|
|
32
|
-
const driver = new index_js_1.MySqlDriver(decodeURIComponent(connection), dist);
|
|
33
|
-
driver.generate();
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
console.log(`Only MySQL is supported for now`);
|
|
37
|
-
process.exit(1);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
21
|
+
});
|
|
22
|
+
commander_1.program
|
|
23
|
+
.command(`pull`)
|
|
24
|
+
.description(`Pull using DATABASE_URL from .env in project directory`)
|
|
25
|
+
.action(cli_js_1.Pull);
|
|
26
|
+
commander_1.program.parse();
|
package/dist/core/cli.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Pull = exports.buildEntities = void 0;
|
|
40
|
+
const fs_1 = __importStar(require("fs"));
|
|
41
|
+
const path_1 = __importDefault(require("path"));
|
|
42
|
+
const picocolors_1 = __importDefault(require("picocolors"));
|
|
43
|
+
const index_js_1 = require("./index.js");
|
|
44
|
+
const mysql_1 = require("../drivers/mysql");
|
|
45
|
+
const buildEntities = (connection, dist) => {
|
|
46
|
+
dist = dist || path_1.default.join(`.zorm`);
|
|
47
|
+
const _checkDist = (0, index_js_1.checkDirectory)(path_1.default.join(process.cwd(), dist), true);
|
|
48
|
+
if (!_checkDist.access) {
|
|
49
|
+
console.log(picocolors_1.default.red(`○ ${_checkDist.message}`));
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
if (connection.startsWith(`mysql`)) {
|
|
53
|
+
const driver = new mysql_1.MySqlDriver(decodeURIComponent(connection), dist);
|
|
54
|
+
driver.generate();
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
console.log(`○ Only MySQL is supported for now`);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
exports.buildEntities = buildEntities;
|
|
62
|
+
/**
|
|
63
|
+
* Pull using DATABASE_URL from .env in project directory
|
|
64
|
+
**/
|
|
65
|
+
const Pull = () => {
|
|
66
|
+
const env = path_1.default.join(process.cwd(), `.env`);
|
|
67
|
+
if (!(0, fs_1.existsSync)(env)) {
|
|
68
|
+
console.log(picocolors_1.default.red(`○ ".env" not exists. Create .env and add DATABASE_URL="connection_string"`));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const raw = fs_1.default.readFileSync(env, `utf8`).split(`\n`).filter((line) => line.startsWith(`DATABASE_URL`));
|
|
72
|
+
if (raw.length == 0) {
|
|
73
|
+
console.log(picocolors_1.default.red(`○ DATABASE_URL not found in ".env". Add DATABASE_URL="connection_string"`));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
(0, exports.buildEntities)(raw[0].trim().replace(/DATABASE_URL=|"|"/g, ``));
|
|
77
|
+
};
|
|
78
|
+
exports.Pull = Pull;
|
|
@@ -144,6 +144,10 @@ class MySqlDriver {
|
|
|
144
144
|
// Get table structure
|
|
145
145
|
const [columns] = await self.pool.execute(`DESCRIBE ${tableName}`);
|
|
146
146
|
const entityCode = [
|
|
147
|
+
`/**`,
|
|
148
|
+
`* AutoGenerated by @zuzjs/orm.`,
|
|
149
|
+
`* @ ${new Date().toString().split(` GMT`)[0].trim()}`,
|
|
150
|
+
`*/`,
|
|
147
151
|
`import { Entity, PrimaryColumn, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";\n`,
|
|
148
152
|
`@Entity({ name: "${tableName}" })`,
|
|
149
153
|
`export class ${(0, index_js_1.toPascalCase)(tableName)} extends BaseEntity {\n`
|
|
@@ -16,27 +16,118 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
16
16
|
_update(): this;
|
|
17
17
|
_delete(): this;
|
|
18
18
|
_getRawQuery(): [string, any[]];
|
|
19
|
+
/**
|
|
20
|
+
* Sets the values for an insert or update query.
|
|
21
|
+
* @param data - The data to be inserted or updated.
|
|
22
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
23
|
+
*/
|
|
19
24
|
with(data: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T[]>): this;
|
|
25
|
+
/**
|
|
26
|
+
* Specifies the fields to be selected in a select query.
|
|
27
|
+
* @param fields - The fields to be selected.
|
|
28
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
29
|
+
*/
|
|
20
30
|
select(fields: (keyof T)[]): this;
|
|
31
|
+
/**
|
|
32
|
+
* Adds a WHERE condition to the query.
|
|
33
|
+
* @param condition - The condition to be added.
|
|
34
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
35
|
+
*/
|
|
21
36
|
where(condition: FindOptionsWhere<T>): this;
|
|
37
|
+
/**
|
|
38
|
+
* Adds an OR condition to the query.
|
|
39
|
+
* @param condition - The condition to be added.
|
|
40
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
41
|
+
*/
|
|
22
42
|
or(condition: FindOptionsWhere<T>): this;
|
|
43
|
+
/**
|
|
44
|
+
* Adds an ORDER BY clause to the query.
|
|
45
|
+
* @param field - The field to order by.
|
|
46
|
+
* @param direction - The direction of the order (ASC or DESC).
|
|
47
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
48
|
+
*/
|
|
23
49
|
orderBy(field: keyof T, direction?: "ASC" | "DESC"): this;
|
|
50
|
+
/**
|
|
51
|
+
* Adds a LIMIT clause to the query.
|
|
52
|
+
* @param n - The maximum number of records to return.
|
|
53
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
54
|
+
*/
|
|
24
55
|
limit(n: number): this;
|
|
56
|
+
/**
|
|
57
|
+
* Adds an OFFSET clause to the query.
|
|
58
|
+
* @param n - The number of records to skip.
|
|
59
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
60
|
+
*/
|
|
25
61
|
offset(n: number): this;
|
|
62
|
+
/**
|
|
63
|
+
* Adds relations to be included in the query.
|
|
64
|
+
* @param relations - The relations to be included.
|
|
65
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
66
|
+
*/
|
|
26
67
|
relations(relations: string[]): this;
|
|
68
|
+
/**
|
|
69
|
+
* Adds an INNER JOIN clause to the query.
|
|
70
|
+
* @param relation - The relation to join.
|
|
71
|
+
* @param alias - The alias for the joined relation.
|
|
72
|
+
* @param condition - Optional condition for the join.
|
|
73
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
74
|
+
*/
|
|
27
75
|
innerJoin(relation: string, alias: string, condition?: string): this;
|
|
76
|
+
/**
|
|
77
|
+
* Adds a LEFT JOIN clause to the query.
|
|
78
|
+
* @param relation - The relation to join.
|
|
79
|
+
* @param alias - The alias for the joined relation.
|
|
80
|
+
* @param condition - Optional condition for the join.
|
|
81
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
82
|
+
*/
|
|
28
83
|
leftJoin(relation: string, alias: string, condition?: string): this;
|
|
84
|
+
/**
|
|
85
|
+
* Adds a GROUP BY clause to the query.
|
|
86
|
+
* @param field - The field to group by.
|
|
87
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
88
|
+
*/
|
|
29
89
|
groupBy(field: keyof T): this;
|
|
90
|
+
/**
|
|
91
|
+
* Adds a HAVING clause to the query.
|
|
92
|
+
* @param condition - The condition for the HAVING clause.
|
|
93
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
94
|
+
*/
|
|
30
95
|
having(condition: string): this;
|
|
96
|
+
/**
|
|
97
|
+
* Adds an IN clause to the query.
|
|
98
|
+
* @param field - The field to check.
|
|
99
|
+
* @param values - The values for the IN clause.
|
|
100
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
101
|
+
*/
|
|
31
102
|
in(field: keyof T, values: any[]): this;
|
|
103
|
+
/**
|
|
104
|
+
* Adds a DISTINCT clause to the query.
|
|
105
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
106
|
+
*/
|
|
32
107
|
distinct(): this;
|
|
33
108
|
count(): Promise<number>;
|
|
34
109
|
sum(field: keyof T): Promise<number>;
|
|
35
110
|
avg(field: keyof T): Promise<number>;
|
|
36
111
|
min(field: keyof T): Promise<number>;
|
|
37
112
|
max(field: keyof T): Promise<number>;
|
|
113
|
+
/**
|
|
114
|
+
* Executes a raw SQL query.
|
|
115
|
+
* @param sql - The raw SQL query.
|
|
116
|
+
* @param params - Optional parameters for the query.
|
|
117
|
+
* @returns A promise that resolves with the query result.
|
|
118
|
+
*/
|
|
38
119
|
rawQuery(sql: string, params?: any): Promise<any>;
|
|
120
|
+
/**
|
|
121
|
+
* Executes the query and returns the result.
|
|
122
|
+
* @returns A promise that resolves with the query result.
|
|
123
|
+
*/
|
|
39
124
|
execute(): Promise<R>;
|
|
125
|
+
/**
|
|
126
|
+
* Handles the fulfillment and rejection of the promise.
|
|
127
|
+
* @param onfulfilled - The callback to execute when the promise is fulfilled.
|
|
128
|
+
* @param onrejected - The callback to execute when the promise is rejected.
|
|
129
|
+
* @returns A promise that resolves with the result of the callback.
|
|
130
|
+
*/
|
|
40
131
|
then<TResult1 = R, TResult2 = never>(onfulfilled?: ((value: R) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
41
132
|
}
|
|
42
133
|
export default ZormQueryBuilder;
|
|
@@ -63,40 +63,88 @@ class ZormQueryBuilder extends Promise {
|
|
|
63
63
|
_getRawQuery() {
|
|
64
64
|
return this.queryBuilder.getQueryAndParameters();
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Sets the values for an insert or update query.
|
|
68
|
+
* @param data - The data to be inserted or updated.
|
|
69
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
70
|
+
*/
|
|
66
71
|
with(data) {
|
|
67
72
|
this.queryValues = data;
|
|
68
73
|
return this;
|
|
69
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Specifies the fields to be selected in a select query.
|
|
77
|
+
* @param fields - The fields to be selected.
|
|
78
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
79
|
+
*/
|
|
70
80
|
select(fields) {
|
|
71
81
|
this.queryBuilder
|
|
72
82
|
.select(fields.map(field => `${this.entityAlias}.${String(field)}`));
|
|
73
83
|
return this;
|
|
74
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Adds a WHERE condition to the query.
|
|
87
|
+
* @param condition - The condition to be added.
|
|
88
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
89
|
+
*/
|
|
75
90
|
where(condition) {
|
|
76
91
|
this.queryBuilder.where(condition);
|
|
77
92
|
this.whereCount++;
|
|
78
93
|
return this;
|
|
79
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Adds an OR condition to the query.
|
|
97
|
+
* @param condition - The condition to be added.
|
|
98
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
99
|
+
*/
|
|
80
100
|
or(condition) {
|
|
81
101
|
this.queryBuilder.orWhere(condition);
|
|
82
102
|
return this;
|
|
83
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Adds an ORDER BY clause to the query.
|
|
106
|
+
* @param field - The field to order by.
|
|
107
|
+
* @param direction - The direction of the order (ASC or DESC).
|
|
108
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
109
|
+
*/
|
|
84
110
|
orderBy(field, direction = "ASC") {
|
|
85
111
|
this.queryBuilder.orderBy(`${this.entityAlias}.${String(field)}`, direction);
|
|
86
112
|
return this;
|
|
87
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Adds a LIMIT clause to the query.
|
|
116
|
+
* @param n - The maximum number of records to return.
|
|
117
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
118
|
+
*/
|
|
88
119
|
limit(n) {
|
|
89
120
|
this.queryBuilder.limit(n);
|
|
90
121
|
return this;
|
|
91
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Adds an OFFSET clause to the query.
|
|
125
|
+
* @param n - The number of records to skip.
|
|
126
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
127
|
+
*/
|
|
92
128
|
offset(n) {
|
|
93
129
|
this.queryBuilder.offset(n);
|
|
94
130
|
return this;
|
|
95
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Adds relations to be included in the query.
|
|
134
|
+
* @param relations - The relations to be included.
|
|
135
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
136
|
+
*/
|
|
96
137
|
relations(relations) {
|
|
97
138
|
relations.forEach(relation => this.queryBuilder.leftJoinAndSelect(`${this.entityAlias}.${relation}`, relation));
|
|
98
139
|
return this;
|
|
99
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Adds an INNER JOIN clause to the query.
|
|
143
|
+
* @param relation - The relation to join.
|
|
144
|
+
* @param alias - The alias for the joined relation.
|
|
145
|
+
* @param condition - Optional condition for the join.
|
|
146
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
147
|
+
*/
|
|
100
148
|
innerJoin(relation, alias, condition) {
|
|
101
149
|
if (condition) {
|
|
102
150
|
this.queryBuilder.innerJoin(`${this.entityAlias}.${relation}`, alias, condition);
|
|
@@ -106,6 +154,13 @@ class ZormQueryBuilder extends Promise {
|
|
|
106
154
|
}
|
|
107
155
|
return this;
|
|
108
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Adds a LEFT JOIN clause to the query.
|
|
159
|
+
* @param relation - The relation to join.
|
|
160
|
+
* @param alias - The alias for the joined relation.
|
|
161
|
+
* @param condition - Optional condition for the join.
|
|
162
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
163
|
+
*/
|
|
109
164
|
leftJoin(relation, alias, condition) {
|
|
110
165
|
if (condition) {
|
|
111
166
|
this.queryBuilder.leftJoin(`${this.entityAlias}.${relation}`, alias, condition);
|
|
@@ -115,18 +170,38 @@ class ZormQueryBuilder extends Promise {
|
|
|
115
170
|
}
|
|
116
171
|
return this;
|
|
117
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Adds a GROUP BY clause to the query.
|
|
175
|
+
* @param field - The field to group by.
|
|
176
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
177
|
+
*/
|
|
118
178
|
groupBy(field) {
|
|
119
179
|
this.queryBuilder.groupBy(`${this.entityAlias}.${String(field)}`);
|
|
120
180
|
return this;
|
|
121
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Adds a HAVING clause to the query.
|
|
184
|
+
* @param condition - The condition for the HAVING clause.
|
|
185
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
186
|
+
*/
|
|
122
187
|
having(condition) {
|
|
123
188
|
this.queryBuilder.having(condition);
|
|
124
189
|
return this;
|
|
125
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Adds an IN clause to the query.
|
|
193
|
+
* @param field - The field to check.
|
|
194
|
+
* @param values - The values for the IN clause.
|
|
195
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
196
|
+
*/
|
|
126
197
|
in(field, values) {
|
|
127
198
|
this.queryBuilder.andWhere(`${this.entityAlias}.${String(field)} IN (:...values)`, { values });
|
|
128
199
|
return this;
|
|
129
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Adds a DISTINCT clause to the query.
|
|
203
|
+
* @returns The current instance of ZormQueryBuilder.
|
|
204
|
+
*/
|
|
130
205
|
distinct() {
|
|
131
206
|
this.queryBuilder.distinct(true);
|
|
132
207
|
return this;
|
|
@@ -150,9 +225,19 @@ class ZormQueryBuilder extends Promise {
|
|
|
150
225
|
const result = await this.queryBuilder.select(`MAX(${this.entityAlias}.${String(field)})`, "max").getRawOne();
|
|
151
226
|
return result.max || 0;
|
|
152
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Executes a raw SQL query.
|
|
230
|
+
* @param sql - The raw SQL query.
|
|
231
|
+
* @param params - Optional parameters for the query.
|
|
232
|
+
* @returns A promise that resolves with the query result.
|
|
233
|
+
*/
|
|
153
234
|
async rawQuery(sql, params) {
|
|
154
235
|
return await this.repository.query(sql, params);
|
|
155
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Executes the query and returns the result.
|
|
239
|
+
* @returns A promise that resolves with the query result.
|
|
240
|
+
*/
|
|
156
241
|
async execute() {
|
|
157
242
|
try {
|
|
158
243
|
switch (this.action) {
|
|
@@ -217,6 +302,12 @@ class ZormQueryBuilder extends Promise {
|
|
|
217
302
|
}
|
|
218
303
|
}
|
|
219
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Handles the fulfillment and rejection of the promise.
|
|
307
|
+
* @param onfulfilled - The callback to execute when the promise is fulfilled.
|
|
308
|
+
* @param onrejected - The callback to execute when the promise is rejected.
|
|
309
|
+
* @returns A promise that resolves with the result of the callback.
|
|
310
|
+
*/
|
|
220
311
|
then(onfulfilled, onrejected) {
|
|
221
312
|
return this.execute().then(onfulfilled, onrejected);
|
|
222
313
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,89 @@
|
|
|
1
1
|
import { EntitySchema, EntityTarget, MixedList, ObjectLiteral, Repository } from "typeorm";
|
|
2
2
|
import ZormQueryBuilder from "./drivers/queryBuilder.js";
|
|
3
3
|
import { InsertQueryResult, SelectQueryResult, UpdateQueryResult } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
6
|
+
*/
|
|
4
7
|
declare class Zorm {
|
|
8
|
+
/**
|
|
9
|
+
* Singleton instance of Zorm.
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
5
12
|
private static instance;
|
|
13
|
+
/**
|
|
14
|
+
* TypeORM DataSource instance.
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
6
17
|
private dataSource;
|
|
18
|
+
/**
|
|
19
|
+
* Flag to track if the connection is initialized.
|
|
20
|
+
* @private
|
|
21
|
+
*/
|
|
7
22
|
private initialized;
|
|
23
|
+
/**
|
|
24
|
+
* Determines whether to use Promises for queries.
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
8
27
|
private usePromise;
|
|
28
|
+
/**
|
|
29
|
+
* Private constructor to enforce singleton pattern.
|
|
30
|
+
* @param {string} connectionString - The database connection string.
|
|
31
|
+
* @param {string | null} [entitiesPath] - Path to the entities directory.
|
|
32
|
+
* @param {boolean} [usePromise] - Whether to use Promises for queries.
|
|
33
|
+
* @private
|
|
34
|
+
*/
|
|
9
35
|
private constructor();
|
|
36
|
+
/**
|
|
37
|
+
* Returns the singleton instance of Zorm.
|
|
38
|
+
* @param {string} connectionString - The database connection string.
|
|
39
|
+
* @param {string | null} [entitiesPath] - Path to the entities directory.
|
|
40
|
+
* @param {boolean} [usePromise] - Whether to use Promises for queries.
|
|
41
|
+
* @returns {Zorm} The singleton instance of Zorm.
|
|
42
|
+
*/
|
|
10
43
|
static get(connectionString: string, entitiesPath?: string | null, usePromise?: boolean): Zorm;
|
|
44
|
+
/**
|
|
45
|
+
* Connects to the database and initializes entities.
|
|
46
|
+
* @param {MixedList<string | Function | EntitySchema<any>>} entities - List of entity schemas.
|
|
47
|
+
* @returns {Promise<void>} Resolves when the connection is initialized.
|
|
48
|
+
*/
|
|
11
49
|
connect(entities: MixedList<string | Function | EntitySchema<any>>): Promise<void>;
|
|
12
50
|
/**
|
|
13
51
|
* Returns the appropriate QueryBuilder based on the database type.
|
|
52
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
53
|
+
* @param {QueryAction} action - The query action type.
|
|
54
|
+
* @returns {ZormQueryBuilder<T, R>} Query builder instance.
|
|
55
|
+
* @private
|
|
14
56
|
*/
|
|
15
57
|
private getQueryBuilder;
|
|
16
58
|
/**
|
|
17
|
-
*
|
|
59
|
+
* Retrieves the repository for a given entity.
|
|
60
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
61
|
+
* @returns {Repository<T>} The repository instance.
|
|
18
62
|
*/
|
|
19
63
|
getRepository<T extends ObjectLiteral>(entity: EntityTarget<T>): Repository<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Creates a new record in the database.
|
|
66
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
67
|
+
* @returns {ZormQueryBuilder<T, InsertQueryResult>} The query builder instance.
|
|
68
|
+
*/
|
|
20
69
|
create<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, InsertQueryResult>;
|
|
70
|
+
/**
|
|
71
|
+
* Finds records in the database.
|
|
72
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
73
|
+
* @returns {ZormQueryBuilder<T, SelectQueryResult>} The query builder instance.
|
|
74
|
+
*/
|
|
21
75
|
find<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, SelectQueryResult>;
|
|
76
|
+
/**
|
|
77
|
+
* Updates records in the database.
|
|
78
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
79
|
+
* @returns {ZormQueryBuilder<T, UpdateQueryResult>} The query builder instance.
|
|
80
|
+
*/
|
|
22
81
|
update<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, UpdateQueryResult>;
|
|
82
|
+
/**
|
|
83
|
+
* Deletes records from the database.
|
|
84
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
85
|
+
* @returns {ZormQueryBuilder<T, DeleteQueryResult>} The query builder instance.
|
|
86
|
+
*/
|
|
23
87
|
delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, UpdateQueryResult>;
|
|
24
88
|
}
|
|
25
89
|
export default Zorm;
|
package/dist/index.js
CHANGED
|
@@ -9,11 +9,37 @@ const index_js_1 = require("./core/index.js");
|
|
|
9
9
|
const picocolors_1 = __importDefault(require("picocolors"));
|
|
10
10
|
const index_js_2 = require("./drivers/mysql/index.js");
|
|
11
11
|
const queryBuilder_js_1 = __importDefault(require("./drivers/queryBuilder.js"));
|
|
12
|
+
/**
|
|
13
|
+
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
14
|
+
*/
|
|
12
15
|
class Zorm {
|
|
16
|
+
/**
|
|
17
|
+
* Singleton instance of Zorm.
|
|
18
|
+
* @private
|
|
19
|
+
*/
|
|
13
20
|
static instance;
|
|
21
|
+
/**
|
|
22
|
+
* TypeORM DataSource instance.
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
14
25
|
dataSource;
|
|
26
|
+
/**
|
|
27
|
+
* Flag to track if the connection is initialized.
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
15
30
|
initialized = false;
|
|
31
|
+
/**
|
|
32
|
+
* Determines whether to use Promises for queries.
|
|
33
|
+
* @private
|
|
34
|
+
*/
|
|
16
35
|
usePromise = false;
|
|
36
|
+
/**
|
|
37
|
+
* Private constructor to enforce singleton pattern.
|
|
38
|
+
* @param {string} connectionString - The database connection string.
|
|
39
|
+
* @param {string | null} [entitiesPath] - Path to the entities directory.
|
|
40
|
+
* @param {boolean} [usePromise] - Whether to use Promises for queries.
|
|
41
|
+
* @private
|
|
42
|
+
*/
|
|
17
43
|
constructor(connectionString, entitiesPath, usePromise) {
|
|
18
44
|
const _dist = entitiesPath || path_1.default.join(`src`, `zorm`);
|
|
19
45
|
const dist = path_1.default.join(process.cwd(), _dist);
|
|
@@ -40,12 +66,24 @@ class Zorm {
|
|
|
40
66
|
process.exit(1);
|
|
41
67
|
}
|
|
42
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns the singleton instance of Zorm.
|
|
71
|
+
* @param {string} connectionString - The database connection string.
|
|
72
|
+
* @param {string | null} [entitiesPath] - Path to the entities directory.
|
|
73
|
+
* @param {boolean} [usePromise] - Whether to use Promises for queries.
|
|
74
|
+
* @returns {Zorm} The singleton instance of Zorm.
|
|
75
|
+
*/
|
|
43
76
|
static get(connectionString, entitiesPath, usePromise) {
|
|
44
77
|
if (!Zorm.instance) {
|
|
45
78
|
Zorm.instance = new Zorm(connectionString, entitiesPath, usePromise);
|
|
46
79
|
}
|
|
47
80
|
return Zorm.instance;
|
|
48
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Connects to the database and initializes entities.
|
|
84
|
+
* @param {MixedList<string | Function | EntitySchema<any>>} entities - List of entity schemas.
|
|
85
|
+
* @returns {Promise<void>} Resolves when the connection is initialized.
|
|
86
|
+
*/
|
|
49
87
|
async connect(entities) {
|
|
50
88
|
if (!this.initialized) {
|
|
51
89
|
try {
|
|
@@ -61,6 +99,10 @@ class Zorm {
|
|
|
61
99
|
}
|
|
62
100
|
/**
|
|
63
101
|
* Returns the appropriate QueryBuilder based on the database type.
|
|
102
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
103
|
+
* @param {QueryAction} action - The query action type.
|
|
104
|
+
* @returns {ZormQueryBuilder<T, R>} Query builder instance.
|
|
105
|
+
* @private
|
|
64
106
|
*/
|
|
65
107
|
getQueryBuilder(entity, action) {
|
|
66
108
|
const repository = this.getRepository(entity);
|
|
@@ -79,23 +121,45 @@ class Zorm {
|
|
|
79
121
|
}
|
|
80
122
|
}
|
|
81
123
|
/**
|
|
82
|
-
*
|
|
124
|
+
* Retrieves the repository for a given entity.
|
|
125
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
126
|
+
* @returns {Repository<T>} The repository instance.
|
|
83
127
|
*/
|
|
84
128
|
getRepository(entity) {
|
|
85
129
|
return this.dataSource.getRepository(entity);
|
|
86
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Creates a new record in the database.
|
|
133
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
134
|
+
* @returns {ZormQueryBuilder<T, InsertQueryResult>} The query builder instance.
|
|
135
|
+
*/
|
|
87
136
|
create(entity) {
|
|
88
137
|
return this.getQueryBuilder(entity, "create");
|
|
89
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Finds records in the database.
|
|
141
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
142
|
+
* @returns {ZormQueryBuilder<T, SelectQueryResult>} The query builder instance.
|
|
143
|
+
*/
|
|
90
144
|
find(entity) {
|
|
91
145
|
return this.getQueryBuilder(entity, "select");
|
|
92
146
|
}
|
|
93
147
|
// upsert<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, InsertQueryResult> {
|
|
94
148
|
// return this.getQueryBuilder(entity, "upsert");
|
|
95
149
|
// }
|
|
150
|
+
/**
|
|
151
|
+
* Updates records in the database.
|
|
152
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
153
|
+
* @returns {ZormQueryBuilder<T, UpdateQueryResult>} The query builder instance.
|
|
154
|
+
*/
|
|
96
155
|
update(entity) {
|
|
97
156
|
return this.getQueryBuilder(entity, "update");
|
|
98
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Deletes records from the database.
|
|
160
|
+
* @param {EntityTarget<T>} entity - The entity target.
|
|
161
|
+
* @returns {ZormQueryBuilder<T, DeleteQueryResult>} The query builder instance.
|
|
162
|
+
*/
|
|
99
163
|
delete(entity) {
|
|
100
164
|
return this.getQueryBuilder(entity, "delete");
|
|
101
165
|
}
|