Created
March 31, 2017 10:38
-
-
Save hailiang-wang/352fc1088a1a89796cfaf83c7ad42ffd to your computer and use it in GitHub Desktop.
TypeScript Mongoose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Database management | |
*/ | |
import * as mongoose from 'mongoose'; | |
import logging from './logging.service'; | |
import config from '../config/environment'; | |
const logger = logging.getLogger('database.service'); | |
mongoose.Promise = Promise; | |
// 链接数据库 | |
mongoose.connect(config.mongo.uris, config.mongo.options); | |
let database = mongoose.connection; | |
database.on('connected', function() { | |
logger.info('[MongoDB]: connected.'); | |
}); | |
database.once('open', function() { | |
logger.info('[MongoDB]: opened.'); /*global.gfs = Grid(database.db);*/ | |
}); | |
database.on('error', function(err) { | |
logger.error(err); | |
}); | |
database.on('disconnected', function() { | |
logger.warn('[MongoDB]: disconnected.'); | |
}); | |
database.on('reconnected', function() { | |
logger.info('[MongoDB]: reconnected.'); | |
}); | |
process.on('SIGINT', function() { | |
database.close(function() { | |
logger.info('[MongoDB]: App exits.'); | |
process.exit(0); | |
}); | |
}); | |
export {database, mongoose}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Message Model | |
*/ | |
// https://github.com/Appsilon/styleguide/wiki/mongoose-typescript-models | |
import {mongoose} from '../services/database.service'; | |
import * as timestamps from 'mongoose-timestamp'; | |
interface IMessage { | |
sender: string; | |
recipient: string; | |
group: string; | |
channel: string; | |
agent: string; | |
textMessage: string; | |
}; | |
interface IMessageModel extends IMessage, mongoose.Document { } | |
var messageSchema = new mongoose.Schema({ | |
sender: String, | |
recipient: String, | |
group: String, | |
channel: String, | |
agent: String, | |
textMessage: String | |
}); | |
messageSchema.plugin(timestamps); | |
var Message = mongoose.model<IMessageModel>("Message", messageSchema); | |
export {IMessage, IMessageModel, Message}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { test } from 'ava'; | |
import * as MessageModel from '../models/message.model'; | |
let Message = MessageModel.Message; | |
test.only('Message Test', async t => { | |
console.log('saving ...'); | |
try { | |
let msg = await (new Message({ | |
agent: 'foo', | |
channel: 'bar', | |
sender: 'send', | |
recipient: 'foo', | |
group: 'ss', | |
textMessage: 'foo' | |
})).save(); | |
console.log('saved', JSON.stringify(msg)); | |
t.pass(); | |
} catch (e) { | |
t.fail(e); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment