-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
69 lines (61 loc) · 1.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
///<reference path="./typings/main/ambient/async/index.d.ts" />
///<reference path="./typings/main/definitions/chai/index.d.ts" />
///<reference path="./typings/main/ambient/mocha/index.d.ts" />
///<reference path="./typings/main/ambient/mongodb/index.d.ts" />
///<reference path="./typings/main/ambient/mongoose/index.d.ts" />
'use strict';
import * as async from 'async';
import {expect} from 'chai';
import {Db} from 'mongodb';
import * as mongoose from 'mongoose';
interface IUser extends mongoose.Schema {
name: string;
userName: string;
password: string;
}
var Schema = mongoose.Schema,
userSchema = new Schema({
name: String,
userName: { type: String, required: true, unique: true },
password: { type: String, required: true },
admin: Boolean,
location: String,
meta: {
age: Number,
website: String
},
createdAt: Date,
updateAt: Date
}),
user = mongoose.model('User', userSchema),
testDb = 'test' + Date.now(),
testConnString = 'mongodb://localhost/' + testDb;
class Startup {
public static main(): number {
async.series([
callback => mongoose.connect(testConnString).connection
.once('connected', callback),
callback => {
var u1 = new user({
name: 'user1',
userName: 'user1',
password: 'password1'
});
u1.save(callback);
},
callback => {
var db = <Db>mongoose.connection.db;
if (((<string>db.databaseName || '').indexOf('test') >= 0)) {
db.dropDatabase(() => mongoose.connection.close(callback));
} else {
mongoose.connection.close(callback);
}
}
], (err, result) => {
if (err) throw err;
console.log("OK");
});
return 0;
}
}
Startup.main();