Skip to content

Commit

Permalink
feat(api): migrated database from mongodb to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Oct 7, 2018
1 parent 669f6e3 commit af401cc
Show file tree
Hide file tree
Showing 36 changed files with 221 additions and 436 deletions.
115 changes: 0 additions & 115 deletions .deploy/mongodb/README.md

This file was deleted.

106 changes: 0 additions & 106 deletions .deploy/mongodb/mongodb.tmpl.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .deploy/postgres/scripts/create_databases.sh
@@ -0,0 +1,23 @@
#!/bin/bash

set -e
set -u

function create_user_and_database() {
local database=$1
local username=$2
local password=$3
echo " Creating user '$username' and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE ROLE $username WITH PASSWORD '$password' NOSUPERUSER LOGIN;
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $username;
EOSQL
}

if [ -n "$COCKPIT_POSTGRES_DB" ]; then
create_user_and_database $COCKPIT_POSTGRES_DB $COCKPIT_POSTGRES_USER $COCKPIT_POSTGRES_PASSWORD
fi
if [ -n "$KEYCLOAK_POSTGRES_DB" ]; then
create_user_and_database $KEYCLOAK_POSTGRES_DB $KEYCLOAK_POSTGRES_USER $KEYCLOAK_POSTGRES_PASSWORD
fi
11 changes: 11 additions & 0 deletions .deploy/postgres/scripts/keycloak-init.sql
@@ -0,0 +1,11 @@
-- Keycloak table creation
-- Keycloak database Creation

-- Create access role for keycloak
CREATE ROLE keycloak WITH PASSWORD 'keycloak123' NOSUPERUSER LOGIN;

-- Create database for keycloak
CREATE DATABASE keycloak;

-- Grant all permissions to user keycloak
GRANT ALL ON DATABASE keycloak TO keycloak;
18 changes: 18 additions & 0 deletions apps/api-e2e/jest.config.js
@@ -0,0 +1,18 @@
module.exports = {
name: 'api-e2e',
testRegex: '.spec.ts$',
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular/preprocessor.js',
},
moduleFileExtensions: ['js', 'json', 'ts'],
collectCoverage: true,
coverageReporters: ['html'],
testEnvironment: 'node',
coverageDirectory: '../../coverage/apps/api-e2e',
globals: {
'ts-jest': {
tsConfig: "./tsconfig.e2e.json"
}
}
};

@@ -1,7 +1,10 @@
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { INestApplication } from '@nestjs/common';
// @ts-ignore
import { AppModule } from '../../api/src/app.module';

jest.setTimeout(30000);

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand Down
10 changes: 10 additions & 0 deletions apps/api-e2e/tsconfig.e2e.json
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc/apps/api-e2e",
"module": "commonjs",
"target": "es6",
"types": ["jest", "node"]
},
"include": ["**/*.ts"]
}
30 changes: 30 additions & 0 deletions apps/api-e2e/tslint.json
@@ -0,0 +1,30 @@
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"jsRules": {
"no-unused-expression": true
},
"rules": {
"eofline": false,
"quotemark": [true, "single"],
"indent": false,
"member-access": [false],
"ordered-imports": [false],
"max-line-length": [true, 150],
"member-ordering": [false],
"curly": false,
"interface-name": [false],
"array-type": [false],
"no-console": false,
"no-empty-interface": false,
"no-empty": false,
"arrow-parens": false,
"object-literal-sort-keys": false,
"no-unused-expression": false,
"max-classes-per-file": [false],
"variable-name": [false],
"one-line": [false],
"one-variable-per-declaration": [false]
},
"rulesDirectory": []
}
4 changes: 3 additions & 1 deletion apps/api/API-TESTING.md
Expand Up @@ -3,7 +3,9 @@ API Testing

### Database
```bash
mongo -u "mdbuser" -p "cockpit123" --authenticationDatabase "cockpit"
psql -h <host> -p <port> -u <database>
psql -h <host> -p <port> -U <username> -W <password> <database>

```

### REST API
Expand Down
22 changes: 12 additions & 10 deletions apps/api/README.md
Expand Up @@ -26,17 +26,22 @@ Code Style | [Prettier](https://github.com/prettier/prettier) & [TS-Lint]
#### MongoDB
> start mongodb
```bash
# start local mongodb
docker-compose up -V mongodb
# stop local mongodb before restart again
docker-compose down -v
# start local postgres
docker-compose up postgres
docker-compose up -V postgres #remove volumes

# stop local postgres before restart again
docker-compose down
docker-compose down -v #remove volumes
```
> if error `The container name "/mongodb" is already in use by container`, remove orphan container.
> if error `The container name "/postgres" is already in use by container`, remove orphan container.
```bash
docker ps -a
docker rm 82be5234c94a
```

> make sure to delete **dist/apps/api** before running any of the following commands.
> other wish old .js **entity** classes my conflict and cause unexpected bugs.
#### Run Dev Mode
```bash
Expand Down Expand Up @@ -102,13 +107,10 @@ nest g exception auth --dry-run
> coverage will be generate in coverage/apps/api
```bash
# unit tests
npm run api:test

# test coverage
npm run api:test:cov
ng test api

# e2e tests
npm run api:e2e
ng test api-e2e
```


Expand Down
4 changes: 2 additions & 2 deletions apps/api/jest.config.js
@@ -1,5 +1,5 @@
module.exports = {
name: 'api',
preset: '../../jest.config.nest.js',
"coverageDirectory": "../../coverage/apps/api",
preset: '../../jest.config.js',
coverageDirectory: '../../coverage/apps/api'
};

0 comments on commit af401cc

Please sign in to comment.