Skip to content

Commit

Permalink
test: fix broken types
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 11, 2024
1 parent 9dbd7fc commit 44da18c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 80 deletions.
5 changes: 3 additions & 2 deletions src/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import knex, { Knex } from 'knex'
import { EventEmitter } from 'node:events'
import { patchKnex } from 'knex-dynamic-connection'
import type { Logger } from '@adonisjs/core/logger'
import { HealthCheckResult } from '@adonisjs/core/types/health'
// @ts-expect-error
import { resolveClientNameWithAliases } from 'knex/lib/util/helpers.js'
import { ConnectionConfig, ConnectionContract, ReportNode } from '../types/database.js'
import { ConnectionConfig, ConnectionContract } from '../types/database.js'

import { Logger as ConnectionLogger } from './logger.js'
import * as errors from '../errors.js'
Expand Down Expand Up @@ -392,7 +393,7 @@ export class Connection extends EventEmitter implements ConnectionContract {
/**
* Returns the healthcheck report for the connection
*/
async getReport(): Promise<ReportNode> {
async getReport(): Promise<HealthCheckResult> {
const error = await this.checkWriteHost()
let readError: Error | undefined

Expand Down
28 changes: 0 additions & 28 deletions src/connection/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { Emitter } from '@adonisjs/core/events'
import type { Logger } from '@adonisjs/core/logger'

import {
ReportNode,
ConnectionNode,
ConnectionConfig,
ConnectionContract,
Expand Down Expand Up @@ -244,31 +243,4 @@ export class ConnectionManager implements ConnectionManagerContract {
this.connections.delete(connectionName)
}
}

/**
* Returns the report for all the connections marked for healthChecks.
*/
async report(): Promise<any & { meta: ReportNode[] }> {
const reports = await Promise.all(
Array.from(this.connections.keys())
.filter((one) => this.get(one)!.config.healthCheck)
.map((one) => {
this.connect(one)
return this.get(one)!.connection!.getReport()
})
)

const healthy = !reports.find((report) => !!report.error)

return {
displayName: 'Database',
health: {
healthy,
message: healthy
? 'All connections are healthy'
: 'One or more connections are not healthy',
},
meta: reports,
}
}
}
25 changes: 0 additions & 25 deletions src/database/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class Database extends Macroable {
* A store of global transactions
*/
connectionGlobalTransactions: Map<string, TransactionClientContract> = new Map()
hasHealthChecksEnabled = false
prettyPrint = prettyPrint

constructor(
Expand All @@ -66,23 +65,6 @@ export class Database extends Macroable {
this.primaryConnectionName = this.config.connection

this.registerConnections()
this.findIfHealthChecksAreEnabled()
}

/**
* Compute whether health check is enabled or not after registering the connections.
* There are chances that all pre-registered connections are not using health
* checks but a dynamic connection is using it. We don't support that use case
* for now, since it complicates things a lot and forces us to register the
* health checker on demand.
*/
private findIfHealthChecksAreEnabled() {
for (let [, conn] of this.manager.connections) {
if (conn.config.healthCheck) {
this.hasHealthChecksEnabled = true
break
}
}
}

/**
Expand Down Expand Up @@ -254,13 +236,6 @@ export class Database extends Macroable {
: client.transaction(callbackOrOptions)
}

/**
* Invokes `manager.report`
*/
report() {
return this.manager.report()
}

/**
* Begin a new global transaction
*/
Expand Down
24 changes: 5 additions & 19 deletions src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import type { Pool } from 'tarn'
import type { EventEmitter } from 'node:events'
import type { ConnectionOptions } from 'node:tls'
import type { Emitter } from '@adonisjs/core/events'
import type { HealthCheckResult } from '@adonisjs/core/types/health'
import { LucidModel, ModelQueryBuilderContract } from './model.js'
import {
DatabaseQueryBuilderContract,
FromTable,
InsertQueryBuilderContract,
RawBuilderContract,
RawQueryBindings,
RawBuilderContract,
RawQueryBuilderContract,
ReferenceBuilderContract,
InsertQueryBuilderContract,
DatabaseQueryBuilderContract,
} from './querybuilder.js'

/**
Expand Down Expand Up @@ -309,15 +310,6 @@ type SharedConnectionNode = {
port?: number
}

/**
* Shape of the report node for the database connection report
*/
export type ReportNode = {
connection: string
message: string
error: any
}

/**
* Migrations config
*/
Expand All @@ -344,7 +336,6 @@ export type SharedConfigNode = {
debug?: boolean
asyncStackTraces?: boolean
revision?: number
healthCheck?: boolean
migrations?: MigratorConfig
seeders?: SeedersConfig
wipe?: { ignoreTables?: string[] }
Expand Down Expand Up @@ -638,11 +629,6 @@ export interface ConnectionManagerContract {
* re-add it using the `add` method
*/
release(connectionName: string): Promise<void>

/**
* Returns the health check report for registered connections
*/
report(): Promise<any & { meta: ReportNode[] }>
}

/**
Expand Down Expand Up @@ -712,7 +698,7 @@ export interface ConnectionContract extends EventEmitter {
/**
* Returns the connection report
*/
getReport(): Promise<ReportNode>
getReport(): Promise<HealthCheckResult>
}

/**
Expand Down
12 changes: 6 additions & 6 deletions test/orm/base_model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6029,7 +6029,7 @@ test.group('Base Model | date', (group) => {
User.$adapter = adapter

adapter.on('insert', (model: LucidRow, _: any) => {
assert.instanceOf((model as User).dob, DateTime)
assert.instanceOf((model as User).dob, DateTime as any)
})

user.username = 'virk'
Expand Down Expand Up @@ -6066,7 +6066,7 @@ test.group('Base Model | date', (group) => {
User.$adapter = adapter

adapter.on('insert', (model: LucidRow, _: any) => {
assert.instanceOf((model as User).dob, DateTime)
assert.instanceOf((model as User).dob, DateTime as any)
assert.isUndefined((model as User).createdAt)
})

Expand Down Expand Up @@ -6097,7 +6097,7 @@ test.group('Base Model | date', (group) => {
const user = new User()
User.$adapter = adapter
adapter.on('update', (model: LucidRow) => {
assert.instanceOf((model as User).updatedAt, DateTime)
assert.instanceOf((model as User).updatedAt, DateTime as any)
})

user.username = 'virk'
Expand Down Expand Up @@ -6320,7 +6320,7 @@ test.group('Base Model | date', (group) => {

await db.insertQuery().table('users').insert({ username: 'virk' })
const user = await User.find(1)
assert.instanceOf(user!.createdAt, DateTime)
assert.instanceOf(user!.createdAt, DateTime as any)
})

test('ignore null or empty values during fetch', async ({ fs, assert }) => {
Expand Down Expand Up @@ -6520,7 +6520,7 @@ test.group('Base Model | datetime', (group) => {
const user = new User()
user.username = 'virk'
await user.save()
assert.instanceOf(user.joinedAt, DateTime)
assert.instanceOf(user.joinedAt, DateTime as any)

const createdUser = await db.from('users').select('*').first()

Expand Down Expand Up @@ -6688,7 +6688,7 @@ test.group('Base Model | datetime', (group) => {

await db.insertQuery().table('users').insert({ username: 'virk' })
const user = await User.find(1)
assert.instanceOf(user!.createdAt, DateTime)
assert.instanceOf(user!.createdAt, DateTime as any)
})

test('ignore null or empty values during fetch', async ({ fs, assert }) => {
Expand Down

0 comments on commit 44da18c

Please sign in to comment.