Objection.js

适用于 Node.js 的 SQL 友好型 ORM。『An SQL-friendly ORM for Node.js』

Github stars Tracking Chart

Objection.js 是 Node.jsORM,它的目标是不妨碍你的工作,让你尽可能轻松地使用SQL和底层数据库引擎的全部功能,同时还能让你轻松愉快地使用常用功能。

尽管 ORM 是描述 objection 最常用的缩写,但更准确的描述是称其为关系查询生成器。它不仅拥有 SQL 查询生成器的所有优点,还拥有一套强大的关系处理工具。

Objection.js 基于名为 knex 的 SQL 查询生成器。objection.js 支持 knex 支持的所有数据库。SQLite3、Postgres 和 MySQL已通过全面测试(thoroughly tested)。

objection.js 能为你带来什么?

objection.js 无法为你提供的功能:

  • 完全面向对象的数据库视图。使用 objection,你不用处理实体。而是查询。反对并不试图用面向对象的等价物来包装每一个概念。Hibernate 是做到这一点的最佳尝试(我认为),它非常出色,但它有80万行代码,需要学习的概念比 SQL 本身要多得多。关键是,编写一个优秀的传统 ORM 是一项艰巨的任务。Objection 试图提供一种完全不同的 SQL 工作方式。
  • 自定义查询 DSL。SQL 是一种查询语言。但这并不意味着你必须编写 SQL 字符串。我们使用基于 knex 的查询生成器来构建 SQL。不过,如果查询生成器因某种原因失效,也可以使用原始辅助函数轻松编写原始(raw)SQL 字符串。
  • 根据模型定义自动创建和迁移数据库模式。从模型定义中自动生成数据库模式对于简单的事情很有用,但通常在做一些非小事时会碍手碍脚。Objection.js 将与模式相关的工作留给了你。Knex 有一个很棒的迁移工具(migration tool),我们向你推荐它。请查看示例项目(example project)。

最好的入门方法就是克隆我们的示例项目(example project)并开始使用。我们还有一个 Typescript 版本(typescript version)。

查看本期内容(this issue),了解谁在使用 objection 以及他们的看法。

快捷方式:

 

(First edition: vz edited at 2024.11.24)

Main metrics

Overview
Name With OwnerVincit/objection.js
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 4)
Platform
License:MIT License
所有者活动
Created At2015-04-14 10:36:51
Pushed At2025-01-09 17:59:56
Last Commit At
Release Count196
Last Release Name3.1.5 (Posted on )
First Release Name0.1.0 (Posted on 2015-06-13 21:19:41)
用户参与
Stargazers Count7.3k
Watchers Count77
Fork Count642
Commits Count2.2k
Has Issues Enabled
Issues Count1949
Issue Open Count93
Pull Requests Count470
Pull Requests Open Count29
Pull Requests Close Count278
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Build Status Coverage Status Join the chat at https://gitter.im/Vincit/objection.js

Objection.js

Objection.js is an ORM for Node.js that aims to stay out of your way and make it as easy as possible to use the full power of SQL and the underlying database engine while still making the common stuff easy and enjoyable.

Even though ORM is the best commonly known acronym to describe objection, a more accurate description is to call it a relational query builder. You get all the benefits of an SQL query builder but also a powerful set of tools for working with relations.

Objection.js is built on an SQL query builder called knex. All databases supported by knex are supported by objection.js. SQLite3, Postgres and MySQL are thoroughly tested.

What objection.js gives you:

What objection.js doesn't give you:

  • A fully object oriented view of your database
    With objection you don't work with entities. You work with queries. Objection doesn't try to wrap every concept with an
    object oriented equivalent. The best attempt to do that (IMO) is Hibernate, which is excellent, but it has 800k lines
    of code and a lot more concepts to learn than SQL itself. The point is, writing a good traditional ORM is borderline
    impossible. Objection attempts to provide a completely different way of working with SQL.
  • A custom query DSL. SQL is used as a query language.
    This doesn't mean you have to write SQL strings though. A query builder based on knex is
    used to build the SQL. However, if the query builder fails you for some reason, raw SQL strings can be easily
    written using the raw helper function.
  • Automatic database schema creation and migration from model definitions.
    For simple things it is useful that the database schema is automatically generated from the model definitions,
    but usually just gets in your way when doing anything non-trivial. Objection.js leaves the schema related things
    to you. knex has a great migration tool that we recommend for this job. Check
    out the example project.

The best way to get started is to clone our example project and start playing with it. There's also a typescript version available.

Check out this issue to see who is using objection and what they think about it.

Shortcuts: