Skip to content

Commit

Permalink
some tidyup (updated 16:37)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Sep 4, 2024
1 parent 1ca75b9 commit c30762d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 56 deletions.
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Documentation for pgkit.dev",
"scripts": {
"generate": "tsx scripts/build",
"build": "rm -rf .next && next build",
"build": "rm -rf .next && next build && echo build done",
"dev": "next dev"
},
"dependencies": {
Expand Down
53 changes: 1 addition & 52 deletions packages/typegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ export const generate = async (params: Partial<Options>) => {
const {psql: _psql} = psqlClient(`${psqlCommand} "${connectionString}"`, pool)

const _gdesc = (sql: string) => {
sql = sql.trim().replace(/;$/, '')
// // assert.ok(!sql.includes(';'), `Can't use \\gdesc on query containing a semicolon. Query: ${sql}`)
// if (sql.includes(';')) {
// const simplified = tryOrDefault(
// () => removeSimpleComments(sql),
// tryOrDefault(() => toSql.statement(parseFirst(sql)), ''),
// )
// }

return neverthrow.ok(sql)
.map(sql => sql.trim().replace(/;$/, ''))
.andThen(sql => sql.includes(';') ? neverthrow.ok(removeSimpleComments(sql)) : neverthrow.ok(sql))
Expand All @@ -61,21 +52,6 @@ export const generate = async (params: Partial<Options>) => {
}
return new Error(message, {cause: err})
}))

// try {
// const result = await psql(`${sql} \\gdesc`)
// return neverthrow.ok(result)
// } catch {
// const simplified = tryOrDefault(
// () => removeSimpleComments(sql),
// tryOrDefault(() => toSql.statement(parseFirst(sql)), ''),
// )

// return await psql(`${simplified} \\gdesc`).then(
// good => neverthrow.ok(good),
// bad => neverthrow.err(new Error(`Simplified query failed: ${simplified} (original: ${sql})`, {cause: bad})),
// )
// }
}

const psql = memoizee(_psql, {max: 1000})
Expand Down Expand Up @@ -234,7 +210,7 @@ export const generate = async (params: Partial<Options>) => {
const describeQuery = async (query: ExtractedQuery) => {
const typeability = getTypeability(query.template)
if (typeability.isErr()) {
return typeability.mapErr(err => new Error(`${getLogQueryReference(query)} [!] Query is not typeable.`, {cause: err}))
return typeability.mapErr(err => new Error(`${getLogQueryReference(query)} [!] Query is not typeable.`, {cause: err})) satisfies neverthrow.Result<any, Error> as never
}

const fieldsResult = await getFields(query)
Expand All @@ -257,33 +233,6 @@ export const generate = async (params: Partial<Options>) => {
}))

return res


// try {
// if (isUntypeable(query.template)) {
// logger.debug(`${getLogQueryReference(query)} [!] Query is not typeable.`)
// return null
// }

// return {
// ...query,
// fields: await getFields(query),
// parameters: query.file.endsWith('.sql') ? await getParameters(query) : [],
// }
// } catch (e) {
// console.error(e.stack)
// let message = `${getLogQueryReference(query)} [!] Extracting types from query failed: ${e}.`
// if (query.sql.includes('--')) {
// message += ' Try moving comments to dedicated lines.'
// }

// if (query.sql.includes(';')) {
// message += ` Try removing trailing semicolons, separating multi-statement queries into separate queries, using a template variable for semicolons inside strings, or ignoring this query.`
// }

// logger.warn(message)
// return null
// }
}

// use pgsql-ast-parser or fallback to add column information (i.e. nullability)
Expand Down
10 changes: 9 additions & 1 deletion packages/typegen/src/query/analyze-select-statement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Client, Queryable, sql, Transactable} from '@pgkit/client'
import {Queryable, sql, Transactable} from '@pgkit/client'
import * as assert from 'assert'
import * as lodash from 'lodash'
import {parse, toSql} from 'pgsql-ast-parser'
Expand Down Expand Up @@ -61,6 +61,14 @@ export const analyzeSelectStatement = async (
`

const results = await t.any<SelectStatementAnalyzedColumn>(analyzedColumns)
.catch((e: unknown): SelectStatementAnalyzedColumn[] => {
if (String(e).match(/column .* has pseudo-type/)) {
// e.g. `select pg_advisory_lock(1)`
return []
}
throw e
})

const deduped = lodash.uniqBy<SelectStatementAnalyzedColumn>(results, JSON.stringify)
const formattedSqlStatements = lodash.uniqBy(deduped, r => r.formatted_query)

Expand Down
24 changes: 22 additions & 2 deletions packages/typegen/test/limitations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,17 @@ test('queries with comments are modified', async () => {
-- comment
test_table -- comment
---
(original:
select
1 as a, -- comment
-- comment
2 as b,
'--' as c, -- comment
id
from
-- comment
test_table -- comment
)
Try moving comments to dedicated lines.
`)
})
Expand Down Expand Up @@ -348,7 +358,17 @@ test('queries with complex CTEs and comments fail with helpful warning', async (
)
select * from def
---
(original:
with abc as (
select table_name -- comment
from information_schema.tables
),
def as (
select table_schema
from information_schema.tables, abc
)
select * from def
)
Try moving comments to dedicated lines.
`)
})
Expand Down

0 comments on commit c30762d

Please sign in to comment.