Koa使用Sqlite3和Sequelize

栏目: IT技术 · 发布时间: 4年前

内容简介:基于 Sqlite3 的项目,直接是用 Sequelize 进行对象和表的映射。

SQLite 是一种嵌入式数据库,它的数据库就是一个文件。

Sequelize 是一个基于 promise 的 Node.js ORM , 目前支持 Postgres , MySQL , MariaDB , SQLite 以及 Microsoft SQL Server . 它具有强大的事务支持, 关联关系, 预读和延迟加载,读取复制等功能.

基于 Sqlite 3 的项目,直接是用 Sequelize 进行对象和表的映射。

环境配置

// 安装
npm i sequelize sqlite3 -S

项目结构

Koa使用Sqlite3和Sequelize

配置文件夹 config

// config/config.js
const path = require('path')
const sqlite3 = require('sqlite3').verbose()
module.exports = {
  test: {
    storage: path.join(__dirname, '../db/db_test.sqlite'), // 文件路径
    host: 'localhost', // 地址
    dialect: 'sqlite', // 目标数据库种类
    dialectModule: sqlite3
    logging: console.log,
  }
}

注意:sqlite 可以忽略密码。(PS:使用密码可能会无法创建 sqlite 文件)

在 window 中需要手动引入 sqlite3 否则会找不到。

模型文件夹 models

该文件夹中 index.js 入口文件,链接数据库。

// models/index.js
const fs = require('fs')
const path = require('path') 
const basename = path.basename(__filename) // 当前文件名
const Sequelize = require('sequelize')
const config = require('../config/config') // 配置文件
const sequelize = new Sequelize(undefined, undefined, undefined, config.test) // sqlite: 前三个参数传 undefined。

const db = {}
// 引入其他的Model文件。
fs.readdirSync(__dirname)
	// 过滤:不是index.js, 含有.、以 .js 结尾的文件
  .filter(file => (file.indexOf('.' !== 0) && (file !== basename) && (file.slice(-3) === '.js')))
  .forEach(file => {
  	// 引入所有的Model文件
  	// 采用 import 引入
    const model = sequelize.import(path.join(__dirname, file))
    db[model.name] = model
  })
// 关联配置
Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db)
  }
})

db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db

模型文件 User

// models/user.js
module.exports = (sequelize, DataTypes) => {
  // 参数一:modelName: user
  // 参数二:定义属性
  // 参数三:其他配置 https://sequelize.org/master/class/lib/model.js~Model.html#static-method-init
  const User = sequelize.define('user', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    age: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    gender: {
      type: DataTypes.ENUM,
      values: ['男', '女', '未知'],
      defaultValue: '未知'
    }
  }, {
    tableName: 'user'
  })
  User.associate = models => {
    // 定义关联
  }
  return User
}

参考: 其他配置:options

知识点

分页

// controller/UserCtl.js
class UserCtl {
  /**
   * 获取所有用户
   * @param {Object} ctx 
   */
  async index(ctx) {
    // 分页 page, pageSize
    // 使用 findAndCountAll 代替 findAll
    const { page = 1, pageSize = 10 } = ctx.request.body
    // 偏移量
    let offset = (page - 1) * pageSize
    try {    
      const {count, rows} = await db.user.findAndCountAll({
        limit: parseInt(pageSize), // 页容量
        offset: offset, // 偏移量
      })
      const data = {
        code: 0,
        msg: 'success',
        totalCount: count,
        data: rows,
      }
      ctx.ok(data)
    } catch (error) {
      const data = {
        code: 1,
        msg: '服务错误~',
        err: error.name
      }
      ctx.badRequest(data)
    }
  }
}

API文档:findAndCountAll

findAndCountAllfindAll 区别。返回结果不同:前者返回 Promise <{count: number , rows: Model []}>,count 对象总数,rows本次查到的对象数组。后者直接返回对象数组。

有人遇到个错误:分页总数计算错误问题) (PS:暂时还没有遇到)


以上所述就是小编给大家介绍的《Koa使用Sqlite3和Sequelize》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

HTML5

HTML5

Matthew David / Focal Press / 2010-07-29 / USD 39.95

Implement the powerful new multimedia and interactive capabilities offered by HTML5, including style control tools, illustration tools, video, audio, and rich media solutions. Understand how HTML5 is ......一起来看看 《HTML5》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具