javascript node.js 的哪个 ORM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19447850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which ORM for node.js?
提问by Daniele Salvatore Albano
I know this is a common question but i've done tests and i need some particular features!
我知道这是一个常见问题,但我已经做过测试,我需要一些特殊的功能!
The features i need are:
我需要的功能是:
- map properties to column names
- use a table name different from the model name
- support for soft-deletes (paranoid mode on sequelize)
- support for record timestamping (with the ability to specify, for every different model, the column name)
- support for foreign keys
- must support mysql and sqlite
- the architecture must support a model per file
- 将属性映射到列名
- 使用与模型名称不同的表名称
- 支持软删除(续集的偏执模式)
- 支持记录时间戳(能够为每个不同的模型指定列名)
- 支持外键
- 必须支持mysql和sqlite
- 架构必须支持每个文件的模型
Optional features:
可选功能:
- cache (support for redis/memcache)
- command line tool to generate models from database
- 缓存(支持 redis/memcache)
- 从数据库生成模型的命令行工具
I've tested:
我测试过:
- node-orm
- to handle own column names you need a workaround
- soft deletes aren't supported and can't be supported even using an external plugin (i tried to wrote one using beforeRemove hook, but i can "stop" it from removing the record)
- don't support a model per file (you need a workaround)
- sequelize
- don't create foreign keys
- can't map properties to column names
- support a model per file but it doesn't work very well (you need to put relationship in the file that include the models)
- node-persist
- i don't like the need to specify the connection instance for everything
- 节点形式
- 要处理自己的列名,您需要一种解决方法
- 不支持软删除,即使使用外部插件也无法支持(我尝试使用 beforeRemove 钩子编写一个,但我可以“阻止”它删除记录)
- 不支持每个文件的模型(您需要一个解决方法)
- 续集
- 不要创建外键
- 无法将属性映射到列名
- 支持每个文件的模型,但效果不佳(您需要将关系放在包含模型的文件中)
- 节点持久化
- 我不喜欢需要为所有内容指定连接实例
Right now i'm going to test JugglingDB and Bookshelf.js (but i don't like too much the last one).
现在我要测试 JugglingDB 和 Bookshelf.js(但我不太喜欢最后一个)。
回答by tgriesser
Bookshelf should currently support all of those:
Bookshelf 目前应该支持所有这些:
- The mapping of columns to property names with the formatand parsemethods.
- Using a different table name with the tableNameattribute.
- Timestamping can take custom columns with the hasTimestampattribute.
- Foreign keys can be defined with knexschema builder... they're not well documented but you can see an example in the tests here
- Supports mysql, sqlite and postgres
Definitely supports one model per file... the relations are defined in methods, so you can do:
var Classroom = Bookshelf.Model.extend({ tableName: 'classrooms', student: function() { // Relating to a model from a file in the same directory. return this.hasMany(require('./student')); } }); new Classroom({id: 1}) .fetch({withRelated: ['students']) .then(function(classroom) { console.log(JSON.stringify(classroom)); });
- 使用格式和 解析方法将列映射到属性名称 。
- 使用具有tableName属性的不同表名 。
- 时间戳可以采用带有hasTimestamp属性的自定义列 。
- 外键可以用knex模式构建器定义 ……它们没有很好的文档记录,但您可以在此处的测试中看到一个示例
- 支持 mysql、sqlite 和 postgres
绝对支持每个文件一个模型……关系在方法中定义,因此您可以执行以下操作:
var Classroom = Bookshelf.Model.extend({ tableName: 'classrooms', student: function() { // Relating to a model from a file in the same directory. return this.hasMany(require('./student')); } }); new Classroom({id: 1}) .fetch({withRelated: ['students']) .then(function(classroom) { console.log(JSON.stringify(classroom)); });
Official soft delete support is in the works, but is definitely easily achieved by extending the model and providing a new destroy method like so:
官方软删除支持正在开发中,但通过扩展模型并提供新的销毁方法绝对可以轻松实现,如下所示:
destroy: function(options) {
if (options.softDelete) {
return this.save({'deleted_at': new Date});
}
return bookshelf.Model.prototype.destroy.call(this, arguments);
}
It doesn't hook into a cache yet, because cache invalidation on relations is pretty tricky, definitely something under consideration.
它还没有连接到缓存中,因为关系上的缓存失效非常棘手,肯定是在考虑中。
If you see anything that seems to be missing, feel free to open a ticket.
如果您看到任何似乎丢失的东西,请随时开票。
回答by Oleksandr Knyga
You may take a look at light-orm: https://github.com/knyga/light-ormSimple ORM. Don't create foreign keys. Can't map properties to column names. But could be connected to any rdbms, and super simple.
你可以看看 light-orm:https: //github.com/knyga/light-ormSimple ORM。不要创建外键。无法将属性映射到列名。但是可以连接到任何rdbms,而且超级简单。