Merge pull request #4 from RiccardoSenica/remove-prisma
chore: remove prisma
This commit is contained in:
@@ -51,10 +51,6 @@ yarn dev
|
||||
- `yarn audit` - Run audit
|
||||
- `yarn vercel:link` - Link Vercel project
|
||||
- `yarn vercel:env` - Pull .env from Vercel
|
||||
- `yarn prisma:migrate` - Migrate database
|
||||
- `yarn prisma:push` - Push migrations
|
||||
- `yarn prisma:generate`- Generate Prisma types
|
||||
- `yarn prisma:reset` - Reset database
|
||||
|
||||
## ⚠️ Disclaimer
|
||||
|
||||
|
||||
@@ -1,21 +1,11 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { generate } from '@utils/consumer/store';
|
||||
import { rateLimiter } from '@utils/rateLimiter';
|
||||
|
||||
export async function POST() {
|
||||
const rateLimit = await rateLimiter();
|
||||
if (rateLimit) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Rate limit exceeded.' },
|
||||
{ status: 429 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await generate();
|
||||
|
||||
return NextResponse.json({
|
||||
id: data.id,
|
||||
consumer: data.consumer
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,17 +1,8 @@
|
||||
import { generate } from '@purchases/store';
|
||||
import { purchasesRequestSchema } from '@purchases/types';
|
||||
import { rateLimiter } from '@utils/rateLimiter';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const rateLimit = await rateLimiter();
|
||||
if (rateLimit) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Rate limit exceeded.' },
|
||||
{ status: 429 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
@@ -23,9 +14,9 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
const { id, consumer } = result.data;
|
||||
const { consumer } = result.data;
|
||||
|
||||
const purchaseList = await generate(id, consumer, new Date());
|
||||
const purchaseList = await generate(consumer, new Date());
|
||||
|
||||
return NextResponse.json(purchaseList);
|
||||
} catch (error) {
|
||||
|
||||
@@ -8,7 +8,6 @@ import { LineChart, PersonStanding, Download, Sparkles } from 'lucide-react';
|
||||
import axios from 'axios';
|
||||
|
||||
export const Content = () => {
|
||||
const [consumerId, setConsumerId] = useState<number>();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [consumer, setConsumer] = useState<Consumer | null>(null);
|
||||
@@ -67,7 +66,6 @@ export const Content = () => {
|
||||
}
|
||||
);
|
||||
|
||||
setConsumerId(data.id);
|
||||
setConsumer(data.consumer);
|
||||
setEditedConsumer(JSON.stringify(data.consumer, null, 2));
|
||||
} catch (err) {
|
||||
@@ -108,7 +106,7 @@ export const Content = () => {
|
||||
|
||||
try {
|
||||
const jsonData = JSON.parse(editedConsumer);
|
||||
const requestData = { id: consumerId, consumer: jsonData };
|
||||
const requestData = { consumer: jsonData };
|
||||
|
||||
const validationResult = purchasesRequestSchema.safeParse(requestData);
|
||||
if (!validationResult.success) {
|
||||
|
||||
10
package.json
10
package.json
@@ -5,7 +5,7 @@
|
||||
"author": "riccardo@frompixels.com",
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "prisma generate && next build",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint && eslint . --fix",
|
||||
"format": "prettier --config .prettierrc '**/*.{ts,tsx,json,md}' --write",
|
||||
@@ -13,15 +13,10 @@
|
||||
"prepare": "husky install",
|
||||
"audit": "audit-ci",
|
||||
"vercel:link": "vercel link",
|
||||
"vercel:env": "vercel env pull .env",
|
||||
"prisma:migrate": "npx prisma migrate dev",
|
||||
"prisma:push": "npx prisma db push",
|
||||
"prisma:generate": "npx prisma generate",
|
||||
"prisma:reset": "npx prisma db push --force-reset"
|
||||
"vercel:env": "vercel env pull .env"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.32.1",
|
||||
"@prisma/client": "^6.0.1",
|
||||
"axios": "^1.7.9",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
@@ -52,7 +47,6 @@
|
||||
"lint-staged": "^15.1.0",
|
||||
"postcss": "^8.4.49",
|
||||
"prettier": "^3.1.0",
|
||||
"prisma": "^6.0.1",
|
||||
"tailwindcss": "^3.4.15",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
"typescript": "^5.3.0"
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "consumer" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"letters" TEXT NOT NULL,
|
||||
"year" INTEGER NOT NULL,
|
||||
"zipCode" TEXT NOT NULL,
|
||||
"persona" JSONB NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "consumer_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "purchases" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"value" JSONB NOT NULL,
|
||||
"consumerId" INTEGER NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "purchases_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "purchases" ADD CONSTRAINT "purchases_consumerId_fkey" FOREIGN KEY ("consumerId") REFERENCES "consumer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `persona` on the `consumer` table. All the data in the column will be lost.
|
||||
- You are about to drop the column `year` on the `consumer` table. All the data in the column will be lost.
|
||||
- Added the required column `birthday` to the `consumer` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE "consumer" DROP COLUMN "persona",
|
||||
DROP COLUMN "year",
|
||||
ADD COLUMN "birthday" TIMESTAMP(3) NOT NULL,
|
||||
ADD COLUMN "editedValue" JSONB,
|
||||
ADD COLUMN "value" JSONB;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "purchases" ALTER COLUMN "value" DROP NOT NULL;
|
||||
@@ -1,4 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "consumer" ALTER COLUMN "letters" DROP NOT NULL,
|
||||
ALTER COLUMN "zipCode" DROP NOT NULL,
|
||||
ALTER COLUMN "birthday" DROP NOT NULL;
|
||||
@@ -1,3 +0,0 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
@@ -1,9 +0,0 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
||||
|
||||
export const prisma = globalForPrisma.prisma || new PrismaClient();
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
|
||||
|
||||
export default prisma;
|
||||
@@ -1,34 +0,0 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
|
||||
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
|
||||
}
|
||||
|
||||
model Consumer {
|
||||
id Int @id @default(autoincrement())
|
||||
letters String?
|
||||
birthday DateTime?
|
||||
zipCode String?
|
||||
value Json?
|
||||
editedValue Json?
|
||||
purchaseLists PurchaseList[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@map("consumer")
|
||||
}
|
||||
|
||||
model PurchaseList {
|
||||
id Int @id @default(autoincrement())
|
||||
value Json?
|
||||
consumer Consumer @relation(fields: [consumerId], references: [id])
|
||||
consumerId Int
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@map("purchases")
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import { Tool } from './tool';
|
||||
import { BaseTool, makeRequest } from '@anthropic';
|
||||
import { generatePrompt } from './prompt';
|
||||
import { generateConsumerSeed } from '@utils/generateConsumerSeed';
|
||||
import prisma from '@prisma';
|
||||
|
||||
export async function generate() {
|
||||
const { letters, birthday, zipCode } = generateConsumerSeed();
|
||||
@@ -24,19 +23,7 @@ export async function generate() {
|
||||
|
||||
console.info('Generated consumer by Anthropic', validConsumer.data);
|
||||
|
||||
const newConsumer = await prisma.consumer.create({
|
||||
data: {
|
||||
letters,
|
||||
birthday: birthday.toDate(),
|
||||
zipCode,
|
||||
value: validConsumer.data
|
||||
}
|
||||
});
|
||||
|
||||
console.info(`Consumer with id ${newConsumer.id} stored in database.`);
|
||||
|
||||
return {
|
||||
id: newConsumer.id,
|
||||
consumer: validConsumer.data
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
@@ -3,13 +3,8 @@ import { Tool } from './tool';
|
||||
import { BaseTool, makeRequest } from '@anthropic';
|
||||
import { generatePrompt } from './prompt';
|
||||
import { Consumer } from '@consumer/types';
|
||||
import prisma from '@prisma';
|
||||
|
||||
export async function generate(
|
||||
id: number | undefined,
|
||||
editedConsumer: Consumer,
|
||||
date: Date
|
||||
) {
|
||||
export async function generate(editedConsumer: Consumer, date: Date) {
|
||||
const consumerPrompt = await generatePrompt(
|
||||
editedConsumer,
|
||||
parseInt(process.env.PURCHASE_REFLECTION_THRESHOLD ?? '50'),
|
||||
@@ -17,10 +12,6 @@ export async function generate(
|
||||
parseInt(process.env.NUMBER_OF_WEEKS ?? '4')
|
||||
);
|
||||
|
||||
console.info(
|
||||
`Generating new purchase list for consumer with id ${id ?? 'N/A'}`
|
||||
);
|
||||
|
||||
try {
|
||||
const result = (await makeRequest(
|
||||
consumerPrompt,
|
||||
@@ -39,31 +30,7 @@ export async function generate(
|
||||
);
|
||||
|
||||
console.info(
|
||||
`Generated ${totalPurchases} purchases for new purchase list for consumer wth id ${id}`
|
||||
);
|
||||
|
||||
const [consumer, purchaseList] = await prisma.$transaction(async tx => {
|
||||
const consumer = id
|
||||
? await tx.consumer.update({
|
||||
where: { id },
|
||||
data: { editedValue: editedConsumer }
|
||||
})
|
||||
: await tx.consumer.create({
|
||||
data: { editedValue: editedConsumer }
|
||||
});
|
||||
|
||||
const purchaseList = await tx.purchaseList.create({
|
||||
data: {
|
||||
consumerId: consumer.id,
|
||||
value: validPurchases.data
|
||||
}
|
||||
});
|
||||
|
||||
return [consumer, purchaseList];
|
||||
});
|
||||
|
||||
console.info(
|
||||
`Purchase list with id ${purchaseList.id} for consumer with id ${consumer.id} stored in database.`
|
||||
`Generated ${totalPurchases} purchases for new purchase list for consumer`
|
||||
);
|
||||
|
||||
return validPurchases.data;
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import prisma from '@prisma';
|
||||
|
||||
export async function rateLimiter() {
|
||||
if (!process.env.RATE_LIMIT) {
|
||||
throw Error('Rate limit missing.');
|
||||
}
|
||||
|
||||
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
|
||||
const count = await prisma.$transaction(async tx => {
|
||||
const consumersCount = await tx.consumer.count({
|
||||
where: {
|
||||
createdAt: {
|
||||
gt: yesterday
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const purchaseListsCount = await tx.purchaseList.count({
|
||||
where: {
|
||||
createdAt: {
|
||||
gt: yesterday
|
||||
}
|
||||
}
|
||||
});
|
||||
return consumersCount + purchaseListsCount;
|
||||
});
|
||||
|
||||
return count > parseInt(process.env.RATE_LIMIT);
|
||||
}
|
||||
52
yarn.lock
52
yarn.lock
@@ -375,47 +375,6 @@
|
||||
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
|
||||
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==
|
||||
|
||||
"@prisma/client@^6.0.1":
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-6.1.0.tgz#179d3b70586e7be522f6f1f0a82cca01396f719a"
|
||||
integrity sha512-AbQYc5+EJKm1Ydfq3KxwcGiy7wIbm4/QbjCKWWoNROtvy7d6a3gmAGkKjK0iUCzh+rHV8xDhD5Cge8ke/kiy5Q==
|
||||
|
||||
"@prisma/debug@6.1.0":
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@prisma/debug/-/debug-6.1.0.tgz#a27a1d144f72a3bc95061ecb0255e7554d9d59ec"
|
||||
integrity sha512-0himsvcM4DGBTtvXkd2Tggv6sl2JyUYLzEGXXleFY+7Kp6rZeSS3hiTW9mwtUlXrwYbJP6pwlVNB7jYElrjWUg==
|
||||
|
||||
"@prisma/engines-version@6.1.0-21.11f085a2012c0f4778414c8db2651556ee0ef959":
|
||||
version "6.1.0-21.11f085a2012c0f4778414c8db2651556ee0ef959"
|
||||
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-6.1.0-21.11f085a2012c0f4778414c8db2651556ee0ef959.tgz#0b21ebf57362ffe35d0760c39855f90bbfa0f2fd"
|
||||
integrity sha512-PdJqmYM2Fd8K0weOOtQThWylwjsDlTig+8Pcg47/jszMuLL9iLIaygC3cjWJLda69siRW4STlCTMSgOjZzvKPQ==
|
||||
|
||||
"@prisma/engines@6.1.0":
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-6.1.0.tgz#2195244a8ce33839a8131e4465624e21d1f8d042"
|
||||
integrity sha512-GnYJbCiep3Vyr1P/415ReYrgJUjP79fBNc1wCo7NP6Eia0CzL2Ot9vK7Infczv3oK7JLrCcawOSAxFxNFsAERQ==
|
||||
dependencies:
|
||||
"@prisma/debug" "6.1.0"
|
||||
"@prisma/engines-version" "6.1.0-21.11f085a2012c0f4778414c8db2651556ee0ef959"
|
||||
"@prisma/fetch-engine" "6.1.0"
|
||||
"@prisma/get-platform" "6.1.0"
|
||||
|
||||
"@prisma/fetch-engine@6.1.0":
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@prisma/fetch-engine/-/fetch-engine-6.1.0.tgz#2a5174787bf57c9b1d5d400bb923e0dc6a73a794"
|
||||
integrity sha512-asdFi7TvPlEZ8CzSZ/+Du5wZ27q6OJbRSXh+S8ISZguu+S9KtS/gP7NeXceZyb1Jv1SM1S5YfiCv+STDsG6rrg==
|
||||
dependencies:
|
||||
"@prisma/debug" "6.1.0"
|
||||
"@prisma/engines-version" "6.1.0-21.11f085a2012c0f4778414c8db2651556ee0ef959"
|
||||
"@prisma/get-platform" "6.1.0"
|
||||
|
||||
"@prisma/get-platform@6.1.0":
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@prisma/get-platform/-/get-platform-6.1.0.tgz#d4394a24ef91af6675a92382ed40e6e6e07eeb13"
|
||||
integrity sha512-ia8bNjboBoHkmKGGaWtqtlgQOhCi7+f85aOkPJKgNwWvYrT6l78KgojLekE8zMhVk0R9lWcifV0Pf8l3/15V0Q==
|
||||
dependencies:
|
||||
"@prisma/debug" "6.1.0"
|
||||
|
||||
"@rtsao/scc@^1.1.0":
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@rtsao/scc/-/scc-1.1.0.tgz#927dd2fae9bc3361403ac2c7a00c32ddce9ad7e8"
|
||||
@@ -1982,7 +1941,7 @@ fs.realpath@^1.0.0:
|
||||
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
|
||||
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==
|
||||
|
||||
fsevents@2.3.3, fsevents@~2.3.2:
|
||||
fsevents@~2.3.2:
|
||||
version "2.3.3"
|
||||
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
|
||||
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==
|
||||
@@ -3369,15 +3328,6 @@ prettier@^3.1.0:
|
||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.4.2.tgz#a5ce1fb522a588bf2b78ca44c6e6fe5aa5a2b13f"
|
||||
integrity sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==
|
||||
|
||||
prisma@^6.0.1:
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/prisma/-/prisma-6.1.0.tgz#738f657fdd5ab8e6775f385db81bf7e61c70fbaf"
|
||||
integrity sha512-aFI3Yi+ApUxkwCJJwyQSwpyzUX7YX3ihzuHNHOyv4GJg3X5tQsmRaJEnZ+ZyfHpMtnyahhmXVfbTZ+lS8ZtfKw==
|
||||
dependencies:
|
||||
"@prisma/engines" "6.1.0"
|
||||
optionalDependencies:
|
||||
fsevents "2.3.3"
|
||||
|
||||
prop-types@^15.8.1:
|
||||
version "15.8.1"
|
||||
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
|
||||
|
||||
Reference in New Issue
Block a user