Using the magic of joins
This commit is contained in:
@ -5,10 +5,9 @@ import httpErrors from 'http-errors';
|
|||||||
import auth from './middlewares/authentication';
|
import auth from './middlewares/authentication';
|
||||||
import pagination from './middlewares/pagination';
|
import pagination from './middlewares/pagination';
|
||||||
import { presentDocument } from '../presenters';
|
import { presentDocument } from '../presenters';
|
||||||
import { Document, Collection, Star, View } from '../models';
|
import { User, Document, Collection, Star, View } from '../models';
|
||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
router.post('documents.list', auth(), pagination(), async ctx => {
|
router.post('documents.list', auth(), pagination(), async ctx => {
|
||||||
let { sort = 'updatedAt', direction } = ctx.body;
|
let { sort = 'updatedAt', direction } = ctx.body;
|
||||||
if (direction !== 'ASC') direction = 'DESC';
|
if (direction !== 'ASC') direction = 'DESC';
|
||||||
@ -19,6 +18,7 @@ router.post('documents.list', auth(), pagination(), async ctx => {
|
|||||||
order: [[sort, direction]],
|
order: [[sort, direction]],
|
||||||
offset: ctx.state.pagination.offset,
|
offset: ctx.state.pagination.offset,
|
||||||
limit: ctx.state.pagination.limit,
|
limit: ctx.state.pagination.limit,
|
||||||
|
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
|
||||||
});
|
});
|
||||||
|
|
||||||
let data = await Promise.all(documents.map(doc => presentDocument(ctx, doc)));
|
let data = await Promise.all(documents.map(doc => presentDocument(ctx, doc)));
|
||||||
@ -60,7 +60,12 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
|
|||||||
const views = await Star.findAll({
|
const views = await Star.findAll({
|
||||||
where: { userId: user.id },
|
where: { userId: user.id },
|
||||||
order: [[sort, direction]],
|
order: [[sort, direction]],
|
||||||
include: [{ model: Document }],
|
include: [
|
||||||
|
{
|
||||||
|
model: Document,
|
||||||
|
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
|
||||||
|
},
|
||||||
|
],
|
||||||
offset: ctx.state.pagination.offset,
|
offset: ctx.state.pagination.offset,
|
||||||
limit: ctx.state.pagination.limit,
|
limit: ctx.state.pagination.limit,
|
||||||
});
|
});
|
||||||
|
@ -115,7 +115,31 @@ const Document = sequelize.define(
|
|||||||
},
|
},
|
||||||
classMethods: {
|
classMethods: {
|
||||||
associate: models => {
|
associate: models => {
|
||||||
Document.belongsTo(models.User);
|
Document.belongsTo(models.Collection, {
|
||||||
|
foreignKey: 'atlasId',
|
||||||
|
});
|
||||||
|
Document.belongsTo(models.User, {
|
||||||
|
as: 'createdBy',
|
||||||
|
foreignKey: 'createdById',
|
||||||
|
});
|
||||||
|
Document.belongsTo(models.User, {
|
||||||
|
as: 'updatedBy',
|
||||||
|
foreignKey: 'lastModifiedById',
|
||||||
|
});
|
||||||
|
Document.hasMany(models.Star, {
|
||||||
|
as: 'starred',
|
||||||
|
});
|
||||||
|
Document.addScope(
|
||||||
|
'defaultScope',
|
||||||
|
{
|
||||||
|
include: [
|
||||||
|
{ model: models.Collection },
|
||||||
|
{ model: models.User, as: 'createdBy' },
|
||||||
|
{ model: models.User, as: 'updatedBy' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ override: true }
|
||||||
|
);
|
||||||
},
|
},
|
||||||
findById: async id => {
|
findById: async id => {
|
||||||
if (isUUID(id)) {
|
if (isUUID(id)) {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { Collection, Star, User, View } from '../models';
|
// @flow
|
||||||
|
import { Star, User, Document, View } from '../models';
|
||||||
import presentUser from './user';
|
import presentUser from './user';
|
||||||
import presentCollection from './collection';
|
import presentCollection from './collection';
|
||||||
|
|
||||||
async function present(ctx, document, options) {
|
async function present(ctx: Object, document: Document, options: Object = {}) {
|
||||||
options = {
|
options = {
|
||||||
includeCollection: true,
|
includeCollection: true,
|
||||||
includeCollaborators: true,
|
includeCollaborators: true,
|
||||||
@ -10,8 +11,6 @@ async function present(ctx, document, options) {
|
|||||||
...options,
|
...options,
|
||||||
};
|
};
|
||||||
ctx.cache.set(document.id, document);
|
ctx.cache.set(document.id, document);
|
||||||
|
|
||||||
const userId = ctx.state.user.id;
|
|
||||||
const data = {
|
const data = {
|
||||||
id: document.id,
|
id: document.id,
|
||||||
url: document.getUrl(),
|
url: document.getUrl(),
|
||||||
@ -20,37 +19,23 @@ async function present(ctx, document, options) {
|
|||||||
text: document.text,
|
text: document.text,
|
||||||
html: document.html,
|
html: document.html,
|
||||||
preview: document.preview,
|
preview: document.preview,
|
||||||
|
collection: presentCollection(ctx, document.collection),
|
||||||
createdAt: document.createdAt,
|
createdAt: document.createdAt,
|
||||||
createdBy: undefined,
|
createdBy: presentUser(ctx, document.createdBy),
|
||||||
updatedAt: document.updatedAt,
|
updatedAt: document.updatedAt,
|
||||||
updatedBy: undefined,
|
updatedBy: presentUser(ctx, document.updatedBy),
|
||||||
team: document.teamId,
|
team: document.teamId,
|
||||||
collaborators: [],
|
collaborators: [],
|
||||||
|
starred: !!document.starred,
|
||||||
|
views: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
data.starred = !!await Star.findOne({
|
|
||||||
where: { documentId: document.id, userId },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (options.includeViews) {
|
if (options.includeViews) {
|
||||||
data.views = await View.sum('count', {
|
data.views = await View.sum('count', {
|
||||||
where: { documentId: document.id },
|
where: { documentId: document.id },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.includeCollection) {
|
|
||||||
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
|
||||||
const collection =
|
|
||||||
options.collection ||
|
|
||||||
(await Collection.findOne({
|
|
||||||
where: {
|
|
||||||
id: document.atlasId,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
return presentCollection(ctx, collection);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.includeCollaborators) {
|
if (options.includeCollaborators) {
|
||||||
// This could be further optimized by using ctx.cache
|
// This could be further optimized by using ctx.cache
|
||||||
data.collaborators = await User.findAll({
|
data.collaborators = await User.findAll({
|
||||||
@ -62,18 +47,6 @@ async function present(ctx, document, options) {
|
|||||||
}).map(user => presentUser(ctx, user));
|
}).map(user => presentUser(ctx, user));
|
||||||
}
|
}
|
||||||
|
|
||||||
const createdBy = await ctx.cache.get(
|
|
||||||
document.createdById,
|
|
||||||
async () => await User.findById(document.createdById)
|
|
||||||
);
|
|
||||||
data.createdBy = await presentUser(ctx, createdBy);
|
|
||||||
|
|
||||||
const updatedBy = await ctx.cache.get(
|
|
||||||
document.lastModifiedById,
|
|
||||||
async () => await User.findById(document.lastModifiedById)
|
|
||||||
);
|
|
||||||
data.updatedBy = await presentUser(ctx, updatedBy);
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user