prisma-safe-ai-query 1.0.5 → 1.0.6
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/README.md +15 -16
- package/package.json +1 -1
- package/prisma_safe_ai_query_readme (1).md +124 -0
package/README.md
CHANGED
|
@@ -17,8 +17,7 @@ Instead of manually configuring the Gemini SDK, our built-in `PrismaGeminiAgent`
|
|
|
17
17
|
Ensure you have your Gemini API key in your `.env` file:
|
|
18
18
|
```env
|
|
19
19
|
GEMINI_API_KEY=your_copied_api_key_here
|
|
20
|
-
GEMINI_MODEL=
|
|
21
|
-
|
|
20
|
+
GEMINI_MODEL=gemini_model_name_here
|
|
22
21
|
```
|
|
23
22
|
*(Need an API key? Get one for free at [aistudio.google.com](https://aistudio.google.com/))*
|
|
24
23
|
|
|
@@ -50,22 +49,21 @@ async function handleChatSubmit(userInput) {
|
|
|
50
49
|
### Step 2: The Backend API Route (Next.js / Express)
|
|
51
50
|
Your backend receives the prompt, safely converts it to a Prisma query, executes it dynamically across ANY table, and returns the data.
|
|
52
51
|
|
|
52
|
+
**Import your Prisma Client**
|
|
53
53
|
```typescript
|
|
54
|
-
//
|
|
55
|
-
//prisma v5 import
|
|
54
|
+
// Prisma v5 import
|
|
56
55
|
import { PrismaClient } from '@prisma/client';
|
|
57
56
|
const prisma = new PrismaClient();
|
|
58
57
|
|
|
59
|
-
//
|
|
60
|
-
const prisma = require("./helper/prisma");
|
|
61
|
-
|
|
62
|
-
// You can also follow my tutorial here to learn how to install prisma v7.8.0 in your project
|
|
58
|
+
// Prisma v6+ import from helper file
|
|
59
|
+
// const prisma = require("./helper/prisma");
|
|
63
60
|
```
|
|
64
|
-
*https://charisintelligence.com.ng/prisma-v7-express-postgresql
|
|
65
|
-
|
|
61
|
+
*(Check out my tutorial on [how to install Prisma v7.8.0 in your Express/PostgreSQL project](https://charisintelligence.com.ng/prisma-v7-express-postgresql/))*
|
|
66
62
|
|
|
63
|
+
**Execute the AI Agent**
|
|
67
64
|
```typescript
|
|
68
65
|
import { PrismaGeminiAgent } from 'prisma-safe-ai-query';
|
|
66
|
+
|
|
69
67
|
// Automatically picks up GEMINI_API_KEY from process.env
|
|
70
68
|
const aiAgent = new PrismaGeminiAgent();
|
|
71
69
|
|
|
@@ -101,14 +99,15 @@ export async function POST(req: Request) {
|
|
|
101
99
|
|
|
102
100
|
## 🔒 Security Configuration
|
|
103
101
|
|
|
104
|
-
You can strictly control what the AI can and cannot access by passing a configuration object as the second parameter to the aiAgent.generateSafeQuery() method.
|
|
102
|
+
You can strictly control what the AI can and cannot access by passing a configuration object as the second parameter to the `aiAgent.generateSafeQuery()` method.
|
|
105
103
|
|
|
106
|
-
Where to configure this
|
|
104
|
+
**Where to configure this:** You should update these settings directly inside your backend controller or API route file (e.g., `app/api/chat/route.ts` in Next.js, or your Express endpoint function) exactly where you process the incoming chat request.
|
|
107
105
|
|
|
108
106
|
```typescript
|
|
109
107
|
const { targetTable, where } = await aiAgent.generateSafeQuery(userPrompt, {
|
|
110
108
|
|
|
111
|
-
// OPTION 1: Describe your DB to Gemini so it doesn't hallucinate tables.
|
|
109
|
+
// OPTION 1: Describe your DB to Gemini so it doesn't hallucinate tables.
|
|
110
|
+
// A fast hack is to use an AI (like ChatGPT) to generate your dbContext by giving it your prisma schema.
|
|
112
111
|
dbContext: "We have tables: user, product. User has fields: email, role, status.",
|
|
113
112
|
|
|
114
113
|
// OPTION 2: Block specific sensitive fields across all tables
|
|
@@ -120,6 +119,6 @@ const { targetTable, where } = await aiAgent.generateSafeQuery(userPrompt, {
|
|
|
120
119
|
```
|
|
121
120
|
|
|
122
121
|
## 👨💻 Author
|
|
123
|
-
|
|
124
|
-
https://
|
|
125
|
-
|
|
122
|
+
Created by **Olugbenga Taiwo**
|
|
123
|
+
- [LinkedIn](https://www.linkedin.com/in/taiwo-olugbenga)
|
|
124
|
+
- [GitHub Profile](https://github.com/oluibk2020/)
|
package/package.json
CHANGED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# 🛡️ Prisma Safe AI Query
|
|
2
|
+
|
|
3
|
+
A lightweight, enterprise-grade utility that securely translates natural language into strongly typed, safe Prisma queries using Google Gemini. By bundling `@google/genai` and `zod` directly, this package eliminates all backend boilerplate. You pass in a user prompt, and the package handles the AI orchestration, schema enforcement, and SQL-injection prevention automatically.
|
|
4
|
+
|
|
5
|
+
## 📦 Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install prisma-safe-ai-query
|
|
9
|
+
```
|
|
10
|
+
*(Requires `@prisma/client` as a peer dependency)*
|
|
11
|
+
|
|
12
|
+
## 🚀 Quick Start (The PrismaGeminiAgent)
|
|
13
|
+
|
|
14
|
+
Instead of manually configuring the Gemini SDK, our built-in `PrismaGeminiAgent` handles everything end-to-end.
|
|
15
|
+
|
|
16
|
+
### 1. Set your API Key
|
|
17
|
+
Ensure you have your Gemini API key in your `.env` file:
|
|
18
|
+
```env
|
|
19
|
+
GEMINI_API_KEY=your_copied_api_key_here
|
|
20
|
+
GEMINI_MODEL=gemini-3.8-flash
|
|
21
|
+
```
|
|
22
|
+
*(Need an API key? Get one for free at [aistudio.google.com](https://aistudio.google.com/))*
|
|
23
|
+
|
|
24
|
+
## 💬 The Chat Flow (Frontend to Backend)
|
|
25
|
+
|
|
26
|
+
This package makes it incredibly easy to build a "Chat with your Database" UI. Your frontend remains a simple chat interface, while `PrismaGeminiAgent` handles the heavy lifting on your backend.
|
|
27
|
+
|
|
28
|
+
### Step 1: The Frontend (React/Vue/Vanilla)
|
|
29
|
+
The user types a natural language query in your chat UI (e.g., *"Show me all active users in London"*). The frontend simply POSTs this message to your backend.
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
// Example React submit handler
|
|
33
|
+
async function handleChatSubmit(userInput) {
|
|
34
|
+
const response = await fetch('/api/chat', {
|
|
35
|
+
method: 'POST',
|
|
36
|
+
headers: { 'Content-Type': 'application/json' },
|
|
37
|
+
body: JSON.stringify({ userPrompt: userInput })
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const result = await response.json();
|
|
41
|
+
|
|
42
|
+
if (result.error) return alert(result.error);
|
|
43
|
+
|
|
44
|
+
console.log("Safe Database Results:", result.data);
|
|
45
|
+
// Render results in your chat UI...
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Step 2: The Backend API Route (Next.js / Express)
|
|
50
|
+
Your backend receives the prompt, safely converts it to a Prisma query, executes it dynamically across ANY table, and returns the data.
|
|
51
|
+
|
|
52
|
+
**Import your Prisma Client**
|
|
53
|
+
```typescript
|
|
54
|
+
// Prisma v5 import
|
|
55
|
+
import { PrismaClient } from '@prisma/client';
|
|
56
|
+
const prisma = new PrismaClient();
|
|
57
|
+
|
|
58
|
+
// Prisma v6+ import from helper file
|
|
59
|
+
// const prisma = require("./helper/prisma");
|
|
60
|
+
```
|
|
61
|
+
*(Check out my tutorial on [how to install Prisma v7.8.0 in your Express/PostgreSQL project](https://charisintelligence.com.ng/prisma-v7-express-postgresql/))*
|
|
62
|
+
|
|
63
|
+
**Execute the AI Agent**
|
|
64
|
+
```typescript
|
|
65
|
+
import { PrismaGeminiAgent } from 'prisma-safe-ai-query';
|
|
66
|
+
|
|
67
|
+
// Automatically picks up GEMINI_API_KEY from process.env
|
|
68
|
+
const aiAgent = new PrismaGeminiAgent();
|
|
69
|
+
|
|
70
|
+
export async function POST(req: Request) {
|
|
71
|
+
const { userPrompt } = await req.json(); // e.g. "Show me all active users in London"
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
// 1. One line handles Gemini generation, dynamic routing, and Zod security!
|
|
75
|
+
const { targetTable, where } = await aiAgent.generateSafeQuery(userPrompt, {
|
|
76
|
+
dbContext: "Available tables are user, product, and order.", // Helps Gemini route correctly
|
|
77
|
+
blockList: ['password', 'credit_card'] // Automatically blocks these columns
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// 2. DYNAMIC ROUTING: Validate the table and execute
|
|
81
|
+
if (!targetTable || !(targetTable in prisma)) {
|
|
82
|
+
return new Response(JSON.stringify({ error: "Invalid table requested." }), { status: 400 });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Executes equivalent of prisma.user.findMany(...)
|
|
86
|
+
const dbResults = await (prisma as any)[targetTable].findMany({
|
|
87
|
+
where: where,
|
|
88
|
+
take: 10
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// 3. Return the data to the frontend chat UI
|
|
92
|
+
return new Response(JSON.stringify({ data: dbResults }), { status: 200 });
|
|
93
|
+
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return new Response(JSON.stringify({ error: error.message }), { status: 400 });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## 🔒 Security Configuration
|
|
101
|
+
|
|
102
|
+
You can strictly control what the AI can and cannot access by passing a configuration object as the second parameter to the `aiAgent.generateSafeQuery()` method.
|
|
103
|
+
|
|
104
|
+
**Where to configure this:** You should update these settings directly inside your backend controller or API route file (e.g., `app/api/chat/route.ts` in Next.js, or your Express endpoint function) exactly where you process the incoming chat request.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const { targetTable, where } = await aiAgent.generateSafeQuery(userPrompt, {
|
|
108
|
+
|
|
109
|
+
// OPTION 1: Describe your DB to Gemini so it doesn't hallucinate tables.
|
|
110
|
+
// A fast hack is to use an AI (like ChatGPT) to generate your dbContext by giving it your prisma schema.
|
|
111
|
+
dbContext: "We have tables: user, product. User has fields: email, role, status.",
|
|
112
|
+
|
|
113
|
+
// OPTION 2: Block specific sensitive fields across all tables
|
|
114
|
+
blockList: ['passwordHash', 'billingDetails'],
|
|
115
|
+
|
|
116
|
+
// OPTION 3: Strict Mode - allow ONLY these fields
|
|
117
|
+
// allowList: ['status', 'role', 'city']
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## 👨💻 Author
|
|
122
|
+
Created by **Olugbenga Taiwo**
|
|
123
|
+
- [LinkedIn](https://www.linkedin.com/in/taiwo-olugbenga)
|
|
124
|
+
- [GitHub Profile](https://github.com/oluibk2020/)
|