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
6 changes: 3 additions & 3 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 generate a directory with the structure of your postgres database schemas. By default, generation runs migrations against an in-memory PGlite database, so a local Postgres server is not required.

## Features

Expand Down Expand Up @@ -44,7 +44,7 @@ npm install pgstrap --save-dev
npm run db:migrate
```

6. Generate types and structure:
6. Generate types and structure without needing a local Postgres server:
```bash
npm run db:generate
```
Expand All @@ -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 against an in-memory PGlite instance, so a local Postgres server is not required. Use `pgstrap generate --no-pglite` if you want to generate from `DATABASE_URL` 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 against an in-memory PGlite database before generating types",
})
},
async (argv) => {
generate({ ...(await getProjectContext()), pglite: !!argv.pglite })
generate({
...(await getProjectContext()),
pglite: argv.pglite !== false,
})
},
)
.parse()
118 changes: 66 additions & 52 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 All @@ -27,65 +27,79 @@ export const generate = async ({
const net = await import("node:net")

const db = new PGlite()
let server: ReturnType<typeof net.createServer> | undefined
const prevDbUrl = process.env.DATABASE_URL

await migrate({
client: db as any,
migrationsDir,
defaultDatabase,
cwd: process.cwd(),
schemas,
})
try {
await migrate({
client: db as any,
migrationsDir,
defaultDatabase,
cwd: process.cwd(),
schemas,
})

const server = net.createServer(async (socket) => {
const connection = await fromNodeSocket(socket, {
serverVersion: "16.3 (PGlite)",
auth: {
method: "password",
validateCredentials: ({ username, password }: any) =>
username === "postgres" && password === "postgres",
getClearTextPassword: () => "postgres",
},
async onStartup() {
await (db as any).waitReady
},
async onMessage(data: Uint8Array, { isAuthenticated }: any) {
if (!isAuthenticated) return
try {
const { data: responseData } = await (db as any).execProtocol(data)
return responseData
} catch {
return undefined
}
},
server = net.createServer(async (socket) => {
await fromNodeSocket(socket, {
serverVersion: "16.3 (PGlite)",
auth: {
method: "password",
validateCredentials: ({ username, password }: any) =>
username === "postgres" && password === "postgres",
getClearTextPassword: () => "postgres",
},
async onStartup() {
await (db as any).waitReady
},
async onMessage(data: Uint8Array, { isAuthenticated }: any) {
if (!isAuthenticated) return
try {
const { data: responseData } = await (db as any).execProtocol(
data,
)
return responseData
} catch {
return undefined
}
},
})
})
})

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

const prevDbUrl = process.env.DATABASE_URL
process.env.DATABASE_URL = connectionString
process.env.DATABASE_URL = connectionString

await zg.generate({
db: {
connectionString,
},
schemas: Object.fromEntries(
schemas.map((s) => [s, { include: "*", exclude: [] }]),
),
outDir: dbDir,
})
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,
})
await dumpTree({
targetDir: path.join(dbDir, "structure"),
defaultDatabase: "postgres",
schemas,
})
} finally {
if (server?.listening) {
await new Promise<void>((resolve, reject) => {
server!.close((err?: Error) => {
if (err) reject(err)
else resolve()
})
})
}
await (db as any).close?.()

server.close()
if (prevDbUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = prevDbUrl
if (prevDbUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = prevDbUrl
}
return
}

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

test("generate with pglite runs migrations and dumps structure", async () => {
test("generate defaults to pglite and dumps structure without postgres", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-generate-"))
const prevDbUrl = process.env.DATABASE_URL
const migrationsDir = path.join(tmp, "migrations")
fs.mkdirSync(migrationsDir, { recursive: true })
fs.writeFileSync(
Expand All @@ -27,7 +28,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 All @@ -42,6 +42,7 @@ test("generate with pglite runs migrations and dumps structure", async () => {

expect(fs.existsSync(zapatosFile)).toBe(true)
expect(fs.existsSync(path.join(structureDir, "table.sql"))).toBe(true)
expect(process.env.DATABASE_URL).toBe(prevDbUrl)

fs.rmSync(tmp, { recursive: true, force: true })
})