Skip to content

orudis/loopback-example-database

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

loopback-example-database

This project contains examples to demonstrate LoopBack connectors for databases:

You can pretty much switch between the databases by updating datasources.json and models.json. No code change is required. In the following steps, we'll use postgresql as the example.

For those who are not familiar with LoopBack, it’s an open source mobile backend framework that connects mobile devices to enterprise data. LoopBack provides out-of-box data access capabilities for models through pluggable datasources and connectors. Connectors provide connectivity to variable backend systems, such as databases or REST APIs. Models are in turn exposed to mobile devices as REST APIs and SDKs.

Prerequisite

First, make sure you have strong-cli installed.

    npm install -g strong-cli

Next, you need a running MongoDB server. In this article, you'll connect to an instance running on demo.strongloop.com.

Create the LoopBack application

To demonstrate how to use MongoDB connector for LoopBack, we'll create a simple application from scratch using the slc command:

    slc lb project loopback-example-database
    cd loopback-example-database
    slc lb datasource accountDB --connector postgresql
    slc lb model account -i --data-source accountDB

Follow the prompts to create your model with the following properties:

  • email: string - The email id for the account
  • level: number - The game level you are in
  • created: date - The date your account is created
  • modified: date - The date your account is updated

The properties will be saved to models.json.

Install dependencies

Let's add the loopback-connector-mongodb module and install the dependencies.

    npm install loopback-connector-mongodb --save

Configure the data source

The generated data source use the memory connector by default, to connect to MongoDB, we'll modify the data source configuration as follows.

    vi datasources.json

Note: Future releases will probably generate a config.json file for the data source configuration.

In datasoures.json, replace the data source configuration for mongodb with the following snippet:

  "accountDB": {
    "connector": "mongodb",
    "host": "demo.strongloop.com",
    "database": "demo",
    "username": "demo",
    "password": "L00pBack",
    "port": 27017
  }

Create the collection and add test data

Now we have an account model in LoopBack, do we need to run some SQL statements to create the corresponding table in MongoDB database?

Sure, but even simpler, LoopBack provides Node.js APIs to do so automatically. The code is create-test-data.js.

    node create-test-data

Let's look at the code:

      accounts.forEach(function(act) {
        Account.create(act, function(err, result) {
          if(!err) {
            console.log('Record created:', result);
          }
        });
    });

dataSource.automigrate() creates or recreates the table in MongoDB based on the model definition for account. Please note the call will drop the table if it exists and your data will be lost. We can use dataSource.autoupdate() instead to keep the existing data.

Account.create() inserts two sample records to the MongoDB table.

Run the application

    node app

Open your browser now.

To get all accounts, go to http://localhost:3000/api/accounts.

    [
      {
        "email": "foo@bar.com",
        "level": 10,
        "created": "2013-10-15T21:34:50.000Z",
        "modified": "2013-10-15T21:34:50.000Z",
        "id": 1
      },
      {
        "email": "bar@bar.com",
        "level": 20,
        "created": "2013-10-15T21:34:50.000Z",
        "modified": "2013-10-15T21:34:50.000Z",
        "id": 2
      }
    ]

To get an account by id, go to http://localhost:3000/api/accounts/1.

    {
      "email": "foo@bar.com",
      "level": 10,
      "created": "2013-10-15T21:34:50.000Z",
      "modified": "2013-10-15T21:34:50.000Z",
      "id": "1"
    }

All the REST APIs can be explored at:

http://127.0.0.1:3000/explorer

As you have seen, the MongoDB connector for LoopBack enables applications to work with data in MongoDB databases. It can be new data generated by mobile devices that need to be persisted, or existing data that need to be shared between mobile clients and other backend applications. No matter where you start, LoopBack makes it easy to handle your data with MongoDB. It’s great to have MongoDB in the Loop!

About

LoopBack example for various databases

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors