|
1 |
| -loopback-connector-postgresql |
2 |
| -============================= |
| 1 | +## loopback-connector-postgresql |
3 | 2 |
|
4 |
| -PostgreSQL Connector for LoopBack |
| 3 | +The PostgreSQL Connector module for for [loopback-datasource-juggler](http://docs.strongloop.com/loopback-datasource-juggler/). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To simplify the installation of [node-postgresql](https://github.com/strongloop/node-postgresql) module and PostgreSQL instant clients, |
| 8 | +we introduce [loopback-postgresql-installer](https://github.com/strongloop/loopback-postgresql-installer) as a dependency which installs |
| 9 | +and configures node-postgresql upon `npm install`. |
| 10 | + |
| 11 | +Please note `config.oracleUrl` is the property to define the base URL to download the corresponding node-postgresql bundle for the local |
| 12 | +environment. |
| 13 | + |
| 14 | +The bundle file name is `loopback-postgresql-<platform>-<arch>-<version>.tar.gz`. The `version` is the same as the `version` in package.json. |
| 15 | + |
| 16 | + "dependencies": { |
| 17 | + "loopback-postgresql-installer": "git+ssh://[email protected]:strongloop/loopback-postgresql-installer.git", |
| 18 | + ... |
| 19 | + }, |
| 20 | + "config": { |
| 21 | + "oracleUrl": "http://7e9918db41dd01dbf98e-ec15952f71452bc0809d79c86f5751b6.r22.cf1.rackcdn.com" |
| 22 | + }, |
| 23 | + |
| 24 | +**The `oracleUrl` can be overridden via LOOPBACK_ORACLE_URL environment variable.** |
| 25 | + |
| 26 | +For MacOSX, the full URL is: |
| 27 | + |
| 28 | +http://7e9918db41dd01dbf98e-ec15952f71452bc0809d79c86f5751b6.r22.cf1.rackcdn.com/loopback-postgresql-MacOSX-x64-0.0.1.tar.gz |
| 29 | + |
| 30 | +`libaio` library is required on Linux systems: |
| 31 | + |
| 32 | +* On Unbuntu/Debian |
| 33 | + |
| 34 | + sudo apt-get install libaio1 |
| 35 | + |
| 36 | +* On Fedora/CentOS/RHEL |
| 37 | + |
| 38 | + sudo yum install libaio |
| 39 | + |
| 40 | + |
| 41 | +**Please make sure c:\instantclient_12_1\vc10 comes before c:\instantclient_12_1** |
| 42 | + |
| 43 | +## Connector settings |
| 44 | + |
| 45 | +The connector can be configured using the following settings from the data source. |
| 46 | + |
| 47 | +* host or hostname (default to 'localhost'): The host name or ip address of the PostgreSQL DB server |
| 48 | +* port (default to 1521): The port number of the PostgreSQL DB server |
| 49 | +* username or user: The user name to connect to the PostgreSQL DB |
| 50 | +* password: The password |
| 51 | +* database (default to 'XE'): The PostgreSQL DB listener name |
| 52 | +* debug (default to false) |
| 53 | + |
| 54 | +## Discovering Models |
| 55 | + |
| 56 | +PostgreSQL data sources allow you to discover model definition information from existing postgresql databases. See the following APIs: |
| 57 | + |
| 58 | + - [dataSource.discoverModelDefinitions([username], fn)](https://github.com/strongloop/loopback#datasourcediscovermodeldefinitionsusername-fn) |
| 59 | + - [dataSource.discoverSchema([owner], name, fn)](https://github.com/strongloop/loopback#datasourcediscoverschemaowner-name-fn) |
| 60 | + |
| 61 | +### Asynchronous APIs for discovery |
| 62 | + |
| 63 | +* PostgreSQL.prototype.discoverModelDefinitions = function (options, cb) |
| 64 | + - options: |
| 65 | + - all: {Boolean} To include tables/views from all schemas/owners |
| 66 | + - owner/schema: {String} The schema/owner name |
| 67 | + - views: {Boolean} To include views |
| 68 | + - cb: |
| 69 | + - Get a list of table/view names, for example: |
| 70 | + |
| 71 | + {type: 'table', name: 'INVENTORY', owner: 'STRONGLOOP' } |
| 72 | + {type: 'table', name: 'LOCATION', owner: 'STRONGLOOP' } |
| 73 | + {type: 'view', name: 'INVENTORY_VIEW', owner: 'STRONGLOOP' } |
| 74 | + |
| 75 | + |
| 76 | +* PostgreSQL.prototype.discoverModelProperties = function (table, options, cb) |
| 77 | + - table: {String} The name of a table or view |
| 78 | + - options: |
| 79 | + - owner/schema: {String} The schema/owner name |
| 80 | + - cb: |
| 81 | + - Get a list of model property definitions, for example: |
| 82 | + |
| 83 | + { owner: 'STRONGLOOP', |
| 84 | + tableName: 'PRODUCT', |
| 85 | + columnName: 'ID', |
| 86 | + dataType: 'VARCHAR2', |
| 87 | + dataLength: 20, |
| 88 | + nullable: 'N', |
| 89 | + type: 'String' } |
| 90 | + { owner: 'STRONGLOOP', |
| 91 | + tableName: 'PRODUCT', |
| 92 | + columnName: 'NAME', |
| 93 | + dataType: 'VARCHAR2', |
| 94 | + dataLength: 64, |
| 95 | + nullable: 'Y', |
| 96 | + type: 'String' } |
| 97 | + |
| 98 | + |
| 99 | +* PostgreSQL.prototype.discoverPrimaryKeys= function(table, options, cb) |
| 100 | + - table: {String} The name of a table or view |
| 101 | + - options: |
| 102 | + - owner/schema: {String} The schema/owner name |
| 103 | + - cb: |
| 104 | + - Get a list of primary key definitions, for example: |
| 105 | + |
| 106 | + { owner: 'STRONGLOOP', |
| 107 | + tableName: 'INVENTORY', |
| 108 | + columnName: 'PRODUCT_ID', |
| 109 | + keySeq: 1, |
| 110 | + pkName: 'ID_PK' } |
| 111 | + { owner: 'STRONGLOOP', |
| 112 | + tableName: 'INVENTORY', |
| 113 | + columnName: 'LOCATION_ID', |
| 114 | + keySeq: 2, |
| 115 | + pkName: 'ID_PK' } |
| 116 | + |
| 117 | +* PostgreSQL.prototype.discoverForeignKeys= function(table, options, cb) |
| 118 | + - table: {String} The name of a table or view |
| 119 | + - options: |
| 120 | + - owner/schema: {String} The schema/owner name |
| 121 | + - cb: |
| 122 | + - Get a list of foreign key definitions, for example: |
| 123 | + |
| 124 | + { fkOwner: 'STRONGLOOP', |
| 125 | + fkName: 'PRODUCT_FK', |
| 126 | + fkTableName: 'INVENTORY', |
| 127 | + fkColumnName: 'PRODUCT_ID', |
| 128 | + keySeq: 1, |
| 129 | + pkOwner: 'STRONGLOOP', |
| 130 | + pkName: 'PRODUCT_PK', |
| 131 | + pkTableName: 'PRODUCT', |
| 132 | + pkColumnName: 'ID' } |
| 133 | + |
| 134 | + |
| 135 | +* PostgreSQL.prototype.discoverExportedForeignKeys= function(table, options, cb) |
| 136 | + |
| 137 | + - table: {String} The name of a table or view |
| 138 | + - options: |
| 139 | + - owner/schema: {String} The schema/owner name |
| 140 | + - cb: |
| 141 | + - Get a list of foreign key definitions that reference the primary key of the given table, for example: |
| 142 | + |
| 143 | + { fkName: 'PRODUCT_FK', |
| 144 | + fkOwner: 'STRONGLOOP', |
| 145 | + fkTableName: 'INVENTORY', |
| 146 | + fkColumnName: 'PRODUCT_ID', |
| 147 | + keySeq: 1, |
| 148 | + pkName: 'PRODUCT_PK', |
| 149 | + pkOwner: 'STRONGLOOP', |
| 150 | + pkTableName: 'PRODUCT', |
| 151 | + pkColumnName: 'ID' } |
| 152 | + |
| 153 | + |
| 154 | +### Synchronous APIs for discovery |
| 155 | + |
| 156 | +* PostgreSQL.prototype.discoverModelDefinitionsSync = function (options) |
| 157 | +* PostgreSQL.prototype.discoverModelPropertiesSync = function (table, options) |
| 158 | +* PostgreSQL.prototype.discoverPrimaryKeysSync= function(table, options) |
| 159 | +* PostgreSQL.prototype.discoverForeignKeysSync= function(table, options) |
| 160 | +* PostgreSQL.prototype.discoverExportedForeignKeysSync= function(table, options) |
| 161 | + |
| 162 | +### Discover/build/try the models |
| 163 | + |
| 164 | +The following example uses `discoverAndBuildModels` to discover, build and try the models: |
| 165 | + |
| 166 | + dataSource.discoverAndBuildModels('INVENTORY', { owner: 'STRONGLOOP', visited: {}, associations: true}, |
| 167 | + function (err, models) { |
| 168 | + // Show records from the models |
| 169 | + for(var m in models) { |
| 170 | + models[m].all(show); |
| 171 | + }; |
| 172 | + |
| 173 | + // Find one record for inventory |
| 174 | + models.Inventory.findOne({}, function(err, inv) { |
| 175 | + console.log("\nInventory: ", inv); |
| 176 | + // Follow the foreign key to navigate to the product |
| 177 | + inv.product(function(err, prod) { |
| 178 | + console.log("\nProduct: ", prod); |
| 179 | + console.log("\n ------------- "); |
| 180 | + }); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | +## Model definition for PostgreSQL |
| 185 | + |
| 186 | +The model definition consists of the following properties: |
| 187 | + |
| 188 | +* name: Name of the model, by default, it's the camel case of the table |
| 189 | +* options: Model level operations and mapping to PostgreSQL schema/table |
| 190 | +* properties: Property definitions, including mapping to PostgreSQL column |
| 191 | + |
| 192 | + { |
| 193 | + "name":"Inventory", |
| 194 | + "options":{ |
| 195 | + "idInjection":false, |
| 196 | + "postgresql":{ |
| 197 | + "schema":"STRONGLOOP", |
| 198 | + "table":"INVENTORY" |
| 199 | + } |
| 200 | + }, |
| 201 | + "properties":{ |
| 202 | + "productId":{ |
| 203 | + "type":"String", |
| 204 | + "required":true, |
| 205 | + "length":20, |
| 206 | + "id":1, |
| 207 | + "postgresql":{ |
| 208 | + "columnName":"PRODUCT_ID", |
| 209 | + "dataType":"VARCHAR2", |
| 210 | + "dataLength":20, |
| 211 | + "nullable":"N" |
| 212 | + } |
| 213 | + }, |
| 214 | + "locationId":{ |
| 215 | + "type":"String", |
| 216 | + "required":true, |
| 217 | + "length":20, |
| 218 | + "id":2, |
| 219 | + "postgresql":{ |
| 220 | + "columnName":"LOCATION_ID", |
| 221 | + "dataType":"VARCHAR2", |
| 222 | + "dataLength":20, |
| 223 | + "nullable":"N" |
| 224 | + } |
| 225 | + }, |
| 226 | + "available":{ |
| 227 | + "type":"Number", |
| 228 | + "required":false, |
| 229 | + "length":22, |
| 230 | + "postgresql":{ |
| 231 | + "columnName":"AVAILABLE", |
| 232 | + "dataType":"NUMBER", |
| 233 | + "dataLength":22, |
| 234 | + "nullable":"Y" |
| 235 | + } |
| 236 | + }, |
| 237 | + "total":{ |
| 238 | + "type":"Number", |
| 239 | + "required":false, |
| 240 | + "length":22, |
| 241 | + "postgresql":{ |
| 242 | + "columnName":"TOTAL", |
| 243 | + "dataType":"NUMBER", |
| 244 | + "dataLength":22, |
| 245 | + "nullable":"Y" |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + |
| 252 | +## Type Mapping |
| 253 | + |
| 254 | + - Number |
| 255 | + - Boolean |
| 256 | + - String |
| 257 | + - null |
| 258 | + - Object |
| 259 | + - undefined |
| 260 | + - Date |
| 261 | + - Array |
| 262 | + - Buffer |
| 263 | + |
| 264 | +### JSON to PostgreSQL Types |
| 265 | + |
| 266 | +* String|JSON|Text|default: VARCHAR2, default length is 1024 |
| 267 | +* Number: NUMBER |
| 268 | +* Date: DATE |
| 269 | +* Timestamp: TIMESTAMP(3) |
| 270 | +* Boolean: CHAR(1) |
| 271 | + |
| 272 | +### PostgreSQL Types to JSON |
| 273 | + |
| 274 | +* CHAR(1): Boolean |
| 275 | +* CHAR(n), VARCHAR, VARCHAR2, LONG VARCHAR, NCHAR, NVARCHAR2: String |
| 276 | +* LONG, BLOB, CLOB, NCLOB: Buffer |
| 277 | +* NUMBER, INTEGER, DECIMAL, DOUBLE, FLOAT, BIGINT, SMALLINT, REAL, NUMERIC, BINARY_FLOAT, BINARY_DOUBLE, UROWID, ROWID: Number |
| 278 | +* DATE, TIMESTAMP: Date |
| 279 | +* default: String |
| 280 | + |
| 281 | +## Destroying Models |
| 282 | + |
| 283 | +Destroying models may result in errors due to foreign key integrity. Make sure to delete any related models first before calling delete on model's with relationships. |
| 284 | + |
| 285 | +## Auto Migrate / Auto Update |
| 286 | + |
| 287 | +After making changes to your model properties you must call `Model.automigrate()` or `Model.autoupdate()`. Only call `Model.automigrate()` on new models |
| 288 | +as it will drop existing tables. |
| 289 | + |
| 290 | +LoopBack PostgreSQL connector creates the following schema objects for a given model: |
| 291 | + |
| 292 | +* A table, for example, PRODUCT |
| 293 | +* A sequence for the primary key, for example, PRODUCT_ID_SEQUENCE |
| 294 | +* A trigger to generate the primary key from the sequnce, for example, PRODUCT_ID_TRIGGER |
| 295 | + |
| 296 | + |
| 297 | +## Running examples |
| 298 | + |
| 299 | +* example/app.js: Demonstrate the asynchronous discovery |
| 300 | +* example/app-sync.js: Demonstrate the synchronous discovery |
| 301 | + |
| 302 | +## Running tests |
| 303 | + |
| 304 | + npm test |
0 commit comments