forked from dresende/node-orm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorm.d.ts
More file actions
275 lines (232 loc) · 10.9 KB
/
orm.d.ts
File metadata and controls
275 lines (232 loc) · 10.9 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
/// <reference path="sql-query.d.ts" />
declare module "orm" {
import events = require('events');
import sqlquery = require('sqlquery');
module orm {
/**
* Parameter Type Interfaces
**/
export interface Model {
(): Instance;
(...ids: any[]): Instance;
properties: { [property: string]: Property };
settings: Settings;
drop(callback?: (err: Error) => void): Model;
sync(callback?: (err: Error) => void): Model;
get(...args: any[]): Model;
find(conditions: { [property: string]: any }, callback: (err: Error, results: Instance[]) => void): Model;
find(conditions: { [property: string]: any }, options: {
limit?: number;
order?: any;
}, callback: (err: Error, results: Instance[]) => void): Model;
find(conditions: { [property: string]: any }, limit: number, order: string[], callback: (err: Error, results: Instance[]) => void): Model;
find(conditions: { [property: string]: any }): IChainFind;
all(conditions: { [property: string]: any }, callback: (err: Error, results: Instance[]) => void): Model;
all(conditions: { [property: string]: any }, options: {
limit?: number;
order?: any;
}, callback: (err: Error, results: Instance[]) => void): Model;
all(conditions: { [property: string]: any }, limit: number, order: string[], callback: (err: Error, results: Instance[]) => void): Model;
one(conditions: { [property: string]: any }, callback: (err: Error, result: Instance) => void): Model;
one(conditions: { [property: string]: any }, options: {
limit?: number;
order?: any;
}, callback: (err: Error, result: Instance) => void): Model;
one(conditions: { [property: string]: any }, limit: number, order: string[], callback: (err: Error, result: Instance) => void): Model;
count(callback: (err: Error, count: number) => void): Model;
count(conditions: { [property: string]: any }, callback: (err: Error, count: number) => void): Model;
aggregate(conditions: { [property: string]: any }): IAggregated;
aggregate(properties: string[]): IAggregated;
aggregate(conditions: { [property: string]: any }, properties: string[]): IAggregated;
exists(id: any, callback: (err: Error, exists: boolean) => void): Model;
exists(...args: any[]): Model;
create(data: { [property: string]: any; }, callback: (err: Error, instance: Instance) => void): Model;
create(...args: any[]): Model;
clear(): Model;
table: string;
id: string[];
[property: string]: any;
}
export interface Instance {
on(event: string, callback): Instance;
save(): Instance;
save(data: { [property: string]: any; }, callback: (err: Error) => void): Instance;
save(data: { [property: string]: any; }, options: any, callback: (err: Error) => void): Instance;
saved: boolean;
remove(callback: (err: Error) => void): Instance;
isInstance: boolean;
isPersisted: boolean;
isShell: boolean;
validate(callback: (errors: Error[]) => void);
model: Model;
[property: string]: any;
}
export interface ModelOptions {
id?: string[];
autoFetch?: boolean;
autoFetchLimit?: number;
cacheFetch?: boolean;
hooks?: { [property: string]: Hooks };
methods?: { [name: string]: Function };
}
export interface Hooks {
beforeValidation?: (next?) => void;
beforeCreate?: (next?) => void;
afterCreate?: (next?) => void;
beforeSave?: (next?) => void;
afterSave?: (next?) => void;
afterLoad?: (next?) => void;
afterAutoFetch?: (next?) => void;
beforeRemove?: (next?) => void;
afterRemove?: (next?) => void;
}
export interface IConnectionOptions {
protocol: string;
host?: string;
port?: number;
auth?: string;
username?: string;
password?: string;
database?: string;
pool?: boolean;
debug?: boolean;
}
export interface IAggregated {
groupBy(...columns: string[]): IAggregated;
limit(limit: number): IAggregated;
limit(offset: number, limit: number): IAggregated;
order(...order: string[]): IAggregated;
select(columns: string[]): IAggregated;
select(...columns: string[]): IAggregated;
as(alias: string): IAggregated;
call(fun: string, args: any[]): IAggregated;
get(callback: (err: Error, instance: Instance) => void);
}
export interface IChainFind {
find(conditions: { [property: string]: any }): IChainFind;
only(...args: string[]): IChainFind;
limit(limit: number): IChainFind;
offset(offset: number): IChainFind;
run(callback: (err: Error, results: Instance[]) => void): void;
count(callback: (err: Error, count: number) => void): void;
remove(callback: (err: Error) => void): void;
save(callback: (err: Error) => void): void;
each(callback: (result: Instance) => void): void;
each(): IChainFind;
filter(callback: (result: Instance) => boolean): IChainFind;
sort(callback: (a: Instance, b: Instance) => boolean): IChainFind;
get(callback: (results: Instance[]) => void): IChainFind;
}
/*
* Classes
*/
export class ORM extends events.EventEmitter {
validators: enforce;
enforce: enforce;
settings: Settings;
driver_name: string;
driver: any;
tools: any;
models: { [key: string]: Model };
plugins: Plugin[];
use(plugin: string, options?: any): ORM;
use(plugin: Plugin, options?: any): ORM;
define(name: string, properties: { [key: string]: Property }, opts?: ModelOptions): Model;
ping(callback: (err: Error) => void): ORM;
close(callback: (err: Error) => void): ORM;
load(file: string, callback: (err: Error) => void): any;
sync(callback: (err: Error) => void): ORM;
drop(callback: (err: Error) => void): ORM;
}
export class enforce {
static required(message?: string);
static notEmptyString(message?: string);
static rangeNumber(min: number, max: number, message?: string);
static rangeLength(min: number, max: number, message?: string);
static insideList(inside: string[], message?: string);
static insideList(inside: number[], message?: string);
static outsideList(outside: string[], message?: string);
static outsideList(outside: number[], message?: string);
static password(conditions?: string, message?: string);
static patterns(expr: RegExp, message?: string);
static patterns(expr: string, flags: string, message?: string);
static equalToProperty(name: string, message?: string);
static unique(message?: string);
static unique(opts: { ignoreCase: boolean }, message?: string);
}
export function equalToProperty(name: string, message?: string);
export function unique(message?: string);
export function unique(opts: { ignoreCase: boolean }, message?: string);
export class singleton {
static clear(key?: string): singleton;
static get(key, opts: {
cache?: any;
save_check?: boolean;
}, createCb: Function, returnCb: Function);
}
export class Settings {
static Container: any;
static defaults(): {
properties: {
primary_key: string;
association_key: string;
required: boolean;
};
instance: {
cache: boolean;
cacheSaveCheck: boolean;
autoSave: boolean;
autoFetch: boolean;
autoFetchLimit: number;
cascadeRemove: boolean;
returnAllErrors: boolean;
};
connection: {
reconnect: boolean;
poll: boolean;
debug: boolean;
};
};
constructor(settings: any);
//[key: string]: {
// get: (key, def) => any;
// set: (key, value) => Settings;
// unset: (...keys: string[]) => Settings;
//}
}
export var settings: Settings;
export class Property {
static normalize(property: string, settings: Settings): any;
static validate(value: any, property: string): any;
}
export interface ErrorCodes {
QUERY_ERROR: number;
NOT_FOUND: number;
NOT_DEFINED: number;
NO_SUPPORT: number;
MISSING_CALLBACK: number;
PARAM_MISMATCH: number;
CONNECTION_LOST: number;
generateError(code: number, message: string, extra: any): Error;
}
export function Text(type: string): sqlquery.TextQuery;
export function eq(value: any): sqlquery.Comparator;
export function ne(value: any): sqlquery.Comparator;
export function gt(value: any): sqlquery.Comparator;
export function gte(value: any): sqlquery.Comparator;
export function lt(value: any): sqlquery.Comparator;
export function lte(value: any): sqlquery.Comparator;
export function like(value: string): sqlquery.Comparator;
export function between(a: number, b: number): sqlquery.Comparator;
export function not_between(a: number, b: number): sqlquery.Comparator;
export function express(uri: string, handlers: {
define(db: ORM, models: { [key: string]: Model });
}): (req, res, next) => void;
export function use(connection, protocol: string, options, callback: (err: Error, db?: ORM) => void);
export function connect(uri: string): ORM;
export function connect(uri: string, callback: (err: Error, db: ORM) => void);
export function connect(options: IConnectionOptions): ORM;
export function connect(options: IConnectionOptions, callback: (err: Error, db: ORM) => void);
}
export = orm;
}