Closed the1mills closed 8 years ago
I fixed this problem - what was happening is that the automigration was not complete before creating the db entities.
For your use, the following is the same script in the readme, but blocks on automigration:
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback-example-access-control
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
const async = require('async');
module.exports = function (app, cb) {
var User = app.models.user;
var Role = app.models.Role;
var Project = app.models.project;
var RoleMapping = app.models.RoleMapping;
var Team = app.models.team;
function automigrateItem(item) {
return function (cb) {
app.dataSources.psql.automigrate(item, function (err) {
err ? cb(err) : (function () {
console.log('automigration is done for the following entity => ' + item);
cb(null);
})();
});
}
}
async.parallel([
automigrateItem('user'),
automigrateItem('project'),
automigrateItem('team'),
automigrateItem('Role'),
automigrateItem('RoleMapping')
],
function (err) {
if (err) {
cb(err);
}
else {
User.create([
{username: 'John', email: 'john@doe.com', password: 'opensesame'},
{username: 'Jane', email: 'jane@doe.com', password: 'opensesame'},
{username: 'Bob', email: 'bob@projects.com', password: 'opensesame'}
],
function (err, users) {
if (err) return cb(err);
console.log('Created users:', users);
async.parallel([
function (cb) {
// create project 1 and make john the owner
users[0].projects.create({
name: 'project1',
balance: 100
}, function (err, project) {
if (err) return cb(err);
console.log('Created project:', project);
// add team members
Team.create([
{ownerId: project.ownerId, memberId: users[0].id},
{ownerId: project.ownerId, memberId: users[1].id}
], function (err, team) {
if (err) return cb(err);
console.log('Created team:', team);
cb(null);
});
});
},
function (cb) {
//create project 2 and make jane the owner
users[1].projects.create({
name: 'project2',
balance: 100
}, function (err, project) {
if (err) return cb(err);
console.log('Created project:', project);
//add team members
Team.create({
ownerId: project.ownerId,
memberId: users[1].id
}, function (err, team) {
if (err) return cb(err);
console.log('Created team:', team);
cb(null);
});
});
}
], function (err) {
if (err) return cb(err);
//create the admin role
Role.create({
name: 'admin'
}, function (err, role) {
if (err) return cb(err);
console.log('Created role:', role);
//make bob an admin
role.principals.create({
principalType: RoleMapping.USER,
principalId: users[2].id
}, function (err, principal) {
if (err) return cb(err);
console.log('Created principal:', principal);
cb(null);
});
});
});
});
}
});
};
the above assumes you are using postgres (psql)
this line above
app.dataSources.psql.automigrate(item, function (err) {})
will differ for whoever you are
(you can close issue, thx)
@ORESoftware - This reply saved me a massive headache! Thanks!
Np! This automigration stuff is massively useful for working with standard RDBMs. (I am ORESoftware's alter ego :)
Followed the instructions in the readme, got this error:
anyone know what might cause this? thanks