#loopback-example-mongodb
Basic instructions:
git clone https://github.com/strongloop/loopback-example-mongodb.git
cd loopback-example-mongodb
npm install
Then run any script in server/bin
(for example, node server/bin/discover-schema.js
).
##Prerequisites
###Tutorials
###Knowledge
##Procedure
###1. Create the application
####Application information
- Name:
loopback-example-mongodb
- Directory to contain the project:
loopback-example-mongodb
slc loopback loopback-example-mongodb
... # follow the prompts
cd loopback-example-mongodb
###2. Install the connector
npm install --save loopback-connector-mongodb
###3. Configure the datasource
####Datasource information
- Datasource:
accountDs
- Connector:
MongoDB
slc loopback:datasource accountDs
... # follow the prompts
Add the datasource configurations to
server/datasources.json
.
We provide a demo server for convenience sake, but feel free to use your own database server.
###4. Add a model
####Model information
- Name:
Account
- Datasource:
accountDs
- 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
- Datasource:
slc loopback:model Account
... # follow the prompts
###5. Add a script to migrate data
Create a directory for to store scripts.
mkdir server/bin
bin
is a directory name commonly used for executable files on unix and unix-like systems.
Create automigrate.js
inside the
bin
directory.
datasSource.automigrate()
requires INSERT object, CREATE DDL, and DROP DDL rights to execute properly.
####Test the script
#####WARNING
dataSource.automigrate()
creates a new table in the database if it doesn't exist. If the table already exists, it will be DESTROYED and ALL existing data will be dropped. If you want to keep the existing data, usedatasource.autoupdate()
instead.
node server/bin/automigrate.js
This script creates two models in the specified data source.
You can view the newly inserted data using built-in API explorer. Start the application with
slc run
and browse tolocalhost:3000/explorer
to inspect the data.
###6. Add a script to perform instance introspection
Discovery is the process of reverse engineering a LoopBack model from an existing database schema.
The LoopBack MongoDB connector does not support discovery. However, you can use [instance instrospection], which creates LoopBack model from an existing JavaScript object.
See the instance introspection documentation for more information.
###8. Conclusion
You've successfully implemented various MongoDB database features provided by LoopBack. See the official documentation and loopback-connector-mongodb for more information.