Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb multiplexer #83

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,12 @@ Self-hosted arrangements can also be made. For more information contact the Apos
If we are hosting Apostrophe Assembly for you, then you can deploy updates to your staging cloud by pushing to your `staging` git branch, and deploy updates to your production cloud by pushing to your `production` git branch. You will receive notifications in our shared Slack channel, including links to access the deployment progress logs.

Apostrophe will complete asset builds for each theme, as well as running any necessary new database migrations for each site, before switching to the newly deployed version of the code.


## Using MongoDB Multiplexer

Apostrophe creates one MongoDB database per site. With very large numbers of sites (more than about 250), this may lead to performance problems. This can be addressed using our MongoDB Multiplexer, which allows many virtual databases to be stored in a single database, *provided that all sites have the same MongoDB collection structure and indexes* (or a compatible subset thereof).

To use this feature the project must be launched with `MONGODB_MULTIPLEXER_URI` set to the MongoDB connection URI of the single real database in which your sites and dashboard should be stored.

Note that MongoDB Multiplexer has some additional restrictions and limitations. For more information see the [MongoDB Multiplexer documentation](https://www.npmjs.com/package/@apostrophecms-pro/mongodb-multiplexer).
50 changes: 34 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
const mongo = require('mongodb');
const mm = require('@apostrophecms-pro/mongodb-multiplexer');

// Domain name configuration is in domains.js
const domains = require('./domains.js');

const dashboardHostnames = Object.values(domains).map(domain => `dashboard.${domain}`.replace(/:\d+$/, ''));

require('@apostrophecms-pro/multisite')({
// Default port, for dev
port: 3000,
// Change this to a hardcoded string when forking to make a new project.
// Just set it to a string which should never change. Ideally should match
// your repo name followed by a -, however if you plan to use a
// cheap Atlas cluster (below M10), you must use a unique prefix less
// than 12 characters (before the -).
shortNamePrefix: process.env.APOS_PREFIX || 'a3ab-',
// For development. An environment variable overrides this in staging/production
mongodbUrl: 'mongodb://localhost:27017',
sessionSecret: 'CHANGEME',
sites: require('./sites/index.js'),
dashboard: require('./dashboard/index.js'),
dashboardHostname: dashboardHostnames
}).then(function (result) {
go().then(function (result) {
// There is no top level await so we catch this here.
// At this point either the task is running or the site is up.
// A task will exit on its own.
}).catch(function (err) {
console.error(err);
process.exit(1);
});

async function go() {
let client = null;
if (process.env.MONGODB_MULTIPLEXER_URI) {
const realClient = await mongo.MongoClient.connect(process.env.MONGODB_MULTIPLEXER_URI, {
useUnifiedTopology: true
});
const realDb = realClient.db();
client = mm({
db: realDb
});
}
await require('@apostrophecms-pro/multisite')({
// Default port, for dev
port: 3000,
// Change this to a hardcoded string when forking to make a new project.
// Just set it to a string which should never change. Ideally should match
// your repo name followed by a -, however if you plan to use a
// cheap Atlas cluster (below M10), you must use a unique prefix less
// than 12 characters (before the -).
shortNamePrefix: process.env.APOS_PREFIX || 'a3ab-',
// For development. An environment variable overrides this in staging/production
mongodbUrl: 'mongodb://localhost:27017',
sessionSecret: 'CHANGEME',
sites: require('./sites/index.js'),
dashboard: require('./dashboard/index.js'),
dashboardHostname: dashboardHostnames,
client
});
}
Loading