knex-schema-inspector
Advanced tools
Comparing version 0.0.21 to 0.0.22
@@ -278,4 +278,7 @@ import Knex from 'knex'; | ||
); | ||
return results[0][0]['Column_name'] as string; | ||
if (results.length && results[0].length) { | ||
return results[0][0]['Column_name'] as string; | ||
} | ||
return null; | ||
} | ||
} |
@@ -49,3 +49,2 @@ import Knex from 'knex'; | ||
.from('DBA_TABLES'); | ||
return records.map(({ TABLE_NAME }) => TABLE_NAME); | ||
@@ -52,0 +51,0 @@ } |
@@ -236,19 +236,19 @@ import Knex from 'knex'; | ||
LEFT JOIN ( | ||
SELECT | ||
k1.table_schema, | ||
k1.table_name, | ||
k1.column_name, | ||
k2.table_schema AS referenced_table_schema, | ||
k2.table_name AS referenced_table_name, | ||
k2.column_name AS referenced_column_name | ||
FROM | ||
information_schema.key_column_usage k1 | ||
SELECT | ||
k1.table_schema, | ||
k1.table_name, | ||
k1.column_name, | ||
k2.table_schema AS referenced_table_schema, | ||
k2.table_name AS referenced_table_name, | ||
k2.column_name AS referenced_column_name | ||
FROM | ||
information_schema.key_column_usage k1 | ||
JOIN information_schema.referential_constraints fk using ( | ||
constraint_schema, constraint_name | ||
) | ||
JOIN information_schema.key_column_usage k2 ON k2.constraint_schema = fk.unique_constraint_schema | ||
AND k2.constraint_name = fk.unique_constraint_name | ||
) | ||
JOIN information_schema.key_column_usage k2 ON k2.constraint_schema = fk.unique_constraint_schema | ||
AND k2.constraint_name = fk.unique_constraint_name | ||
AND k2.ordinal_position = k1.position_in_unique_constraint | ||
) ffk ON ffk.table_name = c.table_name | ||
AND ffk.column_name = c.column_name | ||
) ffk ON ffk.table_name = c.table_name | ||
AND ffk.column_name = c.column_name | ||
` | ||
@@ -338,3 +338,3 @@ ) | ||
async primary(table: string): Promise<string> { | ||
const { column_name } = await this.knex | ||
const result = await this.knex | ||
.select('information_schema.key_column_usage.column_name') | ||
@@ -354,4 +354,4 @@ .from('information_schema.key_column_usage') | ||
return column_name; | ||
return result ? result.column_name : null; | ||
} | ||
} |
@@ -22,3 +22,3 @@ import Knex from 'knex'; | ||
hasColumn(table: string, column: string): Promise<boolean>; | ||
primary(table: string): Promise<string>; | ||
primary(table: string): Promise<string | null>; | ||
@@ -25,0 +25,0 @@ // Not in MySQL |
{ | ||
"name": "knex-schema-inspector", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"description": "[WIP] Utility for extracting information about existing DB schema", | ||
@@ -9,3 +9,5 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"prepare": "npm run build" | ||
"prepare": "npm run build", | ||
"lint": "prettier --check .", | ||
"test": "npm run lint && ts-mocha test/**/*.spec.ts" | ||
}, | ||
@@ -32,8 +34,15 @@ "repository": { | ||
"devDependencies": { | ||
"@types/chai": "^4.2.14", | ||
"@types/lodash.flatten": "^4.4.6", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.0.13", | ||
"chai": "^4.2.0", | ||
"husky": "^4.2.5", | ||
"knex": "^0.21.1", | ||
"lint-staged": "^10.2.11", | ||
"mocha": "^8.2.0", | ||
"mysql": "^2.18.1", | ||
"pg": "^8.4.0", | ||
"prettier": "^2.0.5", | ||
"ts-mocha": "^7.0.0", | ||
"typescript": "^3.9.5" | ||
@@ -40,0 +49,0 @@ }, |
@@ -38,8 +38,8 @@ # knex-schema-inspector | ||
connection: { | ||
host : '127.0.0.1', | ||
user : 'your_database_user', | ||
password : 'your_database_password', | ||
database : 'myapp_test', | ||
charset : 'utf8' | ||
} | ||
host: '127.0.0.1', | ||
user: 'your_database_user', | ||
password: 'your_database_password', | ||
database: 'myapp_test', | ||
charset: 'utf8', | ||
}, | ||
}); | ||
@@ -218,4 +218,24 @@ | ||
### Tests | ||
First start docker containers: | ||
```shell | ||
$ docker-compose up -d | ||
``` | ||
Then run tests: | ||
```shell | ||
$ npm test | ||
``` | ||
Standard mocha filter (grep) can be used: | ||
```shell | ||
$ npm test -- -g '.tableInfo' | ||
``` | ||
## License | ||
[MIT](https://choosealicense.com/licenses/mit/) | ||
[MIT](https://choosealicense.com/licenses/mit/) |
309601
67
6881
240
14