forked from dynamoose/dynamoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamoose.d.ts
303 lines (267 loc) · 11.3 KB
/
dynamoose.d.ts
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
// Custom made dynamoose declaration file.
import * as _AWS from 'aws-sdk';
declare module "dynamoose" {
export var AWS: typeof _AWS;
export function local(url: string): void;
export function ddb(): _AWS.DynamoDB;
export function setDocumentClient(documentClient: _AWS.DynamoDB.DocumentClient): void;
export function model<DataSchema, KeySchema>(
modelName: string,
schema: Schema | SchemaAttributes,
options?: ModelOption
): ModelConstructor<DataSchema, KeySchema>;
export function setDefaults(options: ModelOption): void;
export interface ModelOption {
create?: boolean, // Create table in DB, if it does not exist,
update?: boolean, // Update remote indexes if they do not match local index structure
waitForActive?: boolean, // Wait for table to be created before trying to us it
waitForActiveTimeout?: number, // wait 3 minutes for table to activate
prefix?: string, // Set table name prefix
suffix?: string, // Set table name suffix
serverSideEncryption?: boolean, // Set SSESpecification.Enabled (server-side encryption) to true or false (default: true)
}
/**
* Schema
*/
export class Schema {
constructor(schema: SchemaAttributes, options?: SchemaOptions);
method(name: string, fn: any): any;
parseDynamo(model: any, dynamoObj: any): any;
static(name: string, fn: any): any;
toDynamo(model: any): any;
virtual(name: string, options: any): any;
virtualpath(name: string): any;
}
export interface RawSchemaAttributeDefinition<Constructor, Type> {
[key: string]: SchemaAttributeDefinition<Constructor, Type>
| RawSchemaAttributeDefinition<Constructor, Type>;
}
export interface SchemaAttributeDefinition<Constructor, Type> {
type: Constructor;
validate?: (v: Type) => boolean;
hashKey?: boolean;
rangeKey?: boolean;
required?: boolean;
get?: () => Type;
set?: (v: Type) => void;
trim?: boolean;
lowercase?: boolean;
uppercase?: boolean;
/**
* Indicating Secondary Index.
* 'true' is means local, project all
*/
index?: boolean | IndexDefinition | IndexDefinition[];
default?: (() => Type) | Type
}
export interface SchemaOptions {
throughput?: boolean | { read: number, write: number };
useNativeBooleans?: boolean;
useDocumentTypes?: boolean;
timestamps?: boolean | { createdAt: string, updatedAt: string };
expires?: number | { ttl: number, attribute: string };
saveUnknown?: boolean;
// @todo more strong type definition
attributeToDynamo?: (name: string, json: any, model: any, defaultFormatter: any) => any;
attributeFromDynamo?: (name: string, json: any, fallback: any) => any;
}
export interface SchemaAttributes {
[key: string]: (
SchemaAttributeDefinition<NumberConstructor, number>
| SchemaAttributeDefinition<[NumberConstructor], number[]>
| SchemaAttributeDefinition<DateConstructor, Date>
| SchemaAttributeDefinition<StringConstructor, string>
| SchemaAttributeDefinition<[StringConstructor], string[]>
| SchemaAttributeDefinition<ObjectConstructor, Object>
| SchemaAttributeDefinition<ArrayConstructor, Array<any>>
| SchemaAttributeDefinition<any, any>
| RawSchemaAttributeDefinition<any, any>
)
}
/**
* Index
*/
interface IndexDefinition {
name?: string;
global?: boolean;
rangeKey?: string;
project?: boolean | string[];
throughput?: number | { read: number, write: number };
}
/**
* Table
*/
export class Table {
constructor(name: string, schema: any, options: any, base: any);
create(next: any): any;
createIndex(attributes: any, indexSpec: any): any;
delete(next: any): any;
deleteIndex(indexname: string): any;
describe(next: any): any;
init(next: any): any;
update(next: any): any;
waitForActive(timeout: any, next: any): any;
}
/**
* Model
*/
export class Model<ModelData> {
constructor(obj: ModelData);
put(options: PutOptions, callback: (err: Error) => void): Promise<Model<ModelData>>;
save(options: SaveOptions, callback: (err: Error) => void): Promise<Model<ModelData>>;
delete(callback?: (err: Error) => void): Promise<undefined>;
put(callback: (err: Error) => void): Promise<Model<ModelData>>;
put(options: ModelData, callback?: (err: Error) => void): Promise<Model<ModelData>>;
save(callback?: (err: Error) => void): Promise<Model<ModelData>>;
save(options: ModelData, callback?: (err: Error) => void): Promise<Model<ModelData>>;
originalItem(): object;
populate<T>(path: string | PopulateOptions): Promise<Model<ModelData> & T>
}
type PopulateOptions = { path: string, model: string, populate?: PopulateOptions }
export interface PutOptions {
/**
* Overwrite existing item. Defaults to true for `model.put` and false for `Model.create`.
*/
overwrite?: boolean;
/**
* Whether to update the documents timestamps or not. Defaults to true.
*/
updateTimestamps?: boolean;
/**
* An expression for a conditional update. See the AWS documentation for more information about condition expressions.
*/
condition?: string;
/**
* A map of name substitutions for the condition expression.
*/
conditionNames?: any;
/**
* A map of values for the condition expression. Note that in order for automatic object conversion to work, the keys in this object must match schema attribute names.
*/
conditionValues?: any;
}
type SaveOptions = PutOptions;
export interface ModelConstructor<DataSchema, KeySchema> {
new(value?: DataSchema): ModelSchema<DataSchema>;
(value?: DataSchema): ModelSchema<DataSchema>;
readonly prototype: ModelSchema<DataSchema>;
batchPut(items: DataSchema[], options?: PutOptions, callback?: (err: Error, items: ModelSchema<DataSchema>[]) => void): Promise<ModelSchema<DataSchema>[]>;
batchPut(items: DataSchema[], callback?: (err: Error, items: ModelSchema<DataSchema>[]) => void): Promise<ModelSchema<DataSchema>[]>;
create(item: DataSchema, options?: PutOptions, callback?: (err: Error, model: ModelSchema<DataSchema>) => void): Promise<ModelSchema<DataSchema>>;
create(item: DataSchema, callback?: (err: Error, model: ModelSchema<DataSchema>) => void): Promise<ModelSchema<DataSchema>>;
create(item: DataSchema, options?: PutOptions): Promise<ModelSchema<DataSchema>>;
get(key: KeySchema, callback?: (err: Error, data: DataSchema) => void): Promise<ModelSchema<DataSchema> | undefined>;
batchGet(key: KeySchema[], callback?: (err: Error, data: DataSchema) => void): Promise<ModelSchema<DataSchema>[]>;
delete(key: KeySchema, callback?: (err: Error) => void): Promise<undefined>;
batchDelete(keys: KeySchema[], callback?: (err: Error) => void): Promise<undefined>;
query(query: QueryFilter, callback?: (err: Error, results: ModelSchema<DataSchema>[]) => void): QueryInterface<ModelSchema<DataSchema>, QueryResult<ModelSchema<DataSchema>>>;
queryOne(query: QueryFilter, callback?: (err: Error, results: ModelSchema<DataSchema>) => void): QueryInterface<ModelSchema<DataSchema>, ModelSchema<DataSchema>>;
scan(filter?: ScanFilter, callback?: (err: Error, results: ModelSchema<DataSchema>[]) => void): ScanInterface<ModelSchema<DataSchema>>;
update(key: KeySchema, update: UpdateUpdate<DataSchema>, options: UpdateOption, callback: (err: Error, items: ModelSchema<DataSchema>[]) => void): void;
update(key: KeySchema, update: UpdateUpdate<DataSchema>, callback: (err: Error, items: ModelSchema<DataSchema>[]) => void): void;
update(key: KeySchema, update: UpdateUpdate<DataSchema>, options?: UpdateOption): Promise<ModelSchema<DataSchema>>;
}
type ModelSchema<T> = Model<T> & T;
/**
* Update
*/
/**
* Updates and existing item in the table. Three types of updates: $PUT, $ADD, and $DELETE.
* Put is the default behavior.
*/
type UpdateUpdate<DataSchema> = (
Partial<DataSchema>
| { $PUT: Partial<DataSchema> }
| { $ADD: Partial<DataSchema> }
| { $DELETE: Partial<DataSchema> }
);
export interface UpdateOption {
/**
* If true, the attribute can be updated to an empty array. If false, empty arrays will remove the attribute. Defaults to false.
*/
allowEmptyArray: boolean;
/**
* If true, required attributes will be filled with their default values on update (regardless of you specifying them for the update). Defaults to false.
*/
createRequired: boolean;
/**
* If true, the timestamps attributes will be updated. Will not do anything if timestamps attribute were not specified. Defaults to true.
*/
updateTimestamps: boolean;
}
/**
* Query
*/
type QueryFilter = any;
export interface QueryInterface<T, R> {
exec(callback?: (err: Error, result: R) => void): Promise<R>;
where(rangeKey: string): QueryInterface<T, R>;
filter(filter: string): QueryInterface<T, R>;
and(): QueryInterface<T, R>;
or(): QueryInterface<T, R>;
not(): QueryInterface<T, R>;
null(): QueryInterface<T, R>;
eq(value: any): QueryInterface<T, R>;
lt(value: any): QueryInterface<T, R>;
le(value: any): QueryInterface<T, R>;
ge(value: any): QueryInterface<T, R>;
gt(value: any): QueryInterface<T, R>;
beginsWith(value: string): QueryInterface<T, R>;
between(valueA: any, valueB: any): QueryInterface<T, R>;
contains(value: string): QueryInterface<T, R>;
in(values: any[]): QueryInterface<T, R>;
limit(limit: number): QueryInterface<T, R>;
consistent(): QueryInterface<T, R>;
descending(): QueryInterface<T, R>;
ascending(): QueryInterface<T, R>;
startAt(key: QueryKey): QueryInterface<T, R>;
attributes(attributes: string[]): QueryInterface<T, R>;
count(): QueryInterface<T, R>;
counts(): QueryInterface<T, R>;
}
export interface QueryResult<T> extends Array<T> {
lastKey?: QueryKey;
}
type QueryKey = string;
/**
* Scan
*/
type ScanFilter = string | any;
export interface ScanInterface<T> {
exec(callback?: (err: Error, result: ScanResult<T>) => void): Promise<ScanResult<T>>;
all(delay?: number, max?: number): ScanInterface<T>;
parallel(totalSegments: number): ScanInterface<T>;
using(indexName: string): ScanInterface<T>;
consistent(filter?: any): ScanInterface<T>;
where(filter: any): ScanInterface<T>;
filter(filter: any): ScanInterface<T>;
and(): ScanInterface<T>;
not(): ScanInterface<T>;
null(): ScanInterface<T>;
eq(value: any): ScanInterface<T>;
lt(value: any): ScanInterface<T>;
le(value: any): ScanInterface<T>;
ge(value: any): ScanInterface<T>;
gt(value: any): ScanInterface<T>;
beginsWith(value: any): ScanInterface<T>;
between(valueA: any, valueB: any): ScanInterface<T>;
contains(value: any): ScanInterface<T>;
beginsWith(value: any): ScanInterface<T>;
in(value: any): ScanInterface<T>;
limit(limit: number): ScanInterface<T>;
startAt(key: ScanKey): ScanInterface<T>;
attributes(value: any): ScanInterface<T>;
count(): ScanInterface<T>;
counts(): ScanInterface<T>;
}
export interface ScanResult<ModelData> extends Array<ModelData> {
lastKey?: ScanKey;
}
type ScanKey = string;
export class VirtualType {
constructor(options: any, name: string);
applyVirtuals(model: any): void;
get(fn: any): any;
set(fn: any): any;
}
}