-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to use in-memory and file DB ion production build
- Loading branch information
Showing
8 changed files
with
94 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
type AssetsPaths = | ||
| { assets: string; routes: string } | ||
| { assets: undefined; routes: undefined } | ||
|
||
export type Config = { | ||
db: string | ||
env: 'development' | 'production' | 'test' | ||
} & AssetsPaths | ||
|
||
function getDefaultDatabase(env: Config['env']): string { | ||
if (env === 'production') { | ||
throw new Error('Set DATABASE_URL with PostgreSQL credentials') | ||
} else if (env === 'test') { | ||
return 'memory://' | ||
} else { | ||
return 'file://./db/pgdata' | ||
} | ||
} | ||
|
||
function getPaths(from: Record<string, string | undefined>): AssetsPaths { | ||
if (from.ASSETS_DIR && from.ROUTES_FILE) { | ||
return { assets: from.ASSETS_DIR, routes: from.ROUTES_FILE } | ||
} else if (!from.ASSETS_DIR && !from.ROUTES_FILE) { | ||
return { assets: undefined, routes: undefined } | ||
} else { | ||
throw new Error('ASSETS_DIR and ROUTES_FILE must be set together') | ||
} | ||
} | ||
|
||
export function getConfig(from: Record<string, string | undefined>): Config { | ||
let env = from.NODE_ENV ?? 'development' | ||
if (env !== 'test' && env !== 'production' && env !== 'development') { | ||
throw new Error('Unknown NODE_ENV') | ||
} | ||
return { | ||
db: from.DATABASE_URL ?? getDefaultDatabase(env), | ||
env, | ||
...getPaths(from) | ||
} | ||
} | ||
|
||
export const config = getConfig(process.env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,61 @@ | ||
import { deepStrictEqual, equal, throws } from 'node:assert' | ||
import { test } from 'node:test' | ||
|
||
import { getEnv } from '../env.ts' | ||
import { config, getConfig } from '../config.ts' | ||
|
||
const DATABASE_URL = 'postgresql://user:pass@localhost:5432/db' | ||
|
||
test('throws on missed DATABASE_URL in production', () => { | ||
throws(() => { | ||
getEnv({ NODE_ENV: 'production' }) | ||
getConfig({ NODE_ENV: 'production' }) | ||
}, /Set DATABASE_URL with PostgreSQL credentials/) | ||
equal( | ||
getEnv({ | ||
getConfig({ | ||
DATABASE_URL, | ||
NODE_ENV: 'production' | ||
}).DATABASE_URL, | ||
}).db, | ||
DATABASE_URL | ||
) | ||
equal(getEnv({ NODE_ENV: 'test' }).DATABASE_URL, undefined) | ||
equal(getEnv({ DATABASE_URL, NODE_ENV: 'test' }).DATABASE_URL, DATABASE_URL) | ||
equal(getConfig({ NODE_ENV: 'test' }).db, 'memory://') | ||
equal(getConfig({ DATABASE_URL, NODE_ENV: 'test' }).db, DATABASE_URL) | ||
equal(getConfig({ NODE_ENV: 'development' }).db, 'file://./db/pgdata') | ||
equal(getConfig({ DATABASE_URL, NODE_ENV: 'development' }).db, DATABASE_URL) | ||
}) | ||
|
||
test('checks that assets is together with routes', () => { | ||
throws(() => { | ||
getEnv({ ASSETS_DIR: './dist/' }) | ||
getConfig({ ASSETS_DIR: './dist/' }) | ||
}, /ASSETS_DIR and ROUTES_FILE/) | ||
throws(() => { | ||
getEnv({ ROUTES_FILE: './routes.regexp' }) | ||
getConfig({ ROUTES_FILE: './routes.regexp' }) | ||
}, /ASSETS_DIR and ROUTES_FILE/) | ||
}) | ||
|
||
test('checks NODE_ENV', () => { | ||
equal(getEnv({}).NODE_ENV, 'development') | ||
equal(getEnv({ NODE_ENV: 'test' }).NODE_ENV, 'test') | ||
equal(getConfig({}).env, 'development') | ||
equal(getConfig({ NODE_ENV: 'test' }).env, 'test') | ||
throws(() => { | ||
getEnv({ NODE_ENV: 'staging' }) | ||
getConfig({ NODE_ENV: 'staging' }) | ||
}, /NODE_ENV/) | ||
}) | ||
|
||
test('passes keys', () => { | ||
deepStrictEqual( | ||
getEnv({ | ||
getConfig({ | ||
ASSETS_DIR: './dist/', | ||
DATABASE_URL, | ||
NODE_ENV: 'production', | ||
ROUTES_FILE: './routes.regexp' | ||
}), | ||
{ | ||
ASSETS_DIR: './dist/', | ||
DATABASE_URL, | ||
NODE_ENV: 'production', | ||
ROUTES_FILE: './routes.regexp' | ||
assets: './dist/', | ||
db: DATABASE_URL, | ||
env: 'production', | ||
routes: './routes.regexp' | ||
} | ||
) | ||
}) | ||
|
||
test('has predefined config', () => { | ||
equal(config.env, 'test') | ||
}) |