From 6a9bf9572c25537c57e7e624a7cd3ee385cb558e Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 31 Dec 2020 21:04:03 +0800 Subject: [PATCH] fix: view filtering does not work --- packages/app/src/api/index.ts | 41 ++++++++++---------- packages/database/src/utils.ts | 2 +- packages/plugin-pages/src/actions/getView.ts | 3 ++ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index 521adbc696..6c23097a12 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -91,26 +91,26 @@ api.resourcer.use(async (ctx: actions.Context, next) => { await next(); }); -api.resourcer.use(async (ctx: actions.Context, next) => { - const { resourceField, resourceName, viewName, filter } = ctx.action.params; - // TODO: 需要补充默认视图的情况 - let view: any; - if (viewName) { - view = await ctx.db.getModel('views').findOne({ - where: { - collection_name: resourceField ? resourceField.options.target : resourceName, - name: viewName, - } - }); - const viewFilter = view.get('filter'); - if (viewFilter) { - const args = [viewFilter, filter].filter(Boolean); - ctx.action.setParam('filter', {and: args}); - console.log(ctx.action.params.filter); - } - } - await next(); -}); +// api.resourcer.use(async (ctx: actions.Context, next) => { +// const { resourceField, resourceName, viewName, filter } = ctx.action.params; +// // TODO: 需要补充默认视图的情况 +// let view: any; +// if (viewName) { +// view = await ctx.db.getModel('views').findOne({ +// where: { +// collection_name: resourceField ? resourceField.options.target : resourceName, +// name: viewName, +// } +// }); +// const viewFilter = view.get('filter'); +// if (viewFilter) { +// const args = [viewFilter, filter].filter(Boolean); +// ctx.action.setParam('filter', {and: args}); +// console.log(ctx.action.params.filter); +// } +// } +// await next(); +// }); api.registerPlugin('plugin-collections', [path.resolve(__dirname, '../../../plugin-collections'), {}]); api.registerPlugin('plugin-pages', [path.resolve(__dirname, '../../../plugin-pages'), {}]); @@ -119,6 +119,7 @@ api.registerPlugin('plugin-file-manager', [path.resolve(__dirname, '../../../plu (async () => { await api.loadPlugins(); + await api.database.getModel('collections').load({skipExisting: true}); api.listen(process.env.HTTP_PORT, () => { console.log(`http://localhost:${process.env.HTTP_PORT}/`); }); diff --git a/packages/database/src/utils.ts b/packages/database/src/utils.ts index 2049102c6f..f315effcac 100644 --- a/packages/database/src/utils.ts +++ b/packages/database/src/utils.ts @@ -53,7 +53,7 @@ export function toWhere(options: any, context: ToWhereContext = {}) { let k; switch (typeof opKey) { case 'function': - const name = model ? model.options.name.plural : ''; + const name = model ? model.name : ''; const result = opKey(items[key], { model, database, diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts index 7e14d42990..08068820f8 100644 --- a/packages/plugin-pages/src/actions/getView.ts +++ b/packages/plugin-pages/src/actions/getView.ts @@ -222,6 +222,9 @@ export default async (ctx, next) => { if (resourceName === 'collections') { actionDefaultParams.sort = ['sort']; } + if (view.filter) { + actionDefaultParams.filter = view.filter; + } const appends = []; for (const field of fields) {