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

Mongoose populate #2928

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -305,7 +305,7 @@ declare class Mongoose$Document {
markModified(path: string): void;
modifiedPaths(): string[];

populate(path?: string | Object, cb?: (err: Error, doc: this) => void): void;
populate(path?: string | Object, cb?: (err: Error, doc: this) => void): this;
execPopulate(): Promise<this>;
populated(path: string): ?MongoId;
toJSON(options?: ToObjectOpts<this>): Object;
Expand Down
20 changes: 16 additions & 4 deletions definitions/npm/mongoose_v4.x.x/flow_v0.50.x-/test_mongoose-v4.js
Expand Up @@ -2,7 +2,7 @@

import mongoose, { Schema } from "mongoose";

export const AdminSchema = new Schema(
export const AdminSchema: Mongoose$Schema<AdminDoc> = new Schema(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in this case you want to use type parameters like export const AdminSchema = new Schema<Mongoose$Schema<AdminDoc>>(... ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flow added support for these few versions back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it like new Schema<AdminDoc>(...) but I get a Missing type annotation for 'Mongoose$Schema' again. Did not figure out why...

{
_id: String,
email: {
Expand Down Expand Up @@ -112,25 +112,37 @@ const a2 = new Admin({ email: 123, token: "www" });
Admin.aggregate([ { $project : { email : 1 } } ]).allowDiskUse(true).exec()

//
export const UserSchema = new Schema(
export const UserSchema: Mongoose$Schema<Mongoose$Document> = new Schema(
{
email: {
type: String,
match: /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/,
set: (v: string) => v.toLowerCase().trim(),
required: true
},
name: String
name: String,
admin: {
type: mongoose.Schema.Types.ObjectId,
ref: "Admin"
}
},
{
timestamps: {
createdAt: "created_at",
updatedAt: "updated_at"
},
collection: "admin"
collection: "user"
}
);

const User = mongoose.model("User", UserSchema);

User.findOne({}).exec().then( user => {
if (user) {
user.populate("admin").execPopulate();
}
})

mongoose.disconnect(err => console.log("err"));

mongoose.disconnect().then(data => console.log(data));
Expand Up @@ -351,7 +351,7 @@ declare class Mongoose$Document {
markModified(path: string): void;
modifiedPaths(): string[];

populate(path?: string | Object, cb?: (err: Error, doc: this) => void): void;
populate(path?: string | Object, cb?: (err: Error, doc: this) => void): this;
execPopulate(): Promise<this>;
populated(path: string): ?MongoId;
toJSON(options?: ToObjectOpts<this>): Object;
Expand Down
26 changes: 16 additions & 10 deletions definitions/npm/mongoose_v5.x.x/flow_v0.50.x-/test_mongoose-v5.js
Expand Up @@ -2,13 +2,7 @@

import mongoose, { Schema } from "mongoose";

mongoose.connect('mongodb://host:port/dbname', error => {
console.log(error)
})

mongoose.connect('mongodb://host:port/dbname').then(data => (data: Mongoose$Connection))

export const AdminSchema = new Schema(
export const AdminSchema: Mongoose$Schema<AdminDoc> = new Schema(
{
_id: String,
email: {
Expand Down Expand Up @@ -118,25 +112,37 @@ const a2 = new Admin({ email: 123, token: "www" });
Admin.aggregate([ { $project : { email : 1 } } ]).allowDiskUse(true).exec()

//
export const UserSchema = new Schema(
export const UserSchema: Mongoose$Schema<Mongoose$Document> = new Schema(
{
email: {
type: String,
match: /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/,
set: (v: string) => v.toLowerCase().trim(),
required: true
},
name: String
name: String,
admin: {
type: mongoose.Schema.Types.ObjectId,
ref: "Admin"
}
},
{
timestamps: {
createdAt: "created_at",
updatedAt: "updated_at"
},
collection: "admin"
collection: "user"
}
);

const User = mongoose.model("User", UserSchema);

User.findOne({}).exec().then( user => {
if (user) {
user.populate("admin").execPopulate();
}
})

mongoose.disconnect(err => console.log("err"));

mongoose.disconnect().then(data => console.log(data));