-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c4165a
commit fe84fe1
Showing
10 changed files
with
368 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Oops, something went wrong.