forked from gordonball/pixly-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
46 lines (40 loc) · 1.23 KB
/
db.js
File metadata and controls
46 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
require("dotenv").config();
console.log("env!!!", process.env.DATABASE);
/** Database setup for pixly */
const { Sequelize, DataTypes } = require("sequelize");
const sequelize = new Sequelize({
database: process.env.DATABASE,
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
dialect: "postgres",
});
/**
* Example function from sequelize to test DB connection is established
*/
async function testConnection() {
try {
await sequelize.authenticate();
console.log("Connection has been established successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
}
}
testConnection();
// Definition of the images table and the data that will be passed in
const ImageModel = sequelize.define(
"images",
{
image_url: {
type: DataTypes.TEXT,
allowNull: false,
primaryKey: true,
},
title: { type: DataTypes.TEXT, allowNull: false },
description: { type: DataTypes.TEXT, allowNull: false },
uploaded_by: { type: DataTypes.TEXT, allowNull: false },
metadata: { type: DataTypes.JSON, allowNull: true },
},
{ timestamps: false }
);
module.exports = { sequelize, ImageModel };