Skip to content

Commit 19f385d

Browse files
author
Lucas Matías Ciruzzi
committed
Added camo support
1 parent 44c9273 commit 19f385d

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

camo/camo-tests.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference path="./camo.d.ts" />
2+
3+
import {
4+
connect,
5+
Document as CamoDocument,
6+
DocumentSchema,
7+
SchemaTypeExtended
8+
} from "camo";
9+
10+
connect("mongodb://user:password@localhost:27017/database?authSource=admin").then(() => {
11+
let document = new CamoDocument();
12+
13+
interface UserSchema extends DocumentSchema {
14+
name: string;
15+
password: string;
16+
friends: string[];
17+
dateCreated?: Date;
18+
}
19+
20+
class User extends CamoDocument {
21+
private name: SchemaTypeExtended = String;
22+
private password: SchemaTypeExtended = String;
23+
private friends: SchemaTypeExtended = [String];
24+
private dateCreated: SchemaTypeExtended = {
25+
type: Date,
26+
default: Date.now
27+
};
28+
static collectionName() {
29+
return "users";
30+
}
31+
}
32+
33+
var newUser = User.create<UserSchema>({
34+
name: "user-1",
35+
password: "secret",
36+
friends: ["user-2", "user-3"]
37+
});
38+
39+
newUser.save().then(done => {
40+
console.log(done._id);
41+
});
42+
43+
});

camo/camo.d.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)