@dbcube/query-builder 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/.npmignore +51 -0
- package/CONTRIBUTING.md +9 -0
- package/LICENSE +21 -0
- package/README.md +134 -0
- package/dist/index.cjs +923 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +391 -0
- package/dist/index.d.ts +391 -0
- package/dist/index.js +892 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
- package/tsup.config.ts +14 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
type WhereCallback = (query: Table) => void;
|
|
2
|
+
type DatabaseRecord = Record<string, any>;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Main class to handle MySQL database connections and queries.
|
|
6
|
+
* Implements the Singleton pattern to ensure a single instance of the connection pool.
|
|
7
|
+
*/
|
|
8
|
+
declare class Database {
|
|
9
|
+
private name;
|
|
10
|
+
private engine;
|
|
11
|
+
private computedFields;
|
|
12
|
+
private triggers;
|
|
13
|
+
constructor(name: string);
|
|
14
|
+
useComputes(): Promise<void>;
|
|
15
|
+
useTriggers(): Promise<void>;
|
|
16
|
+
connect(): Promise<void>;
|
|
17
|
+
disconnect(): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Creates and returns a new instance of `Table` for the specified table.
|
|
20
|
+
* This method is used to start building queries for a specific table.
|
|
21
|
+
* It provides a fluent interface for common database operations like select, insert, update, and delete.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} tableName - The name of the table to query.
|
|
24
|
+
* @returns {Table} - Returns a new instance of `Table` for the specified table.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Select all records from a table
|
|
28
|
+
* const users = await db.table('users').get();
|
|
29
|
+
*
|
|
30
|
+
* // Select records with conditions
|
|
31
|
+
* const activeUsers = await db.table('users')
|
|
32
|
+
* .where('status', '=', 'active')
|
|
33
|
+
* .orderBy('created_at', 'DESC')
|
|
34
|
+
* .limit(10)
|
|
35
|
+
* .get();
|
|
36
|
+
*
|
|
37
|
+
* // Insert records
|
|
38
|
+
* await db.table('users').insert([
|
|
39
|
+
* { name: 'John', email: 'john@example.com', age: 30 }
|
|
40
|
+
* ]);
|
|
41
|
+
*
|
|
42
|
+
* // Update records
|
|
43
|
+
* await db.table('users')
|
|
44
|
+
* .where('id', '=', 1)
|
|
45
|
+
* .update({ status: 'inactive' });
|
|
46
|
+
*
|
|
47
|
+
* // Delete records
|
|
48
|
+
* await db.table('users')
|
|
49
|
+
* .where('status', '=', 'deleted')
|
|
50
|
+
* .delete();
|
|
51
|
+
*
|
|
52
|
+
* // Access column management
|
|
53
|
+
* const columns = await db.table('users').columns().get();
|
|
54
|
+
*/
|
|
55
|
+
table(tableName: string): Table;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Class to build and execute SQL queries for a specific table.
|
|
59
|
+
* Supports operations like SELECT, INSERT, UPDATE, DELETE, and more.
|
|
60
|
+
*/
|
|
61
|
+
declare class Table {
|
|
62
|
+
private engine;
|
|
63
|
+
private nextType;
|
|
64
|
+
private dml;
|
|
65
|
+
private computedFields;
|
|
66
|
+
private trigger;
|
|
67
|
+
private triggers;
|
|
68
|
+
constructor(instance: any, databaseName: string, tableName: string, engine?: any, computedFields?: any[], triggers?: any[]);
|
|
69
|
+
/**
|
|
70
|
+
* Specifies the columns to select in a SELECT query.
|
|
71
|
+
*
|
|
72
|
+
* @param {string[]} fields - Array of column names to select. If empty, selects all columns.
|
|
73
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* const users = await db.table('users').select(['id', 'name']).get();
|
|
77
|
+
* console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
|
|
78
|
+
*/
|
|
79
|
+
select(fields?: string[]): Table;
|
|
80
|
+
/**
|
|
81
|
+
* Adds a WHERE condition to the query.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} column - The column to filter by.
|
|
84
|
+
* @param {string} operator - The comparison operator (e.g., '=', '>', '<').
|
|
85
|
+
* @param {any} value - The value to compare against.
|
|
86
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* const users = await db.table('users').where('age', '>', 25).get();
|
|
90
|
+
* console.log(users); // [{ id: 1, name: 'John', age: 30 }]
|
|
91
|
+
*/
|
|
92
|
+
where(column: string, operator?: string, value?: any): Table;
|
|
93
|
+
/**
|
|
94
|
+
* Adds an OR WHERE condition to the query.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} column - The column to filter by.
|
|
97
|
+
* @param {string} operator - The comparison operator (e.g., '=', '>', '<').
|
|
98
|
+
* @param {any} value - The value to compare against.
|
|
99
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* const users = await db.table('users').where('age', '>', 25).orWhere('name', '=', 'Jane').get();
|
|
103
|
+
* console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
|
|
104
|
+
*/
|
|
105
|
+
orWhere(column: string, operator?: string, value?: any): Table;
|
|
106
|
+
/**
|
|
107
|
+
* Adds a grouped WHERE condition to the query.
|
|
108
|
+
*
|
|
109
|
+
* @param {WhereCallback} callback - A callback function that receives a new Table instance to build the grouped conditions.
|
|
110
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* const users = await db.table('users').whereGroup(query => {
|
|
114
|
+
* query.where('age', '>', 25).orWhere('name', '=', 'Jane');
|
|
115
|
+
* }).get();
|
|
116
|
+
* console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
|
|
117
|
+
*/
|
|
118
|
+
whereGroup(callback: WhereCallback): Table;
|
|
119
|
+
or(): Table;
|
|
120
|
+
and(): Table;
|
|
121
|
+
/**
|
|
122
|
+
* Adds a WHERE BETWEEN condition to the query.
|
|
123
|
+
*
|
|
124
|
+
* @param {string} column - The column to filter by.
|
|
125
|
+
* @param {[any, any]} values - A tuple with two values representing the range.
|
|
126
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* const users = await db.table('users').whereBetween('age', [20, 30]).get();
|
|
130
|
+
* console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
|
|
131
|
+
*/
|
|
132
|
+
whereBetween(column: string, values: [any, any]): Table;
|
|
133
|
+
/**
|
|
134
|
+
* Adds a WHERE IN condition to the query.
|
|
135
|
+
*
|
|
136
|
+
* @param {string} column - The column to filter by.
|
|
137
|
+
* @param {any[]} values - An array of values to match.
|
|
138
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* const users = await db.table('users').whereIn('id', [1, 2]).get();
|
|
142
|
+
* console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
|
|
143
|
+
*/
|
|
144
|
+
whereIn(column: string, values: any[]): Table;
|
|
145
|
+
/**
|
|
146
|
+
* Adds a WHERE IS NULL condition to the query.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} column - The column to filter by.
|
|
149
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* const users = await db.table('users').whereNull('email').get();
|
|
153
|
+
* console.log(users); // [{ id: 3, name: 'Alice', email: null }]
|
|
154
|
+
*/
|
|
155
|
+
whereNull(column: string): Table;
|
|
156
|
+
/**
|
|
157
|
+
* Adds a WHERE IS NOT NULL condition to the query.
|
|
158
|
+
*
|
|
159
|
+
* @param {string} column - The column to filter by.
|
|
160
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* const users = await db.table('users').whereNotNull('email').get();
|
|
164
|
+
* console.log(users); // [{ id: 1, name: 'John', email: 'john@example.com' }]
|
|
165
|
+
*/
|
|
166
|
+
whereNotNull(column: string): Table;
|
|
167
|
+
/**
|
|
168
|
+
* Adds a JOIN clause to the query.
|
|
169
|
+
*
|
|
170
|
+
* @param {string} table - The table to join.
|
|
171
|
+
* @param {string} column1 - The column from the current table.
|
|
172
|
+
* @param {string} operator - The comparison operator (e.g., '=', '>', '<').
|
|
173
|
+
* @param {string} column2 - The column from the joined table.
|
|
174
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* const users = await db.table('users').join('orders', 'users.id', '=', 'orders.user_id').get();
|
|
178
|
+
* console.log(users); // [{ id: 1, name: 'John', order_id: 101 }]
|
|
179
|
+
*/
|
|
180
|
+
join(table: string, column1: string, operator: string, column2: string): Table;
|
|
181
|
+
/**
|
|
182
|
+
* Adds a LEFT JOIN clause to the query.
|
|
183
|
+
*
|
|
184
|
+
* @param {string} table - The table to join.
|
|
185
|
+
* @param {string} column1 - The column from the current table.
|
|
186
|
+
* @param {string} operator - The comparison operator (e.g., '=', '>', '<').
|
|
187
|
+
* @param {string} column2 - The column from the joined table.
|
|
188
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* const users = await db.table('users').leftJoin('orders', 'users.id', '=', 'orders.user_id').get();
|
|
192
|
+
* console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: 2, name: 'Jane', order_id: null }]
|
|
193
|
+
*/
|
|
194
|
+
leftJoin(table: string, column1: string, operator: string, column2: string): Table;
|
|
195
|
+
/**
|
|
196
|
+
* Adds a RIGHT JOIN clause to the query.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} table - The table to join.
|
|
199
|
+
* @param {string} column1 - The column from the current table.
|
|
200
|
+
* @param {string} operator - The comparison operator (e.g., '=', '>', '<').
|
|
201
|
+
* @param {string} column2 - The column from the joined table.
|
|
202
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* const users = await db.table('users').rightJoin('orders', 'users.id', '=', 'orders.user_id').get();
|
|
206
|
+
* console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: null, name: null, order_id: 102 }]
|
|
207
|
+
*/
|
|
208
|
+
rightJoin(table: string, column1: string, operator: string, column2: string): Table;
|
|
209
|
+
/**
|
|
210
|
+
* Adds an ORDER BY clause to the query.
|
|
211
|
+
*
|
|
212
|
+
* @param {string} column - The column to order by.
|
|
213
|
+
* @param {'ASC' | 'DESC'} direction - The sorting direction ('ASC' or 'DESC').
|
|
214
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* const users = await db.table('users').orderBy('name', 'ASC').get();
|
|
218
|
+
* console.log(users); // [{ id: 2, name: 'Jane' }, { id: 1, name: 'John' }]
|
|
219
|
+
*/
|
|
220
|
+
orderBy(column: string, direction?: 'ASC' | 'DESC'): Table;
|
|
221
|
+
/**
|
|
222
|
+
* Adds a GROUP BY clause to the query.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} column - The column to group by.
|
|
225
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* const users = await db.table('users').groupBy('age').get();
|
|
229
|
+
* console.log(users); // [{ age: 30, count: 1 }, { age: 25, count: 1 }]
|
|
230
|
+
*/
|
|
231
|
+
groupBy(column: string): Table;
|
|
232
|
+
/**
|
|
233
|
+
* Adds a DISTINCT clause to the query.
|
|
234
|
+
*
|
|
235
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* const users = await db.table('users').distinct().select(['name']).get();
|
|
239
|
+
* console.log(users); // [{ name: 'John' }, { name: 'Jane' }]
|
|
240
|
+
*/
|
|
241
|
+
distinct(): Table;
|
|
242
|
+
/**
|
|
243
|
+
* Adds a COUNT clause to the query.
|
|
244
|
+
*
|
|
245
|
+
* @param {string} column - The column to count (default is '*').
|
|
246
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* const count = await db.table('users').count().first();
|
|
250
|
+
* console.log(count); // { count: 2 }
|
|
251
|
+
*/
|
|
252
|
+
count(column?: string): Table;
|
|
253
|
+
/**
|
|
254
|
+
* Adds a SUM clause to the query.
|
|
255
|
+
*
|
|
256
|
+
* @param {string} column - The column to sum.
|
|
257
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* const totalAge = await db.table('users').sum('age').first();
|
|
261
|
+
* console.log(totalAge); // { sum: 55 }
|
|
262
|
+
*/
|
|
263
|
+
sum(column: string): Table;
|
|
264
|
+
/**
|
|
265
|
+
* Adds an AVG clause to the query.
|
|
266
|
+
*
|
|
267
|
+
* @param {string} column - The column to calculate the average.
|
|
268
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* const avgAge = await db.table('users').avg('age').first();
|
|
272
|
+
* console.log(avgAge); // { avg: 27.5 }
|
|
273
|
+
*/
|
|
274
|
+
avg(column: string): Table;
|
|
275
|
+
/**
|
|
276
|
+
* Adds a MAX clause to the query.
|
|
277
|
+
*
|
|
278
|
+
* @param {string} column - The column to find the maximum value.
|
|
279
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* const maxAge = await db.table('users').max('age').first();
|
|
283
|
+
* console.log(maxAge); // { max: 30 }
|
|
284
|
+
*/
|
|
285
|
+
max(column: string): Table;
|
|
286
|
+
/**
|
|
287
|
+
* Adds a MIN clause to the query.
|
|
288
|
+
*
|
|
289
|
+
* @param {string} column - The column to find the minimum value.
|
|
290
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* const minAge = await db.table('users').min('age').first();
|
|
294
|
+
* console.log(minAge); // { min: 25 }
|
|
295
|
+
*/
|
|
296
|
+
min(column: string): Table;
|
|
297
|
+
/**
|
|
298
|
+
* Adds a LIMIT clause to the query.
|
|
299
|
+
*
|
|
300
|
+
* @param {number} number - The maximum number of rows to return.
|
|
301
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* const users = await db.table('users').limit(1).get();
|
|
305
|
+
* console.log(users); // [{ id: 1, name: 'John', age: 30 }]
|
|
306
|
+
*/
|
|
307
|
+
limit(number: number): Table;
|
|
308
|
+
/**
|
|
309
|
+
* Adds pagination to the query using LIMIT and OFFSET.
|
|
310
|
+
*
|
|
311
|
+
* @param {number} number - The page number (starting from 1).
|
|
312
|
+
* @returns {Table} - Returns the current instance of Table for method chaining.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* const users = await db.table('users').limit(1).page(2).get();
|
|
316
|
+
* console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
|
|
317
|
+
*/
|
|
318
|
+
page(number: number): Table;
|
|
319
|
+
/**
|
|
320
|
+
* Executes the query and returns all matching rows.
|
|
321
|
+
*
|
|
322
|
+
* @returns {Promise<DatabaseRecord[]>} - Returns an array of rows.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* const users = await db.table('users').get();
|
|
326
|
+
* console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
|
|
327
|
+
*/
|
|
328
|
+
get(): Promise<DatabaseRecord[]>;
|
|
329
|
+
/**
|
|
330
|
+
* Executes the query and returns the first matching row.
|
|
331
|
+
*
|
|
332
|
+
* @returns {Promise<DatabaseRecord | null>} - Returns the first row or null if no rows match.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* const user = await db.table('users').first();
|
|
336
|
+
* console.log(user); // { id: 1, name: 'John' }
|
|
337
|
+
*/
|
|
338
|
+
first(): Promise<DatabaseRecord | null>;
|
|
339
|
+
/**
|
|
340
|
+
* Finds a row by a specific column value.
|
|
341
|
+
*
|
|
342
|
+
* @param {any} value - The value to search for.
|
|
343
|
+
* @param {string} column - The column to search in (default is 'id').
|
|
344
|
+
* @returns {Promise<DatabaseRecord | null>} - Returns the first matching row or null if no rows match.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* const user = await db.table('users').find(1);
|
|
348
|
+
* console.log(user); // { id: 1, name: 'John' }
|
|
349
|
+
*/
|
|
350
|
+
find(value: any, column?: string): Promise<DatabaseRecord | null>;
|
|
351
|
+
/**
|
|
352
|
+
* Inserts one or more rows into the table.
|
|
353
|
+
*
|
|
354
|
+
* @param {DatabaseRecord[]} data - An array of objects representing the rows to insert.
|
|
355
|
+
* @returns {Promise<DatabaseRecord[]>} - Returns an array of the inserted rows.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* const newUsers = await db.table('users').insert([
|
|
359
|
+
* { name: 'Alice', age: 28 },
|
|
360
|
+
* { name: 'Bob', age: 32 }
|
|
361
|
+
* ]);
|
|
362
|
+
* console.log(newUsers); // [{ id: 3, name: 'Alice', age: 28 }, { id: 4, name: 'Bob', age: 32 }]
|
|
363
|
+
*/
|
|
364
|
+
insert(data: DatabaseRecord[]): Promise<DatabaseRecord[]>;
|
|
365
|
+
/**
|
|
366
|
+
* Updates rows in the table based on the defined conditions.
|
|
367
|
+
*
|
|
368
|
+
* @param {DatabaseRecord} data - An object with key-value pairs representing the fields to update.
|
|
369
|
+
* @returns {Promise<any>} - Returns the result of the update operation.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* const result = await db.table('users')
|
|
373
|
+
* .where('id', '=', 1)
|
|
374
|
+
* .update({ name: 'John Updated', age: 31 });
|
|
375
|
+
* console.log(result); // { affectedRows: 1 }
|
|
376
|
+
*/
|
|
377
|
+
update(data: DatabaseRecord): Promise<any>;
|
|
378
|
+
/**
|
|
379
|
+
* Deletes rows from the table based on the defined conditions.
|
|
380
|
+
*
|
|
381
|
+
* @returns {Promise<any>} - Returns the result of the delete operation.
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* const result = await db.table('users').where('id', '=', 1).delete();
|
|
385
|
+
* console.log(result); // { affectedRows: 1 }
|
|
386
|
+
*/
|
|
387
|
+
delete(): Promise<any>;
|
|
388
|
+
private getResponse;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export { Database, Table, Database as default };
|