-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add defineConfig/loadConfig to pgkit package
- Loading branch information
Showing
4 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
export type Config = { | ||
client: { | ||
connectionString: string | ||
} | ||
typegen?: { | ||
connectionString?: string | ||
} | ||
migrator?: { | ||
connectionString?: string | ||
/** @default '${cwd}/migrations' */ | ||
migrationsTableName?: string | ||
/** @default 'migrations' */ | ||
migrationsPath?: string | ||
} | ||
} | ||
|
||
export const defineConfig = (config: Config) => config | ||
|
||
export const loadConfig = async () => { | ||
const importx = await import('importx') | ||
const configLocations = ['pgkit.config.ts', 'pgkit.config.js'] | ||
let config: Config | undefined | ||
let cwd = process.cwd() | ||
while (!config) { | ||
for (const configLocation of configLocations) { | ||
const filepath = path.join(cwd, configLocation) | ||
if (fs.existsSync(filepath)) { | ||
config = await importx.import(filepath, { | ||
parentURL: new URL(`file://${cwd}`), | ||
}) | ||
if ((config as {default?: Config}).default) { | ||
config = (config as {default?: Config}).default | ||
} | ||
if (!config) { | ||
throw new Error(`Config file ${filepath} doesn't export a valid config object.`) | ||
} | ||
break | ||
} | ||
} | ||
cwd = path.dirname(cwd) | ||
} | ||
|
||
if (!config) { | ||
throw new Error(`No config file found. Try creating one at ${path.join(cwd, configLocations[0])}`) | ||
} | ||
|
||
return config | ||
} |
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,5 @@ | ||
export default { | ||
client: { | ||
connectionString: 'postgresql://postgres:postgres@localhost:5432/postgres', | ||
}, | ||
} satisfies import('./packages/pgkit/src/config').Config |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.