Skip to content

Commit

Permalink
SDK supports
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-mabry committed Sep 10, 2024
1 parent 580d212 commit 37e8385
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 168 deletions.
78 changes: 70 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export enum QueryBuilderAction {
RENAME_TABLE = 'renameTable',
ALTER_TABLE = 'alterTable',
ADD_COLUMNS = 'addColumns',
DROP_COLUMNS = 'dropColumns',
RENAME_COLUMNS = 'renameColumns',
UPDATE_COLUMNS = 'updateColumns',
}

export interface QueryBuilder {
Expand All @@ -30,7 +33,14 @@ export interface QueryBuilder {
// Used specifically for SELECT statements, useful when joining multiple tables
columnsWithTable?: { schema?: string; table: string; columns: string[] }[]
// Used when column names and type are required, such as CREATE TABLE
columns?: { name: string; type: ColumnDataType }[]
columns?: {
default?: string
nullable?: boolean
name?: string
type?: ColumnDataType | string
oldName?: string
newName?: string
}[]
whereClauses?: string[]
joins?: string[]
data?: { [key: string]: any }
Expand Down Expand Up @@ -90,14 +100,25 @@ export interface OuterbaseType {
// Table operations
createTable?: (table: string) => OuterbaseType
renameTable?: (old: string, name: string) => OuterbaseType
addColumns: (
columns: { name: string; type: ColumnDataType }[]
addColumns: (columns: { name: string; type: string }[]) => OuterbaseType
dropColumns: (columns: string[]) => OuterbaseType
updateColumn: (
columns: {
name: string
type: ColumnDataType
nullable?: boolean
default?: string
}[]
) => OuterbaseType
dropTable?: (table: string) => OuterbaseType
alterTable?: (table: string) => OuterbaseType
alterTable: (table: string) => OuterbaseType
columns?: (
columns: { name: string; type: ColumnDataType }[]
) => OuterbaseType

renameColumns: (
columns: { newName: string; oldName: string }[]
) => OuterbaseType
// WORK IN PROGRESS
// WORK IN PROGRESS
// WORK IN PROGRESS
Expand Down Expand Up @@ -237,7 +258,34 @@ export function Outerbase(connection: Connection): OuterbaseType {
}
return this
},

renameColumns(columns) {
this.queryBuilder = {
...this.queryBuilder,
columns,
action: QueryBuilderAction.RENAME_COLUMNS,
}
return this
},
dropColumns(columns) {
this.queryBuilder = {
...this.queryBuilder,
columns: columns.map((column) => ({
name: column,
// We don't really care what the type is
type: ColumnDataType.STRING,
})),
action: QueryBuilderAction.DROP_COLUMNS,
}
return this
},
updateColumn(columns) {
this.queryBuilder = {
...this.queryBuilder,
columns,
action: QueryBuilderAction.UPDATE_COLUMNS,
}
return this
},
insert(data) {
this.queryBuilder = {
action: QueryBuilderAction.INSERT,
Expand Down Expand Up @@ -446,16 +494,30 @@ function buildQueryString(
query
).query
break

case QueryBuilderAction.ADD_COLUMNS:
query.query = dialect.addColumn(
queryBuilder,
queryType,
query
).query
break

case QueryBuilderAction.ALTER_TABLE:
query.query = dialect.addColumn(
case QueryBuilderAction.DROP_COLUMNS:
query.query = dialect.dropColumn(
queryBuilder,
queryType,
query
).query
break
case QueryBuilderAction.RENAME_COLUMNS:
query.query = dialect.renameColumn(
queryBuilder,
queryType,
query
).query
break
case QueryBuilderAction.UPDATE_COLUMNS:
query.query = dialect.updateColumn(
queryBuilder,
queryType,
query
Expand Down
31 changes: 11 additions & 20 deletions src/connections/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,31 @@ export class BigQueryConnection implements Connection {
}

public async fetchDatabaseSchema(): Promise<Database> {
let database: Database = []
const database: Database = {}

// Fetch all datasets
const [datasets] = await this.bigQuery.getDatasets()
if (datasets.length === 0) {
throw new Error('No datasets found in the project.')
}

// Iterate over each dataset
for (const dataset of datasets) {
const datasetId = dataset.id
if (datasetId === undefined) continue
// Fetch all tables in the dataset
if (!datasetId) continue

const [tables] = await dataset.getTables()

const schemaMap: { [key: string]: Table[] } = {}
if (!database[datasetId]) {
database[datasetId] = {} // Initialize schema in the database
}

for (const table of tables) {
// Fetch table schema (columns)
const [metadata] = await table.getMetadata()

const columns = metadata.schema.fields.map(
(field: any, index: number) => {
const currentColumn: TableColumn = {
(field: any, index: number): TableColumn => {
return {
name: field.name,
type: field.type,
position: index,
Expand All @@ -138,30 +141,18 @@ export class BigQueryConnection implements Connection {
unique: false, // BigQuery does not have a concept of unique constraints
references: [], // BigQuery does not support foreign keys
}
return currentColumn
}
)

const currentTable: Table = {
name: table.id ?? '',
schema: datasetId, // Assign dataset (schema) name to the table
columns: columns,
indexes: [], // BigQuery does not support indexes
constraints: [], // BigQuery does not support primary keys, foreign keys, or unique constraints
}

if (!schemaMap[datasetId]) {
schemaMap[datasetId] = []
}

schemaMap[datasetId].push(currentTable)
database[datasetId][table.id ?? ''] = currentTable
}

database = Object.entries(schemaMap).map(([schemaName, tables]) => {
return {
[schemaName]: tables,
}
})
}

return database
Expand Down
33 changes: 13 additions & 20 deletions src/connections/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,13 @@ export class CloudflareD1Connection implements Connection {

public async fetchDatabaseSchema(): Promise<Database> {
const exclude_tables = ['_cf_kv', 'sqlite_schema', 'sqlite_temp_schema']
const constraints: Constraint[] = []

let database: Database = []
const schemaMap: { [key: string]: Table[] } = {}
const schemaMap: Record<string, Record<string, Table>> = {}

const { data } = await this.query({
query: `PRAGMA table_list`,
})

const allTables = (
data as {
schema: string
Expand All @@ -172,7 +171,9 @@ export class CloudflareD1Connection implements Connection {
}[]
).filter(
(row) =>
!row.name.startsWith('_lite') && !row.name.startsWith('sqlite_')
!row.name.startsWith('_lite') &&
!row.name.startsWith('sqlite_') &&
!exclude_tables.includes(row.name?.toLowerCase())
)

for (const table of allTables) {
Expand Down Expand Up @@ -212,6 +213,8 @@ export class CloudflareD1Connection implements Connection {
!row.table.startsWith('sqlite_')
)

const constraints: Constraint[] = []

if (fkConstraintData.length > 0) {
const fkConstraints: Constraint = {
name: 'FOREIGN KEY',
Expand All @@ -224,10 +227,6 @@ export class CloudflareD1Connection implements Connection {
fkConstraintData.forEach((fkConstraint) => {
const currentConstraint: ConstraintColumn = {
columnName: fkConstraint.from,
constraintName: fkConstraint.to,
constraintSchema: table.schema,
tableName: fkConstraint.table,
tableSchema: table.schema,
}
fkConstraints.columns.push(currentConstraint)
})
Expand All @@ -236,7 +235,7 @@ export class CloudflareD1Connection implements Connection {

const indexes: TableIndex[] = []
const columns = tableData.map((column) => {
// Primary keys are ALWAYS considered Indexes
// Primary keys are ALWAYS considered indexes
if (column.pk === 1) {
indexes.push({
name: column.name,
Expand All @@ -258,27 +257,21 @@ export class CloudflareD1Connection implements Connection {

return currentColumn
})
const current: Table = {

const currentTable: Table = {
name: table.name,
columns: columns,
indexes: indexes,
constraints: constraints,
}

if (!schemaMap[table.schema]) {
schemaMap[table.schema] = []
schemaMap[table.schema] = {}
}

schemaMap[table.schema].push(current)
schemaMap[table.schema][table.name] = currentTable
}

// Transform schemaMap into the final Database structure
database = Object.entries(schemaMap).map(([schemaName, tables]) => {
return {
[schemaName]: tables,
}
})

return database
return schemaMap
}
}
4 changes: 2 additions & 2 deletions src/connections/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryType } from '../query-params'
import { Query } from '../query'
import { Schema } from '../models/database'
import { Database } from '../models/database'
import { AbstractDialect } from 'src/query-builder'

export type OperationResponse = {
Expand All @@ -23,5 +23,5 @@ export interface Connection {
) => Promise<{ data: any; error: Error | null; query: string }>

// Retrieve metadata about the database, useful for introspection.
fetchDatabaseSchema?: () => Promise<Schema[]>
fetchDatabaseSchema?: () => Promise<Database>
}
Loading

0 comments on commit 37e8385

Please sign in to comment.