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

Added multi-app support in DatabaseAdapter.js #139

Merged
merged 1 commit into from
Feb 1, 2016
Merged

Added multi-app support in DatabaseAdapter.js #139

merged 1 commit into from
Feb 1, 2016

Conversation

jamiechapman
Copy link

When creating multiple instances of ParseServer to connect to different databases using a single server, the databaseURI was getting overwritten by the last defined connection string (issue: #116).

This patch stores each connection string by appId so that they're not overwritten when creating new ParseServer objects.

This patch then allows the user to host multiple apps on the same server like this:-

var api = new ParseServer({appId:'app1', 'databaseUri:'mongodb://.../database1', ...});
var api2 = new ParseServer({appId:'app2', 'databaseUri:'mongodb://.../database2', ...});

app.use('/app1/parse', api);
app.use('/app2/parse', api2);

@gfosco
Copy link
Contributor

gfosco commented Feb 1, 2016

Sure, looks good. Thanks. 👍

gfosco added a commit that referenced this pull request Feb 1, 2016
Added multi-app support in DatabaseAdapter.js
@gfosco gfosco merged commit 5dc66fd into parse-community:master Feb 1, 2016
@ucoker
Copy link

ucoker commented Apr 30, 2016

It's worked.
My code:

  • index.js
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();

var api = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/dev', // Connection string for your MongoDB database
  cloud: '/home/parse/parse-server/cloud/main.js', // Absolute path to your Cloud Code
  appId: 'bb',
  masterKey: 'your-master-key', // Keep this key secret!
  serverURL: 'http://localhost:1337/parse' // Don't forget to change to https if needed
});
app.use('/parse/bb', api);

var api2 = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/dev2', // Connection string for your MongoDB database
  cloud: '/home/parse/parse-server/cloud/main.js', // Absolute path to your Cloud Code
  appId: 'cc',
  masterKey: 'your-master-key', // Keep this key secret!
  serverURL: 'http://localhost:1337/parse' // Don't forget to change to https if needed
});
app.use('/parse/cc', api2);

app.listen(1337, function() {
  console.log('parse-server-example running on port 1337.');
});

@flovilmart
Copy link
Contributor

It Wont Work With cloud code....

@ralphilius
Copy link

@flovilmart Would it work if I use pm2 to set up multiple instances in which each one has separate cloud code url?

@ucoker
Copy link

ucoker commented May 7, 2016

@ralphilius You can try this.

├── parse-server
│   ├── apps
│   │   ├── app1
│   │   │   └── cloud
│   │   │       └── main.js
│   │   └── app2
│   │       └── cloud
│   │           └── main.js
│   ├── ecosystem.json
│   ├── index.js
│   ├── logs
│   ├── node_modules [259 entries exceeds filelimit, not opening dir]
│   ├── package.json
│   └── parse-dashboard-config.json
  • index.js
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var moment = require('moment');

var app = express();

var api_app1 = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/dbApp1', // Connection string for your MongoDB database
  cloud: '/home/parse/parse-server/apps/app1/cloud/main.js', // Absolute path to your Cloud Code
  appId: 'app1',
  masterKey: 'the_masterKey', // Keep this key secret!
  serverURL: 'http://localhost:1337/parse' // Don't forget to change to https if needed
});
app.use('/parse/app1', api_app1);

var api_app2 = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/dbApp2', // Connection string for your MongoDB database
  cloud: '/home/parse/parse-server/apps/app2/cloud/main.js', // Absolute path to your Cloud Code
  appId: 'app2',
  masterKey: 'the_masterKey', // Keep this key secret!
  serverURL: 'http://localhost:1337/parse' // Don't forget to change to https if needed
});
app.use('/parse/app2', api_app2);

app.listen(1337, function() {
  var dateNow = moment(new Date()).format("YYYY-MMM-D HH:mm --> ");
  console.log(dateNow + 'parse-server running on port 1337.');
});
  • parse-dashboard-config.json
{
    "apps": [{
        "serverURL": "https://youdomain.com/parse/app1",
        "appId": "app1",
        "masterKey": "the_masterKey",
        "appName": "app1"
    },{
        "serverURL": "https://youdomain.com/parse/app2",
        "appId": "app2",
        "masterKey": "the_masterKey",
        "appName": "app2"
    }],

    "users": [{
        "user":"user1",
        "pass":"pass"
    },{
        "user":"user2",
        "pass":"pass"
    }]
}
  • ecosystem.json
{
    "apps" : [{
        "name"        : "parse-apps",
        "script"      : "/usr/bin/node",
        "watch"       : true,
        "merge_logs"  : true,
        "cwd"         : "/home/parse/parse-server",
        "args"        : "index.js"
    },{
    "name"        : "parse-dashboard",
    "script"      : "/usr/bin/parse-dashboard",
    "watch"       : true,
    "merge_logs"  : true,
    "cwd"         : "/home/parse/parse-server",
    "args"         : "--config parse-dashboard-config.json --mountPath /dashboard"
    }]
}

then, pm2 start ecosystem.json
Hope to help you.

@ralphilius
Copy link

Thanks for verifying @ucoker
This is similar to what I use. I am using 2 pm2 instances with different env variables and nginx as http proxy

@flovilmart
Copy link
Contributor

Your clous code won't work

@n00r
Copy link

n00r commented Jul 28, 2016

how to create dynamically ......like back4app.

@chderen
Copy link
Contributor

chderen commented Jul 28, 2016

you can run with parse-server-example or other methods multi apps instance.
by running the new ParseServer() function with the correct params.
i don't know about cloud code, but other functionality works grate.

there is one problem, creating more then 10 apps will brake the run time, because at ParseServer.js there is a listening to event process.on('uncaughtException', ...) which can run only ten times.
my solution was to create edit the source at that point.

the difference between the apps is by the http request credentials.

good lock!

@wujohns
Copy link

wujohns commented Jan 6, 2017

When you create multi app instance in this way,The param global.Parse.Cloud will be overwritten util all instances created.I agree @flovilmart

@wujohns
Copy link

wujohns commented Jan 6, 2017

The feature for multi app instance is uncompleted in parse-server 2.3.1

@walxlgf
Copy link

walxlgf commented May 19, 2020

LiveQuery does not work it this way. Any body can solve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants