Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Ref type and type guards #1936

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export * from './errors';
export * from './factories';
export * from './interfaces';
export * from './mongoose.module';
export * from './typeguards';
export * from './types';
export * from './utils';
64 changes: 64 additions & 0 deletions lib/typeguards/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Model, RefType, Types } from 'mongoose';
import { AllowedRefTypes, DocumentType, Ref } from '../types';

export function isDocument<T, S extends RefType>(
doc: Ref<T, S> | null | undefined,
): doc is DocumentType<T> {
return doc instanceof Model;
}

export function isDocumentArray<T, S extends RefType>(
docs: Types.Array<Ref<T, S>> | null | undefined,
): docs is Types.Array<DocumentType<NonNullable<T>>>;

export function isDocumentArray<T, S extends RefType>(
docs: Ref<T, S>[] | null | undefined,
): docs is DocumentType<NonNullable<T>>[];

export function isDocumentArray(docs: Ref<any, any>[] | null | undefined) {
return Array.isArray(docs) && docs.every((v) => isDocument(v));
}

export function isRefType<T, S extends RefType>(
doc: Ref<T, S> | null | undefined,
refType: AllowedRefTypes,
): doc is NonNullable<S> {
if (doc == null || isDocument(doc)) {
return false;
}

if (refType === Types.ObjectId) {
return doc instanceof Types.ObjectId;
}

if (refType === String) {
return typeof doc === 'string';
}

if (refType === Number) {
return typeof doc === 'number';
}

if (refType === Buffer || refType === Types.Buffer) {
return doc instanceof Buffer;
}

return false;
}

export function isRefTypeArray<T, S extends RefType>(
docs: Types.Array<Ref<T, S>> | null | undefined,
refType: AllowedRefTypes,
): docs is Types.Array<NonNullable<S>>;

export function isRefTypeArray<T, S extends RefType>(
docs: Ref<T, S>[] | null | undefined,
refType: AllowedRefTypes,
): docs is NonNullable<S>[];

export function isRefTypeArray(
docs: Ref<any, any>[] | null | undefined,
refType: AllowedRefTypes,
): unknown {
return Array.isArray(docs) && docs.every((v) => isRefType(v, refType));
}
22 changes: 22 additions & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Document, RefType, Require_id, Types } from 'mongoose';

export type AllowedRefTypes =
| typeof String
| typeof Number
| typeof Buffer
| typeof Types.ObjectId
| typeof Types.Buffer;

export type DocumentType<T, QueryHelpers = any> = Document<
unknown,
QueryHelpers,
T
> &
Require_id<T>;

export type Ref<
T,
RawId extends RefType = T extends { _id: RefType }
? T['_id']
: Types.ObjectId,
> = DocumentType<T> | RawId;
Loading