Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
typescript + mongoose
/// <reference path="../typings/tsd.d.ts" />
import db = require('../db/db');
import IAuthorDocument = require('../db/IAuthorDocument');
class Author {
private _author:IAuthorDocument;
constructor(author:IAuthorDocument) {
this._author = author;
}
get name():string {
return this._author.name;
}
get isValid():boolean {
return !!this._author;
}
public static createNewAuthor = (name:string, email:string, callback:(err:any, author:Author)=>void) => {
db.Author.createNewAuthor(name, email, (err:any, author:IAuthorDocument)=> {
callback(err, new Author(author));
});
};
public static getById = (authorId:string, callback:(err:any, author?:IAuthorDocument)=>void) => {
db.Author.findById(authorId, (err:any, author:IAuthorDocument) => {
if(err) {
return callback(err);
}
if(!author) {
return callback(new Error('not found.'), author);
}
callback(null, author);
});
};
}
export = Author;
/// <reference path="../typings/tsd.d.ts" />
import mongoose = require('mongoose');
import IAuthorDocument = require('IAuthorDocument');
import IAuthorDocumentModel = require('IAuthorDocumentModel');
var AuthorSchema:mongoose.Schema = new mongoose.Schema({
email: String,
name: {type: String, required: true, index: {unique: true}},
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now }
});
AuthorSchema.static('createNewAuthor',
(name:string, email:string, callback:(err:any, result:IAuthorDocument)=>void) => {
AuthorDocumentModel.findByName(name, (err:any, author:IAuthorDocument) => {
if(err) {
return callback(err, null);
}
if(author) {
return callback(new Error('already exists.'), author);
}
author = <IAuthorDocument>new AuthorDocumentModel({name: name, email: email});
author.save(callback);
});
});
AuthorSchema.static('findByName', (name:string, callback:(err:any, result:IAuthorDocument)=>void)=> {
AuthorDocumentModel.findOne({name: name}, callback);
});
var AuthorDocumentModel:IAuthorDocumentModel = <IAuthorDocumentModel>mongoose.model('Author', AuthorSchema);
export = AuthorDocumentModel;
/// <reference path="../typings/tsd.d.ts" />
import mongoose = require('mongoose');
import AuthorDocumentModel = require('./AuthorDocumentModel');
import IAuthorDocumentModel = require('./IAuthorDocumentModel');
class db {
public static connect(db:string) {
mongoose.connect('mongodb://' + db);
}
public static debug(debug:any) {
mongoose.set('debug', debug);
}
static get Author():IAuthorDocumentModel {
return <IAuthorDocumentModel>AuthorDocumentModel;
}
}
export = db;
/// <reference path="../typings/tsd.d.ts" />
import mongoose = require('mongoose');
interface IAuthorDocument extends mongoose.Document {
email: string;
name: string;
created: Date;
updated: Date;
}
export = IAuthorDocument;
/// <reference path="../typings/tsd.d.ts" />
import mongoose = require('mongoose');
import IAuthorDocument = require('IAuthorDocument');
interface IAuthorDocumentModel extends mongoose.Model<IAuthorDocument> {
findByName:(name:string, callback:(err:any, author:IAuthorDocument)=>void)=>void;
createNewAuthor:(name:string, email:string, callback:(err:any, result:IAuthorDocument)=>void)=>void;
}
export = IAuthorDocumentModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.