@zuzjs/orm 0.1.7 → 0.1.9
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/drivers/mysql/index.js +2 -3
- package/dist/drivers/queryBuilder.d.ts +4 -0
- package/dist/drivers/queryBuilder.js +18 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/types.d.ts +2 -1
- package/dist/types.js +5 -5
- package/package.json +1 -1
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.MySqlDriver = exports.MySQLErrorMap = exports.parseConnectionString = void 0;
|
|
7
|
-
const promise_1 = __importDefault(require("mysql2/promise"));
|
|
8
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const promise_1 = __importDefault(require("mysql2/promise"));
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const picocolors_1 = __importDefault(require("picocolors"));
|
|
11
11
|
const index_js_1 = require("../../core/index.js");
|
|
@@ -152,7 +152,6 @@ class MySqlDriver {
|
|
|
152
152
|
const imports = [];
|
|
153
153
|
const _imports = [`Entity`, `BaseEntity`];
|
|
154
154
|
// Get table structure
|
|
155
|
-
// const [columns]: [any[], any] = await self.pool!.execute(`DESCRIBE ${tableName}`);
|
|
156
155
|
const [columns] = await this.pool.execute(`SELECT COLUMN_NAME as \`Field\`, COLUMN_TYPE as \`Type\`, COLUMN_KEY as \`Key\`, IS_NULLABLE as \`Null\`, COLUMN_DEFAULT as \`Default\`, EXTRA as \`Extra\`, COLUMN_COMMENT as \`Comment\`
|
|
157
156
|
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION ASC`, [this.conn.database, tableName]);
|
|
158
157
|
const entityCode = [];
|
|
@@ -187,7 +186,7 @@ class MySqlDriver {
|
|
|
187
186
|
if (Comment && Comment.length > 0) {
|
|
188
187
|
entityCode.push(`\t/** @comment ${Comment} */`);
|
|
189
188
|
}
|
|
190
|
-
entityCode.push(`\t${Field}!: ${tsType};\n`);
|
|
189
|
+
entityCode.push(`\t${Field}!: ${Key == `PRI` && [`int`, `bigint`].includes(Type) ? `number` : tsType};\n`);
|
|
191
190
|
}
|
|
192
191
|
// Add foreign key relationships
|
|
193
192
|
if (foreignKeys[tableName]) {
|
|
@@ -10,12 +10,16 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
10
10
|
private queryValues;
|
|
11
11
|
private usePromise;
|
|
12
12
|
private whereCount;
|
|
13
|
+
private isActiveRecord;
|
|
14
|
+
private activeRecord;
|
|
13
15
|
constructor(repository: Repository<T>, _action: QueryAction, _usePromise?: boolean);
|
|
14
16
|
_create(): this;
|
|
15
17
|
upsert(): this;
|
|
16
18
|
_update(): this;
|
|
17
19
|
_delete(): this;
|
|
18
20
|
_getRawQuery(): [string, any[]];
|
|
21
|
+
active(): this;
|
|
22
|
+
_saveActiveRecord(activeRecord: T): Promise<T | null>;
|
|
19
23
|
/**
|
|
20
24
|
* Sets the values for an insert or update query.
|
|
21
25
|
* @param data - The data to be inserted or updated.
|
|
@@ -10,6 +10,8 @@ class ZormQueryBuilder extends Promise {
|
|
|
10
10
|
queryValues = null;
|
|
11
11
|
usePromise;
|
|
12
12
|
whereCount = 0;
|
|
13
|
+
isActiveRecord = false;
|
|
14
|
+
activeRecord;
|
|
13
15
|
constructor(repository, _action, _usePromise) {
|
|
14
16
|
super(() => { }); // Required for extending Promise
|
|
15
17
|
this.repository = repository;
|
|
@@ -17,6 +19,7 @@ class ZormQueryBuilder extends Promise {
|
|
|
17
19
|
this.queryBuilder = repository.createQueryBuilder(this.entityAlias);
|
|
18
20
|
this.action = _action;
|
|
19
21
|
this.usePromise = _usePromise || false;
|
|
22
|
+
this.activeRecord = null;
|
|
20
23
|
}
|
|
21
24
|
_create() {
|
|
22
25
|
if (this.queryValues) {
|
|
@@ -68,6 +71,15 @@ class ZormQueryBuilder extends Promise {
|
|
|
68
71
|
_getRawQuery() {
|
|
69
72
|
return this.queryBuilder.getQueryAndParameters();
|
|
70
73
|
}
|
|
74
|
+
active() {
|
|
75
|
+
this.isActiveRecord = true;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
async _saveActiveRecord(activeRecord) {
|
|
79
|
+
// if (!this.activeRecord) throw new Error("No active record found. Use `findOne` first.");
|
|
80
|
+
return this.repository.save(activeRecord);
|
|
81
|
+
// return this
|
|
82
|
+
}
|
|
71
83
|
/**
|
|
72
84
|
* Sets the values for an insert or update query.
|
|
73
85
|
* @param data - The data to be inserted or updated.
|
|
@@ -316,12 +328,16 @@ class ZormQueryBuilder extends Promise {
|
|
|
316
328
|
case "select":
|
|
317
329
|
default:
|
|
318
330
|
const _select = await this.queryBuilder.getMany();
|
|
319
|
-
|
|
331
|
+
const _result = {
|
|
320
332
|
hasRows: _select.length > 0,
|
|
321
333
|
count: _select.length,
|
|
322
334
|
row: _select.length > 0 ? _select[0] : null,
|
|
323
|
-
rows: _select.length > 1 ? _select : []
|
|
335
|
+
rows: _select.length > 1 ? _select : [],
|
|
324
336
|
};
|
|
337
|
+
if (this.isActiveRecord) {
|
|
338
|
+
_result.save = () => this._saveActiveRecord(_select[0]);
|
|
339
|
+
}
|
|
340
|
+
return _result;
|
|
325
341
|
}
|
|
326
342
|
}
|
|
327
343
|
catch (err) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
1
2
|
import { EntitySchema, EntityTarget, MixedList, ObjectLiteral, Repository } from "typeorm";
|
|
2
3
|
import ZormQueryBuilder from "./drivers/queryBuilder.js";
|
|
3
4
|
import { DeleteQueryResult, InsertQueryResult, SelectQueryResult, UpdateQueryResult } from "./types.js";
|
|
4
|
-
import "reflect-metadata";
|
|
5
5
|
/**
|
|
6
6
|
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
7
7
|
*/
|
package/dist/index.js
CHANGED
|
@@ -18,12 +18,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
20
|
const path_1 = __importDefault(require("path"));
|
|
21
|
+
const picocolors_1 = __importDefault(require("picocolors"));
|
|
22
|
+
require("reflect-metadata");
|
|
21
23
|
const typeorm_1 = require("typeorm");
|
|
22
24
|
const index_js_1 = require("./core/index.js");
|
|
23
|
-
const picocolors_1 = __importDefault(require("picocolors"));
|
|
24
25
|
const index_js_2 = require("./drivers/mysql/index.js");
|
|
25
26
|
const queryBuilder_js_1 = __importDefault(require("./drivers/queryBuilder.js"));
|
|
26
|
-
require("reflect-metadata");
|
|
27
27
|
/**
|
|
28
28
|
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
29
29
|
*/
|
package/dist/types.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export type SelectQueryResult = {
|
|
|
36
36
|
row?: any;
|
|
37
37
|
rows?: any[];
|
|
38
38
|
error?: QueryError;
|
|
39
|
+
save?: () => void;
|
|
39
40
|
};
|
|
40
41
|
export type UpdateQueryResult = {
|
|
41
42
|
updated: boolean;
|
|
@@ -48,4 +49,4 @@ export type DeleteQueryResult = {
|
|
|
48
49
|
count: number;
|
|
49
50
|
error?: QueryError;
|
|
50
51
|
};
|
|
51
|
-
export {
|
|
52
|
+
export { BaseEntity, Column, Entity, JoinColumn, OneToOne, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
package/dist/types.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PrimaryGeneratedColumn = exports.PrimaryColumn = exports.OneToOne = exports.JoinColumn = exports.Entity = exports.Column = exports.BaseEntity = void 0;
|
|
4
4
|
var typeorm_1 = require("typeorm");
|
|
5
|
+
Object.defineProperty(exports, "BaseEntity", { enumerable: true, get: function () { return typeorm_1.BaseEntity; } });
|
|
6
|
+
Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return typeorm_1.Column; } });
|
|
5
7
|
Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return typeorm_1.Entity; } });
|
|
8
|
+
Object.defineProperty(exports, "JoinColumn", { enumerable: true, get: function () { return typeorm_1.JoinColumn; } });
|
|
9
|
+
Object.defineProperty(exports, "OneToOne", { enumerable: true, get: function () { return typeorm_1.OneToOne; } });
|
|
6
10
|
Object.defineProperty(exports, "PrimaryColumn", { enumerable: true, get: function () { return typeorm_1.PrimaryColumn; } });
|
|
7
11
|
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; } });
|
|
10
|
-
Object.defineProperty(exports, "OneToOne", { enumerable: true, get: function () { return typeorm_1.OneToOne; } });
|
|
11
|
-
Object.defineProperty(exports, "JoinColumn", { enumerable: true, get: function () { return typeorm_1.JoinColumn; } });
|