Skip to content

Commit

Permalink
Fixed #26
Browse files Browse the repository at this point in the history
  • Loading branch information
ozziest authored and ozgur-shape committed Jul 25, 2021
1 parent a225899 commit 8866be1
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
node-version: [14.x]
database: [mysql8, mysql57]
database: [mysql8, mysql57, "postgres"]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## [0.13.0 (2021-07-25)](https://github.com/axe-api/axe-api/compare/0.12.2...0.13.0)

### Features

- PostgreSQL database analyzer and integration tests have been added.

## [0.12.2 (2021-07-24)](https://github.com/axe-api/axe-api/compare/0.12.1...0.12.2)

### Fixed
Expand Down
124 changes: 123 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axe-api",
"version": "0.12.2",
"version": "0.13.0",
"description": "AXE API is a simple tool which has been created based on Express and Knex.js to create Rest APIs quickly.",
"main": "index.js",
"type": "module",
Expand All @@ -15,7 +15,8 @@
"lint": "eslint src/**",
"lint:watch": "esw --watch --color",
"test:integration:mysql8": "cd ./tests/integrations && node index.js mysql8",
"test:integration:mysql57": "cd ./tests/integrations && node index.js mysql57"
"test:integration:mysql57": "cd ./tests/integrations && node index.js mysql57",
"test:integration:postgres": "cd ./tests/integrations && node index.js postgres"
},
"dependencies": {
"change-case": "^4.1.2",
Expand Down Expand Up @@ -47,6 +48,7 @@
"jest": "^27.0.6",
"mysql": "^2.18.1",
"nodemon": "^2.0.12",
"pg": "^8.6.0",
"set-value": ">=4.0.0"
}
}
6 changes: 3 additions & 3 deletions src/handlers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export default async (context) => {
formData,
});

let [insertedPrimaryKeyValue] = await trx(model.instance.table).insert(
formData
);
let [insertedPrimaryKeyValue] = await trx(model.instance.table)
.insert(formData)
.returning("id");

// If the user use a special primary key value, we should use that value
if (insertedPrimaryKeyValue === 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/resolvers/databaseDetectors/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mySQLDetector from "./mySqlDetector.js";
import sqliteDetector from "./sqliteDetector.js";
import postgresDetector from "./postgresDetector.js";

export { mySQLDetector, sqliteDetector };
export { mySQLDetector, sqliteDetector, postgresDetector };
37 changes: 37 additions & 0 deletions src/resolvers/databaseDetectors/postgresDetector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const getDatabaseColumns = async (knex, schema) => {
const { rows } = await knex.raw(
`
SELECT C.*, TC.constraint_type = 'PRIMARY KEY' AS is_primary
FROM information_schema.columns C
LEFT JOIN information_schema.key_column_usage KCU ON KCU.table_name = C.table_name AND KCU.column_name = C.column_name
LEFT JOIN information_schema.table_constraints TC ON TC.constraint_name = KCU.constraint_name
WHERE C.table_schema = ?
`,
[schema]
);
return rows;
};

export default async ({ knex, schema }) => {
const databaseColumns = await getDatabaseColumns(knex, schema);

if (!databaseColumns) {
throw new Error("Auto-column detection is failed on PostgreSQL!");
}

return databaseColumns.map((i) => {
return {
name: i.column_name,
tableName: i.table_name,
isNullable: i.is_nullable === "YES",
dataType: i.data_type,
defaultValue: i.column_default,
maxLength: i.character_maximum_length,
numericPrecision: i.numeric_precision,
numericScale: i.numeric_scale,
isPrimary: i.is_primary ? i.is_primary : false,
isAutoIncrement:
i.column_default && i.column_default.indexOf("nextval") > -1,
};
});
};
19 changes: 17 additions & 2 deletions src/resolvers/detectDbColumns.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import IoC from "./../core/IoC.js";
import { mySQLDetector, sqliteDetector } from "./databaseDetectors/index.js";
import {
mySQLDetector,
sqliteDetector,
postgresDetector,
} from "./databaseDetectors/index.js";

const DATABASE_DETECTORS = {
mysql: mySQLDetector,
sqlite3: sqliteDetector,
postgres: postgresDetector,
};

const getDatabaseDetector = (databaseClient) => {
Expand All @@ -19,9 +24,19 @@ const getDatabaseColumns = async () => {
const database = await IoC.use("Database");
const databaseClient = Config.Database.client.toLowerCase();
const detector = getDatabaseDetector(databaseClient);
let schema = Config.Database.connection.database;
if (
Config.Database.connection.searchPath &&
Config.Database.connection.searchPath.length
) {
schema =
Config.Database.connection.searchPath[
Config.Database.connection.searchPath.length - 1
];
}
return await detector({
knex: database,
schema: Config.Database.connection.database,
schema,
});
};

Expand Down
11 changes: 11 additions & 0 deletions tests/integrations/docker-compose.postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3.9"

services:
database:
image: postgres:13
environment:
POSTGRES_DB: axeapi
POSTGRES_USER: axeapi
POSTGRES_PASSWORD: 123456
ports:
- "5433:5432"
Loading

0 comments on commit 8866be1

Please sign in to comment.