Backend
The feature lib/ directory is in @INC, so Clarive loads its BaselinerX::* modules at startup,
as with any other feature. No core change is needed. Perl changes need a web server restart.
Permissions (actions)¶
lib/BaselinerX/HelloReact.pm:
package BaselinerX::HelloReact; use Moose; use Baseliner::Core::Registry ':dsl'; use Baseliner::Utils qw(_locl); register 'action.hello_react.view' => { name => _locl('View Hello React pages') }; register 'action.hello_react.config' => { name => _locl('Configure Hello React in a project') }; register 'action.hello_react.admin' => { name => _locl('Administer Hello React') }; no Moose; __PACKAGE__->meta->make_immutable; 1;
- The actions show up in the role editor (under the
hello_reactfolder). - Once granted in a role, they reach the browser in
appData.permissions, which is what the SDK checks in menus, routes andcan(). - Keep the names in a
src/actions.jsto avoid repeating strings in the frontend.
Endpoints: /feature/<id>/api/...¶
lib/BaselinerX/Controller/HelloReact.pm:
package BaselinerX::Controller::HelloReact; use Moose; BEGIN { extends 'Catalyst::Controller' } use Baseliner::Utils qw(_now); # sdk.api.get('greeting') -> GET /feature/hello-react/api/greeting __PACKAGE__->config( namespace => 'feature/hello-react/api' ); sub greeting : Local : Does('ACL') : ACL('action.hello_react.view') { my ( $self, $c ) = @_; my $p = $c->req->params; $c->stash->{json} = { success => \1, message => 'Hello ' . $c->username, id_project => $p->{id_project}, time => _now(), }; $c->forward('View::JSON'); } sub save : Local : Does('ACL') : ACL('action.hello_react.admin') { my ( $self, $c ) = @_; my $name = $c->req->params->{name} // ''; $c->stash->{json} = { success => \1, saved => $name, by => $c->username }; $c->forward('View::JSON'); } no Moose; __PACKAGE__->meta->make_immutable; 1;
Rules:
- namespace =
'feature/<id>/api', with the exact directory<id>. That is whatsdk.apicalls. - Every action has
Does('ACL') : ACL('action....'). Without the action, the server answers a JSON 403 andsdk.apirejects witherr.status === 403. Hiding a button or a route in the frontend protects nothing. - Parameters:
api.getandapi.postarrive in$c->req->params.api.postJSON: the JSON (if it is an object) is merged into$c->req->params, and the original JSON, with its arrays intact, is kept in$c->req->{body_data}.
- Response:
$c->stash->{json} = {...}and$c->forward('View::JSON'). For errors, return an HTTP error status with amsg: that is what the frontend gets inerr.message. - A path under
/feature/<id>/api/that does not exist returns a JSON 404 (not an HTML page). - Everything under
/feature/<id>/requires a logged in session, unlike/plugin/<id>/....
The usual tools are available in the controller: $c->username, mdb->..., ci->..., Util->...,
as in any core controller.
Declaring components for a repository: viewer_components¶
The core screens that accept feature components (see SDK Reference)
learn which component to paint from the repository CI: its viewer_components method, which
returns a map of place to full component name. The core defines it empty ({}) in
Baseliner::Role::CI::Repository and sends it to the frontend with each revision and repository.
It is declared by the owner of the repository type, never by the core. Only the keys revision,
select and browse are used; the missing ones get the core view.
-
A feature with a Perl backend: its own CI class in
<feature>/lib/BaselinerX/CI/, loaded with the rest of the feature'sBaselinerX::*modules:```perl package BaselinerX::CI::HelloRepository; use Moose; BEGIN { extends 'BaselinerX::CI::GitRepository' }
sub collection { 'HelloRepository' } sub viewer_components { +{ revision => 'hello-react/RevisionViewer' } }
1; ```
-
A JS plugin CI: next to its
viewer_type:js viewer_type: () => 'reserve', viewer_components: () => ({ revision: 'foo/RevisionViewer', select: 'foo/SelectRevisions', browse: 'foo/BrowseRepository' }),
The topic view is cached: after changing viewer_components, restart the web server and clear the
cache (cla db-cache_clear) before checking a topic.
Repositories with their own revisions: revision hooks¶
A select component usually attaches revisions that are not plain VCS refs (objects picked in the
UI, uploaded files...). The core does not know how to build, delete or trash them, so it asks the
repository and revision CIs through these optional methods. None of them is needed for a git-like
repository: without them the core keeps its usual behaviour.
| Method | On | Called when | Returns |
|---|---|---|---|
create_revision_from_data({ revision, id_revision, topic_mid }) |
repository | a revision is attached to a topic (/ci/attach_revisions); revision is the decoded revision parameter sent by the component |
{ mid }, or { error => 1, msg => '...' } |
delete_revision($ci_revision) |
repository | a revision is deleted from a topic (/topic/delete_revision), instead of the git branch cleanup |
true on success |
revisions_owned_by_topic |
repository | a revision is removed from a topic revisions field: when true, it is also deleted (with delete_revision if the repository has it) |
0 by default |
owned_data |
any CI | the CI is deleted | { collection => query }: documents of other collections that belong to the CI. They go to the trash with it, are deleted, and are restored with it. {} by default |
In a JS plugin CI they are plain methods. The CI they are called on is this, so write them as
functions, not arrow functions:
methods: { create_revision_from_data: function (args) { // this = the repository; args = { revision, id_revision, topic_mid } // create or update the revision CI, return { mid: ... } }, delete_revision: function (revision) { // clean up what the revision left behind, then: require('cla/ci').delete(revision.mid()); return 1; }, revisions_owned_by_topic: function () { return 1; } }
and in the revision class:
methods: { owned_data: function () { return { foo_objects: { id_revision: this.mid() } }; } }
Data migrations¶
A feature that changes the structure of its data ships the change as a migration in
<feature>/migrations/NNNN_name.pm (or .js). cla migra runs them after the core ones and keeps
their version apart (migration_feature_<id>, in the clarive collection).
package BaselinerX::Foo::Migrations::0001_drop_old_field; use Moose; sub upgrade { mdb->topic->update_many( { old_field => { '$exists' => 1 } }, { '$unset' => { old_field => 1 } } ); } sub downgrade { } 1;
Write upgrade so that running it twice changes nothing.
The first time a feature with migrations is installed, run them by hand:
cla migra-start --init
The migrations of a newly installed feature are not initialized: the server does not start (it
reports that migrations are not up to date) and --migrate / --migrate-yes stop with
"Migrations are not initialized". --init initializes them (the core and the features already
initialized are skipped) and then runs them all. cla migra-state shows the version of each feature.
From then on, new migrations of the feature behave like the core ones: the server refuses to start
until they are run with cla migra-start, or started with --migrate (asks) / --migrate-yes.