Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
3c1fb16
chore: update node version
dahal May 19, 2025
b4d9e5a
feat: prisma => drizzle and next 15 upgrade attempt
dahal May 21, 2025
8142e2b
feat: fix typesafety issues and refactor prisma query with drizzle
dahal May 21, 2025
bb7412b
feat: reformat files
dahal May 21, 2025
fa2d25a
feat: contd. type, import and query refactor
dahal May 21, 2025
a6d890d
feat: export proper schema and their types, and reformat
dahal May 21, 2025
fd64b86
feat: add few missing packages, few more to come
dahal May 21, 2025
7873bf5
fix: fixing type and enum imports
dahal May 21, 2025
be9089e
chore: reformat
dahal May 21, 2025
160a1eb
feat: install missing packages
dahal May 23, 2025
3835eee
fix: enum imports
dahal May 23, 2025
e616d05
fix: typesafety issues
dahal May 23, 2025
f39d244
fix: more typesafety issues
dahal May 23, 2025
dcf5f10
feat: more typesafety issue fixes on components
dahal May 23, 2025
9218833
fix: isLoading vs isPending trpc state
dahal May 23, 2025
b1b0e13
fix: some interesting typesafety fixes
dahal May 23, 2025
a33cdb8
chore: referencing file with `@/component`
dahal May 23, 2025
bd5bf34
chore: remove unwanted imports
dahal May 23, 2025
2b5e1ee
fix: typecheck
dahal May 23, 2025
8ae2ebd
fix: typecheck
dahal May 23, 2025
8f1ec6a
chore: remove un-necessary import
dahal May 23, 2025
4c910ee
chore: some more typesafety issues
dahal May 23, 2025
339a6e6
feat: component cleanup - checking in
dahal May 23, 2025
0b95fe2
chore: more import and typesafety cleanups
dahal May 23, 2025
fb8e501
feat: fix some more typesafety issues and missing packages
dahal May 24, 2025
9730b27
feat: refactor and document `@captable/db` package
dahal May 24, 2025
3637e06
fix: database, schema, enum all things db import
dahal May 24, 2025
2903a97
feat: replace prisma with drizzle query
dahal May 24, 2025
52aaeca
feat: more prisma - drizzle conversion
dahal May 24, 2025
8171704
feat: more db prisma - drizzle migration, and cuid as primary default id
dahal May 24, 2025
974c48f
fix: drizzling and some high level fixes
dahal May 24, 2025
b563505
fix: some more prisma to drizzle migration
dahal May 24, 2025
d73a407
fix: finally replace prisma with drizzle
dahal May 24, 2025
3ad159d
chore: removing additional layer
dahal May 24, 2025
3543ffc
1
dahal May 24, 2025
50243d6
chore: fixing build errors
dahal May 24, 2025
f88beea
feat: just to many fixes to count, including email getting its own pa…
dahal May 25, 2025
460b64d
chore: reformat
dahal May 25, 2025
476df5c
fix: email stuffs
dahal May 25, 2025
fcf029d
feat: get the build to work -- close
dahal May 26, 2025
e3e5396
feat: create utils package, cleanup email package, and fix more build…
dahal May 26, 2025
20be72b
feat: create cursor/rules for all apps and packages
dahal May 26, 2025
c851c24
fix: email component imports
dahal May 27, 2025
65903b0
chore: looks like NODE_ENV=staging is not a thing
dahal May 27, 2025
a6abd98
chore: fix build warning
dahal May 27, 2025
4f981fd
feat: next.config.js => next.config.ts
dahal May 27, 2025
0d60e90
feat: build finally works 🎉
dahal May 27, 2025
e870d02
feat: finalize email package setup and refactor
dahal May 27, 2025
c6c1c7b
chore: reformat
dahal May 27, 2025
d6b79f2
fix: fixing trpc error
dahal May 27, 2025
37d03b9
fix: fixing some components to make them dark/light compatible
dahal May 27, 2025
744351a
fix: logger package
dahal May 27, 2025
e9035d4
chore: lock in bun 1.2.15
dahal May 29, 2025
3d2c6d8
feat: upgrade next to 15.3.2
dahal May 29, 2025
f8b6e88
feat: updating auth function so migrating to better-auth becomes easier
dahal May 29, 2025
a927971
feat: creating better auth tables
dahal May 29, 2025
dfba6ae
feat: another steps forward to replace next-auth with better-auth
dahal May 29, 2025
154fb1e
feat: next-auth to better-auth - a work in progress
dahal May 30, 2025
95e6052
feat: fixing some session exports
dahal May 30, 2025
305232f
fix: next-auth to better-auth migration, types and other stuffs
dahal May 30, 2025
95d197d
feat: remove all references and files for next-auth
dahal May 30, 2025
bd7c7e1
feat: missing pieces on auth package
dahal May 30, 2025
4ecb32a
feat: seperate client and server for clientSideSession and serverSide…
dahal May 31, 2025
3a235a4
fix: auth related imports
dahal May 31, 2025
cd96d4c
feat: add missing headers
dahal May 31, 2025
0964b76
chore: add missing headers
dahal May 31, 2025
eff0950
feat: fix build errors, and some email package re/org
dahal May 31, 2025
623a75b
fix: email package build errors
dahal May 31, 2025
d3af458
chore: turbo upgrade
dahal May 31, 2025
9ee4817
feat: re/org theme code and fix dark/light mode flickering
dahal May 31, 2025
62ccd1d
whole lot of changes including dark/lite mode support
dahal May 31, 2025
ead4b9b
feat: add missing package
dahal Jun 1, 2025
cd81f52
feat: refactor rbac to its own package (#554)
dahal Jun 1, 2025
9c41244
WIP - reinventing the wheel, with our own queue system
dahal Jun 2, 2025
ad21aaf
chore: reformat
dahal Jun 2, 2025
533ecd5
chore: rename serverless-queue to just queue
dahal Jun 2, 2025
235420a
feat: index table as per the queries we run on jobs
dahal Jun 2, 2025
efc1b31
feat: convert class to function, and update some function names
dahal Jun 2, 2025
b0e2704
chore: fixing some type errors
dahal Jun 2, 2025
492b75f
feat: dev job runner, also fix dx command
dahal Jun 2, 2025
fb68ab2
feat: cleanup jobs demo mode logger
dahal Jun 2, 2025
92f3a0e
feat: update documentations
dahal Jun 2, 2025
27e2d4e
Merge pull request #555 from captableinc/feat/queue
dahal Jun 2, 2025
745ee01
feat: re-organize queue package
dahal Jun 4, 2025
3b6f0e9
feat: supporting both long running process and serverless, and 100% t…
dahal Jun 4, 2025
aa05ee6
Merge pull request #557 from captableinc/feat/queue-refactor
dahal Jun 4, 2025
c5c341b
chore: update queue docs with some relevant information
dahal Jun 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: refactor and document @captable/db package
  • Loading branch information
dahal committed May 24, 2025
commit 9730b2735f6a06f533a20f26c5a84ee357a1a79b
368 changes: 363 additions & 5 deletions packages/db/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,373 @@
# @captable/db

To install dependencies:
A tree-shakable database package for Captable, built with [Drizzle ORM](https://orm.drizzle.team/) and PostgreSQL.

## 🌟 Features

- **Tree-shakable exports**: Import only what you need
- **Type-safe**: Full TypeScript support with inferred types
- **Comprehensive**: Complete database schema and utilities
- **Drizzle ORM**: Built on the modern, performant Drizzle ORM
- **PostgreSQL**: Optimized for PostgreSQL databases
- **Auto-exports**: New tables are automatically available - no manual configuration needed

## 📦 Installation

```bash
bun install
bun install @captable/db
```

## 🚀 Quick Start

### Single Import Interface

```typescript
import {
db,
eq,
users,
companies,
UserSchema,
whereClause,
paginationClause
} from "@captable/db";

// Query users
const allUsers = await db.select().from(users);

// Query with conditions
const user = await db
.select()
.from(users)
.where(eq(users.email, "[email protected]"))
.limit(1);

// Using utility functions
const paginatedUsers = await db
.select()
.from(users)
.where(eq(users.email, "[email protected]"))
.limit(10)
.offset(0);
```

## 📋 Available Exports

### Database Instance & Types

- `db` - The main database instance
- `DB` - Database type
- `DBTransaction` - Transaction type
- `drizzleConfig` - Drizzle configuration

### Schema Tables (Auto-exported)

**All database tables are automatically available** - no manual configuration needed!

When you add a new table to any schema file, it's immediately available for import:

```typescript
import {
users,
companies,
members,
stakeholders,
accounts,
sessions,
passkeys,
verificationTokens,
bankAccounts,
audits,
shareClasses,
equityPlans,
buckets,
documents,
dataRooms,
templates,
shares,
options,
investments,
safes,
convertibleNotes,
updates,
billingProducts,
billingPrices,
billingSubscriptions,
billingCustomers,
accessTokens,
// ... any new tables you add
} from "@captable/db";
```

### Schema Types (Auto-exported)

**All Zod schemas and TypeScript types are automatically available**:

```typescript
import {
UserSchema,
CompanySchema,
BillingProductSchema,
// ... any new schemas you add
} from "@captable/db";

// Validate user data
const validatedUser = UserSchema.parse(userData);

// Infer types
type User = typeof users.$inferSelect;
type NewUser = typeof users.$inferInsert;
```

### Enums (Auto-exported)

**All database enums are automatically available**:

```typescript
import {
MemberStatusEnum,
RolesEnum,
StakeholderTypeEnum,
ShareLegendsEnum,
OptionTypeEnum,
SafeTypeEnum,
UpdateStatusEnum,
// ... any new enums you add
} from "@captable/db";
```

### Drizzle ORM Operators (Auto-exported)

Common query operators re-exported for convenience:

```typescript
import {
eq, // Equal
ne, // Not equal
gt, // Greater than
gte, // Greater than or equal
lt, // Less than
lte, // Less than or equal
isNull, // Is NULL
isNotNull, // Is NOT NULL
and, // AND condition
or, // OR condition
not, // NOT condition
like, // LIKE (case-sensitive)
ilike, // ILIKE (case-insensitive)
between, // BETWEEN
inArray, // IN array
notInArray, // NOT IN array
desc, // Descending order
asc, // Ascending order
sql, // Raw SQL
count // Count function
} from "@captable/db";
```

### Utility Functions (Auto-exported)

Custom helper functions for common operations:

```typescript
import {
whereClause,
orderByClause,
paginationClause
} from "@captable/db";

// Build complex where clauses
const whereCondition = whereClause(
eq(users.active, true),
like(users.email, "%@company.com")
);

// Order by with direction
const orderBy = orderByClause(users.createdAt, "desc");

// Pagination
const pagination = paginationClause(2, 20); // page 2, 20 items per page
```

## 🌳 Tree Shaking

This package is fully tree-shakable using modern wildcard exports. You can import only what you need:

```typescript
// Import only specific tables and operators
import { db, users, companies, eq, and } from "@captable/db";

// Import utilities separately if needed
import { paginationClause } from "@captable/db";

// Import specific schemas
import { UserSchema } from "@captable/db";
```

To run:
The `package.json` includes:
- `"sideEffects": false` for optimal tree-shaking
- Proper `exports` field for module resolution
- TypeScript support with type definitions

**Note**: Modern bundlers (Webpack 5+, Vite, Rollup) handle wildcard exports (`export *`) efficiently and only include the modules you actually import.

## 📚 Usage Examples

### Basic Queries

```typescript
import { db, users, eq } from "@captable/db";

// Select all users
const allUsers = await db.select().from(users);

// Select specific user
const user = await db
.select()
.from(users)
.where(eq(users.id, "user-id"))
.limit(1);

// Select with multiple conditions
const activeUsers = await db
.select()
.from(users)
.where(and(
eq(users.active, true),
isNotNull(users.emailVerified)
));
```

### Joins

```typescript
import { db, users, companies, members, eq } from "@captable/db";

const usersWithCompanies = await db
.select({
user: users,
company: companies
})
.from(users)
.innerJoin(members, eq(users.id, members.userId))
.innerJoin(companies, eq(members.companyId, companies.id));
```

### Transactions

```typescript
import { db, users, companies } from "@captable/db";

await db.transaction(async (tx) => {
// Insert company
const [company] = await tx
.insert(companies)
.values({ name: "New Company" })
.returning();

// Insert user
await tx
.insert(users)
.values({
name: "John Doe",
email: "[email protected]"
});
});
```

### Using Utilities

```typescript
import {
db,
users,
eq,
whereClause,
orderByClause,
paginationClause
} from "@captable/db";

const { limit, offset } = paginationClause(1, 10);
const { where } = whereClause(eq(users.active, true));
const { orderBy } = orderByClause(users.createdAt, "desc");

const paginatedUsers = await db
.select()
.from(users)
.where(where)
.orderBy(orderBy)
.limit(limit)
.offset(offset);
```

## 🛠 Development

### Scripts

```bash
bun run index.ts
# Lint code
bun run lint

# Format code
bun run format

# Generate migrations
bun run generate

# Run migrations
bun run migrate

# Combined migration workflow
bun run db:migrate

# Open Drizzle Studio
bun run db:studio

# Seed database
bun run db:seed
```

### Project Structure

```
packages/db/
├── schema/ # Database schema definitions
│ ├── index.ts # Schema exports
│ ├── enums.ts # Database enums
│ ├── users.ts # Users table
│ ├── companies.ts # Companies table
│ └── ... # Other schema files
├── migrations/ # Database migrations
├── seeds/ # Database seed files
├── utils.ts # Utility functions
├── config.ts # Drizzle configuration
├── index.ts # Main export file
└── package.json # Package configuration
```

## 🔧 Configuration

The database connection is configured via environment variables:

```env
DATABASE_URL=postgresql://user:password@localhost:5432/database
```

## 📄 License

This package is part of the Captable project and follows the same license terms.

## 🤝 Contributing

1. Make changes to schema files in `schema/` directory
2. Update exports in `schema/index.ts` if adding new schema files
3. Run `bun run generate` to create migrations
4. Test your changes

**Note**: Individual tables, types, and enums are automatically exported from the main package - no manual export management needed in the main `index.ts` file!

## 🔗 Related

This project was created using `bun init` in bun v1.1.21. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
- [Drizzle ORM Documentation](https://orm.drizzle.team/)
- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
- [Captable Main Repository](https://github.com/octolane/captable)
Loading