{"action":"created","issue":{"url":"https://api.github.com/repos/mongoosastic/mongoosastic/issues/57","labels_url":"https://api.github.com/repos/mongoosastic/mongoosastic/issues/57/labels{/name}","comments_url":"https://api.github.com/repos/mongoosastic/mongoosastic/issues/57/comments","events_url":"https://api.github.com/repos/mongoosastic/mongoosastic/issues/57/events","html_url":"https://github.com/mongoosastic/mongoosastic/issues/57","id":63806316,"number":57,"title":"Singleton or dependency injection pattern","user":{"login":"tsturzl","id":623753,"avatar_url":"https://avatars.githubusercontent.com/u/623753?v=3","gravatar_id":"","url":"https://api.github.com/users/tsturzl","html_url":"https://github.com/tsturzl","followers_url":"https://api.github.com/users/tsturzl/followers","following_url":"https://api.github.com/users/tsturzl/following{/other_user}","gists_url":"https://api.github.com/users/tsturzl/gists{/gist_id}","starred_url":"https://api.github.com/users/tsturzl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tsturzl/subscriptions","organizations_url":"https://api.github.com/users/tsturzl/orgs","repos_url":"https://api.github.com/users/tsturzl/repos","events_url":"https://api.github.com/users/tsturzl/events{/privacy}","received_events_url":"https://api.github.com/users/tsturzl/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/mongoosastic/mongoosastic/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/mongoosastic/mongoosastic/labels/help%20wanted","name":"help wanted","color":"159818"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-03-23T19:06:10Z","updated_at":"2015-03-24T16:20:58Z","closed_at":null,"body":"Why don't you use a singleton or dependency injection pattern? For each schema I'm creating a new elasticsearch client. Though elasticsearch isn't a persistent connection I find it odd that each time I use this plugin on a schema I'm creating an entirely new elasticsearch client.\r\n\r\nI basically have to load my config file, grab the connection info out, and pass it as an option for each schema. Why not just have `mongoosastic.connect(\"host\", port)`, call it once and reuse that same elasticsearch client. You've gone to the extent of using the _nop_ module, yet you initiate a new client for each schema. It's not terribly inefficient, but its definitely not convenient. As I typically only have a single elasticsearch cluster for an entire project, but I might have multiple types/indices.\r\n\r\nThis approach would be similar to how mongoose does it themselves. Is there any reason why this hasn't been done? You also don't allow passing multiple hosts. Is there any reason for this?"},"comment":{"url":"https://api.github.com/repos/mongoosastic/mongoosastic/issues/comments/85587561","html_url":"https://github.com/mongoosastic/mongoosastic/issues/57#issuecomment-85587561","issue_url":"https://api.github.com/repos/mongoosastic/mongoosastic/issues/57","id":85587561,"user":{"login":"tsturzl","id":623753,"avatar_url":"https://avatars.githubusercontent.com/u/623753?v=3","gravatar_id":"","url":"https://api.github.com/users/tsturzl","html_url":"https://github.com/tsturzl","followers_url":"https://api.github.com/users/tsturzl/followers","following_url":"https://api.github.com/users/tsturzl/following{/other_user}","gists_url":"https://api.github.com/users/tsturzl/gists{/gist_id}","starred_url":"https://api.github.com/users/tsturzl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tsturzl/subscriptions","organizations_url":"https://api.github.com/users/tsturzl/orgs","repos_url":"https://api.github.com/users/tsturzl/repos","events_url":"https://api.github.com/users/tsturzl/events{/privacy}","received_events_url":"https://api.github.com/users/tsturzl/received_events","type":"User","site_admin":false},"created_at":"2015-03-24T16:20:58Z","updated_at":"2015-03-24T16:20:58Z","body":"Right, I just wanted to inquire before sending a PR. I have the project forked. I'm hoping to have it done by next week. The only problem is this won't be backwards compatible. As singleton require prototype object, and mongoosastic currently uses closure objects. So I can't provide an instanced object. What I'm doing is wrapping the Mongoosastic object in a prototype object, then passing a `new` singleton object to `module.exports` this singleton has methods: `.connect(host/s)`, `.plugin(schema,options)`. However, you can `var mongoosastic = require('mongoosastic').plugin` and you can pass connection settings as is currently done.\r\n\r\nIs this going to be a problem? Because this will break the current pattern, though you can use the work around stated above.\r\n\r\nSo new:\r\n\r\n```\r\nvar mongoose     = require('mongoose')\r\n  , mongoosastic = require('mongoosastic')\r\n  , Schema       = mongoose.Schema\r\n\r\nmongoosastic.connect('host:9200');\r\n\r\nvar User = new Schema({\r\n    name: String\r\n  , email: String\r\n  , city: String\r\n})\r\n\r\nUser.plugin(mongoosastic.plugin);\r\n```\r\n\r\nOld:\r\n\r\n```\r\nvar mongoose     = require('mongoose')\r\n  , mongoosastic = require('mongoosastic')\r\n  , Schema       = mongoose.Schema\r\n\r\nvar User = new Schema({\r\n    name: String\r\n  , email: String\r\n  , city: String\r\n})\r\n\r\nUser.plugin(mongoosastic, {host: 'host:9200'});\r\n```\r\n\r\nWork around:\r\n\r\n```\r\nvar mongoose     = require('mongoose')\r\n  , mongoosastic = require('mongoosastic').plugin\r\n  , Schema       = mongoose.Schema\r\n\r\nvar User = new Schema({\r\n    name: String\r\n  , email: String\r\n  , city: String\r\n})\r\n\r\nUser.plugin(mongoosastic);\r\n```"}}