|
| 1 | +# loopback-connector-db2 |
| 2 | + |
| 3 | +[IBM® DB2®](http://www.ibm.com/analytics/us/en/technology/db2/) is the database of choice for robust, enterprise-wide solutions handling high-volume workloads. |
| 4 | +It is optimized to deliver industry-leading performance while lowering costs. The `loopback-connector-db2` module is the LoopBack connector for DB2. |
| 5 | + |
| 6 | +The LoopBack DB2 connector supports: |
| 7 | + |
| 8 | +- All [create, retrieve, update, and delete operations](http://loopback.io/doc/en/lb2/Creating-updating-and-deleting-data.html). |
| 9 | +- [Queries](http://loopback.io/doc/en/lb2/Querying-data.html) with fields, limit, order, skip and where filters. |
| 10 | +- All supported DB2 LUW versions. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Enter the following in the top-level directory of your LoopBack application: |
| 15 | + |
| 16 | +``` |
| 17 | +$ npm install loopback-connector-db2 --save |
| 18 | +``` |
| 19 | + |
| 20 | +The `--save` option adds the dependency to the application's `package.json` file. |
| 21 | + |
| 22 | +## Configuration |
| 23 | + |
| 24 | +Use the [data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to add the DB2 data source to your application. |
| 25 | +The entry in the application's `server/datasources.json` will look something like this: |
| 26 | + |
| 27 | +```js |
| 28 | +"mydb": { |
| 29 | + "name": "mydb", |
| 30 | + "connector": "db2" |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +Edit `server/datasources.json` to add other supported properties as required: |
| 35 | + |
| 36 | +```js |
| 37 | +"mydb": { |
| 38 | + "name": "mydb", |
| 39 | + "connector": "db2", |
| 40 | + "username": <username>, |
| 41 | + "password": <password>, |
| 42 | + "database": <database name>, |
| 43 | + "hostname": <db2 server hostname>, |
| 44 | + "port": <port number> |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +The following table describes the connector properties. |
| 49 | + |
| 50 | +Property | Type | Description |
| 51 | +---------------| --------| -------- |
| 52 | +database | String | Database name |
| 53 | +schema | String | Specifies the default schema name that is used to qualify unqualified database objects in dynamically prepared SQL statements. The value of this property sets the value in the CURRENT SCHEMA special register on the database server. The schema name is case-sensitive, and must be specified in uppercase characters |
| 54 | +username | String | DB2 Username |
| 55 | +password | String | DB2 password associated with the username above |
| 56 | +hostname | String | DB2 server hostname or IP address |
| 57 | +port | String | DB2 server TCP port number |
| 58 | +useLimitOffset | Boolean | LIMIT and OFFSET must be configured on the DB2 server before use (compatibility mode) |
| 59 | +supportDashDB | Boolean | Create ROW ORGANIZED tables to support dashDB. |
| 60 | +dsn | String | DSN string; can be used instead of the username, password, database, hostname and port properties |
| 61 | +maxPoolSize | Number | Maximum number of connections in the connection pool |
| 62 | + |
| 63 | + |
| 64 | +Alternatively, you can create and configure the data source in JavaScript code. |
| 65 | +For example: |
| 66 | + |
| 67 | +```js |
| 68 | +var DataSource = require('loopback-datasource-juggler').DataSource; |
| 69 | +var DB2 = require('loopback-connector-db2'); |
| 70 | + |
| 71 | +var config = { |
| 72 | + username: process.env.DB2_USERNAME, |
| 73 | + password: process.env.DB2_PASSWORD, |
| 74 | + hostname: process.env.DB2_HOSTNAME, |
| 75 | + port: 50000, |
| 76 | + database: 'SQLDB', |
| 77 | +}; |
| 78 | + |
| 79 | +var db = new DataSource(DB2, config); |
| 80 | + |
| 81 | +var User = db.define('User', { |
| 82 | + name: { type: String }, |
| 83 | + email: { type: String }, |
| 84 | +}); |
| 85 | + |
| 86 | +db.autoupdate('User', function(err) { |
| 87 | + if (err) { |
| 88 | + console.log(err); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + User.create({ |
| 93 | + name: 'Tony', |
| 94 | + email: 'tony@t.com', |
| 95 | + }, function(err, user) { |
| 96 | + console.log(err, user); |
| 97 | + }); |
| 98 | + |
| 99 | + User.find({ where: { name: 'Tony' }}, function(err, users) { |
| 100 | + console.log(err, users); |
| 101 | + }); |
| 102 | + |
| 103 | + User.destroyAll(function() { |
| 104 | + console.log('example complete'); |
| 105 | + }); |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +## Running tests |
| 110 | + |
| 111 | +### Own instance |
| 112 | + |
| 113 | +If you have a local or remote DB2 instance and would like to use that to run the test suite, use the following command: |
| 114 | +- Linux |
| 115 | +```bash |
| 116 | +DB2_HOSTNAME=<HOST> DB2_PORTNUM=<PORT> DB2_USERNAME=<USER> DB2_PASSWORD=<PASSWORD> DB2_DATABASE=<DATABASE> DB2_SCHEMA=<SCHEMA> CI=true npm test |
| 117 | +``` |
| 118 | +- Windows |
| 119 | +```bash |
| 120 | +SET DB2_HOSTNAME=<HOST> |
| 121 | +SET DB2_PORTNUM=<PORT> |
| 122 | +SET DB2_USERNAME=<USER> |
| 123 | +SET DB2_PASSWORD=<PASSWORD> |
| 124 | +SET DB2_DATABASE=<DATABASE> |
| 125 | +SET DB2_SCHEMA=<SCHEMA> |
| 126 | +SET CI=true |
| 127 | +npm test |
| 128 | +``` |
| 129 | + |
| 130 | +#### How to get a local DB2 instance: |
| 131 | + |
| 132 | +- Go to [IBM DB2 trials](http://www.ibm.com/analytics/us/en/technology/db2/db2-trials.html) page. |
| 133 | +- Register for an account. |
| 134 | +- Download either IBM DB2 or IBM DB2 Express-C. |
| 135 | +- For documentation or more information about the installation or setup, see http://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.kc.doc/welcome.html |
| 136 | + |
| 137 | +#### IBM DB2 Express-C scenario on Windows: |
| 138 | +- Run the setup file. |
| 139 | +- Set user information for the DB2 Administration server. |
| 140 | +- Write down the user information and the password that you create. User name is `db2admin` by default but it could be modified. |
| 141 | +- Configure DB2 instance and write down the port number. It is 50000 by default. |
| 142 | +- Once setup is done, Start the `default DB2 and Database Client Interface Selection Wizard`, and proceed with the configuration. |
| 143 | +- Ensure that the DB2 Data server runtime client is started. The default name is `DB2COPY1`. |
| 144 | +- Let's assume your database name is `sample`, and schema name is `STRONGLOOP`. |
| 145 | +- In Windows, start the DB2 Command window-Administrator (In Mac or Linux, use terminal with proper privileges). |
| 146 | +- Make sure that you are in this path `...\IBM\SQLLIB\BIN` (In mac, it should be ` /Users/<userid>/sqllib\bin`), and type the following commands: |
| 147 | + |
| 148 | +``` |
| 149 | +>set db2instance=server1 |
| 150 | +
|
| 151 | +>db2 connect to sample |
| 152 | +
|
| 153 | +>db2 set schema to STRONGLOOP |
| 154 | +``` |
| 155 | + |
| 156 | +### Docker |
| 157 | +If you do not have a local DB2 instance, you can also run the test suite with very minimal requirements. |
| 158 | +- Assuming you have [Docker](https://docs.docker.com/engine/installation/) installed, run the following script which would spawn a DB2 instance on your local: |
| 159 | +```bash |
| 160 | +source setup.sh <HOST> <PORT> <PASSWORD> <DATABASE> |
| 161 | +``` |
| 162 | +where `<HOST>`, `<PORT>`, `<PASSWORD>` and `<DATABASE>` are optional parameters. By default, the user is `db2inst1`. |
| 163 | +- Run the test: |
| 164 | +```bash |
| 165 | +npm test |
| 166 | +``` |
0 commit comments