Skip to content

Commit e7ab38b

Browse files
committed
Define Resource model and route
1 parent 408a40a commit e7ab38b

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

routes/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "dotenv/config";
22
import cors from "cors";
33
import express from "express";
44
import routes from "./routes";
5+
import models, { sequelize } from "./models";
56

67
const app = express();
78

@@ -17,10 +18,13 @@ app.use(express.urlencoded({ extended: true }));
1718
// App level function used to intercept any requests and do something before
1819
// sending a response back (i.e., defining context or authentication).
1920
app.use((req, res, next) => {
21+
req.context = {
22+
models
23+
// TODO: Add current_user authentication context
24+
};
25+
2026
// The next function is called to signalize that the middleware has finished
2127
// its job. Important for when middleware uses asynchronous functions.
22-
23-
// TODO: Define context.models.resources
2428
next();
2529
});
2630

@@ -30,6 +34,10 @@ app.get("/", (req, res) => {
3034
return res.send("Recieved a GET HTTP method");
3135
});
3236

33-
app.listen(process.env.PORT, () => {
34-
console.log(`Listening on port ${process.env.PORT}`);
37+
// Re-initialize your database on every Express server start
38+
const eraseDatabaseOnSync = true;
39+
sequelize.sync({ force: eraseDatabaseOnSync }).then(async () => {
40+
app.listen(process.env.PORT, () => {
41+
console.log(`Listening on port ${process.env.PORT}`);
42+
});
3543
});

src/models/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Sequelize from "sequelize";
2+
3+
const sequelize = new Sequelize(
4+
process.env.DATABASE,
5+
process.env.DATABASE_USER,
6+
process.env.DATABASE_PASSWORD,
7+
{
8+
dialect: "postgres"
9+
}
10+
);
11+
12+
const models = {
13+
Resource: sequelize.import("./resource")
14+
};
15+
16+
Object.keys(models).forEach(key => {
17+
if ("associate" in models[key]) {
18+
models[key].associate(models);
19+
}
20+
});
21+
22+
export { sequelize };
23+
export default models;

models/resource.js renamed to src/models/resource.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const resource = (sequelize, DataTypes) => {
2525
type: DataTypes.STRING
2626
});
2727

28+
// TODO: define resource associate to belong to user
29+
// Resource.associate = models => {
30+
// Resource.belongsTo(models.User)
31+
// }
2832
return Resource;
2933
};
3034

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import resource from "./resource";
22

3-
export { resource };
3+
export default { resource };
File renamed without changes.

0 commit comments

Comments
 (0)