Skip to content

Commit d33b48b

Browse files
authored
foreign db refactor (#852)
* foreign db refactor * add changeset * update message
1 parent 4a716da commit d33b48b

7 files changed

Lines changed: 50 additions & 157 deletions

File tree

.changeset/dry-bags-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@flatfile/plugin-foreign-db-extractor': minor
3+
---
4+
5+
This release reflects the foreign db refactor in platform

flatfilers/sandbox/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { contacts } from './sheets/contacts'
44
import '@flatfile/http-logger/init'
55
import api, { type Flatfile } from '@flatfile/api'
66
import { exportWorkbookPlugin } from '@flatfile/plugin-export-workbook'
7+
import { foreignDBExtractor } from '@flatfile/plugin-foreign-db-extractor'
78

89
export default async function (listener: FlatfileListener) {
10+
listener.use(foreignDBExtractor())
911
// Cache the sheets to avoid multiple API calls
1012
listener.on('job:ready', { job: 'workbook:downloadWorkbook' }, async (e) => {
1113
await e.cache.init('sheets-schema', async () => {

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/foreign-db-extractor/src/database.user.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

plugins/foreign-db-extractor/src/index.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import type { FlatfileEvent, FlatfileListener } from '@flatfile/listener'
33
import type sql from 'mssql'
44
import { pollDatabaseStatus } from './database.poll.status'
55
import { restoreDatabase } from './database.restore'
6-
import type { DBUser } from './database.user'
7-
import { pollForUser } from './database.user'
86
import { s3Upload } from './s3.upload'
9-
import { generateSheets } from './sheets.generator'
7+
import { configureWorkbook } from './workbook.configure'
108

119
const api = new FlatfileClient()
1210

@@ -83,29 +81,8 @@ export const foreignDBExtractor = () => {
8381
await pollDatabaseStatus(connectionConfig.database)
8482

8583
// Step 2.5: Retrieve user credentials for the database
86-
await tick(85, 'Retrieving database user credentials')
87-
const user = (await pollForUser(connectionConfig.database)) as DBUser
88-
connectionConfig.user = user.username
89-
connectionConfig.password = user.password
90-
91-
// Step 2.6: Create a Workbook
92-
// Get column names for all tables, loop through them and create Sheets for each table
93-
await tick(90, 'Creating workbook')
94-
const sheets = await generateSheets(connectionConfig)
95-
96-
// Sheets need to be added to the workbook before adding the connectionType to ensure the correct ephemeral
97-
// driver is used
98-
await api.workbooks.update(workbook.id, {
99-
sheets,
100-
})
101-
102-
// Update workbook with connection config
103-
await api.workbooks.update(workbook.id, {
104-
metadata: {
105-
connectionType: 'FOREIGN_MSSQL',
106-
connectionConfig,
107-
},
108-
})
84+
await tick(85, 'Configuring workbook')
85+
await configureWorkbook(workbook.id)
10986

11087
// Step 2.7: Update file with workbookId
11188
await tick(95, 'Updating file')

plugins/foreign-db-extractor/src/sheets.generator.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Flatfile } from '@flatfile/api'
2+
import fetch from 'cross-fetch'
3+
4+
/**
5+
* The configureWorkbook function is responsible for initiating the workbook configuration. It sends a GET request to
6+
* the foreigndb API to configure the workbook. This must be called after the database restore is complete.
7+
*
8+
* @param workbookId
9+
* @returns
10+
*/
11+
export async function configureWorkbook(
12+
workbookId: string
13+
): Promise<Flatfile.SuccessData | Error> {
14+
const response = await fetch(
15+
`${process.env.AGENT_INTERNAL_URL}/v1/foreigndb/${workbookId}/configure`,
16+
{
17+
method: 'POST',
18+
headers: {
19+
'Content-Type': 'application/json',
20+
Authorization: `Bearer ${process.env.FLATFILE_BEARER_TOKEN}`,
21+
},
22+
}
23+
)
24+
25+
const jsonResponse = await response.json()
26+
27+
if (response.status !== 200) {
28+
throw new Error(
29+
`Status: ${response.status} Message: ${jsonResponse.message}`
30+
)
31+
}
32+
33+
return jsonResponse
34+
}

0 commit comments

Comments
 (0)