Leoric,又一个号称借鉴 Active Record 的 ORM 轮子
发布于 6 年前 作者 dotnil 3381 次浏览 来自 分享

Leoric 是一个类似 Active Record 的 JavaScript 的 ORM 模块,从一个内部版本中重构出的方案。先前在 reddit.com/r/node 发过一则广告帖,要回答的第一个问题应该都是差不多的,就是“Leoric 和市面上现有的 JavaScript ORM 模块相比优势在哪?”

Leoric 非常新,在性能方面测试还不充分,就目前而言,它的优势在于它的 API 非常简单而且强大。例如在编写查询条件时,遇到 || 复合条件的时候,用 mongoose 或者 knex 可能需要这样写:

Table.find({ $or: [ { foo: null }, { foo: values } ] })
// or with knex
Table.where({ foo: null }).orWhere({ foo: values })

用 Leoric 只需:

Table.find('foo = null or foo = ?', values)

如果还要考虑伪删除(不真正 DELETE 记录,而是使用 deleted_at 字段标记),在 mongoose 或者 knex 里是:

// MongoDB-like
Table.find({ $and: [ { $or: [ { foo: null }, { foo: values } ] }, { deletedAt: null }]
// formatted
Table.find({
  $and: [
    { 
      $or: [ 
        { foo: null },
        { foo: values }
      ]
    },
	{ deletedAt: null }
  ]
})
// or with knex
Table.where(function() {
  this.where({ foo: null }).orWhere({ foo: values })
}).andWhere({ deletedAt: null })

在 Leoric 里则是:

Table.find('(foo = null or foo = ?) and deletedAt is null', values)

另一个可以体现 Leoric 和其他 ORM 模块区别的地方是关联关系的配置与使用,以 Objection.js 为例,要设置一个 belongsTo 关联:

class Animal extends Model {
  static relationMappings = {
    owner: {
      relation: Model.BelongsToOneRelation,
      modelClass: Person,
      join: {
        from: 'animal.ownerId',
        to: 'person.id'
      }
    }
  }
}

在 Leoric 里则是:

class Animal extends Bone {
  static describe() {
    this.belongsTo('owner', { Model: 'Person' })
  }
}

Leoric 是我在 2017 年交出的两份 Node.js 答卷之一,希望能给各位 Web developer 带来帮助。欢迎阅读官方介绍文档、以及 Github

5 回复

又是这种魔符啊。

多用函数式和声明式,才是符合人类的操作吧。

$or操作都也没什么不好,前端需要多条件的复杂查询,直接传个Object过来就很省事了。

而不用拼这么一个字符串,或者服务端把Object转成这样的字符串

来自酷炫的 CNodeMD

“这种魔符”指的是啥哇? 其实这里的重点不是讨论 object condition 和 templated string condition 哪个更好,只是想给更喜欢 template string condition 的 Node.js 开发者一个选择。 “而不用拼这么一个字符串,或者服务端把 Object 转成这样的字符串” 这一句我也不太懂,尤其是后半句。

@dotnil 魔符就是magic string, 简单来说, 学习这个工具, 还得学习这个工具的模板语法.

“而不用拼这么一个字符串,或者服务端把 Object 转成这样的字符串”

比如客户端需要查询 这个条件

$or: [ 
        { foo: null },
        { foo: values }
      ]

客户端可以直接把这个json丢给服务端, 服务端拿到就可以直接查询.

如果服务端的ORM需要这种格式(foo = null or foo = ?) and deletedAt is null, 那么要么是客户端转换, 要么是服务端转换

头一次见到这样的翻译,我孤陋寡闻了哈哈哈。 (foo = null or foo = ?) and deletedAt is null 只是一个条件表达式,如果觉得 is null 太 sql,写成 (foo = null or foo = ?) and deletedAt = null 除了 == 变成 =,其余跟 js 语句毫无差别。只要底层是使用 sql 交互的关系型数据库,客户端无论是传 object 还是 string,最终都还是得转成 (foo = null or foo = ?) and deletedAt is null 的格式。这句表达式对应的 object condition 写法完整的是:

{
  $and: [
    { 
      $or: [ 
        { foo: null },
        { foo: values }
      ]
    },
	{ deletedAt: null }
  ]
}

我是觉得这样太繁琐了,相当于我帮别人 ORM 库写 sql ast,再让它帮我序列化出字符串跟 mysql 或者其他数据库交互,因此造了这个轮子。如果你觉得写 object condition 很直观,那确实 Leoric 不太适合你的使用习惯。

同意一楼,这样反而倒退了

回到顶部