@trycompai/db 1.1.1 → 1.2.0
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/INTEGRATION_GUIDE.md +155 -266
- package/dist/{prisma/schema.prisma → schema.prisma} +2 -1
- package/package.json +18 -70
- package/README.md +0 -83
- package/dist/index.d.mts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -24
- package/dist/index.mjs +0 -2
- package/dist/prisma/functionDefinition.sql +0 -18
- package/dist/prisma/randomSecret.sql +0 -12
- package/dist/setup.d.mts +0 -7
- package/dist/setup.d.ts +0 -7
- package/dist/setup.js +0 -35
- package/dist/setup.mjs +0 -10
- package/dist/types.d.mts +0 -1
- package/dist/types.d.ts +0 -1
- package/dist/types.js +0 -24
- package/dist/types.mjs +0 -2
- package/scripts/setup-consumer.sh +0 -67
package/INTEGRATION_GUIDE.md
CHANGED
|
@@ -1,360 +1,249 @@
|
|
|
1
|
-
# Integration Guide
|
|
1
|
+
# Database Integration Guide
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This package provides a combined Prisma schema file for your application.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Schema-Only Distribution
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- A Turborepo-based project
|
|
9
|
-
- PostgreSQL database (local or cloud)
|
|
7
|
+
The `@trycompai/db` package provides a single, combined schema file that includes all our database models. You generate your own Prisma client from this schema.
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
**What's included:**
|
|
10
|
+
- 📄 Combined Prisma schema file
|
|
11
|
+
- 🗂️ All database models and enums
|
|
12
|
+
- 🔗 Proper relationships and constraints
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
**Benefits:**
|
|
15
|
+
- ✅ Always up-to-date with your Prisma version
|
|
16
|
+
- ✅ No version conflicts
|
|
17
|
+
- ✅ You control the generator configuration
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
cd apps/your-app
|
|
17
|
-
bun add @trycompai/db @prisma/client prisma
|
|
18
|
-
```
|
|
19
|
+
## Installation
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
```bash
|
|
22
|
+
# Using bun (recommended)
|
|
23
|
+
bun add @trycompai/db @prisma/client
|
|
24
|
+
bun add -D prisma
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
# Using npm
|
|
27
|
+
npm install @trycompai/db @prisma/client
|
|
28
|
+
npm install -D prisma
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
# Using yarn
|
|
31
|
+
yarn add @trycompai/db @prisma/client
|
|
32
|
+
yarn add -D prisma
|
|
26
33
|
```
|
|
27
34
|
|
|
28
|
-
##
|
|
35
|
+
## Setup
|
|
29
36
|
|
|
30
|
-
|
|
37
|
+
After installation, copy the schema file to your project and generate the Prisma client:
|
|
31
38
|
|
|
32
39
|
```bash
|
|
33
|
-
|
|
40
|
+
# Copy the schema file
|
|
41
|
+
cp node_modules/@trycompai/db/dist/schema.prisma prisma/schema.prisma
|
|
42
|
+
|
|
43
|
+
# Add the generator block to your schema
|
|
44
|
+
echo "
|
|
45
|
+
generator client {
|
|
46
|
+
provider = \"prisma-client-js\"
|
|
47
|
+
output = \"./generated\"
|
|
48
|
+
}" >> prisma/schema.prisma
|
|
49
|
+
|
|
50
|
+
# Generate the Prisma client
|
|
51
|
+
npx prisma generate
|
|
34
52
|
```
|
|
35
53
|
|
|
36
|
-
|
|
54
|
+
Or create a setup script in your `package.json`:
|
|
37
55
|
|
|
38
56
|
```json
|
|
39
57
|
{
|
|
40
58
|
"scripts": {
|
|
41
|
-
"db:
|
|
42
|
-
"
|
|
59
|
+
"db:setup": "cp node_modules/@trycompai/db/dist/schema.prisma prisma/schema.prisma && echo '\ngenerator client {\n provider = \"prisma-client-js\"\n output = \"./generated\"\n}' >> prisma/schema.prisma && prisma generate",
|
|
60
|
+
"db:generate": "prisma generate",
|
|
61
|
+
"db:migrate": "prisma migrate dev",
|
|
62
|
+
"db:studio": "prisma studio"
|
|
43
63
|
}
|
|
44
64
|
}
|
|
45
65
|
```
|
|
46
66
|
|
|
47
|
-
##
|
|
67
|
+
## Creating Your Database Client
|
|
48
68
|
|
|
49
|
-
Create
|
|
69
|
+
Create a database client in your project:
|
|
50
70
|
|
|
51
71
|
```typescript
|
|
52
|
-
|
|
72
|
+
// lib/db.ts (or wherever you prefer)
|
|
73
|
+
import { PrismaClient } from '../prisma/generated';
|
|
53
74
|
|
|
54
|
-
const globalForPrisma =
|
|
55
|
-
prisma: PrismaClient | undefined;
|
|
56
|
-
};
|
|
75
|
+
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
|
57
76
|
|
|
58
|
-
export const db =
|
|
59
|
-
globalForPrisma.prisma ??
|
|
60
|
-
new PrismaClient({
|
|
61
|
-
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
|
62
|
-
});
|
|
77
|
+
export const db = globalForPrisma.prisma || new PrismaClient();
|
|
63
78
|
|
|
64
79
|
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;
|
|
65
80
|
```
|
|
66
81
|
|
|
67
|
-
##
|
|
82
|
+
## Git Configuration
|
|
68
83
|
|
|
69
|
-
|
|
84
|
+
The Prisma client is generated to `src/db/generated/` and is automatically added to your `.gitignore`:
|
|
70
85
|
|
|
71
|
-
```json
|
|
72
|
-
{
|
|
73
|
-
"pipeline": {
|
|
74
|
-
"db:generate": {
|
|
75
|
-
"cache": false,
|
|
76
|
-
"outputs": ["node_modules/.prisma/**"]
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
86
|
```
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
}
|
|
87
|
+
# Generated Prisma Client
|
|
88
|
+
src/db/generated/
|
|
101
89
|
```
|
|
102
90
|
|
|
103
|
-
|
|
91
|
+
> **💡 Note**: The file structure and setup is identical for both standalone and monorepo installations. The only difference is the schema source (combined schema from npm package vs. individual schema files from workspace) during the initial setup process.
|
|
104
92
|
|
|
105
|
-
|
|
106
|
-
import { NextResponse } from 'next/server';
|
|
107
|
-
import { db } from '@/lib/db';
|
|
108
|
-
import type { Prisma } from '@trycompai/db';
|
|
93
|
+
⚠️ **Important**: Never commit the generated Prisma client - it's generated fresh on each install and build.
|
|
109
94
|
|
|
110
|
-
|
|
111
|
-
const data = (await request.json()) as Prisma.UserCreateInput;
|
|
95
|
+
## Environment Setup
|
|
112
96
|
|
|
113
|
-
|
|
114
|
-
data,
|
|
115
|
-
});
|
|
97
|
+
Create a `.env` file in your project root:
|
|
116
98
|
|
|
117
|
-
|
|
118
|
-
|
|
99
|
+
```env
|
|
100
|
+
DATABASE_URL="postgresql://username:password@localhost:5432/database"
|
|
119
101
|
```
|
|
120
102
|
|
|
121
|
-
|
|
103
|
+
## Usage
|
|
122
104
|
|
|
123
|
-
|
|
124
|
-
'use server';
|
|
105
|
+
Import and use the database client in your application:
|
|
125
106
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
export async function createUser(data: Prisma.UserCreateInput): Promise<User> {
|
|
130
|
-
return await db.user.create({ data });
|
|
131
|
-
}
|
|
132
|
-
```
|
|
107
|
+
```typescript
|
|
108
|
+
// Import the database client
|
|
109
|
+
import { db } from '@trycompai/db';
|
|
133
110
|
|
|
134
|
-
|
|
111
|
+
// Import types
|
|
112
|
+
import type { User, Organization, Departments } from '@trycompai/db/types';
|
|
135
113
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
});
|
|
114
|
+
// Query examples
|
|
115
|
+
const users = await db.user.findMany();
|
|
116
|
+
const organizations = await db.organization.findMany();
|
|
160
117
|
```
|
|
161
118
|
|
|
162
|
-
##
|
|
119
|
+
## Available Models
|
|
163
120
|
|
|
164
|
-
|
|
121
|
+
This package includes schemas for:
|
|
165
122
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
123
|
+
- 👤 **Authentication**: Users, Sessions, Accounts, Members
|
|
124
|
+
- 🏢 **Organizations**: Organizations, Invitations, Onboarding
|
|
125
|
+
- 📋 **Policies**: Policies, Policy Templates, Departments
|
|
126
|
+
- 🎯 **Tasks**: Tasks, Task Templates, Task Status
|
|
127
|
+
- 🔒 **Controls**: Controls, Framework Requirements
|
|
128
|
+
- 📊 **Risks**: Risk Management, Risk Categories
|
|
129
|
+
- 🏭 **Vendors**: Vendor Management, Contacts
|
|
130
|
+
- 🔧 **Integrations**: Third-party Integrations, Results
|
|
131
|
+
- 💬 **Comments**: Comments, Attachments
|
|
132
|
+
- 🔑 **API Keys**: API Key Management
|
|
133
|
+
- 📝 **Audit Logs**: Activity Tracking
|
|
176
134
|
|
|
177
|
-
##
|
|
135
|
+
## Commands
|
|
178
136
|
|
|
179
|
-
|
|
137
|
+
```bash
|
|
138
|
+
# Generate Prisma client (if needed)
|
|
139
|
+
bunx prisma generate --schema=src/db/schema.prisma
|
|
180
140
|
|
|
181
|
-
|
|
182
|
-
|
|
141
|
+
# View database in Prisma Studio
|
|
142
|
+
bunx prisma studio --schema=src/db/schema.prisma
|
|
183
143
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
email: { contains: '@example.com' },
|
|
187
|
-
createdAt: { gte: new Date('2024-01-01') },
|
|
188
|
-
};
|
|
144
|
+
# Reset database (careful!)
|
|
145
|
+
bunx prisma migrate reset --schema=src/db/schema.prisma
|
|
189
146
|
|
|
190
|
-
|
|
147
|
+
# Push schema changes (development)
|
|
148
|
+
bunx prisma db push --schema=src/db/schema.prisma
|
|
191
149
|
```
|
|
192
150
|
|
|
193
|
-
|
|
151
|
+
## TypeScript Integration
|
|
194
152
|
|
|
195
|
-
|
|
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
|
|
153
|
+
All types are automatically generated and available:
|
|
209
154
|
|
|
210
155
|
```typescript
|
|
211
|
-
import type {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
name: 'John Doe',
|
|
222
|
-
organizationId: org.id,
|
|
223
|
-
},
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
return { user, org };
|
|
227
|
-
});
|
|
156
|
+
import type {
|
|
157
|
+
User,
|
|
158
|
+
Organization,
|
|
159
|
+
Policy,
|
|
160
|
+
Risk,
|
|
161
|
+
Task,
|
|
162
|
+
Departments,
|
|
163
|
+
RiskStatus,
|
|
164
|
+
PolicyStatus,
|
|
165
|
+
} from '@/db/types';
|
|
228
166
|
```
|
|
229
167
|
|
|
230
|
-
##
|
|
168
|
+
## Troubleshooting
|
|
231
169
|
|
|
232
|
-
###
|
|
170
|
+
### Schema not found
|
|
233
171
|
|
|
234
|
-
|
|
172
|
+
If the schema wasn't copied automatically, run:
|
|
235
173
|
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
"dependencies": {
|
|
239
|
-
"@trycompai/db": "^1.1.0"
|
|
240
|
-
}
|
|
241
|
-
}
|
|
174
|
+
```bash
|
|
175
|
+
bunx @trycompai/db postinstall
|
|
242
176
|
```
|
|
243
177
|
|
|
244
|
-
###
|
|
178
|
+
### Client generation fails
|
|
245
179
|
|
|
246
180
|
```bash
|
|
247
|
-
#
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
# Run database generation across all apps
|
|
251
|
-
turbo run db:generate
|
|
181
|
+
# Manually generate the client
|
|
182
|
+
bunx prisma generate --schema=src/db/schema.prisma
|
|
252
183
|
```
|
|
253
184
|
|
|
254
|
-
###
|
|
185
|
+
### Generated files appearing in git
|
|
255
186
|
|
|
256
|
-
|
|
187
|
+
If you see generated Prisma files in your git status, add this to your `.gitignore`:
|
|
257
188
|
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
|
|
189
|
+
```gitignore
|
|
190
|
+
# Generated Prisma Client
|
|
191
|
+
src/db/generated/
|
|
261
192
|
```
|
|
262
193
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
### "Cannot find module '@prisma/client'"
|
|
194
|
+
Then clean up any tracked files:
|
|
266
195
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
|
196
|
+
```bash
|
|
197
|
+
git rm -r --cached src/db/generated/
|
|
198
|
+
git commit -m "Remove generated Prisma files from tracking"
|
|
199
|
+
```
|
|
289
200
|
|
|
290
|
-
|
|
201
|
+
### Database connection issues
|
|
291
202
|
|
|
292
|
-
1.
|
|
293
|
-
2.
|
|
294
|
-
3.
|
|
295
|
-
4. **Use transactions** for operations that must succeed together
|
|
296
|
-
5. **Enable query logging** only in development to avoid performance impact
|
|
203
|
+
1. Verify your `DATABASE_URL` in `.env`
|
|
204
|
+
2. Ensure your database is running
|
|
205
|
+
3. Check network connectivity
|
|
297
206
|
|
|
298
|
-
##
|
|
207
|
+
## Framework Compatibility
|
|
299
208
|
|
|
300
|
-
|
|
209
|
+
This package works with:
|
|
301
210
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
211
|
+
- ✅ Next.js (App Router & Pages Router)
|
|
212
|
+
- ✅ Remix
|
|
213
|
+
- ✅ Express.js
|
|
214
|
+
- ✅ Fastify
|
|
215
|
+
- ✅ Any Node.js framework
|
|
305
216
|
|
|
306
|
-
|
|
307
|
-
prisma: PrismaClient | undefined;
|
|
308
|
-
};
|
|
217
|
+
## Production Deployment
|
|
309
218
|
|
|
310
|
-
|
|
311
|
-
globalForPrisma.prisma ??
|
|
312
|
-
new PrismaClient({
|
|
313
|
-
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
|
314
|
-
});
|
|
219
|
+
The postinstall script automatically runs during deployment on:
|
|
315
220
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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
|
-
```
|
|
221
|
+
- ✅ Vercel (Node.js runtime)
|
|
222
|
+
- ✅ Railway (Docker containers)
|
|
223
|
+
- ✅ Render (Native Linux)
|
|
224
|
+
- ✅ AWS Lambda (Amazon Linux)
|
|
225
|
+
- ✅ Google Cloud Functions (Ubuntu-based)
|
|
226
|
+
- ✅ Docker (Debian/Alpine support)
|
|
340
227
|
|
|
341
|
-
|
|
228
|
+
**Cross-Platform Compatibility:**
|
|
229
|
+
The included binary targets ensure your app works across different deployment environments without additional configuration. The Prisma client will automatically select the correct binary for each platform.
|
|
342
230
|
|
|
343
|
-
|
|
231
|
+
For other platforms, ensure the postinstall script runs during your build process.
|
|
344
232
|
|
|
345
|
-
|
|
346
|
-
**Distribution**: Single combined `dist/prisma/schema.prisma` file for consumers
|
|
233
|
+
## Support
|
|
347
234
|
|
|
348
|
-
|
|
235
|
+
For issues or questions:
|
|
349
236
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
237
|
+
1. Check this integration guide
|
|
238
|
+
2. Verify your environment setup
|
|
239
|
+
3. Ensure all dependencies are installed
|
|
240
|
+
4. Check the Prisma documentation
|
|
353
241
|
|
|
354
|
-
|
|
242
|
+
## Schema Updates
|
|
355
243
|
|
|
356
|
-
|
|
244
|
+
When the `@trycompai/db` package is updated with new schema changes:
|
|
357
245
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
246
|
+
1. Update the package: `bun update @trycompai/db`
|
|
247
|
+
2. The postinstall script will automatically update your schema
|
|
248
|
+
3. Run `bunx prisma generate --schema=src/db/schema.prisma` to update your client
|
|
249
|
+
4. Update your application code if needed
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
generator client {
|
|
2
2
|
provider = "prisma-client-js"
|
|
3
|
-
previewFeatures = ["
|
|
3
|
+
previewFeatures = ["postgresqlExtensions"]
|
|
4
4
|
binaryTargets = ["native", "darwin-arm64", "debian-openssl-3.0.x", "linux-musl-openssl-3.0.x"]
|
|
5
|
+
output = "./generated/prisma"
|
|
5
6
|
}
|
|
6
7
|
|
|
7
8
|
datasource db {
|
package/package.json
CHANGED
|
@@ -1,95 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trycompai/db",
|
|
3
3
|
"description": "Database package with Prisma client and schema for Comp AI",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@prisma/client": "^6.13.0"
|
|
7
|
+
},
|
|
5
8
|
"devDependencies": {
|
|
6
|
-
"@prisma/client": "^6.9.0",
|
|
7
|
-
"prisma": "6.9.0",
|
|
8
9
|
"@trycompai/tsconfig": "workspace:*",
|
|
9
|
-
"
|
|
10
|
-
"tsup": "^8.5.0",
|
|
10
|
+
"prisma": "^6.13.0",
|
|
11
11
|
"typescript": "^5.8.3"
|
|
12
12
|
},
|
|
13
|
-
"peerDependencies": {
|
|
14
|
-
"@prisma/client": "^6.9.0",
|
|
15
|
-
"prisma": "^6.9.0"
|
|
16
|
-
},
|
|
17
13
|
"exports": {
|
|
18
|
-
"
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
20
|
-
"import": "./dist/index.mjs",
|
|
21
|
-
"require": "./dist/index.js"
|
|
22
|
-
},
|
|
23
|
-
"./types": {
|
|
24
|
-
"types": "./dist/types.d.ts",
|
|
25
|
-
"import": "./dist/types.mjs",
|
|
26
|
-
"require": "./dist/types.js"
|
|
27
|
-
},
|
|
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"
|
|
14
|
+
"./schema": "./dist/schema.prisma"
|
|
34
15
|
},
|
|
35
16
|
"files": [
|
|
36
|
-
"dist
|
|
37
|
-
"scripts/setup-consumer.sh",
|
|
17
|
+
"dist/schema.prisma",
|
|
38
18
|
"README.md",
|
|
39
19
|
"INTEGRATION_GUIDE.md"
|
|
40
20
|
],
|
|
41
|
-
"keywords": [
|
|
42
|
-
"comp-ai",
|
|
43
|
-
"database",
|
|
44
|
-
"postgresql",
|
|
45
|
-
"prisma"
|
|
46
|
-
],
|
|
47
|
-
"license": "MIT",
|
|
48
|
-
"main": "./dist/index.js",
|
|
49
|
-
"module": "./dist/index.mjs",
|
|
50
|
-
"prisma": {
|
|
51
|
-
"schema": "prisma",
|
|
52
|
-
"seed": "ts-node prisma/seed/seed.ts"
|
|
53
|
-
},
|
|
54
21
|
"publishConfig": {
|
|
55
22
|
"access": "public"
|
|
56
23
|
},
|
|
57
24
|
"repository": {
|
|
58
25
|
"type": "git",
|
|
59
|
-
"url": "git+https://github.com/
|
|
26
|
+
"url": "git+https://github.com/trycompai/comp.git",
|
|
60
27
|
"directory": "packages/db"
|
|
61
28
|
},
|
|
62
29
|
"scripts": {
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"db:generate": "
|
|
66
|
-
"db:migrate": "
|
|
67
|
-
"db:push": "
|
|
68
|
-
"db:seed": "
|
|
69
|
-
"db:studio": "
|
|
70
|
-
"dev": "tsup --watch",
|
|
30
|
+
"lint": "prettier --check 'src/**/*.{ts,tsx,js,jsx,json}' 'prisma/**/*.prisma' && tsc --noEmit",
|
|
31
|
+
"check-types": "tsc --noEmit",
|
|
32
|
+
"db:generate": "prisma generate",
|
|
33
|
+
"db:migrate": "prisma migrate dev",
|
|
34
|
+
"db:push": "prisma db push",
|
|
35
|
+
"db:seed": "prisma db seed",
|
|
36
|
+
"db:studio": "prisma studio",
|
|
71
37
|
"docker:clean": "docker-compose down -v",
|
|
72
38
|
"docker:down": "docker-compose down",
|
|
73
39
|
"docker:up": "docker-compose up -d",
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
|
|
77
|
-
},
|
|
78
|
-
"tsup": {
|
|
79
|
-
"entry": [
|
|
80
|
-
"src/index.ts",
|
|
81
|
-
"src/types.ts",
|
|
82
|
-
"src/setup.ts"
|
|
83
|
-
],
|
|
84
|
-
"format": [
|
|
85
|
-
"cjs",
|
|
86
|
-
"esm"
|
|
87
|
-
],
|
|
88
|
-
"dts": true,
|
|
89
|
-
"clean": true,
|
|
90
|
-
"external": [
|
|
91
|
-
"@prisma/client"
|
|
92
|
-
]
|
|
93
|
-
},
|
|
94
|
-
"types": "./dist/index.d.ts"
|
|
40
|
+
"build": "node scripts/combine-schemas.js",
|
|
41
|
+
"prepublishOnly": "bun run build"
|
|
42
|
+
}
|
|
95
43
|
}
|
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
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,24 +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 __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
|
|
17
|
-
// src/index.ts
|
|
18
|
-
var index_exports = {};
|
|
19
|
-
module.exports = __toCommonJS(index_exports);
|
|
20
|
-
__reExport(index_exports, require("@prisma/client"), module.exports);
|
|
21
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
-
0 && (module.exports = {
|
|
23
|
-
...require("@prisma/client")
|
|
24
|
-
});
|
package/dist/index.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
-- Create function to generate prefixed CUID with sortable timestamp (compact)
|
|
2
|
-
CREATE OR REPLACE FUNCTION generate_prefixed_cuid(prefix text)
|
|
3
|
-
RETURNS text AS $$
|
|
4
|
-
DECLARE
|
|
5
|
-
timestamp_hex text;
|
|
6
|
-
random_hex text;
|
|
7
|
-
BEGIN
|
|
8
|
-
-- Generate timestamp component (seconds since epoch) as hex
|
|
9
|
-
timestamp_hex = LOWER(TO_HEX(EXTRACT(EPOCH FROM NOW())::BIGINT));
|
|
10
|
-
|
|
11
|
-
-- Generate 8 random bytes and encode as hex (16 characters)
|
|
12
|
-
-- Ensure we call the function from the correct schema if pgcrypto is installed elsewhere
|
|
13
|
-
random_hex = encode(gen_random_bytes(8), 'hex');
|
|
14
|
-
|
|
15
|
-
-- Combine prefix, timestamp, and random hex string
|
|
16
|
-
RETURN prefix || '_' || timestamp_hex || random_hex;
|
|
17
|
-
END;
|
|
18
|
-
$$ LANGUAGE plpgsql;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
-- Create function to generate a random secret
|
|
2
|
-
CREATE OR REPLACE FUNCTION generate_random_secret(length integer DEFAULT 32)
|
|
3
|
-
RETURNS text AS $$
|
|
4
|
-
DECLARE
|
|
5
|
-
result text;
|
|
6
|
-
BEGIN
|
|
7
|
-
-- Generate random bytes and encode as hex
|
|
8
|
-
-- Using gen_random_bytes from pgcrypto extension
|
|
9
|
-
result = encode(gen_random_bytes(length), 'hex');
|
|
10
|
-
RETURN result;
|
|
11
|
-
END;
|
|
12
|
-
$$ LANGUAGE plpgsql;
|
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';
|
package/dist/types.js
DELETED
|
@@ -1,24 +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 __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
|
|
17
|
-
// src/types.ts
|
|
18
|
-
var types_exports = {};
|
|
19
|
-
module.exports = __toCommonJS(types_exports);
|
|
20
|
-
__reExport(types_exports, require("@prisma/client"), module.exports);
|
|
21
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
-
0 && (module.exports = {
|
|
23
|
-
...require("@prisma/client")
|
|
24
|
-
});
|
package/dist/types.mjs
DELETED
|
@@ -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
|
-
"
|