Skip to content

Commit

Permalink
feat: convert sql ddl -> blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrugger committed Nov 3, 2023
1 parent 0c4165a commit fe84fe1
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 28 deletions.
104 changes: 99 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions plugins/sql-ddl-converter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@flatfile/plugin-convert-sql-ddl",
"version": "0.0.2",
"description": "A plugin for converting SQL DDL into Flatfile Blueprint.",
"registryMetadata": {
"category": "schema-converters"
},
"engines": {
"node": ">= 16"
},
"source": "src/index.ts",
"main": "dist/main.js",
"module": "dist/module.mjs",
"types": "dist/types.d.ts",
"scripts": {
"build": "parcel build",
"dev": "parcel watch",
"check": "tsc ./**/*.ts --noEmit --esModuleInterop",
"test": "jest ./**/*.spec.ts --config=../../jest.config.js --runInBand"
},
"keywords": [],
"author": "Flatfile, Inc.",
"repository": {
"type": "git",
"url": "https://github.com/FlatFilers/flatfile-plugins.git",
"directory": "plugins/sql-ddl-converter"
},
"license": "ISC",
"dependencies": {
"@flatfile/api": "^1.5.30",
"@flatfile/plugin-convert-json-schema": "^0.0.2",
"@flatfile/plugin-space-configure": "^0.1.5",
"axios": "^1.5.1",
"sql-ddl-to-json-schema": "^4.1.0"
}
}
31 changes: 31 additions & 0 deletions plugins/sql-ddl-converter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Flatfile } from '@flatfile/api'
import { FlatfileEvent, FlatfileListener } from '@flatfile/listener'
import { configureSpace } from '@flatfile/plugin-space-configure'
import {
ModelsToSheetConfig,
PartialWorkbookConfig,
generateSetup,
} from './setup.factory'

export function configureSpaceWithSqlDDL(
sqlDdlPath: string,
options?: {
models?: ModelsToSheetConfig
workbookConfig?: PartialWorkbookConfig
debug?: boolean
},
callback?: (
event: FlatfileEvent,
workbookIds: string[],
tick: (progress?: number, message?: string) => Promise<Flatfile.JobResponse>
) => any | Promise<any>
) {
return async function (listener: FlatfileListener) {
listener.use(
configureSpace(await generateSetup(sqlDdlPath, options), callback)
)
}
}

export type { SetupFactory } from '@flatfile/plugin-space-configure'
export * from './setup.factory'
78 changes: 78 additions & 0 deletions plugins/sql-ddl-converter/src/setup.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Flatfile } from '@flatfile/api'
import { generateFields } from '@flatfile/plugin-convert-json-schema'
import { SetupFactory } from '@flatfile/plugin-space-configure'
import { Parser } from 'sql-ddl-to-json-schema'

import { FlatfileEvent } from '@flatfile/listener'
import * as fs from 'fs'
import * as path from 'path'

export type PartialWorkbookConfig = Omit<
Flatfile.CreateWorkbookConfig,
'sheets' | 'name'
> & {
name?: string
}
export type PartialSheetConfig = Omit<
Flatfile.SheetConfig,
'fields' | 'name'
> & {
name?: string
}

export type ModelsToSheetConfig = { [key: string]: PartialSheetConfig }

export async function generateSetup(
sqlDdlPath: string,
options?: {
models?: ModelsToSheetConfig
workbookConfig?: PartialWorkbookConfig
debug?: boolean
}
): Promise<SetupFactory> {
const sql: string = fs
.readFileSync(path.join(__dirname, '../', sqlDdlPath))
.toString()

const parser = new Parser('mysql')
const compactJsonTablesArray = parser.feed(sql).toCompactJson(parser.results)
// console.dir(compactJsonTablesArray, { depth: null })
const schemas = parser
.feed(sql)
.toJsonSchemaArray({ useRef: true }, compactJsonTablesArray)
// console.dir(schemas, { depth: null })
const sheetConfigs: Flatfile.SheetConfig[] = await Promise.all(
Object.entries(schemas)
.filter(
([key]) => !options?.models || options?.models.hasOwnProperty(key)
)
.map(async ([key, schema]) => {
const fields: Flatfile.Property[] = await generateFields(schema)

const requiredFields = new Set(schema.required || [])
fields.forEach((field) => {
if (requiredFields.has(field.key)) {
field.constraints?.push({ type: 'required' })
}
})

const modelDetails = options?.models?.[schema.title]
return {
name: modelDetails?.name || schema.title,
slug: modelDetails?.slug || schema.title,
fields,
...modelDetails,
}
})
)
// console.dir(sheetConfigs, { depth: null })
return {
workbooks: [
{
name: 'Hello world',
sheets: sheetConfigs,
...options?.workbookConfig,
},
],
}
}
17 changes: 0 additions & 17 deletions plugins/sql-ddl/package.json

This file was deleted.

31 changes: 31 additions & 0 deletions plugins/sql-ddl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Flatfile } from '@flatfile/api'
import { FlatfileEvent, FlatfileListener } from '@flatfile/listener'
import { configureSpace } from '@flatfile/plugin-space-configure'
import {
ModelsToSheetConfig,
PartialWorkbookConfig,
generateSetup,
} from './setup.factory'

export function configureSpaceWithSqlDDL(
sqlDdlPath: string,
options?: {
models?: ModelsToSheetConfig
workbookConfig?: PartialWorkbookConfig
debug?: boolean
},
callback?: (
event: FlatfileEvent,
workbookIds: string[],
tick: (progress?: number, message?: string) => Promise<Flatfile.JobResponse>
) => any | Promise<any>
) {
return async function (listener: FlatfileListener) {
listener.use(
configureSpace(await generateSetup(sqlDdlPath, options), callback)
)
}
}

export type { SetupFactory } from '@flatfile/plugin-space-configure'
export * from './setup.factory'
Loading

0 comments on commit fe84fe1

Please sign in to comment.