Skip to content

An example demonstrating LoopBack with MongoDB.

License

Notifications You must be signed in to change notification settings

Witvoet/loopback-example-mongodb

Repository files navigation

#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
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, use datasource.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 to localhost: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.


About

An example demonstrating LoopBack with MongoDB.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published