Skip to main content

Collections

Create, change and delete collections. Every endpoint needs a superuser: anyone else gets a 401 when signed out and a 403 when signed in.

In the SDK examples, superuser is a client signed in as a superuser, or nextbase.client({ superuser: true }) in server code.

List collections​

const collections = await superuser.collections.getFullList();
GET /collections

Responses

{
"page": 1,
"perPage": 2,
"totalItems": 2,
"totalPages": 1,
"items": [
{ "id": "_superusers", "name": "_superusers", "type": "auth", "system": true, "...": "..." },
{
"id": "c1a2b3c4d5e6f7g",
"name": "posts",
"type": "base",
"system": false,
"fields": [
{ "id": "_id", "name": "id", "type": "text", "system": true, "primaryKey": true, "...": "..." },
{ "id": "title00000", "name": "title", "type": "text", "required": true, "max": 200, "...": "..." }
],
"indexes": [],
"listRule": "",
"viewRule": "",
"createRule": null,
"updateRule": null,
"deleteRule": null
}
]
}

View a collection​

GET /collections/{collection}

Returns one collection, by name or id. SDK: superuser.collections.getOne("posts").

Create a collection​

await superuser.collections.create({
name: "posts",
listRule: "",
viewRule: "",
fields: [
{ name: "title", type: "text", required: true, max: 200 },
{ id: "slug000000", name: "slug", type: "text", required: true },
{ name: "created", type: "autodate" },
],
indexes: [{ name: "posts_slug", unique: true, fields: ["slug000000"] }],
});
POST /collections

Body

ParamTypeDescription
namestringRequired. Letters, digits and _, starting with a letter.
typestringOptional. base (default) or auth.
idstringOptional. 15 characters from a-z0-9. Generated when left out.
fieldsarrayOptional. Field definitions. System fields are added for you. Without fields, created and updated are added.
indexesarrayOptional. { name, fields, unique?, type? }, where fields are field ids.
listRule, viewRule, createRule, updateRule, deleteRulestring or nullOptional. Rules. Default null (superusers only).
authRule, manageRulestring or nullOptional, auth collections. Defaults "" and null.
identityFieldsstring[]Optional, auth collections. Fields to sign in with. Default ["email"].

Responses

The created collection, with every default filled in.

Update a collection​

Applies a new definition. Settings you leave out keep their current value. Fields are matched by id: send the existing fields (with their ids) along with new ones, since a field missing from fields is dropped with its data.

const posts = await superuser.collections.getOne("posts");
await superuser.collections.update("posts", {
fields: [...posts.fields, { name: "summary", type: "text" }],
listRule: "published = true",
});
PATCH /collections/{collection}

Body: any of the fields accepted on create.

Responses: 200 with the updated collection, or 400 with field errors. A change that conflicts with existing data, such as a unique index over duplicate values, is a 400, and nothing is changed.

Delete a collection​

Deletes the collection, its table and all of its records. A collection that other collections' relation fields point at cannot be deleted.

await superuser.collections.delete("posts");
DELETE /collections/{collection}

Responses: 204 with no body, 400 when it cannot be deleted, 404 when it does not exist.