From 521c79476a00a1f6586645d6c1641a4399c2a933 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 13 Oct 2024 23:16:46 +0500 Subject: [PATCH] feat: fetch index information for models Signed-off-by: Muhammad Aaqil --- lib/datasource.js | 21 +++++++++++++++++---- test/discovery.test.js | 4 ++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index f35a7774c..4b5061534 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1716,7 +1716,7 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { columns.forEach(function(item) { const propName = nameMapper('column', item.columnName); - schema.properties[propName] = { + const propertyDetails = { type: item.type, required: !item.generated && (item.nullable === 'N' || item.nullable === 'NO' || item.nullable === 0 || item.nullable === false), @@ -1727,12 +1727,25 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { scale: item.dataScale, generated: item.generated || false, }; + if ( + item.indexType === 'BTREE' && + item.indexName !== 'PRIMARY' && + !item.isForeignKey + ) { + propertyDetails.index = {unique: true}; + } + if (pks[item.columnName]) { - schema.properties[propName].id = pks[item.columnName]; + propertyDetails.id = pks[item.columnName]; } - if (uniqueKeys.includes(propName)) { - schema.properties[propName]['index'] = {unique: true}; + if ( + uniqueKeys.includes(propName) && + propertyDetails['index'] === undefined + ) { + propertyDetails['index'] = {unique: true}; } + + schema.properties[propName] = propertyDetails; const dbSpecific = schema.properties[propName][dbType] = { columnName: item.columnName, dataType: item.dataType, diff --git a/test/discovery.test.js b/test/discovery.test.js index f537b9ab5..6ab322a6a 100644 --- a/test/discovery.test.js +++ b/test/discovery.test.js @@ -29,6 +29,7 @@ describe('Memory connector with mocked discovery', function() { owner: 'STRONGLOOP', tableName: 'INVENTORY', columnName: 'PRODUCT_ID', + indexType: 'BTREE', dataType: 'varchar', dataLength: 20, dataPrecision: null, @@ -281,6 +282,9 @@ describe('Memory connector with mocked discovery', function() { scale: null, type: undefined, generated: true, + index: { + unique: true, + }, }, total: { length: null,