Skip to content

Instantly share code, notes, and snippets.

@nodkz
Last active June 16, 2018 20:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodkz/9575f7d658f812c51a04609310c3cdf9 to your computer and use it in GitHub Desktop.
Save nodkz/9575f7d658f812c51a04609310c3cdf9 to your computer and use it in GitHub Desktop.
Covering mongoose models with flowtype
/* @flow */
/* eslint-disable */
import mongoose from 'mongoose';
export type MongoId = typeof mongoose.Types.ObjectId | {
toString(): string,
};
export type MongoOrScalarId = MongoId | string | number;
declare class MongooseDocumentFieldsT {
id: string | number;
_id: MongoOrScalarId;
__v?: number;
}
// D - Document instance
// S - Shape of document props
declare class MongooseDocumentMethodsT<D,S> extends MongooseDocumentFieldsT {
constructor(data?: S): D,
save(): Promise<D>;
}
type UpdateResult = {
nMatched: number,
nUpserted: number,
nModified: number,
ok?: boolean,
};
type MongooseProjection = Object | string;
declare class MongooseQuery<Result> extends Promise<Result> {
exec(): Promise<Result>,
where(criteria: Object): MongooseQuery<Result>,
sort(fields: Object | string): MongooseQuery<Result>,
limit(n: number): MongooseQuery<Result>,
select(fields: MongooseProjection): MongooseQuery<Result>,
};
// D - Document instance
// S - Shape of document
declare class MongooseModelT<D,S> {
constructor(data?: S): D,
find(criteria: Object, projection?: MongooseProjection, options?: Object): MongooseQuery<Array<D>>;
findOne(criteria: Object, projection?: MongooseProjection): MongooseQuery<?D>;
findById(id: MongoOrScalarId, projection?: MongooseProjection): MongooseQuery<?D>;
count(criteria: Object): Promise<number>;
remove(criteria: Object): Promise<mixed>;
update(criteria: Object, update: Object, options?: Object): (Promise<UpdateResult> & { exec(): Promise<UpdateResult> });
findOneAndRemove(criteria: ?Object, options?: Object): MongooseQuery<?D>,
findByIdAndRemove(id: MongoOrScalarId, options?: Object): MongooseQuery<?D>,
modelName: string,
schema: any,
create(doc: S | S[]): Promise<D>,
where(criteria: Object): MongooseQuery<D>,
}
/// DO NOT REMOVE!!!
/// FOLLOWING STUBS ARE REQUIRED FOR PROPER IMPORT CHECKS
// $FlowFixMe
export class MongooseDocumentFieldsT {};
// $FlowFixMe
export class MongooseDocumentMethodsT {};
// $FlowFixMe
export class MongooseModelT {};
/* @flow */
/* eslint-disable */
import {
MongooseDocumentMethodsT,
MongooseModelT,
MongooseDocumentFieldsT,
} from './mongoose.flowtype';
import type { $Request } from 'express';
import { AvatarUrlSchema } from '../customTypes/avatarUrl'
import type { AvatarUrlShape } from '../customTypes/avatarUrl';
import type { CabinetDocument } from '../cabinet';
export type UserDocument = DocumentT;
declare module.exports: {
UserShape: Class<ShapeT>,
User: Class<DocumentT> & StaticMethodsT<DocumentT, ShapeT>,
};
declare class FieldsT extends MongooseDocumentFieldsT {
// required properties
email: string,
provider: string,
providerId: string,
token: string,
// properties
name?: string,
avatarUrl: typeof AvatarUrlSchema | AvatarUrlShape,
oneTimeTokenExp?: string,
meta?: any,
lastIp: String,
lastLoginAt: Date,
// virtuals
password?: string,
tokenHash?: string, // only get
}
declare class StaticMethodsT<D,S> extends MongooseModelT<D,S> {
findByProvider(provider: string, providerId: string): Promise<D>,
isValidEmail(email: string): boolean,
findByEmailOrCreate(opts: {| email: string, password: string, provider: string, providerId: string |}): Promise<D>,
}
declare class MethodsT<D,S> extends MongooseDocumentMethodsT<D,S> {
encryptPassword(password: string): string,
checkPassword(password: string): boolean,
genPassword(len?: number): string,
oneTimeLoginGenerate(expiredAfter?: number): string,
oneTimeTokenGenerate(expiredAfter?: number): string,
oneTimeTokenCheck(oneTimeToken: string): boolean,
oneTimeTokenRevoke(): void,
touchLastLoginAt(req: $Request): D,
getCabinets(): Promise<Array<CabinetDocument>>,
}
/// DO NOT REMOVE!!!
/// FOLLOWING DECLARATIONS ARE REQUIRED FOR PROPER TYPE BINDINGS
declare class MongooseDocument<D,S> extends MethodsT<D,S> mixins FieldsT {};
declare type ShapeT = $Shape<FieldsT>;
declare type DocumentT = MongooseDocument<DocumentT, ShapeT>;
@nodkz
Copy link
Author

nodkz commented Aug 29, 2017

Founded better solution with schema.loadClass():
https://twitter.com/nodkz/status/902431500389539841

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment