Skip to content

Commit

Permalink
add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
zfben committed Feb 2, 2025
1 parent ddafb6a commit d3ad589
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Run Tests
run: npm run ci
env:
PG_CONNECTION: postgresql://postgres:postgres@localhost:5432
DATABASE_URL: postgresql://postgres:postgres@localhost:5432
- uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
32 changes: 3 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
},
"funding": "https://github.com/sponsors/faasjs",
"scripts": {
"build": "tsup-node src/index.ts --config tsup.config.ts",
"build": "tsup-node --config tsup.config.ts",
"test": "vitest --run",
"ci": "vitest run --silent --coverage"
},
"bin": {
"typed-pg": "dist/cli/index.mjs"
},
"files": ["dist"],
"devDependencies": {
"postgres": "*",
"tsx": "*",
"tsup": "*",
"faasjs": "*",
"@faasjs/lint": "*",
Expand All @@ -40,7 +44,8 @@
"@vitest/coverage-v8": "*"
},
"peerDependencies": {
"postgres": "*"
"postgres": "*",
"tsx": "*"
},
"engines": {
"node": ">=22.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import postgres from 'postgres'

export const DATABASE_URL = process.env.DATABASE_URL as string || 'postgresql://development@pg/development'

export function createTestingPostgres() {
return postgres(process.env.PG_CONNECTION || 'postgresql://development@pg/development')
return postgres(DATABASE_URL)
}
10 changes: 10 additions & 0 deletions src/cli/__tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest'
import { main } from '../main'

describe('cli', () => {
it('DATABASE_URL is required', async () => {
delete process.env.DATABASE_URL

await expect(async () => main()).rejects.toThrow('DATABASE_URL not set, please run `DATABASE_URL=postgres://<your pg url> typed-pg`')
})
})
5 changes: 5 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env -S npx tsx

import { main } from './main'

main()
19 changes: 19 additions & 0 deletions src/cli/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import postgres from "postgres"

export async function main() {
const connection = process.env.DATABASE_URL as string

if (!connection)
throw Error('DATABASE_URL not set, please run `DATABASE_URL=postgres://<your pg url> typed-pg`')

const sql = postgres(connection)

try {
await sql`SELECT 1`
} catch (error) {
console.error(error)
throw Error('Error connecting to database, please check your DATABASE_URL')
}

console.log('Connected to database')
}
33 changes: 31 additions & 2 deletions src/schema-builder/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('SchemaBuilder', () => {
const builder = new SchemaBuilder(client)

builder.createTable('creators', table => {
table.string('string')
table.string('string').primary().unique()
table.number('number')
table.boolean('boolean')
table.date('date')
Expand Down Expand Up @@ -76,6 +76,16 @@ describe('SchemaBuilder', () => {
['creators']
)

expect(
indices.find(i => i.indexname.includes('creators_pkey'))
).toMatchObject({
schemaname: 'public',
tablename: 'creators',
indexname: 'creators_pkey',
tablespace: null,
indexdef: 'CREATE UNIQUE INDEX creators_pkey ON public.creators USING btree (string)',
})

expect(
indices.find(i => i.indexname.includes('idx_creators_string'))
).toMatchObject({
Expand Down Expand Up @@ -108,7 +118,10 @@ describe('SchemaBuilder', () => {
table.dropColumn('created_at')
table.alterColumn('updated_at', {
type: 'date',
defaultValue: 'NULL'
defaultValue: 'NULL',
primary: true,
unique: true,
check: 'updated_at > now()',
})
})

Expand All @@ -122,12 +135,28 @@ describe('SchemaBuilder', () => {
for (const column of [
{ column_name: 'new_string', data_type: 'character varying' },
{ column_name: 'number', data_type: 'integer' },
{ column_name: 'updated_at', data_type: 'date', column_default: null },
]) {
expect(
columns.find(c => c.column_name === column.column_name)
).toMatchObject(column)
}

const indices = await client.raw(
'SELECT * FROM pg_indexes WHERE tablename = ?',
['alters']
)

expect(
indices.find(i => i.indexname.includes('alters_pkey'))
).toMatchObject({
schemaname: 'public',
tablename: 'alters',
indexname: 'alters_pkey',
tablespace: null,
indexdef: 'CREATE UNIQUE INDEX alters_pkey ON public.alters USING btree (updated_at)',
})

await client.raw('DROP TABLE alters')
})
})
Loading

0 comments on commit d3ad589

Please sign in to comment.