Skip to content

Commit

Permalink
add typescript types, begin typescript test
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Feb 14, 2024
1 parent 7c696a3 commit 648239d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,25 @@ async function gh13999() {
}
}
}

function gh4727() {
const userSchema = new mongoose.Schema({
name: String
});
const companySchema = new mongoose.Schema({
name: String,
users: [{ ref: 'User', type: mongoose.Schema.Types.ObjectId }]
});

mongoose.model('UserTestHydrate', userSchema);
const Company = mongoose.model('CompanyTestHyrdrate', companySchema);

const users = [{ _id: new mongoose.Types.ObjectId(), name: 'Val' }];
const company = { _id: new mongoose.Types.ObjectId(), name: 'Booster', users: [users[0]] };

expectType<ReturnType<(typeof Company)['hydrate']> | null>(
Company.hydrate(company, null, { hydratedPopulatedDocs: true })
);


}
7 changes: 6 additions & 1 deletion types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ declare module 'mongoose' {
skipValidation?: boolean;
}

interface HydrateOptions {
setters?: boolean;
hydratedPopulatedDocs?: boolean;
}

interface InsertManyOptions extends
PopulateOption,
SessionOption {
Expand Down Expand Up @@ -371,7 +376,7 @@ declare module 'mongoose' {
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
* The document returned has no paths marked as modified initially.
*/
hydrate(obj: any, projection?: AnyObject, options?: { setters?: boolean }): THydratedDocumentType;
hydrate(obj: any, projection?: AnyObject, options?: HydrateOptions): THydratedDocumentType;

/**
* This function is responsible for building [indexes](https://www.mongodb.com/docs/manual/indexes/),
Expand Down

0 comments on commit 648239d

Please sign in to comment.