xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js ORM Sequelize All In One

Node.js ORM Sequelize All In One

Sequelize is an easy-to-use and promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite, DB2, Microsoft SQL Server, and Snowflake.
It features solid transaction support, relations, eager and lazy loading, read replication and more.

https://sequelize.org/

https://www.npmjs.com/package/sequelize

image

sqlite3

$ npm i sequelize sqlite3
# or
$ yarn add sequelize sqlite3
import { Sequelize, DataTypes } from 'sequelize';

const sequelize = new Sequelize('sqlite::memory:');
// DEFINE MODELS
const User = sequelize.define('User', {
  username: DataTypes.STRING,
  birthday: DataTypes.DATE,
});
// PERSIST
const jane = await User.create({
  username: 'janedoe',
  birthday: new Date(1980, 6, 20),
});
// QUERY
const users = await User.findAll();

https://sequelize.org/docs/v6/getting-started/

public constructor(database: string, username: string, password: string, options: object)

const { Sequelize } = require('sequelize');

// Option 1: Passing a connection URI
const sequelize = new Sequelize('sqlite::memory:') // Example for sqlite
const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname') // Example for postgres

// Option 2: Passing parameters separately (sqlite)
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: 'path/to/database.sqlite'
});

// Option 3: Passing parameters separately (other dialects)
const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: /* one of 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql' | 'db2' | 'snowflake' | 'oracle' */
});

https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-constructor-constructor

sequelize-cli

$ npm i -D sequelize-cli
$ npx sequelize --help
$ sequelize --version

$ sequelize db:migrate
$ sequelize db:seed
$ sequelize init

https://www.npmjs.com/package/sequelize-cli

demos

macOS

$ brew list | grep mysql

$ brew services list | grep mysql
Name                  Status     User       File
httpd                 error  256 xgqfrms-mm ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist
mongodb-community@4.2 none       xgqfrms-mm
php                   none
unbound               none

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://github.com/sequelize/sequelize
https://github.com/prisma/prisma
https://github.com/typeorm/typeorm

https://www.imooc.com/u/index/notebook



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-09-19 23:06  xgqfrms  阅读(6)  评论(2编辑  收藏  举报