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 Story = require('./story'); | |
const Tag = require('./tag'); | |
const TagLink = require('./tagLink'); | |
const User = require('./user'); | |
TagLink.belongsTo(Tag); | |
Tag.Associate([Story, User]); |
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 db = require('../_db'); | |
const Sequelize = require('sequelize'); | |
class tag extends Sequelize.Model { | |
Associate(Models) { | |
for (const Model of Models) { | |
Model.belongsToMany(this, { | |
foreignKey: 'foreign_key', | |
constraints: false, | |
through: { |
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 db = require('../_db'); | |
const Sequelize = require('sequelize'); | |
class tag_link extends Sequelize.Model { | |
//methods, hooks, etc.... | |
} | |
tag_link.init({ | |
table: { | |
type: Sequelize.STRING, |
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 db = require('../_db'); | |
const Sequelize = require('sequelize'); | |
class tag_link extends Sequelize.Model { | |
} | |
tag_link.init({ | |
//fill in | |
},{ |
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 Tag = require('./tag') | |
const Story = require('./story') | |
Tag.belongsToMany(Story) | |
Story.belongsToMany(Tag) | |
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 db = require('../_db'); | |
const Sequelize = require('sequelize'); | |
class story extends Sequelize.Model { | |
//methods, hooks, etc.... | |
} | |
story.init({ | |
title: { | |
type: Sequelize.STRING, |
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 db = require('../_db'); | |
const Sequelize = require('sequelize'); | |
class tag extends Sequelize.Model { | |
//methods, hooks, etc.... | |
} | |
tag.init({ | |
name: { | |
type: Sequelize.STRING(100), |
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 asyncMiddleware = require('./utils/asyncMiddleware'); | |
router.get('/users/:id', asyncMiddleware(async (req, res, next) => { | |
/* | |
if there is an error thrown in getUserFromDb, asyncMiddleware | |
will pass it to next() and express will handle the error; | |
*/ | |
const user = await getUserFromDb({ id: req.params.id }) | |
res.json(user); | |
})); |
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 asyncMiddleware = fn => | |
(req, res, next) => { | |
Promise.resolve(fn(req, res, next)) | |
.catch(next); | |
}; |
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
router.get('/user/:id', async (req, res, next) => { | |
try { | |
const user = await getUserFromDb({ id: req.params.id }) | |
res.json(user); | |
} catch (e) { | |
//this will eventually be handled by your error handling middleware | |
next(e) | |
} | |
}) |