zoukankan      html  css  js  c++  java
  • [Node.js] Sequelize Intro

    ORMS allow us to easily switch to a different dialect of SQL (e.g. PostgreSQL, MySQL), without having to modify the code that interacts with the database. If we were to write SQL queries directly, instead of using an ORM, we would have to modify our SQL statements to be compatible with the dialect of the database that we are using.

    import {Sequelize} from 'sequelize-typescript';
    import { config } from './config/config';
    
    
    const c = config.dev;
    
    // Instantiate new Sequelize instance!
    export const sequelize = new Sequelize({
      "username": c.username,
      "password": c.password,
      "database": c.database,
      "host":     c.host,
    
      dialect: 'postgres',
      storage: ':memory:',
    });
    Migrations
    • Migration refers to modifying the database (by adding or removing tables or columns, for instance, or switching to a different dialect of SQL) to a newer version (usually based on new business requirements).
    • Up migration is the process of modifying the database to a newer state.
    • Down migration is the process of reversing an up migration, to a prior state.

    Read more at the Sequelize docs on migrations

    Note Migrations is a loaded term. We most commonly refer to migrations when changing database table states (new columns, adding tables, etc). However, it can also refer to migrating infrastructure - for examples Postgres to MySQL.

    'use strict';
    module.exports = {
      up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('User', {
          id: {
            allowNull: false,
            autoIncrement: true,
            type: Sequelize.INTEGER
          },
          email: {
            type: Sequelize.STRING,
            primaryKey: true
          },
          password_hash: {
            type: Sequelize.STRING
          },
          createdAt: {
            allowNull: false,
            type: Sequelize.DATE
          },
          updatedAt: {
            allowNull: false,
            type: Sequelize.DATE
          }
        });
      },
      down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('User');
      }
    };
  • 相关阅读:
    eclipse新建工作空间后的常用设置
    Maven将代码及依赖打成一个Jar包的方式
    MemCache详细解读(转)
    memcached单机或热备的安装部署
    memcache的基本操作
    Java中int和String类型之间转换
    Linux中普通用户配置sudo权限(带密或免密)
    Java字符串中常用字符占用字节数
    java各种数据类型的数组元素的默认值
    validator
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14566239.html
Copyright © 2011-2022 走看看