forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-mysql-wrapper.d.ts
More file actions
627 lines (506 loc) · 23.2 KB
/
node-mysql-wrapper.d.ts
File metadata and controls
627 lines (506 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Type definitions for node-mysql-wrapper
// Project: https://github.com/nodets/node-mysql-wrapper
// Definitions by: Makis Maropoulos <https://github.com/kataras>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path='./../mysql/mysql.d.ts' />
///<reference path='./../bluebird/bluebird.d.ts' />
declare module "node-mysql-wrapper" {
import * as Mysql from 'mysql';
import * as Promise from 'bluebird';
import {EventEmitter} from 'events';
var EQUAL_TO_PROPERTY_SYMBOL: string;
var TABLE_RULES_PROPERTY: string;
type DeleteAnswer = {
affectedRows: number;
table: string;
};
type RawRules = {
table: string,
begin: string,
orderBy: string,
orderByDesc: string,
groupBy: string,
limit: number, // limit = limitStart =0 and limitEnd = limit.
limitStart: number,
limitEnd: number,
end: string
};
type TableToSearchPart = { tableName: string, propertyName: string };
interface Map<T> {
[index: string]: T;
}
interface IQuery<T> {
_table: Table<T>;
execute(rawCriteria: any, callback?: (_results: any) => any): Promise<any>;
}
interface IQueryConstructor<T> {
new (_table: Table<T>): IQuery<T>;
}
class Helper {
/**
* Callback like forEach
* @name valueCallback
* @function
* @param {T} the value of the object's key
* @returnTye {U}
* @return {U}
*/
/**
* Callback like forEach
* @name keyCallback
* @function
* @param {string} the name of the object's key
* @returnTye {U}
* @return {U}
*/
constructor();
/**
* Create and return a copy of an object.
* @param {T} object the object you want to copy.
* @returnType {T}
* @return {T}
*/
static copyObject<T>(object: T): T;
/**
* Converts any_string to anyString and returns it.
* @param {string} columnKey the string you want to convert.
* @returnType {string}
* @return {string}
*/
static toObjectProperty(columnKey: string): string;
/**
* Converts anyString to any_string and returns it.
* @param {string} objectKey the string you want to convert.
* @returnType {string}
* @return {string}
*/
static toRowProperty(objectKey: string): string;
/**
* Iterate object's keys and return their values to the callback.
* @param {Map<T>} map the object.
* @param {valueCallback}
* @returnType {U}
* @return {U}
*/
static forEachValue<T, U>(map: Map<T>, callback: (value: T) => U): U;
/**
* Iterate object's keys and return their names to the callback.
* @param {Map<T>} map the object.
* @param {keyCallback}
* @returnType {U}
* @return {U}
*/
static forEachKey<T, U>(map: Map<T>, callback: (key: string) => U): U;
/**
* Checks if anything is a function.
* @param {functionToCheck} the object or function to pass
* @return boolean
*/
static isFunction(functionToCheck: any): boolean;
/**
* Checks if an object has 'tableRules' property.
* @param {obj} the object to pass
* @return boolean
*/
static hasRules(obj: any): boolean;
}
interface ICriteriaParts {
rawCriteriaObject: any;
tables: TableToSearchPart[];
noDatabaseProperties: string[];
whereClause: string;
selectFromClause<T>(_table: Table<T>): string;
}
class CriteriaParts implements ICriteriaParts {
/**
* The raw format of the criteria eg: {yearsOld:22}.
*/
rawCriteriaObject: any;
/**
* Which tables to search after the find method of the proto table finish.
*/
tables: TableToSearchPart[];
/**
* The properties of the criteria which don't belong to the database's table.
*/
noDatabaseProperties: string[];
/**
* The converted/exported where clause.
*/
whereClause: string;
constructor(rawCriteriaObject: any, tables: TableToSearchPart[], noDatabaseProperties: string[], whereClause: string);
selectFromClause<T>(_table: Table<T>): string;
}
class CriteriaDivider<T> {
private _table;
constructor(table: Table<T>);
/**
* Builds the criteria raw object to Criteria object.
* @param {any} rawCriteriaObject the criteria at raw format you pass eg: {yearsOld:18}.
* @returnType {Criteria}
* @return {Criteria}
*/
build(rawCriteriaObject: any): CriteriaParts;
}
class SelectQueryRules {
private lastPropertyClauseName: string;
manuallyEndClause: string;
manuallyBeginClause: string;
exceptColumns: string[];
orderByColumn: string;
orderByDescColumn: string;
groupByColumn: string;
limitStart: number;
limitEnd: number;
public tableName: string; //auto den benei oute sto last, oute sto from.
static build(): SelectQueryRules;
private last(propertyClauseName);
except(...columns: string[]): SelectQueryRules;
/**
* Same as .except(...columns)
*/
exclude(...columns: string[]): SelectQueryRules;
orderBy(columnKey: string, descending?: boolean): SelectQueryRules;
groupBy(columnKey: string): SelectQueryRules;
limit(limitRowsOrStart: number, limitEnd?: number): SelectQueryRules;
appendToBegin(manualAfterWhereString: string): SelectQueryRules;
appendToEnd(manualAfterWhereString: string): SelectQueryRules;
append(appendToCurrent: string): SelectQueryRules;
clearOrderBy(): SelectQueryRules;
clearGroupBy(): SelectQueryRules;
clearLimit(): SelectQueryRules;
clearEndClause(): SelectQueryRules;
clearBeginClause(): SelectQueryRules;
clear(): SelectQueryRules;
from(parentRule: SelectQueryRules): SelectQueryRules;
isEmpty(): boolean;
toString(): string;
toRawObject(): RawRules;
static toString(rules: SelectQueryRules): string;
static toRawObject(rules: SelectQueryRules): RawRules;
static fromRawObject(obj: RawRules): SelectQueryRules;
}
class CriteriaBuilder<T>{
private rawCriteria: any;
private primaryTable: Table<T>;
private parentBuilder: CriteriaBuilder<any>;
constructor(primaryTable: Table<T>); //to arxiko apo to Table.ts 9a benei
constructor(primaryTable: Table<T>, tableName: string, parentBuilder: CriteriaBuilder<any>);// auta 9a benoun apo to parent select query.
constructor(primaryTable: Table<T>, tablePropertyName?: string, parentBuilder?: CriteriaBuilder<any>);
except(...columns: string[]): CriteriaBuilder<T>;
/**
* Same as .except(...columns)
*/
exclude(...columns: string[]): CriteriaBuilder<T>;
where(key: string, value: any): CriteriaBuilder<T>;
private createRulesIfNotExists(): void;
orderBy(column: string, desceding?: boolean): CriteriaBuilder<T>;
limit(start: number, end?: number): CriteriaBuilder<T>;
join(realTableName: string, foreignColumnName: string): CriteriaBuilder<T>;
joinAs(tableNameProperty: string, realTableName: string, foreignColumnName: string): CriteriaBuilder<T>;
at(tableNameProperty: string): CriteriaBuilder<T>;
parent(): CriteriaBuilder<T>;
original(): CriteriaBuilder<T>;
/**
* Auto kanei kuklous mexri na paei sto primary table kai ekei na epistrepsei to sunoliko raw criteria gia execute i kati allo.
*/
build(): any;
static from<T>(table: Table<T>): CriteriaBuilder<T>
}
class SelectQuery<T> implements IQuery<T> { // T for Table's result type.
_table: Table<T>
constructor(_table: Table<T>);
private parseQueryResult(result: any, criteria: ICriteriaParts): Promise<any>;
/**
* Executes the select and returns the Promise.
*/
promise(rawCriteria: any, callback?: (_results: T[]) => any): Promise<T[]>;
/**
* Exactly the same thing as promise().
* Executes the select and returns the Promise.
*/
execute(rawCriteria: any, callback?: (_results: T[]) => any): Promise<T[]>;
}
class SaveQuery<T> implements IQuery<T> {
_table: Table<T>
constructor(_table: Table<T>);
execute(criteriaRawJsObject: any, callback?: (_result: T | any) => any): Promise<T | any>;
}
class DeleteQuery<T> implements IQuery<T>{
_table: Table<T>
constructor(_table: Table<T>);
execute(criteriaOrID: any | number | string, callback?: (_result: DeleteAnswer) => any): Promise<DeleteAnswer>;
}
class Connection extends EventEmitter {
/**
* The real database connection socket.
*/
connection: Mysql.IConnection;
/**
* Collection of the supported event types for the tables.
*/
eventTypes: string[];
/**
* Force to fetch ONLY these Database table names {array of string}.
*/
tableNamesToUseOnly: any[];
/**
* All tables {MysqlTable} inside this connection's database.
*/
tables: Table<any>[];
constructor(connection: string | Mysql.IConnection);
/**
* Creates the MysqlConnection from the connection url or the real connection object.
* @param {string | Mysql.IConnection} connection the connection url or the real connection object.
* @returnType {nothing}
* @return {nothing}
*/
create(connection: string | Mysql.IConnection): void;
/**
* Attach a real connection.
* @param {Mysql.IConnection} connection the real connection object.
* @returnType {nothing}
* @return {nothing}
*/
attach(connection: Mysql.IConnection): void;
/**
* Close the entire real connection and remove all event's listeners (if exist).
* @param {function} callback If error occurs when closing the connection, this callback has the responsibility to catch it.
* @returnType {nothing}
* @return {nothing}
*/
end(callback?: (error: any) => void): void;
/**
* Close the entire real connection and remove all event's listeners (if exist).
* the difference from the 'end' is that this method doesn't care about errors so no callback passing here.
*/
destroy(): void;
/**
* Link the real connection with this MysqlConnection object.
* @param {function} readyCallback when the link operation is done this callback is executed.
* @returnType {Promise}
* @return {Promise}
*/
link(readyCallback?: () => void): Promise<void>;
/**
* Force to use/fetch information from only certain of database's tables, otherwise all database's tables information will be fetched.
* @param {Array} tables the array of the tables {string}
* @returnType {nothing}
* @return {nothing}
*/
useOnly(...tables: any[]): void;
/**
* This method has the resposibility of fetching the correct tables from the database ( table = columns' names, primary key name).
* @returnType {Promise}
* @return {Promise}
*/
fetchDatabaseInfornation(): Promise<void>;
/**
* Escape the query column's value and return it.
* @param {string} val the value which will be escaped.
* @returnType {string}
* @return {string}
*/
escape(val: string): string;
/**
* Call when must notify the Database events, SAVE(INSERT,UPDATE), REMOVE(DELETE).
* @param {string} tableWhichCalled the table name which event is coming from.
* @param {string} queryStr the full parsed query string which used to determinate the type of event to notify.
* @param {any[]} parsedResults the parsed results (results after a method parse/edit/export them as objects), these are passing to the watch listener(s).
* @returnType {nothing}
* @return {nothing}
*/
notice(tableWhichCalled: string, queryStr: string, parsedResults: any[]): void;
/**
* Adds an event listener/watcher on a table for a 'database event'.
* @param {string} tableName the table name which you want to add the event listener.
* @param {string or string[]} evtType the event(s) type you want to watch, one of these(string) or an array of them(string[]): ["INSERT", "UPDATE", "REMOVE", "SAVE"].
* @param {function} callback Callback which has one parameter(typeof any[]) which filled by the parsedResults (results after query executed and exports to object(s)).
* @returnType {nothing}
* @return {nothing}
*/
watch(tableName: string, evtType: any, callback: (parsedResults: any[]) => void): void;
/**
* Removes an event listener/watcher from a table for a specific event type.
* @param {string} tableName the table name which you want to remove the event listener.
* @param {string} evtType the Event type you want to remove, one of these: "INSERT", "UPDATE", "REMOVE", "SAVE".
* @param {function} callbackToRemove the callback that you were used for watch this event type.
* @returnType {nothing}
* @return {nothing}
*/
unwatch(tableName: string, evtType: string, callbackToRemove: (parsedResults: any[]) => void): void;
/**
* Executes a database query.
* @param {string} queryStr the query text/string to be executed.
* @param {function} callback the function will be called and fill the one and only parameter when an errors occurs.
* @param {any[]} queryArguments (optional) the query arguments you want to pass into query. ['arg1','arg2']...
* @returnType {nothing}
* @return {nothing}
*/
query(queryStr: string, callback: (err: Mysql.IError, results: any) => any, queryArguments?: any[]): void;
/**
* Returns a MysqlTable object from the database factory. (Note: this method doesn't create anything, just finds and returns the correct table, you don't have to create anything at all. Tables are fetched by the library itself.)
* If you are using typescript you can pass a class (generic<T>) in order to use the auto completion assistance on table's results methods(find,findById,findAll,save,remove,safeRemove).
* @param {string} tableName the table name which you want to get, on the form of: 'anyDatabaseTable' OR 'any_database_table' (possible your real table name into your database).
* @returnType {MysqlTable}
* @return {MysqlTable}
*/
table<T>(tableName: string): Table<T>;
}
class Table<T> {
private _name: string;
private _connection: Connection;
private _columns: string[];
private _primaryKey: string;
private _criteriaDivider: CriteriaDivider<T>;
private _rules: SelectQueryRules;
private _selectQuery: SelectQuery<T>
private _saveQuery: SaveQuery<T>;
private _deleteQuery: DeleteQuery<T>;
constructor(tableName: string, connection: Connection);
/**
* An array of all columns' names inside this table.
*/
columns: string[];
/**
* The name of the primary key column which this table is using.
*/
primaryKey: string;
/**
* The MysqlConnection object which this MysqlTable belongs.
*/
connection: Connection;
/**
* The real database name of the table. Autofilled by library.
*/
name: string;
/**
* Set of the query rules that will be applied after the 'where clause' on each select query executed by this table.
* @return {SelectQueryRules}
*/
rules: SelectQueryRules;
/**
* Returns this table's criteria divider class.
* @return {CriteriaDivider}
*/
criteriaDivider: CriteriaDivider<T>;
/**
* Returns new Criteria Builder each time.
* Helps you to make criteria raw js objects ready to use in find,remove and save methods.
* @return {CriteriaBuilder}
*/
criteria: CriteriaBuilder<T>;
/**
* Adds or turn on an event listener/watcher on a table for a 'database event'.
* @param {string} evtType the event type you want to watch, one of these: ["INSERT", "UPDATE", "REMOVE", "SAVE"].
* @param {function} callback Callback which has one parameter(typeof any[]) which filled by the parsedResults (results after query executed and exports to object(s)).
* @returnType {nothing}
* @return {nothing}
*/
on(evtType: string, callback: (parsedResults: any[]) => void): void;
/**
* Removes or turn off an event listener/watcher from a table for a specific event type.
* @param {string} evtType the Event type you want to remove, one of these: "INSERT", "UPDATE", "REMOVE", "SAVE".
* @param {function} callbackToRemove the callback that you were used for watch this event type.
* @returnType {nothing}
* @return {nothing}
*/
off(evtType: string, callbackToRemove: (parsedResults: any[]) => void): void;
/**
* Use it when you want to check if extended function is exists here.
* @param {string} extendedFunctionName the name of the function you want to check.
* @returnType {boolean}
* @return {boolean}
*/
has(extendedFunctionName: string): boolean;
/**
* Extends this table's capabilities with a function.
* @param {string} functionName the function name you want to use, this is used when you want to call this function later.
* @param {function} theFunction the function with any optional parameters you want to pass along.
* @returnType {nothing}
* @return {nothing}
*/
extend(functionName: string, theFunction: (...args: any[]) => any): void;
/**
* Converts and returns an object from this form: { a_property:'dsda', other_property:something, any_property_name:true } to { aProperty:..., otherProperty...,anyPropertyName...}
* @param {any} row the raw row object.
* @returnType {any}
* @return {any}
*/
objectFromRow(row: any): any;
/**
* Converts and returns an object from this form: { aProperty:'dsda', otherProperty:something, anyPropertyName:true } to { a_property:..., other_property...,any_property_name...}
* @param {any} row the raw row object.
* @returnType {any}
* @return {any}
*/
rowFromObject(obj: any): any;
/**
* Returns and array of [columns[],values[]]
* @param {any} jsObject the raw row object.
* @returnType {array}
* @return {array}
*/
getRowAsArray(jsObject: any): Array<any>;
/**
* Returns the primary key's value from an object.
* @param {any} jsObject the object which you want to find and return the value of the primary key.
* @returnType {number | string}
* @return {number | string}
*/
getPrimaryKeyValue(jsObject: any): number | string;
/**
*
*/
find(criteriaRawJsObject: any): Promise<T[]>; // only criteria
find(criteriaRawJsObject: any, callback: ((_results: T[]) => any)): Promise<T[]>; // criteria and callback
find(criteriaRawJsObject: any, callback?: (_results: T[]) => any): Promise<T[]>;
findSingle(criteriaRawJsObject: any, callback?: (_result: T) => any): Promise<T>;
findById(id: number|string): Promise<T>; // without callback
findById(id: number | string, callback?: (result: T) => any): Promise<T>;
findAll(): Promise<T[]>; // only criteria and promise
findAll(tableRules: RawRules): Promise<T[]> // only rules and promise
findAll(tableRules?: RawRules, callback?: (_results: T[]) => any): Promise<T[]>;
save(criteriaRawJsObject: any): Promise<any>; //without callback
save(criteriaRawJsObject: any, callback?: (_result: any) => any): Promise<any>;
remove(id: number | string): Promise<DeleteAnswer>; // ID without callback
remove(criteriaRawObject: any): Promise<DeleteAnswer>; // criteria obj without callback
remove(criteriaOrID: any | number | string, callback?: (_result: DeleteAnswer) => any): Promise<DeleteAnswer>;
}
class Wrapper {
connection: Connection;
readyListenerCallbacks: Function[];
constructor(connection?: Connection);
static when(..._promises: Promise<any>[]): Promise<any>;
setConnection(connection: Connection): void;
/**
* Force to use/fetch information from only certain of database's tables, otherwise all database's tables information will be fetched.
* @param {Array} tables the array of the tables {string}
* @returnType {nothing}
* @return {nothing}
*/
useOnly(...useTables: any[]): void;
has(tableName: string, functionName?: string): boolean;
ready(callback: () => void): void;
table<T>(tableName: string): Table<T>;
noticeReady(): void;
removeReadyListener(callback: () => void): void;
query(queryStr: string, callback: (err: Mysql.IError, results: any) => any, queryArguments?: any[]): void;
/**
* Close the entire real connection and remove all event's listeners (if exist).
* the difference from the 'end' is that this method doesn't care about errors so no callback passing here.
*/
destroy(): void;
/**
* Close the entire real connection and remove all event's listeners (if exist).
* @param {function} maybeAcallbackError If error occurs when closing the connection, this callback has the responsibility to catch it.
* @returnType {nothing}
* @return {nothing}
*/
end(maybeAcallbackError: (err: any) => void): void;
newTableRules(tableName: string): SelectQueryRules;
buildRules(): SelectQueryRules;
buildRules(parentRules?: SelectQueryRules): SelectQueryRules;
}
function wrap(mysqlUrlOrObjectOrMysqlAlreadyConnection: Mysql.IConnection | string, ...useTables: any[]): Wrapper;
}