Created
August 17, 2022 11:17
-
-
Save ericpkatz/f94c93f1715f6ac7eba82f1f8f6adbaf to your computer and use it in GitHub Desktop.
Backend For Facet Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Sequelize = require('sequelize'); | |
const conn = new Sequelize(process.env.DATABASE_URL || 'postgres://localhost/acme_db'); | |
const express = require('express'); | |
const app = express(); | |
const path = require('path'); | |
app.use('/dist', express.static('dist')); | |
app.get('/', (req, res, next)=> res.sendFile(path.join(__dirname, 'index.html'))); | |
app.get('/products', async(req, res, next)=> { | |
try { | |
res.send(await Product.findAll({ | |
include: [ | |
Color, Size | |
] | |
})); | |
} | |
catch(ex){ | |
next(ex); | |
} | |
}); | |
const Product = conn.define('product', { | |
name: { | |
type: Sequelize.STRING | |
}, | |
inStock: { | |
type: Sequelize.BOOLEAN, | |
defaultValue: true | |
} | |
}); | |
const Color = conn.define('color', { | |
name: { | |
type: Sequelize.STRING | |
} | |
}); | |
const Size = conn.define('size', { | |
name: { | |
type: Sequelize.STRING | |
} | |
}); | |
Product.belongsTo(Size); | |
Product.belongsTo(Color); | |
const setup = async()=> { | |
try { | |
await conn.sync({ force: true }); | |
const [red, blue, green, sm, md, lg] = await Promise.all([ | |
...['red', 'blue', 'green'].map( name => Color.create({ name })), | |
...['sm','md','lg'].map(name => Size.create({ name })) | |
]); | |
await Promise.all([ | |
Product.create({ name: 'foo', colorId: red.id, sizeId: sm.id, inStock: false}), | |
Product.create({ name: 'bar', colorId: blue.id, sizeId: md.id}), | |
Product.create({ name: 'bazz', colorId: blue.id, sizeId: lg.id}), | |
Product.create({ name: 'quq', colorId: blue.id, sizeId: lg.id}), | |
Product.create({ name: 'quux', colorId: green.id, sizeId: sm.id}), | |
]); | |
const port = process.env.PORT || 3000; | |
app.listen(port, ()=> console.log(`listening on port ${port}`)); | |
} | |
catch(ex){ | |
console.log(ex); | |
} | |
}; | |
setup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment