91 lines
2.3 KiB
Markdown
91 lines
2.3 KiB
Markdown
# Drizzle Reader
|
|
|
|
Reads TypeScript/JavaScript files containing Drizzle ORM schema definitions and extracts database schema information.
|
|
|
|
## Overview
|
|
|
|
The Drizzle Reader parses Drizzle ORM schema files (TypeScript/JavaScript) that define database tables using Drizzle's schema builder and converts them into RelSpec's internal database model representation.
|
|
|
|
## Features
|
|
|
|
- Parses Drizzle schema definitions
|
|
- Extracts table, column, and relationship information
|
|
- Supports various Drizzle column types
|
|
- Handles constraints and indexes
|
|
|
|
## Usage
|
|
|
|
### Basic Example
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers/drizzle"
|
|
)
|
|
|
|
func main() {
|
|
options := &readers.ReaderOptions{
|
|
FilePath: "/path/to/schema.ts",
|
|
}
|
|
|
|
reader := drizzle.NewReader(options)
|
|
db, err := reader.ReadDatabase()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("Found %d schemas\n", len(db.Schemas))
|
|
}
|
|
```
|
|
|
|
### CLI Example
|
|
|
|
```bash
|
|
# Read Drizzle schema and convert to JSON
|
|
relspec --input drizzle --in-file schema.ts --output json --out-file schema.json
|
|
|
|
# Convert Drizzle to GORM models
|
|
relspec --input drizzle --in-file schema/ --output gorm --out-file models.go
|
|
```
|
|
|
|
## Example Drizzle Schema
|
|
|
|
```typescript
|
|
import { pgTable, serial, varchar, text, timestamp, integer } from 'drizzle-orm/pg-core';
|
|
import { relations } from 'drizzle-orm';
|
|
|
|
export const users = pgTable('users', {
|
|
id: serial('id').primaryKey(),
|
|
username: varchar('username', { length: 50 }).notNull().unique(),
|
|
email: varchar('email', { length: 100 }).notNull(),
|
|
createdAt: timestamp('created_at').notNull().defaultNow(),
|
|
});
|
|
|
|
export const posts = pgTable('posts', {
|
|
id: serial('id').primaryKey(),
|
|
userId: integer('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
|
|
title: varchar('title', { length: 200 }).notNull(),
|
|
content: text('content'),
|
|
});
|
|
|
|
export const usersRelations = relations(users, ({ many }) => ({
|
|
posts: many(posts),
|
|
}));
|
|
|
|
export const postsRelations = relations(posts, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [posts.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
```
|
|
|
|
## Notes
|
|
|
|
- Supports both PostgreSQL and MySQL Drizzle schemas
|
|
- Extracts relationship information from `relations` definitions
|
|
- Schema defaults to `public` for PostgreSQL
|