{"id":13388760,"url":"https://github.com/loopbackio/loopback-connector-mysql","last_synced_at":"2025-12-17T21:26:08.672Z","repository":{"id":42658696,"uuid":"11557649","full_name":"loopbackio/loopback-connector-mysql","owner":"loopbackio","description":"Loopback Connector for MySQL","archived":false,"fork":false,"pushed_at":"2025-05-08T13:54:20.000Z","size":1328,"stargazers_count":125,"open_issues_count":25,"forks_count":182,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-05-08T14:37:29.424Z","etag":null,"topics":["hacktoberfest","loopback","loopback4","mysql","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loopbackio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-07-21T05:28:58.000Z","updated_at":"2025-05-08T13:28:30.000Z","dependencies_parsed_at":"2023-02-08T13:46:42.851Z","dependency_job_id":"617162c4-0087-423e-9edb-bfdb3a1efe7a","html_url":"https://github.com/loopbackio/loopback-connector-mysql","commit_stats":{"total_commits":542,"total_committers":80,"mean_commits":6.775,"dds":0.7878228782287823,"last_synced_commit":"d4a39f7ad2fb65de637bfc812de61836ab6d36c3"},"previous_names":["strongloop/loopback-connector-mysql"],"tags_count":74,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loopbackio","download_url":"https://codeload.github.com/loopbackio/loopback-connector-mysql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253734891,"owners_count":21955772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","loopback","loopback4","mysql","nodejs"],"created_at":"2024-07-30T13:00:53.669Z","updated_at":"2025-12-17T21:26:08.611Z","avatar_url":"https://github.com/loopbackio.png","language":"JavaScript","readme":"# loopback-connector-mysql\n\n[MySQL](https://www.mysql.com/) is a popular open-source relational database\nmanagement system (RDBMS). The `loopback-connector-mysql` module provides the\nMySQL connector module for the LoopBack framework.\n\n## Installation\n\nIn your application root directory, enter this command to install the connector:\n\n```sh\nnpm install loopback-connector-mysql --save\n```\n\n**Note**: Since `loopback-connector-mysql` v7.x.x, this MySQL connector has dropped support for MySQL 5.7 and requires MySQL 8.0+.\n\nThis installs the module from npm and adds it as a dependency to the\napplication's `package.json` file.\n\nIf you create a MySQL data source using the data source generator as described\nbelow, you don't have to do this, since the generator will run `npm install` for\nyou.\n\n## Creating a MySQL data source\n\nFor LoopBack 4 users, use the LoopBack 4\n[Command-line interface](https://loopback.io/doc/en/lb4/Command-line-interface.html)\nto generate a DataSource with MySQL connector to your LB4 application. Run\n[`lb4 datasource`](https://loopback.io/doc/en/lb4/DataSource-generator.html), it\nwill prompt for configurations such as host, post, etc. that are required to\nconnect to a MySQL database.\n\nAfter setting it up, the configuration can be found under\n`src/datasources/\u003cDataSourceName\u003e.datasource.ts`, which would look like this:\n\n```ts\nconst config = {\n  name: 'db',\n  connector: 'mysql',\n  url: '',\n  host: 'localhost',\n  port: 3306,\n  user: 'user',\n  password: 'pass',\n  database: 'testdb',\n};\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\nUse\nthe [Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to\nadd a MySQL data source to your application.  \nThe generator will prompt for the database server hostname, port, and other\nsettings required to connect to a MySQL database. It will also run the\n`npm install` command above for you.\n\nThe entry in the application's `/server/datasources.json` will look like this:\n\n```javascript\n\"mydb\": {\n  \"name\": \"mydb\",\n  \"connector\": \"mysql\",\n  \"host\": \"myserver\",\n  \"port\": 3306,\n  \"database\": \"mydb\",\n  \"password\": \"mypassword\",\n  \"user\": \"admin\"\n }\n```\n\n\u003c/details\u003e\n\nEdit `\u003cDataSourceName\u003e.datasources.ts` to add any other additional properties\nthat you require.\n\n### Properties\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth width=\"150\"\u003eProperty\u003c/th\u003e\n      \u003cth width=\"80\"\u003eType\u003c/th\u003e\n      \u003cth\u003eDescription\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003ecollation\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eDetermines the charset for the connection.  Default is utf8_general_ci.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003econnector\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eConnector name, either “loopback-connector-mysql” or “mysql”.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003econnectionLimit\u003c/td\u003e\n      \u003ctd\u003eNumber\u003c/td\u003e\n      \u003ctd\u003eThe maximum number of connections to create at once.  Default is 10.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003edatabase\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eDatabase name\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003edebug\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eIf true, turn on verbose mode to debug database queries and lifecycle.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003ehost\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eDatabase host name\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003epassword\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003ePassword to connect to database\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eport\u003c/td\u003e\n      \u003ctd\u003eNumber\u003c/td\u003e\n      \u003ctd\u003eDatabase TCP port\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003esocketPath\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eThe path to a unix domain socket to connect to. When used host and port are ignored.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003esupportBigNumbers\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eEnable this option to deal with big numbers (BIGINT and DECIMAL columns) in the database. Default is false.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003etimeZone\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eThe timezone used to store local dates.  Default is ‘local’.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eurl\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eConnection URL of form \u003ccode\u003emysql://user:password@host/db\u003c/code\u003e.  Overrides other connection settings.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eusername\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eUsername to connect to database\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eallowExtendedOperators\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eSet to \u003ccode\u003etrue\u003c/code\u003e to enable MySQL-specific operators\n          such as \u003ccode\u003ematch\u003c/code\u003e. Learn more in\n          \u003ca href=\"#extended-operators\"\u003eExtended operators\u003c/a\u003e below.\n      \u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n**NOTE**: In addition to these properties, you can use additional parameters\nsupported by [`node-mysql`](https://github.com/felixge/node-mysql).\n\n## Type mappings\n\nSee [LoopBack 4 types](http://loopback.io/doc/en/lb4/LoopBack-types.html) (or [LoopBack 3 types](http://loopback.io/doc/en/lb3/LoopBack-types.html)) for\ndetails on LoopBack's data types.\n\n### LoopBack to MySQL types\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth width=\"450\"\u003eLoopBack Type\u003c/th\u003e\n      \u003cth width=\"450\"\u003eMySQL Type\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e    \n    \u003ctr\u003e\n      \u003ctd\u003eString/JSON\u003c/td\u003e\n      \u003ctd\u003eVARCHAR\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eText\u003c/td\u003e\n      \u003ctd\u003eTEXT\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eNumber\u003c/td\u003e\n      \u003ctd\u003eINT\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eDate\u003c/td\u003e\n      \u003ctd\u003eDATETIME\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eTINYINT(1)\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ca href=\"http://apidocs.strongloop.com/loopback-datasource-juggler/#geopoint\" class=\"external-link\"\u003eGeoPoint\u003c/a\u003e object\u003c/td\u003e\n      \u003ctd\u003ePOINT\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eCustom Enum type\u003cbr\u003e(See \u003ca href=\"#enum\"\u003eEnum\u003c/a\u003e below)\u003c/td\u003e\n      \u003ctd\u003eENUM\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n### MySQL to LoopBack types\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth width=\"450\"\u003eMySQL Type\u003c/th\u003e\n      \u003cth width=\"450\"\u003eLoopBack Type\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eCHAR\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eBIT(1)\u003cbr\u003eCHAR(1)\u003cbr\u003eTINYINT(1)\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eVARCHAR\u003cbr\u003eTINYTEXT\u003cbr\u003eMEDIUMTEXT\u003cbr\u003eLONGTEXT\u003cbr\u003eTEXT\u003cbr\u003eENUM\u003cbr\u003eSET\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eTINYBLOB\u003cbr\u003eMEDIUMBLOB\u003cbr\u003eLONGBLOB\u003cbr\u003eBLOB\u003cbr\u003eBINARY\u003cbr\u003eVARBINARY\u003cbr\u003eBIT\u003c/td\u003e\n      \u003ctd\u003eNode.js \u003ca href=\"http://nodejs.org/api/buffer.html\"\u003eBuffer object\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eTINYINT\u003cbr\u003eSMALLINT\u003cbr\u003eINT\u003cbr\u003eMEDIUMINT\u003cbr\u003eYEAR\u003cbr\u003eFLOAT\u003cbr\u003eDOUBLE\u003cbr\u003eNUMERIC\u003cbr\u003eDECIMAL\u003c/td\u003e\n      \u003ctd\u003e\n        \u003cp\u003eNumber\u003cbr\u003eFor FLOAT and DOUBLE, see \u003ca href=\"#floating-point-types\"\u003eFloating-point types\u003c/a\u003e. \u003c/p\u003e\n        \u003cp\u003eFor NUMERIC and DECIMAL, see \u003ca href=\"MySQL-connector.html\"\u003eFixed-point exact value types\u003c/a\u003e\u003c/p\u003e\n      \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eDATE\u003cbr\u003eTIMESTAMP\u003cbr\u003eDATETIME\u003c/td\u003e\n      \u003ctd\u003eDate\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n_NOTE_ as of v3.0.0 of MySQL Connector, the following flags were introduced:\n\n- `treatCHAR1AsString` default `false` - treats CHAR(1) as a String instead of a\n  Boolean\n- `treatBIT1AsBit` default `true` - treats BIT(1) as a Boolean instead of a\n  Binary\n- `treatTINYINT1AsTinyInt` default `true` - treats TINYINT(1) as a Boolean\n  instead of a Number\n\n## Data mapping properties\n\nExcept the common database-specific properties we introduce in [How LoopBack Models Map To Database Tables/Collections](https://loopback.io/doc/en/lb4/Model.html#how-loopback-models-map-to-database-tablescollections), the following are more detailed examples and MySQL-specific settings.\n\n### Table/Column Names\n\nBesides the basic LoopBack types, as we introduced above, you can also specify\nadditional MySQL-specific properties for a LoopBack model. It would be mapped to\nthe database.\n\nUse the `mysql.\u003cproperty\u003e` in the model definition or the property definition to\nconfigure the table/column definition.\n\nFor example, the following settings would allow you to have custom table name\n(`Custom_User`) and column name (`custom_id` and `custom_name`). Such mapping is\nuseful when you'd like to have different table/column names from the model:\n\n{% include code-caption.html content=\"user.model.ts\" %}\n\n```ts\n@model({\n  settings: { mysql: { schema: 'testdb', table: 'Custom_User'} },\n})\nexport class User extends Entity {\n  @property({\n    type: 'number',\n    required: true,\n    id: true,\n    mysql: {\n      columnName: 'custom_id',\n    },\n  })\n  id: number;\n\n  @property({\n    type: 'string',\n    mysql: {\n      columnName: 'custom_name',\n    },\n  })\n  name?: string;\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\n```javascript\n{\n  \"name\": \"User\",\n  \"options\": {\n    \"mysql\": {\n      \"schema\": \"testdb\",\n      \"table\": \"Custom_User\"\n    }\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\",\n      \"required\": true,\n      \"mysql\": {\n        \"columnName\": \"custom_id\",\n      }\n    },\n    \"name\": {\n      \"type\": \"String\",\n      \"mysql\": {\n        \"columnName\": \"custom_name\",\n      }\n    },\n  }\n}\n```\n\n\u003c/details\u003e\n\n### Numeric Types\n\nExcept the names, you can also use the dataType column/property attribute to\nspecify what MySQL column type to use. The following MySQL type-dataType\ncombinations are supported:\n\n- number\n- integer\n- tinyint\n- smallint\n- mediumint\n- int\n- bigint\n- float\n- double\n- decimal\n\nThe following examples will be in LoopBack 4 style, but it's the same if you\nprovide `mysql.\u003cproperty\u003e` to the LB3 property definition.\n\n#### Floating-point types\n\nFor Float and Double data types, use the `precision` and `scale` options to\nspecify custom precision. Default is (16,8).\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eExample\u003c/strong\u003e\u003c/summary\u003e\n\n```ts\n@property({\n  type: 'Number',\n  mysql: {\n    dataType: 'float',\n    precision: 20,\n    scale: 4\n  }\n})\nprice: Number;\n```\n\n\u003c/details\u003e\n\n#### Fixed-point exact value types\n\nFor Decimal and Numeric types, use the `precision` and `scale` options to\nspecify custom precision. Default is (9,2). These aren't likely to function as\ntrue fixed-point.\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eExample\u003c/strong\u003e\u003c/summary\u003e\n\n```ts\n@property({\n  type: 'Number',\n  mysql: {\n    dataType: 'decimal',\n    precision: 12,\n    scale: 8\n  }\n})\nprice: Number;\n```\n\n\u003c/details\u003e\n\n### Text types\n\nConvert String / DataSource.Text / DataSource.JSON to the following MySQL types:\n\n- varchar\n- char\n- text\n- mediumtext\n- tinytext\n- longtext\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eExample\u003c/strong\u003e\u003c/summary\u003e\n\n```ts\n@property({\n  type: 'String',\n  mysql: {\n    dataType: 'char',\n    dataLength: 24 // limits the property length\n  },\n})\nuserName: String;\n```\n\n\u003c/details\u003e\n\n### Dat types\n\nConvert JSON Date types to datetime or timestamp.\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eExample\u003c/strong\u003e\u003c/summary\u003e\n\n```ts\n@property({\n  type: 'Date',\n  mysql: {\n    dataType: 'timestamp',\n  },\n})\nstartTime: Date;\n```\n\n\u003c/details\u003e\n\n### Enum\n\nSee the [Model ENUM property](https://loopback.io/doc/en/lb4/Model.html#enum-property) for details. \n\n### Default Clause/Constant\n\nUse the `default` and `dataType` properties to have MySQL handle **setting column `DEFAULT` value**.\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eExample\u003c/strong\u003e\u003c/summary\u003e\n\n```ts\n@property({\n  type: 'String',\n  mysql: {\n    dataType: 'varchar',\n    default: 'pending'\n  }\n})\nstatus: String;\n\n@property({\n  type: 'Number',\n  mysql: {\n    dataType: 'int',\n    default: 42\n  }\n})\nmaxDays: Number;\n\n@property({\n  type: 'boolean',\n  mysql: {\n    dataType: 'tinyint',\n    default: 1\n  }\n})\nisDone: Boolean;\n```\n\n\u003c/details\u003e\n\nFor the date or timestamp types use `CURRENT_TIMESTAMP` or `now`.\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eExample\u003c/strong\u003e\u003c/summary\u003e\n\n```ts\n@property({\n  type: 'Date',\n  mysql: {\n    dataType: 'datetime',\n    default: 'CURRENT_TIMESTAMP'\n  }\n})\nlast_modified: Date;\n```\n\n\u003c/details\u003e\n\n**NOTE**: The following column types do **NOT** supported\n[MySQL Default Values](https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html):\n\n- BLOB\n- TEXT\n- GEOMETRY\n- JSON\n\n## Extended operators\nMySQL connector supports the following MySQL-specific operators:\n- [`match`](#operator-match)\nPlease note extended operators are disabled by default, you must enable\nthem at datasource level or model level by setting `allowExtendedOperators` to\n`true`.\n### Operator `match`\nThe `match` operator allows you to perform a full text search using the [MATCH() .. AGAINST()](https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html) operator in MySQL.\n\nThree different modes of the `MATCH` clause are also available in the form of operators - \n\n- `matchbool` for [Boolean Full Text Search](https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html)\n- `matchnl` for [Natural Language Full Text Search](https://dev.mysql.com/doc/refman/8.0/en/fulltext-natural-language.html)\n- `matchqe` for [Full-Text Searches with Query Expansion](https://dev.mysql.com/doc/refman/8.0/en/fulltext-query-expansion.html)\n- `matchnlqe` for [Full-Text Searches with Query Expansion](https://dev.mysql.com/doc/refman/8.0/en/fulltext-query-expansion.html) with the `IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION` modifier.\n\nBy default, the `match` operator works in Natural Language mode.\n\n**Note** The fields you are querying must be setup with a `FULLTEXT` index to perform full text search on them.\nAssuming a model such as this:\n```ts\n@model({\n  settings: {\n    allowExtendedOperators: true,\n  }\n})\nclass Post {\n  @property({\n    type: 'string',\n    mysql: {\n      index: {\n        kind: 'FULLTEXT'\n      }\n    },\n  })\n  content: string;\n}\n```\nYou can query the content field as follows:\n```ts\nconst posts = await postRepository.find({\n  where: {\n    {\n      content: {match: 'someString'},\n    }\n  }\n});\n```\n\n## Discovery and auto-migration\n\n### Model discovery\n\nThe MySQL connector supports _model discovery_ that enables you to create\nLoopBack models based on an existing database schema. Once you defined your\ndatasource:\n\n- LoopBack 4 users could use the commend\n  [`lb4 discover`](https://loopback.io/doc/en/lb4/Discovering-models.html) to\n  discover models.\n- For LB3 users, please check\n  [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).\n  (See\n  [database discovery API](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels)\n  for related APIs information)\n\n### Auto-migration\n\nThe MySQL connector also supports _auto-migration_ that enables you to create a\ndatabase schema from LoopBack models. For example, based on the following model,\nthe auto-migration method would create/alter existing `Customer` table in the\ndatabase. Table `Customer` would have two columns: `name` and `id`, where `id`\nis also the primary key that has `auto_increment` set as it has definition of\n`type: 'Number'` and `generated: true`:\n\n```ts\n@model()\nexport class Customer extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true,\n  })\n  id: number;\n\n  @property({\n    type: 'string',\n  })\n  name: string;\n}\n```\n\nMoreover, additional MySQL-specific properties mentioned in the\n[Data mapping properties](#data-mapping-properties) section work with\nauto-migration as well.\n\n#### Auto-generated ids\n\nFor now LoopBack MySQL connector only supports auto-generated id\n(`generated: true`) for integer type as for MySQL, the default id type is\n_integer_. If you'd like to use other types such as string (uuid) as the id\ntype, you can:\n\n- use uuid that is **generated by your LB application** by setting\n  [`defaultFn: uuid`](https://loopback.io/doc/en/lb4/Model.html#property-decorator).\n\n```ts\n  @property({\n    id: true,\n    type: 'string'\n    defaultFn: 'uuidv4',\n    // generated: true,  -\u003e not needed\n  })\n  id: string;\n```\n\n- Alter the table in your database to use a certain function if you prefer\n  having **the database to generate the value**.\n\n```ts\n  @property({\n    id: true,\n    type: 'string'\n    generated: true,  // to indicate the value generates by the db\n    useDefaultIdType: false,  // needed\n  })\n  id: string;\n```\n\n#### Auto-migrate/Auto-update models with foreign keys\n\nForeign key constraints can be defined in the model definition.\n\n**Note**: The order of table creation is important. A referenced table must\nexist before creating a foreign key constraint. The order can be specified \nusing the optional \u003ca href=\"https://loopback.io/doc/en/lb4/apidocs.repository.schemamigrationoptions.html\"\u003e`SchemaMigrationOptions`\u003c/a\u003e argument of `migrateSchema`:\n\n```\nawait app.migrateSchema({\n\tmodels: [ 'Customer', 'Order' ]\n});\n```\n\nDefine your models and the foreign key constraints as follows:\n\n{% include code-caption.html content=\"customer.model.ts\" %}\n\n```ts\n@model()\nexport class Customer extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true,\n  })\n  id: number;\n\n  @property({\n    type: 'string',\n  })\n  name: string;\n}\n```\n\n`order.model.ts`:\n\n```ts\n@model({\n  settings: {\n    foreignKeys: {\n      fk_order_customerId: {\n        name: 'fk_order_customerId',\n        entity: 'Customer',\n        entityKey: 'id',\n        foreignKey: 'customerId',\n      },\n    },\n  })\nexport class Order extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true\n  })\n  id: number;\n\n  @property({\n    type: 'string'\n  })\n  name: string;\n\n  @property({\n    type: 'Number'\n  })\n  customerId: number;\n}\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\n```json\n({\n  \"name\": \"Customer\",\n  \"options\": {\n    \"idInjection\": false\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\",\n      \"id\": 1\n    },\n    \"name\": {\n      \"type\": \"String\",\n      \"required\": false\n    }\n  }\n},\n{\n  \"name\": \"Order\",\n  \"options\": {\n    \"idInjection\": false,\n    \"foreignKeys\": {\n      \"fk_order_customerId\": {\n        \"name\": \"fk_order_customerId\",\n        \"entity\": \"Customer\",\n        \"entityKey\": \"id\",\n        \"foreignKey\": \"customerId\"\n      }\n    }\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\"\n      \"id\": 1\n    },\n    \"customerId\": {\n      \"type\": \"Number\"\n    },\n    \"description\": {\n      \"type\": \"String\",\n      \"required\": false\n    }\n  }\n})\n```\n\n\u003c/details\u003e\n\nMySQL handles the foreign key integrity by the referential action specified by\n`ON UPDATE` and `ON DELETE`. You can specify which referential actions the\nforeign key follows in the model definition upon auto-migrate or auto-update\noperation. Both `onDelete` and `onUpdate` default to `restrict`.\n\nTake the example we showed above, let's add the referential action to the\nforeign key `customerId`:\n\n```ts\n@model({\n  settings: {\n    foreignKeys: {\n      fk_order_customerId: {\n        name: 'fk_order_customerId',\n        entity: 'Customer',\n        entityKey: 'id',\n        foreignKey: 'customerId',\n        onUpdate: 'restrict', // restrict|cascade|set null|no action|set default\n        onDelete: 'cascade'   // restrict|cascade|set null|no action|set default\n      },\n    },\n  })\nexport class Order extends Entity {\n...\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\n**model-definiton.json**\n\n```json\n{\n  \"name\": \"Customer\",\n  \"options\": {\n    \"idInjection\": false\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\",\n      \"id\": 1\n    },\n    \"name\": {\n      \"type\": \"String\",\n      \"required\": false\n    }\n  }\n},\n{\n  \"name\": \"Order\",\n  \"options\": {\n    \"idInjection\": false,\n    \"foreignKeys\": {\n      \"fk_order_customerId\": {\n        \"name\": \"fk_order_customerId\",\n        \"entity\": \"Customer\",\n        \"entityKey\": \"id\",\n        \"foreignKey\": \"customerId\",\n        \"onUpdate\": \"restrict\",\n        \"onDelete\": \"cascade\"\n      }\n    }\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\"\n      \"id\": 1\n    },\n    \"customerId\": {\n      \"type\": \"Number\"\n    },\n    \"description\": {\n      \"type\": \"String\",\n      \"required\": false\n    }\n  }\n}\n```\n\n**boot-script.js**\n\n```js\nmodule.exports = function (app) {\n  var mysqlDs = app.dataSources.mysqlDS;\n  var Book = app.models.Order;\n  var Author = app.models.Customer;\n\n  // first autoupdate the `Customer` model to avoid foreign key constraint failure\n  mysqlDs.autoupdate('Customer', function (err) {\n    if (err) throw err;\n    console.log('\\nAutoupdated table `Customer`.');\n\n    mysqlDs.autoupdate('Order', function (err) {\n      if (err) throw err;\n      console.log('\\nAutoupdated table `Order`.');\n      // at this point the database table `Order` should have one foreign key `customerId` integrated\n    });\n  });\n};\n```\n\n\u003c/details\u003e\n\n#### Breaking Changes with GeoPoint since 5.x\n\nPrior to `loopback-connector-mysql@5.x`, MySQL connector was saving and loading\nGeoPoint properties from the MySQL database in reverse. MySQL expects values to\nbe `POINT(X, Y)` or `POINT(lng, lat)`, but the connector was saving them in the\nopposite order(i.e. `POINT(lat,lng)`).\n\nUse the `geopoint` type to achieve so:\n\n```ts\n  @property({\n    type: 'geopoint'\n  })\n  name: GeoPoint;\n```\n\nIf you have an application with a model that has a GeoPoint property using\nprevious versions of this connector, you can migrate your models using the\nfollowing programmatic approach:\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eClick here to expand\u003c/strong\u003e\u003c/summary\u003e\n\n**NOTE** Please back up the database tables that have your application data\nbefore performing any of the steps.\n\n1. Create a boot script under `server/boot/` directory with the following:\n\n```js\n'use strict';\nmodule.exports = function (app) {\n  function findAndUpdate() {\n    var teashop = app.models.teashop;\n    //find all instances of the model we'd like to migrate\n    teashop.find({}, function (err, teashops) {\n      teashops.forEach(function (teashopInstance) {\n        //what we fetch back from the db is wrong, so need to revert it here\n        var newLocation = {\n          lng: teashopInstance.location.lat,\n          lat: teashopInstance.location.lng,\n        };\n        //only update the GeoPoint property for the model\n        teashopInstance.updateAttribute('location', newLocation, function (\n          err,\n          inst,\n        ) {\n          if (err) console.log('update attribute failed', err);\n          else console.log('updateAttribute successful');\n        });\n      });\n    });\n  }\n\n  findAndUpdate();\n};\n```\n\n2. Run the boot script by simply running your application or `node .`\n\nFor the above example, the model definition is as follows:\n\n```json\n{\n  \"name\": \"teashop\",\n  \"base\": \"PersistedModel\",\n  \"idInjection\": true,\n  \"options\": {\n    \"validateUpsert\": true\n  },\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\",\n      \"default\": \"storename\"\n    },\n    \"location\": {\n      \"type\": \"geopoint\"\n    }\n  },\n  \"validations\": [],\n  \"relations\": {},\n  \"acls\": [],\n  \"methods\": {}\n}\n```\n\n\u003c/details\u003e\n\n## Running tests\n\n### Own instance\n\nIf you have a local or remote MySQL instance and would like to use that to run\nthe test suite, use the following command:\n\n- Linux\n\n```bash\nMYSQL_HOST=\u003cHOST\u003e MYSQL_PORT=\u003cPORT\u003e MYSQL_USER=\u003cUSER\u003e MYSQL_PASSWORD=\u003cPASSWORD\u003e MYSQL_DATABASE=\u003cDATABASE\u003e CI=true npm test\n```\n\n- Windows\n\n```bash\nSET MYSQL_HOST=\u003cHOST\u003e SET MYSQL_PORT=\u003cPORT\u003e SET MYSQL_USER=\u003cUSER\u003e SET MYSQL_PASSWORD=\u003cPASSWORD\u003e SET MYSQL_DATABASE=\u003cDATABASE\u003e SET CI=true npm test\n```\n\n### Docker\n\nIf you do not have a local MySQL instance, you can also run the test suite with\nvery minimal requirements.\n\n- Assuming you have [Docker](https://docs.docker.com/engine/installation/)\n  installed, run the following script which would spawn a MySQL instance on your\n  local:\n\n```bash\nsource setup.sh \u003cHOST\u003e \u003cPORT\u003e \u003cUSER\u003e \u003cPASSWORD\u003e \u003cDATABASE\u003e\n```\n\nwhere `\u003cHOST\u003e`, `\u003cPORT\u003e`, `\u003cUSER\u003e`, `\u003cPASSWORD\u003e` and `\u003cDATABASE\u003e` are optional\nparameters. The default values are `localhost`, `3306`, `root`, `pass` and\n`testdb` respectively.\n\n- Run the test:\n\n```bash\nnpm test\n```\n","funding_links":[],"categories":["Connectors"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floopbackio%2Floopback-connector-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floopbackio%2Floopback-connector-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floopbackio%2Floopback-connector-mysql/lists"}