1 |
|
2 | import * as _AWS from 'aws-sdk';
|
3 |
|
4 | declare module "dynamoose" {
|
5 | export var AWS: typeof _AWS;
|
6 |
|
7 | export function local(url?: string): void;
|
8 | export function ddb(): _AWS.DynamoDB;
|
9 | export function setDocumentClient(documentClient: _AWS.DynamoDB.DocumentClient): void;
|
10 |
|
11 | export function model<DataSchema, KeySchema>(
|
12 | modelName: string,
|
13 | schema: Schema | SchemaAttributes,
|
14 | options?: ModelOption
|
15 | ): ModelConstructor<DataSchema, KeySchema>;
|
16 | export function setDefaults(options: ModelOption): void;
|
17 | export function setDDB(ddb: _AWS.DynamoDB): void;
|
18 | export function revertDDB(): void;
|
19 | export function transaction<DataSchema, KeySchema>(
|
20 | items: Array<
|
21 | Promise<ModelSchema<DataSchema>>
|
22 | | _AWS.DynamoDB.TransactWriteItem
|
23 | | _AWS.DynamoDB.TransactGetItem
|
24 | >,
|
25 | options?: TransactionOptions,
|
26 | next?: (err: Error, data: TransactionReturnData<DataSchema>) => void
|
27 | ): Promise<TransactionReturnData<DataSchema>>
|
28 | export interface TransactionReturnData<DataSchema> {
|
29 | TransactItems: Array<ModelSchema<DataSchema>>
|
30 | }
|
31 | export interface TransactionOptions {
|
32 | type: 'get' | 'write'
|
33 | }
|
34 |
|
35 | export interface ModelOption {
|
36 | create?: boolean,
|
37 | update?: boolean,
|
38 | waitForActive?: boolean,
|
39 | waitForActiveTimeout?: number,
|
40 | prefix?: string,
|
41 | suffix?: string,
|
42 | streamOptions?: {
|
43 | enabled: boolean,
|
44 | type: 'NEW_IMAGE'|'OLD_IMAGE'|'NEW_AND_OLD_IMAGES'|'KEYS_ONLY',
|
45 | },
|
46 | serverSideEncryption?: boolean,
|
47 | }
|
48 |
|
49 | |
50 |
|
51 |
|
52 | export class Schema {
|
53 | constructor(schema: SchemaAttributes, options?: SchemaOptions);
|
54 | method(name: string, fn: any): any;
|
55 | parseDynamo(model: any, dynamoObj: any): any;
|
56 | static(name: string, fn: any): any;
|
57 | toDynamo(model: any): any;
|
58 | virtual(name: string, options: any): any;
|
59 | virtualpath(name: string): any;
|
60 | }
|
61 |
|
62 |
|
63 | export interface RawSchemaAttributeDefinition<Constructor, Type> {
|
64 | [key: string]: SchemaAttributeDefinition<Constructor, Type>
|
65 | | RawSchemaAttributeDefinition<Constructor, Type>;
|
66 | }
|
67 | export interface SchemaAttributeDefinition<Constructor, Type> {
|
68 | type: Constructor;
|
69 | validate?: (v: Type) => boolean | Promise<boolean>;
|
70 | hashKey?: boolean;
|
71 | rangeKey?: boolean;
|
72 | required?: boolean;
|
73 | get?: () => Type;
|
74 | set?: (v: Type) => void;
|
75 | trim?: boolean;
|
76 | lowercase?: boolean;
|
77 | uppercase?: boolean;
|
78 | |
79 |
|
80 |
|
81 |
|
82 | index?: boolean | IndexDefinition | IndexDefinition[];
|
83 | default?: (() => Type) | Type
|
84 | }
|
85 | export interface SchemaOptions {
|
86 | throughput?: boolean | { read: number, write: number } | "ON_DEMAND";
|
87 | useNativeBooleans?: boolean;
|
88 | useDocumentTypes?: boolean;
|
89 | timestamps?: boolean | { createdAt: string, updatedAt: string };
|
90 | expires?: number | { ttl: number, attribute: string, returnExpiredItems: boolean };
|
91 | saveUnknown?: boolean;
|
92 |
|
93 | // @todo more strong type definition
|
94 | attributeToDynamo?: (name: string, json: any, model: any, defaultFormatter: any) => any;
|
95 | attributeFromDynamo?: (name: string, json: any, fallback: any) => any;
|
96 | }
|
97 |
|
98 | export interface SchemaAttributes {
|
99 | [key: string]: (
|
100 | SchemaAttributeDefinition<NumberConstructor, number>
|
101 | | SchemaAttributeDefinition<[NumberConstructor], number[]>
|
102 | | SchemaAttributeDefinition<DateConstructor, Date>
|
103 | | SchemaAttributeDefinition<StringConstructor, string>
|
104 | | SchemaAttributeDefinition<[StringConstructor], string[]>
|
105 | | SchemaAttributeDefinition<ObjectConstructor, Object>
|
106 | | SchemaAttributeDefinition<ArrayConstructor, Array<any>>
|
107 | | SchemaAttributeDefinition<any, any>
|
108 | | RawSchemaAttributeDefinition<any, any>
|
109 | | NumberConstructor
|
110 | | [NumberConstructor]
|
111 | | DateConstructor
|
112 | | StringConstructor
|
113 | | [StringConstructor]
|
114 | | ObjectConstructor
|
115 | | ArrayConstructor
|
116 | )
|
117 | }
|
118 |
|
119 | |
120 |
|
121 |
|
122 | interface IndexDefinition {
|
123 | name?: string;
|
124 | global?: boolean;
|
125 | rangeKey?: string;
|
126 | project?: boolean | string[];
|
127 | throughput?: number | { read: number, write: number };
|
128 | }
|
129 |
|
130 | |
131 |
|
132 |
|
133 | export class Table {
|
134 | constructor(name: string, schema: any, options: any, base: any);
|
135 | create(next: any): any;
|
136 | createIndex(attributes: any, indexSpec: any): any;
|
137 | delete(next: any): any;
|
138 | deleteIndex(indexname: string): any;
|
139 | describe(next: any): any;
|
140 | init(next: any): any;
|
141 | update(next: any): any;
|
142 | waitForActive(timeout: any, next: any): any;
|
143 | getTableReq(): any;
|
144 | }
|
145 |
|
146 | /**
|
147 | * Model
|
148 | */
|
149 | export class Model<ModelData> {
|
150 | constructor(obj: ModelData);
|
151 |
|
152 | delete(callback?: (err: Error) => void): Promise<undefined>;
|
153 |
|
154 | put(options: PutOptions, callback?: (err: Error) => void): Promise<Model<ModelData>>;
|
155 | put(callback: (err: Error) => void): Promise<Model<ModelData>>;
|
156 |
|
157 | save(options: SaveOptions, callback?: (err: Error) => void): Promise<Model<ModelData>>;
|
158 | save(callback?: (err: Error) => void): Promise<Model<ModelData>>;
|
159 |
|
160 | originalItem(): object;
|
161 |
|
162 | populate<T>(path: string | PopulateOptions): Promise<Model<ModelData> & T>
|
163 | }
|
164 | type PopulateOptions = { path: string, model: string, populate?: PopulateOptions }
|
165 |
|
166 | export interface PutOptions {
|
167 | |
168 |
|
169 |
|
170 | overwrite?: boolean;
|
171 | |
172 |
|
173 |
|
174 | updateTimestamps?: boolean;
|
175 | |
176 |
|
177 |
|
178 | updateExpires?: boolean;
|
179 | |
180 |
|
181 |
|
182 | condition?: string;
|
183 | |
184 |
|
185 |
|
186 | conditionNames?: any;
|
187 | |
188 |
|
189 |
|
190 | conditionValues?: any;
|
191 | }
|
192 | type SaveOptions = PutOptions;
|
193 |
|
194 | export interface ModelConstructor<DataSchema, KeySchema> {
|
195 | new(value?: DataSchema): ModelSchema<DataSchema>;
|
196 | (value?: DataSchema): ModelSchema<DataSchema>;
|
197 | readonly prototype: ModelSchema<DataSchema>;
|
198 |
|
199 | batchPut(items: DataSchema[], options?: PutOptions, callback?: (err: Error, items: ModelSchema<DataSchema>[]) => void): Promise<ModelSchema<DataSchema>[]>;
|
200 | batchPut(items: DataSchema[], callback?: (err: Error, items: ModelSchema<DataSchema>[]) => void): Promise<ModelSchema<DataSchema>[]>;
|
201 |
|
202 | create(item: DataSchema, options?: PutOptions, callback?: (err: Error, model: ModelSchema<DataSchema>) => void): Promise<ModelSchema<DataSchema>>;
|
203 | create(item: DataSchema, callback?: (err: Error, model: ModelSchema<DataSchema>) => void): Promise<ModelSchema<DataSchema>>;
|
204 | create(item: DataSchema, options?: PutOptions): Promise<ModelSchema<DataSchema>>;
|
205 |
|
206 | get(key: KeySchema, callback?: (err: Error, data: DataSchema) => void): Promise<ModelSchema<DataSchema> | undefined>;
|
207 | batchGet(key: KeySchema[], callback?: (err: Error, data: DataSchema) => void): Promise<ModelSchema<DataSchema>[]>;
|
208 |
|
209 | delete(key: KeySchema, callback?: (err: Error) => void): Promise<undefined>;
|
210 | batchDelete(keys: KeySchema[], callback?: (err: Error) => void): Promise<undefined>;
|
211 |
|
212 | query(query: QueryFilter, callback?: (err: Error, results: ModelSchema<DataSchema>[]) => void): QueryInterface<ModelSchema<DataSchema>, QueryResult<ModelSchema<DataSchema>>>;
|
213 | queryOne(query: QueryFilter, callback?: (err: Error, results: ModelSchema<DataSchema>) => void): QueryInterface<ModelSchema<DataSchema>, ModelSchema<DataSchema>>;
|
214 | scan(filter?: ScanFilter, callback?: (err: Error, results: ModelSchema<DataSchema>[]) => void): ScanInterface<ModelSchema<DataSchema>>;
|
215 |
|
216 | update(key: KeySchema, update: UpdateUpdate<DataSchema>, options: UpdateOptions, callback: (err: Error, items: ModelSchema<DataSchema>[]) => void): void;
|
217 | update(key: KeySchema, update: UpdateUpdate<DataSchema>, callback: (err: Error, items: ModelSchema<DataSchema>[]) => void): void;
|
218 | update(key: KeySchema, update: UpdateUpdate<DataSchema>, options?: UpdateOptions): Promise<ModelSchema<DataSchema>>;
|
219 |
|
220 | transaction: ModelTransactionConstructor<DataSchema, KeySchema>
|
221 | }
|
222 | type ModelSchema<T> = Model<T> & T;
|
223 |
|
224 | |
225 |
|
226 |
|
227 |
|
228 | |
229 |
|
230 |
|
231 |
|
232 | type UpdateUpdate<DataSchema> = (
|
233 | Partial<DataSchema>
|
234 | | { $PUT: Partial<DataSchema> }
|
235 | | { $ADD: Partial<DataSchema> }
|
236 | | { $DELETE: Partial<DataSchema> }
|
237 | );
|
238 |
|
239 | export interface UpdateOptions {
|
240 | |
241 |
|
242 |
|
243 | allowEmptyArray?: boolean;
|
244 | |
245 |
|
246 |
|
247 | createRequired?: boolean;
|
248 | |
249 |
|
250 |
|
251 | updateTimestamps?: boolean;
|
252 | |
253 |
|
254 |
|
255 | returnValues?:
|
256 | | "NONE"
|
257 | | "ALL_OLD"
|
258 | | "UPDATED_OLD"
|
259 | | "ALL_NEW"
|
260 | | "UPDATED_NEW";
|
261 | |
262 |
|
263 |
|
264 | condition?: string;
|
265 | |
266 |
|
267 |
|
268 | conditionNames?: any;
|
269 | |
270 |
|
271 |
|
272 | conditionValues?: any;
|
273 | }
|
274 |
|
275 |
|
276 | |
277 |
|
278 |
|
279 | type QueryFilter = any;
|
280 | export interface QueryInterface<T, R> {
|
281 | exec(callback?: (err: Error, result: R) => void): Promise<R>;
|
282 | where(rangeKey: string): QueryInterface<T, R>;
|
283 | filter(filter: string): QueryInterface<T, R>;
|
284 | and(): QueryInterface<T, R>;
|
285 | or(): QueryInterface<T, R>;
|
286 | not(): QueryInterface<T, R>;
|
287 | null(): QueryInterface<T, R>;
|
288 | eq(value: any): QueryInterface<T, R>;
|
289 | lt(value: any): QueryInterface<T, R>;
|
290 | le(value: any): QueryInterface<T, R>;
|
291 | ge(value: any): QueryInterface<T, R>;
|
292 | gt(value: any): QueryInterface<T, R>;
|
293 | beginsWith(value: string): QueryInterface<T, R>;
|
294 | between(valueA: any, valueB: any): QueryInterface<T, R>;
|
295 | contains(value: string): QueryInterface<T, R>;
|
296 | in(values: any[]): QueryInterface<T, R>;
|
297 | limit(limit: number): QueryInterface<T, R>;
|
298 | consistent(): QueryInterface<T, R>;
|
299 | descending(): QueryInterface<T, R>;
|
300 | ascending(): QueryInterface<T, R>;
|
301 | startAt(key: QueryKey): QueryInterface<T, R>;
|
302 | attributes(attributes: string[]): QueryInterface<T, R>;
|
303 | count(): QueryInterface<T, R>;
|
304 | counts(): QueryInterface<T, R>;
|
305 | using(indexName: string): QueryInterface<T, R>;
|
306 | }
|
307 | export interface QueryResult<T> extends Array<T> {
|
308 | lastKey?: QueryKey;
|
309 | }
|
310 | type QueryKey = any;
|
311 |
|
312 |
|
313 | |
314 |
|
315 |
|
316 | type ScanFilter = string | any;
|
317 |
|
318 | export interface ScanInterface<T> {
|
319 | exec(callback?: (err: Error, result: ScanResult<T>) => void): Promise<ScanResult<T>>;
|
320 | all(delay?: number, max?: number): ScanInterface<T>;
|
321 | parallel(totalSegments: number): ScanInterface<T>;
|
322 | using(indexName: string): ScanInterface<T>;
|
323 | consistent(filter?: any): ScanInterface<T>;
|
324 | where(filter: any): ScanInterface<T>;
|
325 | filter(filter: any): ScanInterface<T>;
|
326 | and(): ScanInterface<T>;
|
327 | not(): ScanInterface<T>;
|
328 | null(): ScanInterface<T>;
|
329 | eq(value: any): ScanInterface<T>;
|
330 | lt(value: any): ScanInterface<T>;
|
331 | le(value: any): ScanInterface<T>;
|
332 | ge(value: any): ScanInterface<T>;
|
333 | gt(value: any): ScanInterface<T>;
|
334 | beginsWith(value: any): ScanInterface<T>;
|
335 | between(valueA: any, valueB: any): ScanInterface<T>;
|
336 | contains(value: any): ScanInterface<T>;
|
337 | beginsWith(value: any): ScanInterface<T>;
|
338 | in(value: any): ScanInterface<T>;
|
339 | limit(limit: number): ScanInterface<T>;
|
340 | startAt(key: ScanKey): ScanInterface<T>;
|
341 | attributes(value: any): ScanInterface<T>;
|
342 | count(): ScanInterface<T>;
|
343 | counts(): ScanInterface<T>;
|
344 | }
|
345 |
|
346 | export interface ScanResult<ModelData> extends Array<ModelData> {
|
347 | lastKey?: ScanKey;
|
348 | }
|
349 |
|
350 | type ScanKey = any;
|
351 |
|
352 | export class VirtualType {
|
353 | constructor(options: any, name: string);
|
354 | applyVirtuals(model: any): void;
|
355 | get(fn: any): any;
|
356 | set(fn: any): any;
|
357 | }
|
358 |
|
359 | /**
|
360 | * Transaction
|
361 | */
|
362 | export interface ModelTransactionConstructor<DataSchema, KeySchema> {
|
363 | new(value?: DataSchema): ModelSchema<DataSchema>;
|
364 | (value?: DataSchema): ModelSchema<DataSchema>;
|
365 | readonly prototype: ModelSchema<DataSchema>;
|
366 |
|
367 | create(item: DataSchema, options?: PutOptions, callback?: (err: Error, model: ModelSchema<DataSchema>) => void): Promise<ModelSchema<DataSchema>>;
|
368 | create(item: DataSchema, callback?: (err: Error, model: ModelSchema<DataSchema>) => void): Promise<ModelSchema<DataSchema>>;
|
369 | create(item: DataSchema, options?: PutOptions): Promise<ModelSchema<DataSchema>>;
|
370 |
|
371 | get(key: KeySchema, callback?: (err: Error, data: DataSchema) => void): Promise<ModelSchema<DataSchema> | undefined>;
|
372 |
|
373 | delete(key: KeySchema, callback?: (err: Error) => void): Promise<undefined>;
|
374 |
|
375 | update(key: KeySchema, update: UpdateUpdate<DataSchema>, options: UpdateOptions, callback: (err: Error, items: ModelSchema<DataSchema>[]) => void): void;
|
376 | update(key: KeySchema, update: UpdateUpdate<DataSchema>, callback: (err: Error, items: ModelSchema<DataSchema>[]) => void): void;
|
377 | update(key: KeySchema, update: UpdateUpdate<DataSchema>, options?: UpdateOptions): Promise<ModelSchema<DataSchema>>;
|
378 |
|
379 | conditionCheck(key: KeySchema, options?: ConditionOptions): void
|
380 | }
|
381 | export interface ConditionOptions {
|
382 | condition: string,
|
383 | conditionNames: object,
|
384 | conditionValues: object,
|
385 | }
|
386 | }
|