Answer the question
In order to leave comments, you need to log in
Service - Repository approach in express?
Good evening, I use the approach with layers - Service, Repository (I don’t know how such an organization is called correctly)
The bottom line is that the route handler should not be a big sheet with calls to the database and so on, but be simple and use the services / repositories API:
const router = express.Router();
router.get('/', async (req, res) => {
const posts = await PostService.getAll(req.query.page);
res.json({
count: posts.count,
results: posts.rows,
});
});
router.post('/', async (req, res) => {
const post = await PostService.create(req.body);
res.json(post);
});
// и тд
const PostRepository = require('./PostRepository');
class PostService {
async static getAll(page) {
const limit = 15;
const offset = page > 0? +page : 0;
return await PostRepository.getAll(limit, offset);
}
async static create(post) {
const transaction = db.beginTransaction();
try {
const newPost = await PostRepository.create(post);
// какие-то еще действия с newPost
transaction.commit();
} catch(err) {
transaction.rollBack();
return false;
}
return newPost;
}
}
module.exports = PostService;
const Post = require('./PostModel');
class PostRepository {
static getAll(limit, offset) {
return Post.findAndCountAll({ limit, offset });
}
static create(body) {
let result = Post.create({ title: body.title, ...});
if(!result) {
throw new Error('ошибка создания записи');
} else {
return result;
}
}
}
module.exports = PostRepository;
Answer the question
In order to leave comments, you need to log in
Your Post.create({ title: body.title, ...}) will throw an exception (as I understand sequelize is used), the if(!result) check seems redundant to me.
I also aggregate static functions into a class, so you don’t need to write a bunch of module.exports. It’s
more convenient to do transactions with a callback, so you don’t need to write rollback and commit.
Taking into account all of the above, it turns out that PostRepository is not needed, the model can handle this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question