When fetching data from an API, my approach looks like this:
async function getApiData() {
const promises = [];
for (let i = 0; i < PAGE_COUNT; i++) {
const apiData = fetch(...);
}
const apiData = await Promise.all(promises);
return apiData;
}
The result of this function is a promise that resolves to an array containing objects. Each object represents data retrieved from the API and follows a specific pattern, for example:
const exampleData = { name: 'john', lastName: 'doe' };
To define a type for this in Typescript, I can specify it within my getApiData
function.
In addition, I am using Prisma with a schema structured as follows:
model Person {
id Int @id @default(autoincrement())
name String
lastName String
}
While I can import typings specific to Prisma such as Person
, there are variations in typing between this and what I use inside getApiData
. This inconsistency generates an error when attempting to import and apply the Prisma typing:
Property 'id' is missing in type but required in type `{name, lastName} but required in type 'Repository'
This situation prompts two main questions:
- How can I utilize the Prisma typing for shaping my API data? Creating a separate interface solely for accommodating one field difference feels cumbersome.
- From a typescript perspective, how can I insert any form of data into the Prisma database? Given that the
id
field is autogenerated upon insertion, does Prisma necessitate a specific typing for data insertion or can a more general approach be employed?