fix: view filtering does not work

This commit is contained in:
chenos 2020-12-31 21:04:03 +08:00
parent f7f8e12ac9
commit 6a9bf9572c
3 changed files with 25 additions and 21 deletions

View File

@ -91,26 +91,26 @@ api.resourcer.use(async (ctx: actions.Context, next) => {
await next(); await next();
}); });
api.resourcer.use(async (ctx: actions.Context, next) => { // api.resourcer.use(async (ctx: actions.Context, next) => {
const { resourceField, resourceName, viewName, filter } = ctx.action.params; // const { resourceField, resourceName, viewName, filter } = ctx.action.params;
// TODO: 需要补充默认视图的情况 // // TODO: 需要补充默认视图的情况
let view: any; // let view: any;
if (viewName) { // if (viewName) {
view = await ctx.db.getModel('views').findOne({ // view = await ctx.db.getModel('views').findOne({
where: { // where: {
collection_name: resourceField ? resourceField.options.target : resourceName, // collection_name: resourceField ? resourceField.options.target : resourceName,
name: viewName, // name: viewName,
} // }
}); // });
const viewFilter = view.get('filter'); // const viewFilter = view.get('filter');
if (viewFilter) { // if (viewFilter) {
const args = [viewFilter, filter].filter(Boolean); // const args = [viewFilter, filter].filter(Boolean);
ctx.action.setParam('filter', {and: args}); // ctx.action.setParam('filter', {and: args});
console.log(ctx.action.params.filter); // console.log(ctx.action.params.filter);
} // }
} // }
await next(); // await next();
}); // });
api.registerPlugin('plugin-collections', [path.resolve(__dirname, '../../../plugin-collections'), {}]); api.registerPlugin('plugin-collections', [path.resolve(__dirname, '../../../plugin-collections'), {}]);
api.registerPlugin('plugin-pages', [path.resolve(__dirname, '../../../plugin-pages'), {}]); api.registerPlugin('plugin-pages', [path.resolve(__dirname, '../../../plugin-pages'), {}]);
@ -119,6 +119,7 @@ api.registerPlugin('plugin-file-manager', [path.resolve(__dirname, '../../../plu
(async () => { (async () => {
await api.loadPlugins(); await api.loadPlugins();
await api.database.getModel('collections').load({skipExisting: true});
api.listen(process.env.HTTP_PORT, () => { api.listen(process.env.HTTP_PORT, () => {
console.log(`http://localhost:${process.env.HTTP_PORT}/`); console.log(`http://localhost:${process.env.HTTP_PORT}/`);
}); });

View File

@ -53,7 +53,7 @@ export function toWhere(options: any, context: ToWhereContext = {}) {
let k; let k;
switch (typeof opKey) { switch (typeof opKey) {
case 'function': case 'function':
const name = model ? model.options.name.plural : ''; const name = model ? model.name : '';
const result = opKey(items[key], { const result = opKey(items[key], {
model, model,
database, database,

View File

@ -222,6 +222,9 @@ export default async (ctx, next) => {
if (resourceName === 'collections') { if (resourceName === 'collections') {
actionDefaultParams.sort = ['sort']; actionDefaultParams.sort = ['sort'];
} }
if (view.filter) {
actionDefaultParams.filter = view.filter;
}
const appends = []; const appends = [];
for (const field of fields) { for (const field of fields) {