A brief tutorial on using the LoopBack Oracle connector.

See also:

Note: This page was generated from the loopback-example-database README.

loopback-example-database

A tutorial for basic database related features.

Overview

Topics covered

  • Data sources
    • Creating
    • Configuring
  • Models
    • Creating
  • Automigration
  • Instance introspection (Discovery)

Database specific tutorials

Database specific tutorials are on separate branches. The master branch containsthe tutorial for MongoDB.

BranchConnector
masterMongoDB
mssqlMicrosoft SQL Server
mysqlMySQL
oracleOracle
postgresqlPostgreSQL

For example, to view the MySQL example:

  1. git clone https://github.com/strongloop/loopback-example-database
  2. cd loopback-example-database
  3. git checkout mysql

Prerequisites

Before starting this tutorial, make sure you have the following installed:

Running the example

  1. git clone https://github.com/strongloop/loopback-example-database
  2. cd loopback-example-database
  3. npm install
  4. npm start

Tutorial - MongoDB

1. Create a new LoopBack app

App info

  • Name: loopback-example-database
  • Dir to contain the project: loopback-example-database
  1. lb app loopback-example-database
  2. _-----_
  3. | | ╭──────────────────────────╮
  4. |--(o)--| Let's create a LoopBack │
  5. `---------´ │ application! │
  6. ( _´U`_ ) ╰──────────────────────────╯
  7. /___A___\ /
  8. | ~ |
  9. __'.___.'__
  10. ´ ` |° ´ Y `
  11. ? What's the name of your application? loopback-example-database
  12. ? Enter name of the directory to contain the project: loopback-example-database
  13. info change the working directory to loopback-example-database
  14. ? Which version of LoopBack would you like to use? 3.x (current)
  15. ? What kind of application do you have in mind? empty-server (An empty LoopBack API, without any c
  16. onfigured models or datasources)

2. Install the LoopBack MongoDB connector

  1. cd loopback-example-database
  2. npm install --save loopback-connector-mongodb

3. Create a data source

Data source info

  • Data source name: accountDS
  • Select the connector for accountDS: MongoDB
  1. lb datasource accountDS
  2. ... # follow the prompts

This creates a new data source named accountDS that uses the MongoDBconnector.

4. Configure the data source

For the purposes of this example, we will use a preconfigured StrongLoop MongoDBserver. Edit server/datasources.json to set the MongoDB configs:

  1. {
  2. ...
  3. "accountDS": {
  4. "name": "accountDS",
  5. "connector": "mongodb",
  6. "host": "demo.strongloop.com",
  7. "port": 27017,
  8. "database": "demo",
  9. "username": "demo",
  10. "password": "L00pBack"
  11. }
  12. }

Feel free to use your own local MongoDB instance. Simply change the configsabove to match your own.

5. Create a new model

  1. lb model
  2. ... # follow the prompts

Model Info

  • Model name: Account
  • Attach Account to: accountDS (mongodb)
  • Base class: PersistedModel
  • Expose via REST: Yes
  • Custom plural form: Leave blank
  • Properties:
    • email
      • String
      • Not required
    • createdAt
      • Date
      • Not required
    • lastModifiedAt
      • Date
      • Not required
  1. slc loopback:model Account
  2. ... # follow the prompts

6. Create the collection with sample data - Automigration

With the account model configured, we can generate the correspondingMongoDB collection using the info from the Account metadata in common/models/account.jsonvia auto-migration.

Start by creating a dir to store general-purpose scripts:

  1. mkdir bin

Inside that dir, create a script named automigrate.js.To create the Account collection and create two sample accounts, run:

  1. node bin/automigrate.js

WARNING

The automigrate function creates a new collection if it doesn’t exist. Ifthe collection already exists, it will be destroyed and it’s data will bedeleted. If you want to keep this data, use autoupdate instead.

You should see:

  1. Created: { email: 'baz@qux.com',
  2. createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  3. lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  4. id: 562986213ea33440575c6588 }
  5. Created: { email: 'foo@bar.com',
  6. createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  7. lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  8. id: 562986213ea33440575c6587 }

If you are using Node 4, it is safe to ignore Swagger: skipping unknown type"ObjectId". This warning will be addressed in a future update.

7. View data using the explorer

Projects scaffolded via slc loopback come with loopback-component-explorerpreconfigured. From the project root, start the server:

  1. node .

Then to view the existing account data, browse to localhost:3000/explorer andclick:

  • GET /Accounts
  • Try it out!You should see:
  1. [
  2. {
  3. "email": "foo@bar.com",
  4. "createdAt": "2015-10-23T00:58:09.280Z",
  5. "lastModifiedAt": "2015-10-23T00:58:09.280Z",
  6. "id": "562986213ea33440575c6587"
  7. },
  8. {
  9. "email": "baz@qux.com",
  10. "createdAt": "2015-10-23T00:58:09.280Z",
  11. "lastModifiedAt": "2015-10-23T00:58:09.280Z",
  12. "id": "562986213ea33440575c6588"
  13. }
  14. ]

Try out some of the other endpoints to get a feel for how explorer works.

8. Add a script to perform instance instrospection (Discovery)

Discoveryis the process of reverse engineering a LoopBack model from an existing database schema.

The LoopBack MongoDB connector does not support discovery. However, you can useinstance instrospection, which creates a LoopBack model from an existingJavaScript object.

To do this, create a script named instance-introspections.jsin the bin dir. Then run:

  1. node bin/instance-introspection

You should see:

  1. Created: { email: 'bob.doe@ibm.com',
  2. createdAt: Thu Oct 22 2015 19:38:20 GMT-0700 (PDT),
  3. lastModifiedAt: Thu Oct 22 2015 19:38:20 GMT-0700 (PDT),
  4. id: 56299d9d71c7f600719ca39f }

See the official docsfor more info.


More LoopBack examples

Tags: example_app