ts-prorm-orm 1.2.3 → 2.0.0
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/CHANGELOG.md +199 -1
- package/README.md +32 -24
- package/dist/compliance/index.d.ts +2 -0
- package/dist/compliance/index.js +13 -1
- package/dist/connection-manager.js +13 -1
- package/dist/core/expressions.d.ts +58 -0
- package/dist/core/expressions.js +83 -0
- package/dist/core/hooks-manager.d.ts +179 -0
- package/dist/core/hooks-manager.js +291 -0
- package/dist/core/types.d.ts +235 -0
- package/dist/core/types.js +9 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.js +11 -1
- package/dist/diagrams/chen-diagram.d.ts +15 -12
- package/dist/diagrams/chen-diagram.js +76 -85
- package/dist/diagrams/class-diagram.d.ts +9 -8
- package/dist/diagrams/class-diagram.js +60 -82
- package/dist/diagrams/core/index.d.ts +13 -0
- package/dist/diagrams/core/index.js +33 -0
- package/dist/diagrams/core/layout.d.ts +104 -0
- package/dist/diagrams/core/layout.js +223 -0
- package/dist/diagrams/{svg-dom.d.ts → core/svg.d.ts} +8 -20
- package/dist/diagrams/{svg-dom.js → core/svg.js} +21 -29
- package/dist/diagrams/core/theme.d.ts +84 -0
- package/dist/diagrams/core/theme.js +62 -0
- package/dist/diagrams/dependency-diagram.d.ts +15 -14
- package/dist/diagrams/dependency-diagram.js +71 -105
- package/dist/diagrams/er-diagram.d.ts +13 -26
- package/dist/diagrams/er-diagram.js +180 -259
- package/dist/diagrams/flow-diagram.d.ts +16 -11
- package/dist/diagrams/flow-diagram.js +96 -91
- package/dist/diagrams/gantt-diagram.d.ts +10 -5
- package/dist/diagrams/gantt-diagram.js +77 -103
- package/dist/diagrams/index-diagram.d.ts +10 -10
- package/dist/diagrams/index-diagram.js +106 -116
- package/dist/diagrams/index.d.ts +6 -9
- package/dist/diagrams/index.js +9 -24
- package/dist/diagrams/migration-diagram.d.ts +8 -4
- package/dist/diagrams/migration-diagram.js +189 -161
- package/dist/diagrams/model-diagram.d.ts +34 -34
- package/dist/diagrams/model-diagram.js +106 -485
- package/dist/diagrams/package-diagram.d.ts +9 -6
- package/dist/diagrams/package-diagram.js +64 -101
- package/dist/diagrams/relational-diagram.d.ts +12 -10
- package/dist/diagrams/relational-diagram.js +87 -114
- package/dist/diagrams/schemadoc-diagram.d.ts +8 -8
- package/dist/diagrams/schemadoc-diagram.js +71 -93
- package/dist/diagrams/sequence-diagram.d.ts +10 -10
- package/dist/diagrams/sequence-diagram.js +64 -89
- package/dist/diagrams/state-diagram.d.ts +10 -9
- package/dist/diagrams/state-diagram.js +69 -92
- package/dist/diagrams/tree-diagram.d.ts +11 -5
- package/dist/diagrams/tree-diagram.js +41 -65
- package/dist/dialects/clickhouse/index.js +15 -0
- package/dist/dialects/cockroachdb/index.d.ts +1 -1
- package/dist/dialects/cockroachdb/index.js +32 -63
- package/dist/dialects/db2/index.js +23 -8
- package/dist/dialects/dialect.d.ts +29 -5
- package/dist/dialects/duckdb/index.js +9 -2
- package/dist/dialects/hana/index.js +22 -5
- package/dist/dialects/lock-clause-helper.d.ts +167 -0
- package/dist/dialects/lock-clause-helper.js +316 -0
- package/dist/dialects/mariadb/index.d.ts +7 -4
- package/dist/dialects/mariadb/index.js +36 -60
- package/dist/dialects/mssql/index.js +34 -3
- package/dist/dialects/mysql/index.d.ts +1 -1
- package/dist/dialects/mysql/index.js +17 -45
- package/dist/dialects/oracle/index.d.ts +14 -3
- package/dist/dialects/oracle/index.js +66 -36
- package/dist/dialects/order-expression-helper.d.ts +72 -0
- package/dist/dialects/order-expression-helper.js +154 -0
- package/dist/dialects/partial-index.d.ts +52 -0
- package/dist/dialects/partial-index.js +251 -0
- package/dist/dialects/postgres/index.d.ts +1 -1
- package/dist/dialects/postgres/index.js +43 -68
- package/dist/dialects/redshift/index.js +9 -2
- package/dist/dialects/snowflake/index.js +17 -0
- package/dist/dialects/spanner/index.js +7 -2
- package/dist/dialects/sqlite/driver.d.ts +55 -0
- package/dist/dialects/sqlite/driver.js +102 -0
- package/dist/dialects/sqlite/index.d.ts +1 -1
- package/dist/dialects/sqlite/index.js +49 -27
- package/dist/graph/base-graph-dialect.d.ts +24 -3
- package/dist/graph/base-graph-dialect.js +43 -2
- package/dist/graph/dgraph/index.js +6 -0
- package/dist/graph/gremlin/index.d.ts +6 -0
- package/dist/graph/gremlin/index.js +12 -1
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/neo4j/index.d.ts +2 -0
- package/dist/graph/neo4j/index.js +6 -1
- package/dist/graph/types.d.ts +12 -1
- package/dist/index.d.ts +12 -3
- package/dist/index.js +53 -7
- package/dist/logging/index.d.ts +10 -0
- package/dist/logging/index.js +23 -0
- package/dist/logging/query-logging.d.ts +82 -0
- package/dist/logging/query-logging.js +102 -0
- package/dist/models/decorators.d.ts +39 -3
- package/dist/models/decorators.js +74 -0
- package/dist/models/eager-load.d.ts +99 -0
- package/dist/models/eager-load.js +348 -5
- package/dist/models/indexes.d.ts +3 -2
- package/dist/models/indexes.js +7 -1
- package/dist/models/model.js +69 -43
- package/dist/prorm.d.ts +175 -409
- package/dist/prorm.js +1132 -553
- package/dist/query-builders/order-limit-builder.js +12 -10
- package/dist/query-builders/sql-compiler.d.ts +10 -3
- package/dist/query-builders/sql-compiler.js +14 -43
- package/dist/query-interface.d.ts +2 -1
- package/dist/sql-constants.d.ts +44 -0
- package/dist/sql-constants.js +138 -1
- package/dist/types/index.d.ts +53 -17
- package/dist/types/index.js +10 -15
- package/dist/types/query-types.d.ts +26 -0
- package/dist/types/query-types.js +30 -0
- package/package.json +32 -5
- package/dist/decorators/belongs-to-many.js +0 -115
- package/dist/decorators/belongs-to.js +0 -115
- package/dist/decorators/has-many.js +0 -127
- package/dist/decorators/has-one.js +0 -116
- package/dist/diagrams/palette.d.ts +0 -138
- package/dist/diagrams/palette.js +0 -194
- package/dist/hooks/hooks-manager.d.ts +0 -189
- package/dist/hooks/hooks-manager.js +0 -350
- package/dist/hooks/index.d.ts +0 -8
- package/dist/hooks/index.js +0 -37
- /package/dist/{logging.d.ts → logging/logger.d.ts} +0 -0
- /package/dist/{logging.js → logging/logger.js} +0 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lifecycle hook registries for a Prorm instance.
|
|
3
|
+
*
|
|
4
|
+
* `HooksManager` holds instance-wide hooks (define/sync/connect/destroy/...);
|
|
5
|
+
* `ModelHooks` holds the per-model set. Both were declared inline in prorm.ts,
|
|
6
|
+
* above the Prorm class.
|
|
7
|
+
*
|
|
8
|
+
* Not to be confused with `src/hooks/hooks-manager.ts` - a separate,
|
|
9
|
+
* currently unreferenced model-level hook system that shares the class name.
|
|
10
|
+
*/
|
|
11
|
+
import type { HookCallback, Connection, SyncOptions } from './types';
|
|
12
|
+
import type { AttributeOptions, ModelStatic, DestroyOptions, ReloadOptions } from '../types';
|
|
13
|
+
import type { ModelOptions as ModelOptionsType } from '../types';
|
|
14
|
+
/** Loose model type, matching the alias prorm.ts uses. */
|
|
15
|
+
type AnyModel = any;
|
|
16
|
+
/**
|
|
17
|
+
* Hook manager for Prorm instance
|
|
18
|
+
*/
|
|
19
|
+
export declare class HooksManager {
|
|
20
|
+
private beforeDefineHooks;
|
|
21
|
+
private afterDefineHooks;
|
|
22
|
+
private beforeSyncHooks;
|
|
23
|
+
private afterSyncHooks;
|
|
24
|
+
private beforeConnectHooks;
|
|
25
|
+
private afterConnectHooks;
|
|
26
|
+
private beforeDisconnectHooks;
|
|
27
|
+
private afterDisconnectHooks;
|
|
28
|
+
private beforeDestroyHooks;
|
|
29
|
+
private afterDestroyHooks;
|
|
30
|
+
private beforeUpsertHooks;
|
|
31
|
+
private afterUpsertHooks;
|
|
32
|
+
private beforeReloadHooks;
|
|
33
|
+
/**
|
|
34
|
+
* Register a beforeDefine hook
|
|
35
|
+
*/
|
|
36
|
+
beforeDefine(callback: HookCallback): void;
|
|
37
|
+
/**
|
|
38
|
+
* Register an afterDefine hook
|
|
39
|
+
*/
|
|
40
|
+
afterDefine(callback: HookCallback): void;
|
|
41
|
+
/**
|
|
42
|
+
* Register a beforeSync hook
|
|
43
|
+
*/
|
|
44
|
+
beforeSync(callback: HookCallback): void;
|
|
45
|
+
/**
|
|
46
|
+
* Register an afterSync hook
|
|
47
|
+
*/
|
|
48
|
+
afterSync(callback: HookCallback): void;
|
|
49
|
+
/**
|
|
50
|
+
* Register a beforeConnect hook
|
|
51
|
+
*/
|
|
52
|
+
beforeConnect(callback: HookCallback): void;
|
|
53
|
+
/**
|
|
54
|
+
* Register an afterConnect hook
|
|
55
|
+
*/
|
|
56
|
+
afterConnect(callback: HookCallback): void;
|
|
57
|
+
/**
|
|
58
|
+
* Register a beforeDisconnect hook
|
|
59
|
+
*/
|
|
60
|
+
beforeDisconnect(callback: HookCallback): void;
|
|
61
|
+
/**
|
|
62
|
+
* Register an afterDisconnect hook
|
|
63
|
+
*/
|
|
64
|
+
afterDisconnect(callback: HookCallback): void;
|
|
65
|
+
/**
|
|
66
|
+
* Register a beforeDestroy hook
|
|
67
|
+
*/
|
|
68
|
+
beforeDestroy(callback: HookCallback): void;
|
|
69
|
+
/**
|
|
70
|
+
* Register an afterDestroy hook
|
|
71
|
+
*/
|
|
72
|
+
afterDestroy(callback: HookCallback): void;
|
|
73
|
+
/**
|
|
74
|
+
* Register a beforeUpsert hook
|
|
75
|
+
*/
|
|
76
|
+
beforeUpsert(callback: HookCallback): void;
|
|
77
|
+
/**
|
|
78
|
+
* Register an afterUpsert hook
|
|
79
|
+
*/
|
|
80
|
+
afterUpsert(callback: HookCallback): void;
|
|
81
|
+
/**
|
|
82
|
+
* Register a beforeReload hook
|
|
83
|
+
*/
|
|
84
|
+
beforeReload(callback: HookCallback): void;
|
|
85
|
+
/**
|
|
86
|
+
* Execute all beforeDefine hooks
|
|
87
|
+
*/
|
|
88
|
+
runBeforeDefine(modelName: string, attributes: Record<string, AttributeOptions>, options: ModelOptionsType): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Execute all afterDefine hooks
|
|
91
|
+
*/
|
|
92
|
+
runAfterDefine(model: ModelStatic<AnyModel>): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Execute all beforeSync hooks
|
|
95
|
+
*/
|
|
96
|
+
runBeforeSync(options: SyncOptions): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Execute all afterSync hooks
|
|
99
|
+
*/
|
|
100
|
+
runAfterSync(options: SyncOptions): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Execute all beforeConnect hooks
|
|
103
|
+
*/
|
|
104
|
+
runBeforeConnect(): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Execute all afterConnect hooks
|
|
107
|
+
* @param connection - The connection object to pass to hooks
|
|
108
|
+
*/
|
|
109
|
+
runAfterConnect(connection: Connection): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Execute all beforeDisconnect hooks
|
|
112
|
+
* @param connection - The connection object to pass to hooks
|
|
113
|
+
*/
|
|
114
|
+
runBeforeDisconnect(connection: Connection): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Execute all afterDisconnect hooks
|
|
117
|
+
* @param connection - The connection object to pass to hooks
|
|
118
|
+
*/
|
|
119
|
+
runAfterDisconnect(connection: Connection): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Execute all beforeDestroy hooks
|
|
122
|
+
* @param options - The destroy options
|
|
123
|
+
*/
|
|
124
|
+
runBeforeDestroy(options: DestroyOptions): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Execute all afterDestroy hooks
|
|
127
|
+
* @param options - The destroy options
|
|
128
|
+
*/
|
|
129
|
+
runAfterDestroy(options: DestroyOptions): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Execute all beforeUpsert hooks
|
|
132
|
+
* @param options - The upsert options
|
|
133
|
+
*/
|
|
134
|
+
runBeforeUpsert(options: any): Promise<void>;
|
|
135
|
+
/**
|
|
136
|
+
* Execute all afterUpsert hooks
|
|
137
|
+
* @param options - The upsert options
|
|
138
|
+
*/
|
|
139
|
+
runAfterUpsert(options: any): Promise<void>;
|
|
140
|
+
/**
|
|
141
|
+
* Execute all beforeReload hooks
|
|
142
|
+
* @param options - The reload options
|
|
143
|
+
*/
|
|
144
|
+
runBeforeReload(options: ReloadOptions): Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Add a hook by name
|
|
147
|
+
* @param hookName - The name of the hook (e.g., 'beforeDefine', 'afterSync')
|
|
148
|
+
* @param callback - The hook callback function
|
|
149
|
+
*/
|
|
150
|
+
addHook(hookName: string, callback: HookCallback): void;
|
|
151
|
+
/**
|
|
152
|
+
* Clear all hooks
|
|
153
|
+
*/
|
|
154
|
+
clearHooks(): void;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Model-level hooks manager
|
|
158
|
+
*/
|
|
159
|
+
export declare class ModelHooks {
|
|
160
|
+
private beforeSyncHooks;
|
|
161
|
+
private afterSyncHooks;
|
|
162
|
+
/**
|
|
163
|
+
* Register a beforeSync hook on the model
|
|
164
|
+
*/
|
|
165
|
+
beforeSync(callback: HookCallback): void;
|
|
166
|
+
/**
|
|
167
|
+
* Register an afterSync hook on the model
|
|
168
|
+
*/
|
|
169
|
+
afterSync(callback: HookCallback): void;
|
|
170
|
+
/**
|
|
171
|
+
* Run beforeSync hooks
|
|
172
|
+
*/
|
|
173
|
+
runBeforeSync(options: SyncOptions): Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Run afterSync hooks
|
|
176
|
+
*/
|
|
177
|
+
runAfterSync(options: SyncOptions): Promise<void>;
|
|
178
|
+
}
|
|
179
|
+
export {};
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Lifecycle hook registries for a Prorm instance.
|
|
4
|
+
*
|
|
5
|
+
* `HooksManager` holds instance-wide hooks (define/sync/connect/destroy/...);
|
|
6
|
+
* `ModelHooks` holds the per-model set. Both were declared inline in prorm.ts,
|
|
7
|
+
* above the Prorm class.
|
|
8
|
+
*
|
|
9
|
+
* Not to be confused with `src/hooks/hooks-manager.ts` - a separate,
|
|
10
|
+
* currently unreferenced model-level hook system that shares the class name.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.ModelHooks = exports.HooksManager = void 0;
|
|
14
|
+
/**
|
|
15
|
+
* Hook manager for Prorm instance
|
|
16
|
+
*/
|
|
17
|
+
class HooksManager {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.beforeDefineHooks = [];
|
|
20
|
+
this.afterDefineHooks = [];
|
|
21
|
+
this.beforeSyncHooks = [];
|
|
22
|
+
this.afterSyncHooks = [];
|
|
23
|
+
this.beforeConnectHooks = [];
|
|
24
|
+
this.afterConnectHooks = [];
|
|
25
|
+
this.beforeDisconnectHooks = [];
|
|
26
|
+
this.afterDisconnectHooks = [];
|
|
27
|
+
this.beforeDestroyHooks = [];
|
|
28
|
+
this.afterDestroyHooks = [];
|
|
29
|
+
this.beforeUpsertHooks = [];
|
|
30
|
+
this.afterUpsertHooks = [];
|
|
31
|
+
this.beforeReloadHooks = [];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Register a beforeDefine hook
|
|
35
|
+
*/
|
|
36
|
+
beforeDefine(callback) {
|
|
37
|
+
this.beforeDefineHooks.push(callback);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Register an afterDefine hook
|
|
41
|
+
*/
|
|
42
|
+
afterDefine(callback) {
|
|
43
|
+
this.afterDefineHooks.push(callback);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Register a beforeSync hook
|
|
47
|
+
*/
|
|
48
|
+
beforeSync(callback) {
|
|
49
|
+
this.beforeSyncHooks.push(callback);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Register an afterSync hook
|
|
53
|
+
*/
|
|
54
|
+
afterSync(callback) {
|
|
55
|
+
this.afterSyncHooks.push(callback);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Register a beforeConnect hook
|
|
59
|
+
*/
|
|
60
|
+
beforeConnect(callback) {
|
|
61
|
+
this.beforeConnectHooks.push(callback);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Register an afterConnect hook
|
|
65
|
+
*/
|
|
66
|
+
afterConnect(callback) {
|
|
67
|
+
this.afterConnectHooks.push(callback);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Register a beforeDisconnect hook
|
|
71
|
+
*/
|
|
72
|
+
beforeDisconnect(callback) {
|
|
73
|
+
this.beforeDisconnectHooks.push(callback);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Register an afterDisconnect hook
|
|
77
|
+
*/
|
|
78
|
+
afterDisconnect(callback) {
|
|
79
|
+
this.afterDisconnectHooks.push(callback);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Register a beforeDestroy hook
|
|
83
|
+
*/
|
|
84
|
+
beforeDestroy(callback) {
|
|
85
|
+
this.beforeDestroyHooks.push(callback);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Register an afterDestroy hook
|
|
89
|
+
*/
|
|
90
|
+
afterDestroy(callback) {
|
|
91
|
+
this.afterDestroyHooks.push(callback);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Register a beforeUpsert hook
|
|
95
|
+
*/
|
|
96
|
+
beforeUpsert(callback) {
|
|
97
|
+
this.beforeUpsertHooks.push(callback);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Register an afterUpsert hook
|
|
101
|
+
*/
|
|
102
|
+
afterUpsert(callback) {
|
|
103
|
+
this.afterUpsertHooks.push(callback);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Register a beforeReload hook
|
|
107
|
+
*/
|
|
108
|
+
beforeReload(callback) {
|
|
109
|
+
this.beforeReloadHooks.push(callback);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Execute all beforeDefine hooks
|
|
113
|
+
*/
|
|
114
|
+
async runBeforeDefine(modelName, attributes, options) {
|
|
115
|
+
for (const hook of this.beforeDefineHooks) {
|
|
116
|
+
await hook({ modelName, attributes, options });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Execute all afterDefine hooks
|
|
121
|
+
*/
|
|
122
|
+
async runAfterDefine(model) {
|
|
123
|
+
for (const hook of this.afterDefineHooks) {
|
|
124
|
+
await hook({ model });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Execute all beforeSync hooks
|
|
129
|
+
*/
|
|
130
|
+
async runBeforeSync(options) {
|
|
131
|
+
for (const hook of this.beforeSyncHooks) {
|
|
132
|
+
await hook({ options });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Execute all afterSync hooks
|
|
137
|
+
*/
|
|
138
|
+
async runAfterSync(options) {
|
|
139
|
+
for (const hook of this.afterSyncHooks) {
|
|
140
|
+
await hook({ options });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Execute all beforeConnect hooks
|
|
145
|
+
*/
|
|
146
|
+
async runBeforeConnect() {
|
|
147
|
+
for (const hook of this.beforeConnectHooks) {
|
|
148
|
+
await hook();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Execute all afterConnect hooks
|
|
153
|
+
* @param connection - The connection object to pass to hooks
|
|
154
|
+
*/
|
|
155
|
+
async runAfterConnect(connection) {
|
|
156
|
+
for (const hook of this.afterConnectHooks) {
|
|
157
|
+
await hook(connection);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Execute all beforeDisconnect hooks
|
|
162
|
+
* @param connection - The connection object to pass to hooks
|
|
163
|
+
*/
|
|
164
|
+
async runBeforeDisconnect(connection) {
|
|
165
|
+
for (const hook of this.beforeDisconnectHooks) {
|
|
166
|
+
await hook(connection);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Execute all afterDisconnect hooks
|
|
171
|
+
* @param connection - The connection object to pass to hooks
|
|
172
|
+
*/
|
|
173
|
+
async runAfterDisconnect(connection) {
|
|
174
|
+
for (const hook of this.afterDisconnectHooks) {
|
|
175
|
+
await hook(connection);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Execute all beforeDestroy hooks
|
|
180
|
+
* @param options - The destroy options
|
|
181
|
+
*/
|
|
182
|
+
async runBeforeDestroy(options) {
|
|
183
|
+
for (const hook of this.beforeDestroyHooks) {
|
|
184
|
+
await hook({ options });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Execute all afterDestroy hooks
|
|
189
|
+
* @param options - The destroy options
|
|
190
|
+
*/
|
|
191
|
+
async runAfterDestroy(options) {
|
|
192
|
+
for (const hook of this.afterDestroyHooks) {
|
|
193
|
+
await hook({ options });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Execute all beforeUpsert hooks
|
|
198
|
+
* @param options - The upsert options
|
|
199
|
+
*/
|
|
200
|
+
async runBeforeUpsert(options) {
|
|
201
|
+
for (const hook of this.beforeUpsertHooks) {
|
|
202
|
+
await hook({ options });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Execute all afterUpsert hooks
|
|
207
|
+
* @param options - The upsert options
|
|
208
|
+
*/
|
|
209
|
+
async runAfterUpsert(options) {
|
|
210
|
+
for (const hook of this.afterUpsertHooks) {
|
|
211
|
+
await hook({ options });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Execute all beforeReload hooks
|
|
216
|
+
* @param options - The reload options
|
|
217
|
+
*/
|
|
218
|
+
async runBeforeReload(options) {
|
|
219
|
+
for (const hook of this.beforeReloadHooks) {
|
|
220
|
+
await hook({ options });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Add a hook by name
|
|
225
|
+
* @param hookName - The name of the hook (e.g., 'beforeDefine', 'afterSync')
|
|
226
|
+
* @param callback - The hook callback function
|
|
227
|
+
*/
|
|
228
|
+
addHook(hookName, callback) {
|
|
229
|
+
const methodName = hookName;
|
|
230
|
+
if (typeof this[methodName] === 'function') {
|
|
231
|
+
this[methodName](callback);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Clear all hooks
|
|
236
|
+
*/
|
|
237
|
+
clearHooks() {
|
|
238
|
+
this.beforeDefineHooks = [];
|
|
239
|
+
this.afterDefineHooks = [];
|
|
240
|
+
this.beforeSyncHooks = [];
|
|
241
|
+
this.afterSyncHooks = [];
|
|
242
|
+
this.beforeConnectHooks = [];
|
|
243
|
+
this.afterConnectHooks = [];
|
|
244
|
+
this.beforeDisconnectHooks = [];
|
|
245
|
+
this.afterDisconnectHooks = [];
|
|
246
|
+
this.beforeDestroyHooks = [];
|
|
247
|
+
this.afterDestroyHooks = [];
|
|
248
|
+
this.beforeUpsertHooks = [];
|
|
249
|
+
this.afterUpsertHooks = [];
|
|
250
|
+
this.beforeReloadHooks = [];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
exports.HooksManager = HooksManager;
|
|
254
|
+
/**
|
|
255
|
+
* Model-level hooks manager
|
|
256
|
+
*/
|
|
257
|
+
class ModelHooks {
|
|
258
|
+
constructor() {
|
|
259
|
+
this.beforeSyncHooks = [];
|
|
260
|
+
this.afterSyncHooks = [];
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Register a beforeSync hook on the model
|
|
264
|
+
*/
|
|
265
|
+
beforeSync(callback) {
|
|
266
|
+
this.beforeSyncHooks.push(callback);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Register an afterSync hook on the model
|
|
270
|
+
*/
|
|
271
|
+
afterSync(callback) {
|
|
272
|
+
this.afterSyncHooks.push(callback);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Run beforeSync hooks
|
|
276
|
+
*/
|
|
277
|
+
async runBeforeSync(options) {
|
|
278
|
+
for (const hook of this.beforeSyncHooks) {
|
|
279
|
+
await hook({ options });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Run afterSync hooks
|
|
284
|
+
*/
|
|
285
|
+
async runAfterSync(options) {
|
|
286
|
+
for (const hook of this.afterSyncHooks) {
|
|
287
|
+
await hook({ options });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
exports.ModelHooks = ModelHooks;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types and interfaces for the Prorm instance API.
|
|
3
|
+
*
|
|
4
|
+
* These were declared inline in prorm.ts, scattered above and below the
|
|
5
|
+
* 7,500-line Prorm class. They are re-exported from prorm.ts so the public API
|
|
6
|
+
* is unchanged.
|
|
7
|
+
*/
|
|
8
|
+
import type { AttributeOptions, ModelStatic } from '../types';
|
|
9
|
+
import type { ModelOptions as ModelOptionsType } from '../types';
|
|
10
|
+
import type { Transaction } from '../transaction';
|
|
11
|
+
import { QueryTypes } from '../types/query-types';
|
|
12
|
+
import type { Col, Literal } from './expressions';
|
|
13
|
+
/**
|
|
14
|
+
* Hook types for the ORM
|
|
15
|
+
*/
|
|
16
|
+
export type HookCallback = (...args: any[]) => void | Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Every model lifecycle hook the ORM fires.
|
|
19
|
+
*
|
|
20
|
+
* Typing hook names as a union rather than `string` makes a misspelled hook a
|
|
21
|
+
* compile error instead of a callback that silently never runs - which is
|
|
22
|
+
* exactly how eleven of these went unnoticed.
|
|
23
|
+
*/
|
|
24
|
+
export type HookName = 'beforeValidate' | 'afterValidate' | 'beforeCreate' | 'afterCreate' | 'beforeUpdate' | 'afterUpdate' | 'beforeDestroy' | 'afterDestroy' | 'beforeSave' | 'afterSave' | 'beforeUpsert' | 'afterUpsert' | 'beforeFind' | 'afterFind' | 'beforeCount' | 'beforeBulkCreate' | 'afterBulkCreate' | 'beforeBulkUpdate' | 'afterBulkUpdate' | 'beforeBulkDestroy' | 'afterBulkDestroy';
|
|
25
|
+
/** A model's hook map, as passed to `define(..., { hooks })`. */
|
|
26
|
+
export type ModelHookMap = Partial<Record<HookName, HookCallback>>;
|
|
27
|
+
/**
|
|
28
|
+
* Connection type for hook callbacks
|
|
29
|
+
*/
|
|
30
|
+
export interface Connection {
|
|
31
|
+
id: string;
|
|
32
|
+
createdAt: number;
|
|
33
|
+
[key: string]: any;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Options for sync method
|
|
37
|
+
*/
|
|
38
|
+
export interface SyncOptions {
|
|
39
|
+
/**
|
|
40
|
+
* If true, drops all tables and recreates them.
|
|
41
|
+
* WARNING: This will destroy all data in the tables!
|
|
42
|
+
* @default false
|
|
43
|
+
*/
|
|
44
|
+
force?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* If true, alter tables to match model definitions.
|
|
47
|
+
* This will add new columns, remove extra columns, and change column types as needed.
|
|
48
|
+
* Preserves existing data in the table.
|
|
49
|
+
* Can be an object with { drop: false } to prevent dropping columns
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
alter?: boolean | {
|
|
53
|
+
drop?: boolean;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Regular expression to match the database name.
|
|
57
|
+
* Sync will only proceed if the database name matches this pattern.
|
|
58
|
+
* Useful for development vs production environments.
|
|
59
|
+
* Example: /test/ - only syncs if database name contains "test"
|
|
60
|
+
*/
|
|
61
|
+
match?: RegExp;
|
|
62
|
+
/**
|
|
63
|
+
* Custom logging function
|
|
64
|
+
*/
|
|
65
|
+
logging?: boolean | ((sql: string, timing?: number) => void);
|
|
66
|
+
/**
|
|
67
|
+
* If true, run sync hooks (beforeSync, afterSync, etc.)
|
|
68
|
+
* @default true
|
|
69
|
+
*/
|
|
70
|
+
hooks?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* If true, also sync indexes
|
|
73
|
+
* @default true
|
|
74
|
+
*/
|
|
75
|
+
indexes?: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* If true, also sync foreign key constraints
|
|
78
|
+
* @default true
|
|
79
|
+
*/
|
|
80
|
+
constraints?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Options for raw query execution
|
|
84
|
+
*/
|
|
85
|
+
export interface RawQueryResult {
|
|
86
|
+
rows: unknown[];
|
|
87
|
+
count: number;
|
|
88
|
+
isSelect: boolean;
|
|
89
|
+
}
|
|
90
|
+
export interface QueryOptions {
|
|
91
|
+
/**
|
|
92
|
+
* Query type - determines how results are processed
|
|
93
|
+
* Forces the query type for result handling
|
|
94
|
+
* - SELECT: Returns array of rows
|
|
95
|
+
* - INSERT: Returns [rows, created]
|
|
96
|
+
* - UPDATE: Returns affected count
|
|
97
|
+
* - DELETE: Returns affected count
|
|
98
|
+
* - BULKDELETE: Returns affected count
|
|
99
|
+
* - UPSERT: Returns [rows, created]
|
|
100
|
+
*/
|
|
101
|
+
type?: QueryTypes | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'BULKDELETE' | 'UPSERT' | 'BULKINSERT' | 'VERSION' | 'SHOWTABLES' | 'DESCRIBE' | 'RAW';
|
|
102
|
+
/**
|
|
103
|
+
* Replacements for named (:param) or positional (?) placeholders
|
|
104
|
+
* For named: { username: 'john' } replaces :username in SQL
|
|
105
|
+
* For positional: ['john', 'smith'] replaces ? in order
|
|
106
|
+
*/
|
|
107
|
+
replacements?: Record<string, unknown> | unknown[];
|
|
108
|
+
/**
|
|
109
|
+
* Return raw results without model instantiation
|
|
110
|
+
*/
|
|
111
|
+
raw?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Whether to log the query
|
|
114
|
+
*/
|
|
115
|
+
logging?: boolean | ((sql: string, timing?: number) => void);
|
|
116
|
+
/**
|
|
117
|
+
* When true, logs query execution time
|
|
118
|
+
*/
|
|
119
|
+
benchmark?: boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Transaction to use for the query
|
|
122
|
+
*/
|
|
123
|
+
transaction?: Transaction;
|
|
124
|
+
/**
|
|
125
|
+
* Retry configuration for query execution
|
|
126
|
+
* On connection failure or retryable errors, retry up to max times
|
|
127
|
+
* @param max - Maximum number of retry attempts
|
|
128
|
+
* @param match - Array of error message patterns that trigger retry
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* // Retry on connection refused or timeout errors
|
|
132
|
+
* await prorm.query('SELECT * FROM users', {
|
|
133
|
+
* retry: { max: 3, match: ['Connection refused', 'Timeout', 'ECONNREFUSED'] }
|
|
134
|
+
* });
|
|
135
|
+
*/
|
|
136
|
+
retry?: {
|
|
137
|
+
max: number;
|
|
138
|
+
match?: string[];
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Map raw query results to a model instance
|
|
142
|
+
* When true, maps the result rows to ModelInstance objects using the provided model
|
|
143
|
+
* @default false
|
|
144
|
+
*/
|
|
145
|
+
mapToModel?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Model to use for mapping raw results to model instances
|
|
148
|
+
* Used together with mapToModel to create ModelInstance objects from raw query results
|
|
149
|
+
*/
|
|
150
|
+
model?: ModelStatic<any>;
|
|
151
|
+
}
|
|
152
|
+
/** The shape stored in `Prorm.modelDefinitions`. */
|
|
153
|
+
export type PrormModelDefinition = {
|
|
154
|
+
attributes: Record<string, AttributeOptions>;
|
|
155
|
+
options: ModelOptionsType;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Represents a SQL function expression
|
|
159
|
+
* Used with prorm.fn() to create SQL functions like COUNT, UPPER, YEAR, etc.
|
|
160
|
+
*/
|
|
161
|
+
export interface Fn {
|
|
162
|
+
/**
|
|
163
|
+
* The SQL function name (e.g., 'COUNT', 'UPPER', 'YEAR')
|
|
164
|
+
*/
|
|
165
|
+
fn: string;
|
|
166
|
+
/**
|
|
167
|
+
* The arguments to the function (can be column references, literals, or other functions)
|
|
168
|
+
*/
|
|
169
|
+
args: (string | Col | Fn | Literal)[];
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Represents a SQL CAST expression
|
|
173
|
+
* Used to cast a value to a specific data type
|
|
174
|
+
*/
|
|
175
|
+
export interface Cast {
|
|
176
|
+
/**
|
|
177
|
+
* Type identifier for CAST
|
|
178
|
+
*/
|
|
179
|
+
$type: 'cast';
|
|
180
|
+
/**
|
|
181
|
+
* The value to cast (column reference, literal, or function)
|
|
182
|
+
*/
|
|
183
|
+
value: string | Col | Fn | Literal;
|
|
184
|
+
/**
|
|
185
|
+
* The target data type (e.g., 'VARCHAR', 'INTEGER', 'DATE', 'BOOLEAN')
|
|
186
|
+
*/
|
|
187
|
+
type: string;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Represents a WHERE condition for complex queries
|
|
191
|
+
*/
|
|
192
|
+
export interface WhereObject {
|
|
193
|
+
/**
|
|
194
|
+
* Type identifier for WHERE
|
|
195
|
+
*/
|
|
196
|
+
$type: 'where';
|
|
197
|
+
/**
|
|
198
|
+
* The column or expression to compare
|
|
199
|
+
*/
|
|
200
|
+
col: Col | Fn | Literal;
|
|
201
|
+
/**
|
|
202
|
+
* The value or condition to compare against
|
|
203
|
+
*/
|
|
204
|
+
value: unknown;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Represents an AND or OR combination of conditions
|
|
208
|
+
*/
|
|
209
|
+
export interface AndOrObject {
|
|
210
|
+
/**
|
|
211
|
+
* Type identifier - 'and' or 'or'
|
|
212
|
+
*/
|
|
213
|
+
$type: 'and' | 'or';
|
|
214
|
+
/**
|
|
215
|
+
* The conditions to combine
|
|
216
|
+
*/
|
|
217
|
+
conditions: unknown[];
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Represents a JSON path query for JSON columns
|
|
221
|
+
*/
|
|
222
|
+
export interface JsonObject {
|
|
223
|
+
/**
|
|
224
|
+
* Type identifier for JSON
|
|
225
|
+
*/
|
|
226
|
+
$type: 'json';
|
|
227
|
+
/**
|
|
228
|
+
* The JSON path to query (dot notation or array notation)
|
|
229
|
+
*/
|
|
230
|
+
path: string;
|
|
231
|
+
/**
|
|
232
|
+
* Optional value to compare against
|
|
233
|
+
*/
|
|
234
|
+
value?: unknown;
|
|
235
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Types and interfaces for the Prorm instance API.
|
|
4
|
+
*
|
|
5
|
+
* These were declared inline in prorm.ts, scattered above and below the
|
|
6
|
+
* 7,500-line Prorm class. They are re-exported from prorm.ts so the public API
|
|
7
|
+
* is unchanged.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|