@trycompai/db 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.mjs CHANGED
@@ -1,2 +1,9 @@
1
+ import {
2
+ __reExport,
3
+ __toESM,
4
+ require_prisma
5
+ } from "./chunk-Y7OSPXHC.mjs";
6
+
1
7
  // src/types.ts
2
- export * from "@prisma/client";
8
+ var types_exports = {};
9
+ __reExport(types_exports, __toESM(require_prisma()));
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "@trycompai/db",
3
3
  "description": "Database package with Prisma client and schema for Comp AI",
4
- "version": "1.1.1",
4
+ "version": "1.1.2",
5
+ "dependencies": {
6
+ "@prisma/client": "6.9.0"
7
+ },
5
8
  "devDependencies": {
6
- "@prisma/client": "^6.9.0",
7
9
  "prisma": "6.9.0",
8
10
  "@trycompai/tsconfig": "workspace:*",
9
11
  "ts-node": "^10.9.2",
@@ -11,26 +13,22 @@
11
13
  "typescript": "^5.8.3"
12
14
  },
13
15
  "peerDependencies": {
14
- "@prisma/client": "^6.9.0",
15
- "prisma": "^6.9.0"
16
+ "@prisma/client": "6.9.0",
17
+ "prisma": "6.9.0"
16
18
  },
17
19
  "exports": {
18
- ".": {
19
- "types": "./dist/index.d.ts",
20
+ ".": "./src/index.ts",
21
+ "./types": "./src/types.ts",
22
+ "./schema": "./dist/prisma/schema.prisma",
23
+ "./dist": {
20
24
  "import": "./dist/index.mjs",
21
25
  "require": "./dist/index.js"
22
26
  },
23
- "./types": {
24
- "types": "./dist/types.d.ts",
25
- "import": "./dist/types.mjs",
27
+ "./dist/types": {
28
+ "import": "./dist/types.mjs",
26
29
  "require": "./dist/types.js"
27
30
  },
28
- "./setup": {
29
- "types": "./dist/setup.d.ts",
30
- "import": "./dist/setup.mjs",
31
- "require": "./dist/setup.js"
32
- },
33
- "./schema": "./dist/prisma/schema.prisma"
31
+ "./dist/schema": "./dist/prisma/schema.prisma"
34
32
  },
35
33
  "files": [
36
34
  "dist/**",
@@ -78,14 +76,13 @@
78
76
  "tsup": {
79
77
  "entry": [
80
78
  "src/index.ts",
81
- "src/types.ts",
82
- "src/setup.ts"
79
+ "src/types.ts"
83
80
  ],
84
81
  "format": [
85
82
  "cjs",
86
83
  "esm"
87
84
  ],
88
- "dts": true,
85
+ "dts": false,
89
86
  "clean": true,
90
87
  "external": [
91
88
  "@prisma/client"
@@ -1,360 +0,0 @@
1
- # Integration Guide: Using @trycompai/db in Another Turborepo
2
-
3
- This guide walks you through integrating the `@trycompai/db` package into another Turborepo-based repository.
4
-
5
- ## Prerequisites
6
-
7
- - Node.js 18+ and npm/yarn/bun
8
- - A Turborepo-based project
9
- - PostgreSQL database (local or cloud)
10
-
11
- ## Step 1: Install the Package
12
-
13
- In your app directory (e.g., `apps/web` or `apps/api`):
14
-
15
- ```bash
16
- cd apps/your-app
17
- bun add @trycompai/db @prisma/client prisma
18
- ```
19
-
20
- ## Step 2: Set Up Environment Variables
21
-
22
- Create or update `.env` in your app directory:
23
-
24
- ```env
25
- DATABASE_URL="postgresql://user:password@localhost:5432/your_database"
26
- ```
27
-
28
- ## Step 3: Generate Prisma Client
29
-
30
- Run from your app directory:
31
-
32
- ```bash
33
- npx prisma generate --schema=node_modules/@trycompai/db/dist/prisma/schema.prisma
34
- ```
35
-
36
- **Pro tip**: Add this to your app's `package.json` scripts:
37
-
38
- ```json
39
- {
40
- "scripts": {
41
- "db:generate": "prisma generate --schema=node_modules/@trycompai/db/dist/prisma/schema.prisma",
42
- "postinstall": "npm run db:generate"
43
- }
44
- }
45
- ```
46
-
47
- ## Step 4: Create Database Instance
48
-
49
- Create `lib/db.ts` (or `src/lib/db.ts`) in your app:
50
-
51
- ```typescript
52
- import { PrismaClient } from '@prisma/client';
53
-
54
- const globalForPrisma = globalThis as unknown as {
55
- prisma: PrismaClient | undefined;
56
- };
57
-
58
- export const db =
59
- globalForPrisma.prisma ??
60
- new PrismaClient({
61
- log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
62
- });
63
-
64
- if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;
65
- ```
66
-
67
- ## Step 5: Update Turborepo Configuration
68
-
69
- Add to your root `turbo.json`:
70
-
71
- ```json
72
- {
73
- "pipeline": {
74
- "db:generate": {
75
- "cache": false,
76
- "outputs": ["node_modules/.prisma/**"]
77
- }
78
- }
79
- }
80
- ```
81
-
82
- ## Step 6: Use in Your Application
83
-
84
- ### In Next.js Server Components
85
-
86
- ```typescript
87
- import { db } from '@/lib/db';
88
- import type { User } from '@trycompai/db';
89
-
90
- export default async function UsersPage() {
91
- const users: User[] = await db.user.findMany();
92
-
93
- return (
94
- <div>
95
- {users.map(user => (
96
- <div key={user.id}>{user.name}</div>
97
- ))}
98
- </div>
99
- );
100
- }
101
- ```
102
-
103
- ### In API Routes (Next.js App Router)
104
-
105
- ```typescript
106
- import { NextResponse } from 'next/server';
107
- import { db } from '@/lib/db';
108
- import type { Prisma } from '@trycompai/db';
109
-
110
- export async function POST(request: Request) {
111
- const data = (await request.json()) as Prisma.UserCreateInput;
112
-
113
- const user = await db.user.create({
114
- data,
115
- });
116
-
117
- return NextResponse.json(user);
118
- }
119
- ```
120
-
121
- ### In Server Actions
122
-
123
- ```typescript
124
- 'use server';
125
-
126
- import { db } from '@/lib/db';
127
- import type { User, Prisma } from '@trycompai/db';
128
-
129
- export async function createUser(data: Prisma.UserCreateInput): Promise<User> {
130
- return await db.user.create({ data });
131
- }
132
- ```
133
-
134
- ### In tRPC Procedures
135
-
136
- ```typescript
137
- import { z } from 'zod';
138
- import { router, publicProcedure } from '@/server/trpc';
139
- import { db } from '@/lib/db';
140
- import type { User } from '@trycompai/db';
141
-
142
- export const userRouter = router({
143
- getAll: publicProcedure.query(async (): Promise<User[]> => {
144
- return await db.user.findMany();
145
- }),
146
-
147
- create: publicProcedure
148
- .input(
149
- z.object({
150
- email: z.string().email(),
151
- name: z.string(),
152
- }),
153
- )
154
- .mutation(async ({ input }) => {
155
- return await db.user.create({
156
- data: input,
157
- });
158
- }),
159
- });
160
- ```
161
-
162
- ## Step 7: TypeScript Configuration
163
-
164
- Ensure your `tsconfig.json` includes:
165
-
166
- ```json
167
- {
168
- "compilerOptions": {
169
- "paths": {
170
- "@/*": ["./src/*"],
171
- "@trycompai/db": ["node_modules/@trycompai/db"]
172
- }
173
- }
174
- }
175
- ```
176
-
177
- ## Common Patterns
178
-
179
- ### Type-Safe Queries
180
-
181
- ```typescript
182
- import type { Prisma } from '@trycompai/db';
183
-
184
- // Type-safe where clauses
185
- const whereClause: Prisma.UserWhereInput = {
186
- email: { contains: '@example.com' },
187
- createdAt: { gte: new Date('2024-01-01') },
188
- };
189
-
190
- const users = await db.user.findMany({ where: whereClause });
191
- ```
192
-
193
- ### Including Relations
194
-
195
- ```typescript
196
- const usersWithOrganizations = await db.user.findMany({
197
- include: {
198
- organization: true,
199
- memberships: {
200
- include: {
201
- organization: true,
202
- },
203
- },
204
- },
205
- });
206
- ```
207
-
208
- ### Transactions
209
-
210
- ```typescript
211
- import type { User, Organization } from '@trycompai/db';
212
-
213
- const result = await db.$transaction(async (tx) => {
214
- const org = await tx.organization.create({
215
- data: { name: 'New Org' },
216
- });
217
-
218
- const user = await tx.user.create({
219
- data: {
220
- email: 'user@example.com',
221
- name: 'John Doe',
222
- organizationId: org.id,
223
- },
224
- });
225
-
226
- return { user, org };
227
- });
228
- ```
229
-
230
- ## Turborepo-Specific Tips
231
-
232
- ### 1. Workspace Dependencies
233
-
234
- In your app's `package.json`:
235
-
236
- ```json
237
- {
238
- "dependencies": {
239
- "@trycompai/db": "^1.1.0"
240
- }
241
- }
242
- ```
243
-
244
- ### 2. Development Workflow
245
-
246
- ```bash
247
- # From root of your turborepo
248
- turbo run dev --filter=your-app
249
-
250
- # Run database generation across all apps
251
- turbo run db:generate
252
- ```
253
-
254
- ### 3. CI/CD Pipeline
255
-
256
- Add to your CI workflow:
257
-
258
- ```yaml
259
- - name: Generate Prisma Client
260
- run: turbo run db:generate
261
- ```
262
-
263
- ## Troubleshooting
264
-
265
- ### "Cannot find module '@prisma/client'"
266
-
267
- Run `npx prisma generate --schema=node_modules/@trycompai/db/dist/prisma/schema.prisma` in your app directory.
268
-
269
- ### "PrismaClient is unable to be run in the browser"
270
-
271
- Ensure you're only importing and using the database client in server-side code:
272
-
273
- - Server Components
274
- - API Routes
275
- - Server Actions
276
- - Backend services
277
-
278
- ### Type errors with Prisma types
279
-
280
- 1. Ensure `@prisma/client` version matches the one in `@trycompai/db`
281
- 2. Regenerate the client: `npm run db:generate`
282
- 3. Restart TypeScript server in your IDE
283
-
284
- ### Connection issues
285
-
286
- 1. Verify `DATABASE_URL` is set correctly
287
- 2. Check database is running and accessible
288
- 3. For SSL connections, add `?sslmode=require` to your connection string
289
-
290
- ## Best Practices
291
-
292
- 1. **Never import `db` in client components** - it will fail at runtime
293
- 2. **Use type imports for client code**: `import type { User } from '@trycompai/db'`
294
- 3. **Handle errors gracefully** - wrap database calls in try/catch
295
- 4. **Use transactions** for operations that must succeed together
296
- 5. **Enable query logging** only in development to avoid performance impact
297
-
298
- ## Example Full Setup
299
-
300
- Here's a complete example for a Next.js app in a Turborepo:
301
-
302
- ```typescript
303
- // apps/web/lib/db.ts
304
- import { PrismaClient } from '@prisma/client';
305
-
306
- const globalForPrisma = globalThis as unknown as {
307
- prisma: PrismaClient | undefined;
308
- };
309
-
310
- export const db =
311
- globalForPrisma.prisma ??
312
- new PrismaClient({
313
- log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
314
- });
315
-
316
- if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;
317
-
318
- // apps/web/app/users/page.tsx
319
- import { db } from '@/lib/db';
320
- import type { User } from '@trycompai/db';
321
-
322
- export default async function UsersPage() {
323
- const users: User[] = await db.user.findMany({
324
- orderBy: { createdAt: 'desc' },
325
- });
326
-
327
- return (
328
- <div>
329
- <h1>Users ({users.length})</h1>
330
- {users.map(user => (
331
- <div key={user.id}>
332
- <h2>{user.name}</h2>
333
- <p>{user.email}</p>
334
- </div>
335
- ))}
336
- </div>
337
- );
338
- }
339
- ```
340
-
341
- ## Schema Information
342
-
343
- The package maintains **separate schemas for development and distribution**:
344
-
345
- **Development**: Multiple organized schema files in `prisma/schema/` for better maintainability
346
- **Distribution**: Single combined `dist/prisma/schema.prisma` file for consumers
347
-
348
- The build process combines:
349
-
350
- - Base configuration and datasource
351
- - All model definitions from the `schema/` directory
352
- - Proper relationships and constraints
353
-
354
- This ensures your development setup stays clean while providing consumers with a simple single-file schema.
355
-
356
- ## Need Help?
357
-
358
- - Check the [Prisma documentation](https://www.prisma.io/docs)
359
- - Review the combined schema at `node_modules/@trycompai/db/dist/prisma/schema.prisma`
360
- - Look at type definitions with IDE autocomplete
package/README.md DELETED
@@ -1,83 +0,0 @@
1
- # @trycompai/db
2
-
3
- Shared Prisma schema and types for Comp AI applications.
4
-
5
- ## What This Package Provides
6
-
7
- - 📄 **Prisma Schema**: The source of truth for the database structure
8
- - 🏷️ **TypeScript Types**: Fully typed models for use across applications
9
- - 🔧 **Migration Files**: Database migrations to keep schemas in sync
10
-
11
- ## Installation
12
-
13
- ```bash
14
- # In your app directory
15
- npm install @trycompai/db @prisma/client prisma
16
- # or
17
- bun add @trycompai/db @prisma/client prisma
18
- ```
19
-
20
- ## Setup
21
-
22
- 1. **Generate Prisma Client** in your app:
23
-
24
- ```bash
25
- # Run this in your app directory after installing
26
- npx prisma generate --schema=node_modules/@trycompai/db/dist/prisma/schema.prisma
27
- ```
28
-
29
- 2. **Create your database instance** (`lib/db.ts`):
30
-
31
- ```typescript
32
- import { PrismaClient } from '@prisma/client';
33
-
34
- const globalForPrisma = globalThis as unknown as {
35
- prisma: PrismaClient | undefined;
36
- };
37
-
38
- export const db =
39
- globalForPrisma.prisma ??
40
- new PrismaClient({
41
- log: ['warn', 'error'],
42
- });
43
-
44
- if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;
45
- ```
46
-
47
- 3. **Use in your app**:
48
-
49
- ```typescript
50
- import { db } from '@/lib/db';
51
- import type { User, Organization, Prisma } from '@trycompai/db';
52
-
53
- // Full type safety!
54
- const users: User[] = await db.user.findMany();
55
-
56
- // Create with type-safe input
57
- const newUser = await db.user.create({
58
- data: {
59
- email: 'user@example.com',
60
- name: 'John Doe',
61
- } satisfies Prisma.UserCreateInput,
62
- });
63
- ```
64
-
65
- ## Why This Approach?
66
-
67
- - ✅ **Platform-specific binaries**: Each app generates its own query engine
68
- - ✅ **Proper connection management**: Each app manages its own connections
69
- - ✅ **Type safety**: Share types without runtime coupling
70
- - ✅ **Monorepo friendly**: Works seamlessly with any monorepo setup
71
-
72
- ## Development
73
-
74
- To update the schema:
75
-
76
- 1. Edit `prisma/schema.prisma` in this package
77
- 2. Run migrations: `bun run db:migrate`
78
- 3. Regenerate types: `bun run build`
79
- 4. Update version and publish
80
-
81
- ## Integration Guide
82
-
83
- For detailed instructions on using this package in another Turborepo, see [INTEGRATION_GUIDE.md](./INTEGRATION_GUIDE.md).
package/dist/index.d.mts DELETED
@@ -1,2 +0,0 @@
1
- export * from '@prisma/client';
2
- export { PrismaClient } from '@prisma/client';
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from '@prisma/client';
2
- export { PrismaClient } from '@prisma/client';
package/dist/setup.d.mts DELETED
@@ -1,7 +0,0 @@
1
- import * as _prisma_client_runtime_library from '@prisma/client/runtime/library';
2
- import * as _prisma_client from '@prisma/client';
3
- import { PrismaClient } from '@prisma/client';
4
-
5
- declare const db: PrismaClient<_prisma_client.Prisma.PrismaClientOptions, never, _prisma_client_runtime_library.DefaultArgs>;
6
-
7
- export { db };
package/dist/setup.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import * as _prisma_client_runtime_library from '@prisma/client/runtime/library';
2
- import * as _prisma_client from '@prisma/client';
3
- import { PrismaClient } from '@prisma/client';
4
-
5
- declare const db: PrismaClient<_prisma_client.Prisma.PrismaClientOptions, never, _prisma_client_runtime_library.DefaultArgs>;
6
-
7
- export { db };
package/dist/setup.js DELETED
@@ -1,35 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/setup.ts
21
- var setup_exports = {};
22
- __export(setup_exports, {
23
- db: () => db
24
- });
25
- module.exports = __toCommonJS(setup_exports);
26
- var import_client = require("@prisma/client");
27
- var globalForPrisma = globalThis;
28
- var db = globalForPrisma.prisma ?? new import_client.PrismaClient({
29
- log: ["warn", "error"]
30
- });
31
- if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db;
32
- // Annotate the CommonJS export names for ESM import in node:
33
- 0 && (module.exports = {
34
- db
35
- });
package/dist/setup.mjs DELETED
@@ -1,10 +0,0 @@
1
- // src/setup.ts
2
- import { PrismaClient } from "@prisma/client";
3
- var globalForPrisma = globalThis;
4
- var db = globalForPrisma.prisma ?? new PrismaClient({
5
- log: ["warn", "error"]
6
- });
7
- if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db;
8
- export {
9
- db
10
- };
package/dist/types.d.mts DELETED
@@ -1 +0,0 @@
1
- export * from '@prisma/client';
package/dist/types.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from '@prisma/client';
@@ -1,67 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Setup script for consumers of @trycompai/db
4
-
5
- echo "🔧 Setting up @trycompai/db in your project..."
6
-
7
- # Check if we're in a project with package.json
8
- if [ ! -f "package.json" ]; then
9
- echo "❌ Error: No package.json found. Run this from your project root."
10
- exit 1
11
- fi
12
-
13
- # Install dependencies if not already installed
14
- echo "📦 Checking dependencies..."
15
- if ! grep -q "@trycompai/db" package.json; then
16
- echo "Installing @trycompai/db and dependencies..."
17
- npm install @trycompai/db @prisma/client prisma --save
18
- fi
19
-
20
- # Generate Prisma client
21
- echo "🏗️ Generating Prisma client..."
22
- npx prisma generate --schema=node_modules/@trycompai/db/dist/prisma/schema.prisma
23
-
24
- # Create lib/db.ts if it doesn't exist
25
- if [ ! -f "lib/db.ts" ] && [ ! -f "src/lib/db.ts" ]; then
26
- echo "📝 Creating database instance file..."
27
-
28
- # Determine the correct path
29
- LIB_PATH="lib"
30
- if [ -d "src" ]; then
31
- LIB_PATH="src/lib"
32
- fi
33
-
34
- mkdir -p $LIB_PATH
35
-
36
- cat > $LIB_PATH/db.ts << 'EOF'
37
- import { PrismaClient } from '@prisma/client';
38
-
39
- const globalForPrisma = globalThis as unknown as {
40
- prisma: PrismaClient | undefined;
41
- };
42
-
43
- export const db =
44
- globalForPrisma.prisma ??
45
- new PrismaClient({
46
- log: ['warn', 'error'],
47
- });
48
-
49
- if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;
50
- EOF
51
-
52
- echo "✅ Created $LIB_PATH/db.ts"
53
- fi
54
-
55
- echo "
56
- ✨ Setup complete! You can now use the database:
57
-
58
- import { db } from '@/lib/db';
59
- import type { User, Organization, Prisma } from '@trycompai/db';
60
-
61
- const users = await db.user.findMany();
62
-
63
- // All Prisma types are available:
64
- const newUser = await db.user.create({
65
- data: {...} satisfies Prisma.UserCreateInput
66
- });
67
- "