Skip to content

Commit

Permalink
chore(index.d.ts): add support for lean docs
Browse files Browse the repository at this point in the history
Re: #8108
  • Loading branch information
vkarpov15 committed Nov 13, 2020
1 parent 9b8c39c commit 920148e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
10 changes: 6 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,10 @@ declare module "mongoose" {
set(value: any): this;

/** The return value of this method is used in calls to JSON.stringify(doc). */
toJSON(options?: ToObjectOptions): any;
toJSON(options?: ToObjectOptions): LeanDocument<this>;

/** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
toObject(options?: ToObjectOptions): any;
toObject(options?: ToObjectOptions): LeanDocument<this>;

/** Clears the modified state on the specified path. */
unmarkModified(path: string);
Expand Down Expand Up @@ -1480,7 +1480,7 @@ declare module "mongoose" {
j(val: boolean | null): this;

/** Sets the lean option. */
lean(val?: boolean | any): this;
lean(val?: boolean | any): Query<LeanDocument<DocType>, DocType>;

/** Specifies the maximum number of documents the query will return. */
limit(val: number): this;
Expand Down Expand Up @@ -1702,7 +1702,9 @@ declare module "mongoose" {

export type UpdateQuery<T> = mongodb.UpdateQuery<T> & mongodb.MatchKeysAndValues<T>;

export type DocumentDefinition<T> = Omit<T, Exclude<keyof Document, '_id'>>
export type DocumentDefinition<T> = Omit<T, Exclude<keyof Document, '_id'>>;

export type LeanDocument<T> = Omit<T, Exclude<keyof Document, '_id'>>;

class QueryCursor<DocType extends Document> extends stream.Readable {
/**
Expand Down
23 changes: 23 additions & 0 deletions test/typescript/leanDocuments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Schema, model, Document, Types } from 'mongoose';

const schema: Schema = new Schema({ name: { type: 'String' } });

interface ITest extends Document {
_id?: Types.ObjectId,
name?: string;
}

const Test = model<ITest>('Test', schema);

void async function main() {
const doc: ITest = await Test.findOne();

const pojo = doc.toObject();
await pojo.save();

const _doc = await Test.findOne().lean();
await _doc.save();

const hydrated = Test.hydrate(_doc);
await hydrated.save();
}();
10 changes: 10 additions & 0 deletions test/typescript/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ describe('typescript syntax', function() {
}
assert.equal(errors.length, 0);
});

it('lean documents', function() {
const errors = runTest('leanDocuments.ts');
if (process.env.D && errors.length) {
console.log(errors);
}
assert.equal(errors.length, 2);
assert.ok(errors[0].messageText.includes('Property \'save\' does not exist'), errors[0].messageText);
assert.ok(errors[1].messageText.includes('Property \'save\' does not exist'), errors[0].messageText);
});
});

function runTest(file) {
Expand Down

0 comments on commit 920148e

Please sign in to comment.