Skip to content

Commit 7c08a93

Browse files
committed
feat: add method for mode discovery
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent ae3cf25 commit 7c08a93

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/datasource.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,24 @@ DataSource.prototype.autoupdate = function(models, cb) {
11621162
return cb.promise;
11631163
};
11641164

1165+
/**
1166+
* Discover if database in strict mode.
1167+
* This method returns 0 or 1
1168+
*
1169+
* @param {Function} Callback function. Optional.
1170+
*/
1171+
DataSource.prototype.discoverIsStrict = function(cb) {
1172+
this.freeze();
1173+
cb = cb || utils.createPromiseCallback();
1174+
1175+
if (this.connector.discoverIsStrict) {
1176+
this.connector.discoverIsStrict(cb);
1177+
} else if (cb) {
1178+
process.nextTick(cb);
1179+
}
1180+
return cb.promise;
1181+
};
1182+
11651183
/**
11661184
* Discover existing database tables.
11671185
* This method returns an array of model objects, including {type, name, onwer}
@@ -1625,6 +1643,7 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
16251643
if (followingRelations) {
16261644
tasks.push(this.discoverForeignKeys.bind(this, tableName, options));
16271645
}
1646+
tasks.push(this.discoverIsStrict.bind(this));
16281647

16291648
async.parallel(tasks, function(err, results) {
16301649
if (err) {
@@ -1633,6 +1652,10 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
16331652
}
16341653

16351654
const columns = results[0];
1655+
let isStrict = results[2];
1656+
if (isStrict && isStrict[0]) {
1657+
isStrict = isStrict[0]['globalStrictMode'] | isStrict[0]['sessionStrictMode'];
1658+
}
16361659
if (!columns || columns.length === 0) {
16371660
cb(new Error(g.f('Table \'%s\' does not exist.', tableName)));
16381661
return cb.promise;
@@ -1664,11 +1687,19 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
16641687

16651688
columns.forEach(function(item) {
16661689
const propName = nameMapper('column', item.columnName);
1690+
const jsonSchema = {
1691+
nullable: item.nullable === 'Y' || item.nullable === 'YES' ||
1692+
item.nullable === 1 || item.nullable === true,
1693+
};
1694+
if (isStrict && item.dataLength) {
1695+
jsonSchema.maxLength = item.dataLength;
1696+
}
16671697
schema.properties[propName] = {
16681698
type: item.type,
16691699
required: !item.generated && (item.nullable === 'N' || item.nullable === 'NO' ||
16701700
item.nullable === 0 || item.nullable === false),
16711701
length: item.dataLength,
1702+
jsonSchema,
16721703
precision: item.dataPrecision,
16731704
scale: item.dataScale,
16741705
generated: item.generated,

0 commit comments

Comments
 (0)