-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ostad
authored and
Ostad
committed
Jan 17, 2020
1 parent
8af5d55
commit 65c9f1c
Showing
4 changed files
with
109 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
import * as knex from 'knex'; | ||
import { Config, Database } from './Typings'; | ||
import * as DatabaseTasks from './DatabaseTasks' | ||
import * as TableTasks from './TableTasks' | ||
import * as knex from "knex"; | ||
import { Config, Database } from "./Typings"; | ||
import * as TableTasks from "./TableTasks"; | ||
|
||
/** | ||
* Builds a Database and generates its definitions. | ||
* | ||
* | ||
* @export | ||
* @param {Config} [config] The configuration to use. | ||
* @returns {Promise<Database>} The generated Database. | ||
*/ | ||
export async function buildDatabase (config: Config): Promise<Database> { | ||
let database: Database | ||
let db: knex | ||
export async function buildDatabase(config: Config): Promise<Database> { | ||
let database: Database; | ||
let db: knex; | ||
try { | ||
db = knex(config) | ||
db = knex(config); | ||
database = { | ||
tables: await TableTasks.getAllTables(db, config) | ||
} | ||
} | ||
catch (err) { | ||
throw err | ||
} | ||
finally { | ||
}; | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
if (db !== undefined) { | ||
db.destroy() | ||
db.destroy(); | ||
} | ||
} | ||
return database | ||
} | ||
return database; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,61 @@ | ||
import * as AdapterFactory from './AdapterFactory' | ||
import * as knex from 'knex' | ||
import { Config, Table } from './Typings' | ||
import * as ColumnTasks from './ColumnTasks' | ||
import * as TableSubTasks from './TableSubTasks' | ||
import * as SharedTasks from './SharedTasks' | ||
import * as AdapterFactory from "./AdapterFactory"; | ||
import * as knex from "knex"; | ||
import { Config, Table } from "./Typings"; | ||
import * as ColumnTasks from "./ColumnTasks"; | ||
import * as TableSubTasks from "./TableSubTasks"; | ||
import * as SharedTasks from "./SharedTasks"; | ||
|
||
/** | ||
* Returns all tables from a given database using a configuration. | ||
* | ||
* | ||
* @export | ||
* @param {knex} db The knex context to use. | ||
* @param {Config} config The configuration to use. | ||
* @returns {Promise<Table[]>} | ||
* @returns {Promise<Table[]>} | ||
*/ | ||
export async function getAllTables (db: knex, config: Config): Promise<Table[]> { | ||
const tables = config.tables || [] | ||
const excludedTables = config.excludedTables || [] | ||
const schemas = config.schemas || [] | ||
const adapter = AdapterFactory.buildAdapter(config) | ||
export async function getAllTables(db: knex, config: Config): Promise<Table[]> { | ||
const tables = config.tables || []; | ||
const excludedTables = config.excludedTables || []; | ||
const schemas = config.schemas || []; | ||
const adapter = AdapterFactory.buildAdapter(config); | ||
const allTables = (await adapter.getAllTables(db, schemas)) | ||
.filter(table => tables.length == 0 || tables.includes(`${table.schema}.${table.name}`)) | ||
.filter(table => !excludedTables.includes(`${table.schema}.${table.name}`)) | ||
return await Promise.all(allTables.map(async table => ({ | ||
columns: await ColumnTasks.getColumnsForTable(db, table, config), | ||
name: table.name, | ||
schema: table.schema, | ||
additionalProperties: TableSubTasks.getAdditionalProperties(table.name, table.schema, config), | ||
extends: TableSubTasks.getExtends(table.name, table.schema, config) | ||
} as Table))) | ||
.filter( | ||
table => | ||
tables.length == 0 || tables.includes(`${table.schema}.${table.name}`) | ||
) | ||
.filter(table => !excludedTables.includes(`${table.schema}.${table.name}`)); | ||
return await Promise.all( | ||
allTables.map( | ||
async table => | ||
({ | ||
columns: await ColumnTasks.getColumnsForTable(db, table, config), | ||
name: table.name, | ||
schema: table.schema, | ||
additionalProperties: TableSubTasks.getAdditionalProperties( | ||
table.name, | ||
table.schema, | ||
config | ||
), | ||
extends: TableSubTasks.getExtends(table.name, table.schema, config) | ||
} as Table) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* 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 | ||
* @returns | ||
*/ | ||
export function generateInterfaceName (name: string, config: Config): string { | ||
const interfaceNamePattern = config.interfaceNameFormat || '${table}Entity' | ||
name = name.replace(/ /g, '_') | ||
name = SharedTasks.convertCase(name, config.tableNameCasing) | ||
export function generateInterfaceName(name: string, config: Config): string { | ||
const interfaceNamePattern = config.interfaceNameFormat || "${table}Entity"; | ||
name = name.replace(/ /g, "_"); | ||
name = SharedTasks.convertCase(name, config.tableNameCasing); | ||
if (config.singularTableNames && name[name.length - 1] == "s") { | ||
name = name.substr(0, name.length - 1) | ||
name = name.substr(0, name.length - 1); | ||
} | ||
return interfaceNamePattern.replace('${table}', name) | ||
} | ||
return interfaceNamePattern.replace("${table}", name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,52 @@ | ||
export default { | ||
string: ['nchar', 'nvarchar', 'varchar', 'char', 'tinytext', 'text', 'longtext', 'mediumtext', 'ntext', 'varbinary', 'uuid', 'uniqueidentifier', 'character varying', 'bigint', 'xml'], | ||
number: ['tinyint', 'int', 'numeric', 'integer', 'real', 'smallint', 'decimal', 'float', 'double precision', 'double', 'dec', 'fixed', 'year', 'serial', 'bigserial', 'int4', 'money', 'smallmoney'], | ||
Date: ['datetime', 'timestamp', 'date', 'time', 'timestamp', 'datetime2', 'smalldatetime', 'datetimeoffset'], | ||
boolean: ['bit', 'boolean', 'bool'], | ||
Object: ['json', 'TVP'], | ||
buffer: ['binary', 'varbinary', 'image', 'UDT'] | ||
} | ||
string: [ | ||
"nchar", | ||
"nvarchar", | ||
"varchar", | ||
"char", | ||
"tinytext", | ||
"text", | ||
"longtext", | ||
"mediumtext", | ||
"ntext", | ||
"varbinary", | ||
"uuid", | ||
"uniqueidentifier", | ||
"character varying", | ||
"bigint", | ||
"xml" | ||
], | ||
number: [ | ||
"tinyint", | ||
"int", | ||
"numeric", | ||
"integer", | ||
"real", | ||
"smallint", | ||
"decimal", | ||
"float", | ||
"double precision", | ||
"double", | ||
"dec", | ||
"fixed", | ||
"year", | ||
"serial", | ||
"bigserial", | ||
"int4", | ||
"money", | ||
"smallmoney" | ||
], | ||
Date: [ | ||
"datetime", | ||
"timestamp", | ||
"date", | ||
"time", | ||
"timestamp", | ||
"datetime2", | ||
"smalldatetime", | ||
"datetimeoffset" | ||
], | ||
boolean: ["bit", "boolean", "bool"], | ||
Object: ["json", "TVP"], | ||
buffer: ["binary", "varbinary", "image", "UDT"] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters