Core concepts
The building blocks of a Spica project and how they fit together, from buckets and functions to identities, users, policies and API keys.
Buckets
A bucket is a collection of documents described by a JSON schema, like a table with a typed structure. Each bucket gets its own REST endpoints, an optional GraphQL schema and a realtime WebSocket stream. Bucket properties can be strings, numbers, dates, files, locations, relations to other buckets, hashed or encrypted values and more.
Read more in Buckets.
Functions
A function is a Node.js module you write in JavaScript or TypeScript. Each exported handler is connected to a trigger: an HTTP route, a bucket or database change, a schedule, a system event, a firehose message, a gRPC call or a RabbitMQ message. Functions can install npm packages, read environment variables and secrets, and call Spica through the devkit.
Read more in Functions.
Identities and users
Spica separates the people who build and operate your backend from the people who use your product:
| Identities | Users | |
|---|---|---|
| Who | Developers, editors, operators | Your product's end users |
| Log in with | POST /passport/identify | POST /passport/login |
| Token scheme | IDENTITY | USER |
| Typical access | The panel and management APIs | Your data, filtered by bucket rules |
| SSO | SAML | OAuth (Google, GitHub, Facebook, Okta, Auth0, custom) |
Read more in Authentication & access.
API keys
An API key is a long-lived credential for servers, scripts, the CLI and CI pipelines. Like identities and users, an API key only gets the permissions of the policies attached to it.
Policies
A policy is a named list of statements. Each statement allows an action (for example bucket:data:index) on a module, optionally limited to specific resources (for example a single bucket). Spica ships with read-only and full-access policies for every module, and you can write your own.
Policies are attached to identities, users and API keys. Every request is checked against them.
Bucket rules
Policies decide which endpoints a caller may use. Bucket rules decide which documents a user may read or write. A rule is an expression such as document.owner == auth._id, evaluated for every document. Rules apply to USER requests. Identities and API keys are only checked against their policies.
Storage objects
A storage object is a file plus its metadata (name, content type, size). Objects can be organized into folders by their names, referenced from bucket properties of type storage, and stored on local disk, Amazon S3 or Google Cloud Storage.
Environment variables and secrets
Environment variables and secrets are key/value pairs that you attach to functions. Both appear in process.env. Secrets are encrypted at rest and their values are never returned by the API.
Resources as code
Buckets, functions, policies, environment variables and secrets can be exported into a folder of YAML and code files with the CLI, reviewed in pull requests, and applied to any instance with spica plan and spica apply.
Read more in Resources as code.
How a request flows
- The client sends a request with an
Authorizationheader:IDENTITY <token>,USER <token>orAPIKEY <key>. - Spica authenticates the caller and loads the policies attached to it.
- The policies must allow the action on the resource, otherwise the response is
403 Forbidden. - For bucket data requests made with a
USERtoken, the bucket's read or write rule filters or rejects documents. - The request is executed, the activity is recorded, and change events are sent to realtime subscribers, bucket triggers and webhooks.