简体   繁体   English

Koa:在没有现有包装的情况下连接数据库的最明智的方法是什么?

[英]Koa: What is the most sensible way to connect to a database without an existing wrapper?

I am very new to node & koa, so please excuse my stupidity. 我是Node&Koa的新手,请原谅我的愚蠢。

I am not sure if I mess something up. 我不确定我是否搞砸了。 But I would like to use Koa together with OrientDB. 但是我想将Koa与OrientDB一起使用。 I can connect to OrientDB using Oriento (the module for Node). 我可以使用Oriento(Node的模块)连接到OrientDB。 And I would like to use the power of generators of Koa. 我想使用Koa发电机的力量。

Since the data in my OrientDB database relates to objects I'm using in my app, I would like to implement models (of course). 由于OrientDB数据库中的数据与我在应用程序中使用的对象有关,因此我想实现模型(当然)。 So I guess the connecting to the database part would go in to that. 因此,我想连接到数据库部分会涉及到这一点。

Say I had a model named "Task" then I would like it to expose a couple of methods and getters/setter. 假设我有一个名为“ Task”的模型,那么我希望它公开一些方法和getters / setter方法。 So Task.find(); 所以Task.find(); should get all Tasks from the OrientDB Database 应该从OrientDB数据库中获取所有任务

As far as I understand it, I would hook that somewhere in the middleware stack. 据我了解,我会将其钩在中间件堆栈中的某个位置。 And it would be nice if I could use generators so that my middleware waits until it gets the data back, using yield. 如果可以使用生成器,以便中间件使用yield来等待数据返回,那将是很好的选择。 Some error handling would be good as well... 一些错误处理也将很好。

With all that said: 话虽如此:

  • Are my assumptions correct? 我的假设正确吗? Or is there a better way? 或者,还有更好的方法?
  • Do I have to do that all myself? 我必须自己做吗? Or am I missing modules that facilitate what I am planning? 还是我缺少有助于我计划的模块?
  • What would be a good point to start learning on how to properly do something like that? 开始学习如何正确执行类似操作的好点是什么?
  • Should I just look at existing wrappers for mongodb/mysql/whatever and abstract from that? 我应该只看一下mongodb / mysql / what的现有包装,然后从中进行抽象吗?

Thanks! 谢谢!

I've never used orient-db, but looking at the github page it looks like it offers a connection pool and returns promises. 我从未使用过orient-db,但在github页面上看,它似乎提供了连接池并返回了promise。 Based on that I would do something like this: 基于此,我将执行以下操作:

// in utils/oriento.js or similar
// instantiate it once and share in multiple places(models), since it offers 
// connection pooling
var Oriento = require('oriento');
var server = Oriento({
  host: 'localhost',
  port: 2424,
  username: 'root',
  password: 'yourpassword'
});
var db = server.use({
  name: 'mydb',
  username: 'admin',
  password: 'admin'
});
module.exports = db;

then for models: 然后对于模型:

// models/item.js 
var db = require('../utils/oriento.js');

var Item = function(props){

};

Item.findById = function(id){
    // db.query returns a promise
    return db.query('select from OItem where id=:id', {
      params: {
        id: id
      },
      limit: 1
    });

}
// add getters, setters, find etc...
module.exports = Item;

then in controllers: 然后在控制器中:

// server.js or wherever

var Item = require('./models/item');

app.get('/:id', function *(){
    var id = this.params.id;
    this.body = yield Item.findById(id);
}

Hopefully this helps 希望这会有所帮助

You might want to checkout my screencasts on koajs at http://knowthen.com 您可能想在http://knowthen.com上查看有关koajs的截屏视频

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预加载一批音频文件的最明智的方法 - Most sensible way of preloading a batch of audio files 将XML中的Web数据传递到SQL Server数据库的明智方法 - Sensible Way to Pass Web Data in XML to a SQL Server Database 在带有express和connect的节点中使用中间件的最常见方法是什么 - What is the most common way to use a middleware in node with express and connect 在ng-repeat中的特定点插入DOM元素的最明智的方法? - Most sensible way to insert DOM element at specific point within ng-repeat? 用Koa / Javascript实现Scala`Either [A,B]`结构的方法是什么 - What is a way to implement a scala `Either[A,B]` structure in Koa / Javascript 在代码中找到现有的Google表格图表类型的最有效方法是什么(Google脚本)? - What is the most effective way to find an already existing Google Sheets chart type in code (Google Scripts)? 将Map复制到现有Map中的最佳方法 - Most performant way to copy a Map into an existing Map 在JavaScript中标准化现有数据集的最有效方法 - Most efficient way to Normalized an existing dataset in javascript 在不重新渲染RactiveJS中的整个DOM的情况下,用对象更新数组的最有效方法是什么? - What is the most efficient way to update an array with objects without re-rendering the entire DOM in RactiveJS? javascript / jQuery - 在不替换整个正文的情况下替换正文中所有字符串的最有效方法是什么 - javascript / jQuery - What is the most efficiënt way to replace all the occurrences of a string in a body without replacing the entire body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM