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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![npm version](https://badge.fury.io/js/pgstrap.svg)](https://badge.fury.io/js/pgstrap)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

pgstrap allows you to easily run typescript migrations or generate a directory that represents your database schemas with `table.sql` files. Run `pgstrap generate` to generate a directory with the structure of your postgres database schemas!
pgstrap allows you to easily run typescript migrations or generate a directory that represents your database schemas with `table.sql` files. Run `pgstrap generate` to apply migrations in an in-memory PGlite database and generate a directory with the structure of your postgres database schemas.

## Features

Expand Down Expand Up @@ -55,7 +55,7 @@ npm install pgstrap --save-dev

- `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 by running migrations against an in-memory PGlite instance. Use `pgstrap generate --no-pglite` to generate from a running Postgres database instead.
- `npm run db:create-migration` - Create a new migration file

### Configuration
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:
"Run migrations in an in-memory PGlite database before generating types",
})
},
async (argv) => {
generate({ ...(await getProjectContext()), pglite: !!argv.pglite })
generate({
...(await getProjectContext()),
pglite: argv.pglite !== false,
})
},
)
.parse()
43 changes: 24 additions & 19 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 @@ -67,25 +67,30 @@ export const generate = async ({
const prevDbUrl = process.env.DATABASE_URL
process.env.DATABASE_URL = connectionString

await zg.generate({
db: {
connectionString,
},
schemas: Object.fromEntries(
schemas.map((s) => [s, { include: "*", exclude: [] }]),
),
outDir: dbDir,
})

await dumpTree({
targetDir: path.join(dbDir, "structure"),
defaultDatabase: "postgres",
schemas,
})
try {
await zg.generate({
db: {
connectionString,
},
schemas: Object.fromEntries(
schemas.map((s) => [s, { include: "*", exclude: [] }]),
),
outDir: dbDir,
})

server.close()
if (prevDbUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = prevDbUrl
await dumpTree({
targetDir: path.join(dbDir, "structure"),
defaultDatabase: "postgres",
schemas,
})
} finally {
await new Promise<void>((resolve, reject) => {
server.close((err) => (err ? reject(err) : resolve()))
})
await db.close()
if (prevDbUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = prevDbUrl
}
return
}

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 runs migrations in pglite by default 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