metal-orm 1.0.25 → 1.0.27
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/package.json
CHANGED
|
@@ -37,3 +37,142 @@ export function createMssqlExecutor(
|
|
|
37
37
|
},
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Tedious integration helper (driver adapter)
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
export interface TediousColumn {
|
|
46
|
+
metadata: { colName: string };
|
|
47
|
+
value: unknown;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TediousRequest {
|
|
51
|
+
addParameter(name: string, type: unknown, value: unknown): void;
|
|
52
|
+
on(event: 'row', listener: (columns: TediousColumn[]) => void): void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface TediousRequestCtor {
|
|
56
|
+
new (sql: string, callback: (err?: Error | null) => void): TediousRequest;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface TediousTypes {
|
|
60
|
+
NVarChar: unknown;
|
|
61
|
+
Int: unknown;
|
|
62
|
+
Float: unknown;
|
|
63
|
+
BigInt: unknown;
|
|
64
|
+
Bit: unknown;
|
|
65
|
+
DateTime: unknown;
|
|
66
|
+
VarBinary: unknown;
|
|
67
|
+
[key: string]: unknown;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface TediousModule {
|
|
71
|
+
Request: TediousRequestCtor;
|
|
72
|
+
TYPES: TediousTypes;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface TediousConnectionLike {
|
|
76
|
+
execSql(request: TediousRequest): void;
|
|
77
|
+
beginTransaction?(cb: (err?: Error | null) => void): void;
|
|
78
|
+
commitTransaction?(cb: (err?: Error | null) => void): void;
|
|
79
|
+
rollbackTransaction?(cb: (err?: Error | null) => void): void;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface CreateTediousClientOptions {
|
|
83
|
+
inferType?(value: unknown, TYPES: TediousTypes): unknown;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const defaultInferType = (value: unknown, TYPES: TediousTypes): unknown => {
|
|
87
|
+
if (value === null || value === undefined) return TYPES.NVarChar;
|
|
88
|
+
if (typeof value === 'number') {
|
|
89
|
+
return Number.isInteger(value) ? TYPES.Int : TYPES.Float;
|
|
90
|
+
}
|
|
91
|
+
if (typeof value === 'bigint') return TYPES.BigInt;
|
|
92
|
+
if (typeof value === 'boolean') return TYPES.Bit;
|
|
93
|
+
if (value instanceof Date) return TYPES.DateTime;
|
|
94
|
+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
|
|
95
|
+
return TYPES.VarBinary;
|
|
96
|
+
}
|
|
97
|
+
return TYPES.NVarChar;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export function createTediousMssqlClient(
|
|
101
|
+
connection: TediousConnectionLike,
|
|
102
|
+
{ Request, TYPES }: TediousModule,
|
|
103
|
+
options?: CreateTediousClientOptions
|
|
104
|
+
): MssqlClientLike {
|
|
105
|
+
const inferType = options?.inferType ?? defaultInferType;
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
async query(sql: string, params: unknown[] = []) {
|
|
109
|
+
const rows = await new Promise<Array<Record<string, unknown>>>(
|
|
110
|
+
(resolve, reject) => {
|
|
111
|
+
const collected: Record<string, unknown>[] = [];
|
|
112
|
+
|
|
113
|
+
const request = new Request(sql, err => {
|
|
114
|
+
if (err) return reject(err);
|
|
115
|
+
resolve(collected);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
params.forEach((value, idx) => {
|
|
119
|
+
const sqlType = inferType(value, TYPES);
|
|
120
|
+
request.addParameter(
|
|
121
|
+
`p${idx + 1}`,
|
|
122
|
+
sqlType,
|
|
123
|
+
value as unknown
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
request.on('row', cols => {
|
|
128
|
+
const row: Record<string, unknown> = {};
|
|
129
|
+
for (const col of cols) {
|
|
130
|
+
row[col.metadata.colName] = col.value;
|
|
131
|
+
}
|
|
132
|
+
collected.push(row);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
connection.execSql(request);
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return { recordset: rows };
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
beginTransaction: connection.beginTransaction
|
|
143
|
+
? () =>
|
|
144
|
+
new Promise<void>((resolve, reject) => {
|
|
145
|
+
connection.beginTransaction!(err =>
|
|
146
|
+
err ? reject(err) : resolve()
|
|
147
|
+
);
|
|
148
|
+
})
|
|
149
|
+
: undefined,
|
|
150
|
+
|
|
151
|
+
commit: connection.commitTransaction
|
|
152
|
+
? () =>
|
|
153
|
+
new Promise<void>((resolve, reject) => {
|
|
154
|
+
connection.commitTransaction!(err =>
|
|
155
|
+
err ? reject(err) : resolve()
|
|
156
|
+
);
|
|
157
|
+
})
|
|
158
|
+
: undefined,
|
|
159
|
+
|
|
160
|
+
rollback: connection.rollbackTransaction
|
|
161
|
+
? () =>
|
|
162
|
+
new Promise<void>((resolve, reject) => {
|
|
163
|
+
connection.rollbackTransaction!(err =>
|
|
164
|
+
err ? reject(err) : resolve()
|
|
165
|
+
);
|
|
166
|
+
})
|
|
167
|
+
: undefined,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function createTediousExecutor(
|
|
172
|
+
connection: TediousConnectionLike,
|
|
173
|
+
module: TediousModule,
|
|
174
|
+
options?: CreateTediousClientOptions
|
|
175
|
+
): DbExecutor {
|
|
176
|
+
const client = createTediousMssqlClient(connection, module, options);
|
|
177
|
+
return createMssqlExecutor(client);
|
|
178
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,11 +2,11 @@ export * from './schema/table.js';
|
|
|
2
2
|
export * from './schema/column.js';
|
|
3
3
|
export * from './schema/relation.js';
|
|
4
4
|
export * from './schema/types.js';
|
|
5
|
-
export * from './query-builder/select.js';
|
|
6
|
-
export * from './query-builder/select-helpers.js';
|
|
7
|
-
export * from './query-builder/insert.js';
|
|
8
|
-
export * from './query-builder/update.js';
|
|
9
|
-
export * from './query-builder/delete.js';
|
|
5
|
+
export * from './query-builder/select.js';
|
|
6
|
+
export * from './query-builder/select-helpers.js';
|
|
7
|
+
export * from './query-builder/insert.js';
|
|
8
|
+
export * from './query-builder/update.js';
|
|
9
|
+
export * from './query-builder/delete.js';
|
|
10
10
|
export * from './core/ast/expression.js';
|
|
11
11
|
export * from './core/hydration/types.js';
|
|
12
12
|
export * from './core/dialect/mysql/index.js';
|