Skip to content

Commit 0b95e4e

Browse files
committed
feat: add schema to auth data
1 parent 33439c5 commit 0b95e4e

File tree

10 files changed

+53
-74
lines changed

10 files changed

+53
-74
lines changed

packages/backend/src/apps/databricks/actions/create-row.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { IGlobalVariable, IRawAction } from '@plumber/types'
22

33
import { z } from 'zod'
44

5-
import { databricksConfig } from '@/config/app-env-vars/databricks'
65
import StepError from '@/errors/step'
76
import logger from '@/helpers/logger'
87

9-
import { createClient } from '../auth/create-client'
10-
import { getSchemaName } from '../common/get-schema-name'
8+
import { createSession } from '../auth/create-client'
119

1210
const insertRowSchema = z.object({
1311
tableName: z.string().min(1),
@@ -108,12 +106,8 @@ const createRowAction: IRawAction = {
108106
$.app.name,
109107
)
110108
}
111-
const client = await createClient($)
109+
const { session, endSession } = await createSession($)
112110
const { tableName, rowData } = parametersParseResult.data
113-
const session = await client.openSession({
114-
initialSchema: getSchemaName($),
115-
initialCatalog: databricksConfig.catalog,
116-
})
117111
const columnNames = rowData.map((row) => row.columnName)
118112
const columnValues = rowData.map((row) => row.columnValue)
119113

@@ -125,8 +119,7 @@ const createRowAction: IRawAction = {
125119
.join(',')})`
126120
const operation = await session.executeStatement(statement)
127121
await operation.fetchAll()
128-
await session.close()
129-
await client.close()
122+
await endSession()
130123
$.setActionItem({
131124
raw: {
132125
success: true,

packages/backend/src/apps/databricks/auth/auth-data.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import { screenNameSchema } from '@/helpers/app-auth-schema'
66

77
const databricksAuthDataSchema = z.object({
88
screenName: screenNameSchema,
9+
schema: z
10+
.string()
11+
.min(1, 'Empty Schema Name')
12+
.regex(/^[a-z0-9_]+$/, {
13+
message:
14+
'Schema name can only contain lowercase letters, numbers and underscores',
15+
})
16+
.max(100),
917
token: z.string().min(1, 'Empty Personal Access Token').max(100),
1018
})
1119

packages/backend/src/apps/databricks/auth/create-client.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DBSQLClient, LogLevel } from '@databricks/sql'
55
import { databricksConfig } from '@/config/app-env-vars/databricks'
66
import logger from '@/helpers/logger'
77

8-
export const createClient = async ($: IGlobalVariable) => {
8+
export const createSession = async ($: IGlobalVariable) => {
99
const token = $.auth.data.token
1010
if (typeof token !== 'string') {
1111
throw new Error('Databricks personal access token is required')
@@ -14,16 +14,14 @@ export const createClient = async ($: IGlobalVariable) => {
1414
const client: DBSQLClient = new DBSQLClient({
1515
logger: {
1616
log(level: LogLevel, message: string) {
17-
if (level <= LogLevel.info) {
18-
logger[level]({
19-
userId: $.user?.id,
20-
stepId: $.step?.id,
21-
flowId: $.flow?.id,
22-
testRun: $.execution?.testRun,
23-
event: 'databricks-client-log',
24-
message,
25-
})
26-
}
17+
logger[level]({
18+
userId: $.user?.id,
19+
stepId: $.step?.id,
20+
flowId: $.flow?.id,
21+
testRun: $.execution?.testRun,
22+
event: 'databricks-client-log',
23+
message,
24+
})
2725
},
2826
},
2927
})
@@ -37,7 +35,16 @@ export const createClient = async ($: IGlobalVariable) => {
3735

3836
try {
3937
const connectedClient = await client.connect(connectOptions)
40-
return connectedClient
38+
const session = await connectedClient.openSession({
39+
initialSchema: $.auth.data.schema as string,
40+
initialCatalog: databricksConfig.catalog,
41+
})
42+
const endSession = async () => {
43+
await session.close()
44+
await connectedClient.close()
45+
}
46+
47+
return { session, endSession }
4148
} catch (error) {
4249
throw new Error('Failed to connect to Databricks')
4350
}

packages/backend/src/apps/databricks/auth/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ const auth: IUserAddedConnectionAuth = {
1414
required: true,
1515
readOnly: false,
1616
},
17+
{
18+
key: 'schema',
19+
label: 'Schema',
20+
type: 'string' as const,
21+
required: true,
22+
readOnly: false,
23+
},
1724
{
1825
key: 'token',
1926
label: 'Personal Access Token',

packages/backend/src/apps/databricks/auth/verify-credentials.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ import { IGlobalVariable } from '@plumber/types'
33
import { ZodError } from 'zod'
44
import { fromZodError } from 'zod-validation-error'
55

6-
import { databricksConfig } from '@/config/app-env-vars/databricks'
76
import logger from '@/helpers/logger'
87

98
import { validateAuthData } from './auth-data'
10-
import { createClient } from './create-client'
9+
import { createSession } from './create-client'
1110

1211
const verifyCredentials = async ($: IGlobalVariable) => {
1312
try {
1413
validateAuthData($)
15-
const client = await createClient($)
16-
const session = await client.openSession({
17-
initialCatalog: databricksConfig.catalog,
18-
})
19-
await session.close()
20-
await client.close()
14+
const { endSession } = await createSession($)
15+
await endSession()
2116
} catch (error) {
2217
if (error instanceof ZodError) {
2318
// Auth data validation failed: throws message from first error

packages/backend/src/apps/databricks/common/get-schema-name.ts

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

packages/backend/src/apps/databricks/dynamic-data/create-column.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { IDynamicAction, IGlobalVariable, IJSONObject } from '@plumber/types'
22

33
import { z } from 'zod'
44

5-
import { databricksConfig } from '@/config/app-env-vars/databricks'
65
import { BadUserInputError } from '@/errors/graphql-errors'
76
import logger from '@/helpers/logger'
87

9-
import { createClient } from '../auth/create-client'
10-
import { getSchemaName } from '../common/get-schema-name'
8+
import { createSession } from '../auth/create-client'
119

1210
const createTableSchema = z.object({
1311
// table name must be lowercase and can only contain underscores
@@ -44,17 +42,12 @@ const dynamicData: IDynamicAction = {
4442

4543
const { tableName, columnName } = parametersParseResult.data
4644

47-
const client = await createClient($)
48-
const session = await client.openSession({
49-
initialSchema: getSchemaName($),
50-
initialCatalog: databricksConfig.catalog,
51-
})
45+
const { session, endSession } = await createSession($)
5246
// TODO: properly prepare this statement
5347
const statement = `ALTER TABLE \`${tableName}\` ADD COLUMN \`${columnName}\` STRING;`
5448
const operation = await session.executeStatement(statement)
5549
await operation.fetchAll()
56-
await session.close()
57-
await client.close()
50+
await endSession()
5851
return {
5952
newValue: columnName,
6053
}

packages/backend/src/apps/databricks/dynamic-data/create-table.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { IDynamicAction, IGlobalVariable, IJSONObject } from '@plumber/types'
22

33
import { z } from 'zod'
44

5-
import { databricksConfig } from '@/config/app-env-vars/databricks'
65
import { BadUserInputError } from '@/errors/graphql-errors'
76
import logger from '@/helpers/logger'
87

9-
import { createClient } from '../auth/create-client'
10-
import { getSchemaName } from '../common/get-schema-name'
8+
import { createSession } from '../auth/create-client'
119

1210
const createTableSchema = z.object({
1311
// table name must be lowercase and can only contain underscores
@@ -32,17 +30,12 @@ const dynamicData: IDynamicAction = {
3230
try {
3331
const { tableName } = parametersParseResult.data
3432

35-
const client = await createClient($)
36-
const session = await client.openSession({
37-
initialSchema: getSchemaName($),
38-
initialCatalog: databricksConfig.catalog,
39-
})
33+
const { session, endSession } = await createSession($)
4034
// TODO: properly prepare this statement
4135
const statement = `CREATE TABLE \`${tableName}\`;`
4236
const operation = await session.executeStatement(statement)
4337
await operation.fetchAll()
44-
await session.close()
45-
await client.close()
38+
await endSession()
4639
return {
4740
newValue: tableName,
4841
}

packages/backend/src/apps/databricks/dynamic-data/list-table-columns.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import {
44
IGlobalVariable,
55
} from '@plumber/types'
66

7-
import { databricksConfig } from '@/config/app-env-vars/databricks'
87
import logger from '@/helpers/logger'
98

10-
import { createClient } from '../auth/create-client'
11-
import { getSchemaName } from '../common/get-schema-name'
9+
import { createSession } from '../auth/create-client'
1210
import { DatabrickColumnRes } from '../common/types'
1311

1412
const dynamicData: IDynamicData = {
@@ -26,19 +24,14 @@ const dynamicData: IDynamicData = {
2624
},
2725
}
2826
}
29-
const client = await createClient($)
30-
const session = await client.openSession({
31-
initialSchema: getSchemaName($),
32-
initialCatalog: databricksConfig.catalog,
33-
})
27+
const { session, endSession } = await createSession($)
3428
const operation = await session.getColumns({
3529
tableName: $.step.parameters.tableName as string,
3630
})
3731
const columns = (await operation.fetchAll({
3832
maxRows: 1000,
3933
})) as DatabrickColumnRes[]
40-
await session.close()
41-
await client.close()
34+
await endSession()
4235
return {
4336
data: columns.map((column) => ({
4437
name: column.COLUMN_NAME,

packages/backend/src/apps/databricks/dynamic-data/list-table-names.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
import { databricksConfig } from '@/config/app-env-vars/databricks'
88
import logger from '@/helpers/logger'
99

10-
import { createClient } from '../auth/create-client'
11-
import { getSchemaName } from '../common/get-schema-name'
10+
import { createSession } from '../auth/create-client'
1211
import { DatabrickTableRes } from '../common/types'
1312

1413
const dynamicData: IDynamicData = {
@@ -17,16 +16,16 @@ const dynamicData: IDynamicData = {
1716

1817
async run($: IGlobalVariable): Promise<DynamicDataOutput> {
1918
try {
20-
const client = await createClient($)
21-
const session = await client.openSession()
19+
const { session, endSession } = await createSession($)
2220
const operation = await session.getTables({
2321
catalogName: databricksConfig.catalog,
24-
schemaName: getSchemaName($),
22+
schemaName: $.auth.data.schema as string,
2523
tableTypes: ['TABLE'],
2624
})
2725
const tables = (await operation.fetchAll({
2826
maxRows: 1000,
2927
})) as DatabrickTableRes[]
28+
await endSession()
3029
return {
3130
data: tables.map((row) => ({
3231
name: row.TABLE_NAME,

0 commit comments

Comments
 (0)