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');
      }
    };
  • 相关阅读:
    jvm client模式和server模式
    TOMCAT开启APR模式
    Spring MVC 关于controller的字符编码问题
    彻底解决Spring MVC 中文乱码 问题
    js中字符串拼接html
    分布式文件系统之MooseFS----介绍
    CopyFile函數詳解
    Delphi 接口使用中,对象生命周期管理,如何释放需要注意的问题
    年度调查 看看 2016 年 Go 语言调查结果
    Sleep(0)的作用
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14566239.html
Copyright © 2011-2022 走看看