@zuzjs/orm 0.1.2 → 0.1.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/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 +1 -1
- package/dist/drivers/queryBuilder.js +43 -6
- package/dist/index.d.ts +2 -0
- 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;
|
|
@@ -33,7 +33,7 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
33
33
|
* @param condition - The condition to be added.
|
|
34
34
|
* @returns The current instance of ZormQueryBuilder.
|
|
35
35
|
*/
|
|
36
|
-
where(condition:
|
|
36
|
+
where(condition: Partial<Record<keyof T, any>>): this;
|
|
37
37
|
/**
|
|
38
38
|
* Adds an OR condition to the query.
|
|
39
39
|
* @param condition - The condition to be added.
|
|
@@ -88,10 +88,26 @@ class ZormQueryBuilder extends Promise {
|
|
|
88
88
|
* @returns The current instance of ZormQueryBuilder.
|
|
89
89
|
*/
|
|
90
90
|
where(condition) {
|
|
91
|
-
this.queryBuilder
|
|
91
|
+
const qb = this.queryBuilder;
|
|
92
|
+
Object.entries(condition).forEach(([key, value]) => {
|
|
93
|
+
if (typeof value === "string" && value.startsWith("!")) {
|
|
94
|
+
qb.andWhere(`${this.entityAlias}.${key} != :${key}`, { [key]: value.slice(1) });
|
|
95
|
+
}
|
|
96
|
+
else if (typeof value === "boolean" || typeof value === "number") {
|
|
97
|
+
qb.andWhere(`${this.entityAlias}.${key} = :${key}`, { [key]: value });
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
qb.andWhere(`${this.entityAlias}.${key} = :${key}`, { [key]: value });
|
|
101
|
+
}
|
|
102
|
+
});
|
|
92
103
|
this.whereCount++;
|
|
93
104
|
return this;
|
|
94
105
|
}
|
|
106
|
+
// where(condition: FindOptionsWhere<T>): this {
|
|
107
|
+
// (this.queryBuilder as SelectQueryBuilder<T> | UpdateQueryBuilder<T>).where(condition);
|
|
108
|
+
// this.whereCount++
|
|
109
|
+
// return this;
|
|
110
|
+
// }
|
|
95
111
|
/**
|
|
96
112
|
* Adds an OR condition to the query.
|
|
97
113
|
* @param condition - The condition to be added.
|
|
@@ -239,6 +255,13 @@ class ZormQueryBuilder extends Promise {
|
|
|
239
255
|
* @returns A promise that resolves with the query result.
|
|
240
256
|
*/
|
|
241
257
|
async execute() {
|
|
258
|
+
const removedMethods = {};
|
|
259
|
+
Object.keys(Object.prototype).forEach((method) => {
|
|
260
|
+
if (Object.prototype.hasOwnProperty.call(Object.prototype, method)) {
|
|
261
|
+
removedMethods[method] = Object.prototype[method];
|
|
262
|
+
delete Object.prototype[method];
|
|
263
|
+
}
|
|
264
|
+
});
|
|
242
265
|
try {
|
|
243
266
|
switch (this.action) {
|
|
244
267
|
case "upsert":
|
|
@@ -254,9 +277,18 @@ class ZormQueryBuilder extends Promise {
|
|
|
254
277
|
};
|
|
255
278
|
case "update":
|
|
256
279
|
this._update();
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
280
|
+
const _updateQuery = this.queryBuilder;
|
|
281
|
+
const _update = await _updateQuery.execute();
|
|
282
|
+
const whereQuery = _updateQuery.getQuery().split("WHERE")[1]?.trim(); // Get the WHERE clause
|
|
283
|
+
const _get = this.repository
|
|
284
|
+
.createQueryBuilder(this.entityAlias)
|
|
285
|
+
.where(whereQuery, _updateQuery.getParameters()); // Use the same parameters
|
|
286
|
+
const _updated = await _get.getMany();
|
|
287
|
+
return {
|
|
288
|
+
updated: true,
|
|
289
|
+
record: _updated[0],
|
|
290
|
+
records: _updated.length > 1 ? _updated : null
|
|
291
|
+
};
|
|
260
292
|
case "delete":
|
|
261
293
|
this._delete();
|
|
262
294
|
const _delete = await this.queryBuilder.execute();
|
|
@@ -265,9 +297,9 @@ class ZormQueryBuilder extends Promise {
|
|
|
265
297
|
default:
|
|
266
298
|
const _select = await this.queryBuilder.getMany();
|
|
267
299
|
return {
|
|
268
|
-
hasRows:
|
|
300
|
+
hasRows: _select.length > 0,
|
|
269
301
|
count: _select.length,
|
|
270
|
-
row: _select[0],
|
|
302
|
+
row: _select.length > 0 ? _select[0] : null,
|
|
271
303
|
rows: _select
|
|
272
304
|
};
|
|
273
305
|
}
|
|
@@ -301,6 +333,11 @@ class ZormQueryBuilder extends Promise {
|
|
|
301
333
|
return this.usePromise ? Promise.reject(_s) : _s;
|
|
302
334
|
}
|
|
303
335
|
}
|
|
336
|
+
finally {
|
|
337
|
+
Object.entries(removedMethods).forEach(([method, fn]) => {
|
|
338
|
+
Object.prototype[method] = fn;
|
|
339
|
+
});
|
|
340
|
+
}
|
|
304
341
|
}
|
|
305
342
|
/**
|
|
306
343
|
* 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
3
|
import { 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
|
*/
|
|
@@ -87,3 +88,4 @@ declare class Zorm {
|
|
|
87
88
|
delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, UpdateQueryResult>;
|
|
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.3",
|
|
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",
|