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');
      }
    };
  • 相关阅读:
    http 状态码及含义
    PHP CURL 调用API
    Bootstrap
    JavaScript和快速响应的用户界面
    GitHub配置步骤和简单的git关联
    Git的导入
    java 对象 类 知识点 概览
    java程序执行时,JVM内存
    java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致.
    第六章 进程总结
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14566239.html
Copyright © 2011-2022 走看看