MongoDB and PostgreSQL

Run Spica on MongoDB or PostgreSQL, set each one up correctly, and know the differences your applications will see.

Spica runs on either MongoDB or PostgreSQL. You choose the engine when you create an instance with --database-type, and it stays the same for the instance's lifetime. Both engines support buckets, functions, passport, storage, realtime and the rest of the platform.

MongoDB

Terminal
--database-type=mongodb \
--database-uri="mongodb://mongo-0,mongo-1,mongo-2/?replicaSet=rs0" \
--database-name=spica \
--database-replica-set=rs0
  • MongoDB must run as a replica set, even with a single node. Spica uses change streams for realtime, triggers and webhooks.
  • Credentials can be passed with MONGODB_USERNAME and MONGODB_PASSWORD.
  • Tune --database-pool-size, --database-read-preference and --database-change-stream-await-time for your load.

PostgreSQL

Terminal
--database-type=postgresql \
--database-uri="postgres://postgres:5432/spica"
  • Use the spicaengine/postgres image, or a PostgreSQL server with the pg_cron extension. Spica uses it to expire logs, statuses, tokens and other short-lived records.
  • Credentials can be passed with POSTGRES_USERNAME and POSTGRES_PASSWORD.
  • --database-name is ignored; the database is part of the URI.

Differences your applications see

TopicMongoDBPostgreSQL
Resource ids_id, an ObjectId stringid, a UUIDv7 string
Document keys_id on every documentThe bucket's primary property, which must be uuid, integer, bigint or string and can be generated
Extra property typesuuid, integer, bigint (returned as a string)
Bucket filtersExpressions and JSON (MongoDB query) filtersExpressions only
Translated propertiesAny property typeScalar types
Relations inside arraysSupportedNot supported
Index optionsAny MongoDB index optionunique, sparse, background. 2dsphere indexes have one property and aren't unique.
Document limit with removeSupportedNot with a string primary key
GraphQLSupportedNot available
@spica-devkit/databaseSupportedNot available
@spica-devkit/testingSupportedMongoDB only

A PostgreSQL bucket and a document look like this:

JSON
{"id": "01a2235d-7c41-789b-b7f5-fb7beb95c31e", "title": "Posts", "primary": "id", "properties": {"id": {"type": "bigint", "options": {"generated": true}}}}
JSON
{"id": "1", "slug": "hello-spica", "title": "Hello Spica", "views": 42, "author": "01a2235d-7c6a-7fab-b705-48e72471385d"}

Writing portable code

If your code must run on both engines:

  • Use expression filters everywhere.
  • Read ids through a helper that checks _id and then id.
  • Avoid engine-only features from the table above.

Choosing an engine

Choose the database your team already operates well. MongoDB suits flexible, nested documents and makes JSON filters and GraphQL available. PostgreSQL suits teams that standardize on relational databases, want typed primary keys, or run managed PostgreSQL services.

Running the database

SetupMongoDBPostgreSQL
CLIspica project start <name>spica project start <name> --database-type postgresql
Helmdatabase.type: mongodbdatabase.type: postgresql
ManagedMongoDB Atlas or any replica setAny PostgreSQL with pg_cron

Back up the database regularly, together with the volume or bucket that holds files and function sources.