Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ npm install pgstrap --save-dev
```bash
npm run db:generate
```
This runs against an in-memory PGlite database by default, so no local Postgres server is required.

## Usage

### Available Scripts

- `npm run db:migrate` - Run pending migrations
- `npm run db:reset` - Drop and recreate the database, then run all migrations
- `npm run db:generate` - Generate types and structure dumps. Use `pgstrap generate --pglite` to run migrations against an in-memory PGlite instance.
- `npm run db:generate` - Generate types and structure dumps using an in-memory PGlite instance. Use `pgstrap generate --no-pglite` to connect to a running Postgres database instead.
- `npm run db:create-migration` - Create a new migration file

### Configuration
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"test": "bun test",
"build": "tsup ./src/index.ts ./src/cli.ts --outDir ./dist --dts --sourcemap inline",
"db:generate": "bun run ./src/generate.ts",
"format": "biome format . --write",
"format:check": "biome format ."
},
Expand Down
12 changes: 10 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ import { getProjectContext } from "./get-project-context"
"generate",
"generate types and sql documentation from database",
(yargs) => {
yargs.option("pglite", { type: "boolean", default: false })
yargs.option("pglite", {
type: "boolean",
default: true,
describe:
"Use an in-memory PGlite instance. Pass --no-pglite to connect to a running Postgres database.",
})
},
async (argv) => {
generate({ ...(await getProjectContext()), pglite: !!argv.pglite })
await generate({
...(await getProjectContext()),
pglite: argv.pglite !== false,
})
},
)
.parse()
4 changes: 2 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const generate = async ({
schemas,
defaultDatabase,
dbDir,
pglite = false,
pglite = true,
migrationsDir,
}: Pick<Context, "schemas" | "defaultDatabase" | "dbDir"> & {
pglite?: boolean
Expand Down Expand Up @@ -60,7 +60,7 @@ export const generate = async ({
})
})

await new Promise<void>((resolve) => server.listen(0, resolve))
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve))
const port = (server.address() as any).port
const connectionString = `postgres://postgres:postgres@127.0.0.1:${port}/postgres`

Expand Down
3 changes: 1 addition & 2 deletions tests/generate.pglite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.down = async (pgm) => {
}
`

test("generate with pglite runs migrations and dumps structure", async () => {
test("generate defaults to pglite and dumps structure", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-generate-"))
const migrationsDir = path.join(tmp, "migrations")
fs.mkdirSync(migrationsDir, { recursive: true })
Expand All @@ -27,7 +27,6 @@ test("generate with pglite runs migrations and dumps structure", async () => {
defaultDatabase: "postgres",
dbDir: path.join(tmp, "db"),
migrationsDir,
pglite: true,
})

const zapatosFile = path.join(tmp, "db", "zapatos", "schema.d.ts")
Expand Down