Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persist Users in Strongloop Loopback

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.)

like image 669
Geoffrey Burdett Avatar asked Aug 24 '26 09:08

Geoffrey Burdett


1 Answers

  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

like image 166
DecentGradient Avatar answered Aug 26 '26 22:08

DecentGradient