Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostad authored and Ostad committed Jan 17, 2020
1 parent 8af5d55 commit 65c9f1c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 57 deletions.
33 changes: 15 additions & 18 deletions src/DatabaseFactory.ts
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;
}
74 changes: 43 additions & 31 deletions src/TableTasks.ts
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);
}
58 changes: 51 additions & 7 deletions src/TypeMap.ts
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"]
};
1 change: 0 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { buildDatabase } from "./DatabaseFactory";
import * as fs from "fs";
import * as yargs from "yargs";
import * as path from "path";
Expand Down

0 comments on commit 65c9f1c

Please sign in to comment.