-
Couldn't load subscription status.
- Fork 350
feat: Sensible pg defaults
#1545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shoooe
wants to merge
7
commits into
kysely-org:master
Choose a base branch
from
shoooe:feat/sensible-pg-defaults
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ede6230
Empty
shoooe 5968b6b
Date time, arrays and bigints
shoooe 383a499
WIP annotations
shoooe edaff35
Removed unused comment
shoooe 49b7da8
Review feedback
shoooe fede66a
Moved setTypeParser when creating connection
shoooe 1577828
Merge branch 'master' into feat/sensible-pg-defaults
shoooe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ dist | |
| node_modules | ||
| .vscode | ||
| .idea | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,11 @@ export interface PostgresDialectConfig { | |
| * Called every time a connection is acquired from the pool. | ||
| */ | ||
| onReserveConnection?: (connection: DatabaseConnection) => Promise<void> | ||
|
|
||
| /** | ||
| * @todo: docs | ||
| */ | ||
| types?: Record<number, (value: string) => any> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming is taken from the equivalent option in |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -61,6 +66,7 @@ export interface PostgresPoolClient { | |
| ): Promise<PostgresQueryResult<R>> | ||
| query<R>(cursor: PostgresCursor<R>): PostgresCursor<R> | ||
| release(): void | ||
| setTypeParser(typeOrOid: number, parser: (val: string) => any): void | ||
| } | ||
|
|
||
| export interface PostgresCursor<T> { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shoooe marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { Pool } from 'pg' | ||
| import { Generated, Kysely, PostgresDialect, sql } from '../../../' | ||
| import { jsonObjectFrom, SENSIBLE_TYPES } from '../../../helpers/postgres' | ||
|
|
||
| import { | ||
| destroyTest, | ||
| initTest, | ||
| TestContext, | ||
| expect, | ||
| Database, | ||
| insertDefaultDataSet, | ||
| clearDatabase, | ||
| DIALECTS, | ||
| DIALECT_CONFIGS, | ||
| PLUGINS, | ||
| } from './test-setup.js' | ||
|
|
||
| interface Values { | ||
| id: Generated<number> | ||
| bigint: string | ||
| timestamptz: string | ||
| timestamp: string | ||
| date: string | ||
| time: string | ||
| timetz: string | ||
| array: number[] | ||
| // @todo: bytea | ||
| // @todo: money | ||
| // @todo: range types | ||
| } | ||
|
|
||
| if (DIALECTS.includes('postgres')) { | ||
| const dialect = 'postgres' | ||
|
|
||
| describe(`${dialect} sensible defaults`, () => { | ||
| let ctx: TestContext | ||
| let db: Kysely<Database & { values: Values }> | ||
|
|
||
| before(async function () { | ||
| ctx = await initTest(this, dialect, {}) | ||
|
|
||
| await ctx.db.schema | ||
| .createTable('values') | ||
| .addColumn('id', 'serial', (col) => col.primaryKey()) | ||
| .addColumn('bigint', 'bigint', (col) => col.notNull()) | ||
| .addColumn('timestamptz', 'timestamptz', (col) => col.notNull()) | ||
| .addColumn('timestamp', 'timestamp', (col) => col.notNull()) | ||
| .addColumn('date', 'date', (col) => col.notNull()) | ||
| .addColumn('time', 'time', (col) => col.notNull()) | ||
| .addColumn('timetz', 'timetz', (col) => col.notNull()) | ||
| .addColumn('array', sql`integer[]`, (col) => col.notNull()) | ||
| .execute() | ||
|
|
||
| db = new Kysely<Database & { values: Values }>({ | ||
| dialect: new PostgresDialect({ | ||
| pool: async () => new Pool(DIALECT_CONFIGS.postgres), | ||
| types: SENSIBLE_TYPES, | ||
| }), | ||
| plugins: PLUGINS, | ||
| }) | ||
| }) | ||
|
|
||
| beforeEach(async () => { | ||
| await insertDefaultDataSet(ctx) | ||
|
|
||
| await db | ||
| .insertInto('values') | ||
| .values({ | ||
| bigint: '9223372036854775807', | ||
| timestamptz: '2025-08-10 14:44:40.687342+02', | ||
| timestamp: '2025-08-10 14:44:40.687342Z', | ||
| date: '2025-08-10', | ||
| time: '14:44:40.687342', | ||
| timetz: '14:44:40.687342+02', | ||
| array: [1, 2, 3], | ||
| }) | ||
| .execute() | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await db.deleteFrom('values').execute() | ||
| await clearDatabase(ctx) | ||
| }) | ||
|
|
||
| after(async () => { | ||
| await ctx.db.schema.dropTable('values').ifExists().execute() | ||
| await destroyTest(ctx) | ||
| }) | ||
|
|
||
| it('regular selects should return the same values as JSON serialized values', async () => { | ||
| const columns: (keyof Values)[] = [ | ||
| 'timestamptz', | ||
| 'timestamp', | ||
| 'date', | ||
| 'timetz', | ||
| 'time', | ||
| 'array', | ||
| ] | ||
| const rawValues = await db | ||
| .selectFrom('values') | ||
| .select(columns) | ||
| .executeTakeFirstOrThrow() | ||
| const { value: jsonValues } = await db | ||
| .selectNoFrom((eb) => | ||
| jsonObjectFrom(eb.selectFrom('values').select(columns)) | ||
| .$notNull() | ||
| .as('value'), | ||
| ) | ||
| .executeTakeFirstOrThrow() | ||
|
|
||
| expect(rawValues).to.eql(jsonValues) | ||
| }) | ||
|
|
||
| it('to prevent data loss some types should not have the same value as their JSON serialized equivalent', async () => { | ||
| const columns: (keyof Values)[] = ['bigint'] | ||
| const rawValues = await db | ||
| .selectFrom('values') | ||
| .select(columns) | ||
| .executeTakeFirstOrThrow() | ||
| const { value: jsonValues } = await db | ||
| .selectNoFrom((eb) => | ||
| jsonObjectFrom(eb.selectFrom('values').select(columns)) | ||
| .$notNull() | ||
| .as('value'), | ||
| ) | ||
| .executeTakeFirstOrThrow() | ||
|
|
||
| expect(rawValues.bigint).to.eql('9223372036854775807') | ||
| expect(jsonValues.bigint).to.eql(9223372036854776000) | ||
| }) | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty common macOS file that gets generated by the OS.