Gathering detailed insights and metrics for loopback-connector-mysql-new-instance
Gathering detailed insights and metrics for loopback-connector-mysql-new-instance
Gathering detailed insights and metrics for loopback-connector-mysql-new-instance
Gathering detailed insights and metrics for loopback-connector-mysql-new-instance
Loopback Connector for MySQL
npm install loopback-connector-mysql-new-instance
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (98.16%)
Shell (1.84%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
125 Stars
795 Commits
182 Forks
43 Watchers
19 Branches
56 Contributors
Updated on Jul 14, 2025
Latest Version
5.4.2
Package Id
loopback-connector-mysql-new-instance@5.4.2
Unpacked Size
273.77 kB
Size
60.84 kB
File Count
51
NPM Version
6.9.0
Node Version
8.15.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
MySQL is a popular open-source relational database management system (RDBMS). The loopback-connector-mysql
module provides the MySQL connector module for the LoopBack framework.
In your application root directory, enter this command to install the connector:
1npm install loopback-connector-mysql --save
This installs the module from npm and adds it as a dependency to the application's package.json
file.
If you create a MySQL data source using the data source generator as described below, you don't have to do this, since the generator will run npm install
for you.
Use the Data source generator to add a MySQL data source to your application.
The generator will prompt for the database server hostname, port, and other settings
required to connect to a MySQL database. It will also run the npm install
command above for you.
The entry in the application's /server/datasources.json
will look like this:
1"mydb": { 2 "name": "mydb", 3 "connector": "mysql", 4 "host": "myserver", 5 "port": 3306, 6 "database": "mydb", 7 "password": "mypassword", 8 "user": "admin" 9 }
Edit datasources.json
to add any other additional properties that you require.
Property | Type | Description |
---|---|---|
collation | String | Determines the charset for the connection. Default is utf8_general_ci. |
connector | String | Connector name, either “loopback-connector-mysql” or “mysql”. |
connectionLimit | Number | The maximum number of connections to create at once. Default is 10. |
database | String | Database name |
debug | Boolean | If true, turn on verbose mode to debug database queries and lifecycle. |
host | String | Database host name |
password | String | Password to connect to database |
port | Number | Database TCP port |
socketPath | String | The path to a unix domain socket to connect to. When used host and port are ignored. |
supportBigNumbers | Boolean | Enable this option to deal with big numbers (BIGINT and DECIMAL columns) in the database. Default is false. |
timeZone | String | The timezone used to store local dates. Default is ‘local’. |
url | String | Connection URL of form mysql://user:password@host/db . Overrides other connection settings. |
username | String | Username to connect to database |
NOTE: In addition to these properties, you can use additional parameters supported by node-mysql
.
See LoopBack types for details on LoopBack's data types.
LoopBack Type | MySQL Type |
---|---|
String/JSON | VARCHAR |
Text | TEXT |
Number | INT |
Date | DATETIME |
Boolean | TINYINT(1) |
GeoPoint object | POINT |
Custom Enum type (See Enum below) | ENUM |
MySQL Type | LoopBack Type |
---|---|
CHAR | String |
BIT(1) CHAR(1) TINYINT(1) | Boolean |
VARCHAR TINYTEXT MEDIUMTEXT LONGTEXT TEXT ENUM SET | String |
TINYBLOB MEDIUMBLOB LONGBLOB BLOB BINARY VARBINARY BIT | Node.js Buffer object |
TINYINT SMALLINT INT MEDIUMINT YEAR FLOAT DOUBLE NUMERIC DECIMAL |
Number For NUMERIC and DECIMAL, see Fixed-point exact value types |
DATE TIMESTAMP DATETIME | Date |
NOTE as of v3.0.0 of MySQL Connector, the following flags were introduced:
treatCHAR1AsString
default false
- treats CHAR(1) as a String instead of a BooleantreatBIT1AsBit
default true
- treats BIT(1) as a Boolean instead of a BinarytreatTINYINT1AsTinyInt
default true
- treats TINYINT(1) as a Boolean instead of a NumberUse the mysql
model property to specify additional MySQL-specific properties for a LoopBack model.
For example:
{% include code-caption.html content="/common/models/model.json" %}
1"locationId":{ 2 "type":"String", 3 "required":true, 4 "length":20, 5 "mysql": 6 { 7 "columnName":"LOCATION_ID", 8 "dataType":"VARCHAR", 9 "dataLength":20, 10 "nullable":"N" 11 } 12}
You can also use the dataType column/property attribute to specify what MySQL column type to use for many loopback-datasource-juggler types. The following type-dataType combinations are supported:
Use the limit
option to alter the display width. Example:
1{ userName : { 2 type: String, 3 dataType: 'char', 4 limit: 24 5 } 6}
Use the default
property to have MySQL handle setting column DEFAULT
value.
1"status": { 2 "type": "string", 3 "mysql": { 4 "default": "pending" 5 } 6}, 7"number": { 8 "type": "number", 9 "mysql": { 10 "default": 256 11 } 12}
For the date or timestamp types use CURRENT_TIMESTAMP
or now
:
1"last_modified": { 2 "type": "date", 3 "mysql": { 4 "default":"CURRENT_TIMESTAMP" 5 } 6}
NOTE: The following column types do NOT supported MySQL Default Values:
For Float and Double data types, use the precision
and scale
options to specify custom precision. Default is (16,8). For example:
1{ average : 2 { type: Number, 3 dataType: 'float', 4 precision: 20, 5 scale: 4 6 } 7}
For Decimal and Numeric types, use the precision
and scale
options to specify custom precision. Default is (9,2).
These aren't likely to function as true fixed-point.
Example:
1{ stdDev : 2 { type: Number, 3 dataType: 'decimal', 4 precision: 12, 5 scale: 8 6 } 7}
Convert String / DataSource.Text / DataSource.JSON to the following MySQL types:
Example:
1{ userName : 2 { type: String, 3 dataType: 'char', 4 limit: 24 5 } 6}
Example:
1{ biography : 2 { type: String, 3 dataType: 'longtext' 4 } 5}
Convert JSON Date types to datetime or timestamp
Example:
1{ startTime : 2 { type: Date, 3 dataType: 'timestamp' 4 } 5}
Enums are special. Create an Enum using Enum factory:
1var MOOD = dataSource.EnumFactory('glad', 'sad', 'mad'); 2MOOD.SAD; // 'sad' 3MOOD(2); // 'sad' 4MOOD('SAD'); // 'sad' 5MOOD('sad'); // 'sad' 6{ mood: { type: MOOD }} 7{ choice: { type: dataSource.EnumFactory('yes', 'no', 'maybe'), null: false }}
The MySQL connector supports model discovery that enables you to create LoopBack models based on an existing database schema using the unified database discovery API. For more information on discovery, see Discovering models from relational databases.
The MySQL connector also supports auto-migration that enables you to create a database schema from LoopBack models using the LoopBack automigrate method.
For more information on auto-migration, see Creating a database schema from models for more information.
MySQL handles the foreign key integrity of the related models upon auto-migrate or auto-update operation. It first deletes any related models before calling delete on the models with the relationship.
Example:
model-definiton.json
1{ 2 "name": "Book", 3 "base": "PersistedModel", 4 "idInjection": false, 5 "properties": { 6 "bId": { 7 "type": "number", 8 "id": true, 9 "required": true 10 }, 11 "name": { 12 "type": "string" 13 }, 14 "isbn": { 15 "type": "string" 16 } 17 }, 18 "validations": [], 19 "relations": { 20 "author": { 21 "type": "belongsTo", 22 "model": "Author", 23 "foreignKey": "authorId" 24 } 25 }, 26 "acls": [], 27 "methods": {}, 28 "foreignKeys": { 29 "authorId": { 30 "name": "authorId", 31 "foreignKey": "authorId", 32 "entityKey": "aId", 33 "entity": "Author", 34 "onUpdate": "restrict", 35 "onDelete": "restrict" 36 } 37 } 38}
1{ 2 "name": "Author", 3 "base": "PersistedModel", 4 "idInjection": false, 5 "properties": { 6 "aId": { 7 "type": "number", 8 "id": true, 9 "required": true 10 }, 11 "name": { 12 "type": "string" 13 }, 14 "dob": { 15 "type": "date" 16 } 17 }, 18 "validations": [], 19 "relations": {}, 20 "acls": [], 21 "methods": {} 22}
boot-script.js
1module.exports = function(app) { 2 var mysqlDs = app.dataSources.mysqlDS; 3 var Book = app.models.Book; 4 var Author = app.models.Author; 5 6 // first autoupdate the `Author` model to avoid foreign key constraint failure 7 mysqlDs.autoupdate('Author', function(err) { 8 if (err) throw err; 9 console.log('\nAutoupdated table `Author`.'); 10 11 mysqlDs.autoupdate('Book', function(err) { 12 if (err) throw err; 13 console.log('\nAutoupdated table `Book`.'); 14 // at this point the database table `Book` should have one foreign key `authorId` integrated 15 }); 16 }); 17};
Prior to loopback-connector-mysql@5.x
, MySQL connector was saving and loading GeoPoint properties from the MySQL database in reverse.
MySQL expects values to be POINT(X, Y) or POINT(lng, lat), but the connector was saving them in the opposite order(i.e. POINT(lat,lng)).
If you have an application with a model that has a GeoPoint property using previous versions of this connector, you can migrate your models
using the following programmatic approach:
NOTE Please back up the database tables that have your application data before performing any of the steps.
server/boot/
directory with the following:1'use strict'; 2module.exports = function(app) { 3 function findAndUpdate() { 4 var teashop = app.models.teashop; 5 //find all instances of the model we'd like to migrate 6 teashop.find({}, function(err, teashops) { 7 teashops.forEach(function(teashopInstance) { 8 //what we fetch back from the db is wrong, so need to revert it here 9 var newLocation = {lng: teashopInstance.location.lat, lat: teashopInstance.location.lng}; 10 //only update the GeoPoint property for the model 11 teashopInstance.updateAttribute('location', newLocation, function(err, inst) { 12 if (err) 13 console.log('update attribute failed ', err); 14 else 15 console.log('updateAttribute successful'); 16 }); 17 }); 18 }); 19 } 20 21 findAndUpdate(); 22};
node .
For the above example, the model definition is as follows:
1{ 2 "name": "teashop", 3 "base": "PersistedModel", 4 "idInjection": true, 5 "options": { 6 "validateUpsert": true 7 }, 8 "properties": { 9 "name": { 10 "type": "string", 11 "default": "storename" 12 }, 13 "location": { 14 "type": "geopoint" 15 } 16 }, 17 "validations": [], 18 "relations": {}, 19 "acls": [], 20 "methods": {} 21}
If you have a local or remote MySQL instance and would like to use that to run the test suite, use the following command:
1MYSQL_HOST=<HOST> MYSQL_PORT=<PORT> MYSQL_USER=<USER> MYSQL_PASSWORD=<PASSWORD> MYSQL_DATABASE=<DATABASE> CI=true npm test
1SET MYSQL_HOST=<HOST> SET MYSQL_PORT=<PORT> SET MYSQL_USER=<USER> SET MYSQL_PASSWORD=<PASSWORD> SET MYSQL_DATABASE=<DATABASE> SET CI=true npm test
If you do not have a local MySQL instance, you can also run the test suite with very minimal requirements.
1source setup.sh <HOST> <PORT> <USER> <PASSWORD> <DATABASE>
where <HOST>
, <PORT>
, <USER>
, <PASSWORD>
and <DATABASE>
are optional parameters. The default values are localhost
, 3306
, root
, pass
and testdb
respectively.
1npm test
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 2/6 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More