Creating collections
Collections are the way to describe the structure of data in your application.
Collections contain Items. Each Item has a structure defined by the Fields
defined in the Collection. Items are stored in the database. Access to Items
within a Collection is granted or denied based on Policies assigned to that
Collection.
In order to create a custom collection, you have to add it to your app's definition:
import {App, Collection} from "sealious"; const app = new (class extends App { config = { /* ... */ }; manifest = { /* ... */ }; collections = { ...App.BaseCollections, // Here we define a new collection under the name `tasks` tasks: new (class extends Collection { // the collection has two fields: title (text) and done (boolean) fields = { title: new FieldTypes.Text(), done: new FieldTypes.Boolean(), }; // the default policy for each action for this collection is Public. // Anyone can create. read, edit and delete any Item from this collection. defaultPolicy = new Policies.Public(); })(), }; })();
A collection does not have to be declared inline within the app declaraion. It can easily be stored in a const or exported as a module:
import { Collection } from "sealious"; const tasks = new (class extends Collection { fields = { title: new FieldTypes.Text(), done: new FieldTypes.Boolean(), }; defaultPolicy = new Policies.Public(); })(); export default tasks;
import tasks from "./tasks.js"; const app = new (class extends App { config = { /* ... */ }; manifest = { /* ... */ }; collections = { ...App.BaseCollections, tasks, }; })();
Specifying Policies
You can specify different policies for different actions ran on a given
Collection and its Items. To do that, use the policies key within the
collection's definition. It contains an object where keys are ActionNames
(show, create, edit, delete, list), and values are instances of Policy.
import { Collection } from "sealious"; const tasks = new (class extends Collection { fields = { title: new FieldTypes.Text(), done: new FieldTypes.Boolean(), }; policies = { create: new Policy.Public(), show: new Policy.Owner(), list: new Policy.Owner(), } defaultPolicy = new Policies.Public(); })();
Please note:
- If no policy is specified for an action in the policies object, the defaultPolicy is used;
- You can omit the policies object and just pass defaultPolicy if all the actions have the same policy
- Until {T2103} is resolved, you have to specifiy policies for show and list separately. show is for viewing a single item while knowing its ID, list is for listing all items
Specifying named filters
You can use special filter to segment the collection based on some
filters. Named filters can then be used to apply different Policies based on the
segment the given item belongs to.
import { Collection, SpecialFilters } from "sealious"; const tasks = new (class extends Collection { fields = { title: new FieldTypes.Text(), done: new FieldTypes.Boolean(), }; named_filters = { todo: new SpecialFilters.Matches("tasks", { done: false }), }; })();
With named filters set as above, you can GET /api/v1/collections/tasks/@todo
to list only tasks that are not done yet.
File Metadata
- Mime Type
- text/x-java
- Expires
- Mon, Dec 23, 09:33 (1 d, 5 h)
- Storage Engine
- blob
- Storage Format
- Raw Data
- Storage Handle
- 556156
- Default Alt Text
- creating-collections.remarkup (3 KB)