Leonardo Silveira
Leonardo Silveira
DTDrizzle Team
Created by Leonardo Silveira on 8/21/2024 in #help
how to close underlying connection pool being used by drizzle?
hello all, in my test cases i have those before and after hooks:
test.before(async t => {
t.timeout(30000, 'waiting for the database to be ready')
const initScript = resolve(__dirname, '..', '..', 'infrastructure', 'dev-schema.sql');
// @ts-ignore
t.context.pg = await new PostgreSqlContainer()
.withBindMounts([{
source: initScript,
target: '/docker-entrypoint-initdb.d/init.sql',
}])
.start();
// @ts-ignore
t.context.db = await prepareDb(t.context.pg.getConnectionUri())
// @ts-ignore
t.context.server = await prepareServer(t.context.db)
})

test.after.always(async t => {
try {
// @ts-ignore
await t.context.pg.stop({timeout: 500})
} catch (e) {
// swallow for now
}
})
test.before(async t => {
t.timeout(30000, 'waiting for the database to be ready')
const initScript = resolve(__dirname, '..', '..', 'infrastructure', 'dev-schema.sql');
// @ts-ignore
t.context.pg = await new PostgreSqlContainer()
.withBindMounts([{
source: initScript,
target: '/docker-entrypoint-initdb.d/init.sql',
}])
.start();
// @ts-ignore
t.context.db = await prepareDb(t.context.pg.getConnectionUri())
// @ts-ignore
t.context.server = await prepareServer(t.context.db)
})

test.after.always(async t => {
try {
// @ts-ignore
await t.context.pg.stop({timeout: 500})
} catch (e) {
// swallow for now
}
})
however the try/catch seems to be ineffective. it always ends up like this:
Uncaught exception in app/Server.spec.ts

error: terminating connection due to administrator command

error: terminating connection due to administrator command
at Parser.parseErrorMessage (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/index.ts:7:48)
(...)

✘ app/Server.spec.ts exited with a non-zero exit code: 1


3 tests passed
1 uncaught exception

Process finished with exit code 1
Uncaught exception in app/Server.spec.ts

error: terminating connection due to administrator command

error: terminating connection due to administrator command
at Parser.parseErrorMessage (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/home/sombriks/git/game-shark/api/node_modules/pg-protocol/src/index.ts:7:48)
(...)

✘ app/Server.spec.ts exited with a non-zero exit code: 1


3 tests passed
1 uncaught exception

Process finished with exit code 1
comparing the code with a similar one made with knex, knex offers a destroy method. is there anything similar in drizzle so i can close connections before shut down the test container? any tip is welcome, thanks in andvance.
3 replies
DTDrizzle Team
Created by Leonardo Silveira on 8/8/2024 in #help
How to avoid duplicate definition
Hello all, currently i have this on several of my models:
const authorizationSchema = pgSchema('authorization')

export const Groups = authorizationSchema.table('my_groups', {
id: serial('id').primaryKey(),
description: varchar('description', {length: 255}).notNull(),
created: timestamp('created').notNull().defaultNow(),
updated: timestamp('updated').notNull().defaultNow(),
companies_id: integer('companies_id').notNull().references(() => Companies.id),
parent_my_groups_id: integer('parent_my_groups_id').references((): AnyPgColumn => Groups.id)
})

export type Group = {
id?: number
description: string
created?: Date | string
updated?: Date | string
companies_id: number
parent_my_groups_id?: number | null
}
const authorizationSchema = pgSchema('authorization')

export const Groups = authorizationSchema.table('my_groups', {
id: serial('id').primaryKey(),
description: varchar('description', {length: 255}).notNull(),
created: timestamp('created').notNull().defaultNow(),
updated: timestamp('updated').notNull().defaultNow(),
companies_id: integer('companies_id').notNull().references(() => Companies.id),
parent_my_groups_id: integer('parent_my_groups_id').references((): AnyPgColumn => Groups.id)
})

export type Group = {
id?: number
description: string
created?: Date | string
updated?: Date | string
companies_id: number
parent_my_groups_id?: number | null
}
if feel i am doing something wrong. is there a way to derive the type from the table so i don't have to double it? any guidance is welcome.
3 replies