Skip to main content

Examples of ts functions to be used in prisma datasource interactions

create.ts
import { GSContext, GSStatus, PlainObject } from "@godspeedsystems/core";
import { PrismaClient } from "@prisma/client";

module.exports = async (ctx: GSContext, args: PlainObject) => {
const { inputs: { data: { user, inputs, params, headers } }, logger, datasources } = ctx;
const userId= user.userId ;

const client: PrismaClient = datasources.schmaName.client;
try {
const getUser = await client.User.findUnique({
where: { id: userId } });
if(getUser)
{
return new GSStatus(true, 200, undefined, getUser, undefined );
}
else
{
return new GSStatus(true, 200, undefined, {}, undefined );
}
}
catch (error: any) {
const errorData = error.stack || error;
logger.info(errorData);
}
}
update.ts
import { GSContext, GSStatus, PlainObject } from "@godspeedsystems/core";
import { PrismaClient } from "@prisma/client";
module.exports = async (ctx: GSContext, args: PlainObject) => {
const { inputs: { data: { user, body } }, logger, datasources } = ctx;
try {
const client: PrismaClient = datasources.schemaName.client;
const updatedUser = await client.user.update({
where: { id: user.userId },
data: { ...body } });
const contactEmail = await client.contacts.update({
data: { email: updatedUser.email,
userId: user.userId } });

return new GSStatus(true, 200, undefined, updatedUser, undefined);
}
catch (error: any) {
const errorData = error.stack || error;
return new GSStatus(false, 400, undefined, errorData, undefined);
}
};
import { GSContext, GSStatus, PlainObject } from "@godspeedsystems/core";
import { PrismaClient } from "@prisma/client";

module.exports = async (ctx: GSContext, args: PlainObject) => {
const { inputs: { data: { user, inputs, params, headers } }, logger, datasources } = ctx;
const userId= user.userId ;

const client: PrismaClient = datasources.schemaName.client;
try {
const getUser = await client.User.findUnique({
where: { id: userId } });
if(getUser)
{
return new GSStatus(true, 200, undefined, getUser, undefined );
}
else
{
return new GSStatus(true, 200, undefined, {}, undefined );
}
}
catch (error: any) {
const errorData = error.stack || error;
logger.info(errorData);
}
}