mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-10 07:27:57 -05:00
* add parallel ai, postgres, mysql, slight modifications to dark mode styling * bun install frozen lockfile * new deps
262 lines
7.5 KiB
TypeScript
262 lines
7.5 KiB
TypeScript
import { PostgresIcon } from '@/components/icons'
|
|
import type { BlockConfig } from '@/blocks/types'
|
|
import type { PostgresResponse } from '@/tools/postgresql/types'
|
|
|
|
export const PostgreSQLBlock: BlockConfig<PostgresResponse> = {
|
|
type: 'postgresql',
|
|
name: 'PostgreSQL',
|
|
description: 'Connect to PostgreSQL database',
|
|
longDescription:
|
|
'Connect to any PostgreSQL database to execute queries, manage data, and perform database operations. Supports SELECT, INSERT, UPDATE, DELETE operations with secure connection handling.',
|
|
docsLink: 'https://docs.sim.ai/tools/postgresql',
|
|
category: 'tools',
|
|
bgColor: '#336791',
|
|
icon: PostgresIcon,
|
|
subBlocks: [
|
|
{
|
|
id: 'operation',
|
|
title: 'Operation',
|
|
type: 'dropdown',
|
|
layout: 'full',
|
|
options: [
|
|
{ label: 'Query (SELECT)', id: 'query' },
|
|
{ label: 'Insert Data', id: 'insert' },
|
|
{ label: 'Update Data', id: 'update' },
|
|
{ label: 'Delete Data', id: 'delete' },
|
|
{ label: 'Execute Raw SQL', id: 'execute' },
|
|
],
|
|
value: () => 'query',
|
|
},
|
|
{
|
|
id: 'host',
|
|
title: 'Host',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'localhost or your.database.host',
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'port',
|
|
title: 'Port',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: '5432',
|
|
value: () => '5432',
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'database',
|
|
title: 'Database Name',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'your_database',
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'username',
|
|
title: 'Username',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'postgres',
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'password',
|
|
title: 'Password',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
password: true,
|
|
placeholder: 'Your database password',
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'ssl',
|
|
title: 'SSL Mode',
|
|
type: 'dropdown',
|
|
layout: 'full',
|
|
options: [
|
|
{ label: 'Disabled', id: 'disabled' },
|
|
{ label: 'Required', id: 'required' },
|
|
{ label: 'Preferred', id: 'preferred' },
|
|
],
|
|
value: () => 'preferred',
|
|
},
|
|
// Table field for insert/update/delete operations
|
|
{
|
|
id: 'table',
|
|
title: 'Table Name',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'users',
|
|
condition: { field: 'operation', value: 'insert' },
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'table',
|
|
title: 'Table Name',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'users',
|
|
condition: { field: 'operation', value: 'update' },
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'table',
|
|
title: 'Table Name',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'users',
|
|
condition: { field: 'operation', value: 'delete' },
|
|
required: true,
|
|
},
|
|
// SQL Query field
|
|
{
|
|
id: 'query',
|
|
title: 'SQL Query',
|
|
type: 'code',
|
|
layout: 'full',
|
|
placeholder: 'SELECT * FROM users WHERE active = true',
|
|
condition: { field: 'operation', value: 'query' },
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'query',
|
|
title: 'SQL Query',
|
|
type: 'code',
|
|
layout: 'full',
|
|
placeholder: 'SELECT * FROM table_name',
|
|
condition: { field: 'operation', value: 'execute' },
|
|
required: true,
|
|
},
|
|
// Data for insert operations
|
|
{
|
|
id: 'data',
|
|
title: 'Data (JSON)',
|
|
type: 'code',
|
|
layout: 'full',
|
|
placeholder: '{\n "name": "John Doe",\n "email": "john@example.com",\n "active": true\n}',
|
|
condition: { field: 'operation', value: 'insert' },
|
|
required: true,
|
|
},
|
|
// Set clause for updates
|
|
{
|
|
id: 'data',
|
|
title: 'Update Data (JSON)',
|
|
type: 'code',
|
|
layout: 'full',
|
|
placeholder: '{\n "name": "Jane Doe",\n "email": "jane@example.com"\n}',
|
|
condition: { field: 'operation', value: 'update' },
|
|
required: true,
|
|
},
|
|
// Where clause for update/delete
|
|
{
|
|
id: 'where',
|
|
title: 'WHERE Condition',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'id = 1',
|
|
condition: { field: 'operation', value: 'update' },
|
|
required: true,
|
|
},
|
|
{
|
|
id: 'where',
|
|
title: 'WHERE Condition',
|
|
type: 'short-input',
|
|
layout: 'full',
|
|
placeholder: 'id = 1',
|
|
condition: { field: 'operation', value: 'delete' },
|
|
required: true,
|
|
},
|
|
],
|
|
tools: {
|
|
access: [
|
|
'postgresql_query',
|
|
'postgresql_insert',
|
|
'postgresql_update',
|
|
'postgresql_delete',
|
|
'postgresql_execute',
|
|
],
|
|
config: {
|
|
tool: (params) => {
|
|
switch (params.operation) {
|
|
case 'query':
|
|
return 'postgresql_query'
|
|
case 'insert':
|
|
return 'postgresql_insert'
|
|
case 'update':
|
|
return 'postgresql_update'
|
|
case 'delete':
|
|
return 'postgresql_delete'
|
|
case 'execute':
|
|
return 'postgresql_execute'
|
|
default:
|
|
throw new Error(`Invalid PostgreSQL operation: ${params.operation}`)
|
|
}
|
|
},
|
|
params: (params) => {
|
|
const { operation, data, ...rest } = params
|
|
|
|
// Parse JSON data if it's a string
|
|
let parsedData
|
|
if (data && typeof data === 'string' && data.trim()) {
|
|
try {
|
|
parsedData = JSON.parse(data)
|
|
} catch (parseError) {
|
|
const errorMsg = parseError instanceof Error ? parseError.message : 'Unknown JSON error'
|
|
throw new Error(`Invalid JSON data format: ${errorMsg}. Please check your JSON syntax.`)
|
|
}
|
|
} else if (data && typeof data === 'object') {
|
|
parsedData = data
|
|
}
|
|
|
|
// Build connection config
|
|
const connectionConfig = {
|
|
host: rest.host,
|
|
port: typeof rest.port === 'string' ? Number.parseInt(rest.port, 10) : rest.port || 5432,
|
|
database: rest.database,
|
|
username: rest.username,
|
|
password: rest.password,
|
|
ssl: rest.ssl || 'preferred',
|
|
}
|
|
|
|
// Build params object
|
|
const result: any = { ...connectionConfig }
|
|
|
|
if (rest.table) result.table = rest.table
|
|
if (rest.query) result.query = rest.query
|
|
if (rest.where) result.where = rest.where
|
|
if (parsedData !== undefined) result.data = parsedData
|
|
|
|
return result
|
|
},
|
|
},
|
|
},
|
|
inputs: {
|
|
operation: { type: 'string', description: 'Database operation to perform' },
|
|
host: { type: 'string', description: 'Database host' },
|
|
port: { type: 'string', description: 'Database port' },
|
|
database: { type: 'string', description: 'Database name' },
|
|
username: { type: 'string', description: 'Database username' },
|
|
password: { type: 'string', description: 'Database password' },
|
|
ssl: { type: 'string', description: 'SSL mode' },
|
|
table: { type: 'string', description: 'Table name' },
|
|
query: { type: 'string', description: 'SQL query to execute' },
|
|
data: { type: 'json', description: 'Data for insert/update operations' },
|
|
where: { type: 'string', description: 'WHERE clause for update/delete' },
|
|
},
|
|
outputs: {
|
|
message: {
|
|
type: 'string',
|
|
description: 'Success or error message describing the operation outcome',
|
|
},
|
|
rows: {
|
|
type: 'array',
|
|
description: 'Array of rows returned from the query',
|
|
},
|
|
rowCount: {
|
|
type: 'number',
|
|
description: 'Number of rows affected by the operation',
|
|
},
|
|
},
|
|
}
|