Skip to content

makeCRUDRoutes ​

Overview ​

Generate CRUD routes for a given app and model.

Call this once during application setup and register the two records it returns. Besides building them, it registers the configured checks as a beforeEach guard, so a navigation that changes the app, model, or action reruns them even when it stays inside one of the two records, and it starts watching for a change of authenticated user. When one happens while the application sits on one of these routes, it reruns the same checks against that route and redirects if the new user may not use it. Neither of those reruns the checks for a navigation that only changes the query string or the primary key, since nothing about the checked metadata depends on either.

The two records are actionrouter.detailview at /:app/:model/:action/:pk, for one object, and actionrouter.listview at /:app/:model/:action/, for a model or several objects. The list route reads pk from the query string instead, as one repeated value per primary key (?pk=4&pk=7), which a bulk action targets. Each value is one whole key and is never split, so ?pk=4,7 names the single key 4,7. Both routes pass app, model, action, and pk to the component as props. Either route may carry a returnPath query value, where an action view goes after it succeeds or is cancelled.

Signature ​

makeCRUDRoutes(params)

Parameters ​

NameTypeRequiredDescription
paramsobjectyesThe parameters.

params Properties ​

NameTypeRequiredDefaultDescription
actionRedirectanyThe route to redirect if model/action not found.
authRedirectstringThe route to redirect to if the user is not authenticated.
componentanyThe component to use for the routes.
groupsstring[]The groups required to access the views.
groupsRedirectanyThe route to redirect to if the user is not in the required groups. Required when groups names at least one group.
pathPrefixstringThe prefix to add to the path.
piniaPiniaThe Pinia instance.
routerRouterThe Vue router instance.
vueAppAppThe Vue app instance.

Returns ​

RouteLocationNormalizedGeneric[]

The generated routes.

Examples ​

js
import { createRouter, createWebHistory } from 'vue-router';
import ActionView from '@/views/ActionView.vue';
import { makeCRUDRoutes } from '@vueda/router/makeCrud.js';

const router = createRouter({ history: createWebHistory(), routes: [] });

// In your app setup (after createApp):
router.addRoute(...makeCRUDRoutes({
    component: ActionView,
    vueApp: app,
    router,
    pinia,
    authRedirect: { name: 'login' },
    actionRedirect: { name: 'not-found' },
}));

Source ​

client/lib/router/makeCrud.js:177