kenote-mongoose-helper
v1.1.4
Published
Mongoose's Helper.
Downloads
2
Maintainers
Readme
mongoose-helper
Mongoose's Helper.
Premise
$ npm install mongoose
$ npm install --save-dev @types/mongoose @types/bluebird @types/lodash
# Or
$ yarn add mongoose
$ yarn add -D @types/mongoose @types/bluebird @types/lodash
Installation
$ npm install kenote-mongoose-helper
#
$ yarn add kenote-mongoose-helper
Usages
models/index.ts
import * as mongoose from 'mongoose'
import { Connector, Connect, MountModels } from 'kenote-mongoose-helper'
import userModel from './user'
@Connect({
uris: 'mongodb://localhost:27017/mongodb_test'
})
@MountModels({ userModel })
class MongoDB extends Connector {}
const DB: Connector = new MongoDB()
DB.connect()
export default DB.__Models
models/user.ts
import { Schema, model } from 'mongoose'
export default model('user', new Schema({
id: {
type: Number,
default: 0,
index: { unique: true }
},
username: {
type: String,
required: true
}
}))
proxy/user.ts
import * as mongoose from 'mongoose'
import { MongooseDao, autoNumber, QueryOptions } from 'kenote-mongoose-helper'
import __Model from '../models'
const Model: mongoose.Model<mongoose.Document, {}> = __Models.userModel
const options: QueryOptions = {
name: 'user',
populate: { path: '' },
seqModel: __Models.seqModel
}
@autoNumber({
idName: 'id'
})
class UserDao extends MongooseDao {}
class UserProxy {
public Dao: MongooseDao = new UserDao(Model, options)
}
export default new UserProxy()
**/user.ts
import * as mongoose from 'mongoose'
import userProxy from '../proxy/user'
async function createUser (doc: any): mongoose.Document | null {
try {
let user: mongoose.Document | null = userProxy.Dao.create(doc)
return user
} catch (error) {
console.erroe(error)
}
}
License
this repo is released under the MIT License.