-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(schema): using command's app container to get access to the …
…db instance
- Loading branch information
Showing
5 changed files
with
48 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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,2 @@ | ||
import { configApp } from '@adonisjs/eslint-config' | ||
export default configApp() |
This file contains 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 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 |
---|---|---|
@@ -1,29 +1,26 @@ | ||
import db from '@adonisjs/lucid/services/db' | ||
import { Database } from '@adonisjs/lucid/database' | ||
import { default as schemaInspectorImport } from 'knex-schema-inspector' | ||
import type { Column } from 'knex-schema-inspector/dist/types/column.js' | ||
import type { SchemaInspector } from 'knex-schema-inspector/dist/types/schema-inspector.js' | ||
const schemaInspector = schemaInspectorImport.default | ||
|
||
export type TableSchema = { | ||
name: string | ||
columns: Column[] | ||
} | ||
|
||
export default class Schema { | ||
declare inspector: SchemaInspector | ||
export async function schema(db: Database) { | ||
const knex = db.connection().getWriteClient() | ||
const inspector = schemaInspector(knex) | ||
const tableNames = await inspector.tables() | ||
const promises = tableNames.map(async (name) => ({ | ||
name, | ||
columns: await inspector.columnInfo(name), | ||
})) | ||
|
||
constructor() { | ||
const knex = db.connection().getWriteClient() | ||
this.inspector = schemaInspector(knex) | ||
} | ||
|
||
async getTables(): Promise<TableSchema[]> { | ||
const tableNames = await this.inspector.tables() | ||
const promises = tableNames.map(async (name) => ({ | ||
name, | ||
columns: await this.inspector.columnInfo(name), | ||
})) | ||
const tables = await Promise.all(promises) | ||
|
||
return Promise.all(promises) | ||
return { | ||
inspector, | ||
tables, | ||
} | ||
} |