In the Express-demo project, there are several options for connecting to a database. Here are some possibilities and their corresponding examples using the provided documentation and code snippets.
- Using SQLite3 with Express-GraphQL:
First, install the required dependencies:
npm install graphql express express-graphql sqlite3 --save
Next, create an instance of the Express app and the SQLite3 database within the current project folder:
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const ExpressGraphQL = require("express-graphql");
const graphql = require("graphql");
const app = express();
const database = new sqlite3.Database("./my.db");
Create an SQL table within the database:
database.serialize(function() {
database.run(`CREATE TABLE contacts (
id INTEGER PRIMARY KEY,
firstName TEXT,
lastName TEXT,
email TEXT
)`);
});
Define GraphQL types for each contact attribute:
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID } = graphql;
const ContactType = new GraphQLObjectType({
name: "Contact",
fields: () => ({
id: { type: GraphQLID },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
email: { type: GraphQLString },
}),
});
Create a GraphQL schema:
const schema = new graphql.GraphQLSchema({
types: () => [ContactType],
queries: () => ({
contacts: {
type: new GraphQLList(ContactType),
resolve: () => database.all("SELECT * FROM contacts"),
},
}),
});
Set up the GraphQL API server:
const root = { contacts: () => database.all("SELECT * FROM contacts") };
const graphqlHTTP = require("express-graphql");
app.use("/graphql", graphqlHTTP({ schema, rootValue: root }));
app.listen(3333, () => console.log("Server is connected on port 3333"));
- Using PostgreSQL with Mongoose:
First, install the required dependencies:
npm install express mongoose --save
Create a database/mongoose.js
file for connecting to the PostgreSQL database:
const mongoose = require("mongoose");
mongoose.connect("postgres://username:password@localhost:5432/database_name", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection.on("error", console.error.bind(console, "MongoDB connection error:"));
mongoose.connection.once("open", () => console.log("MongoDB database connection opened"));
Create a database/models/Patient.js
file for defining the Patient schema:
const mongoose = require("mongoose");
const PatientSchema = new mongoose.Schema({
name: String,
age: Number,
});
module.exports = mongoose.model("Patient", PatientSchema);
Use the Mongoose connection and models in the Express app:
const express = require("express");
const mongoose = require("mongoose");
const Patient = require("./database/models/Patient");
const app = express();
mongoose.connect("postgres://username:password@localhost:5433/database_name", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.listen(3000, () => console.log("Server is connected on port 3000"));
- Using MySQL with Express:
First, install the required dependencies:
npm install express mysql --save
Create a database.js
file for connecting to the MySQL database:
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
database: "database_name",
});
connection.connect((err) => {
if (err) throw err;
console.log("Connected to MySQL database!");
});
module.exports = connection;
Use the MySQL connection in the Express app:
const express = require("express");
const connection = require("./database");
const app = express();
app.get("/", (req, res) => {
connection.query("SELECT * FROM table_name", (err, results) => {
if (err) throw err;
res.send(results);
});
});
app.listen(3001, () => console.log("Server is connected on port 3001"));