forked from rmp135/sql-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableTasks.ts
77 lines (73 loc) · 2.78 KB
/
TableTasks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Knex } from 'knex'
import * as AdapterFactory from './AdapterFactory.js'
import { Config, Table } from './Typings.js'
import * as ColumnTasks from './ColumnTasks.js'
import * as SharedTasks from './SharedTasks.js'
import * as SchemaTasks from './SchemaTasks.js'
import pluralize from 'pluralize'
/**
* Returns all tables from a given database filtered by configuration options.
*
* @export
* @param {knex} db The knex context to use.
* @param {Config} config The configuration to use.
* @returns {Promise<Table[]>}
*/
export async function getAllTables(db: Knex, config: Config): Promise<Table[]> {
const adapter = AdapterFactory.buildAdapter(db.client.dialect)
const allTables = (await adapter.getAllTables(db, config.schemas))
.filter(table => config.tables.length === 0 || config.tables.includes(`${table.schema}.${table.name}`))
.filter(table => !config.excludedTables.includes(`${table.schema}.${table.name}`))
.sort((a, b) => a.name.localeCompare(b.name))
return await Promise.all(allTables.map(async table => ({
columns: await ColumnTasks.getColumnsForTable(db, table, config),
interfaceName: generateInterfaceName(table.name, config),
name: table.name,
schema: SchemaTasks.generateSchemaName(table.schema),
additionalProperties: getAdditionalProperties(table.name, table.schema, config),
extends: getExtends(table.name, table.schema, config),
comment: table.comment
} as Table)))
}
/**
* Returns the additional properties to add to the interface.
*
* @export
* @param {string} tableName The name of the table.
* @param {string} schemaName The schema of the table.
* @param {Config} config The configuration to use.
*/
export function getAdditionalProperties(tableName: string, schemaName: string, config: Config): string[] {
const fullName = `${schemaName}.${tableName}`
return config.additionalProperties[fullName] ?? []
}
/**
* Returns any extension that should be applied to the interface.
*
* @export
* @param {string} tableName
* @param {string} schemaName
* @param {Config} config
* @returns {string}
*/
export function getExtends(tableName: string, schemaName: string, config: Config): string {
const fullName = `${schemaName}.${tableName}`
if (config.extends === undefined) return ''
return config.extends[fullName]
}
/**
* Converts a table name to an interface name given a configuration.
*
* @export
* @param {string} name The name of the table.
* @param {Config} config The configuration to use.
* @returns
*/
export function generateInterfaceName(name: string, config: Config): string {
name = name.replace(/ /g, '_')
name = SharedTasks.convertCase(name, config.tableNameCasing)
if (config.singularTableNames) {
name = pluralize.singular(name)
}
return config.interfaceNameFormat.replace('${table}', name)
}