Skip to content
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

Connection interface #44

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/connections/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
import { QueryType } from '../query-params'
import { Query } from '../query'
import { Schema, TableColumn, TableCondition, TableIndex, TableRecord } from 'src/models/database'

export type OperationResponse = {
success: boolean
data: any
error: Error | null
}

export interface Connection {
queryType: QueryType

// Handles logic for securely connecting and properly disposing of the connection.
connect: () => Promise<any>
disconnect: () => Promise<any>

// Raw query execution method that can be used to execute any query.
query: (
query: Query
) => Promise<{ data: any; error: Error | null; query: string }>

// Basic CRUD methods that allow for easy interaction with the connection.
insert?: (record: TableRecord, table: string, schema?: string) => Promise<OperationResponse>
read?: (conditions: TableCondition[], table: string, schema?: string) => Promise<OperationResponse>
update?: (record: TableRecord, conditions: TableCondition[], table: string, schema?: string) => Promise<OperationResponse>
delete?: (conditions: TableCondition[], table: string, schema?: string) => Promise<OperationResponse>

// Table operations
createTable?: (name: string, schema?: string) => Promise<OperationResponse>
dropTable?: (name: string, schema?: string) => Promise<OperationResponse>
renameTable?: (name: string, original: string, schema?: string) => Promise<OperationResponse>

// Column operations
addColumn?: (name: string, table: string, schema?: string) => Promise<OperationResponse>
dropColumn?: (name: string, table: string, schema?: string) => Promise<OperationResponse>
renameColumn?: (name: string, original: string, table: string, schema?: string) => Promise<OperationResponse>
updateColumn?: (name: string, column: TableColumn, table: string, schema?: string) => Promise<OperationResponse>

// Index operations
createIndex?: (index: TableIndex, table: string, schema?: string) => Promise<OperationResponse>
dropIndex?: (name: string, table: string, schema?: string) => Promise<OperationResponse>
renameIndex?: (name: string, original: string, table: string, schema?: string) => Promise<OperationResponse>

// Schema operations
createSchema?: (name: string) => Promise<OperationResponse>
dropSchema?: (name: string) => Promise<OperationResponse>

// Additional operations
fetchDatabaseSchema?: () => Promise<Schema[]>
}
39 changes: 39 additions & 0 deletions src/models/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export type Schema = {
name: string
tables: Table[]
}

export type Table = {
name: string
schema: string
columns: TableColumn[]
indexes: {
name: string
type: 'unique' | 'primary' | 'index'
columns: string[]
}[]
}

export type TableColumn = {
name: string
type: string
nullable: boolean
default: any
primary: boolean
unique: boolean
expression: string
references: TableIndex[]
}

export type TableRecord = Record<string, any>
export type TableCondition = {
column: string
value: any
condition: 'eq' | 'neq' | 'gt' | 'lt' | 'gte' | 'lte' | 'like' | 'in' | 'nin'
}

export type TableIndex = {
name: string
type: 'unique' | 'primary' | 'index'
columns: string[]
}
Loading