13
src/database/database.test.ts
Normal file
13
src/database/database.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { createAddition, readAddition } from './database';
|
||||
|
||||
describe('Database', () => {
|
||||
it('creates a new record in the Addition table and reads it', async () => {
|
||||
const value = 13;
|
||||
|
||||
const createResult = await createAddition(value);
|
||||
|
||||
const readResult = await readAddition(createResult.id);
|
||||
expect(readResult).toBeDefined();
|
||||
expect(readResult.value).toBe(value);
|
||||
});
|
||||
});
|
||||
32
src/database/database.ts
Normal file
32
src/database/database.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
/**
|
||||
* Create a new record in the Addition table.
|
||||
* @param value - The value number to add as a new record.
|
||||
* @returns Whether the database operation succeeded.
|
||||
*/
|
||||
export async function createAddition(value: number) {
|
||||
return await prisma.addition.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
datetime: new Date(),
|
||||
value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a record from the Addition table.
|
||||
* @param id - The id of the record.
|
||||
* @returns The retrieved record, or an error.
|
||||
*/
|
||||
export async function readAddition(id: string) {
|
||||
return await prisma.addition.findUniqueOrThrow({
|
||||
where: {
|
||||
id
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user