Skip to content

Commit

Permalink
Connection interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayden committed Aug 14, 2024
1 parent b850831 commit a02508f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
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[]
}

0 comments on commit a02508f

Please sign in to comment.