@neogroup/neorm 0.0.1
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 +1019 -0
- package/dist/connection.d.ts +10 -0
- package/dist/connection.js +3 -0
- package/dist/connection.js.map +1 -0
- package/dist/data-connection.d.ts +24 -0
- package/dist/data-connection.js +99 -0
- package/dist/data-connection.js.map +1 -0
- package/dist/data-set.d.ts +3 -0
- package/dist/data-set.js +3 -0
- package/dist/data-set.js.map +1 -0
- package/dist/data-source.d.ts +23 -0
- package/dist/data-source.js +56 -0
- package/dist/data-source.js.map +1 -0
- package/dist/data-table.d.ts +19 -0
- package/dist/data-table.js +75 -0
- package/dist/data-table.js.map +1 -0
- package/dist/db.d.ts +57 -0
- package/dist/db.js +213 -0
- package/dist/db.js.map +1 -0
- package/dist/entities/decorators.d.ts +50 -0
- package/dist/entities/decorators.js +305 -0
- package/dist/entities/decorators.js.map +1 -0
- package/dist/entities/entity-query.d.ts +60 -0
- package/dist/entities/entity-query.js +305 -0
- package/dist/entities/entity-query.js.map +1 -0
- package/dist/entities/entity.d.ts +1 -0
- package/dist/entities/entity.js +3 -0
- package/dist/entities/entity.js.map +1 -0
- package/dist/entities/index.d.ts +4 -0
- package/dist/entities/index.js +20 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/entities/relationship.d.ts +15 -0
- package/dist/entities/relationship.js +24 -0
- package/dist/entities/relationship.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/query/builders/default-query-builder.d.ts +87 -0
- package/dist/query/builders/default-query-builder.js +524 -0
- package/dist/query/builders/default-query-builder.js.map +1 -0
- package/dist/query/builders/index.d.ts +2 -0
- package/dist/query/builders/index.js +19 -0
- package/dist/query/builders/index.js.map +1 -0
- package/dist/query/builders/query-builder.d.ts +5 -0
- package/dist/query/builders/query-builder.js +7 -0
- package/dist/query/builders/query-builder.js.map +1 -0
- package/dist/query/conditions.d.ts +60 -0
- package/dist/query/conditions.js +142 -0
- package/dist/query/conditions.js.map +1 -0
- package/dist/query/delete-query.d.ts +8 -0
- package/dist/query/delete-query.js +17 -0
- package/dist/query/delete-query.js.map +1 -0
- package/dist/query/features/has-alias.d.ts +6 -0
- package/dist/query/features/has-alias.js +17 -0
- package/dist/query/features/has-alias.js.map +1 -0
- package/dist/query/features/has-distinct.d.ts +6 -0
- package/dist/query/features/has-distinct.js +20 -0
- package/dist/query/features/has-distinct.js.map +1 -0
- package/dist/query/features/has-field-values.d.ts +9 -0
- package/dist/query/features/has-field-values.js +27 -0
- package/dist/query/features/has-field-values.js.map +1 -0
- package/dist/query/features/has-group-by-fields.d.ts +7 -0
- package/dist/query/features/has-group-by-fields.js +21 -0
- package/dist/query/features/has-group-by-fields.js.map +1 -0
- package/dist/query/features/has-having-conditions.d.ts +13 -0
- package/dist/query/features/has-having-conditions.js +28 -0
- package/dist/query/features/has-having-conditions.js.map +1 -0
- package/dist/query/features/has-joins.d.ts +41 -0
- package/dist/query/features/has-joins.js +67 -0
- package/dist/query/features/has-joins.js.map +1 -0
- package/dist/query/features/has-limit.d.ts +6 -0
- package/dist/query/features/has-limit.js +20 -0
- package/dist/query/features/has-limit.js.map +1 -0
- package/dist/query/features/has-offset.d.ts +6 -0
- package/dist/query/features/has-offset.js +20 -0
- package/dist/query/features/has-offset.js.map +1 -0
- package/dist/query/features/has-order-by-fields.d.ts +15 -0
- package/dist/query/features/has-order-by-fields.js +38 -0
- package/dist/query/features/has-order-by-fields.js.map +1 -0
- package/dist/query/features/has-select-fields.d.ts +11 -0
- package/dist/query/features/has-select-fields.js +21 -0
- package/dist/query/features/has-select-fields.js.map +1 -0
- package/dist/query/features/has-table.d.ts +7 -0
- package/dist/query/features/has-table.js +20 -0
- package/dist/query/features/has-table.js.map +1 -0
- package/dist/query/features/has-unions.d.ts +13 -0
- package/dist/query/features/has-unions.js +21 -0
- package/dist/query/features/has-unions.js.map +1 -0
- package/dist/query/features/has-when.d.ts +3 -0
- package/dist/query/features/has-when.js +13 -0
- package/dist/query/features/has-when.js.map +1 -0
- package/dist/query/features/has-where-conditions.d.ts +35 -0
- package/dist/query/features/has-where-conditions.js +92 -0
- package/dist/query/features/has-where-conditions.js.map +1 -0
- package/dist/query/features/index.d.ts +14 -0
- package/dist/query/features/index.js +31 -0
- package/dist/query/features/index.js.map +1 -0
- package/dist/query/fields.d.ts +12 -0
- package/dist/query/fields.js +18 -0
- package/dist/query/fields.js.map +1 -0
- package/dist/query/index.d.ts +11 -0
- package/dist/query/index.js +28 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/insert-query.d.ts +8 -0
- package/dist/query/insert-query.js +17 -0
- package/dist/query/insert-query.js.map +1 -0
- package/dist/query/query.d.ts +2 -0
- package/dist/query/query.js +7 -0
- package/dist/query/query.js.map +1 -0
- package/dist/query/select-query.d.ts +9 -0
- package/dist/query/select-query.js +33 -0
- package/dist/query/select-query.js.map +1 -0
- package/dist/query/statement.d.ts +4 -0
- package/dist/query/statement.js +3 -0
- package/dist/query/statement.js.map +1 -0
- package/dist/query/table.d.ts +6 -0
- package/dist/query/table.js +3 -0
- package/dist/query/table.js.map +1 -0
- package/dist/query/update-query.d.ts +8 -0
- package/dist/query/update-query.js +17 -0
- package/dist/query/update-query.js.map +1 -0
- package/dist/sources/mysql/index.d.ts +3 -0
- package/dist/sources/mysql/index.js +20 -0
- package/dist/sources/mysql/index.js.map +1 -0
- package/dist/sources/mysql/mysql-connection.d.ts +14 -0
- package/dist/sources/mysql/mysql-connection.js +36 -0
- package/dist/sources/mysql/mysql-connection.js.map +1 -0
- package/dist/sources/mysql/mysql-data-source.d.ts +24 -0
- package/dist/sources/mysql/mysql-data-source.js +72 -0
- package/dist/sources/mysql/mysql-data-source.js.map +1 -0
- package/dist/sources/mysql/mysql-query-builder.d.ts +8 -0
- package/dist/sources/mysql/mysql-query-builder.js +28 -0
- package/dist/sources/mysql/mysql-query-builder.js.map +1 -0
- package/dist/sources/postgres/index.d.ts +2 -0
- package/dist/sources/postgres/index.js +19 -0
- package/dist/sources/postgres/index.js.map +1 -0
- package/dist/sources/postgres/postgres-connection.d.ts +13 -0
- package/dist/sources/postgres/postgres-connection.js +40 -0
- package/dist/sources/postgres/postgres-connection.js.map +1 -0
- package/dist/sources/postgres/postgres-data-source.d.ts +24 -0
- package/dist/sources/postgres/postgres-data-source.js +73 -0
- package/dist/sources/postgres/postgres-data-source.js.map +1 -0
- package/dist/sources/postgres/postgres-query-builder.d.ts +5 -0
- package/dist/sources/postgres/postgres-query-builder.js +13 -0
- package/dist/sources/postgres/postgres-query-builder.js.map +1 -0
- package/dist/sources/sqlite/index.d.ts +3 -0
- package/dist/sources/sqlite/index.js +20 -0
- package/dist/sources/sqlite/index.js.map +1 -0
- package/dist/sources/sqlite/sqlite-connection.d.ts +14 -0
- package/dist/sources/sqlite/sqlite-connection.js +37 -0
- package/dist/sources/sqlite/sqlite-connection.js.map +1 -0
- package/dist/sources/sqlite/sqlite-data-source.d.ts +11 -0
- package/dist/sources/sqlite/sqlite-data-source.js +34 -0
- package/dist/sources/sqlite/sqlite-data-source.js.map +1 -0
- package/dist/sources/sqlite/sqlite-query-builder.d.ts +6 -0
- package/dist/sources/sqlite/sqlite-query-builder.js +23 -0
- package/dist/sources/sqlite/sqlite-query-builder.js.map +1 -0
- package/dist/utilities.d.ts +2 -0
- package/dist/utilities.js +17 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +56 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DataSource } from '../data-source';
|
|
2
|
+
import { CastType } from './entity';
|
|
3
|
+
export interface ColumnOptions {
|
|
4
|
+
/** Automatic type coercion when reading from the database. */
|
|
5
|
+
cast?: CastType;
|
|
6
|
+
/** Mark this column as the primary key. */
|
|
7
|
+
primaryKey?: boolean;
|
|
8
|
+
/** Column value is generated by the database (e.g. AUTOINCREMENT). Excluded from INSERT/UPDATE. */
|
|
9
|
+
autoGenerated?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** Map a class property to a database column. */
|
|
12
|
+
export declare function Column(options?: ColumnOptions): PropertyDecorator;
|
|
13
|
+
/** One-to-one: this model's primary key appears as foreignKey on the related model. */
|
|
14
|
+
export declare function HasOne(related: () => any, foreignKey: string, localKey?: string): PropertyDecorator;
|
|
15
|
+
/** One-to-many: this model's primary key appears as foreignKey on the related model. */
|
|
16
|
+
export declare function HasMany(related: () => any, foreignKey: string, localKey?: string): PropertyDecorator;
|
|
17
|
+
/** Inverse of HasOne/HasMany: this model holds foreignKey that points to the related model. */
|
|
18
|
+
export declare function BelongsTo(related: () => any, foreignKey: string, localKey?: string): PropertyDecorator;
|
|
19
|
+
/** Has-one through an intermediate model. */
|
|
20
|
+
export declare function HasOneThrough(related: () => any, through: () => any, foreignKey: string, throughForeignKey: string, localKey?: string, throughLocalKey?: string): PropertyDecorator;
|
|
21
|
+
/** Has-many through an intermediate model. */
|
|
22
|
+
export declare function HasManyThrough(related: () => any, through: () => any, foreignKey: string, throughForeignKey: string, localKey?: string, throughLocalKey?: string): PropertyDecorator;
|
|
23
|
+
export interface EntityOptions {
|
|
24
|
+
/** Database table name. Defaults to lowercase class name + 's'. */
|
|
25
|
+
table?: string;
|
|
26
|
+
/** Primary key column name. Defaults to 'id'. */
|
|
27
|
+
primaryKey?: string;
|
|
28
|
+
/** DataSource to use. Defaults to DB.getActiveSource(). */
|
|
29
|
+
source?: DataSource | null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Marks a class as an Active Record entity.
|
|
33
|
+
* Injects static query methods (find, where, get, with, …) — no base class required.
|
|
34
|
+
* Use saveEntity() and deleteEntity() for persistence.
|
|
35
|
+
*/
|
|
36
|
+
export declare function Entity(options?: EntityOptions): <T extends new (...args: any[]) => {}>(BaseClass: T) => T;
|
|
37
|
+
export declare class Entities {
|
|
38
|
+
/**
|
|
39
|
+
* Persists an entity instance to the database.
|
|
40
|
+
* - If the primary key is set → UPDATE (auto-generated columns are excluded from SET).
|
|
41
|
+
* - If the primary key is null/undefined → INSERT (auto-generated columns are excluded;
|
|
42
|
+
* the generated PK is written back to the instance).
|
|
43
|
+
*/
|
|
44
|
+
static save(entity: object): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Deletes an entity instance from the database by its primary key.
|
|
47
|
+
* Throws if the primary key is not set.
|
|
48
|
+
*/
|
|
49
|
+
static delete(entity: object): Promise<void>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Entities = exports.Entity = exports.HasManyThrough = exports.HasOneThrough = exports.BelongsTo = exports.HasMany = exports.HasOne = exports.Column = void 0;
|
|
4
|
+
const db_1 = require("../db");
|
|
5
|
+
const query_1 = require("../query");
|
|
6
|
+
const entity_query_1 = require("./entity-query");
|
|
7
|
+
const relationship_1 = require("./relationship");
|
|
8
|
+
const registry = new WeakMap();
|
|
9
|
+
function getOrCreate(proto) {
|
|
10
|
+
if (!registry.has(proto)) {
|
|
11
|
+
registry.set(proto, { columns: [], relationships: [] });
|
|
12
|
+
}
|
|
13
|
+
return registry.get(proto);
|
|
14
|
+
}
|
|
15
|
+
// ── Cast helpers ──────────────────────────────────────────────────────────────
|
|
16
|
+
function applyCast(value, type) {
|
|
17
|
+
if (value == null) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
switch (type) {
|
|
21
|
+
case 'number':
|
|
22
|
+
return Number(value);
|
|
23
|
+
case 'boolean':
|
|
24
|
+
return value === true || value === 1 || value === '1' || value === 'true';
|
|
25
|
+
case 'string':
|
|
26
|
+
return String(value);
|
|
27
|
+
case 'json':
|
|
28
|
+
return typeof value === 'string' ? JSON.parse(value) : value;
|
|
29
|
+
case 'date':
|
|
30
|
+
return value instanceof Date ? value : new Date(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function applyCastForStorage(value, type) {
|
|
34
|
+
if (value == null) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
switch (type) {
|
|
38
|
+
case 'boolean':
|
|
39
|
+
return value ? 1 : 0;
|
|
40
|
+
case 'json':
|
|
41
|
+
return typeof value === 'string' ? value : JSON.stringify(value);
|
|
42
|
+
case 'date':
|
|
43
|
+
return value instanceof Date ? value.toISOString() : value;
|
|
44
|
+
default:
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Map a class property to a database column. */
|
|
49
|
+
function Column(options) {
|
|
50
|
+
return (target, propertyKey) => {
|
|
51
|
+
const m = getOrCreate(target);
|
|
52
|
+
m.columns.push(Object.assign({ name: String(propertyKey) }, options));
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
exports.Column = Column;
|
|
56
|
+
/** One-to-one: this model's primary key appears as foreignKey on the related model. */
|
|
57
|
+
function HasOne(related, foreignKey, localKey = 'id') {
|
|
58
|
+
return (target, propertyKey) => {
|
|
59
|
+
const m = getOrCreate(target);
|
|
60
|
+
m.relationships.push({ name: String(propertyKey), relationship: (0, relationship_1.hasOne)(related, foreignKey, localKey) });
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
exports.HasOne = HasOne;
|
|
64
|
+
/** One-to-many: this model's primary key appears as foreignKey on the related model. */
|
|
65
|
+
function HasMany(related, foreignKey, localKey = 'id') {
|
|
66
|
+
return (target, propertyKey) => {
|
|
67
|
+
const m = getOrCreate(target);
|
|
68
|
+
m.relationships.push({ name: String(propertyKey), relationship: (0, relationship_1.hasMany)(related, foreignKey, localKey) });
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
exports.HasMany = HasMany;
|
|
72
|
+
/** Inverse of HasOne/HasMany: this model holds foreignKey that points to the related model. */
|
|
73
|
+
function BelongsTo(related, foreignKey, localKey = 'id') {
|
|
74
|
+
return (target, propertyKey) => {
|
|
75
|
+
const m = getOrCreate(target);
|
|
76
|
+
m.relationships.push({ name: String(propertyKey), relationship: (0, relationship_1.belongsTo)(related, foreignKey, localKey) });
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
exports.BelongsTo = BelongsTo;
|
|
80
|
+
/** Has-one through an intermediate model. */
|
|
81
|
+
function HasOneThrough(related, through, foreignKey, throughForeignKey, localKey = 'id', throughLocalKey = 'id') {
|
|
82
|
+
return (target, propertyKey) => {
|
|
83
|
+
const m = getOrCreate(target);
|
|
84
|
+
m.relationships.push({
|
|
85
|
+
name: String(propertyKey),
|
|
86
|
+
relationship: (0, relationship_1.hasOneThrough)(related, through, foreignKey, throughForeignKey, localKey, throughLocalKey)
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exports.HasOneThrough = HasOneThrough;
|
|
91
|
+
/** Has-many through an intermediate model. */
|
|
92
|
+
function HasManyThrough(related, through, foreignKey, throughForeignKey, localKey = 'id', throughLocalKey = 'id') {
|
|
93
|
+
return (target, propertyKey) => {
|
|
94
|
+
const m = getOrCreate(target);
|
|
95
|
+
m.relationships.push({
|
|
96
|
+
name: String(propertyKey),
|
|
97
|
+
relationship: (0, relationship_1.hasManyThrough)(related, through, foreignKey, throughForeignKey, localKey, throughLocalKey)
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
exports.HasManyThrough = HasManyThrough;
|
|
102
|
+
/**
|
|
103
|
+
* Marks a class as an Active Record entity.
|
|
104
|
+
* Injects static query methods (find, where, get, with, …) — no base class required.
|
|
105
|
+
* Use saveEntity() and deleteEntity() for persistence.
|
|
106
|
+
*/
|
|
107
|
+
function Entity(options) {
|
|
108
|
+
return function (BaseClass) {
|
|
109
|
+
var _a, _b, _c, _d, _e, _f;
|
|
110
|
+
// Read metadata accumulated by property decorators on BaseClass.prototype.
|
|
111
|
+
const originalName = BaseClass.name;
|
|
112
|
+
const meta = (_a = registry.get(BaseClass.prototype)) !== null && _a !== void 0 ? _a : { columns: [], relationships: [] };
|
|
113
|
+
const tableName = (_b = options === null || options === void 0 ? void 0 : options.table) !== null && _b !== void 0 ? _b : originalName.toLowerCase() + 's';
|
|
114
|
+
const primaryKeyCol = (_e = (_d = (_c = meta.columns.find((c) => c.primaryKey)) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : options === null || options === void 0 ? void 0 : options.primaryKey) !== null && _e !== void 0 ? _e : 'id';
|
|
115
|
+
const fieldNames = meta.columns.map((c) => c.name);
|
|
116
|
+
const autoGeneratedFields = meta.columns.filter((c) => c.autoGenerated).map((c) => c.name);
|
|
117
|
+
const castsMap = {};
|
|
118
|
+
meta.columns.forEach((c) => {
|
|
119
|
+
if (c.cast) {
|
|
120
|
+
castsMap[c.name] = c.cast;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
const relsMap = {};
|
|
124
|
+
meta.relationships.forEach((r) => {
|
|
125
|
+
relsMap[r.name] = r.relationship;
|
|
126
|
+
});
|
|
127
|
+
const configuredSource = (_f = options === null || options === void 0 ? void 0 : options.source) !== null && _f !== void 0 ? _f : null;
|
|
128
|
+
// ── Closures shared by static methods ────────────────────────────────
|
|
129
|
+
function resolveSource() {
|
|
130
|
+
return configuredSource !== null && configuredSource !== void 0 ? configuredSource : db_1.DB.getActiveSource();
|
|
131
|
+
}
|
|
132
|
+
function hydrateRow(row) {
|
|
133
|
+
const instance = new Enhanced();
|
|
134
|
+
const cols = fieldNames.length > 0 ? fieldNames : Object.keys(row);
|
|
135
|
+
for (const col of cols) {
|
|
136
|
+
if (!(col in row)) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const cast = castsMap[col];
|
|
140
|
+
instance[col] = cast ? applyCast(row[col], cast) : row[col];
|
|
141
|
+
}
|
|
142
|
+
return instance;
|
|
143
|
+
}
|
|
144
|
+
function buildQuery() {
|
|
145
|
+
return new entity_query_1.EntityQuery(Enhanced, resolveSource().table(tableName));
|
|
146
|
+
}
|
|
147
|
+
// ── Enhanced class — wraps BaseClass and injects all methods ──────────
|
|
148
|
+
class Enhanced extends BaseClass {
|
|
149
|
+
static fromRow(row) {
|
|
150
|
+
return hydrateRow(row);
|
|
151
|
+
}
|
|
152
|
+
// ── Static query methods ───────────────────────────────────────────
|
|
153
|
+
static async find(id) {
|
|
154
|
+
return buildQuery().find(id);
|
|
155
|
+
}
|
|
156
|
+
static async get() {
|
|
157
|
+
return buildQuery().get();
|
|
158
|
+
}
|
|
159
|
+
static async first() {
|
|
160
|
+
return buildQuery().first();
|
|
161
|
+
}
|
|
162
|
+
static where(...args) {
|
|
163
|
+
return buildQuery().where(...args);
|
|
164
|
+
}
|
|
165
|
+
static whereIn(field, values) {
|
|
166
|
+
return buildQuery().whereIn(field, values);
|
|
167
|
+
}
|
|
168
|
+
static whereNotIn(field, values) {
|
|
169
|
+
return buildQuery().whereNotIn(field, values);
|
|
170
|
+
}
|
|
171
|
+
static whereBetween(field, range) {
|
|
172
|
+
return buildQuery().whereBetween(field, range);
|
|
173
|
+
}
|
|
174
|
+
static whereNotBetween(field, range) {
|
|
175
|
+
return buildQuery().whereNotBetween(field, range);
|
|
176
|
+
}
|
|
177
|
+
static whereNull(field) {
|
|
178
|
+
return buildQuery().whereNull(field);
|
|
179
|
+
}
|
|
180
|
+
static whereNotNull(field) {
|
|
181
|
+
return buildQuery().whereNotNull(field);
|
|
182
|
+
}
|
|
183
|
+
static whereLike(field, pattern) {
|
|
184
|
+
return buildQuery().whereLike(field, pattern);
|
|
185
|
+
}
|
|
186
|
+
static whereNotLike(field, pattern) {
|
|
187
|
+
return buildQuery().whereNotLike(field, pattern);
|
|
188
|
+
}
|
|
189
|
+
static whereColumn(...args) {
|
|
190
|
+
return buildQuery().whereColumn(...args);
|
|
191
|
+
}
|
|
192
|
+
static orderBy(field, direction) {
|
|
193
|
+
return buildQuery().orderBy(field, direction);
|
|
194
|
+
}
|
|
195
|
+
static limit(value) {
|
|
196
|
+
return buildQuery().limit(value);
|
|
197
|
+
}
|
|
198
|
+
static offset(value) {
|
|
199
|
+
return buildQuery().offset(value);
|
|
200
|
+
}
|
|
201
|
+
static select(...args) {
|
|
202
|
+
return buildQuery().select(...args);
|
|
203
|
+
}
|
|
204
|
+
static with(relations, ...rest) {
|
|
205
|
+
return buildQuery().with(relations, ...rest);
|
|
206
|
+
}
|
|
207
|
+
static joinRelationship(name) {
|
|
208
|
+
return buildQuery().joinRelationship(name);
|
|
209
|
+
}
|
|
210
|
+
static innerJoinRelationship(name) {
|
|
211
|
+
return buildQuery().innerJoinRelationship(name);
|
|
212
|
+
}
|
|
213
|
+
static leftJoinRelationship(name) {
|
|
214
|
+
return buildQuery().leftJoinRelationship(name);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// Static metadata (also used by EntityQuery internals)
|
|
218
|
+
Enhanced.table = tableName;
|
|
219
|
+
Enhanced.primaryKey = primaryKeyCol;
|
|
220
|
+
Enhanced.fields = fieldNames;
|
|
221
|
+
Enhanced.autoGeneratedFields = autoGeneratedFields;
|
|
222
|
+
Enhanced.casts = castsMap;
|
|
223
|
+
Enhanced.relationships = relsMap;
|
|
224
|
+
Enhanced.source = configuredSource;
|
|
225
|
+
// Preserve the original class name so default table names and stack traces stay clean.
|
|
226
|
+
Object.defineProperty(Enhanced, 'name', { value: originalName });
|
|
227
|
+
return Enhanced;
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
exports.Entity = Entity;
|
|
231
|
+
// ── Entities namespace ────────────────────────────────────────────────────────
|
|
232
|
+
class Entities {
|
|
233
|
+
/**
|
|
234
|
+
* Persists an entity instance to the database.
|
|
235
|
+
* - If the primary key is set → UPDATE (auto-generated columns are excluded from SET).
|
|
236
|
+
* - If the primary key is null/undefined → INSERT (auto-generated columns are excluded;
|
|
237
|
+
* the generated PK is written back to the instance).
|
|
238
|
+
*/
|
|
239
|
+
static async save(entity) {
|
|
240
|
+
var _a, _b, _c, _d;
|
|
241
|
+
const cls = entity.constructor;
|
|
242
|
+
const pk = cls.primaryKey;
|
|
243
|
+
const tableName = cls.table;
|
|
244
|
+
const fieldNames = (_a = cls.fields) !== null && _a !== void 0 ? _a : [];
|
|
245
|
+
const autoGeneratedFields = (_b = cls.autoGeneratedFields) !== null && _b !== void 0 ? _b : [];
|
|
246
|
+
const castsMap = (_c = cls.casts) !== null && _c !== void 0 ? _c : {};
|
|
247
|
+
const source = (_d = cls.source) !== null && _d !== void 0 ? _d : db_1.DB.getActiveSource();
|
|
248
|
+
// Build the row, skipping auto-generated columns (DB manages their values)
|
|
249
|
+
const row = {};
|
|
250
|
+
const cols = fieldNames.length > 0
|
|
251
|
+
? fieldNames
|
|
252
|
+
: Object.keys(entity).filter((k) => typeof entity[k] !== 'function');
|
|
253
|
+
for (const col of cols) {
|
|
254
|
+
if (autoGeneratedFields.includes(col)) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const value = entity[col];
|
|
258
|
+
if (value === undefined) {
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const cast = castsMap[col];
|
|
262
|
+
row[col] = cast ? applyCastForStorage(value, cast) : value;
|
|
263
|
+
}
|
|
264
|
+
if (entity[pk] != null) {
|
|
265
|
+
// UPDATE — also exclude PK from SET clause
|
|
266
|
+
const updateRow = Object.assign({}, row);
|
|
267
|
+
delete updateRow[pk];
|
|
268
|
+
await source
|
|
269
|
+
.table(tableName)
|
|
270
|
+
.where(pk, entity[pk])
|
|
271
|
+
.update(updateRow);
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
// INSERT — get connection to read back the generated ID
|
|
275
|
+
const conn = await source.getConnection();
|
|
276
|
+
try {
|
|
277
|
+
await conn.execute(new query_1.InsertQuery().setTable(tableName).setFields(row));
|
|
278
|
+
if (autoGeneratedFields.includes(pk)) {
|
|
279
|
+
;
|
|
280
|
+
entity[pk] = await conn.lastInsertId();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
await conn.close();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Deletes an entity instance from the database by its primary key.
|
|
290
|
+
* Throws if the primary key is not set.
|
|
291
|
+
*/
|
|
292
|
+
static async delete(entity) {
|
|
293
|
+
var _a;
|
|
294
|
+
const cls = entity.constructor;
|
|
295
|
+
const pk = cls.primaryKey;
|
|
296
|
+
const pkValue = entity[pk];
|
|
297
|
+
if (pkValue == null) {
|
|
298
|
+
throw new Error('Cannot delete an entity without a primary key value.');
|
|
299
|
+
}
|
|
300
|
+
const source = (_a = cls.source) !== null && _a !== void 0 ? _a : db_1.DB.getActiveSource();
|
|
301
|
+
await source.table(cls.table).where(pk, pkValue).delete();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
exports.Entities = Entities;
|
|
305
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/entities/decorators.ts"],"names":[],"mappings":";;;AAEA,8BAA0B;AAC1B,oCAA6C;AAI7C,iDAA4C;AAC5C,iDAOuB;AAuBvB,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAyB,CAAA;AAErD,SAAS,WAAW,CAAC,KAAa;IAChC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;QACxB,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC,CAAA;KACxD;IAED,OAAO,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAE,CAAA;AAC7B,CAAC;AAED,iFAAiF;AAEjF,SAAS,SAAS,CAAC,KAAU,EAAE,IAAc;IAC3C,IAAI,KAAK,IAAI,IAAI,EAAE;QACjB,OAAO,IAAI,CAAA;KACZ;IAED,QAAQ,IAAI,EAAE;QACZ,KAAK,QAAQ;YACX,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;QACtB,KAAK,SAAS;YACZ,OAAO,KAAK,KAAK,IAAI,IAAI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AAE,GAAW,GAAG,CAAC,UAAU,CAAA;QACjC,MAAM,OAAO,GAAI,MAAc,CAAC,EAAE,CAAC,CAAA;QAEnC,IAAI,OAAO,IAAI,IAAI,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;SACxE;QAED,MAAM,MAAM,GAAe,MAAA,GAAG,CAAC,MAAM,mCAAI,OAAE,CAAC,eAAe,EAAE,CAAA;QAE7D,MAAM,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC,MAAM,EAAE,CAAA;IAC3D,CAAC;CACF;AAhFD,4BAgFC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DataTable } from '../data-table';
|
|
2
|
+
import { Field } from '../query';
|
|
3
|
+
import { Condition, ConditionGroup } from '../query/conditions';
|
|
4
|
+
import { OrderByDirection } from '../query/features/has-order-by-fields';
|
|
5
|
+
import { Relationship } from './relationship';
|
|
6
|
+
type EntityClass<T> = (new () => T) & {
|
|
7
|
+
table: string;
|
|
8
|
+
primaryKey: string;
|
|
9
|
+
relationships: Record<string, Relationship>;
|
|
10
|
+
fromRow(row: Record<string, any>): T;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Chainable query builder for Entities. Wraps DataTable and hydrates rows
|
|
14
|
+
* into typed Entity instances when a terminal method is called.
|
|
15
|
+
*/
|
|
16
|
+
export declare class EntityQuery<T> {
|
|
17
|
+
private _entityClass;
|
|
18
|
+
private _table;
|
|
19
|
+
private _withs;
|
|
20
|
+
constructor(entityClass: EntityClass<T>, table: DataTable);
|
|
21
|
+
with(relations: string | string[], ...rest: string[]): this;
|
|
22
|
+
joinRelationship(relationName: string): this;
|
|
23
|
+
innerJoinRelationship(relationName: string): this;
|
|
24
|
+
leftJoinRelationship(relationName: string): this;
|
|
25
|
+
private _applyJoin;
|
|
26
|
+
get(): Promise<T[]>;
|
|
27
|
+
first(): Promise<T | null>;
|
|
28
|
+
find(id: any): Promise<T | null>;
|
|
29
|
+
private _loadRelations;
|
|
30
|
+
where(callback: (group: ConditionGroup) => void): this;
|
|
31
|
+
where(condition: Condition): this;
|
|
32
|
+
where(field: Field, value: any): this;
|
|
33
|
+
where(field: Field, operator: string, value: any): this;
|
|
34
|
+
whereIn(field: Field, values: any[]): this;
|
|
35
|
+
whereNotIn(field: Field, values: any[]): this;
|
|
36
|
+
whereBetween(field: Field, range: [any, any]): this;
|
|
37
|
+
whereNotBetween(field: Field, range: [any, any]): this;
|
|
38
|
+
whereNull(field: Field): this;
|
|
39
|
+
whereNotNull(field: Field): this;
|
|
40
|
+
whereLike(field: Field, pattern: string): this;
|
|
41
|
+
whereNotLike(field: Field, pattern: string): this;
|
|
42
|
+
whereColumn(field: Field, column: Field): this;
|
|
43
|
+
whereColumn(field: Field, operator: string, column: Field): this;
|
|
44
|
+
orWhere(...args: any[]): this;
|
|
45
|
+
orWhereIn(field: Field, values: any[]): this;
|
|
46
|
+
orWhereNotIn(field: Field, values: any[]): this;
|
|
47
|
+
orWhereBetween(field: Field, range: [any, any]): this;
|
|
48
|
+
orWhereNotBetween(field: Field, range: [any, any]): this;
|
|
49
|
+
orWhereNull(field: Field): this;
|
|
50
|
+
orWhereNotNull(field: Field): this;
|
|
51
|
+
orWhereLike(field: Field, pattern: string): this;
|
|
52
|
+
orWhereNotLike(field: Field, pattern: string): this;
|
|
53
|
+
select(...fields: (Field | Field[])[]): this;
|
|
54
|
+
orderBy(field: Field, direction?: OrderByDirection): this;
|
|
55
|
+
groupBy(...fields: Field[]): this;
|
|
56
|
+
limit(value: number): this;
|
|
57
|
+
offset(value: number): this;
|
|
58
|
+
distinct(): this;
|
|
59
|
+
}
|
|
60
|
+
export {};
|