项目作者: oracle

项目描述 :
Node.js SDK for Oracle NoSQL Database
高级语言: JavaScript
项目地址: git://github.com/oracle/nosql-node-sdk.git
创建时间: 2020-02-05T19:46:09Z
项目社区:https://github.com/oracle/nosql-node-sdk

开源协议:Other

下载


Node.js for Oracle NoSQL Database

Overview

This is version 5.5 of the Node.js SDK for
the Oracle NoSQL Database.
The SDK provides interfaces, documentation, and examples to develop Node.js
applications that use the
Oracle NoSQL Database Cloud Service
and the On-Premise Oracle NoSQL Database. You can use the SDK to access Oracle NoSQL Database
in JavaScript or TypeScript.

Prerequisites

Installation

You may install this SDK either as a dependency of your project:

  1. npm install oracle-nosqldb --save

or globally:

  1. sudo npm install -g oracle-nosqldb

Documentation

See the API and user guide documentation.

Help

When requesting help please be sure to include as much detail as possible,
including version of the SDK and simple, standalone example code as needed.

Quickstart

For detailed information and API documentation about using the SDK in different
environments see the documentation

The following is a quick start tutorial to run a simple program in the supported
environments. The same template source code is used for all environments. The
first step is to cut the program below and paste it into an editor for minor
modifications. The instructions assume that is stored as quickstart.js, but you
can use any name you like. The quickstart example supports 3 environments:

  1. Oracle NoSQL Database Cloud Service
  2. Oracle NoSQL Cloud Simulator
  3. Oracle NoSQL Database on-premise, using the proxy server

See running quickstart for instructions on how to edit and
run the quickstart program in different environments.

  1. /*
  2. * A simple example that
  3. * - creates a table
  4. * - inserts a row using the put() operation
  5. * - reads a row using the get() operation
  6. * - drops the table
  7. *
  8. * To run:
  9. * 1. Edit for your target environment and credentials
  10. * 2. Run it:
  11. * node quickstart.js cloud|cloudsim|kvstore
  12. *
  13. * Use 'cloud' for the Oracle NoSQL Database Cloud Service
  14. * Use 'cloudsim' for the Oracle NoSQL Cloud Simulator
  15. * Use 'kvstore' for the Oracle NoSQL Database on-premise
  16. */
  17. 'use strict';
  18. const NoSQLClient = require('oracle-nosqldb').NoSQLClient;
  19. const Region = require('oracle-nosqldb').Region;
  20. const ServiceType = require('oracle-nosqldb').ServiceType;
  21. // Target table used by this example
  22. const TABLE_NAME = 'NodeQuickstart';
  23. const USAGE = 'Usage: node quickstart.js cloud|cloudsim|kvstore';
  24. async function quickstart() {
  25. let client;
  26. try {
  27. const args = process.argv;
  28. let serviceType = args[2];
  29. if (!serviceType) {
  30. return console.error(USAGE);
  31. }
  32. // Set up access to the cloud service
  33. client = createClient(serviceType);
  34. console.log('Created NoSQLClient instance');
  35. await run(client);
  36. console.log('Success!');
  37. } catch (err) {
  38. console.error(' Error: ' + err.message);
  39. console.error(' from: ');
  40. console.error(err.operation.api.name);
  41. } finally {
  42. if (client) {
  43. client.close();
  44. }
  45. }
  46. }
  47. /*
  48. * This function encapsulates environmental differences and returns a
  49. * client handle to use for data operations.
  50. */
  51. function createClient(serviceType) {
  52. switch(serviceType) {
  53. case 'cloud':
  54. return new NoSQLClient({
  55. /*
  56. * EDIT:
  57. * 1. use desired region id
  58. * 2. your tenancy's OCID, user's OCID
  59. * 3. privateKeyFile path
  60. * 4. fingerprint for uploaded public key
  61. * 5. optional passphrase. If your key has none, delete this
  62. * line (and the leading ',').
  63. */
  64. region: Region.<your-region-here>,
  65. auth: {
  66. iam: {
  67. tenantId: 'your tenancy OCID',
  68. userId: 'your user OCID',
  69. fingerprint: 'your public key fingerprint',
  70. privateKeyFile: 'path to private key file',
  71. passphrase: 'pass phrase if set for your private key'
  72. }
  73. }
  74. });
  75. case 'cloudsim':
  76. /*
  77. * EDIT: if the endpoint does not reflect how the Cloud
  78. * Simulator has been started, modify it accordingly.
  79. */
  80. return new NoSQLClient({
  81. serviceType: ServiceType.CLOUDSIM,
  82. endpoint: 'localhost:8080'
  83. });
  84. case 'kvstore':
  85. /*
  86. * EDIT: if the endpoint does not reflect how the Proxy
  87. * Server has been started, modify it accordingly.
  88. */
  89. return new NoSQLClient({
  90. serviceType: ServiceType.KVSTORE,
  91. endpoint: 'localhost:80'
  92. });
  93. default:
  94. throw new Error('Unknown service type: ' + serviceType);
  95. }
  96. }
  97. /*
  98. * Create a table, read and write a record
  99. */
  100. async function run(client) {
  101. const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} \
  102. (cookie_id LONG, audience_data JSON, PRIMARY KEY(cookie_id))`;
  103. console.log('Create table ' + TABLE_NAME);
  104. let res = await client.tableDDL(createDDL, {
  105. tableLimits: {
  106. readUnits: 50,
  107. writeUnits: 50,
  108. storageGB: 25
  109. }
  110. });
  111. console.log(' Creating table %s', res.tableName);
  112. console.log(' Table state: %s', res.tableState.name);
  113. // Wait for the operation completion
  114. await client.forCompletion(res);
  115. console.log(' Table %s is created', res.tableName);
  116. console.log(' Table state: %s', res.tableState.name);
  117. // Write a record
  118. console.log('\nWrite a record');
  119. res = await client.put(TABLE_NAME, {
  120. cookie_id: 456,
  121. audience_data: {
  122. ipaddr: '10.0.00.yyy',
  123. audience_segment: {
  124. sports_lover: '2019-01-05',
  125. foodie: '2018-12-31'
  126. }
  127. }
  128. });
  129. if (res.consumedCapacity) {
  130. console.log(' Write used: %O', res.consumedCapacity);
  131. }
  132. // Read a record
  133. console.log('\nRead a record');
  134. res = await client.get(TABLE_NAME, { cookie_id: 456 });
  135. console.log(' Got record: %O', res.row);
  136. if (res.consumedCapacity) {
  137. console.log(' Read used: %O', res.consumedCapacity);
  138. }
  139. // Drop the table
  140. console.log('\nDrop table');
  141. const dropDDL = `DROP TABLE ${TABLE_NAME}`;
  142. res = await client.tableDDL(dropDDL);
  143. console.log(' Dropping table %s', res.tableName);
  144. // Wait for the table to be removed
  145. await client.forCompletion(res);
  146. console.log(' Operation completed');
  147. console.log(' Table state is %s', res.tableState.name);
  148. }
  149. quickstart();

Running Quickstart" class="reference-link"> Running Quickstart

Run Against the Oracle NoSQL Database Cloud Service

Running against the Cloud Service requires an Oracle Cloud account. See
Configuring for the Cloud Service for information on getting an account and acquiring required credentials.

  1. Collect the following information:
  • Tenancy ID
  • User ID
  • API signing key (private key file in PEM format)
  • Fingerprint for the public key uploaded to the user’s account
  • Private key pass phrase, needed only if the private key is encrypted
  1. Edit quickstart.js and add your information in the ‘cloud’ section of the
    createClient() function.

  2. Decide the region you want to use and add that in the same section in the
    value for the region key.

  3. Run the program:

  1. node quickstart.js cloud

If you would prefer to create a configuration file for credentials instead of
modifying the program put credentials in a file (see
Using a Configuration File). Then modify quickstart.js to use the file:

Replace

  1. iam: {
  2. tenantId: 'your tenancy OCID',
  3. userId: 'your user OCID',
  4. fingerprint: 'your public key fingerprint',
  5. privateKeyFile: 'path to private key file',
  6. passphrase: 'pass phrase if set for your private key'
  7. }

with

  1. iam: {
  2. configFile: 'path-to-config-file',
  3. profileName: 'DEFAULT'
  4. }

Run Against the Oracle NoSQL Cloud Simulator

Running against the Oracle NoSQL Cloud Simulator requires a running Cloud
Simulator instance. See Using the Cloud Simulator for information on how to download
and start the Cloud Simulator.

  1. Start the Cloud Simulator based on instructions above. Note the HTTP port
    used. By default it is 8080 on localhost.

  2. The quickstart.js program defaults to localhost:8080 so if the Cloud
    Simulator was started using default values no editing is required.

  3. Run the program:

  1. node quickstart.js cloudsim

Run Against Oracle NoSQL on-premise

Running against the Oracle NoSQL Database on-premise requires a running
Oracle NoSQL Database instance as well as a running NoSQL Proxy server instance.
The program will connect to the proxy server.

See Connecting to an On-Premise Oracle NoSQL Database for information on how to download
and start the database instance and proxy server. The database and proxy should
be started without security enabled for this quickstart program to operate
correctly. A secure configuration requires a secure proxy and more complex
configuration.

  1. Start the Oracle NoSQL Database and proxy server based on instructions above.
    Note the HTTP port used. By default the endpoint is localhost:80.

  2. The quickstart.js program defaults to localhost:80. If the proxy was started
    using a different host or port edit the settings accordingly.

  3. Run the program:

  1. node quickstart.js kvstore

Examples

The examples are located in the examples directory. There are two sets of
examples: JavaScript in examples/javascript directory and TypeScript in
examples/typescript directory.

You can run the examples:

  • Against the Oracle NoSQL Database Cloud Service using your Oracle Cloud
    account and credentials.
  • Locally using the
    Oracle NoSQL Database Cloud Simulator.
  • Against the On-Premise Oracle NoSQL Database via the proxy.

examples/config directory contains template JSON configuration files used to
run the examples:

  • cloud_template.json is used to access a cloud service instance and
    allows you to customize configuration. See
    Supply Credentials to the Application. Unused properties must be removed from the template.
  • cloudsim.json is used if you are running against the cloud simulator.
    You may use this file directly as config file if you are running the cloud
    simulator on localhost on port 8080. If the cloud simulator has been started on
    a different host or port, change the endpoint.
  • kvstore_template.json is used to access on-premise NoSQL Database via
    the proxy. Copy that file and fill in appropriate values as described in
    Configuring the SDK.
    If configuring for a not secure store the auth section should be removed.

Alternatively, when using cloud service with default configuration as
described in
Configuring the SDK, you may run examples without providing JSON
configuration file. This assumes that your credentials and your region
identifier are present in an OCI config file ~/.oci/config.

JavaScript Examples

JavaScript examples are in examples/javascript directory. You can copy
all files in this directory to a separate directory. The SDK package
oracle-nosqldb is the only dependency for these examples. You may install
it via package.json in the same directory (alternatively, you may install
the SDK globally). To run an example:

  1. npm install
  2. node <example.js> [optinal_config_file.json]

E.g.

  1. npm install
  2. $ node basic_example.js config.json

TypeScript Examples

TypeScript examples are in examples/typescript directory. There are 4
examples: table_ops.ts, single_row_ops.ts, multi_row_ops.ts and
query_ops.ts. They also share some common functionality (see setup.ts and
common.ts). package.json in the same directory contains scripts to build
and run the examples. You may copy all files in this directory to a separate
directory.

Use npm to install the dependencies, then you can run each example as
follows:

  1. npm install
  2. npx tsx <example.ts> [optional_config_file.json]

E.g.

  1. npm install
  2. npx tsx single_row_ops.ts config.json

The commands above use tsx which is
installed as one of the dependencies.

Alternatively, you can build the examples into JavaScript. Then
run the resulting .js files, which are created in the dist directory, e.g.:

  1. npm run build
  2. node dist/single_row_ops.js config.json

See package.json for more details.

License

Please see the LICENSE file included in the top-level directory of the
package for a copy of the license and additional information.

The THIRD_PARTY_LICENSES file contains third
party notices and licenses.

Contributing

See CONTRIBUTING for details.

Security

See SECURITY for details.