The Oracle connector enables LoopBack applications to connect to Oracle data sources.

See also:

Tip: Use the Oracle installer command, lb oracle,to easily install and troubleshoot installing loopback-oracle-installerand the Oracle data source connector.

Note: This page was generated from the loopback-connector-oracle README.

loopback-connector-oracle

Oracle is an object-relational database management system produced by Oracle Corporation. The loopback-connector-oracle module is the Oracle connector for the LoopBack framework based on the node-oracledb module.

For more information, see the LoopBack documentation).

Prerequisites

Node.js: The Oracle connector requires Node.js version 6.x and up.

Windows: On 32-bit Windows systems, you must use the 32-bit version of Node.js. On 64-bit Windows systems, you must use the 64-bit version of Node.js. For more information, see Node-oracledb Installation on Windows.

Oracle: The Oracle connector requires Oracle client libraries 11.2+ and can connect to Oracle Database Server 9.2+.

Installation

Before installing this module, please follow instructions at https://oracle.github.io/node-oracledb/INSTALL.htmlto make sure all the prerequisites are satisfied.

In your application root directory, enter this command to install the connector:

  1. $ npm install loopback-connector-oracle --save

If you create a Oracle data source using the data source generator as described below, you don’t have to do this, since the generator will run npm install for you.

The libaio library is required on Linux systems:

On Ubuntu/Debian, get it with this command:

  1. sudo apt-get install libaio1

On Fedora/CentOS/RHEL, get it with this command:

  1. sudo yum install libaio

Creating an Oracle data source

Use the Data source generator to add a Oracle data source to your application.The generator will prompt for the database server hostname, port, and other settingsrequired to connect to a Oracle database. It will also run the npm install command above for you.

The entry in the application’s /server/datasources.json will look like this:

/server/datasources.json

  1. "mydb": {
  2. "name": "mydb",
  3. "connector": "oracle",
  4. "tns": "demo",
  5. "host": "myserver",
  6. "port": 3306,
  7. "database": "mydb",
  8. "password": "mypassword",
  9. "user": "admin"
  10. }

Edit datasources.json to add any other additional properties that you require.

Connector properties

The connector properties depend on naming methods you use for the Oracle database.LoopBack supports three naming methods:

  • Easy connect: host/port/database.
  • Local naming (TNS): alias to a full connection string that can specify all the attributes that Oracle supports.
  • Directory naming (LDAP): directory for looking up the full connection string that can specify all the attributes that Oracle supports.

Easy Connect

Easy Connect is the simplest form that provides out-of-the-box TCP/IP connectivity to databases.The data source then has the following settings.

PropertyTypeDefaultDescription
host or hostnameStringlocalhostHost name or IP address of the Oracle database server
portNumber1521Port number of the Oracle database server
username or userString User name to connect to the Oracle database server
passwordString Password to connect to the Oracle database server
databaseStringXEOracle database listener name

For example:

/server/datasources.json

  1. {
  2. "demoDB": {
  3. "connector": "oracle",
  4. "host": "oracle-demo.strongloop.com",
  5. "port": 1521,
  6. "database": "XE",
  7. "username": "demo",
  8. "password": "L00pBack"
  9. }
  10. }

Local and directory naming

Both local and directory naming require that you place configuration files in a TNS admin directory, such as /oracle/admin.

sqlnet.ora

This specifies the supported naming methods; for example:

  1. NAMES.DIRECTORY_PATH=(LDAP,TNSNAMES,EZCONNECT)

nsnames.ora

This maps aliases to connection stringsl for example:

  1. demo1=(DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST=demo.strongloop.com)(PORT=1521)))

ldap.ora

This configures the LDAP server.

  1. DIRECTORY_SERVERS=(localhost:1389)
  2. DEFAULT_ADMIN_CONTEXT="dc=strongloop,dc=com"
  3. DIRECTORY_SERVER_TYPE=OID

Set up TNS_ADMIN environment variable

For the Oracle connector to pick up the configurations, you must set the environment variable ‘TNS_ADMIN’ to the directory containing the .ora files.

  1. export TNS_ADMIN=<directory containing .ora files>

Now you can use either the TNS alias or LDAP service name to configure a data source:

  1. var ds = loopback.createDataSource({
  2. "tns": "demo", // The tns property can be a tns name or LDAP service name
  3. "username": "demo",
  4. "password": "L00pBack"
  5. });

Connection pooling options

Property nameDescriptionDefault value
minConnMinimum number of connections in the connection pool1
maxConnMaxmimum number of connections in the connection pool10
incrConnIncremental number of connections for the connection pool.1
timeout Time-out period in seconds for a connection in the connection pool. The Oracle connector will terminate connections in this connection pool that are idle longer than the time-out period. 10

For example,

/server/datasources.json

  1. {
  2. "demoDB": {
  3. "connector": "oracle",
  4. "minConn":1,
  5. "maxConn":5,
  6. "incrConn":1,
  7. "timeout": 10,
  8. ...
  9. }
  10. }

Connection troubleshooting

If you encounter this error:

  1. Error: ORA-24408: could not generate unique server group name

Then the Oracle 11g client requires an entry with your hostname pointing to127.0.0.1.

To resolve:

Get your hostname. Check your hostname by running this command (for example, if your machine’s name is “earth”):

  1. $ hostname
  2. earth

Update /etc/hosts and map 127.0.0.1 to your hostname “earth”:

  1. ...
  2. 127.0.0.1 localhost earth
  3. ...

Verify the fix. Run the example in examples/app.js:

  1. $ node examples/app.js

For more information, see StackOverflow question.

Model properties

An Oracle model definition consists of the following properties:

  • name: Name of the model, by default, it’s the camel case of the table.
  • options: Model-level operations and mapping to Oracle schema/table.
  • properties: Property definitions, including mapping to Oracle column.

/common/models/model.json

  1. {
  2. "name":"Inventory",
  3. "options":{
  4. "idInjection":false,
  5. "oracle":{
  6. "schema":"STRONGLOOP",
  7. "table":"INVENTORY"
  8. }
  9. },
  10. "properties":{
  11. "productId":{
  12. "type":"String",
  13. "required":true,
  14. "length":20,
  15. "id":1,
  16. "oracle":{
  17. "columnName":"PRODUCT_ID",
  18. "dataType":"VARCHAR2",
  19. "dataLength":20,
  20. "nullable":"N"
  21. }
  22. },
  23. "locationId":{
  24. "type":"String",
  25. "required":true,
  26. "length":20,
  27. "id":2,
  28. "oracle":{
  29. "columnName":"LOCATION_ID",
  30. "dataType":"VARCHAR2",
  31. "dataLength":20,
  32. "nullable":"N"
  33. }
  34. },
  35. "available":{
  36. "type":"Number",
  37. "required":false,
  38. "length":22,
  39. "oracle":{
  40. "columnName":"AVAILABLE",
  41. "dataType":"NUMBER",
  42. "dataLength":22,
  43. "nullable":"Y"
  44. }
  45. },
  46. "total":{
  47. "type":"Number",
  48. "required":false,
  49. "length":22,
  50. "oracle":{
  51. "columnName":"TOTAL",
  52. "dataType":"NUMBER",
  53. "dataLength":22,
  54. "nullable":"Y"
  55. }
  56. }
  57. }
  58. }

Type mapping

See LoopBack types for details on LoopBack’s data types.

JSON to Oracle Types

LoopBack TypeOracle Type
StringJSONTextdefaultVARCHAR2 Default length is 1024
NumberNUMBER
DateDATE
TimestampTIMESTAMP(3)
BooleanCHAR(1)

Oracle Types to JSON

Oracle TypeLoopBack Type
CHAR(1)Boolean
CHAR(n)VARCHARVARCHAR2,LONG VARCHARNCHARNVARCHAR2String
LONG, BLOB, CLOB, NCLOBNode.js Buffer object
NUMBERINTEGERDECIMALDOUBLEFLOATBIGINTSMALLINTREALNUMERICBINARY_FLOATBINARY_DOUBLEUROWIDROWIDNumber
DATETIMESTAMPDate

Discovery and auto-migration

Model discovery

The Oracle connector supports model discovery that enables you to create LoopBack modelsbased on an existing database schema using the unified database discovery API. For more information on discovery, see Discovering models from relational databases.

For an example of model discover, see example/app.js.

Auto-migratiion

The Oracle connector also supports auto-migration that enables you to create a database schemafrom LoopBack models using the LoopBack automigrate method.

For more information on auto-migration, see Creating a database schema from models for more information.

LoopBack Oracle connector creates the following schema objects for a given model:

  • A table, for example, PRODUCT
  • A sequence for the primary key, for example, PRODUCT_ID_SEQUENCE
  • A trigger to generate the primary key from the sequnce, for example, PRODUCT_ID_TRIGGERDestroying models may result in errors due to foreign key integrity. First delete any related models by calling delete on models with relationships.

Running tests

Own instance

If you have a local or remote Oracle instance and would like to use that to run the test suite, use the following command:

  • Linux
  1. ORACLE_HOST=<HOST> ORACLE_PORT=<PORT> ORACLE_USER=<USER> ORACLE_PASSWORD=<PASSWORD> ORACLE_DATABASE=<DATABASE> npm test
  • Windows
  1. SET ORACLE_HOST=<HOST>
  2. SET ORACLE_PORT=<PORT>
  3. SET ORACLE_USER=<USER>
  4. SET ORACLE_PASSWORD=<PASSWORD>
  5. SET ORACLE_DATABASE=<DATABASE>
  6. npm test

Docker

If you do not have a local Oracle instance, you can also run the test suite with very minimal requirements.

  • Assuming you have Docker installed, run the following script which would spawn an Oracle instance on your local machine:
  1. source setup.sh <HOST> <PORT>

where <HOST>, <PORT>, <USER>, and PASSWORD are optional parameters. The default values are localhost, 1521, admin, and 0raclep4ss respectively. The DATABASE setting is always XE.

  • Run the test:
  1. npm test

Tags: connectors