Skip to content

Commit

Permalink
Use mongodb URI
Browse files Browse the repository at this point in the history
  • Loading branch information
0xslipk committed Dec 9, 2022
1 parent 5552fbd commit 2ae51a8
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ DATABASE_NAME=ink
DATABASE_USERNAME=mongodb
DATABASE_PASSWORD=mongodb
DATABASE_PORT=27017
DATABASE_SSL_CA=""
DATABASE_RETRY_ATTEMPTS=5
DATABASE_RETRY_DELAY=3000
DATABASE_URI=

WS_PROVIDER=wss://rococo-contracts-rpc.polkadot.io
LOAD_ALL_BLOCKS=false
Expand Down
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DATABASE_NAME=ink
DATABASE_USERNAME=root
DATABASE_PASSWORD=password
DATABASE_PORT=5432
DATABASE_SSL_CA=""
DATABASE_URI=""
DATABASE_RETRY_ATTEMPTS=5
DATABASE_RETRY_DELAY=3000
WS_PROVIDER=ws://127.0.0.1:9944
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.1
Published by **[blockcoders](https://github.com/blockcoders)** on **2022/12/09**
- [#15](https://github.com/blockcoders/nestjs-ethers/pull/15) Migrate to mongodb

## 1.1.0
Published by **[blockcoders](https://github.com/blockcoders)** on **2022/12/09**
- [#15](https://github.com/blockcoders/nestjs-ethers/pull/15) Migrate to mongodb
Expand Down
2 changes: 1 addition & 1 deletion README-es.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ query {
```graphql
{
"data": {
"version": "v1.1.0"
"version": "v1.1.1"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ query {
```graphql
{
"data": {
"version": "v1.1.0"
"version": "v1.1.1"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ink-substrate-explorer-api",
"version": "1.1.0",
"version": "1.1.1",
"description": "Ink Explorer is an application that provides Ink contracts related information on Substrate based blockchains.",
"author": "Blockcoders <engineering@blockcoders.io>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/app.resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('AppResolver', () => {

describe('version', () => {
it('should return the current version', () => {
expect(resolver.version()).toEqual('v1.1.0')
expect(resolver.version()).toEqual('v1.1.1')
})
})
})
2 changes: 1 addition & 1 deletion src/app.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export class AppResolver {

@Query(/* istanbul ignore next */ () => String)
version(): string {
return 'v1.1.0'
return 'v1.1.1'
}
}
18 changes: 10 additions & 8 deletions src/db/db.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ import { DbService } from './db.service'
useFactory: async (env: EnvService) => {
const config: TypeOrmModuleOptions = {
type: 'mongodb',
host: env.DATABASE_HOST,
port: env.DATABASE_PORT,
username: env.DATABASE_USERNAME,
password: env.DATABASE_PASSWORD,
database: env.DATABASE_NAME,
retryAttempts: env.DATABASE_RETRY_ATTEMPTS,
retryDelay: env.DATABASE_RETRY_DELAY,
synchronize: true,
autoLoadEntities: true,
keepConnectionAlive: false,
}

if (!env.DATABASE_SSL_CA) {
return config
if (env.DATABASE_URI) {
return {
...config,
url: env.DATABASE_URI,
}
}

return {
...config,
sslCA: env.DATABASE_SSL_CA,
host: env.DATABASE_HOST,
port: env.DATABASE_PORT,
username: env.DATABASE_USERNAME,
password: env.DATABASE_PASSWORD,
database: env.DATABASE_NAME,
}
},
}),
Expand Down
20 changes: 13 additions & 7 deletions src/db/db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ export class DbService {
) {}

async addIndexes() {
const host = this.env.DATABASE_HOST
const port = this.env.DATABASE_PORT
const username = this.env.DATABASE_USERNAME
const password = this.env.DATABASE_PASSWORD
const database = this.env.DATABASE_NAME

const con = await mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`)
let con: typeof mongoose

if (this.env.DATABASE_URI) {
con = await mongoose.connect(this.env.DATABASE_URI)
} else {
const host = this.env.DATABASE_HOST
const port = this.env.DATABASE_PORT
const username = this.env.DATABASE_USERNAME
const password = this.env.DATABASE_PASSWORD
const database = this.env.DATABASE_NAME

con = await mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`)
}

// block indexes
const blocks = con.connection.collection('blocks')
Expand Down
4 changes: 2 additions & 2 deletions src/env/env.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class EnvService {
public readonly DATABASE_NAME: string
public readonly DATABASE_USERNAME: string
public readonly DATABASE_PASSWORD: string
public readonly DATABASE_SSL_CA: string
public readonly DATABASE_URI: string
public readonly DATABASE_RETRY_ATTEMPTS: number
public readonly DATABASE_RETRY_DELAY: number
public readonly WS_PROVIDER: string
Expand All @@ -45,7 +45,7 @@ export class EnvService {
this.DATABASE_NAME = this.config.get<string>('DATABASE_NAME', '')
this.DATABASE_USERNAME = this.config.get<string>('DATABASE_USERNAME', '')
this.DATABASE_PASSWORD = this.config.get<string>('DATABASE_PASSWORD', 'password')
this.DATABASE_SSL_CA = this.config.get<string>('DATABASE_SSL_CA', '')
this.DATABASE_URI = this.config.get<string>('DATABASE_URI', '')
this.DATABASE_RETRY_ATTEMPTS = parseInt(this.config.get<string>('DATABASE_RETRY_ATTEMPTS', '20'), 10)
this.DATABASE_RETRY_DELAY = parseInt(this.config.get<string>('DATABASE_RETRY_DELAY', '6000'), 10)
this.WS_PROVIDER = this.config.get<string>('WS_PROVIDER', '')
Expand Down
2 changes: 1 addition & 1 deletion src/env/env.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class EnvironmentVariables {
DATABASE_PASSWORD: string | undefined

@IsString()
DATABASE_SSL_CA: string | undefined
DATABASE_URI: string | undefined

@IsNumberString()
DATABASE_RETRY_ATTEMPTS: string | undefined
Expand Down

0 comments on commit 2ae51a8

Please sign in to comment.