ericholiveira / studio-cluster

Auto cluster for Studio framework
23 stars 4 forks source link

Coupling database connection with services #13

Closed sean-hill closed 8 years ago

sean-hill commented 8 years ago

Hey @ericholiveira!

I'm taking a shot at using studio-cluster in my project. Let's say I have this project structure

Where api server and studio services are two seperate processes. The Studio services will be calling my Database to insert / update information. An example being:

// User Services

'use strict'

const Studio = require('studio')
const Mongoose = require('mongoose')
const UserModel = Mongoose.model('User')
const Services = Studio.services()

class User {
  * create (userData) {
    let newUser = yield new UserModel(userData).save(Studio.defer())
    return newUser
  }
}

Studio.serviceClass(User)

Currently I connect to my Database in the api server project, however when I do that, the Studio services don't have access to that connection or database models. I'm thinking about connecting to the Database in the studio services project and coupling the connection with the services. That way if the api server ever needs access to the Database, it'll have to call a service.

I'd use studio-cluster to share the services between the two processes.

What do you think about this approach?

sean-hill commented 8 years ago

An annoyance I see with this setup is sharing configuration data between the two projects, such as authentication secrets and database connection strings.

ericholiveira commented 8 years ago

I strongly advise you to take the approach you described. Only connect to database using studio services and avoid giving direct access to mongoose models (or any other framework you might be using to connect to db) to your api server. First to keep the responsabilities of each layer sane, and also because Studio lets you add plugins/middlewares to have a better control of whats going on in each call. I always use Studio-timer (Studio.plugin.timer) in my projects sending data to StatsD,for this reason i keep all business code on my services, you can also add timeouts or any other functionality provided by the plugins (or create your own).

ericholiveira commented 8 years ago

Regarding the duplication... you can use different approachs: 1- If your project is not really big, you can keep all code in the same project and just create 2 different entrypoints. 2- Use enviroment variables 3- Creating another repository for your configs and add it as a dependency for the other 2 4- Avoid at all cost configuration on api (if possible) and keep it as just a thin layer between http calls and your services

sean-hill commented 8 years ago

@ericholiveira that has been very helpful. Thank you! Studio.js FTW!

ericholiveira commented 8 years ago

:) I'm closing it for now... if anything goes wrong, just let me know

sean-hill commented 8 years ago

MINDBLOWN!!!

It just worked flawlessly. Two separate processes, with the Services working in harmony. Absolutely amazing.

ericholiveira commented 8 years ago

Thx... try restart process it still works :)