简体   繁体   中英

How can I use MySQL and MongoDB in the same REST API?

I need to create a REST API that uses a MySQL database and a MongoDB database at the same time. I have been looking for a while but I can't find a guide about how to do it. I need to have the REST API ready this Monday. If someone has a link to a guide that could help me a lot.

I am using only mysql with Sequelize, my files are like this.

├───bin
├───config
│    └───config.json
├───models
├───node_modules
├───public
├───routes
├───app.js
├───package-lock.json
└───package.json

config.json:

{
  "development": {
    "username": "root",
    "password": "",
    "database": "name-of-database",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

app.js:

var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var cors = require("cors");

var indexRouter = require("./routes/index");
var productsRouter = require("./routes/product");
var productCategoriesRouter = require("./routes/product_category");
var usersRouter = require("./routes/user");
var authRouter = require("./routes/auth");

var app = express();

app.use(cors());

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/api", indexRouter);
app.use("/api/product", productsRouter);
app.use("/api/product_category", productCategoriesRouter);
app.use("/api/user", usersRouter);
app.use("/api/auth", authRouter);

module.exports = app;

I don't understand very well your question, but you can have as many datasources as yo want in your project, you have to understand a datasource as a place when you can store and retrieve information, it can be a file, memory, mySQL or mongoDB. In terms of architecture and structure you can have a folder db when you can save your any datasources something like that:

├───bin
├───config
│    └───config.json
├───db
│    │───datasource1
│    │     └───models
│    │     └───db1.js
│    └───datasource2
│          └───models
│          └───db2.js
│
│
├───node_modules
├───public
├───routes
├───app.js
├───package-lock.json
└───package.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM