{"_id":"@albanm/jdbc","_rev":"1-c05429316261f302afd7438f47758ed2","name":"@albanm/jdbc","dist-tags":{"latest":"0.6.5"},"versions":{"0.6.5":{"name":"@albanm/jdbc","version":"0.6.5","description":"Node Module JDBC wrapper","main":"index.js","dependencies":{"async":"~2.6.0","java":"^0.10.0","lodash":"~4.17","uuid":"^3.1.0","winston":"^2.4.0"},"devDependencies":{"jshint":"~2.9","lolex":"^2.3.1","nodeunit":"~0.11","q":"^1.5.1"},"scripts":{"pretest":"bash bin/startdbs","test":"nodeunit test","posttest":"bash bin/stopdbs","lint":"jshint lib/**.js test/**.js"},"repository":{"type":"git","url":"git://github.com/CraZySacX/node-jdbc.git"},"directories":{"doc":".","lib":"lib"},"keywords":["node","jdbc","wrapper"],"engines":{"node":">=0.10"},"author":{"name":"Jason Ozias","email":"jason.g.ozias@gmail.com"},"license":"MIT","gitHead":"ab80d695138f727d453b65fa5a1a60e8a0ca9752","bugs":{"url":"https://github.com/CraZySacX/node-jdbc/issues"},"homepage":"https://github.com/CraZySacX/node-jdbc#readme","_id":"@albanm/jdbc@0.6.5","_npmVersion":"6.4.1","_nodeVersion":"11.1.0","_npmUser":{"name":"albanm","email":"alban.mouton@gmail.com"},"dist":{"integrity":"sha512-fX47cQUeMi0wzZUVG/mO/w6tM+5vRBXGriWpoF+GrWw1Kh0zYudqc1QYQEcyFFkdex+tTDH5gstJm+QsQgyTjg==","shasum":"ffbd3eed6237f16aa7d3a1aa62fa5b2ba03ddd1d","tarball":"https://registry.npmjs.org/@albanm/jdbc/-/jdbc-0.6.5.tgz","fileCount":17,"unpackedSize":259456,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcRgYgCRA9TVsSAnZWagAApMgP/3NR/qA6fd25RfaxqeTK\n3cBJ0JCJrO0FXPLo9khSCCTLYOpW/LC8jd85F/HZXVzP/oTWHzctkCeK47Xp\nxYTzetj+bWlIKJyqp0GVTvDyVOxuf92WCHkd3Rz+ii4OJTQJMkBtfhkV/vml\nZNdmOTu9g/SfWey7QLATDFQ71uXZh8YrQnTGHa+jUbwkR8ezzOdkWR/GX6QP\nw41lB++Kxn/sFiC7lJcdTuf2DydS2HM8+csf2UI7NLym7/yKj6OtWB1pMj/t\nhfB1/ai3Y/Xx9/Zcdjfvpuk7RoMT7Dz64C89rw/biWtFTZaXIvcTJoAZsnnR\nuxsRWquZoWOgctTp/euFCNvPGUjArFzZl1xspiw9TgGrv1ksdl0eBoqQcafi\nJIr2uUAfGskQ5EN5LVGp0oOBaFrD+Ze7RNVN/xV9pKD6mbQj6Ocp4Qov9/GJ\n9Cx+goqiUS95lgFi3JrP7sFBgYHfy92oN6FMhcqymAXb0/R7lIumWV69q1Qj\nlxe/Ult68ggpg+izyy8Fc1B/s1SWeYQQmZPZe4n1YM+Pv4OZdu0tAsbtXNWD\nP4ZRVXQKcTQEVtt3AGacv6FVCyJK2ownEhqkz9bNgBZziu7V2q/VJH2XS55Y\nQLoAOHhZ7mQb0emvyh+HCVpYCeKxtA9BIXq5g+M7QoYnyeJx0W0pSkpIESGA\n3L7y\r\n=kRGp\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCt+9/K6MIfn0RIEmOBNb/rTNcp1EP4A6MuwHSpj9ACjQIgQWv3vd9fa6sQ62GB3D+1Fz2A0y1M4yyM8yzCv8RdKHI="}]},"maintainers":[{"name":"albanm","email":"alban.mouton@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jdbc_0.6.5_1548092960157_0.05346035192795706"},"_hasShrinkwrap":false}},"time":{"created":"2019-01-21T17:49:19.969Z","0.6.5":"2019-01-21T17:49:20.265Z","modified":"2022-04-04T12:34:23.434Z"},"maintainers":[{"name":"albanm","email":"alban.mouton@gmail.com"}],"description":"Node Module JDBC wrapper","homepage":"https://github.com/CraZySacX/node-jdbc#readme","keywords":["node","jdbc","wrapper"],"repository":{"type":"git","url":"git://github.com/CraZySacX/node-jdbc.git"},"author":{"name":"Jason Ozias","email":"jason.g.ozias@gmail.com"},"bugs":{"url":"https://github.com/CraZySacX/node-jdbc/issues"},"license":"MIT","readme":"# node-jdbc\nJDBC API Wrapper for node.js\n\n## Latest Version\n- **0.6.2**\n\n## Installation\n- Release: ```npm i --save jdbc```\n- Development: ```npm i --save jdbc@next``` (this will install code from the master branch).\n\nPlease visit [node-jdbc](https://www.npmjs.org/package/jdbc) for information on installing with npm.\n\n## Status\n[![Build Status](https://travis-ci.org/CraZySacX/node-jdbc.svg?branch=master)](https://travis-ci.org/CraZySacX/node-jdbc)\n\n## Supported Java Versions\n- 1.7\n- 1.8\n\n[node-java](https://github.com/joeferner/node-java) has experiemental support for 1.8, and if you are brave you can\ncompile it with such.  All the tests work out of the box on a 1.8 JVM, but I've only wrapped 1.7 functions.\n\nNote that Java 9 is not currently supported.  When node-java supports Java 9, I will look into implementing any\nnew Java 9 API changes (if any).\n\n## Major API Refactor\n\n- **One Instance to Rule Them All (JVM)**\n\n[node-java](https://github.com/joeferner/node-java) spins up one JVM instance only.  Due to this fact, any JVM options\nand classpath setup have to happen before the first java call.  I've created a\nsmall wrapper (jinst.js) to help out with this.  See below for example\nusage.  I usually add this to every file that may be an entry point.  The\n[unit tests](https://github.com/CraZySacX/node-jdbc/tree/master/test)\nare setup like this due to the fact that order can't be guaranteed.\n\n```javascript\nvar jinst = require('jdbc/lib/jinst');\n\n// isJvmCreated will be true after the first java call.  When this happens, the\n// options and classpath cannot be adjusted.\nif (!jinst.isJvmCreated()) {\n  // Add all java options required by your project here.  You get one chance to\n  // setup the options before the first java call.\n  jinst.addOption(\"-Xrs\");\n  // Add all jar files required by your project here.  You get one chance to\n  // setup the classpath before the first java call.\n  jinst.setupClasspath(['./drivers/hsqldb.jar',\n                        './drivers/derby.jar',\n                        './drivers/derbyclient.jar',\n                        './drivers/derbytools.jar']);\n}\n```\n\n\n- **Connection Pooling**\n\nEveryone gets a pool now.  By default with no extra configuration, the pool\nis created with one connection that can be reserved/released.  Currently, the\npool is configured with two options: `minpoolsize` and `maxpoolsize`.  If\n`minpoolsize` is set, when the pool is initialized, `minpoolsize` connections\nwill be created.  If `maxpoolsize` is set (the default value is `minpoolsize`),\nand you try and reserve a connection and there aren't any available, the pool\nwill be grown.  This can happen until `maxpoolsize` connections have been\nreserved.  The pool should be initialized after configuration is set with the\n`initialize()` function.  JDBC connections can then be acquired with the\n`reserve()` function and returned to the pool with the `release()` function.\nBelow is the unit test for the pool that demonstrates this behavior.\n\n```javascript\nvar _ = require('lodash');\nvar nodeunit = require('nodeunit');\nvar jinst = require('../lib/jinst');\nvar Pool = require('../lib/pool');\n\nif (!jinst.isJvmCreated()) {\n  jinst.addOption(\"-Xrs\");\n  jinst.setupClasspath(['./drivers/hsqldb.jar',\n                        './drivers/derby.jar',\n                        './drivers/derbyclient.jar',\n                        './drivers/derbytools.jar']);\n}\n\nvar config = {\n  url: 'jdbc:hsqldb:hsql://localhost/xdb',\n  user : 'SA',\n  password: '',\n  minpoolsize: 2,\n  maxpoolsize: 3\n};\n\nvar testpool = null;\nvar testconn = null;\n\nmodule.exports = {\n  setUp: function(callback) {\n    if (testpool === null) {\n      testpool = new Pool(config);\n    }\n    callback();\n  },\n  testinitialize: function(test) {\n    // Initialize the pool (create minpoolsize connections)\n    testpool.initialize(function(err) {\n      test.expect(1);\n      test.equal(null, err);\n      test.done();\n    });\n  },\n  testreserve: function(test) {\n    // Reserve a connection.\n    testpool.reserve(function(err, conn) {\n      test.expect(4);\n      test.equal(null, err);\n      test.ok(conn && typeof conn == 'object');\n      test.equal(testpool._pool.length, 1);\n      test.equal(testpool._reserved.length, 1);\n      testconn = conn;\n      test.done();\n    });\n  },\n  testrelease: function(test) {\n    // Release a connection.\n    testpool.release(testconn, function(err, conn) {\n      test.expect(3);\n      test.equal(null, err);\n      test.equal(testpool._pool.length, 2);\n      test.equal(testpool._reserved.length, 0);\n      testconn = null;\n      test.done();\n    });\n  },\n  testreserverelease: function(test) {\n    // Reserve then release a connection.\n    testpool.reserve(function(err, conn) {\n      if (err) {\n        console.log(err);\n      } else {\n        testpool.release(conn, function(err) {\n          test.expect(3);\n          test.equal(null, err);\n          test.equal(testpool._pool.length, 2);\n          test.equal(testpool._reserved.length, 0);\n          test.done();\n        });\n      }\n    });\n  },\n  testreservepastmin: function(test) {\n    // Reserve connections past minpoolsize.  This will grow the pool.\n    var conns = [];\n    for(i = 0; i < 3; i++) {\n      testpool.reserve(function(err, conn) {\n        conns.push(conn);\n        if (i == 3) {\n          test.expect(2);\n          test.equal(testpool._pool.length, 0);\n          test.equal(testpool._reserved.length, 3);\n          _.each(conns, function(conn) {\n            testpool.release(conn, function(err) {});\n          });\n          test.done();\n        }\n      });\n    }\n  },\n  testovermax: function(test) {\n    // Reserve connections past maxpoolsize.  This will max out the pool, and\n    // throw an error when the last reserve request is made.\n    var conns = [];\n    for(i = 0; i < 4; i++) {\n      testpool.reserve(function(err, conn) {\n        if (err) {\n          if (i == 3) {\n            test.expect(3);\n            test.ok(err);\n            test.equal(testpool._reserved.length, 3);\n            test.equal(testpool._pool.length, 0);\n            _.each(conns, function(conn) {\n              testpool.release(conn, function(err) {});\n            });\n            test.done();\n          } else {\n            console.log(err);\n          }\n        } else {\n          conns.push(conn);\n        }\n      });\n    }\n  }\n};\n```\n\n\n- **Fully Wrapped Connection API**\n\nThe Java Connection API has almost been completely wrapped.  See\n[connection.js](https://github.com/CraZySacX/node-jdbc/blob/master/lib/connection.js)\nfor a full list of functions.\n\n```javascript\nconn.setAutoCommit(false, function(err) {\n  if (err) {\n    callback(err);\n  } else {\n    callback(null);\n  }\n});\n```\n\n\n- **ResultSet processing separated from statement execution**\n\nResultSet processing has been separated from statement execution to allow for\nmore flexibility.  The ResultSet returned from executing a select query can\nstill be processed into an object array using the `toObjArray()` function on the\nresultset object.\n\n```javascript\n// Select statement example.\nconn.createStatement(function(err, statement) {\n  if (err) {\n    callback(err);\n  } else {\n    statement.executeQuery(\"SELECT * FROM blah;\", function(err, resultset) {\n      if (err) {\n        callback(err)\n      } else {\n        // Convert the result set to an object array.\n        resultset.toObjArray(function(err, results) {\n          if (results.length > 0) {\n            console.log(\"ID: \" + results[0].ID);\n          }\n          callback(null, resultset);\n        });\n      }\n    });\n  }\n});\n```\n\n\n- **Automatically Closing Idle Connections**\n\nIf you pass a **maxidle** property in the config for a new connection pool,\n`pool.reserve()` will close stale connections, and will return a sufficiently fresh connection, or a new connection.  **maxidle** can be number representing the maximum number of milliseconds since a connection was last used, that a connection is still considered alive (without making an extra call to the database to check that the connection is valid).  If **maxidle** is a falsy value or is absent from the config, this feature does not come into effect.  This feature is useful, when connections are automatically closed from the server side after a certain period of time, and when it is not appropriate to use the connection keepalive feature.\n\n## Usage\nSome mininal examples are given below.  I've also created a\n[node-example-jdbc](https://github.com/CraZySacX/node-jdbc-example) project with more thorough examples.\n\n### Initialize\n```javascript\nvar JDBC = require('jdbc');\nvar jinst = require('jdbc/lib/jinst');\n\nif (!jinst.isJvmCreated()) {\n  jinst.addOption(\"-Xrs\");\n  jinst.setupClasspath(['./drivers/hsqldb.jar',\n                        './drivers/derby.jar',\n                        './drivers/derbyclient.jar',\n                        './drivers/derbytools.jar']);\n}\n\nvar config = {\n  // Required\n  url: 'jdbc:hsqldb:hsql://localhost/xdb',\n\n  // Optional\n  drivername: 'my.jdbc.DriverName',\n  minpoolsize: 10,\n  maxpoolsize: 100,\n\n  // Note that if you sepecify the user and password as below, they get\n  // converted to properties and submitted to getConnection that way.  That\n  // means that if your driver doesn't support the 'user' and 'password'\n  // properties this will not work.  You will have to supply the appropriate\n  // values in the properties object instead.\n  user: 'SA',\n  password: '',\n  properties: {}\n};\n\n// or user/password in url\n// var config = {\n//   // Required\n//   url: 'jdbc:hsqldb:hsql://localhost/xdb;user=SA;password=',\n//\n//   // Optional\n//   drivername: 'my.jdbc.DriverName',\n//   minpoolsize: 10\n//   maxpoolsize: 100,\n//   properties: {}\n// };\n\n// or user/password in properties\n// var config = {\n//   // Required\n//   url: 'jdbc:hsqldb:hsql://localhost/xdb',\n//\n//   // Optional\n//   drivername: 'my.jdbc.DriverName',\n//   minpoolsize: 10,\n//   maxpoolsize: 100,\n//   properties: {\n//     user: 'SA',\n//     password: ''\n//     // Other driver supported properties can be added here as well.\n//   }\n// };\n\nvar hsqldb = new JDBC(config);\n\nhsqldb.initialize(function(err) {\n  if (err) {\n    console.log(err);\n  }\n});\n```\n\n### Reserve Connection, Execute Queries, Release Connection\n```javascript\n// This assumes initialization as above.\n// For series execution.\nvar asyncjs = require('async');\n\nhsqldb.reserve(function(err, connObj) {\n  // The connection returned from the pool is an object with two fields\n  // {uuid: <uuid>, conn: <Connection>}\n  if (connObj) {\n    console.log(\"Using connection: \" + connObj.uuid);\n    // Grab the Connection for use.\n    var conn = connObj.conn;\n\n    // Adjust some connection options.  See connection.js for a full set of\n    // supported methods.\n    asyncjs.series([\n      function(callback) {\n        conn.setAutoCommit(false, function(err) {\n          if (err) {\n            callback(err);\n          } else {\n            callback(null);\n          }\n        });\n      },\n      function(callback) {\n        conn.setSchema(\"test\", function(err) {\n          if (err) {\n            callback(err);\n          } else {\n            callback(null);\n          }\n        });\n      }\n    ], function(err, results) {\n      // Check for errors if need be.\n      // results is an array.\n    });\n\n    // Query the database.\n    asyncjs.series([\n      function(callback) {\n        // CREATE SQL.\n        conn.createStatement(function(err, statement) {\n          if (err) {\n            callback(err);\n          } else {\n            statement.executeUpdate(\"CREATE TABLE blah \"\n                                  + \"(id int, name varchar(10), date DATE, \"\n                                  + \" time TIME, timestamp TIMESTAMP);\",\n                                  function(err, count) {\n              if (err) {\n                callback(err);\n              } else {\n                callback(null, count);\n              }\n            });\n          }\n        });\n      },\n      function(callback) {\n        conn.createStatement(function(err, statement) {\n          if (err) {\n            callback(err);\n          } else {\n            statement.executeUpdate(\"INSERT INTO blah \"\n                                  + \"VALUES (1, 'Jason', CURRENT_DATE, \"\n                                  + \"CURRENT_TIME, CURRENT_TIMESTAMP);\",\n                                  function(err, count) {\n              if (err) {\n                callback(err);\n              } else {\n                callback(null, count);\n              }\n            });\n          }\n        });\n      },\n      function(callback) {\n        // Update statement.\n        conn.createStatement(function(err, statement) {\n          if (err) {\n            callback(err);\n          } else {\n            statement.executeUpdate(\"UPDATE blah \"\n                                  + \"SET id = 2 \"\n                                  + \"WHERE name = 'Jason';\",\n                                  function(err, count) {\n              if (err) {\n                callback(err);\n              } else {\n                callback(null, count);\n              }\n            });\n          }\n        });\n      },\n      function(callback) {\n        // Select statement example.\n        conn.createStatement(function(err, statement) {\n          if (err) {\n            callback(err);\n          } else {\n            // Adjust some statement options before use.  See statement.js for\n            // a full listing of supported options.\n            statement.setFetchSize(100, function(err) {\n              if (err) {\n                callback(err);\n              } else {\n                statement.executeQuery(\"SELECT * FROM blah;\",\n                                       function(err, resultset) {\n                  if (err) {\n                    callback(err)\n                  } else {\n                    resultset.toObjArray(function(err, results) {\n                      if (results.length > 0) {\n                        console.log(\"ID: \" + results[0].ID);\n                      }\n                      callback(null, resultset);\n                    });\n                  }\n                });\n              }\n            });\n          }\n        });\n      },\n      function(callback) {\n        conn.createStatement(function(err, statement) {\n          if (err) {\n            callback(err);\n          } else {\n            statement.executeUpdate(\"DELETE FROM blah \"\n                                  + \"WHERE id = 2;\", function(err, count) {\n              if (err) {\n                callback(err);\n              } else {\n                callback(null, count);\n              }\n            });\n          }\n        });\n      },\n      function(callback) {\n        conn.createStatement(function(err, statement) {\n          if (err) {\n            callback(err);\n          } else {\n            statement.executeUpdate(\"DROP TABLE blah;\", function(err, count) {\n              if (err) {\n                callback(err);\n              } else {\n                callback(null, count);\n              }\n            });\n          }\n        });\n      }\n    ], function(err, results) {\n      // Results can also be processed here.\n      // Release the connection back to the pool.\n      hsqldb.release(connObj, function(err) {\n        if (err) {\n          console.log(err.message);\n        }\n      });\n    });\n  }\n});\n```\n","readmeFilename":"README.md"}