generated from alphane/template
Adding a ton of stuff
This commit is contained in:
@@ -45,25 +45,25 @@ export abstract class BaseModel<
|
||||
// identifier: unknown,
|
||||
// options?: FindByPkOptions<M>,
|
||||
// ): Promise<M | null>;
|
||||
public static async findByIdentifierOrPk<M extends BaseModel>(
|
||||
public static async findBySlugOrPk<M extends BaseModel>(
|
||||
this: ModelStatic<M>,
|
||||
identifierOrPk: string | number,
|
||||
slugOrPk: unknown,
|
||||
options?: Omit<FindOptions<Attributes<M>>, "where">
|
||||
): Promise<M | null> {
|
||||
if (typeof identifierOrPk === "number" || !isNaN(Number(identifierOrPk))) {
|
||||
const primaryKey = identifierOrPk
|
||||
if (typeof slugOrPk === "number" || !isNaN(Number(slugOrPk))) {
|
||||
const primaryKey = slugOrPk
|
||||
return this.findByPk(primaryKey, options)
|
||||
}
|
||||
|
||||
const identifier = identifierOrPk
|
||||
if (!("identifier" in this.getAttributes())) {
|
||||
throw new Error(`${this.name} does not have a 'identifier' attribute.`)
|
||||
const slug = slugOrPk
|
||||
if (!("slug" in this.getAttributes())) {
|
||||
throw new Error(`${this.name} does not have a 'slug' attribute.`)
|
||||
}
|
||||
|
||||
return this.findOne({
|
||||
...options,
|
||||
// @ts-expect-error - We know that the model has a slug attribute, and are ignoring the TS error
|
||||
where: { identifier },
|
||||
where: { slug },
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user