xwjs.
xwjs.
DTDrizzle Team
Created by xwjs. on 11/30/2023 in #help
FindFirst Config Optional `with`
Hi, I am trying to do something like an optional findFirst config here. i am expecting the resultType of readTemplater would be with relations of templaterResults. but the type is only templaters without relations.
type Input = NonNullable<
Parameters<(typeof db)['query']['templaters']['findFirst']>[0]
>;

export const readCustomTemplater = async (db: DB, input: Input) => {
try {
const res = await db.query.templaters.findFirst(input);
if (!res) {
throw newResourcesNotFoundError(
new Error('Templater not found'),
templaters,
);
}
return res;
} catch (err) {
throw new Error('An error occured')
}
};

export const readTemplater = async (db: DB, id: string) =>
await readCustomTemplater(db, {
with: {
templaterResults: true,
},
where: (field, { eq }) => eq(field.id, id),
});
type Input = NonNullable<
Parameters<(typeof db)['query']['templaters']['findFirst']>[0]
>;

export const readCustomTemplater = async (db: DB, input: Input) => {
try {
const res = await db.query.templaters.findFirst(input);
if (!res) {
throw newResourcesNotFoundError(
new Error('Templater not found'),
templaters,
);
}
return res;
} catch (err) {
throw new Error('An error occured')
}
};

export const readTemplater = async (db: DB, id: string) =>
await readCustomTemplater(db, {
with: {
templaterResults: true,
},
where: (field, { eq }) => eq(field.id, id),
});
However if i change the readCustomTemplater parameter to specifically receive with input, the result type of readTemplater shows the correct type, which is templaters with relations of templaterResults
type WithInput = NonNullable<
Parameters<(typeof db)['query']['templaters']['findFirst']>[0]
>['with'];

export const readCustomTemplater = async (db: DB, input: WithInput) => {
try {
const res = await db.query.templaters.findFirst({
with: input,
});
if (!res) {
throw newResourcesNotFoundError(
new Error('Templater not found'),
templaters,
);
}
return res;
} catch (err) {
throw new Error('An error occured');
}
};

export const readTemplater = async (db: DB, id: string) =>
await readCustomTemplater(db, {
templaterResults: true,
});
type WithInput = NonNullable<
Parameters<(typeof db)['query']['templaters']['findFirst']>[0]
>['with'];

export const readCustomTemplater = async (db: DB, input: WithInput) => {
try {
const res = await db.query.templaters.findFirst({
with: input,
});
if (!res) {
throw newResourcesNotFoundError(
new Error('Templater not found'),
templaters,
);
}
return res;
} catch (err) {
throw new Error('An error occured');
}
};

export const readTemplater = async (db: DB, id: string) =>
await readCustomTemplater(db, {
templaterResults: true,
});
but my goal here is to fully let the caller have the control of findFirst config
1 replies