1.1.24 • Published 12 months ago
@muybuen/retool-db-react v1.1.24
@muybuen/retool-db-react
A TypeScript first React hook for working with Retool PostgreSQL databases in Next.js applications.
Installation
npm install @muybuen/retool-db-reactUsage
Using in Next.js App Router
- Add your database URL to environment:
RETOOL_DATABASE_URL=postgresql://user:password@host:port/database- Create API route handler:
// app/api/retool-db/[tableName]/route.ts
import { retoolDbHandler } from '@muybuen/retool-db-react/server';
export { retoolDbHandler as POST, retoolDbHandler as PUT, retoolDbHandler as DELETE };- Generate types:
npx retool-db-types --url=$RETOOL_DATABASE_URLClient Components
import { useRetoolDatabase } from '@muybuen/retool-db-react';
import { Users } from './types';
// Basic Query
function UserList() {
const { data, isLoading, error } = useRetoolDatabase<Users>('users');
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<ul>
{data?.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
// Custom Query
function ActiveUsers() {
const { data } = useRetoolDatabase<Users>('users', {
query: 'SELECT * FROM users WHERE status = $1',
params: ['active'],
limit: 50
});
}
// Mutations
function UserForm() {
const { insert, update, remove } = useRetoolDatabase<Users>('users');
const addUser = () => insert({ name: 'John', email: 'john@example.com' });
const updateUser = (id: number) => update({ id }, { name: 'Jane' });
const deleteUser = (id: number) => remove({ id });
}Server Components
import { queryRetoolDatabase } from '@muybuen/retool-db-react/server';
export default async function UsersPage() {
const users = await queryRetoolDatabase<User>('users', {
query: 'SELECT * FROM users WHERE active = $1',
params: [true]
});
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}Type Generation
# All tables
npx retool-db-types --url=your_database_url
# Specific table
npx retool-db-types --url=your_database_url --table=users
# Custom output directory
npx retool-db-types --url=your_database_url --output=./src/typesType Properties
interface RetoolDatabaseOptions {
query?: string; // Custom SQL query
params?: unknown[]; // Query parameters
limit?: number; // Result limit
}
interface RetoolDatabaseConfig {
baseUrl?: string; // Custom API base URL
}
interface RetoolDatabaseReturn<T> {
data: T[] | null;
isLoading: boolean;
error: RetoolDatabaseError | null;
insert: (data: Partial<T>) => Promise<T>;
update: (where: Partial<T>, data: Partial<T>) => Promise<T[]>;
remove: (where: Partial<T>) => Promise<T[]>;
refetch: () => Promise<void>;
}Contributing
This project is maintained by John Choura. See CONTRIBUTORS for contributing guidelines.
License
1.1.24
12 months ago
1.1.23
12 months ago
1.1.22
12 months ago
1.1.22-1
12 months ago
1.1.22-0
12 months ago
1.1.21
12 months ago
1.1.20
12 months ago
1.1.19
12 months ago
1.1.18
12 months ago
1.1.17
12 months ago
1.1.16
12 months ago
1.1.15
12 months ago
1.1.14
12 months ago
1.1.13
12 months ago
1.1.12
12 months ago
1.1.11
12 months ago
1.1.10
12 months ago
1.1.9
12 months ago
1.1.8
12 months ago
1.1.7
12 months ago
1.1.6
12 months ago
1.1.5
12 months ago
1.1.2
12 months ago
1.1.1
12 months ago
1.1.0
12 months ago
1.0.3
12 months ago
1.0.2
12 months ago
1.0.0
12 months ago