Reputation: 10252
I'm trying out loopback and noticed that in the model-config
file it has a reference to 5 models that have their dataSource
set to the memory database db
:
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
}
Is that desired behaviour to keep the users, acl etc in the memory? If not, how can I port those models to mysql
? I tried switching the dataSource
to my mysql
source but the server complains because the tables are not present.
Upvotes: 1
Views: 910
Reputation: 657
Add these table in MySql Database:-
CREATE TABLE `AccessToken` (
`id` VARCHAR(255) NOT NULL,
`ttl` INT(11) NULL DEFAULT NULL,
`scopes` TEXT NULL,
`created` DATETIME NULL DEFAULT NULL,
`userId` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `ACL` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`model` VARCHAR(512) NULL DEFAULT NULL,
`property` VARCHAR(512) NULL DEFAULT NULL,
`accessType` VARCHAR(512) NULL DEFAULT NULL,
`permission` VARCHAR(512) NULL DEFAULT NULL,
`principalType` VARCHAR(512) NULL DEFAULT NULL,
`principalId` VARCHAR(512) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `Role` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(512) NOT NULL,
`description` VARCHAR(512) NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `RoleMapping` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`principalType` VARCHAR(512) NULL DEFAULT NULL,
`principalId` VARCHAR(255) NULL DEFAULT NULL,
`roleId` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `principalId` (`principalId`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `User` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`realm` VARCHAR(512) NULL DEFAULT NULL,
`username` VARCHAR(512) NULL DEFAULT NULL,
`password` VARCHAR(512) NOT NULL,
`email` VARCHAR(512) NOT NULL,
`emailVerified` TINYINT(1) NULL DEFAULT NULL,
`verificationToken` VARCHAR(512) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
Upvotes: -1
Reputation: 2872
Copy paste the following code in server/server.js. (preferably to the last)
var appModels = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role'];
var ds = app.dataSources.mysqlDS;
ds.isActual(appModels, function(err, actual) {
if (!actual) {
ds.autoupdate(appModels, function(err) {
if (err) throw (err);
});
}
});
Don't forget to change the model's datasource to the new datasource in model-config.json. In the code replace the mysqlDS to your datasource.
Cheers!
Reference: https://loopback.io/doc/en/lb3/Creating-database-tables-for-built-in-models.html
Upvotes: 7
Reputation: 2602
You'll need to do a migration. Check out the docs for auto-migrate but the easiest solution (no coding) is to use strongloop arc.
To code it you could drop an auto-migration into a boot script, like dataSource.automigrate();
which would build the tables however that would also drop any existing data you have.
Upvotes: 0