Skip to content

Commit 9f2db71

Browse files
committed
Lecture14
1 parent 3cbd481 commit 9f2db71

File tree

4 files changed

+1366
-0
lines changed

4 files changed

+1366
-0
lines changed

Lecture14/sequelizetest/db.sqlite

12 KB
Binary file not shown.

Lecture14/sequelizetest/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const Sequelize = require('sequelize')
2+
const express = require('express')
3+
const app = express()
4+
5+
const sequelize = new Sequelize({
6+
dialect: 'sqlite',
7+
storage: './db.sqlite'
8+
});
9+
10+
11+
const Bands = sequelize.define('bands', {
12+
id: {
13+
type: Sequelize.INTEGER,
14+
primaryKey: true,
15+
autoIncrement: true
16+
},
17+
title: {
18+
type: Sequelize.STRING,
19+
allowNull: false
20+
},
21+
striked: {
22+
type: Sequelize.BOOLEAN,
23+
defaultValue: false,
24+
allowNull: false
25+
}
26+
})
27+
28+
app.use(express.urlencoded({extended: true}))
29+
30+
const stringToBool = val => val == "true"
31+
32+
app.get('/', (req, res) => {
33+
const filter = req.query || {}
34+
if (typeof filter.striked != 'undefined') {
35+
filter.striked = stringToBool(filter.striked)
36+
}
37+
38+
Bands.findAll({
39+
where: filter
40+
}).then(bands => {
41+
res.json(bands)
42+
})
43+
})
44+
45+
app.post('/', (req, res) => {
46+
Bands.create({
47+
title: req.body.title
48+
}).then(band => {
49+
res.json(band)
50+
})
51+
})
52+
53+
54+
55+
Bands.sync().then(() => {
56+
app.listen(3000)
57+
})
58+
59+
60+
// sequelize.authenticate().then(() => {
61+
// console.log("Connected!")
62+
// }).catch(err => {
63+
// console.log("Not Connected", err)
64+
// })

0 commit comments

Comments
 (0)