feat: add tests for /api/document (#932)
This commit is contained in:
parent
5b4ad941d3
commit
020494f63b
6 changed files with 316 additions and 19 deletions
|
|
@ -1,7 +1,17 @@
|
|||
import 'server-only';
|
||||
|
||||
import { genSaltSync, hashSync } from 'bcrypt-ts';
|
||||
import { and, asc, desc, eq, gt, gte, inArray, lt, SQL } from 'drizzle-orm';
|
||||
import {
|
||||
and,
|
||||
asc,
|
||||
desc,
|
||||
eq,
|
||||
gt,
|
||||
gte,
|
||||
inArray,
|
||||
lt,
|
||||
type SQL,
|
||||
} from 'drizzle-orm';
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import postgres from 'postgres';
|
||||
|
||||
|
|
@ -15,9 +25,9 @@ import {
|
|||
message,
|
||||
vote,
|
||||
type DBMessage,
|
||||
Chat,
|
||||
type Chat,
|
||||
} from './schema';
|
||||
import { ArtifactKind } from '@/components/artifact';
|
||||
import type { ArtifactKind } from '@/components/artifact';
|
||||
|
||||
// Optionally, if not using email/pass login, you can
|
||||
// use the Drizzle adapter for Auth.js / NextAuth
|
||||
|
|
@ -241,14 +251,17 @@ export async function saveDocument({
|
|||
userId: string;
|
||||
}) {
|
||||
try {
|
||||
return await db.insert(document).values({
|
||||
id,
|
||||
title,
|
||||
kind,
|
||||
content,
|
||||
userId,
|
||||
createdAt: new Date(),
|
||||
});
|
||||
return await db
|
||||
.insert(document)
|
||||
.values({
|
||||
id,
|
||||
title,
|
||||
kind,
|
||||
content,
|
||||
userId,
|
||||
createdAt: new Date(),
|
||||
})
|
||||
.returning();
|
||||
} catch (error) {
|
||||
console.error('Failed to save document in database');
|
||||
throw error;
|
||||
|
|
@ -304,7 +317,8 @@ export async function deleteDocumentsByIdAfterTimestamp({
|
|||
|
||||
return await db
|
||||
.delete(document)
|
||||
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)));
|
||||
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)))
|
||||
.returning();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Failed to delete documents by id after timestamp from database',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue