原文: http://zetcode.com/javascript/bookshelf/

Bookshelf.js 教程展示了如何使用 Bookshelf.js ORM 在 JavaScript 中对数据库进行编程。 Bookshelf.js 构建在 Knex 之上。

Bookshelf.js

Bookshelf.js 是基于 Knex SQL 查询生成器构建的 Node.js 的 JavaScript ORM。 它支持基于promise和传统的回调接口。 书架提供事务支持,渴望/嵌套渴望的关系加载,多态关联以及对一对一,一对多和多对多关系的支持。

Bookshelf.js 可与 PostgreSQL,MySQL 和 SQLite3 一起使用。

对象关系映射(ORM)是一种从面向对象的语言访问关系数据库的技术。 它是 Python 数据库 API 的抽象。 在本教程中,我们使用 PostgreSQL。

城市表

我们使用城市表。

cities_postgresql.sql

  1. DROP TABLE IF EXISTS cities;
  2. CREATE TABLE cities(id serial PRIMARY KEY, name VARCHAR(255), population INT);
  3. INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
  4. INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
  5. INSERT INTO cities(name, population) VALUES('Prague', 1280000);
  6. INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
  7. INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
  8. INSERT INTO cities(name, population) VALUES('New York', 8550000);
  9. INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
  10. INSERT INTO cities(name, population) VALUES('Berlin', 3671000);

安装 Bookshelf.js

我们安装书架。

  1. $ node -v
  2. v11.5.0

我们使用 Node 版本 11.5.0。

  1. $ npm init -y

我们启动一个新的 Node 应用。

  1. $ npm i pg
  2. $ npm i knex bookshelf

我们安装了 PostgreSQL 驱动程序 Knex.js 和 Bookshelf.js。

Bookshelf.js计数行

在第一个示例中,我们计算cities表中的行数。

config/db.js

  1. const knex = require('knex')({
  2. client: 'pg',
  3. connection: {
  4. host: '127.0.0.1',
  5. user: 'postgres',
  6. password: '',
  7. database: 'testdb',
  8. charset: 'utf8'
  9. }
  10. });
  11. module.exports.knex = knex;

db.js文件中,我们定义一个 Knex 客户端对象。

model/city.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const City = bookshelf.Model.extend({
  4. tableName: 'cities'
  5. });
  6. module.exports = City;

我们有模型对象。 模型对象映射到数据库表中的一行。

count_cities.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. City.count().then((count) => {
  4. console.log(`There are ${count} cities`);
  5. }).catch((err) => {
  6. console.log(err);
  7. }).finally(() => {
  8. knex.destroy();
  9. });

该示例计算cities表中的行数。 它使用回调。

  1. $ node count_cities.js
  2. There are 8 cities

这是输出。

count_cities2.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. async function countCities() {
  4. try {
  5. let count = await City.count();
  6. console.log(`There are ${count} cities`);
  7. } catch (e) {
  8. logger.info(`No data found ${e}`);
  9. } finally {
  10. knex.destroy();
  11. }
  12. }
  13. countCities();

在第二个示例中,我们将promiseasync/await一起使用。

Bookshelf.js fetch()

fetch()使用当前在模型上设置的任何属性来从数据库获取模型,以形成选择查询。 设置require选项后,如果结果为空,则返回的响应将被NotFoundError拒绝。

  1. $ npm i winston

在此示例中,我们还使用 Winston 日志记录模块。

fetch_city.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. const winston = require('winston');
  4. const consoleTransport = new winston.transports.Console()
  5. const options = {
  6. transports: [consoleTransport]
  7. }
  8. const logger = new winston.createLogger(options)
  9. async function fetch_city() {
  10. try {
  11. let val = await City.where({ 'name': 'Bratislava' }).fetch({require:true});
  12. // console.log(val.toJSON());
  13. logger.info(val);
  14. } catch (e) {
  15. logger.info(`No data found ${e}`);
  16. } finally {
  17. knex.destroy();
  18. }
  19. }
  20. fetch_city();

该示例检索指定的城市。

  1. let val = await City.where({ 'name': 'Bratislava' }).fetch({require:true});

我们获取一个名为'Bratislava'的模型。

  1. logger.info(val);

我们记录返回的数据。

  1. $ node fetch_city.js
  2. {"message":{"id":1,"name":"Bratislava","population":432000},"level":"info"}

这是输出。

Bookshelf.js fetch_all

fetch_all()使用当前在模型上设置的任何查询参数从数据库中获取模型的集合,以形成选择查询。

fetch_all.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. async function fetch_all() {
  4. try {
  5. let vals = await City.fetchAll();
  6. console.log(vals.toJSON());
  7. } catch (e) {
  8. console.log(`Failed to fetch data: ${e}`);
  9. } finally {
  10. knex.destroy();
  11. }
  12. }
  13. fetch_all();

该示例检索所有城市。

  1. let vals = await City.fetchAll();

我们称为fetchAll()函数。

  1. console.log(vals.toJSON());

数据以 JSON 格式写入控制台。

  1. $ node fetch_all.js
  2. [ { id: 1, name: 'Bratislava', population: 432000 },
  3. { id: 2, name: 'Budapest', population: 1759000 },
  4. { id: 3, name: 'Prague', population: 1280000 },
  5. { id: 4, name: 'Warsaw', population: 1748000 },
  6. { id: 5, name: 'Los Angeles', population: 3971000 },
  7. { id: 6, name: 'New York', population: 8550000 },
  8. { id: 7, name: 'Edinburgh', population: 464000 },
  9. { id: 8, name: 'Berlin', population: 3671000 } ]

这是输出。

Bookshelf.js forge()辅助函数

Bookshelf 的forge()是一个简单的辅助函数,可在不需要new关键字的情况下实例化新模型。

forge_helper.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. async function fetch_city() {
  4. try {
  5. let val = await City.forge({ 'id': '4' }).fetch();
  6. console.log(val.toJSON());
  7. } catch (e) {
  8. console.info(`No data found ${e}`);
  9. } finally {
  10. knex.destroy();
  11. }
  12. }
  13. fetch_city();

在示例中,我们使用forge()帮助器选择一个城市。

Bookshelf.js save()

save()保存新模型。

save_city.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. async function save_city() {
  4. try {
  5. let val = await City.forge({ 'name': 'Kyiv', 'population': 2884000}).save();
  6. console.log(val.toJSON());
  7. } catch (e) {
  8. console.log(`Failed to save data: ${e}`);
  9. } finally {
  10. knex.destroy();
  11. }
  12. }
  13. save_city();

该示例将保存一个新城市。

  1. $ node save_city.js
  2. { name: 'Kyiv', population: 2884000, id: 9 }
  3. $ node fetch_all.js
  4. [ { id: 1, name: 'Bratislava', population: 432000 },
  5. { id: 2, name: 'Budapest', population: 1759000 },
  6. { id: 3, name: 'Prague', population: 1280000 },
  7. { id: 4, name: 'Warsaw', population: 1748000 },
  8. { id: 5, name: 'Los Angeles', population: 3971000 },
  9. { id: 6, name: 'New York', population: 8550000 },
  10. { id: 7, name: 'Edinburgh', population: 464000 },
  11. { id: 8, name: 'Berlin', population: 3671000 },
  12. { id: 9, name: 'Kyiv', population: 2884000 } ]

这是输出。

Bookshelf.js orderBy()

orderBy()函数按指定的列名和排序顺序对检索到的数据进行排序。 order参数是可选的,默认为’ASC’。

order_by.js

  1. const knex = require('./config/db').knex;
  2. const City = require('./model/city');
  3. async function fetch_city() {
  4. try {
  5. let vals = await City.forge().orderBy('name', 'DESC').fetchAll({require:true});
  6. console.log(vals.toJSON());
  7. } catch (e) {
  8. console.log(`Failed to fetch data: ${e}`);
  9. } finally {
  10. knex.destroy();
  11. }
  12. }
  13. fetch_city();

在示例中,我们获取所有城市,并按名称降序排列它们。

  1. $ node order_by.js
  2. [ { id: 4, name: 'Warsaw', population: 1748000 },
  3. { id: 3, name: 'Prague', population: 1280000 },
  4. { id: 6, name: 'New York', population: 8550000 },
  5. { id: 5, name: 'Los Angeles', population: 3971000 },
  6. { id: 9, name: 'Kyiv', population: 2884000 },
  7. { id: 7, name: 'Edinburgh', population: 464000 },
  8. { id: 2, name: 'Budapest', population: 1759000 },
  9. { id: 1, name: 'Bratislava', population: 432000 },
  10. { id: 8, name: 'Berlin', population: 3671000 } ]

这是输出。

Bookshelf.js 一对一关系

一对一的关系由hasOne()belongsTo()函数定义。

employees_projects.sql

  1. DROP TABLE IF EXISTS employees;
  2. DROP TABLE IF EXISTS projects;
  3. CREATE TABLE projects(id serial PRIMARY KEY, name VARCHAR(255));
  4. INSERT INTO projects(name) VALUES('Project A');
  5. INSERT INTO projects(name) VALUES('Project B');
  6. INSERT INTO projects(name) VALUES('Project C');
  7. CREATE TABLE employees(id serial PRIMARY KEY, project_id INT REFERENCES projects (id),
  8. name VARCHAR(255));
  9. INSERT INTO employees(project_id, name) VALUES(2, 'John Doe');
  10. INSERT INTO employees(project_id, name) VALUES(1, 'Lucia Smith');

我们有employeesprojects。 员工只能分配到一个项目。

Bookshelf.js hasOne()

hasOne()函数定义模型之间的一对一关系。 hasOne关系指定表恰好具有另一种对象类型,该对象由另一表中的外键指定。

model/project.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const Employee = require('./employee');
  4. const Project = bookshelf.Model.extend({
  5. tableName: 'projects',
  6. employee: function () {
  7. return this.hasOne(Employee);
  8. }
  9. });
  10. module.exports = Project;

Project模型包含hasOne()函数。 通过查询项目,我们可以获取其链接的员工。

model/employee.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const Employee = bookshelf.Model.extend({
  4. tableName: 'employees'
  5. });
  6. module.exports = Employee;

这是Employee模型。

has_one.js

  1. const knex = require('./config/db').knex;
  2. const Project = require('./model/project');
  3. async function doQuery() {
  4. try {
  5. let val = await Project.where({ id: 2 }).fetch({
  6. withRelated: ['employee']
  7. });
  8. console.log(val.toJSON());
  9. } catch (e) {
  10. console.log(`Failed to fetch data: ${e}`);
  11. } finally {
  12. knex.destroy();
  13. }
  14. }
  15. doQuery();

在示例中,我们获取一个项目及其关联的员工。

  1. let val = await Project.where({ id: 3 }).fetch({
  2. withRelated: ['employee']
  3. });

指定withRelated选项以获取集合的模型,并希望加载该模型上命名的任何指定关系。 如果没有此选项,我们将仅获得没有关联员工的项目。

  1. $ node has_one.js
  2. { id: 2,
  3. name: 'Project B',
  4. employee: { id: 1, project_id: 2, name: 'John Doe' } }

这是输出。

Bookshelf.js belongsTo()一对一

当一个模型是另一个目标模型的成员时,将使用belongsTo()函数。 外键在当前(源)模型中定义。 belongsTo()函数用于一对一和one-to-many关系。

model/project.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const Project = bookshelf.Model.extend({
  4. tableName: 'projects'
  5. });
  6. module.exports = Project;

这是Project模型。

model/employee.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const Project = require('./project');
  4. const Employee = bookshelf.Model.extend({
  5. tableName: 'employees',
  6. project: function () {
  7. return this.belongsTo(Project);
  8. }
  9. });
  10. module.exports = Employee;

Employee包含belongsTo()函数。

belongs_to.js

  1. const knex = require('./config/db').knex;
  2. const Employee = require('./model/employee');
  3. async function doQuery() {
  4. try {
  5. let val = await Employee.where({ id: 1 }).fetch({
  6. withRelated: ['project'], require: true
  7. });
  8. console.log(val.toJSON());
  9. } catch (e) {
  10. console.log(`Failed to fetch data: ${e}`);
  11. } finally {
  12. knex.destroy();
  13. }
  14. }
  15. doQuery();

在该示例中,我们通过其链接项目获取一名雇员。

  1. $ node belongs_to.js
  2. { id: 1,
  3. project_id: 2,
  4. name: 'John Doe',
  5. project: { id: 2, name: 'Project B' } }

这是输出。

Bookshelf.js 一对多关系

一对多关系通过hasMany()belongsTo()函数定义。

users_tasks.js

  1. DROP TABLE IF EXISTS tasks;
  2. DROP TABLE IF EXISTS users;
  3. CREATE TABLE users(id serial PRIMARY KEY, name VARCHAR(255));
  4. INSERT INTO users(name) VALUES('John Doe');
  5. INSERT INTO users(name) VALUES('Lucia Smith');
  6. CREATE TABLE tasks(id serial PRIMARY KEY, user_id INT REFERENCES users (id),
  7. name VARCHAR(255));
  8. INSERT INTO tasks(user_id, name) VALUES(1, 'Task A');
  9. INSERT INTO tasks(user_id, name) VALUES(1, 'Task B');
  10. INSERT INTO tasks(user_id, name) VALUES(1, 'Task C');
  11. INSERT INTO tasks(user_id, name) VALUES(2, 'Task D');
  12. INSERT INTO tasks(user_id, name) VALUES(2, 'Task E');

我们有userstasks。 用户可以执行一个或多个任务。 一个任务只能由一个用户拥有。

Bookshelf.js hasMany()

hasMany()定义了模型之间的一对多关系。 该关系指定当前模型在另一张表中具有一个或多个与该模型的主键匹配的行。

model/user.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const Task = require('./task');
  4. const User = bookshelf.Model.extend({
  5. tableName: 'users',
  6. tasks: function() {
  7. return this.hasMany(Task);
  8. }
  9. });
  10. module.exports = User;

User模型包含hasMany()函数。

model/task.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const Task = bookshelf.Model.extend({
  4. tableName: 'tasks',
  5. });
  6. module.exports = Task;

这是Task模型。

has_many.js

  1. const knex = require('./config/db').knex;
  2. const User = require('./model/user');
  3. async function doQuery() {
  4. try {
  5. let val = await User.where({ id: 1 }).fetch({
  6. withRelated: ['tasks'], require: true
  7. });
  8. console.log(val.toJSON());
  9. } catch (e) {
  10. console.log(`Failed to fetch data: ${e}`);
  11. } finally {
  12. knex.destroy();
  13. }
  14. }
  15. doQuery();

在示例中,我们获取用户及其任务。

  1. $ node has_many.js
  2. { id: 1,
  3. name: 'John Doe',
  4. tasks:
  5. [ { id: 1, user_id: 1, name: 'Task A' },
  6. { id: 2, user_id: 1, name: 'Task B' },
  7. { id: 3, user_id: 1, name: 'Task C' } ] }

ID 为 1 的用户具有三个任务。

Bookshelf.js belongsTo()一对多

在一对多的情况下,belongsTo()hasMany()的倒数,并且是关联的一侧。

model/user.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const User = bookshelf.Model.extend({
  4. tableName: 'users',
  5. });
  6. module.exports = User;

这是User模型。

model/task.js

  1. const knex = require('../config/db').knex;
  2. const bookshelf = require('bookshelf')(knex);
  3. const User = require('./user');
  4. const Task = bookshelf.Model.extend({
  5. tableName: 'tasks',
  6. user: function() {
  7. return this.belongsTo(User);
  8. }
  9. });
  10. module.exports = Task;

Task模型包含belongsTo()函数。

belongs_to2.js

  1. const knex = require('./config/db').knex;
  2. const Task = require('./model/task');
  3. async function doQuery() {
  4. try {
  5. let val = await Task.where({ id: 4 }).fetch({
  6. withRelated: ['user'], require: true
  7. });
  8. console.log(val.toJSON());
  9. } catch (e) {
  10. console.log(`Failed to fetch data: ${e}`);
  11. } finally {
  12. knex.destroy();
  13. }
  14. }
  15. doQuery();

在示例中,我们与任务的关联用户一起获取任务。

在本教程中,我们使用书架库。 我们创建了一些与 PostgreSQL 交互的命令行程序。

您可能也对以下相关教程感兴趣: Sequelize 教程Node Postgres 教程JavaScript 教程