1

New to loopback and and I can't for the life of me migrate a single json model to my mongodb data source.

I've been fussing about with: https://docs.strongloop.com/display/public/LB/Creating+a+database+schema+from+models

And here's what I've tried so far:

I created a migration script bin/migrate.js which I plan on running every time I need to migrate changes using auto-update:

var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.acme;
var appModels = ['Client'];
ds.isActual(appModels, function(err, actual) {
    if (!actual) {
        ds.autoupdate(appModels, function(err) {
            if (err){
                throw (err);
            }
        });
    }
});

console.log("Migrated: " + appModels.join());

I checked both using robomongo and the mongo cli and I can't find the generated table:

> use acme
switched to db acme
> db.getCollectionNames();
[ ]

I'm also new to mongodb so there may be something wrong as well in how I'm checking if the migration succeeded.

I've tried the answer here but it didn't work for me: Migrating built-in models to Databases

Some other pertinent stuff:

datasources.json

{
  "acme": {
    "host": "127.0.0.1",
    "port": 27017,
    "database": "acme",
    "username": "root",
    "password": "password",
    "name": "acme",
    "connector": "mongodb",
    "user": "root"
  }
}

My model

{
  "name": "Client",
  "plural": "Clients",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "contact_number": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}
Community
  • 1
  • 1
turntwo
  • 2,452
  • 19
  • 28

1 Answers1

1

Turns out that this was actually working. Coming from a mysql background, I didn't know that the you can only see a mongodb collection once there are documents inside it.

I got the visual confirmation I was looking for when I added the following:

app.models.Client.create([
    {contact_number: '09xxxxxxx', password: 'password', email: 'acme@gmail.com'},
], function(err, clients) {
    if (err){
        throw err;
    }

    console.log('Models created: \n', clients);
});
turntwo
  • 2,452
  • 19
  • 28