3
votes

What have you done to persist the User data in production? Is there an easy way to find the schema of the User model so it can be reproduced in a database?

(Preemptive Note: DiscoverSchema finds the schema of the database, not the model)

(Also, I know the docs say the User model can be persisted by setting the file property in the default db datasource, but I have security, scalability, and durability concerns with that.)

4

4 Answers

6
votes
  1. setup a database.
  2. define a new datasource by editing the ./server/datasources.json by adding your database, for example:

"mongodb_dev": { 
    "name": "mongodb_dev",
    "connector": "mongodb",
    "host": "127.0.0.1", 
    "database": "devDB", 
    "username": "devUser", 
    "password": "devPassword", 
    "port": 27017 
  }
  1. Update your ./server/model-config.json to have the built in models use your new datasource :

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "mongodb_dev"
  },
  "AccessToken": {
    "dataSource": "mongodb_dev",
    "public": false
  },
  "ACL": {
    "dataSource": "mongodb_dev",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "mongodb_dev",
    "public": false
  },
  "Role": {
    "dataSource": "mongodb_dev",
    "public": false
  }
}

3.Create server/create-lb-tables.js file to move the built-in tables to your database with the following

var server = require('./server');
var ds = server.dataSources.mongodb_dev;// <<<<<<note the datasource name
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();
});
  1. Run the script cd server node create-lb-tables.js

This is a link to the official docs about putting built in models on your db

https://docs.strongloop.com/display/public/LB/Creating+database+tables+for+built-in+models

2
votes

You should persist users into your chosen database via a connector.

The file property is only used to persist data to the filesystem and is NOT recommended for production. For production, you should use one of the connectors (MongoDB, MySQL, etc) to persist your data.

See the docs to find out what properties are part of the built-in User model or change the default database settings to persist the User model to the filesystem to see what properties are available in the JSON file output. If you don't understand all this, go through the tutorial series to get an understanding of all these concepts. Cheers. ;)

0
votes

Since no one actually shared how to accomplish this, I built this example using the example-mysql repo from strongloop and tweaked 2 files.

  • model-config.json (change the built-in models from db to your datasource)
  • bin/automigrate.js (add additional automigrate functions for each model)

See how here: https://github.com/mikesparr/loopback-example-users-to-mysql

Good luck!

0
votes
  1. Create a persistent(Mongo or any other) datasource using slc loopback:dataSource
  2. Edit model-config.json and mention above created datasource.
  3. Restart server