Skip to content

Commit b83991f

Browse files
committed
added some tests for models and updated package.json
1 parent 1113d20 commit b83991f

File tree

11 files changed

+498
-125
lines changed

11 files changed

+498
-125
lines changed

gulpfile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ gulp.task('test', function() {
3232
}
3333
);
3434

35-
gulp.task('sanity',['lint','test']);
35+
gulp.task('create', function(){
36+
37+
});
38+
39+
gulp.task('sanity',['lint','test']);

models/Users.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
"use strict";
2+
3+
var db = require('../services/database/mongo');
4+
var _ = require('lodash');
5+
6+
var collection = 'Users';
7+
8+
var debug = require('debug')(collection);
9+
10+
var schemaObject = {
11+
name: {
12+
type: 'String'
13+
}
14+
};
15+
16+
schemaObject.createdAt = {
17+
type: 'Date',
18+
default: Date.now
19+
};
20+
21+
schemaObject.updatedAt = {
22+
type: 'Date'
23+
// default: Date.now
24+
};
25+
26+
schemaObject.owner = {
27+
type: db.Schema.Types.ObjectId,
28+
ref: 'Users'
29+
};
30+
31+
schemaObject.createdBy = {
32+
type: db.Schema.Types.ObjectId,
33+
ref: 'Users'
34+
};
35+
36+
schemaObject.client = {
37+
type: db.Schema.Types.ObjectId,
38+
ref: 'Clients'
39+
};
40+
41+
schemaObject.developer = {
42+
type: db.Schema.Types.ObjectId,
43+
ref: 'Users'
44+
};
45+
46+
schemaObject.tags = {
47+
type: [String],
48+
index: 'text'
49+
};
50+
51+
// Let us define our schema
52+
var Schema = db.Schema(schemaObject);
53+
54+
// Index all text for full text search
55+
// MyModel.find({$text: {$search: searchString}})
56+
// .skip(20)
57+
// .limit(10)
58+
// .exec(function(err, docs) { ... });
59+
// Schema.index({'tags': 'text'});
60+
61+
Schema.statics.search = function(string) {
62+
return this.find({$text: {$search: string}}, { score : { $meta: "textScore" } })
63+
.sort({ score : { $meta : 'textScore' } });
64+
};
65+
66+
// assign a function to the "methods" object of our Schema
67+
// Schema.methods.someMethod = function (cb) {
68+
// return this.model(collection).find({}, cb);
69+
// };
70+
71+
// assign a function to the "statics" object of our Schema
72+
// Schema.statics.someStaticFunction = function(query, cb) {
73+
// eg. pagination
74+
// this.find(query, null, { skip: 10, limit: 5 }, cb);
75+
// };
76+
77+
// Adding hooks
78+
79+
Schema.pre('save', function(next) {
80+
// Indexing for search
81+
var ourDoc = this._doc;
82+
var split = [];
83+
for(var n in ourDoc){
84+
if(typeof ourDoc[n] === 'string'){
85+
split.push(ourDoc[n].split(' '));
86+
}
87+
}
88+
this.tags = split;
89+
next();
90+
});
91+
92+
Schema.post('init', function(doc) {
93+
debug('%s has been initialized from the db', doc._id);
94+
});
95+
96+
Schema.post('validate', function(doc) {
97+
debug('%s has been validated (but not saved yet)', doc._id);
98+
});
99+
100+
Schema.post('save', function(doc) {
101+
debug('%s has been saved', doc._id);
102+
});
103+
104+
Schema.post('remove', function(doc) {
105+
debug('%s has been removed', doc._id);
106+
});
107+
108+
Schema.pre('validate', function(next) {
109+
debug('this gets printed first');
110+
next();
111+
});
112+
113+
Schema.post('validate', function() {
114+
debug('this gets printed second');
115+
});
116+
117+
Schema.pre('find', function(next) {
118+
debug(this instanceof db.Query); // true
119+
this.start = Date.now();
120+
next();
121+
});
122+
123+
Schema.post('find', function(result) {
124+
debug(this instanceof db.Query); // true
125+
// prints returned documents
126+
debug('find() returned ' + JSON.stringify(result));
127+
// prints number of milliseconds the query took
128+
debug('find() took ' + (Date.now() - this.start) + ' millis');
129+
});
130+
131+
Schema.pre('update', function(next) {
132+
// Adding updated date
133+
134+
// Indexing for search
135+
var ourDoc = this._update.$set;
136+
var split = [];
137+
for(var n in ourDoc){
138+
if(typeof ourDoc[n] === 'string'){
139+
split.push(ourDoc[n].split(' '));
140+
}
141+
}
142+
143+
if(!split){
144+
this.update(this._conditions,{ $set: { updatedAt: new Date()} });
145+
}else{
146+
this.update(this._conditions,{ $set: { updatedAt: new Date()}, $addToSet: {tags: {$each: split}} });
147+
}
148+
149+
next();
150+
});
151+
152+
var Model = db.model(collection, Schema);
153+
154+
module.exports = Model;

package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
2-
"name": "snipe",
2+
"name": "api-template",
33
"version": "0.0.1",
4-
"description": "API service for a freelancer marketplace",
4+
"description": "A template for creating APIs Fast",
55
"main": "app.js",
66
"scripts": {
77
"test": "gulp sanity"
88
},
99
"repository": {
1010
"type": "git",
11-
"url": "git+https://github.com/EnsembleLab/snipe.git"
11+
"url": "git+https://github.com/EnsembleLab/api-template.git"
1212
},
1313
"keywords": [
14-
"marketplace",
15-
"freelance"
14+
"REST",
15+
"API"
1616
],
1717
"author": "Olufemi Olanipekun <[email protected]>",
1818
"license": "GPL-3.0",
1919
"bugs": {
20-
"url": "https://github.com/EnsembleLab/snipe/issues"
20+
"url": "https://github.com/EnsembleLab/api-template/issues"
2121
},
22-
"homepage": "https://github.com/EnsembleLab/snipe#readme",
22+
"homepage": "https://github.com/EnsembleLab/api-template#readme",
2323
"devDependencies": {
2424
"chai": "^3.5.0",
2525
"chai-as-promised": "^6.0.0",
@@ -29,7 +29,11 @@
2929
"gulp-nodemon": "^2.2.1",
3030
"jshint": "^2.9.4",
3131
"jshint-stylish": "^2.2.1",
32-
"mocha": "^3.2.0"
32+
"mocha": "^3.2.0",
33+
"mongoose-mock": "^0.4.0",
34+
"proxyquire": "^1.8.0",
35+
"sinon": "^2.3.4",
36+
"sinon-chai": "^2.11.0"
3337
},
3438
"dependencies": {
3539
"aes-js": "^3.0.0",

services/database/mongo.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
var mongoose = require('mongoose');
3+
var config = require('../../config');
4+
var log = require('../../services/logger');
5+
6+
// Connect to DB
7+
mongoose.connect(config.mongoURL);
8+
9+
// Set the prefered promise library
10+
mongoose.Promise = require('q').Promise;
11+
12+
var db = mongoose.connection;
13+
14+
db.on('error', log.error);
15+
db.once('open', function() {
16+
log.info('Database connection successful');
17+
});
18+
19+
module.exports = mongoose;

0 commit comments

Comments
 (0)