npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-connector-mysql-qxl

v5.4.3

Published

MySQL connector for loopback-datasource-juggler!!官方不改,只能自己改了(fix bit(1)=>boolean)

Downloads

13

Readme

loopback-connector-mysql

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.

Installation

In your application root directory, enter this command to install the connector:

npm 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.

Creating a MySQL data source

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:

"mydb": {
  "name": "mydb",
  "connector": "mysql",
  "host": "myserver",
  "port": 3306,
  "database": "mydb",
  "password": "mypassword",
  "user": "admin"
 }

Edit datasources.json to add any other additional properties that you require.

Properties

NOTE: In addition to these properties, you can use additional parameters supported by node-mysql.

Type mappings

See LoopBack types for details on LoopBack's data types.

LoopBack to MySQL types

MySQL to LoopBack types

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 Boolean
  • treatBIT1AsBit default true - treats BIT(1) as a Boolean instead of a Binary
  • treatTINYINT1AsTinyInt default true - treats TINYINT(1) as a Boolean instead of a Number

Using the datatype field/column option with MySQL

Use 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" %}

"locationId":{
    "type":"String",
    "required":true,
    "length":20,
    "mysql":
    {
        "columnName":"LOCATION_ID",
        "dataType":"VARCHAR",
        "dataLength":20,
        "nullable":"N"
    }
}

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:

  • Number
  • integer
  • tinyint
  • smallint
  • mediumint
  • int
  • bigint

Use the limit option to alter the display width. Example:

{ userName : {
    type: String,
    dataType: 'char',
    limit: 24
  }
}

Default Clause/Constant

Use the default property to have MySQL handle setting column DEFAULT value.

"status": {
  "type": "string",
  "mysql": {
    "default": "pending"
  }
},
"number": {
  "type": "number",
  "mysql": {
    "default": 256
  }
}

For the date or timestamp types use CURRENT_TIMESTAMP or now:

"last_modified": {
  "type": "date",
  "mysql": {
    "default":"CURRENT_TIMESTAMP"
  }
}

NOTE: The following column types do NOT supported MySQL Default Values:

  • BLOB
  • TEXT
  • GEOMETRY
  • JSON

Floating-point types

For Float and Double data types, use the precision and scale options to specify custom precision. Default is (16,8). For example:

{ average :
  { type: Number,
    dataType: 'float',
    precision: 20,
    scale: 4
  }
}

Fixed-point exact value types

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:

{ stdDev :
  { type: Number, 
    dataType: 'decimal',
    precision: 12,
    scale: 8
  }
}

Other types

Convert String / DataSource.Text / DataSource.JSON to the following MySQL types:

  • varchar
  • char
  • text
  • mediumtext
  • tinytext
  • longtext

Example: 

{ userName :
  { type: String,
    dataType: 'char',
    limit: 24
  }
}

Example: 

{ biography :
  { type: String,
  dataType: 'longtext'
  }
}

Convert JSON Date types to  datetime or timestamp

Example: 

{ startTime :
  { type: Date,
    dataType: 'timestamp'
  }
}

Enum

Enums are special. Create an Enum using Enum factory:

var MOOD = dataSource.EnumFactory('glad', 'sad', 'mad'); 
MOOD.SAD; // 'sad' 
MOOD(2); // 'sad' 
MOOD('SAD'); // 'sad' 
MOOD('sad'); // 'sad'
{ mood: { type: MOOD }}
{ choice: { type: dataSource.EnumFactory('yes', 'no', 'maybe'), null: false }}

Discovery and auto-migration

Model discovery

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.

Auto-migration

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.

Auto-migrate/Auto-update models with foreign keys

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

{
  "name": "Book",
  "base": "PersistedModel",
  "idInjection": false,
  "properties": {
    "bId": {
      "type": "number",
      "id": true,
      "required": true
    },
    "name": {
      "type": "string"
    },
    "isbn": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "author": {
      "type": "belongsTo",
      "model": "Author",
      "foreignKey": "authorId"
    }
  },
  "acls": [],
  "methods": {},
  "foreignKeys": {
    "authorId": {
      "name": "authorId",
      "foreignKey": "authorId",
      "entityKey": "aId",
      "entity": "Author",
      "onUpdate": "restrict",
      "onDelete": "restrict"
    }
  }
}
{
  "name": "Author",
  "base": "PersistedModel",
  "idInjection": false,
  "properties": {
    "aId": {
      "type": "number",
      "id": true,
      "required": true
    },
    "name": {
      "type": "string"
    },
    "dob": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

boot-script.js

module.exports = function(app) {
  var mysqlDs = app.dataSources.mysqlDS;
  var Book = app.models.Book;
  var Author = app.models.Author;

  // first autoupdate the `Author` model to avoid foreign key constraint failure
  mysqlDs.autoupdate('Author', function(err) {
    if (err) throw err;
    console.log('\nAutoupdated table `Author`.');

    mysqlDs.autoupdate('Book', function(err) {
      if (err) throw err;
      console.log('\nAutoupdated table `Book`.');
      // at this point the database table `Book` should have one foreign key `authorId` integrated
    });
  });
};

Breaking Changes with GeoPoint since 5.x

Prior to [email protected], 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.

  1. Create a boot script under server/boot/ directory with the following:
'use strict';
module.exports = function(app) {
  function findAndUpdate() {
    var teashop = app.models.teashop;
    //find all instances of the model we'd like to migrate
    teashop.find({}, function(err, teashops) {
      teashops.forEach(function(teashopInstance) {
        //what we fetch back from the db is wrong, so need to revert it here
        var newLocation = {lng: teashopInstance.location.lat, lat: teashopInstance.location.lng};
        //only update the GeoPoint property for the model
        teashopInstance.updateAttribute('location', newLocation, function(err, inst) {
          if (err)
            console.log('update attribute failed ', err);
          else
        console.log('updateAttribute successful');
        });
      });
    });
  }

  findAndUpdate();
};
  1. Run the boot script by simply running your application or node .

For the above example, the model definition is as follows:

{
  "name": "teashop",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "default": "storename"
    },
    "location": {
      "type": "geopoint"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

Running tests

Own instance

If you have a local or remote MySQL instance and would like to use that to run the test suite, use the following command:

  • Linux
MYSQL_HOST=<HOST> MYSQL_PORT=<PORT> MYSQL_USER=<USER> MYSQL_PASSWORD=<PASSWORD> MYSQL_DATABASE=<DATABASE> CI=true npm test
  • Windows
SET MYSQL_HOST=<HOST> SET MYSQL_PORT=<PORT> SET MYSQL_USER=<USER> SET MYSQL_PASSWORD=<PASSWORD> SET MYSQL_DATABASE=<DATABASE> SET CI=true npm test

Docker

If you do not have a local MySQL instance, you can also run the test suite with very minimal requirements.

  • Assuming you have Docker installed, run the following script which would spawn a MySQL instance on your local:
source 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.

  • Run the test:
npm test