feat: query to fetch unique columns

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
This commit is contained in:
Muhammad Aaqil 2024-04-14 15:55:45 +05:00 committed by Samarpan Bhattacharya
parent 3596be1fa8
commit 2386245e15
5 changed files with 60 additions and 24 deletions

View File

@ -301,6 +301,29 @@ function mixinDiscovery(MySQL, mysql) {
return sql;
};
/**
* Discover unique keys for a given table
* @param {String} table The table name
* @param {Object} options The options for discovery
*/
/*!
* Retrieves a list of column names that have unique key index
* @param schema
* @param table
* @returns {string}
*/
MySQL.prototype.buildQueryUniqueKeys = function(schema, table) {
const sql = 'SELECT Column_name AS "columnName",' +
' table_schema AS "owner",' +
' table_name AS "tableName"' +
' FROM Information_schema.statistics' +
' WHERE Table_schema = ' + mysql.escape(schema) +
' AND Table_name = ' + mysql.escape(table) +
' AND Non_unique = 0 AND Index_name <> \'PRIMARY\';';
return sql;
};
/**
* Discover foreign keys that reference to the primary key of this table
* @param {String} table The table name

41
package-lock.json generated
View File

@ -22,7 +22,7 @@
"@commitlint/config-conventional": "^19.6.0",
"eslint": "^8.57.1",
"eslint-config-loopback": "^13.1.0",
"loopback-datasource-juggler": "^5.1.3",
"loopback-datasource-juggler": "^5.1.4",
"mocha": "^11.1.0",
"rc": "^1.2.8",
"should": "^13.2.3",
@ -1632,9 +1632,9 @@
}
},
"node_modules/inflection": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/inflection/-/inflection-3.0.0.tgz",
"integrity": "sha512-1zEJU1l19SgJlmwqsEyFTbScw/tkMHFenUo//Y0i+XEP83gDFdMvPizAD/WGcE+l1ku12PcTVHQhO6g5E0UCMw==",
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/inflection/-/inflection-3.0.2.tgz",
"integrity": "sha512-+Bg3+kg+J6JUWn8J6bzFmOWkTQ6L/NHfDRSYU+EVvuKHDxUDHAXgqixHfVlzuBQaPOTac8hn43aPhMNk6rMe3g==",
"dev": true,
"engines": {
"node": ">=18.0.0"
@ -1999,42 +1999,40 @@
"integrity": "sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q=="
},
"node_modules/loopback-connector": {
"version": "6.1.12",
"resolved": "https://registry.npmjs.org/loopback-connector/-/loopback-connector-6.1.12.tgz",
"integrity": "sha512-WO3FVwpbabPfe1h7SmSyB6rBySgScN9mQ0M0GwN8TWbA+0HcCP9DWFiXYPHvS4v7QXOkey62sYVW+7M1l9zkNQ==",
"license": "MIT",
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/loopback-connector/-/loopback-connector-6.2.0.tgz",
"integrity": "sha512-mCYZJCdp0K4tQA5U2XJW1HgWZLCnQ8n/ss+hrLLsqKxBLCal9T3LarBFyFlLUwx4IidKJdQkndE1NUwk948VIA==",
"dependencies": {
"async": "^3.2.6",
"bluebird": "^3.7.2",
"debug": "^4.3.7",
"debug": "^4.4.0",
"msgpack5": "^4.5.1",
"strong-globalize": "^6.0.6",
"uuid": "^11.0.3"
"uuid": "^11.0.5"
},
"engines": {
"node": ">=18"
}
},
"node_modules/loopback-datasource-juggler": {
"version": "5.1.3",
"resolved": "https://registry.npmjs.org/loopback-datasource-juggler/-/loopback-datasource-juggler-5.1.3.tgz",
"integrity": "sha512-r+fVHyziu1LhiuVDUOMF2C8vqBuHSTJvRlvGHGLMFZI/4XsjUciAFil7r1xSFepGAfBDWZEZUya67m0lb05wXQ==",
"version": "5.1.4",
"resolved": "https://registry.npmjs.org/loopback-datasource-juggler/-/loopback-datasource-juggler-5.1.4.tgz",
"integrity": "sha512-DI22buY0aorxC2lgXuMaWwBlN4OSJbd7X0jAtIk1Sv8AUBYBd1izlT/po5B4vkbqGo5btkEjHRFG/Rc0F0swzg==",
"dev": true,
"license": "MIT",
"dependencies": {
"async": "^3.2.6",
"change-case": "^4.1.2",
"debug": "^4.3.7",
"debug": "^4.4.0",
"depd": "^2.0.0",
"inflection": "^3.0.0",
"inflection": "^3.0.2",
"lodash": "^4.17.21",
"loopback-connector": "^6.1.12",
"loopback-connector": "^6.2.0",
"minimatch": "^10.0.1",
"nanoid": "^3.3.8",
"neotraverse": "^0.6.18",
"qs": "^6.13.1",
"strong-globalize": "^6.0.6",
"uuid": "^11.0.3"
"uuid": "^11.0.5"
},
"engines": {
"node": ">=18"
@ -3421,14 +3419,13 @@
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
},
"node_modules/uuid": {
"version": "11.0.3",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-11.0.3.tgz",
"integrity": "sha512-d0z310fCWv5dJwnX1Y/MncBAqGMKEzlBb1AOf7z9K8ALnd0utBX/msg/fA0+sbyN1ihbMsLhrBlnl1ak7Wa0rg==",
"version": "11.0.5",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-11.0.5.tgz",
"integrity": "sha512-508e6IcKLrhxKdBbcA2b4KQZlLVp2+J5UwQ6F7Drckkc5N9ZJwFa4TgWtsww9UG8fGHbm6gbV19TdM5pQ4GaIA==",
"funding": [
"https://github.com/sponsors/broofa",
"https://github.com/sponsors/ctavan"
],
"license": "MIT",
"bin": {
"uuid": "dist/esm/bin/uuid"
}

View File

@ -33,7 +33,7 @@
"@commitlint/config-conventional": "^19.6.0",
"eslint": "^8.57.1",
"eslint-config-loopback": "^13.1.0",
"loopback-datasource-juggler": "^5.1.3",
"loopback-datasource-juggler": "^5.1.4",
"mocha": "^11.1.0",
"rc": "^1.2.8",
"should": "^13.2.3",

View File

@ -283,6 +283,21 @@ describe('Discover LDL schema from a table', function() {
});
});
describe('Discover unique properties', function() {
let schema;
before(function(done) {
db.discoverSchema('CUSTOMER', {owner: 'STRONGLOOP'}, function(err, schema_) {
schema = schema_;
done(err);
});
});
it('should validate unique key for customer', function() {
assert.ok(/STRONGLOOP/i.test(schema.options.mysql.schema));
assert.strictEqual(schema.options.mysql.table, 'CUSTOMER');
assert.strictEqual(schema.properties.email.index.unique, true);
});
});
describe('Discover and handle enum', function() {
let schema;
before(function(done) {

View File

@ -39,7 +39,8 @@ CREATE TABLE `CUSTOMER` (
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
ALTER TABLE `CUSTOMER`
ADD COLUMN `email` VARCHAR(100) UNIQUE;
--
-- Dumping data for table `CUSTOMER`
--