-
Notifications
You must be signed in to change notification settings - Fork 3
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
0 parents
commit a4102f4
Showing
25 changed files
with
600 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"env": { | ||
"test": { | ||
"plugins": [ | ||
"istanbul" | ||
] | ||
} | ||
}, | ||
"plugins": [ | ||
"transform-object-rest-spread", | ||
"transform-flow-strip-types" | ||
], | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": 6 | ||
}, | ||
"useBuiltIns": true | ||
} | ||
] | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": [ | ||
"canonical", | ||
"canonical/flowtype" | ||
], | ||
"root": true, | ||
"rules": { | ||
"flowtype/no-weak-types": 0, | ||
"no-duplicate-imports": 0, | ||
"no-restricted-syntax": 0 | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ignore] | ||
.*/node_modules/.*/test/.* | ||
.*/node_modules/babel-plugin-flow-runtime/.* | ||
.*/node_modules/config-chain/.* | ||
.*/node_modules/conventional-changelog-core/.* | ||
.*/node_modules/flow-runtime/.* | ||
.*/node_modules/npmconf/.* |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
coverage | ||
dist | ||
node_modules | ||
*.log | ||
.* | ||
!.babelrc | ||
!.editorconfig | ||
!.eslintignore | ||
!.eslintrc | ||
!.flowconfig | ||
!.gitignore | ||
!.npmignore | ||
!.travis.yml |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
src | ||
test | ||
coverage | ||
.* | ||
*.log |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
language: node_js | ||
node_js: | ||
- node | ||
- 6 | ||
script: | ||
- npm run build | ||
- npm run test | ||
- npm run lint | ||
# - nyc --silent npm run test | ||
# - nyc report --reporter=text-lcov | coveralls | ||
# - nyc check-coverage --lines 80 | ||
after_success: | ||
- npm run build | ||
- semantic-release pre | ||
- npm publish | ||
- semantic-release post | ||
notifications: | ||
email: false | ||
sudo: false |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Copyright (c) 2017, Gajus Kuizinas (http://gajus.com/) | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
* Neither the name of the Gajus Kuizinas (http://gajus.com/) nor the | ||
names of its contributors may be used to endorse or promote products | ||
derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL ANUARY BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# database-types | ||
|
||
[![Travis build status](http://img.shields.io/travis/gajus/database-types/master.svg?style=flat-square)](https://travis-ci.org/gajus/database-types) | ||
[![Coveralls](https://img.shields.io/coveralls/gajus/database-types.svg?style=flat-square)](https://coveralls.io/github/gajus/database-types) | ||
[![NPM version](http://img.shields.io/npm/v/database-types.svg?style=flat-square)](https://www.npmjs.org/package/database-types) | ||
[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical) | ||
[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas) | ||
|
||
A type generator for Postgres. | ||
|
||
## Example usage | ||
|
||
### Generating Flow types | ||
|
||
```bash | ||
export DATABASE_TYPES_DATABASE_CONNECTION_URI=postgres://postgres:[email protected]/test | ||
export DATABASE_TYPES_COLUMN_FILTER="return !['raster_overviews', 'raster_columns', 'geometry_columns', 'geography_columns', 'spatial_ref_sys'].includes(tableName)" | ||
export DATABASE_TYPES_DIALECT=flow | ||
|
||
database-types generate > ./types.js | ||
|
||
``` | ||
|
||
This generates file containing Flow type declartions in the following format: | ||
|
||
```js | ||
export type ReservationSeatRecordType = {| | ||
+createdAt: string, | ||
+id: number, | ||
+reservationId: number, | ||
+seatId: number | ||
|}; | ||
|
||
export type TicketTypeRecordType = {| | ||
+cinemaId: number, | ||
+id: number, | ||
+name: string, | ||
+nid: string, | ||
+policy: string | null | ||
|}; | ||
|
||
// ... | ||
|
||
``` | ||
|
||
## CLI | ||
|
||
```bash | ||
npm install database-types -g | ||
|
||
Options: | ||
--help Show help [boolean] | ||
--column-filter Function used to filter columns. Function is | ||
constructed using `new Function`. Function | ||
receives table name as the first parameter and | ||
column name as the second parameter (parameter | ||
names are "tableName" and "columnName").[string] | ||
--property-name-formatter Function used to format property name. Function | ||
is constructed using `new Function`. Function | ||
receives column name as the first parameter | ||
(parameter name is "columnName"). The default | ||
behaviour is to (lower) camelCase the column | ||
name. [string] | ||
--type-name-formatter Function used to format type name. Function is | ||
constructed using `new Function`. Function | ||
receives table name as the first parameter | ||
(parameter name is "tableName"). The default | ||
behaviour is to (upper) CamelCase the table name | ||
and suffix it with "RecordType". [string] | ||
--database-connection-uri [required] | ||
--dialect [required] [choices: "flow"] | ||
--include-materialized-views [boolean] [default: true] | ||
|
||
``` |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Gajus Kuizinas", | ||
"url": "http://gajus.com" | ||
}, | ||
"ava": { | ||
"babel": "inherit", | ||
"require": [ | ||
"babel-register" | ||
] | ||
}, | ||
"bin": "./dist/bin/index.js", | ||
"dependencies": { | ||
"debug": "^2.6.8", | ||
"es6-error": "^4.0.2", | ||
"lodash": "^4.17.4", | ||
"mightyql": "^3.2.0", | ||
"yargs": "^8.0.1" | ||
}, | ||
"description": "A type generator for Postgres.", | ||
"devDependencies": { | ||
"ava": "^0.19.1", | ||
"babel-cli": "^6.24.1", | ||
"babel-plugin-istanbul": "^4.1.4", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-env": "^1.5.1", | ||
"babel-register": "^6.24.1", | ||
"coveralls": "^2.13.1", | ||
"eslint": "^3.19.0", | ||
"eslint-config-canonical": "^8.2.1", | ||
"flow-bin": "^0.47.0", | ||
"husky": "^0.13.4", | ||
"nyc": "^11.0.2", | ||
"semantic-release": "^6.3.6" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"keywords": [ | ||
"flowtype", | ||
"postgres" | ||
], | ||
"license": "BSD-3-Clause", | ||
"main": "./dist/index.js", | ||
"name": "database-types", | ||
"nyc": { | ||
"include": [ | ||
"src/**/*.js" | ||
], | ||
"instrument": false, | ||
"reporter": [ | ||
"text-lcov" | ||
], | ||
"require": [ | ||
"babel-register" | ||
], | ||
"sourceMap": false | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gajus/database-types" | ||
}, | ||
"scripts": { | ||
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps", | ||
"lint": "eslint ./src ./test && flow", | ||
"test": "ava --verbose" | ||
}, | ||
"version": "1.0.0" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// @flow | ||
|
||
/* eslint-disable no-new-func */ | ||
|
||
import _ from 'lodash'; | ||
import { | ||
createConnection | ||
} from 'mightyql'; | ||
import type { | ||
ColumnType, | ||
TypePropertyType | ||
} from '../../types'; | ||
import { | ||
getDatabaseTableColumns, | ||
getDatabaseMaterializedViewColumns | ||
} from '../../queries'; | ||
import { | ||
generateFlowTypeDocument, | ||
mapFlowType, | ||
normalizeColumns | ||
} from '../../utilities'; | ||
|
||
export const command = 'generate'; | ||
export const desc = 'Generate Flow types for a Postgres database.'; | ||
|
||
export const builder = (yargs: Object): void => { | ||
yargs | ||
.options({ | ||
'column-filter': { | ||
description: 'Function used to filter columns. Function is constructed using `new Function`. Function receives table name as the first parameter and column name as the second parameter (parameter names are "tableName" and "columnName").', | ||
type: 'string' | ||
}, | ||
'database-connection-uri': { | ||
demand: true | ||
}, | ||
dialect: { | ||
choices: [ | ||
'flow' | ||
], | ||
demand: true | ||
}, | ||
'include-materialized-views': { | ||
default: true, | ||
type: 'boolean' | ||
}, | ||
'property-name-formatter': { | ||
description: 'Function used to format property name. Function is constructed using `new Function`. Function receives column name as the first parameter (parameter name is "columnName"). The default behaviour is to (lower) camelCase the column name.', | ||
type: 'string' | ||
}, | ||
'type-name-formatter': { | ||
description: 'Function used to format type name. Function is constructed using `new Function`. Function receives table name as the first parameter (parameter name is "tableName"). The default behaviour is to (upper) CamelCase the table name and suffix it with "RecordType".', | ||
type: 'string' | ||
} | ||
}); | ||
}; | ||
|
||
export const handler = async (argv: Object): Promise<void> => { | ||
const defaultFormatTypeName = (tableName: string): string => { | ||
return _.upperFirst(_.camelCase(tableName)) + 'RecordType'; | ||
}; | ||
|
||
const defaultFormatPropertyName = (columnName: string): string => { | ||
return _.camelCase(columnName); | ||
}; | ||
|
||
const filterColumns = argv.columnFilter ? new Function('tableName', 'columnName', argv.columnFilter) : null; | ||
const formatTypeName = argv.typeNameFormatter ? new Function('columnName', argv.typeNameFormatter) : defaultFormatTypeName; | ||
const formatPropertyName = argv.propertyNameFormatter ? new Function('tableName', argv.propertyNameFormatter) : defaultFormatPropertyName; | ||
|
||
const createProperties = (columns: $ReadOnlyArray<ColumnType>): $ReadOnlyArray<TypePropertyType> => { | ||
let filteredColumns = columns; | ||
|
||
if (filterColumns) { | ||
filteredColumns = filteredColumns.filter((column) => { | ||
// $FlowFixMe | ||
return filterColumns(column.tableName, column.columnName); | ||
}); | ||
} | ||
|
||
return filteredColumns.map((column) => { | ||
// $FlowFixMe | ||
return { | ||
// $FlowFixMe | ||
name: formatPropertyName(column.columnName), | ||
type: mapFlowType(column.databaseType) + (column.nullable ? ' | null' : ''), | ||
|
||
// $FlowFixMe | ||
typeName: formatTypeName(column.tableName) | ||
}; | ||
}); | ||
}; | ||
|
||
const connection = await createConnection(argv.databaseConnectionUri); | ||
|
||
let unnormalizedColumns; | ||
|
||
unnormalizedColumns = await getDatabaseTableColumns(connection); | ||
|
||
if (argv.includeMaterializedViews) { | ||
unnormalizedColumns = unnormalizedColumns.concat(await getDatabaseMaterializedViewColumns(connection)); | ||
} | ||
|
||
const normalizedColumns = normalizeColumns(unnormalizedColumns); | ||
|
||
const properties = createProperties(normalizedColumns); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(generateFlowTypeDocument(properties)); | ||
|
||
await connection.end(); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env node | ||
|
||
import yargs from 'yargs'; | ||
|
||
process.on('unhandledRejection', (reason) => { | ||
throw reason; | ||
}); | ||
|
||
process.on('uncaughtException', (error) => { | ||
throw error; | ||
}); | ||
|
||
// eslint-disable-next-line no-unused-expressions | ||
yargs | ||
.env('DATABASE_TYPES') | ||
.commandDir('commands') | ||
.help() | ||
.wrap(80) | ||
.argv; |
Oops, something went wrong.