* Describe a table structure * * This method returns an array of hashes containing information about all attributes in the table. * * ```js * { * name: { * type: 'VARCHAR(255)', // this will be 'CHARACTER VARYING' for pg! * allowNull: true, * d
(tableName, options)
| 343 | * @returns {Promise<object>} |
| 344 | */ |
| 345 | async describeTable(tableName, options) { |
| 346 | let schema = null; |
| 347 | let schemaDelimiter = null; |
| 348 | |
| 349 | if (typeof options === class="st">'string') { |
| 350 | schema = options; |
| 351 | } else if (typeof options === class="st">'object' && options !== null) { |
| 352 | schema = options.schema || null; |
| 353 | schemaDelimiter = options.schemaDelimiter || null; |
| 354 | } |
| 355 | |
| 356 | if (typeof tableName === class="st">'object' && tableName !== null) { |
| 357 | schema = tableName.schema; |
| 358 | tableName = tableName.tableName; |
| 359 | } |
| 360 | |
| 361 | const sql = this.queryGenerator.describeTableQuery(tableName, schema, schemaDelimiter); |
| 362 | options = { ...options, type: QueryTypes.DESCRIBE }; |
| 363 | |
| 364 | try { |
| 365 | const data = await this.sequelize.query(sql, options); |
| 366 | /* |
| 367 | * If no data is returned from the query, then the table name may be wrong. |
| 368 | * Query generators that use information_schema for retrieving table info will just return an empty result set, |
| 369 | * it will not throw an error like built-ins do (e.g. DESCRIBE on MySql). |
| 370 | */ |
| 371 | if (_.isEmpty(data)) { |
| 372 | throw new Error(`No description found for class="st">"${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`); |
| 373 | } |
| 374 | |
| 375 | return data; |
| 376 | } catch (e) { |
| 377 | if (e.original && e.original.code === class="st">'ER_NO_SUCH_TABLE') { |
| 378 | throw new Error(`No description found for class="st">"${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`); |
| 379 | } |
| 380 | |
| 381 | throw e; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Add a new column to a table |