#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
###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
###Install the connector
$ npm install --save loopback-connector-mongodb
###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.
###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
###Add a script to migrate data
Create a directory for to store scripts.
$ mkdir server/bin
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 is destroyed and all existing data is 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.
###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.