loopback-example-database
A tutorial for basic database related features.
- Data sources
- Creating
- Configuring
- Models
- Creating
- Automigration
- 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
- LoopBack CLI tools; see
cd loopback-example-database
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
... # follow the prompts
cd loopback-example-database
npm install --save loopback-connector-mysql
3. Create a data source
Data source info
- Data source name:
accountDS
- Select the connector for
accountDS
:MySQL
lb datasource accountDS
... # follow the prompts
This creates a new data source named accountDS
that uses the MySQL connector.
4. Configure the data source
For the purposes of this example, we will use a preconfigured StrongLoop MySQLserver. Edit server/datasources.json
to set the MySQL configs:
Model Info
- Model name:
Account
- Attach
Account
to:accountDS (mysql)
- Base class:
PersistedModel
- Expose via REST:
Yes
- Custom plural form: Leave blank
- Properties:
email
- String
createdAt
- Date
- Not required
lastModifiedAt
- Date
- Not required
lb model Account
... # follow the prompts
6. Create the collection with sample data - Automigration
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
You should see:
Created: { email: 'john.doe@ibm.com',
createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
id: 562986213ea33440575c6588 }
Created: { email: 'jane.doe@ibm.com',
createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
id: 562986213ea33440575c6587 }
7. View data using the explorer
Projects scaffolded via slc loopback
come with loopback-component-explorer
preconfigured. From the project root, start the server:
Then to view the existing account data, browse to localhost:3000/explorer
andclick:
Try it out!
You should see:
[
{
"email": "john.doe@ibm.com",
"createdAt": "2015-10-23T00:58:09.280Z",
"lastModifiedAt": "2015-10-23T00:58:09.280Z",
"id": "562986213ea33440575c6587"
},
{
"email": "jane.doe@ibm.com",
"createdAt": "2015-10-23T00:58:09.280Z",
"lastModifiedAt": "2015-10-23T00:58:09.280Z",
"id": "562986213ea33440575c6588"
}
]
Create a script name . Then run this script todiscover the schema from the existing Account
table:
node bin/discover-schema
You should see:
{
"name": "Account",
"options": {
"idInjection": false,
"mysql": {
"schema": "loopback-example-mysql",
"table": "Account"
}
},
"properties": {
"id": {
"type": "Number",
"required": true,
"length": null,
"precision": 10,
"scale": 0,
"id": 1,
"mysql": {
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "N"
}
},
"email": {
"type": "String",
"length": 512,
"precision": null,
"scale": null,
"mysql": {
"columnName": "email",
"dataType": "varchar",
"dataLength": 512,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"createdat": {
"type": "Date",
"required": false,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "createdAt",
"dataType": "datetime",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
},
"lastmodifiedat": {
"type": "Date",
"required": false,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "lastModifiedAt",
"dataType": "datetime",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
}
}
}
9. Add a script to discover and build models
When retrieving the scheme is not enough, you can discover and build LoopBackmodels in one step.
Create a sript named .Then run:
node bin/discover-and-build-models
You should see:
Tags: example_app