See also:
Note: This page was generated from the loopback-example-database README.
loopback-example-database
A tutorial for basic database related features.
- 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.
For example, to view the MySQL example:
Before starting this tutorial, make sure you have the following installed:
- Node
- NPM
npm install
npm start
1. Create a new LoopBack app
App info
- Name:
loopback-example-database
- Dir to contain the project:
loopback-example-database
lb app loopback-example-database
_-----_
| | ╭──────────────────────────╮
|--(o)--| │ Let's create a LoopBack │
`---------´ │ application! │
( _´U`_ ) ╰──────────────────────────╯
/___A___\ /
| ~ |
__'.___.'__
´ ` |° ´ Y `
? What's the name of your application? loopback-example-database
? Enter name of the directory to contain the project: loopback-example-database
info change the working directory to loopback-example-database
? Which version of LoopBack would you like to use? 3.x (current)
? What kind of application do you have in mind? empty-server (An empty LoopBack API, without any c
onfigured models or datasources)
cd loopback-example-database
npm install --save loopback-connector-mongodb
3. Create a data source
Data source info
- Data source name:
- Select the connector for
accountDS
:MongoDB
lb datasource accountDS
... # follow the prompts
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:
lb model
... # follow the prompts
Model Info
- Model name:
Account
- Attach
Account
to:accountDS (mongodb)
- Base class:
PersistedModel
- Custom plural form: Leave blank
- Properties:
email
- String
- Not required
createdAt
- Date
- Not required
lastModifiedAt
- Date
- Not required
slc loopback:model Account
... # 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 via auto-migration.
Start by creating a dir to store general-purpose scripts:
mkdir bin
Inside that dir, create a script named .To create the Account
collection and create two sample accounts, run:
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, useautoupdate
instead.
You should see:
7. View data using the explorer
node .
Then to view the existing account data, browse to localhost:3000/explorer
andclick:
GET /Accounts
Try it out!
You should see:
[
{
"email": "foo@bar.com",
"createdAt": "2015-10-23T00:58:09.280Z",
"lastModifiedAt": "2015-10-23T00:58:09.280Z",
"id": "562986213ea33440575c6587"
},
{
"email": "baz@qux.com",
"createdAt": "2015-10-23T00:58:09.280Z",
"lastModifiedAt": "2015-10-23T00:58:09.280Z",
"id": "562986213ea33440575c6588"
}
]
Try out some of the other endpoints to get a feel for how explorer works.
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 in the bin
dir. Then run:
node bin/instance-introspection
You should see:
Created: { email: 'bob.doe@ibm.com',
createdAt: Thu Oct 22 2015 19:38:20 GMT-0700 (PDT),
id: 56299d9d71c7f600719ca39f }
See the official docsfor more info.
Tags: example_app