{"action":"created","issue":{"url":"https://api.github.com/repos/vesper-framework/vesper/issues/6","repository_url":"https://api.github.com/repos/vesper-framework/vesper","labels_url":"https://api.github.com/repos/vesper-framework/vesper/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/vesper-framework/vesper/issues/6/comments","events_url":"https://api.github.com/repos/vesper-framework/vesper/issues/6/events","html_url":"https://github.com/vesper-framework/vesper/issues/6","id":312921807,"number":6,"title":"Custom connection options","user":{"login":"josephktcheung","id":4270232,"avatar_url":"https://avatars2.githubusercontent.com/u/4270232?v=4","gravatar_id":"","url":"https://api.github.com/users/josephktcheung","html_url":"https://github.com/josephktcheung","followers_url":"https://api.github.com/users/josephktcheung/followers","following_url":"https://api.github.com/users/josephktcheung/following{/other_user}","gists_url":"https://api.github.com/users/josephktcheung/gists{/gist_id}","starred_url":"https://api.github.com/users/josephktcheung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephktcheung/subscriptions","organizations_url":"https://api.github.com/users/josephktcheung/orgs","repos_url":"https://api.github.com/users/josephktcheung/repos","events_url":"https://api.github.com/users/josephktcheung/events{/privacy}","received_events_url":"https://api.github.com/users/josephktcheung/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-04-10T13:26:57Z","updated_at":"2018-04-11T02:12:55Z","closed_at":null,"author_association":"NONE","body":"Hi,\r\n\r\nThis is a nice framework, thank you for creating this!\r\n\r\nI have 2 questions on customising the TypeORM connection options:\r\n1. Can we have multiple database connections in 1 project? Currently it only connects to 1 database.\r\n2. Would we be able to connect TypeORM by ourselves instead of being handled by the framework?\r\n\r\nBest,\r\nJoseph"},"comment":{"url":"https://api.github.com/repos/vesper-framework/vesper/issues/comments/380304034","html_url":"https://github.com/vesper-framework/vesper/issues/6#issuecomment-380304034","issue_url":"https://api.github.com/repos/vesper-framework/vesper/issues/6","id":380304034,"user":{"login":"josephktcheung","id":4270232,"avatar_url":"https://avatars2.githubusercontent.com/u/4270232?v=4","gravatar_id":"","url":"https://api.github.com/users/josephktcheung","html_url":"https://github.com/josephktcheung","followers_url":"https://api.github.com/users/josephktcheung/followers","following_url":"https://api.github.com/users/josephktcheung/following{/other_user}","gists_url":"https://api.github.com/users/josephktcheung/gists{/gist_id}","starred_url":"https://api.github.com/users/josephktcheung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephktcheung/subscriptions","organizations_url":"https://api.github.com/users/josephktcheung/orgs","repos_url":"https://api.github.com/users/josephktcheung/repos","events_url":"https://api.github.com/users/josephktcheung/events{/privacy}","received_events_url":"https://api.github.com/users/josephktcheung/received_events","type":"User","site_admin":false},"created_at":"2018-04-11T02:12:55Z","updated_at":"2018-04-11T02:12:55Z","author_association":"NONE","body":"Hi @parano, thank you for replying!\r\n\r\nActually putting multiple connections in ormconfig doesn't work. Here's my `ormconfig.js`:\r\n\r\n```\r\nmodule.exports = [{\r\n  \"type\": \"postgres\",\r\n  \"host\": process.env.DB_HOST,\r\n  \"port\": process.env.DB_PORT,\r\n  \"username\": process.env.DB_USERNAME,\r\n  \"password\": process.env.DB_PASSWORD,\r\n  \"database\": process.env.DB_NAME,\r\n  \"schema\": process.env.DB_SCHEMA,\r\n  \"cli\": {\r\n    \"migrationsDir\": \"src/migration\"\r\n  }\r\n},\r\n{\r\n  \"name\": \"db2\",\r\n  \"type\": \"postgres\",\r\n  \"host\": \"localhost\",\r\n  \"port\": 1201,\r\n  \"username\": \"test\",\r\n  \"password\": \"test\",\r\n  \"database\": \"test\",\r\n  \"schema\": \"test\",\r\n  \"cli\": {\r\n    \"migrationsDir\": \"src/migration\"\r\n  }\r\n}];\r\n```\r\n\r\nAnd this is how I initialise vesper in `index.ts`:\r\n```ts\r\nimport { bootstrap } from 'vesper';\r\nimport { getConnection } from 'typeorm';\r\n\r\nbootstrap({\r\n  port: 3000,\r\n  schemas: [`${__dirname}/schema/**/*.graphql`],\r\n}).then(() => {\r\n  const defaultConnection = getConnection();\r\n  console.log('default', defaultConnection);\r\n\r\n  const db2Connection = getConnection('db2');\r\n  console.log('db2', db2Connection);\r\n});\r\n```\r\nWhen you run `index.ts`, it can successfully log the `defaultConnection` but throws `ConnectionNotFoundError: Connection \"db2\" was not found.` when `getConnection('db2')` is run.\r\n\r\nIt's because again internally, vesper runs TypeORM's `createConnection` which only create a single connection in [`SchemaBuilder`](https://github.com/vesper-framework/vesper/blob/15c94f542eb371749436e6c9360ca1b401c79dd5/src/SchemaBuilder.ts#L191). If we want to connect to multiple databases, we need to use TypeORM's [`createConnections`](http://typeorm.io/#/multiple-connections) instead. Also from TypeORM, if `createConnection` is called without any parameter, [only one connection from ormconfig will be created (name \"default\" or connection without name).](https://github.com/typeorm/typeorm/blob/9be7e40e29cbef2024445c9132fb665faf696fde/src/index.ts#L175-L179)\r\n\r\nSo that's why I ask if we can create connections by ourselves because currently vesper is putting constraints on the way database(s) is connected. My pull request is just a hot fix for point 2, ideally we should not [hardcode the connection to always get the `default` one](https://github.com/vesper-framework/vesper/blob/15c94f542eb371749436e6c9360ca1b401c79dd5/src/SchemaBuilder.ts#L169-L171). What if I don't like the name `default` and want to rename it to something more meaningful internally?\r\n\r\nI'm not sure how much changes will be needed for vesper to fully support multiple connections to multiple databases, but I believe it's needed if vesper wants to appeal to a broader audience.\r\n\r\nThanks,\r\nJoseph"}}
{"actor":{"display_login":"josephktcheung"}}