cemremengu / fastify-oracle

Attaches an oracledb connection pool to a Fastify server instance.
Other
13 stars 11 forks source link

Added support for multiple oracle pools #5

Closed cemremengu closed 6 years ago

cemremengu commented 6 years ago

This PR adds support for multiple pool definitions.

Changes

fastify.register(require("fastify-oracle"), {
pool: {
  user: "oracle",
  password: "oracle",
  connectString: "oracle.local:1521/mydb"
},
name: 'mydb'
})

async function (request, reply) {
 // Or this.oracle.mydb.getConnection()
  const conn = await this.oracle.getConnection()
  try {
    const result = await conn.execute("SELECT 1 FROM DUAL",{ }, { outFormat:this.oracle.db.OBJECT })
    return result.rows
  } finally {
    await conn.close()
  }
}
fastify.register(require("fastify-oracle"), {
pool: {
  user: "oracle",
  password: "oracle",
  connectString: "oracle.local:1521/mydb"
},
name: 'mydb'
}).register(require("fastify-oracle"), {
pool: {
  user: "oracle",
  password: "oracle",
  connectString: "oracle.local:1521/mydb"
},
name: 'otherdb'
})

async function (request, reply) {
  const myConn = await this.oracle.mydb.getConnection()
  const otherConn= await this.oracle.otherdb.getConnection()

  try {
    const myResult = await myConn.execute("SELECT 1 FROM DUAL",{ },{ outFormat: this.oracle.db.OBJECT })
    const otherResult = await otherConn.execute("SELECT 1 FROM DUAL",{ },{ outFormat: this.oracle.db.OBJECT})

    return {myResult, otherResult}
  } finally {
    await myConn .close()
    await otherConn.close()
  }
}
cemremengu commented 6 years ago

This PR ~no longer introduces a breaking change. It~ preserves and extends the previous API by exposing getConnection() method and pool object as individual properties. However, if you are using pool directly in your code, you will need to do oracle.pool

cemremengu commented 6 years ago

After spamming you gazillion of times (hopefully you unfollowed this PR), I think I am done 😄

jsumners commented 6 years ago

I just haven't had time to review it again. I'll get to it.