fix: Types on overridden findByPk methods (#5908)

This commit is contained in:
Tom Moor
2023-10-01 15:02:56 -04:00
committed by GitHub
parent e2a6d828a9
commit 41a6f77998
6 changed files with 59 additions and 16 deletions

View File

@@ -1,8 +1,9 @@
/* eslint-disable lines-between-class-members */
import compact from "lodash/compact";
import isNil from "lodash/isNil";
import uniq from "lodash/uniq";
import randomstring from "randomstring";
import type { SaveOptions } from "sequelize";
import type { Identifier, NonNullFindOptions, SaveOptions } from "sequelize";
import {
Sequelize,
Transaction,
@@ -52,6 +53,11 @@ import Length from "./validators/Length";
export const DOCUMENT_VERSION = 2;
type AdditionalFindOptions = {
userId?: string;
includeState?: boolean;
};
@DefaultScope(() => ({
attributes: {
exclude: ["state"],
@@ -429,13 +435,30 @@ class Document extends ParanoidModel {
return this.scope(["defaultScope", collectionScope, viewScope]);
}
/**
* Overrides the standard findByPk behavior to allow also querying by urlId
*
* @param id uuid or urlId
* @param options FindOptions
* @returns A promise resolving to a collection instance or null
*/
static async findByPk(
id: string,
options: FindOptions<Document> & {
userId?: string;
includeState?: boolean;
} = {}
id: Identifier,
options?: NonNullFindOptions<Document> & AdditionalFindOptions
): Promise<Document>;
static async findByPk(
id: Identifier,
options?: FindOptions<Document> & AdditionalFindOptions
): Promise<Document | null>;
static async findByPk(
id: Identifier,
options: (NonNullFindOptions<Document> | FindOptions<Document>) &
AdditionalFindOptions = {}
): Promise<Document | null> {
if (typeof id !== "string") {
return null;
}
// allow default preloading of collection membership if `userId` is passed in find options
// almost every endpoint needs the collection membership to determine policy permissions.
const scope = this.scope([