在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Jackong/koa-mongoose开源软件地址:https://github.com/Jackong/koa-mongoose开源编程语言:JavaScript 100.0%开源软件介绍:koa-mongoose
InstallFeatures
ExamplesWith modelsconst Koa = require('koa')
const mongoose = require('koa-mongoose')
const User = require('./models/user')
const app = new Koa()
app.use(mongoose({
user: '',
pass: '',
host: '127.0.0.1',
port: 27017,
database: 'test',
mongodbOptions:{
poolSize: 5,
native_parser: true
}
}))
app.use(async (ctx, next) => {
let user = new User({
account: 'test',
password: 'test'
})
await user.save()
ctx.body = 'OK'
}) With schemasconst Koa = require('koa')
const mongoose = require('koa-mongoose')
const app = new Koa()
app.use(mongoose({
username: '',
password: '',
host: '127.0.0.1',
port: 27017,
database: 'test',
schemas: './schemas',
mongodbOptions:{
poolSize: 5,
native_parser: true
}
}))
app.use(async (ctx, next) => {
let User = ctx.model('User')
let user = new User({
account: 'test',
password: 'test'
})
//or
let user = ctx.document('User', {
account: 'test',
password: 'test'
})
await user.save()
ctx.body = 'OK'
}) With databaseconst Koa = require('koa')
const mongoose = require('koa-mongoose')
const app = new Koa()
app.use(mongoose({
username: '',
password: '',
host: '127.0.0.1',
port: 27017,
database: ctx => {
return ctx.headers['x-app']
},
schemas: './schemas',
mongodbOptions:{
poolSize: 5,
native_parser: true
}
}))
app.use(async ctx => {
let user = ctx.document('User', {
account: 'test',
password: 'test'
})
await user.save()
ctx.body = 'OK'
}) With uriconst Koa = require('koa')
const mongoose = require('koa-mongoose')
const app = new Koa()
app.use(mongoose({
uri:'mongodb://<user>:<pass>@<host>:<port>/<db-name>',
mongodbOptions:{
poolSize: 5,
native_parser: true
}
}))
app.use(async (ctx, next) => {
let User = ctx.model('User')
let user = new User({
account: 'test',
password: 'test'
})
//or
let user = ctx.document('User', {
account: 'test',
password: 'test'
})
await user.save()
ctx.body = 'OK'
}) With eventsconst Koa = require('koa')
const mongoose = require('koa-mongoose')
const app = new Koa()
app.use(mongoose({
uri:'mongodb://<user>:<pass>@<host>:<port>/<db-name>',
mongodbOptions:{
poolSize: 5,
native_parser: true
},
events: {
connected: ()=>{
console.log('hello there')
}
}
})) Testscd test && docker-compose up -d
HOST=YOUR-DOCKER-HOST-WITHOUT-PORT npm test Licences |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论