8

How to move built-in models like User, Roles, User-Role-Mapping etc... to the database we've created instead of the default datasource:db? The built-in models are not listing in Arc.

I've tried creating a new model which inherits these base model. But, data is not saved into the new model.

Please advice...I've been sitting on it for a couple of weeks. Thanks

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Anoop Thiruonam
  • 2,567
  • 2
  • 28
  • 48

1 Answers1

13

Default "db" datasource is placed in memory. That is why your data are not persisted after you restart application. You have to install appropriate database connector and then you have to add datasource for your database inside server/datasources.js.

http://docs.strongloop.com/display/public/LB/Connecting+models+to+data+sources

If you created application with "slc loopback" command, then your datasource contains only memory connector. Check datasources.js file and you will see something like this:

{
  "db": {
  "name": "db",
  "connector": "memory"
  }
}

If you want to persist your data for example in postgresql database (process is almost same for any supported connector), you have to expand your datasoruces.json file with your database information:

{
  "db": {
  "name": "db",
  "connector": "memory"
  },

  "mydata": {
    "host": "db_host",
    "database": "your_database_name",
    "username": "your_db_username",
    "password": "your_db_password",
    "connector": "postgresql"
  }
}

You can also do this using "slc loopback:datasource" command. Wizard will help you to define your datasource. Don't forget to install db connector.

npm install loopback-connector-postgresql

Last thing to do is to assign your datasource to desired models. You can do this using wizard (see slc loopback:model command), or you can edit server/model-config.json file manually.

{
  "User": {
    "dataSource": "mydata",
    "public": true
  },
  "AccessToken": {
    "dataSource": "mydata",
    "public": false
  },
  "ACL": {
    "dataSource": "mydata",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "mydata",
    "public": false
  },
  "Role": {
    "dataSource": "mydata",
    "public": false
  }
}

UPDATE You can try this code snippet to update your tables from models. Put his code somewhere in server/server.js

var appModels = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role'];

var ds = app.dataSources.mydata;
ds.isActual(appModels, function(err, actual) {
  if (!actual) {
    ds.autoupdate(appModels, function(err) {
      if (err) throw (err);
    });
  }
});

I would recommend you to read about creating/updating database schema from models on strongloop page. Pay attention abut difference between autoupdate and automigrate functions

http://docs.strongloop.com/display/public/LB/Creating+a+database+schema+from+models

A.Z.
  • 1,638
  • 2
  • 18
  • 27
  • Thank you for your reply. I've already tried the same. Only one problem: I can't migrate this to the database. I've tried using Arc and cli command. – Anoop Thiruonam May 10 '15 at 06:28
  • Can you explain with more details what you have tried? – A.Z. May 11 '15 at 06:58
  • 1
    I have updated answer with some code and link to docs where you can read more about updating schema from models. – A.Z. May 11 '15 at 07:06
  • Oh Great! It worked. I pasted the code in your update and it worked. – Anoop Thiruonam May 11 '15 at 17:08
  • What if I need to create migrations in different models at the same time and the result of one migration is the new records in another migration? that's a real scenario when a migration result has to be inserted in another table as a related model. for example you insert a new user and the returned id is the new registry for subscriptions table which has a subscription plan and a userId. for example, how would it look like? – Luis Parada Jul 19 '16 at 03:09
  • 1
    @LuisParada Migration as discussed here is about database schema migration. You should ask another question about creating/migrating data. – A.Z. Jul 20 '16 at 09:35