Skip to content

Commit da6ecc9

Browse files
committed
feat: fetch index information for models
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent b604aa0 commit da6ecc9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/datasource.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
17161716

17171717
columns.forEach(function(item) {
17181718
const propName = nameMapper('column', item.columnName);
1719-
schema.properties[propName] = {
1719+
const propertyDetails = {
17201720
type: item.type,
17211721
required: !item.generated && (item.nullable === 'N' || item.nullable === 'NO' ||
17221722
item.nullable === 0 || item.nullable === false),
@@ -1727,12 +1727,25 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
17271727
scale: item.dataScale,
17281728
generated: item.generated || false,
17291729
};
1730+
if (
1731+
item.indexType === 'BTREE' &&
1732+
item.indexName !== 'PRIMARY' &&
1733+
!item.isForeignKey
1734+
) {
1735+
propertyDetails.index = {unique: true};
1736+
}
1737+
17301738
if (pks[item.columnName]) {
1731-
schema.properties[propName].id = pks[item.columnName];
1739+
propertyDetails[propName].id = pks[item.columnName];
17321740
}
1733-
if (uniqueKeys.includes(propName)) {
1734-
schema.properties[propName]['index'] = {unique: true};
1741+
if (
1742+
uniqueKeys.includes(propName) &&
1743+
propertyDetails[propName]['index'] === undefined
1744+
) {
1745+
propertyDetails[propName]['index'] = {unique: true};
17351746
}
1747+
1748+
schema.properties[propName] = propertyDetails;
17361749
const dbSpecific = schema.properties[propName][dbType] = {
17371750
columnName: item.columnName,
17381751
dataType: item.dataType,

test/discovery.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('Memory connector with mocked discovery', function() {
2929
owner: 'STRONGLOOP',
3030
tableName: 'INVENTORY',
3131
columnName: 'PRODUCT_ID',
32+
indexType: 'BTREE',
3233
dataType: 'varchar',
3334
dataLength: 20,
3435
dataPrecision: null,
@@ -281,6 +282,9 @@ describe('Memory connector with mocked discovery', function() {
281282
scale: null,
282283
type: undefined,
283284
generated: true,
285+
index: {
286+
unique: true,
287+
},
284288
},
285289
total: {
286290
length: null,

0 commit comments

Comments
 (0)