Skip to content

Commit

Permalink
feat: lecture 4 code
Browse files Browse the repository at this point in the history
  • Loading branch information
choyiny committed Jun 1, 2024
1 parent a79faa8 commit b76d9f0
Show file tree
Hide file tree
Showing 14 changed files with 5,058 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lectures/04-data/chirper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
uploads/
node_modules/
.DS_Store
chirper.sqlite
31 changes: 31 additions & 0 deletions lectures/04-data/chirper/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { sequelize } from "./datasource.js";
import express from "express";
import bodyParser from "body-parser";
import { chirpsRouter } from "./routers/chirps-router.js";

const PORT = 3000;
export const app = express();

app.use(bodyParser.json());
app.use(express.static("static"));

// this is a middleware function that logs the incoming request
app.use(function (req, res, next) {
console.log("HTTP request", req.method, req.url, req.body);
next();
});

try {
await sequelize.authenticate();
await sequelize.sync({ alter: { drop: false } });
console.log("Connection has been established successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
}

app.use("/chirps", chirpsRouter);

app.listen(PORT, (err) => {
if (err) console.log(err);
else console.log("HTTP server on http://localhost:%s", PORT);
});
6 changes: 6 additions & 0 deletions lectures/04-data/chirper/datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Sequelize } from "sequelize";

export const sequelize = new Sequelize({
dialect: "sqlite",
storage: "chirper.sqlite",
});
17 changes: 17 additions & 0 deletions lectures/04-data/chirper/models/chirp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { sequelize } from "../datasource.js";
import { DataTypes } from "sequelize";

export const Chirp = sequelize.define("Chirp", {
content: {
type: DataTypes.STRING,
allowNull: false,
},
imageMetadata: {
type: DataTypes.JSON,
allowNull: true,
},
});

// replies to chirps are also chirps
Chirp.hasMany(Chirp);
Chirp.belongsTo(Chirp);
Loading

0 comments on commit b76d9f0

Please sign in to comment.