Tidy, move recent documents to query scope

This commit is contained in:
Tom Moor
2017-07-06 22:02:55 -07:00
parent f08ca8d460
commit b854c2ca53
7 changed files with 46 additions and 64 deletions

View File

@ -18,7 +18,7 @@ class DocumentViewersStore {
this.isFetching = true;
try {
const res = await client.get(
const res = await client.post(
'/views.list',
{
id: this.documentId,

View File

@ -24,7 +24,7 @@ router.post('collections.create', auth(), async ctx => {
});
ctx.body = {
data: await presentCollection(ctx, atlas, true),
data: await presentCollection(ctx, atlas),
};
});
@ -33,7 +33,7 @@ router.post('collections.info', auth(), async ctx => {
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const atlas = await Collection.findOne({
const atlas = await Collection.scope('withRecentDocuments').findOne({
where: {
id,
teamId: user.teamId,
@ -43,13 +43,13 @@ router.post('collections.info', auth(), async ctx => {
if (!atlas) throw httpErrors.NotFound();
ctx.body = {
data: await presentCollection(ctx, atlas, true),
data: await presentCollection(ctx, atlas),
};
});
router.post('collections.list', auth(), pagination(), async ctx => {
const user = ctx.state.user;
const collections = await Collection.findAll({
const collections = await Collection.scope('withRecentDocuments').findAll({
where: {
teamId: user.teamId,
},
@ -58,16 +58,10 @@ router.post('collections.list', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
// Collectiones
let data = [];
await Promise.all(
collections.map(async atlas => {
return data.push(await presentCollection(ctx, atlas, true));
})
const data = await Promise.all(
collections.map(async atlas => await presentCollection(ctx, atlas))
);
data = _.orderBy(data, ['updatedAt'], ['desc']);
ctx.body = {
pagination: ctx.state.pagination,
data,

View File

@ -21,7 +21,9 @@ router.post('documents.list', auth(), pagination(), async ctx => {
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
});
let data = await Promise.all(documents.map(doc => presentDocument(ctx, doc)));
const data = await Promise.all(
documents.map(document => presentDocument(ctx, document))
);
ctx.body = {
pagination: ctx.state.pagination,
@ -42,7 +44,7 @@ router.post('documents.viewed', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
let data = await Promise.all(
const data = await Promise.all(
views.map(view => presentDocument(ctx, view.document))
);
@ -70,7 +72,7 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
limit: ctx.state.pagination.limit,
});
let data = await Promise.all(
const data = await Promise.all(
views.map(view => presentDocument(ctx, view.document))
);
@ -99,8 +101,7 @@ router.post('documents.info', auth(), async ctx => {
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollection: document.private,
includeCollaborators: true,
includeViews: true,
}),
};
});
@ -113,15 +114,8 @@ router.post('documents.search', auth(), async ctx => {
const documents = await Document.searchForUser(user, query);
const data = [];
await Promise.all(
documents.map(async document => {
data.push(
await presentDocument(ctx, document, {
includeCollaborators: true,
})
);
})
const data = await Promise.all(
documents.map(async document => await presentDocument(ctx, document))
);
ctx.body = {
@ -204,9 +198,7 @@ router.post('documents.create', auth(), async ctx => {
}
ctx.body = {
data: await presentDocument(ctx, newDocument, {
includeCollaborators: true,
}),
data: await presentDocument(ctx, newDocument),
};
});
@ -232,9 +224,7 @@ router.post('documents.update', auth(), async ctx => {
}
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollaborators: true,
}),
data: await presentDocument(ctx, document),
};
});
@ -273,10 +263,7 @@ router.post('documents.move', auth(), async ctx => {
}
ctx.body = {
data: await presentDocument(ctx, document, {
includeCollaborators: true,
collection: collection,
}),
data: await presentDocument(ctx, document),
};
});

View File

@ -60,6 +60,16 @@ const Collection = sequelize.define(
as: 'documents',
foreignKey: 'atlasId',
});
Collection.addScope('withRecentDocuments', {
include: [
{
as: 'documents',
limit: 10,
model: models.Document,
order: [['updatedAt', 'DESC']],
},
],
});
},
},
instanceMethods: {

View File

@ -116,6 +116,7 @@ const Document = sequelize.define(
classMethods: {
associate: models => {
Document.belongsTo(models.Collection, {
as: 'collection',
foreignKey: 'atlasId',
});
Document.belongsTo(models.User, {
@ -133,7 +134,7 @@ const Document = sequelize.define(
'defaultScope',
{
include: [
{ model: models.Collection },
{ model: models.Collection, as: 'collection' },
{ model: models.User, as: 'createdBy' },
{ model: models.User, as: 'updatedBy' },
],

View File

@ -1,8 +1,9 @@
// @flow
import _ from 'lodash';
import { Document } from '../models';
import { Collection } from '../models';
import presentDocument from './document';
async function present(ctx, collection, includeRecentDocuments = false) {
async function present(ctx: Object, collection: Collection) {
ctx.cache.set(collection.id, collection);
const data = {
@ -13,31 +14,21 @@ async function present(ctx, collection, includeRecentDocuments = false) {
type: collection.type,
createdAt: collection.createdAt,
updatedAt: collection.updatedAt,
recentDocuments: undefined,
documents: undefined,
};
if (collection.type === 'atlas')
if (collection.type === 'atlas') {
data.documents = await collection.getDocumentsStructure();
}
if (includeRecentDocuments) {
const documents = await Document.findAll({
where: {
atlasId: collection.id,
},
limit: 10,
order: [['updatedAt', 'DESC']],
});
const recentDocuments = [];
await Promise.all(
documents.map(async document => {
recentDocuments.push(
await presentDocument(ctx, document, {
includeCollaborators: true,
})
if (collection.documents) {
data.recentDocuments = await Promise.all(
collection.documents.map(
async document =>
await presentDocument(ctx, document, { includeCollaborators: true })
)
);
})
);
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
}
return data;

View File

@ -5,9 +5,8 @@ import presentCollection from './collection';
async function present(ctx: Object, document: Document, options: Object = {}) {
options = {
includeCollection: true,
includeCollaborators: true,
includeViews: true,
includeViews: false,
...options,
};
ctx.cache.set(document.id, document);
@ -30,8 +29,8 @@ async function present(ctx: Object, document: Document, options: Object = {}) {
views: undefined,
};
if (options.includeCollection) {
data.collection = presentCollection(ctx, document.collection);
if (document.private) {
data.collection = await presentCollection(ctx, document.collection);
}
if (options.includeViews) {