This repository was archived by the owner on Jun 26, 2021. It is now read-only.
-
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
Showing
11 changed files
with
193 additions
and
71 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
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
"scripts": { | ||
"build": "tsc", | ||
"test": "jest", | ||
"lint": "eslint src" | ||
"lint": "eslint src --ext .js,.jsx,.ts,.tsx" | ||
}, | ||
"keywords": [], | ||
"author": "Michał Miszczyszyn <[email protected]> (https://typeofweb.com/)", | ||
|
@@ -32,20 +32,22 @@ | |
"jest": "25.3.0", | ||
"jest-extended": "0.11.5", | ||
"lint-staged": "10.1.3", | ||
"prettier": "2.0.4", | ||
"ts-jest": "25.3.1", | ||
"typescript": "3.8.3" | ||
}, | ||
"dependencies": { | ||
"pg-promise": "10.5.0" | ||
"@types/lodash": "4.14.149", | ||
"lodash": "4.17.15", | ||
"pg-promise": "10.5.0", | ||
"prettier": "2.0.4" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{js,jsx,ts,tsx}": "yarn lint --", | ||
"*.{js,jsx,ts,tsx}": "yarn lint", | ||
"*.{js,jsx,ts,tsx,md,html,css,json,yml}": "prettier --write" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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,68 @@ | ||
import { db, sql } from './db'; | ||
import { ColumnSchema, TableName } from './types'; | ||
import { ColumnSchema, TableName, TableSchema } from './types'; | ||
import Path from 'path'; | ||
import { Table } from '..'; | ||
import Prettier from 'prettier'; | ||
import { defaults } from 'lodash'; | ||
|
||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
class NotSupportedError extends Error { | ||
message = 'NotSupportedError'; | ||
} | ||
|
||
function getTableNames(): Promise<TableName[]> { | ||
return db.manyOrNone(sql('select_table_name.sql')); | ||
return db.manyOrNone(sql(Path.join(__dirname, 'select_table_name.sql'))); | ||
} | ||
|
||
function getTableSchema(tableName: string): Promise<ColumnSchema[]> { | ||
return db.manyOrNone(sql('select_table_schema.sql'), [tableName]); | ||
return db.manyOrNone(sql(Path.join(__dirname, 'select_table_schema.sql')), [tableName]); | ||
} | ||
|
||
async function getTablesSchemas(): Promise< | ||
Array<{ | ||
tableName: string; | ||
schema: ColumnSchema[]; | ||
}> | ||
> { | ||
export async function getTablesSchemas(): Promise<Array<TableSchema>> { | ||
const tableNames = await getTableNames(); | ||
|
||
const result = await Promise.all( | ||
tableNames.map(async i => { | ||
tableNames.map(async (i) => { | ||
return { tableName: i.table_name, schema: await getTableSchema(i.table_name) }; | ||
}), | ||
); | ||
|
||
return result; | ||
} | ||
|
||
export function schemaToObj(schema: TableSchema): Table { | ||
return { | ||
name: schema.tableName, | ||
columns: Object.fromEntries( | ||
schema.schema.map((s) => { | ||
return [s.column_name, { type: s.udt_name, notNull: s.is_nullable === 'NO' }]; | ||
}), | ||
), | ||
}; | ||
} | ||
|
||
export function tableToTSCode(table: Table, opts?: Prettier.Options): string { | ||
const typeName = table.name.slice(0, 1).toLocaleUpperCase() + table.name.slice(1); | ||
const code = `const ${typeName} = ${JSON.stringify(table)} as const;`; | ||
|
||
const defaultOptions: Prettier.Options = { | ||
semi: true, | ||
singleQuote: true, | ||
trailingComma: 'all', | ||
printWidth: 100, | ||
tabWidth: 2, | ||
useTabs: false, | ||
} as const; | ||
const options = defaults({}, opts, defaultOptions); | ||
return Prettier.format(code, { | ||
...options, | ||
parser: 'typescript', | ||
}); | ||
} | ||
|
||
export async function run() { | ||
return getTablesSchemas(); | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const schemas = await getTablesSchemas(); | ||
} |
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
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
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
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
Oops, something went wrong.