|
| 1 | +// Type definitions for camo v0.11.4 |
| 2 | +// Project: https://github.com/scottwrobinson/camo |
| 3 | +// Definitions by: Lucas Matías Ciruzzi <https://github.com/lucasmciruzzi> |
| 4 | +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped |
| 5 | + |
| 6 | +declare module "camo" { |
| 7 | + |
| 8 | + type TypeOrArray<Type> = Type | Type[]; |
| 9 | + |
| 10 | + /** |
| 11 | + * Supported type constructors for document properties |
| 12 | + */ |
| 13 | + export type SchemaTypeConstructor = |
| 14 | + TypeOrArray<StringConstructor> | |
| 15 | + TypeOrArray<NumberConstructor> | |
| 16 | + TypeOrArray<BooleanConstructor> | |
| 17 | + TypeOrArray<DateConstructor> | |
| 18 | + TypeOrArray<ObjectConstructor> | |
| 19 | + TypeOrArray<ArrayConstructor>; |
| 20 | + |
| 21 | + /** |
| 22 | + * Supported types for document properties |
| 23 | + */ |
| 24 | + export type SchemaType = TypeOrArray<string | number | boolean | Date | Object>; |
| 25 | + |
| 26 | + /** |
| 27 | + * Document property with options |
| 28 | + */ |
| 29 | + export interface SchemaTypeOptions<Type> { |
| 30 | + /** |
| 31 | + * Type of data |
| 32 | + */ |
| 33 | + type: SchemaTypeConstructor; |
| 34 | + /** |
| 35 | + * Default value |
| 36 | + */ |
| 37 | + default?: Type; |
| 38 | + /** |
| 39 | + * Min value (only with Number) |
| 40 | + */ |
| 41 | + min?: number; |
| 42 | + /** |
| 43 | + * Max value (only with Number) |
| 44 | + */ |
| 45 | + max?: number; |
| 46 | + /** |
| 47 | + * Posible options |
| 48 | + */ |
| 49 | + choices?: Type[]; |
| 50 | + /** |
| 51 | + * RegEx to match value |
| 52 | + */ |
| 53 | + match?: RegExp; |
| 54 | + /** |
| 55 | + * Validation function |
| 56 | + * |
| 57 | + * @param value Value taken |
| 58 | + * @returns true (validation ok) or false (validation wrong) |
| 59 | + */ |
| 60 | + validate?(value: Type): boolean; |
| 61 | + /** |
| 62 | + * Unique value (like ids) |
| 63 | + */ |
| 64 | + unique?: boolean; |
| 65 | + /** |
| 66 | + * Required field |
| 67 | + */ |
| 68 | + required?: boolean; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Document property type or options |
| 73 | + */ |
| 74 | + export type SchemaTypeExtended = SchemaTypeConstructor | SchemaTypeOptions<SchemaType>; |
| 75 | + |
| 76 | + /** |
| 77 | + * Schema passed to Document.create() |
| 78 | + */ |
| 79 | + interface DocumentSchema { |
| 80 | + /** |
| 81 | + * Index signature |
| 82 | + */ |
| 83 | + [property: string]: SchemaType; |
| 84 | + /** |
| 85 | + * Document id |
| 86 | + */ |
| 87 | + _id?: string; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Camo document instance |
| 92 | + */ |
| 93 | + class DocumentInstance<Schema extends DocumentSchema> { |
| 94 | + public save(): Promise<Schema>; |
| 95 | + public loadOne(): Promise<Schema>; |
| 96 | + public loadMany(): Promise<Schema>; |
| 97 | + public delete(): Promise<Schema>; |
| 98 | + public deleteOne(): Promise<Schema>; |
| 99 | + public deleteMany(): Promise<Schema>; |
| 100 | + public loadOneAndDelete(): Promise<Schema>; |
| 101 | + public count(): Promise<Schema>; |
| 102 | + public preValidate(): Promise<Schema>; |
| 103 | + public postValidate(): Promise<Schema>; |
| 104 | + public preSave(): Promise<Schema>; |
| 105 | + public postSave(): Promise<Schema>; |
| 106 | + public preDelete(): Promise<Schema>; |
| 107 | + public postDelete(): Promise<Schema>; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Camo document |
| 112 | + */ |
| 113 | + export class Document { |
| 114 | + /** |
| 115 | + * Index signature |
| 116 | + */ |
| 117 | + [property: string]: SchemaTypeExtended | string | DocumentInstance<any>; |
| 118 | + /** |
| 119 | + * Static method to define the collection name |
| 120 | + * |
| 121 | + * @returns The collection name |
| 122 | + */ |
| 123 | + static collectionName(): string; |
| 124 | + /** |
| 125 | + * Creates a camo document instance |
| 126 | + * |
| 127 | + * @returns A camo document instance |
| 128 | + */ |
| 129 | + static create<Schema extends DocumentSchema>(schema: Schema): DocumentInstance<Schema>; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Connect function |
| 134 | + * |
| 135 | + * @param uri Connection URI |
| 136 | + */ |
| 137 | + export function connect (uri: string): Promise<any>; |
| 138 | + |
| 139 | +} |
0 commit comments