Page Contents
LoopBack applications come with a small set of built-in models. To create database tables for these models, follow the general procedure for creating a database schema from models using auto-migration.
Important:
If the database has existing tables, running automigrate()
will drop and re-create the tables and thus may lead to loss of data.
To avoid this problem use autoupdate()
.
See Creating a database schema from models for more information.
To create tables for LoopBack built-in models, follow this procedure:
-
Follow the basic procedure in Attaching models to data sources to change from the in-memory data source to the database you want to use.
-
Create
server/create-lb-tables.js
file with the following:var server = require('./server'); var ds = server.dataSources.db; var lbTables = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role']; ds.automigrate(lbTables, function(er) { if (er) throw er; console.log('Loopback tables [' - lbTables - '] created in ', ds.adapter.name); ds.disconnect(); });
-
Run the script manually:
$ cd server $ node create-lb-tables.js