From 19f385d75d893f79f773e88403e17b9b0b6e79b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Mat=C3=ADas=20Ciruzzi?= Date: Thu, 11 Feb 2016 14:26:54 -0300 Subject: [PATCH] Added camo support --- camo/camo-tests.ts | 43 ++++++++++++++ camo/camo.d.ts | 139 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 camo/camo-tests.ts create mode 100644 camo/camo.d.ts diff --git a/camo/camo-tests.ts b/camo/camo-tests.ts new file mode 100644 index 00000000000000..68b515a61cbd19 --- /dev/null +++ b/camo/camo-tests.ts @@ -0,0 +1,43 @@ +/// + +import { + connect, + Document as CamoDocument, + DocumentSchema, + SchemaTypeExtended +} from "camo"; + +connect("mongodb://user:password@localhost:27017/database?authSource=admin").then(() => { + let document = new CamoDocument(); + + interface UserSchema extends DocumentSchema { + name: string; + password: string; + friends: string[]; + dateCreated?: Date; + } + + class User extends CamoDocument { + private name: SchemaTypeExtended = String; + private password: SchemaTypeExtended = String; + private friends: SchemaTypeExtended = [String]; + private dateCreated: SchemaTypeExtended = { + type: Date, + default: Date.now + }; + static collectionName() { + return "users"; + } + } + + var newUser = User.create({ + name: "user-1", + password: "secret", + friends: ["user-2", "user-3"] + }); + + newUser.save().then(done => { + console.log(done._id); + }); + +}); diff --git a/camo/camo.d.ts b/camo/camo.d.ts new file mode 100644 index 00000000000000..70f9817ffb77c3 --- /dev/null +++ b/camo/camo.d.ts @@ -0,0 +1,139 @@ +// Type definitions for camo v0.11.4 +// Project: https://github.com/scottwrobinson/camo +// Definitions by: Lucas Matías Ciruzzi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module "camo" { + + type TypeOrArray = Type | Type[]; + + /** + * Supported type constructors for document properties + */ + export type SchemaTypeConstructor = + TypeOrArray | + TypeOrArray | + TypeOrArray | + TypeOrArray | + TypeOrArray | + TypeOrArray; + + /** + * Supported types for document properties + */ + export type SchemaType = TypeOrArray; + + /** + * Document property with options + */ + export interface SchemaTypeOptions { + /** + * Type of data + */ + type: SchemaTypeConstructor; + /** + * Default value + */ + default?: Type; + /** + * Min value (only with Number) + */ + min?: number; + /** + * Max value (only with Number) + */ + max?: number; + /** + * Posible options + */ + choices?: Type[]; + /** + * RegEx to match value + */ + match?: RegExp; + /** + * Validation function + * + * @param value Value taken + * @returns true (validation ok) or false (validation wrong) + */ + validate?(value: Type): boolean; + /** + * Unique value (like ids) + */ + unique?: boolean; + /** + * Required field + */ + required?: boolean; + } + + /** + * Document property type or options + */ + export type SchemaTypeExtended = SchemaTypeConstructor | SchemaTypeOptions; + + /** + * Schema passed to Document.create() + */ + interface DocumentSchema { + /** + * Index signature + */ + [property: string]: SchemaType; + /** + * Document id + */ + _id?: string; + } + + /** + * Camo document instance + */ + class DocumentInstance { + public save(): Promise; + public loadOne(): Promise; + public loadMany(): Promise; + public delete(): Promise; + public deleteOne(): Promise; + public deleteMany(): Promise; + public loadOneAndDelete(): Promise; + public count(): Promise; + public preValidate(): Promise; + public postValidate(): Promise; + public preSave(): Promise; + public postSave(): Promise; + public preDelete(): Promise; + public postDelete(): Promise; + } + + /** + * Camo document + */ + export class Document { + /** + * Index signature + */ + [property: string]: SchemaTypeExtended | string | DocumentInstance; + /** + * Static method to define the collection name + * + * @returns The collection name + */ + static collectionName(): string; + /** + * Creates a camo document instance + * + * @returns A camo document instance + */ + static create(schema: Schema): DocumentInstance; + } + + /** + * Connect function + * + * @param uri Connection URI + */ + export function connect (uri: string): Promise; + +}