From 7c08a93930becfe2af4d2b6a9c468f51f7c01e50 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 3 Mar 2024 21:27:53 +0500 Subject: [PATCH] feat: add method for mode discovery Signed-off-by: Muhammad Aaqil --- lib/datasource.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/datasource.js b/lib/datasource.js index 0ca428314..ee41a1d3e 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1162,6 +1162,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} @@ -1625,6 +1643,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) { @@ -1633,6 +1652,10 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { } const columns = results[0]; + let isStrict = results[2]; + if (isStrict && isStrict[0]) { + isStrict = isStrict[0]['globalStrictMode'] | isStrict[0]['sessionStrictMode']; + } if (!columns || columns.length === 0) { cb(new Error(g.f('Table \'%s\' does not exist.', tableName))); return cb.promise; @@ -1664,11 +1687,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,