( model: ModelStatic<Model<any, any>>, schema: string, withoutForeignKey: boolean )
| 491 | } |
| 492 | |
| 493 | export function generateCreateTableQuery( |
| 494 | model: ModelStatic<Model<any, any>>, |
| 495 | schema: string, |
| 496 | withoutForeignKey: boolean |
| 497 | ): QueryString[] { |
| 498 | const tableName = model.tableName; |
| 499 | |
| 500 | const attributes = model.getAttributes(); |
| 501 | const columnDefinitions: string[] = []; |
| 502 | const primaryKeyColumns: string[] = []; |
| 503 | const comments: Query[] = []; |
| 504 | |
| 505 | Object.keys(attributes).forEach((key) => { |
| 506 | const attr = attributes[key]; |
| 507 | |
| 508 | assert(attr.field, 'Expected field to be set on attribute'); |
| 509 | |
| 510 | const columnDefinition = `"${attr.field}" ${formatAttributes(attr, schema, withoutForeignKey)}`; |
| 511 | |
| 512 | columnDefinitions.push(columnDefinition); |
| 513 | if (attr.comment) { |
| 514 | comments.push(commentColumnQuery(schema, tableName, attr.field, attr.comment)); |
| 515 | } |
| 516 | if (attr.primaryKey) { |
| 517 | primaryKeyColumns.push(`"${attr.field}"`); |
| 518 | } |
| 519 | }); |
| 520 | |
| 521 | const primaryKeyDefinition = `, PRIMARY KEY (${primaryKeyColumns.join(', ')})`; |
| 522 | |
| 523 | const tableQuery = `CREATE TABLE IF NOT EXISTS "${schema}"."${tableName}" (${columnDefinitions.join( |
| 524 | ',\n ' |
| 525 | )}${primaryKeyDefinition});`; |
| 526 | |
| 527 | return [tableQuery, ...comments]; |
| 528 | } |
| 529 | |
| 530 | export function generateCreateIndexQuery( |
| 531 | indexes: readonly ModelIndexesOptions[], |
no test coverage detected