Can I have multiple databases?

Strapi allows multiple connections:

module.exports = ({ env }) => ({
  defaultConnection: 'default', // this defines the default connections
  connections: {
    default: { //that's the default connection
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/main.db'), //uses main db
      },
      options: {
        useNullAsDefault: true,
      },
    },
    messages: { //that's the connection for messages db
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/messages.db'), //uses another db
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});

Now in model file config you can define which connection should be used for that model:

{
  "kind": "collectionType",
  "connection": "messages", //use the "default" connection or the "messages" connection.
  "info": {
    "name": "messages",
    "description": "This represents the Messages model"
  },
  "attributes": {
     // ... 
  }
}
5 Likes