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');
      }
    };
  • 相关阅读:
    指针与强制类型转换
    String 转Clob
    Oracle数据类型对应Java类型
    Oracle常见的问题
    面对批量更新之字符串的拼接
    java使用sigar 遇到问题的解决方案
    关于IE8以上 不引人css 症状
    java Springmvc ajax上传
    再也不要看到Eclipse万恶的arg0,arg1提示
    TextView实现跑马灯效果
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14566239.html
Copyright © 2011-2022 走看看