用nodejs做网站 - 5

595 查看

数据库

传统建表就是直接用SQL写出来,在数据库中执行。但我不是很擅长这种事,于是我找了工具帮我处理。

Knex http://knexjs.org/

Knex.js is a "batteries included" SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, and Oracle designed to be flexible, portable, and fun to use.

首先让我们在项目中和全局都安装knex。因为我们用的是mysql,所以mysql的模块也要安装。

➜ npm install knex --save
➜ npm install knex -g
➜ npm install mysql --save

装好之后,别忘了检查一下。

➜ knex --version
Knex CLI version:  0.11.7
Local Knex version:  0.11.7

初次使用的时候,可以用命令行创建一个配置文件。创建在项目的根目录中。

➜ knex init
Created ./knexfile.js

这个是创建knexfile.js里的默认内容。

// Update with your config settings.

module.exports = {

  development: {
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3'
    }
  },

  staging: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user:     'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  },

  production: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user:     'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  }

};

我们之前写了一个config.js,里面就包含了数据库的设置。这个时候就需要我们合并一下了,合并之后就是下面这个样子了。

这当中使用了lodash这个库,这个库就是一个工具库。其中有很多的函数可以直接用,很方便。

https://lodash.com/

A modern JavaScript utility library delivering modularity, performance, & extras.

var config = require('./config');
var _ = require('lodash');

var baseConfig = {
  migrations: {
    directory: './db/migrations'
  }
}

module.exports = {
  development: _.extend(config.development.database, baseConfig),
  staging    : _.extend(config.staging.database, baseConfig),
  production : _.extend(config.production.database, baseConfig)
};

我们第一次做的更新就是创建users表,用于存储用户的信息。我们在knex的配置文件中设定了migrations的目录,所以直接生成在了那个个目录当中。

➜ knex migrate:make add-user
Using environment: development
Created Migration: /Users/limi/website/db/migrations/20160719121550_add-user.js

刚生成完,里面是什么都没有的。生成表的代码我们就写在这个地方,里面定义了更新和回滚的操作,分别放在up和down中。我们顺便在up中给users表插入了一条数据。

这里面用到了Promise,如果你不了解的话可以参考以下。

exports.up = function(knex, Promise) {
  return Promise.all([
    // 创建表
    knex.schema.createTableIfNotExists('users', function (table) {
      table.increments('id').primary().notNullable();
      table.string('name').notNullable();
      table.string('password').notNullable();
      table.string('email').notNullable();
      table.string('uuid').notNullable();
      table.string('status').notNullable().defaultTo('active');
      table.dateTime('created_at').notNullable();
      table.dateTime('updated_at').nullable();
    }),
    // 插入数据
    knex.table('users').insert({
      name: 'limichange',
      email: 'limichange@hotmail.com',
      password: 'asdfasdf',
      uuid: '1',
      created_at: new Date()
    })
  ]);
};
exports.down = function(knex, Promise) {
  return Promise.all([
    // 删除表
    knex.schema.dropTable('users')
  ]);
};

接下来用命令行工具来更新数据库。

➜ knex migrate:latest
Using environment: development
Batch 1 run: 1 migrations
/Users/limi/website/db/migrations/20160719121550_add-user.js

只要执行撤销的操作,就可以让数据库回滚。

➜ knex migrate:rollback
Using environment: development
Batch 1 rolled back: 1 migrations
/Users/limi/website/db/migrations/20160719121550_add-user.js