feat(esmole): store, sources, registry, manager, backend binders
This commit is contained in:
1 parent
42712d508c
commit
889e844768
11 files changed
+330
No files matched your search
@@ -0,0 +1,10 @@
|
||||
export const parseArgs = (
|
||||
argv: string[],
|
||||
env: NodeJS.ProcessEnv = process.env
|
||||
): { configPath?: string } => {
|
||||
const flagIndex = argv.indexOf('--config')
|
||||
if (flagIndex !== -1 && argv[flagIndex + 1]) {
|
||||
return { configPath: argv[flagIndex + 1] }
|
||||
}
|
||||
return { configPath: env.ESMOLE_CONFIG }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { BackendTarget } from '@dbmole/core'
|
||||
import { createEsBackend, type EsBackend } from './es/backend.js'
|
||||
import { type ConnectionConfig, defaultPort } from './schema.js'
|
||||
|
||||
export const resolvePort = (config: ConnectionConfig): number => config.port ?? defaultPort()
|
||||
|
||||
export const createBackend = (target: BackendTarget<ConnectionConfig>): EsBackend =>
|
||||
createEsBackend(target)
|
||||
|
||||
// test_connection ping: GET / returns version.number on real ES.
|
||||
export const ping = async (managed: { backend: EsBackend }): Promise<Record<string, unknown>> => {
|
||||
const res = await managed.backend.request('GET', '/')
|
||||
const version = (res.body as { version?: { number?: string } })?.version?.number
|
||||
return { status: res.status, version: version ?? null }
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import {
|
||||
createManager as coreCreate,
|
||||
type ManagedConnection,
|
||||
type Manager,
|
||||
openTunnel
|
||||
} from '@dbmole/core'
|
||||
import type { EsBackend } from './es/backend.js'
|
||||
import { createBackend, resolvePort } from './esBackend.js'
|
||||
import type { EsRegistry } from './registry.js'
|
||||
import type { ConnectionConfig } from './schema.js'
|
||||
|
||||
export type EsManager = Manager<ConnectionConfig, EsBackend>
|
||||
export type EsManaged = ManagedConnection<ConnectionConfig, EsBackend>
|
||||
|
||||
export const createManager = (registry: EsRegistry): EsManager =>
|
||||
coreCreate<ConnectionConfig, EsBackend>(registry, {
|
||||
createBackend,
|
||||
resolvePort,
|
||||
createTunnel: (ssh, host, port) => openTunnel(ssh, host, port, 'esmole')
|
||||
})
|
||||
@@ -0,0 +1,18 @@
|
||||
import { createRegistry as coreCreate, type Registry } from '@dbmole/core'
|
||||
import { type ConnectionConfig, connectionConfigSchema } from './schema.js'
|
||||
import { readConfigFile, readEnvConnections } from './sources.js'
|
||||
import { readStore, writeStore } from './store.js'
|
||||
|
||||
export type EsRegistry = Registry<ConnectionConfig>
|
||||
export type RegistryOptions = { storePath: string; configPath?: string; env?: NodeJS.ProcessEnv }
|
||||
|
||||
export const createRegistry = (options: RegistryOptions): EsRegistry => {
|
||||
const env = options.env ?? process.env
|
||||
return coreCreate<ConnectionConfig>({
|
||||
schema: connectionConfigSchema,
|
||||
readStore: () => readStore(options.storePath),
|
||||
writeStore: (configs) => writeStore(options.storePath, configs),
|
||||
readConfigFile: () => readConfigFile(options.configPath),
|
||||
readEnvConnections: () => readEnvConnections(env)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import {
|
||||
readConfigFile as coreReadConfig,
|
||||
readEnvConnections as coreReadEnv,
|
||||
type StoreOptions
|
||||
} from '@dbmole/core'
|
||||
import { type ConnectionConfig, connectionConfigSchema } from './schema.js'
|
||||
|
||||
const OPTS: StoreOptions = { logLabel: 'esmole' }
|
||||
|
||||
export const readEnvConnections = (env: NodeJS.ProcessEnv = process.env): ConnectionConfig[] =>
|
||||
coreReadEnv(connectionConfigSchema, env, 'ESMOLE_CONNECTIONS', OPTS)
|
||||
|
||||
export const readConfigFile = (path: string | undefined): ConnectionConfig[] =>
|
||||
coreReadConfig(connectionConfigSchema, path, OPTS)
|
||||
@@ -0,0 +1,14 @@
|
||||
import { homedir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { readStore as coreReadStore, type StoreOptions, writeStore } from '@dbmole/core'
|
||||
import { type ConnectionConfig, connectionConfigSchema } from './schema.js'
|
||||
|
||||
const OPTS: StoreOptions = { logLabel: 'esmole' }
|
||||
|
||||
export const defaultStorePath = (env: NodeJS.ProcessEnv = process.env): string =>
|
||||
env.ESMOLE_STORE ?? join(homedir(), '.config', 'esmole', 'connections.json')
|
||||
|
||||
export const readStore = (path: string): ConnectionConfig[] =>
|
||||
coreReadStore(connectionConfigSchema, path, OPTS)
|
||||
|
||||
export { writeStore }
|
||||
Reference in new issue
Block a user