* 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)
| 365 | * @returns {Promise<object>} |
| 366 | */ |
| 367 | async describeTable(tableName, options) { |
| 368 | let schema = null; |
| 369 | let schemaDelimiter = null; |
| 370 | |
| 371 | if (typeof options === 'string') { |
| 372 | schema = options; |
| 373 | } else if (typeof options === 'object' && options !== null) { |
| 374 | schema = options.schema || null; |
| 375 | schemaDelimiter = options.schemaDelimiter || null; |
| 376 | } |
| 377 | |
| 378 | if (typeof tableName === 'object' && tableName !== null) { |
| 379 | schema = tableName.schema; |
| 380 | tableName = tableName.tableName; |
| 381 | } |
| 382 | |
| 383 | const sql = this.queryGenerator.describeTableQuery(tableName, schema, schemaDelimiter); |
| 384 | options = { ...options, type: QueryTypes.DESCRIBE }; |
| 385 | |
| 386 | try { |
| 387 | const data = await this.sequelize.query(sql, options); |
| 388 | /* |
| 389 | * If no data is returned from the query, then the table name may be wrong. |
| 390 | * Query generators that use information_schema for retrieving table info will just return an empty result set, |
| 391 | * it will not throw an error like built-ins do (e.g. DESCRIBE on MySql). |
| 392 | */ |
| 393 | if (_.isEmpty(data)) { |
| 394 | throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`); |
| 395 | } |
| 396 | |
| 397 | return data; |
| 398 | } catch (e) { |
| 399 | if (e.original && e.original.code === 'ER_NO_SUCH_TABLE') { |
| 400 | throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`); |
| 401 | } |
| 402 | |
| 403 | throw e; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Add a new column to a table |