在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:d-band/koa-orm开源软件地址:https://github.com/d-band/koa-orm开源编程语言:JavaScript 100.0%开源软件介绍:koa-ormInstallationnpm install koa-orm ExampleSingle database const join = require('path').join;
const config = {
name: 'test',
modelPath: join(__dirname, 'models'),
database: 'orm_test',
username: 'root',
password: 'pass',
dialect: 'mysql',
host: '127.0.0.1',
port: 3306,
pool: {
max: 10,
min: 0,
idle: 30000
}
};
const orm = require('koa-orm')(config);
app.use(orm.middleware);
app.use(async function (ctx) {
const raws = await ctx.orm().sql.select().from('table');
// const raws = await ctx.orm('test').sql('table').select();
ctx.body = raws;
}); Multiple database const join = require('path').join;
const configs = [{
name: 'user',
database: 'db_user',
username: 'root',
password: 'pass',
dialect: 'mysql',
host: '127.0.0.1',
port: 3306,
modelPath: join(__dirname, 'models/user')
}, {
name: 'product',
database: 'db_product',
username: 'root',
password: 'pass',
dialect: 'mysql',
host: '127.0.0.1',
port: 3306,
modelPath: join(__dirname, 'models/product')
}];
const orm = require('koa-orm')(configs);
app.use(orm.middleware);
app.use(async function (ctx) {
const { User } = ctx.orm('user');
const { Product } = ctx.orm('product');
const { userId } = ctx.params;
const user = await User.findByPk(userId);
const products = await Product.findAll({
where: { userId }
});
ctx.body = { user, products };
}); API
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论