buster/packages/database/.cursor/schema.mdc

38 lines
1.3 KiB
Plaintext
Raw Normal View History

---
globs: src/schema.ts
alwaysApply: true
---
## Adding a New JSONB Column
When adding a new JSONB column to the database schema, it is important to ensure type safety and maintainability by defining the corresponding TypeScript type. Follow these steps:
1. **Define the TypeScript Type**:
- Navigate to the `@/schema-types` directory.
- Create a new file for your entity if it doesn't exist (e.g., `newEntity.ts`).
- Define the TypeScript type for the JSONB column. For example:
```typescript
export type NewEntityConfig = {
key: string;
value: any; // Use specific types instead of 'any' for better type safety
};
```
2. **Export the Type**:
- Ensure the new type is exported from the `index.ts` file in the `@/schema-types` directory.
3. **Consume the Type in Schema**:
- In the `src/schema.ts` file, use the defined type for the JSONB column. For example:
```typescript
import type { NewEntityConfig } from './schema-types';
export const newEntities = pgTable('new_entities', {
// ... other columns
config: jsonb('config').$type<NewEntityConfig>().default(sql`'{}'::jsonb`).notNull(),
});
```
By following these steps, you ensure that the JSONB column is type-safe and maintainable, leveraging TypeScript's compile-time checks and IntelliSense support.