Skip to content

Commit

Permalink
feat: fetch index information for models
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
  • Loading branch information
aaqilniz committed Nov 30, 2024
1 parent b604aa0 commit 521c794
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions test/discovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -281,6 +282,9 @@ describe('Memory connector with mocked discovery', function() {
scale: null,
type: undefined,
generated: true,
index: {
unique: true,
},
},
total: {
length: null,
Expand Down

0 comments on commit 521c794

Please sign in to comment.