Skip to content

Commit

Permalink
feat: add method for mode discovery
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Mar 9, 2024
1 parent ae3cf25 commit 13bf61a
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,15 @@ DataSource.prototype.setup = function(dsName, settings) {
debug('Connection fails: %s\nIt will be retried for the next request.', err);
} else {
g.error('Connection fails: %s\nIt will be retried for the next request.', err);
this.emit('error', err);
if (settings.catchFailure) {
try {
this.emit('error', err);
} catch (error) {
console.log(error);
}
} else {
this.emit('error', err);
}
}
} else {
// Either lazyConnect or connector initialize() defers the connection
Expand Down Expand Up @@ -1162,6 +1170,24 @@ DataSource.prototype.autoupdate = function(models, cb) {
return cb.promise;
};

/**
* Discover if database in strict mode.
* This method returns 0 or 1
*
* @param {Function} Callback function. Optional.
*/
DataSource.prototype.discoverIsStrict = function(cb) {
this.freeze();
cb = cb || utils.createPromiseCallback();

if (this.connector.discoverIsStrict) {
this.connector.discoverIsStrict(cb);
} else if (cb) {
process.nextTick(cb);
}
return cb.promise;
};

/**
* Discover existing database tables.
* This method returns an array of model objects, including {type, name, onwer}
Expand Down Expand Up @@ -1625,6 +1651,7 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
if (followingRelations) {
tasks.push(this.discoverForeignKeys.bind(this, tableName, options));
}
tasks.push(this.discoverIsStrict.bind(this));

async.parallel(tasks, function(err, results) {
if (err) {
Expand All @@ -1633,6 +1660,10 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
}

const columns = results[0];
let isStrict = results[2];
if (isStrict && isStrict[0]) {
isStrict = isStrict[0]['strictMode'];
}
if (!columns || columns.length === 0) {
cb(new Error(g.f('Table \'%s\' does not exist.', tableName)));
return cb.promise;
Expand Down Expand Up @@ -1664,11 +1695,19 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {

columns.forEach(function(item) {
const propName = nameMapper('column', item.columnName);
const jsonSchema = {
nullable: item.nullable === 'Y' || item.nullable === 'YES' ||
item.nullable === 1 || item.nullable === true,
};
if (isStrict && item.dataLength) {
jsonSchema.maxLength = item.dataLength;
}
schema.properties[propName] = {
type: item.type,
required: !item.generated && (item.nullable === 'N' || item.nullable === 'NO' ||
item.nullable === 0 || item.nullable === false),
length: item.dataLength,
jsonSchema,
precision: item.dataPrecision,
scale: item.dataScale,
generated: item.generated,
Expand Down

0 comments on commit 13bf61a

Please sign in to comment.