userauth.json
{
"name": "userauth",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id":{
"type":"number",
"required":true,
"length":11,
"mysql":
{
"columnName":"id",
"dataType":"INT",
"dataLength":11,
"nullable":"N"
}
},
"firstname":{
"type":"string",
"required":true,
"length":25,
"mysql":
{
"columnName":"firstname",
"dataType":"VARCHAR",
"dataLength":25,
"nullable":"N"
}
},
"lastname":{
"type":"string",
"required":true,
"length":25,
"mysql":
{
"columnName":"lastname",
"dataType":"VARCHAR",
"dataLength":25,
"nullable":"N"
}
},
"email":{
"type":"string",
"required":true,
"length":50,
"mysql":
{
"columnName":"email",
"dataType":"VARCHAR",
"dataLength":50,
"nullable":"N"
}
},
"password":{
"type":"string",
"required":true,
"length":30,
"mysql":
{
"columnName":"password",
"dataType":"VARCHAR",
"dataLength":30,
"nullable":"N"
}
},
"dd":{
"type":"number",
"required":true,
"length":2,
"mysql":
{
"columnName":"dd",
"dataType":"INT",
"dataLength":2,
"nullable":"N"
}
},
"mm":{
"type":"number",
"required":true,
"length":2,
"mysql":
{
"columnName":"mm",
"dataType":"INT",
"dataLength":2,
"nullable":"N"
}
},
"yyyy":{
"type":"number",
"required":true,
"length":4,
"mysql":
{
"columnName":"yyyy",
"dataType":"INT",
"dataLength":4,
"nullable":"N"
}
}
},
"validations": [],
"relations": {},
"acl": [],
"methods": {}
}
userauth.js
'use strict';
module.exports = function(userauth) {
};
model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"userauth": {
"dataSource": "db",
"public": true
}
}
datasource.json
{
"db": {
"host": "localhost",
"port": 3306,
"url": "",
"database": "users",
"password": "12121212",
"name": "db",
"user": "root",
"connector": "mysql"
}
}
ERROR IN RESPONSE WHEN TRYING TO GET or POST
> {
> "error": {
> "statusCode": 500,
> "name": "Error",
> "message": "ER_BAD_FIELD_ERROR: Unknown column 'model' in 'field list'",
> "code": "ER_BAD_FIELD_ERROR",
> "errno": 1054,
> "sqlMessage": "Unknown column 'model' in 'field list'",
> "sqlState": "42S22",
> "index": 0,
> "sql": "SELECT `model`,`property`,`accessType`,`permission`,`principalType`,`principalId`,`id`
> FROM `ACL` WHERE `model` IN ('userauth','*') AND `property` IN
> ('find','*') AND `accessType` IN ('READ','*') ORDER BY `id`",
mySQL db is already connected.
another point I noted that loopback is creating own db name as "acl" and not using the db name defined while creating the model.
I have db name users, and created table acl with the exact column names properties name in userauth.json file
LoopBack is actually not creating its own database named as "ACL". As you have created a datasource with the required details of the database, your app is using that database.
You can use the following script to identify and create tables in the database based on LoopBack models you've created.
Create a file: "createTables.js" in server folder, and add following code:
var server = require('./server');
var ds = server.dataSources.mysql;
var lbTables = ['User', 'AccessToken', 'ACL', 'RoleMapping', 'Role', 'userauth'];
ds.automigrate(lbTables, function (er) {
if (er) throw er;
console.log('Loopback tables [' - lbTables - '] created in ', ds.adapter.name);
ds.disconnect();
});
This will create all of the tables with columns based on the properties of the LoopBack models. You can run the script by moving in the server directory and running node createTable.js command.
Refer to their documentation: https://loopback.io/doc/en/lb2/Creating-database-tables-for-built-in-models.html
Warning: Auto-migration will drop an existing table if its name matches a model name. When tables with data exist, use auto-update to avoid data loss.
This is actually used for creating tables for built-in loopback models, but you can add other models (using their names) to lbTables array and the script will create the database tables for those models too.