Skip to content

Commit 6030b13

Browse files
authored
Merge pull request #4 from StrontiumJS/master
sync fork
2 parents a5db8b3 + 31d4cea commit 6030b13

File tree

5 files changed

+72
-18
lines changed

5 files changed

+72
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strontium",
3-
"version": "2.7.0",
3+
"version": "2.7.4",
44
"description": "Strontium is a TypeScript toolkit for High Performance API servers built for Production not Projects.",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",

src/bootstrap/drivers/Environment.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Container } from "inversify"
2+
import { Logger } from "../../logging"
3+
import { Process } from "../../runtime"
4+
import { ObjectValidator, ValidatedObject, isObject } from "../../validation"
5+
6+
export class Environment<O extends ObjectValidator> implements Process {
7+
private validatedEnvironment?: ValidatedObject<O>
8+
9+
constructor(private validator: O) {}
10+
11+
public getKey<K extends keyof ValidatedObject<O>>(
12+
key: K
13+
): ValidatedObject<O>[K] {
14+
if (this.validatedEnvironment !== undefined) {
15+
return this.validatedEnvironment[key]
16+
} else {
17+
throw new Error(
18+
"An environment value was accessed before the Environment container completed initialization."
19+
)
20+
}
21+
}
22+
23+
public isHealthy(): boolean {
24+
return this.validatedEnvironment !== undefined
25+
}
26+
27+
public async startup(container: Container): Promise<void> {
28+
container.bind(Environment).toConstantValue(this)
29+
30+
try {
31+
this.validatedEnvironment = await isObject(this.validator)(
32+
process.env
33+
)
34+
} catch (e) {
35+
if (container.isBound(Logger)) {
36+
container.get(Logger).error("Environment validation failed!", e)
37+
} else {
38+
console.error(e)
39+
}
40+
41+
throw e
42+
}
43+
}
44+
45+
public async shutdown(container: Container): Promise<void> {
46+
container.unbind(Environment)
47+
48+
this.validatedEnvironment = undefined
49+
}
50+
}

src/bootstrap/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./drivers/Environment"

src/datastore/drivers/pg/PGStore.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,26 @@ export class PGStore implements Process, SQLStore {
4646

4747
let connection = await this.connection.connect()
4848

49-
// By default, use PostgreSQL default isolation level (READ COMMITTED)
50-
let query = "BEGIN"
51-
if (isolationLevel !== undefined) {
52-
switch (isolationLevel) {
53-
case PGIsolationLevel.SERIALIZABLE:
54-
query = "BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE"
55-
break
56-
case PGIsolationLevel.REPEATABLE_READ:
57-
query = "BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ"
58-
break
59-
case PGIsolationLevel.READ_COMMITED:
60-
query = "BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED"
61-
break
62-
case PGIsolationLevel.READ_UNCOMMITTED:
63-
query = "BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"
64-
break
65-
}
49+
// By default, use PostgreSQL default isolation level (READ COMMITTED out of the box)
50+
let query: string
51+
switch (isolationLevel) {
52+
case PGIsolationLevel.SERIALIZABLE:
53+
query = "BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE"
54+
break
55+
case PGIsolationLevel.REPEATABLE_READ:
56+
query = "BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ"
57+
break
58+
case PGIsolationLevel.READ_COMMITED:
59+
query = "BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED"
60+
break
61+
case PGIsolationLevel.READ_UNCOMMITTED:
62+
query = "BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"
63+
break
64+
default:
65+
query = "BEGIN"
66+
break
6667
}
68+
6769
await connection.query(query)
6870

6971
return new PGTransaction(connection, this.logger)

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./bootstrap"
12
export * from "./cryptography"
23
export * from "./datastore"
34
export * from "./errors"

0 commit comments

Comments
 (0)