57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
||
|
const UserError = require('vn-loopback/util/user-error');
|
||
|
|
||
|
module.exports = Self => {
|
||
|
Self.remoteMethod('getEnumValues', {
|
||
|
description: 'Return enum values of column',
|
||
|
accessType: 'EXECUTE',
|
||
|
accepts: [
|
||
|
{
|
||
|
arg: 'schema',
|
||
|
type: 'string',
|
||
|
description: 'The schema of db',
|
||
|
required: true,
|
||
|
},
|
||
|
{
|
||
|
arg: 'table',
|
||
|
type: 'string',
|
||
|
description: 'The table of schema',
|
||
|
required: true,
|
||
|
},
|
||
|
{
|
||
|
arg: 'column',
|
||
|
type: 'string',
|
||
|
description: 'The column of table',
|
||
|
required: true,
|
||
|
},
|
||
|
],
|
||
|
returns: {
|
||
|
type: 'any',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: `/get-enum-values`,
|
||
|
verb: 'GET'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.getEnumValues = async(schema, table, column) => {
|
||
|
const stmt = new ParameterizedSQL(`
|
||
|
SELECT COLUMN_TYPE
|
||
|
FROM information_schema.COLUMNS
|
||
|
WHERE TABLE_SCHEMA = ?
|
||
|
AND TABLE_NAME = ?
|
||
|
AND COLUMN_NAME = ?
|
||
|
AND DATA_TYPE = 'enum';`,
|
||
|
[schema, table, column]);
|
||
|
|
||
|
const conn = Self.dataSource.connector;
|
||
|
const [result] = await conn.executeStmt(stmt);
|
||
|
|
||
|
if (!result) throw new UserError(`No results found`);
|
||
|
|
||
|
const regex = /'([^']*)'/g;
|
||
|
return result.COLUMN_TYPE.match(regex).map(match => match.slice(1, -1));
|
||
|
};
|
||
|
};
|