UNPKG

14.9 kBTypeScriptView Raw
1// Custom made dynamoose declaration file.
2import * as _AWS from 'aws-sdk';
3
4declare 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, // Create table in DB, if it does not exist,
37 update?: boolean, // Update remote indexes if they do not match local index structure
38 waitForActive?: boolean, // Wait for table to be created before trying to us it
39 waitForActiveTimeout?: number, // wait 3 minutes for table to activate
40 prefix?: string, // Set table name prefix
41 suffix?: string, // Set table name suffix
42 streamOptions?: { // Set table stream options
43 enabled: boolean, // Enable/disable stream
44 type: 'NEW_IMAGE'|'OLD_IMAGE'|'NEW_AND_OLD_IMAGES'|'KEYS_ONLY', // https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_StreamSpecification.html#DDB-Type-StreamSpecification-StreamViewType
45 },
46 serverSideEncryption?: boolean, // Set SSESpecification.Enabled (server-side encryption) to true or false (default: true)
47 }
48
49 /**
50 * Schema
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 * Indicating Secondary Index.
80 * 'true' is means local, project all
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 * Index
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 * Table
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 * Overwrite existing item. Defaults to true for `model.put` and false for `Model.create`.
169 */
170 overwrite?: boolean;
171 /**
172 * Whether to update the documents timestamps or not. Defaults to true.
173 */
174 updateTimestamps?: boolean;
175 /**
176 * Whether to update the documents expires or not. Defaults to false.
177 */
178 updateExpires?: boolean;
179 /**
180 * An expression for a conditional update. See the AWS documentation for more information about condition expressions.
181 */
182 condition?: string;
183 /**
184 * A map of name substitutions for the condition expression.
185 */
186 conditionNames?: any;
187 /**
188 * 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.
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 * Update
226 */
227
228 /**
229 * Updates and existing item in the table. Three types of updates: $PUT, $ADD, and $DELETE.
230 * Put is the default behavior.
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 * If true, the attribute can be updated to an empty array. If false, empty arrays will remove the attribute. Defaults to false.
242 */
243 allowEmptyArray?: boolean;
244 /**
245 * If true, required attributes will be filled with their default values on update (regardless of you specifying them for the update). Defaults to false.
246 */
247 createRequired?: boolean;
248 /**
249 * If true, the timestamps attributes will be updated. Will not do anything if timestamps attribute were not specified. Defaults to true.
250 */
251 updateTimestamps?: boolean;
252 /**
253 * Specifies what should be returned after update successfully completes.
254 */
255 returnValues?:
256 | "NONE"
257 | "ALL_OLD"
258 | "UPDATED_OLD"
259 | "ALL_NEW"
260 | "UPDATED_NEW";
261 /**
262 * An expression for a conditional update. See the AWS documentation for more information about condition expressions.
263 */
264 condition?: string;
265 /**
266 * A map of name substitutions for the condition expression.
267 */
268 conditionNames?: any;
269 /**
270 * 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.
271 */
272 conditionValues?: any;
273 }
274
275
276 /**
277 * Query
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 * Scan
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}