Ví dụ cấu hình Bộ định tuyến MySQL

Now we start our Second Example with the /share/doc/mysqlrouter/sample_mysqlrouter.ini file which is part of the downloaded package :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

[MẶC ĐỊNH]

logging_folder =

plugin_folder = / usr / local/lib/mysqlrouter

config_folder = / etc / mysql

runtime_folder = / var / run

 

[máy ghi]

cấp độ = THÔNG TIN

 

 

[fabric_cache]

địa chỉ = your_fabric_node. ví dụ. com. 32275

người dùng =

 

[định tuyến. basic_failover]

bind_port = 7001

chế độ = đọc - ghi

đích = mysql - server1:3306,mysql - server2

 

[định tuyến. homepage_reads_fabric]

bind_port = 7002

điểm đến = kết cấu + bộ đệm:///group/homepage_group?allow_primary_reads=yes

chế độ = đọc - chỉ

Các phần mặc định và logger là tùy chọn liên quan đến đường dẫn tệp nhật ký bộ định tuyến, plugin (. so) đường dẫn và cấp nhật ký

Phần bộ đệm vải. Nó nhận địa chỉ nơi lưu trữ trạng thái đang chạy và người dùng để kết nối với thiết lập vải. Khi bộ định tuyến được khởi động, nó sẽ nhắc nhập mật khẩu tương ứng

Ví dụ cấu hình Bộ định tuyến MySQL

Phần định tuyến vải. Trong phần này, một nhóm vải được định cấu hình là đích. Vì vậy, dựa trên một trong 2 chế độ có thể là “chỉ đọc” hoặc “chỉ đọc”, nó sẽ chuyển hướng các kết nối đến kết nối chính (đọc-ghi) hoặc kết nối phụ (chỉ đọc) tương ứng

Nếu “allow_primary_reads=yes” được thêm vào uri đích với chế độ chỉ đọc, thì chính cũng sẽ được xem xét trong danh sách các máy chủ có sẵn cho bộ định tuyến để xử lý các yêu cầu của máy khách. Tùy chọn “allow_primary_reads” không có ý nghĩa gì nếu chế độ được cấu hình là đọc-ghi

Vì vậy, trong các cấu hình ví dụ trên, việc kết nối máy khách với cổng liên kết 7002 sẽ trả về kết nối đến máy chủ có sẵn trong nhóm có tên “homepage_group”. Trong ví dụ của chúng tôi allow_primary_reads=yes, vì vậy nó có thể trả về bất kỳ máy chủ nào từ máy chủ phụ hoặc chính. Nếu chỉ có một nô lệ trong nhóm và “allow_primary_reads=no”, thì chính máy chủ đó sẽ xử lý các yêu cầu của máy khách. Kết nối máy khách với cổng 7001 sẽ luôn trả về máy chủ đầu tiên có sẵn trong danh sách đích. tôi. e. máy chủ mysql1. 3306. Nếu máy chủ đầu tiên này không khả dụng thì nó sẽ trả về mysql_server2

Trong hướng dẫn này, chúng tôi sẽ giới thiệu cách xây dựng một Nút đơn giản. js API hỗ trợ các hoạt động CRUD và lưu trữ dữ liệu trong cơ sở dữ liệu MySQL. API ví dụ bao gồm các tuyến để truy xuất, cập nhật, tạo và xóa bản ghi trong cơ sở dữ liệu MySQL, bản ghi trong ví dụ là bản ghi người dùng nhưng cùng một mẫu CRUD và cấu trúc mã có thể được áp dụng cho bất kỳ loại dữ liệu nào. g. sản phẩm, dịch vụ, bài viết, vv

Mã MySQL thế hệ DB đầu tiên với Sequelize

nút. js API tự động tạo cơ sở dữ liệu MySQL khi khởi động (nếu được yêu cầu) và đồng bộ hóa các bảng và cột db với các mô hình javascript được xác định bằng thư viện ORM Sequelize. Việc tạo cơ sở dữ liệu và đồng bộ hóa mô hình được thực hiện bởi hàm

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
3 trong trình bao bọc cơ sở dữ liệu MySQL

Mã trên GitHub

Dự án API CRUD có sẵn trên GitHub tại https. //github. com/cornflourblue/node-mysql-crud-api

Nội dung hướng dẫn

Công cụ cần thiết cho hướng dẫn này

Để làm theo các bước trong hướng dẫn này, bạn sẽ cần những thứ sau

  • Nút. js & npm - bao gồm Nút. js, công cụ dòng lệnh và trình quản lý gói, hãy cài đặt nó từ https. //nodejs. tổ chức/
  • MySQL - bạn sẽ cần quyền truy cập vào phiên bản máy chủ MySQL đang chạy để API kết nối, nó có thể ở xa (e. g. Azure, AWS, v.v.) hoặc trên máy cục bộ của bạn. Phiên bản Máy chủ cộng đồng có sẵn miễn phí từ https. // nhà phát triển. mysql. com/downloads/mysql/, đảm bảo nó đã được khởi động để API có thể kết nối với nó. Hướng dẫn cài đặt có sẵn tại https. // nhà phát triển. mysql. com/doc/refman/8. 0/vi/đang cài đặt. html
  • Trình chỉnh sửa mã để xem và chỉnh sửa mã API, không quan trọng là mã nào, cá nhân tôi sử dụng Visual Studio Code, đây là trình chỉnh sửa miễn phí chạy trên Windows, Mac và Linux, bạn có thể tải xuống tại https. //mã số. Visual Studio. com/

Chạy API CRUD Node + MySQL cục bộ

  1. Tải xuống hoặc sao chép mã nguồn dự án từ https. //github. com/cornflourblue/node-mysql-crud-api
  2. Cài đặt tất cả các gói npm cần thiết bằng cách chạy
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    0 hoặc
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    1 từ dòng lệnh trong thư mục gốc của dự án (nơi chứa gói. json được định vị)
  3. Cập nhật thông tin đăng nhập cơ sở dữ liệu trong
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    2 để kết nối với phiên bản máy chủ MySQL của bạn và đảm bảo máy chủ MySQL đang chạy
  4. Khởi động API bằng cách chạy
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    3 (hoặc
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    4 để bắt đầu với gật đầu) từ dòng lệnh trong thư mục gốc của dự án, bạn sẽ thấy thông báo
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    5
  5. Bạn có thể kiểm tra API trực tiếp bằng một công cụ như Postman hoặc kết nối nó với ví dụ về các ứng dụng React hoặc Angular bên dưới

Tiếp tục đồng bộ hóa mô hình

Khi khởi động, bạn cũng sẽ thấy đầu ra như bên dưới từ quá trình đồng bộ hóa mô hình Sequelize được thực thi trong trình bao bọc cơ sở dữ liệu MySQL, nó cho thấy việc tạo và đồng bộ hóa bảng

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
6 dựa trên mô hình người dùng Sequelize

Executing (default): CREATE TABLE IF NOT EXISTS `Users` (`id` INTEGER NOT NULL auto_increment , `email` VARCHAR(255) NOT NULL, `passwordHash` VARCHAR(255) NOT NULL, `title` VARCHAR(255) NOT NULL, `firstName` VARCHAR(255) NOT NULL, `lastName` VARCHAR(255) NOT NULL, `role` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW FULL COLUMNS FROM `Users`;
Executing (default): SELECT CONSTRAINT_NAME as constraint_name,CONSTRAINT_NAME as constraintName,CONSTRAINT_SCHEMA as constraintSchema,CONSTRAINT_SCHEMA as constraintCatalog,TABLE_NAME as tableName,TABLE_SCHEMA as tableSchema,TABLE_SCHEMA as tableCatalog,COLUMN_NAME as columnName,REFERENCED_TABLE_SCHEMA as referencedTableSchema,REFERENCED_TABLE_SCHEMA as referencedTableCatalog,REFERENCED_TABLE_NAME as referencedTableName,REFERENCED_COLUMN_NAME as referencedColumnName FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME = 'Users' AND CONSTRAINT_NAME!='PRIMARY' AND CONSTRAINT_SCHEMA='node-mysql-crud-api' AND REFERENCED_TABLE_NAME IS NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `email` `email` VARCHAR(255) NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `passwordHash` `passwordHash` VARCHAR(255) NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `title` `title` VARCHAR(255) NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `firstName` `firstName` VARCHAR(255) NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `lastName` `lastName` VARCHAR(255) NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `role` `role` VARCHAR(255) NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `createdAt` `createdAt` DATETIME NOT NULL;
Executing (default): ALTER TABLE `Users` CHANGE `updatedAt` `updatedAt` DATETIME NOT NULL;
Executing (default): SHOW INDEX FROM `Users`

Chạy ứng dụng React với Node + MySQL CRUD API

Để biết chi tiết đầy đủ về ứng dụng React CRUD, hãy xem bài đăng React - Ví dụ CRUD với React Hook Form. Nhưng để đứng dậy và chạy nhanh, chỉ cần làm theo các bước dưới đây

  1. Tải xuống hoặc sao chép mã hướng dẫn React từ https. //github. com/cornflourblue/react-hook-form-crud-example
  2. Cài đặt tất cả các gói npm cần thiết bằng cách chạy
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    0 hoặc
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    1 từ dòng lệnh trong thư mục gốc của dự án (nơi chứa gói. json được định vị)
  3. Xóa hoặc comment 2 dòng bên dưới comment
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    9 nằm trong file
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    10
  4. Khởi động ứng dụng bằng cách chạy
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    3 từ dòng lệnh trong thư mục gốc của dự án, thao tác này sẽ khởi chạy trình duyệt hiển thị ứng dụng và nó sẽ được kết nối với API Node + MySQL CRUD mà bạn đã chạy

Chạy ứng dụng góc với API CRUD Node + MySQL

Để biết chi tiết đầy đủ về ứng dụng Angular CRUD, hãy xem bài viết Angular 11 - Ví dụ CRUD với Reactive Forms. Nhưng để đứng dậy và chạy nhanh, chỉ cần làm theo các bước dưới đây

  1. Tải xuống hoặc sao chép mã hướng dẫn Angular từ https. //github. com/cornflourblue/angular-11-crud-example
  2. Cài đặt tất cả các gói npm cần thiết bằng cách chạy
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    0 từ dòng lệnh trong thư mục gốc của dự án (nơi chứa gói. json được định vị)
  3. Xóa hoặc nhận xét dòng bên dưới nhận xét
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    13 nằm trong tệp
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    14
  4. Khởi động ứng dụng bằng cách chạy
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    3 từ dòng lệnh trong thư mục gốc của dự án, thao tác này sẽ khởi chạy trình duyệt hiển thị ứng dụng và nó sẽ được kết nối với Nút. js + MySQL CRUD API mà bạn đã chạy

Nút. js + MySQL Cấu trúc dự án CRUD API

Dự án hướng dẫn được cấu trúc thành các thư mục tính năng (người dùng) và các thư mục thành phần không phải tính năng/dùng chung (_helpers, _middleware). Các thư mục thành phần được chia sẻ chứa mã có thể được sử dụng trên nhiều tính năng hoặc các phần khác của ứng dụng và được bắt đầu bằng dấu gạch dưới

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
16 để nhóm chúng lại với nhau và giúp dễ dàng phân biệt giữa mã cụ thể của tính năng và mã được chia sẻ

Ví dụ CRUD hiện chỉ chứa một tính năng (người dùng), nhưng có thể dễ dàng mở rộng với các tính năng khác bằng cách sao chép thư mục người dùng và theo cùng một mẫu

Nhấp vào bất kỳ liên kết nào bên dưới để chuyển xuống phần mô tả của từng tệp cùng với mã của nó

thư mục người trợ giúp

Thư mục người trợ giúp chứa tất cả các bit và phần không vừa với các thư mục khác nhưng không biện minh cho việc có một thư mục của riêng chúng

Trình bao bọc cơ sở dữ liệu MySQL

Trình bao bọc cơ sở dữ liệu MySQL kết nối với MySQL bằng Sequelize & ứng dụng khách MySQL2 và xuất một đối tượng hiển thị tất cả các mô hình cơ sở dữ liệu cho ứng dụng (hiện chỉ có

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
17). Nó cung cấp một cách dễ dàng để truy cập bất kỳ phần nào của cơ sở dữ liệu từ một điểm duy nhất

Hàm

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
3 được thực thi một lần khi khởi động API và thực hiện các tác vụ sau

  • Kết nối với máy chủ MySQL bằng ứng dụng khách db
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    19 và thực hiện truy vấn để tạo cơ sở dữ liệu API nếu nó chưa tồn tại
  • Kết nối với cơ sở dữ liệu API với ORM Sequelize
  • Khởi tạo mô hình
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    17 và gắn nó vào đối tượng
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    11 đã xuất
  • Tự động tạo/cập nhật các bảng trong cơ sở dữ liệu MySQL để khớp với mô hình Sequelize (nếu cần) bằng cách gọi
    const config = require('config.json');
    const mysql = require('mysql2/promise');
    const { Sequelize } = require('sequelize');
    
    module.exports = db = {};
    
    initialize();
    
    async function initialize() {
        // create db if it doesn't already exist
        const { host, port, user, password, database } = config.database;
        const connection = await mysql.createConnection({ host, port, user, password });
        await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
    
        // connect to db
        const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });
    
        // init models and add them to the exported db object
        db.User = require('../users/user.model')(sequelize);
    
        // sync all models with database
        await sequelize.sync({ alter: true });
    }
    12. Để biết thêm thông tin về các tùy chọn đồng bộ hóa mô hình Sequelize, hãy xem https. // sắp xếp lại. org/master/manual/model-basics. html#model-đồng bộ hóa
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}

Đối tượng vai trò / Enum

Đối tượng vai trò xác định tất cả các vai trò trong ứng dụng ví dụ, tôi đã tạo nó để sử dụng như một enum để tránh chuyển vai trò xung quanh dưới dạng chuỗi, vì vậy thay vì

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
13 và
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
14, chúng ta có thể sử dụng
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
15 và
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
16

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
1

Thể hiện. thư mục phần mềm trung gian js

Thư mục phần mềm trung gian chứa Express. js có thể được sử dụng bởi các tuyến / tính năng trong Nút. API CRUD js

Phần mềm trung gian xử lý lỗi toàn cầu

Đường dẫn. /_middleware/trình xử lý lỗi. js

Trình xử lý lỗi chung được sử dụng để bắt tất cả các lỗi và loại bỏ nhu cầu mã xử lý lỗi trùng lặp trong ứng dụng CRUD. Nó được cấu hình như phần mềm trung gian trong máy chủ chính. tập tin js

Theo quy ước, các lỗi loại

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
17 được coi là lỗi tùy chỉnh (dành riêng cho ứng dụng), điều này giúp đơn giản hóa mã để đưa ra các lỗi tùy chỉnh vì chỉ cần đưa ra một chuỗi (e. g.
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
18). Hơn nữa, nếu một lỗi tùy chỉnh kết thúc bằng các từ
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
19, mã phản hồi 404 được trả về, nếu không, phản hồi 400 tiêu chuẩn sẽ được trả về. Xem dịch vụ người dùng để biết một số ví dụ về lỗi tùy chỉnh do API đưa ra, các lỗi được lưu trong bộ điều khiển người dùng cho từng tuyến và được chuyển đến hàm
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
20 để chuyển chúng tới trình xử lý lỗi chung này

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
1

Xác thực phần mềm trung gian yêu cầu

Đường dẫn. /_middleware/xác thực-yêu cầu. js

Hàm phần mềm trung gian yêu cầu xác thực xác thực phần thân của yêu cầu đối với đối tượng giản đồ Joi

Nó được sử dụng bởi các chức năng phần mềm trung gian lược đồ trong bộ điều khiển để xác thực các yêu cầu bằng lược đồ cho một tuyến đường cụ thể (e. g.

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
21 để tạo tuyến người dùng trong bộ điều khiển người dùng). Để biết thêm thông tin về xác thực lược đồ Joi, hãy xem https. //www. npmjs. com/gói/joi

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
2

Thư mục tính năng người dùng

Thư mục người dùng chứa tất cả mã dành riêng cho tính năng người dùng của Nút. js + API CRUD của MySQL

Sắp xếp lại mô hình người dùng

Đường dẫn. /người dùng/người dùng. người mẫu. js

Mô hình người dùng sử dụng Sequelize để xác định lược đồ cho bảng

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
22 trong cơ sở dữ liệu MySQL. Đối tượng mô hình Sequelize đã xuất cung cấp toàn quyền truy cập để thực hiện các thao tác CRUD (tạo, đọc, cập nhật, xóa) trên người dùng trong MySQL, hãy xem dịch vụ người dùng bên dưới để biết ví dụ về dịch vụ đang được sử dụng (thông qua trình trợ giúp
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
11)

Theo mặc định,

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
24 định cấu hình mô hình để loại trừ hàm băm mật khẩu khỏi kết quả truy vấn. Phạm vi
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
25 có thể được sử dụng để truy vấn người dùng và bao gồm hàm băm mật khẩu trong kết quả. Để biết thêm thông tin về Sequelize scopes, xem https. // sắp xếp lại. org/master/manual/phạm vi. html

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
0

Dịch vụ người dùng

Đường dẫn. /người dùng/người dùng. Dịch vụ. js

Dịch vụ người dùng chịu trách nhiệm cho tất cả các tương tác cơ sở dữ liệu và logic nghiệp vụ cốt lõi liên quan đến các hoạt động CRUD của người dùng, nó gói gọn tất cả các tương tác với mô hình người dùng Sequelize và hiển thị một tập hợp các phương thức đơn giản được sử dụng bởi bộ điều khiển người dùng

Phần trên cùng của tệp chứa đối tượng dịch vụ đã xuất chỉ có tên phương thức để dễ dàng xem nhanh tất cả các phương thức, phần còn lại của tệp chứa các hàm triển khai cho từng phương thức dịch vụ, tiếp theo là các hàm trợ giúp cục bộ

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
1

Thể hiện. Bộ điều khiển người dùng js

Đường dẫn. /người dùng/người dùng. bộ điều khiển. js

Bộ điều khiển người dùng xác định tất cả các tuyến đường

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
26 cho Nút. js + MySQL CRUD API, các định nghĩa tuyến đường được nhóm lại với nhau ở đầu tệp và các chức năng triển khai ở bên dưới, tiếp theo là các chức năng xác thực lược đồ tuyến đường. Bộ điều khiển được liên kết với đường dẫn
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
26 trong máy chủ chính. tập tin js

Các tuyến yêu cầu xác thực lược đồ bao gồm chức năng phần mềm trung gian với quy ước đặt tên

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
28 (e. g.
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
21). Mỗi chức năng xác thực lược đồ xác định một lược đồ cho nội dung yêu cầu bằng cách sử dụng thư viện Joi và gọi
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
00 để đảm bảo nội dung yêu cầu hợp lệ. Nếu xác thực thành công, yêu cầu sẽ tiếp tục đến chức năng phần mềm trung gian tiếp theo (chức năng định tuyến), nếu không, lỗi sẽ được trả về với chi tiết về lý do tại sao xác thực không thành công. Để biết thêm thông tin về xác thực lược đồ Joi, hãy xem https. //www. npmjs. com/gói/joi

Express là máy chủ web được sử dụng bởi API CRUD, đây là một trong những khung ứng dụng web phổ biến nhất cho Node. js. Để biết thêm thông tin xem https. // expressjs. com/

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
7

Cấu hình Api

Tệp cấu hình API chứa dữ liệu cấu hình cho API CRUD, nó bao gồm các tùy chọn kết nối

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
01 cho cơ sở dữ liệu MySQL, thuộc tính
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
02 là tên của cơ sở dữ liệu được API tự động tạo khi khởi động

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
0

Bưu kiện. json

Gói. tệp json chứa thông tin cấu hình dự án bao gồm Node. js gói

const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
03 được cài đặt khi bạn chạy
const config = require('config.json');
const mysql = require('mysql2/promise');
const { Sequelize } = require('sequelize');

module.exports = db = {};

initialize();

async function initialize() {
    // create db if it doesn't already exist
    const { host, port, user, password, database } = config.database;
    const connection = await mysql.createConnection({ host, port, user, password });
    await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);

    // connect to db
    const sequelize = new Sequelize(database, user, password, { dialect: 'mysql' });

    // init models and add them to the exported db object
    db.User = require('../users/user.model')(sequelize);

    // sync all models with database
    await sequelize.sync({ alter: true });
}
0

Cấu hình bộ định tuyến MySQL là gì?

Bộ định tuyến MySQL là một phần của Cụm InnoDB và là phần mềm trung gian nhẹ cung cấp định tuyến minh bạch giữa ứng dụng của bạn và Máy chủ MySQL phía sau . Nó được sử dụng cho nhiều trường hợp sử dụng khác nhau, chẳng hạn như cung cấp tính sẵn sàng cao và khả năng mở rộng bằng cách định tuyến lưu lượng cơ sở dữ liệu đến các máy chủ MySQL back-end phù hợp.

Mật khẩu cho cấu hình bộ định tuyến MySQL là gì?

Tên người dùng mặc định cho bộ định tuyến MySQL của bạn là root. Mật khẩu mặc định là (trống) . Nhập tên người dùng và mật khẩu, nhấn "Enter" và bây giờ bạn sẽ thấy bảng điều khiển của bộ định tuyến.

Tôi có nên khởi động bộ định tuyến MySQL không?

Không cố định cấu hình Bộ định tuyến MySQL theo cách thủ công để chuyển hướng đến các phiên bản máy chủ. Luôn sử dụng tùy chọn --bootstrap vì điều này đảm bảo rằng Bộ định tuyến MySQL lấy cấu hình từ siêu dữ liệu .

Tệp cấu hình MySQL ở đâu?

ini C. \Tệp chương trình\MySQL\Máy chủ MySQL 5. 5\của tôi. cnf .