Skip to content

Commit

Permalink
Merge pull request #42 from dynajoe/add-types-upgrade-ts
Browse files Browse the repository at this point in the history
Add types to query context on PgError
  • Loading branch information
stephenreddek committed Mar 18, 2024
2 parents 0bee559 + 5a3dad2 commit a72a46e
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 93 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [12, 14, 16]
node-version: [14, 16, 18, 20]

services:
postgres:
Expand All @@ -36,4 +36,4 @@ jobs:
- name: Test
run: |
npm ci
npm test
npm run test
36 changes: 18 additions & 18 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "tinypg",
"version": "7.0.1",
"version": "8.0.0",
"description": "Easy way to call sql files using postgres.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -32,7 +32,7 @@
],
"license": "MIT",
"engines": {
"node": ">= 12.0.0"
"node": ">= 14.0.0"
},
"dependencies": {
"@scaleleap/pg-format": "^1.0.0",
Expand All @@ -44,7 +44,7 @@
},
"devDependencies": {
"@types/chai": "3.4.34",
"@types/lodash": "^4.14.169",
"@types/lodash": "4.14.158",
"@types/mocha": "2.2.41",
"@types/node": "^8.10.59",
"@types/pg": "^8.6.0",
Expand All @@ -54,7 +54,7 @@
"pg": "^8.0.0",
"rimraf": "^3.0.2",
"source-map-support": "^0.5.19",
"typescript": "^3.9.7"
"typescript": "^5.1.3"
},
"peerDependencies": {
"pg": "^8.0.0"
Expand Down
18 changes: 0 additions & 18 deletions src/errors.ts

This file was deleted.

15 changes: 12 additions & 3 deletions src/index.ts
Expand Up @@ -4,9 +4,18 @@ import { TinyPg } from './tiny'

export { TinyPg, FormattableDbCall } from './tiny'

export { Result, TinyPgOptions, QueryBeginContext, QueryCompleteContext, SqlParseResult, SqlFile, TinyPgParams, TinyCallContext } from './types'

export { TinyPgError, TinyPgErrorTransformer } from './errors'
export {
Result,
TinyPgOptions,
QueryBeginContext,
QueryCompleteContext,
SqlParseResult,
SqlFile,
TinyPgParams,
TinyCallContext,
TinyPgError,
TinyPgErrorTransformer,
} from './types'

export { parseFiles } from './parser'

Expand Down
3 changes: 1 addition & 2 deletions src/parser.ts
Expand Up @@ -2,7 +2,6 @@ import * as T from './types'
import * as _ from 'lodash'
import * as Fs from 'fs'
import * as Path from 'path'
import * as E from './errors'
import { parseSql } from 'tinypg-parser'

const Glob = require('glob')
Expand Down Expand Up @@ -44,7 +43,7 @@ export function parseFiles(root_directories: string[]): T.SqlFile[] {
return c[0].relative_path
}).join(', ')}). All source files under root dirs must have different relative paths.`

throw new E.TinyPgError(message)
throw new T.TinyPgError(message)
}

return result
Expand Down
2 changes: 1 addition & 1 deletion src/test/helper.ts
Expand Up @@ -3,7 +3,7 @@ import * as Pg from 'pg'

export const connection_string = 'postgres://postgres@localhost:5432/tinypg_test?sslmode=disable'

export async function dbQuery(query: string, args: any[] = null): Promise<Pg.QueryResult> {
export async function dbQuery(query: string, args?: any[]): Promise<Pg.QueryResult> {
const client = new Pg.Client(connection_string)
await client.connect()

Expand Down
34 changes: 20 additions & 14 deletions src/test/hooks.ts
Expand Up @@ -60,12 +60,15 @@ describe('Hooks', () => {

describe('and there is a transaction', () => {
it('should keep the same transaction_id across multiple sql calls', async () => {
let transaction_ids = []
let query_ids = []
let transaction_ids: string[] = []
let query_ids: string[] = []

const hooks: TinyHooks = {
preSql: (ctx, name, params) => {
transaction_ids.push(ctx.transaction_id)
if (!_.isNil(ctx.transaction_id)) {
transaction_ids.push(ctx.transaction_id)
}

query_ids.push(ctx.query_id)
return { ctx: { ...ctx, foo: 'bar' }, args: [name, params] }
},
Expand Down Expand Up @@ -265,11 +268,14 @@ describe('Hooks', () => {

describe('and there is a transaction', () => {
it('should keep the same transaction_id across multiple query calls', async () => {
let transaction_ids = []
let query_ids = []
let transaction_ids: string[] = []
let query_ids: string[] = []
const hooks: TinyHooks = {
preRawQuery: (ctx, name, params) => {
transaction_ids.push(ctx.transaction_id)
if (!_.isNil(ctx.transaction_id)) {
transaction_ids.push(ctx.transaction_id)
}

query_ids.push(ctx.query_id)
return { ctx: { ...ctx, foo: 'bar' }, args: [name, params] }
},
Expand Down Expand Up @@ -428,7 +434,7 @@ describe('Hooks', () => {
describe('transaction', () => {
it('should thread transaction context (withHooks and via options)', async () => {
let final_context: any
let tx_id: string
let tx_id: string = ''

const hooks: TinyHooks = {
preTransaction: transaction_id => {
Expand Down Expand Up @@ -479,10 +485,10 @@ describe('Hooks', () => {

describe('and every hook throws an error', () => {
it('should catch the errors', async () => {
let pre_tx_id: string
let commit_tx_id: string
let rollback_tx_id: string
let begin_tx_id: string
let pre_tx_id: string | null = null
let commit_tx_id: string | null = null
let rollback_tx_id: string | null = null
let begin_tx_id: string | null = null

const hooks: TinyHooks = {
preTransaction: transaction_id => {
Expand Down Expand Up @@ -593,8 +599,8 @@ describe('Hooks', () => {
})

describe('with async_hooks', () => {
let final_context = null
let hooked_tiny = null
let final_context: any | null = null
let hooked_tiny: TinyPg | null = null

beforeEach(() => {
hooked_tiny = tiny.withHooks({
Expand All @@ -620,7 +626,7 @@ describe('Hooks', () => {
it('should run preSql in the same execution context', async () => {
const caller_async_execution_id = AsyncHooks.executionAsyncId()

await hooked_tiny.sql('a.select')
await hooked_tiny!.sql('a.select')

expect(caller_async_execution_id).to.not.equal(0)
expect(final_context.preSql.async_id).to.equal(caller_async_execution_id)
Expand Down
4 changes: 2 additions & 2 deletions src/test/multiple_dirs.ts
@@ -1,6 +1,6 @@
import * as H from './helper'
import * as E from '../errors'
import { expect } from 'chai'
import * as T from '../types'

describe('Multiple root directories', () => {
it('should allow specifying multiple directories that do not conflict', () => {
Expand All @@ -17,6 +17,6 @@ describe('Multiple root directories', () => {
H.newTiny({
root_dir: [__dirname + '/multi/a_sql', __dirname + '/sql'],
})
}).to.throw(E.TinyPgError)
}).to.throw(T.TinyPgError)
})
})
8 changes: 4 additions & 4 deletions src/test/parsing.ts
Expand Up @@ -13,13 +13,13 @@ describe('parseFiles', () => {

it('should parse files', () => {
const parse_file_marker = _.find(result, x => x.key.indexOf('parse_file_test_marker') != -1)
expect(parse_file_marker.name).to.equal('a_parse_file_test_marker')
expect(parse_file_marker.relative_path).to.equal('a/parse_file_test_marker.sql')
expect(parse_file_marker?.name).to.equal('a_parse_file_test_marker')
expect(parse_file_marker?.relative_path).to.equal('a/parse_file_test_marker.sql')
})

it('should correctly format root level file names', () => {
const root_level_file = _.find(result, x => x.key.indexOf('root_level_sql_file') != -1)
expect(root_level_file.name).to.equal('root_level_sql_file')
expect(root_level_file.relative_path).to.equal('root_level_sql_file.sql')
expect(root_level_file?.name).to.equal('root_level_sql_file')
expect(root_level_file?.relative_path).to.equal('root_level_sql_file.sql')
})
})

0 comments on commit a72a46e

Please sign in to comment.