feat: ditch triplit, move to postgresql + drizzle orm
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { models } from "~~/drizzle/schema";
|
||||
import { db } from "~~/server/lib/db";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await protectRoute(event);
|
||||
const userId = event.context.user!.id;
|
||||
|
||||
const modelId = getRouterParam(event, 'modelId')!;
|
||||
|
||||
const res = await db.delete(models).where(
|
||||
and(
|
||||
eq(models.id, modelId),
|
||||
eq(models.userId, userId),
|
||||
)
|
||||
);
|
||||
|
||||
if (res.rowCount === 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid model',
|
||||
});
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { models } from "~~/drizzle/schema";
|
||||
import { db } from "~~/server/lib/db";
|
||||
import * as z from 'zod';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await protectRoute(event);
|
||||
const userId = event.context.user!.id;
|
||||
|
||||
const result = await readValidatedBody(event, (body) =>
|
||||
z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
cost: z.object({
|
||||
prompt: z.string().optional(),
|
||||
completion: z.string().optional(),
|
||||
request: z.string().optional(),
|
||||
image: z.string().optional(),
|
||||
imageTokens: z.string().optional(),
|
||||
imageOutput: z.string().optional(),
|
||||
audio: z.string().optional(),
|
||||
audioOutput: z.string().optional(),
|
||||
inputAudioCache: z.string().optional(),
|
||||
webSearch: z.string().optional(),
|
||||
internalReasoning: z.string().optional(),
|
||||
inputCacheRead: z.string().optional(),
|
||||
inputCacheWrite: z.string().optional(),
|
||||
discount: z.string().optional(),
|
||||
}).optional(),
|
||||
inputModalities: z.array(z.string()).optional(),
|
||||
outputModalities: z.array(z.string()).optional(),
|
||||
capabilities: z.array(z.string()).optional(),
|
||||
contextWindow: z.number().optional(),
|
||||
supportedParameters: z.array(z.string()).optional(),
|
||||
isCustom: z.boolean().optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
releasedAt: z.string().optional(),
|
||||
})
|
||||
.safeParse(body),
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: result.error.issues[0]!.message,
|
||||
});
|
||||
}
|
||||
|
||||
const modelId = getRouterParam(event, 'modelId')!;
|
||||
|
||||
const { name, cost, inputModalities, outputModalities, capabilities, contextWindow, supportedParameters, isCustom, enabled, releasedAt } = result.data;
|
||||
|
||||
const existing = await db.query.models.findFirst({
|
||||
where: {
|
||||
id: modelId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid model',
|
||||
});
|
||||
}
|
||||
|
||||
const res = await db.update(models)
|
||||
.set({
|
||||
name,
|
||||
cost,
|
||||
inputModalities,
|
||||
outputModalities,
|
||||
capabilities,
|
||||
contextWindow,
|
||||
supportedParameters,
|
||||
isCustom,
|
||||
enabled,
|
||||
releasedAt: releasedAt ? new Date(releasedAt) : existing.releasedAt ?? null,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(models.id, modelId),
|
||||
eq(models.userId, userId),
|
||||
)
|
||||
);
|
||||
|
||||
if (res.rowCount === 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid model',
|
||||
});
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
import * as z from 'zod';
|
||||
import { models } from '~~/drizzle/schema';
|
||||
import { db } from '~~/server/lib/db';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await protectRoute(event);
|
||||
|
||||
const result = await readValidatedBody(event, (body) =>
|
||||
z.object({
|
||||
id: z.string(),
|
||||
providerId: z.string(),
|
||||
externalId: z.string(),
|
||||
name: z.string(),
|
||||
cost: z.object({
|
||||
prompt: z.string().optional(),
|
||||
completion: z.string().optional(),
|
||||
request: z.string().optional(),
|
||||
image: z.string().optional(),
|
||||
imageTokens: z.string().optional(),
|
||||
imageOutput: z.string().optional(),
|
||||
audio: z.string().optional(),
|
||||
audioOutput: z.string().optional(),
|
||||
inputAudioCache: z.string().optional(),
|
||||
webSearch: z.string().optional(),
|
||||
internalReasoning: z.string().optional(),
|
||||
inputCacheRead: z.string().optional(),
|
||||
inputCacheWrite: z.string().optional(),
|
||||
discount: z.string().optional(),
|
||||
}).optional(),
|
||||
inputModalities: z.array(z.string()).optional(),
|
||||
outputModalities: z.array(z.string()).optional(),
|
||||
capabilities: z.array(z.string()).optional(),
|
||||
contextWindow: z.number().nullable().optional(),
|
||||
supportedParameters: z.array(z.string()).optional(),
|
||||
isCustom: z.boolean().optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
releasedAt: z.date().nullable().optional(),
|
||||
})
|
||||
.safeParse(body),
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
...result.error
|
||||
});
|
||||
}
|
||||
|
||||
const provider = await db.query.providers.findFirst({
|
||||
where: {
|
||||
id: result.data.providerId,
|
||||
userId: event.context.user!.id,
|
||||
},
|
||||
with: {
|
||||
models: true,
|
||||
}
|
||||
});
|
||||
|
||||
if (!provider) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: 'Provider not found',
|
||||
});
|
||||
}
|
||||
|
||||
const model = await db.insert(models).values({
|
||||
id: result.data.id,
|
||||
userId: event.context.user!.id,
|
||||
providerId: result.data.providerId,
|
||||
externalId: result.data.externalId,
|
||||
name: result.data.name,
|
||||
cost: result.data.cost,
|
||||
inputModalities: result.data.inputModalities,
|
||||
outputModalities: result.data.outputModalities,
|
||||
capabilities: result.data.capabilities,
|
||||
contextWindow: result.data.contextWindow,
|
||||
supportedParameters: result.data.supportedParameters,
|
||||
isCustom: result.data.isCustom,
|
||||
enabled: result.data.enabled,
|
||||
releasedAt: result.data.releasedAt,
|
||||
}).onConflictDoNothing().returning();
|
||||
|
||||
return {
|
||||
model,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user