Skip to content

Commit ec8459a

Browse files
committed
adding before hooks in engine and all the connections
1 parent ee66bcc commit ec8459a

8 files changed

Lines changed: 125 additions & 9 deletions

File tree

examples/with-mysql2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@stackpress/inquire-mysql2": "0.7.2",
12-
"mysql2": "3.16.0"
12+
"mysql2": "3.22.0"
1313
},
1414
"devDependencies": {
1515
"@types/node": "25.0.3",

packages/inquire-mysql2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"chai": "4.5.0",
7777
"mocha": "10.8.2",
7878
"ts-mocha": "10.0.0",
79-
"mysql2": "3.16.0",
79+
"mysql2": "3.22.0",
8080
"ts-node": "10.9.2",
8181
"tsx": "4.21.0",
8282
"typescript": "5.9.3"

packages/inquire-mysql2/src/Connection.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export default class Mysql2Connection implements Connection<Resource> {
1818
protected _lastId?: number|string;
1919
//the database connection
2020
protected _resource: Connector;
21+
//A hook used for logging purposes. Can also manipulate the final
22+
// query before execution.
23+
protected _before = async (_request: QueryObject) => {};
24+
25+
/**
26+
* Returns the before hook
27+
*/
28+
public get before() {
29+
return this._before;
30+
}
2131

2232
/**
2333
* Get the last inserted id
@@ -26,6 +36,13 @@ export default class Mysql2Connection implements Connection<Resource> {
2636
return this._lastId;
2737
}
2838

39+
/**
40+
* Sets the before hook
41+
*/
42+
public set before(before: (request: QueryObject) => Promise<void>) {
43+
this._before = before;
44+
}
45+
2946
/**
3047
* Set the connection
3148
*/
@@ -108,8 +125,13 @@ export default class Mysql2Connection implements Connection<Resource> {
108125
* Call the database. If no values are provided, use exec
109126
*/
110127
protected async _query<R = unknown>(request: QueryObject) {
128+
//allow last minute request manipulation
129+
await this._before(request);
130+
//extract the query and values from the request
111131
const { query, values = [] } = request;
132+
//get the proprietary resource
112133
const resource = await this.resource();
134+
//query the database and return the results
113135
const results = await resource.execute(query, values);
114136
if (Array.isArray(results[0])) {
115137
return results as Results<R>;

packages/inquire-pg/src/Connection.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ export default class PGConnection implements Connection<Resource> {
1515
public readonly dialect: Dialect = Pgsql;
1616
//the database connection
1717
protected _resource: Connector;
18+
//A hook used for logging purposes. Can also manipulate the final
19+
// query before execution.
20+
protected _before = async (_request: QueryObject) => {};
21+
22+
/**
23+
* Returns the before hook
24+
*/
25+
public get before() {
26+
return this._before;
27+
}
1828

1929
/**
2030
* Get the last inserted id
@@ -23,6 +33,13 @@ export default class PGConnection implements Connection<Resource> {
2333
return undefined;
2434
}
2535

36+
/**
37+
* Sets the before hook
38+
*/
39+
public set before(before: (request: QueryObject) => Promise<void>) {
40+
this._before = before;
41+
}
42+
2643
/**
2744
* Set the connection
2845
*/
@@ -116,8 +133,13 @@ export default class PGConnection implements Connection<Resource> {
116133
* Call the database. If no values are provided, use exec
117134
*/
118135
protected async _query<R = unknown>(request: QueryObject) {
136+
//allow last minute request manipulation
137+
await this._before(request);
138+
//extract the query and values from the request
119139
const { query, values = [] } = request;
140+
//get the proprietary resource
120141
const resource = await this.resource();
142+
//query the database and return the results
121143
return (await resource.query(query, values)) as Results<R>;
122144
}
123145
}

packages/inquire-pglite/src/Connection.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ export default class PGLiteConnection implements Connection<Resource> {
1717
public readonly dialect: Dialect = Pgsql;
1818
//the database connection
1919
protected _resource: Connector;
20+
//A hook used for logging purposes. Can also manipulate the final
21+
// query before execution.
22+
protected _before = async (_request: QueryObject) => {};
23+
24+
/**
25+
* Returns the before hook
26+
*/
27+
public get before() {
28+
return this._before;
29+
}
2030

2131
/**
2232
* Get the last inserted id
@@ -25,6 +35,13 @@ export default class PGLiteConnection implements Connection<Resource> {
2535
return undefined;
2636
}
2737

38+
/**
39+
* Sets the before hook
40+
*/
41+
public set before(before: (request: QueryObject) => Promise<void>) {
42+
this._before = before;
43+
}
44+
2845
/**
2946
* Set the connection
3047
*/
@@ -122,7 +139,11 @@ export default class PGLiteConnection implements Connection<Resource> {
122139
request: QueryObject,
123140
resource: Resource|TX
124141
) {
142+
//allow last minute request manipulation
143+
await this._before(request);
144+
//extract the query and values from the request
125145
const { query, values = [] } = request;
146+
//query the database and return the results
126147
return values.length === 0
127148
? (await resource.exec(query))[0] as Results<R>
128149
: await resource.query(query, values) as Results<R>;

packages/inquire-sqlite3/src/Connection.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export default class BetterSqlite3Connection implements Connection<Resource> {
1616
protected _lastId?: number|string;
1717
//the database connection
1818
protected _resource: Connector;
19+
//A hook used for logging purposes. Can also manipulate the final
20+
// query before execution.
21+
protected _before = async (_request: QueryObject) => {};
22+
23+
/**
24+
* Returns the before hook
25+
*/
26+
public get before() {
27+
return this._before;
28+
}
1929

2030
/**
2131
* Get the last inserted id
@@ -24,6 +34,13 @@ export default class BetterSqlite3Connection implements Connection<Resource> {
2434
return this._lastId;
2535
}
2636

37+
/**
38+
* Sets the before hook
39+
*/
40+
public set before(before: (request: QueryObject) => Promise<void>) {
41+
this._before = before;
42+
}
43+
2744
/**
2845
* Set the connection
2946
*/
@@ -107,7 +124,11 @@ export default class BetterSqlite3Connection implements Connection<Resource> {
107124
* Call the database. If no values are provided, use exec
108125
*/
109126
protected async _query<R = unknown>(request: QueryObject) {
127+
//allow last minute request manipulation
128+
await this._before(request);
129+
//extract the query and values from the request
110130
const { query, values = [] } = request;
131+
//query the database and return the results
111132
const resource = await this.resource();
112133
const stmt = resource.prepare(query);
113134
const QUERY = query.toUpperCase();

packages/inquire/src/Engine.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ import { jsonCompare } from './helpers.js';
1717
export default class Engine<R = unknown> {
1818
//database connection
1919
public readonly connection: Connection<R>;
20+
//A hook used for logging purposes. Can also manipulate the final
21+
// query before execution. Can also provide the results of the query,
22+
// in which case the database will not be queried.
23+
protected _before: <R = unknown>(
24+
request: QueryObject
25+
) => Promise<R[] | void> = async (_request: QueryObject) => {};
26+
27+
/**
28+
* Returns the before hook
29+
*/
30+
public get before() {
31+
return this._before;
32+
}
2033

2134
/**
2235
* Returns sql dialect
@@ -25,6 +38,15 @@ export default class Engine<R = unknown> {
2538
return this.connection.dialect;
2639
}
2740

41+
/**
42+
* Sets the before hook
43+
*/
44+
public set before(before: <R = unknown>(
45+
request: QueryObject
46+
) => Promise<R[] | void>) {
47+
this._before = before;
48+
}
49+
2850
/**
2951
* Sets the query callback
3052
*/
@@ -183,13 +205,19 @@ export default class Engine<R = unknown> {
183205
* native database engine connection. Any code that uses
184206
* this library should not care about the kind of database.
185207
*/
186-
public query<R = unknown>(query: QueryObject): Promise<R[]>;
187-
public query<R = unknown>(query: string, values?: Value[]): Promise<R[]>;
188-
public query<R = unknown>(query: string|QueryObject, values: Value[] = []) {
208+
public async query<R = unknown>(query: QueryObject): Promise<R[]>;
209+
public async query<R = unknown>(query: string, values?: Value[]): Promise<R[]>;
210+
public async query<R = unknown>(query: string|QueryObject, values: Value[] = []) {
189211
if (typeof query === 'string') {
190212
query = { query, values };
191213
}
192-
return this.connection.query<R>(query);
214+
//allow last minute request manipulation and
215+
// possibly short circuit the query with results
216+
const results = await this._before(query);
217+
//if results were provided, return them
218+
if (results) return results;
219+
//otherwise, query the database
220+
return await this.connection.query<R>(query);
193221
}
194222

195223
/**

packages/inquire/src/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ export interface Connection<R = unknown> {
181181
//sql language dialect
182182
dialect: Dialect;
183183

184-
/**
185-
* Get the last inserted id
186-
*/
184+
//Get the last inserted id
187185
lastId: string | number | undefined;
188186

187+
//A hook used for logging purposes. Can also manipulate the final
188+
// query before execution.
189+
before: (request: QueryObject) => Promise<void>;
190+
189191
/**
190192
* Formats the query to what the database connection understands
191193
* Formats the values to what the database connection accepts

0 commit comments

Comments
 (0)