@zuzjs/orm 0.1.2 → 0.1.4
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 +2 -2
- package/dist/core/cli.js +1 -1
- package/dist/drivers/mysql/index.js +6 -2
- package/dist/drivers/queryBuilder.d.ts +2 -1
- package/dist/drivers/queryBuilder.js +58 -10
- package/dist/index.d.ts +4 -2
- package/dist/index.js +16 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +7 -0
- package/package.json +2 -1
package/dist/bin.js
CHANGED
|
@@ -7,7 +7,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
const commander_1 = require("commander");
|
|
8
8
|
const cli_js_1 = require("./core/cli.js");
|
|
9
9
|
const picocolors_1 = __importDefault(require("picocolors"));
|
|
10
|
-
const version = `0.1.2`;
|
|
11
10
|
commander_1.program
|
|
12
11
|
.name("zorm")
|
|
13
12
|
.description("ZuzORM is a lightweight ORM wrapper around TypeORM with support for MySQL.");
|
|
@@ -16,7 +15,8 @@ commander_1.program
|
|
|
16
15
|
.option(`-v, --version`)
|
|
17
16
|
.description("Displays current version of ZuzORM")
|
|
18
17
|
.action(() => {
|
|
19
|
-
|
|
18
|
+
const packageJson = require("../package.json");
|
|
19
|
+
console.log(picocolors_1.default.cyan(`ZuzORM v${packageJson.version}`));
|
|
20
20
|
process.exit(1);
|
|
21
21
|
});
|
|
22
22
|
commander_1.program
|
package/dist/core/cli.js
CHANGED
|
@@ -43,7 +43,7 @@ const picocolors_1 = __importDefault(require("picocolors"));
|
|
|
43
43
|
const index_js_1 = require("./index.js");
|
|
44
44
|
const mysql_1 = require("../drivers/mysql");
|
|
45
45
|
const buildEntities = (connection, dist) => {
|
|
46
|
-
dist = dist || path_1.default.join(
|
|
46
|
+
dist = dist || path_1.default.join(`src`, `zorm`);
|
|
47
47
|
const _checkDist = (0, index_js_1.checkDirectory)(path_1.default.join(process.cwd(), dist), true);
|
|
48
48
|
if (!_checkDist.access) {
|
|
49
49
|
console.log(picocolors_1.default.red(`○ ${_checkDist.message}`));
|
|
@@ -148,10 +148,11 @@ class MySqlDriver {
|
|
|
148
148
|
`* AutoGenerated by @zuzjs/orm.`,
|
|
149
149
|
`* @ ${new Date().toString().split(` GMT`)[0].trim()}`,
|
|
150
150
|
`*/`,
|
|
151
|
-
`import { Entity, PrimaryColumn, PrimaryGeneratedColumn, Column, BaseEntity } from "
|
|
151
|
+
`import { Entity, PrimaryColumn, PrimaryGeneratedColumn, Column, BaseEntity } from "@zuzjs/orm";\n`,
|
|
152
152
|
`@Entity({ name: "${tableName}" })`,
|
|
153
153
|
`export class ${(0, index_js_1.toPascalCase)(tableName)} extends BaseEntity {\n`
|
|
154
154
|
];
|
|
155
|
+
let hasPrimary = false;
|
|
155
156
|
for (const column of columns) {
|
|
156
157
|
// const { Field, Type, Key } = column;
|
|
157
158
|
const { Field, Type, Key, Null, Default, Extra } = column;
|
|
@@ -159,6 +160,7 @@ class MySqlDriver {
|
|
|
159
160
|
// Handle primary key
|
|
160
161
|
if (Key === "PRI") {
|
|
161
162
|
entityCode.push(Extra.includes("auto_increment") ? `\t@PrimaryGeneratedColumn()` : `\t@PrimaryColumn()`);
|
|
163
|
+
hasPrimary = true;
|
|
162
164
|
}
|
|
163
165
|
else {
|
|
164
166
|
let columnDecorator = `\t@Column({ type: "${columnType}"`;
|
|
@@ -174,12 +176,14 @@ class MySqlDriver {
|
|
|
174
176
|
entityCode.push(`\t${Field}!: ${tsType};\n`);
|
|
175
177
|
}
|
|
176
178
|
entityCode.push(`}`);
|
|
179
|
+
if (!hasPrimary) {
|
|
180
|
+
console.log(picocolors_1.default.bgRed(picocolors_1.default.whiteBright(` WARNING `)), picocolors_1.default.yellow(`○ "${tableName}" does not have a primary column. Primary column is required.`));
|
|
181
|
+
}
|
|
177
182
|
// Write entity file
|
|
178
183
|
fs_1.default.writeFileSync(path_1.default.join(this.dist, `${tableName}.ts`), entityCode.join(`\n`));
|
|
179
184
|
}
|
|
180
185
|
await self.pool.end();
|
|
181
186
|
console.log(picocolors_1.default.green(`✓ ${tables.length} Tables Processed.`));
|
|
182
|
-
process.exit(1);
|
|
183
187
|
}
|
|
184
188
|
}
|
|
185
189
|
exports.MySqlDriver = MySqlDriver;
|
|
@@ -28,12 +28,13 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
28
28
|
* @returns The current instance of ZormQueryBuilder.
|
|
29
29
|
*/
|
|
30
30
|
select(fields: (keyof T)[]): this;
|
|
31
|
+
private applyCondition;
|
|
31
32
|
/**
|
|
32
33
|
* Adds a WHERE condition to the query.
|
|
33
34
|
* @param condition - The condition to be added.
|
|
34
35
|
* @returns The current instance of ZormQueryBuilder.
|
|
35
36
|
*/
|
|
36
|
-
where(condition:
|
|
37
|
+
where(condition: Partial<Record<keyof T, any>>): this;
|
|
37
38
|
/**
|
|
38
39
|
* Adds an OR condition to the query.
|
|
39
40
|
* @param condition - The condition to be added.
|
|
@@ -57,7 +57,12 @@ class ZormQueryBuilder extends Promise {
|
|
|
57
57
|
return this;
|
|
58
58
|
}
|
|
59
59
|
_delete() {
|
|
60
|
-
this.
|
|
60
|
+
if (this.whereCount > 0) {
|
|
61
|
+
this.queryBuilder = this.queryBuilder.delete();
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw (0, core_1.stackTrace)(`○ Delete must have at least one WHERE condition. You forgot to call .where({ condition: value })`);
|
|
65
|
+
}
|
|
61
66
|
return this;
|
|
62
67
|
}
|
|
63
68
|
_getRawQuery() {
|
|
@@ -82,13 +87,31 @@ class ZormQueryBuilder extends Promise {
|
|
|
82
87
|
.select(fields.map(field => `${this.entityAlias}.${String(field)}`));
|
|
83
88
|
return this;
|
|
84
89
|
}
|
|
90
|
+
applyCondition(qb, condition, type) {
|
|
91
|
+
Object.entries(condition).forEach(([key, value]) => {
|
|
92
|
+
if (typeof value === "string") {
|
|
93
|
+
const match = value.match(/^(!=|>=|<=|>|<|=)\s*(.+)$/); // Improved regex
|
|
94
|
+
if (match) {
|
|
95
|
+
const [, operator, rawValue] = match;
|
|
96
|
+
const sqlOperator = operator; // Directly use the matched operator
|
|
97
|
+
const paramKey = `${key}Param`; // Unique parameter name
|
|
98
|
+
const parsedValue = !isNaN(Number(rawValue)) ? Number(rawValue) : rawValue.trim(); // Convert to number if possible
|
|
99
|
+
qb[type](`${qb.alias}.${key} ${sqlOperator} :${paramKey}`, { [paramKey]: parsedValue });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Default case (normal equality condition)
|
|
104
|
+
qb[type](`${this.entityAlias}.${key} = :${key}`, { [key]: value });
|
|
105
|
+
});
|
|
106
|
+
}
|
|
85
107
|
/**
|
|
86
108
|
* Adds a WHERE condition to the query.
|
|
87
109
|
* @param condition - The condition to be added.
|
|
88
110
|
* @returns The current instance of ZormQueryBuilder.
|
|
89
111
|
*/
|
|
90
112
|
where(condition) {
|
|
91
|
-
this.queryBuilder
|
|
113
|
+
const qb = this.queryBuilder;
|
|
114
|
+
this.applyCondition(qb, condition, `andWhere`);
|
|
92
115
|
this.whereCount++;
|
|
93
116
|
return this;
|
|
94
117
|
}
|
|
@@ -98,7 +121,8 @@ class ZormQueryBuilder extends Promise {
|
|
|
98
121
|
* @returns The current instance of ZormQueryBuilder.
|
|
99
122
|
*/
|
|
100
123
|
or(condition) {
|
|
101
|
-
this.queryBuilder
|
|
124
|
+
const qb = this.queryBuilder;
|
|
125
|
+
this.applyCondition(qb, condition, `orWhere`);
|
|
102
126
|
return this;
|
|
103
127
|
}
|
|
104
128
|
/**
|
|
@@ -239,6 +263,13 @@ class ZormQueryBuilder extends Promise {
|
|
|
239
263
|
* @returns A promise that resolves with the query result.
|
|
240
264
|
*/
|
|
241
265
|
async execute() {
|
|
266
|
+
const removedMethods = {};
|
|
267
|
+
Object.keys(Object.prototype).forEach((method) => {
|
|
268
|
+
if (Object.prototype.hasOwnProperty.call(Object.prototype, method)) {
|
|
269
|
+
removedMethods[method] = Object.prototype[method];
|
|
270
|
+
delete Object.prototype[method];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
242
273
|
try {
|
|
243
274
|
switch (this.action) {
|
|
244
275
|
case "upsert":
|
|
@@ -254,21 +285,33 @@ class ZormQueryBuilder extends Promise {
|
|
|
254
285
|
};
|
|
255
286
|
case "update":
|
|
256
287
|
this._update();
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
288
|
+
const _updateQuery = this.queryBuilder;
|
|
289
|
+
const _update = await _updateQuery.execute();
|
|
290
|
+
const whereQuery = _updateQuery.getQuery().split("WHERE")[1]?.trim(); // Get the WHERE clause
|
|
291
|
+
const _get = this.repository
|
|
292
|
+
.createQueryBuilder(this.entityAlias)
|
|
293
|
+
.where(whereQuery, _updateQuery.getParameters()); // Use the same parameters
|
|
294
|
+
const _updated = await _get.getMany();
|
|
295
|
+
return {
|
|
296
|
+
updated: _update.affected ? _update.affected > 0 : false,
|
|
297
|
+
record: _updated[0],
|
|
298
|
+
records: _updated.length > 1 ? _updated : []
|
|
299
|
+
};
|
|
260
300
|
case "delete":
|
|
261
301
|
this._delete();
|
|
262
302
|
const _delete = await this.queryBuilder.execute();
|
|
263
|
-
return {
|
|
303
|
+
return {
|
|
304
|
+
deleted: _delete.affected ? _delete.affected > 0 : false,
|
|
305
|
+
count: _delete.affected || 0
|
|
306
|
+
};
|
|
264
307
|
case "select":
|
|
265
308
|
default:
|
|
266
309
|
const _select = await this.queryBuilder.getMany();
|
|
267
310
|
return {
|
|
268
|
-
hasRows:
|
|
311
|
+
hasRows: _select.length > 0,
|
|
269
312
|
count: _select.length,
|
|
270
|
-
row: _select[0],
|
|
271
|
-
rows: _select
|
|
313
|
+
row: _select.length > 0 ? _select[0] : null,
|
|
314
|
+
rows: _select.length > 1 ? _select : []
|
|
272
315
|
};
|
|
273
316
|
}
|
|
274
317
|
}
|
|
@@ -301,6 +344,11 @@ class ZormQueryBuilder extends Promise {
|
|
|
301
344
|
return this.usePromise ? Promise.reject(_s) : _s;
|
|
302
345
|
}
|
|
303
346
|
}
|
|
347
|
+
finally {
|
|
348
|
+
Object.entries(removedMethods).forEach(([method, fn]) => {
|
|
349
|
+
Object.prototype[method] = fn;
|
|
350
|
+
});
|
|
351
|
+
}
|
|
304
352
|
}
|
|
305
353
|
/**
|
|
306
354
|
* Handles the fulfillment and rejection of the promise.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EntitySchema, EntityTarget, MixedList, ObjectLiteral, Repository } from "typeorm";
|
|
2
2
|
import ZormQueryBuilder from "./drivers/queryBuilder.js";
|
|
3
|
-
import { InsertQueryResult, SelectQueryResult, UpdateQueryResult } from "./types.js";
|
|
3
|
+
import { DeleteQueryResult, InsertQueryResult, SelectQueryResult, UpdateQueryResult } from "./types.js";
|
|
4
|
+
import "reflect-metadata";
|
|
4
5
|
/**
|
|
5
6
|
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
6
7
|
*/
|
|
@@ -84,6 +85,7 @@ declare class Zorm {
|
|
|
84
85
|
* @param {EntityTarget<T>} entity - The entity target.
|
|
85
86
|
* @returns {ZormQueryBuilder<T, DeleteQueryResult>} The query builder instance.
|
|
86
87
|
*/
|
|
87
|
-
delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T,
|
|
88
|
+
delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, DeleteQueryResult>;
|
|
88
89
|
}
|
|
89
90
|
export default Zorm;
|
|
91
|
+
export * from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
18
|
};
|
|
@@ -9,6 +23,7 @@ const index_js_1 = require("./core/index.js");
|
|
|
9
23
|
const picocolors_1 = __importDefault(require("picocolors"));
|
|
10
24
|
const index_js_2 = require("./drivers/mysql/index.js");
|
|
11
25
|
const queryBuilder_js_1 = __importDefault(require("./drivers/queryBuilder.js"));
|
|
26
|
+
require("reflect-metadata");
|
|
12
27
|
/**
|
|
13
28
|
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
14
29
|
*/
|
|
@@ -165,3 +180,4 @@ class Zorm {
|
|
|
165
180
|
}
|
|
166
181
|
}
|
|
167
182
|
exports.default = Zorm;
|
|
183
|
+
__exportStar(require("./types.js"), exports);
|
package/dist/types.d.ts
CHANGED
|
@@ -39,6 +39,8 @@ export type SelectQueryResult = {
|
|
|
39
39
|
};
|
|
40
40
|
export type UpdateQueryResult = {
|
|
41
41
|
updated: boolean;
|
|
42
|
+
record?: ObjectLiteral;
|
|
43
|
+
records?: ObjectLiteral[];
|
|
42
44
|
error?: QueryError;
|
|
43
45
|
};
|
|
44
46
|
export type DeleteQueryResult = {
|
|
@@ -46,3 +48,4 @@ export type DeleteQueryResult = {
|
|
|
46
48
|
count: number;
|
|
47
49
|
error?: QueryError;
|
|
48
50
|
};
|
|
51
|
+
export { Entity, PrimaryColumn, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseEntity = exports.Column = exports.PrimaryGeneratedColumn = exports.PrimaryColumn = exports.Entity = void 0;
|
|
4
|
+
var typeorm_1 = require("typeorm");
|
|
5
|
+
Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return typeorm_1.Entity; } });
|
|
6
|
+
Object.defineProperty(exports, "PrimaryColumn", { enumerable: true, get: function () { return typeorm_1.PrimaryColumn; } });
|
|
7
|
+
Object.defineProperty(exports, "PrimaryGeneratedColumn", { enumerable: true, get: function () { return typeorm_1.PrimaryGeneratedColumn; } });
|
|
8
|
+
Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return typeorm_1.Column; } });
|
|
9
|
+
Object.defineProperty(exports, "BaseEntity", { enumerable: true, get: function () { return typeorm_1.BaseEntity; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zuzjs/orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"orm",
|
|
6
6
|
"zuz",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"engines": {
|
|
29
29
|
"node": ">=18.17.0"
|
|
30
30
|
},
|
|
31
|
+
"sideEffects": ["reflect-metadata"],
|
|
31
32
|
"dependencies": {
|
|
32
33
|
"@types/md5": "^2.3.5",
|
|
33
34
|
"commander": "^13.1.0",
|