Skip to content

Commit 02e8234

Browse files
committed
feat: add enum values to db specific configs
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 22caf56 commit 02e8234

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

lib/datasource.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
17331733
if (uniqueKeys.includes(propName)) {
17341734
schema.properties[propName]['index'] = {unique: true};
17351735
}
1736-
const dbSpecific = schema.properties[propName][dbType] = {
1736+
const dbSpecific = {
17371737
columnName: item.columnName,
17381738
dataType: item.dataType,
17391739
dataLength: item.dataLength,
@@ -1742,6 +1742,18 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
17421742
nullable: item.nullable,
17431743
generated: item.generated || false,
17441744
};
1745+
if (
1746+
item.dataType &&
1747+
item.dataType.toLowerCase().includes('enum')
1748+
) {
1749+
let enumItems = '';
1750+
const enumRemoved = item.dataType.toLowerCase().split('enum')[1];
1751+
const enumValues = enumRemoved.slice(1, -1).replaceAll('\'', '');
1752+
const enumValuesArray = enumValues.split(',');
1753+
enumValuesArray.forEach(item => { enumItems += `'${item}',`; });
1754+
dbSpecific.value = enumItems.toString();
1755+
}
1756+
schema.properties[propName][dbType] = dbSpecific;
17451757
// merge connector-specific properties
17461758
if (item[dbType]) {
17471759
for (const k in item[dbType]) {

test/discovery.test.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ describe('Memory connector with mocked discovery', function() {
3636
nullable: 0,
3737
generated: true,
3838
},
39+
{
40+
owner: 'STRONGLOOP',
41+
tableName: 'INVENTORY',
42+
columnName: 'PRODUCT_TYPE',
43+
dataType: "ENUM('IMPORTED', 'LOCAL')",
44+
nullable: 1,
45+
generated: false,
46+
},
3947
{
4048
owner: 'STRONGLOOP',
4149
tableName: 'INVENTORY',
@@ -84,7 +92,7 @@ describe('Memory connector with mocked discovery', function() {
8492
const s = schemas['STRONGLOOP.INVENTORY'];
8593
s.name.should.be.eql('Inventory');
8694
Object.keys(s.properties).should.be.eql(
87-
['productId', 'locationId', 'available', 'total'],
95+
['productId', 'productType', 'locationId', 'available', 'total'],
8896
);
8997
done();
9098
});
@@ -110,7 +118,7 @@ describe('Memory connector with mocked discovery', function() {
110118
const s = schemas['STRONGLOOP.INVENTORY'];
111119
s.name.should.be.eql('Inventory');
112120
Object.keys(s.properties).should.be.eql(
113-
['PRODUCT_ID', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
121+
['PRODUCT_ID', 'PRODUCT_TYPE', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
114122
);
115123
done();
116124
});
@@ -128,7 +136,7 @@ describe('Memory connector with mocked discovery', function() {
128136
const s = schemas['STRONGLOOP.INVENTORY'];
129137
s.name.should.be.eql('inventory');
130138
Object.keys(s.properties).should.be.eql(
131-
['product_id', 'location_id', 'available', 'total'],
139+
['product_id', 'product_type', 'location_id', 'available', 'total'],
132140
);
133141
done();
134142
});
@@ -142,7 +150,7 @@ describe('Memory connector with mocked discovery', function() {
142150
const s = schemas['STRONGLOOP.INVENTORY'];
143151
s.name.should.be.eql('INVENTORY');
144152
Object.keys(s.properties).should.be.eql(
145-
['PRODUCT_ID', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
153+
['PRODUCT_ID', 'PRODUCT_TYPE', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
146154
);
147155
done();
148156
});
@@ -202,7 +210,7 @@ describe('Memory connector with mocked discovery', function() {
202210
s.name.should.be.eql('Inventory');
203211

204212
Object.keys(s.properties).should.be.eql(
205-
['productId', 'locationId', 'available', 'total'],
213+
['productId', 'productType', 'locationId', 'available', 'total'],
206214
);
207215
done();
208216
})
@@ -235,13 +243,32 @@ describe('Memory connector with mocked discovery', function() {
235243
dataType: 'int',
236244
nullable: 1,
237245
generated: false,
246+
value: '\'imported\',\' local\','
238247
},
239248
precision: 10,
240249
required: false,
241250
scale: 0,
242251
type: undefined,
243252
generated: false,
244253
},
254+
productType: {
255+
type: undefined,
256+
required: false,
257+
jsonSchema: {nullable: true},
258+
length: undefined,
259+
precision: undefined,
260+
scale: undefined,
261+
generated: false,
262+
memory: {
263+
columnName: 'PRODUCT_TYPE',
264+
dataType: 'ENUM(\'IMPORTED\', \'LOCAL\')',
265+
dataLength: undefined,
266+
dataPrecision: undefined,
267+
dataScale: undefined,
268+
nullable: 1,
269+
generated: false,
270+
},
271+
},
245272
locationId: {
246273
length: 20,
247274
jsonSchema: {

0 commit comments

Comments
 (0)