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 typings broke the example #7

Open
rpenido opened this issue Dec 29, 2022 · 3 comments
Open

Mongoose typings broke the example #7

rpenido opened this issue Dec 29, 2022 · 3 comments
Labels

Comments

@rpenido
Copy link

rpenido commented Dec 29, 2022

Running the example gives the following type error:

Property 'id' does not exist on type 'LeanDocument<{ name?: string; } & { _id: ObjectId; }>'.

I think that it is related to this: Automattic/mongoose#11761

Don't know if this can be solved in the plugin level.

@vkarpov15
Copy link
Member

@rpenido can you please provide a code sample that demonstrates the issue you're seeing?

@rpenido
Copy link
Author

rpenido commented Dec 31, 2022

The code in README of this repo, for example:

mongoose: 6.8.2
mongoose-lean-id: 0.3.0

const schema = new mongoose.Schema({
      name: String
    });

    schema.plugin(mongooseLeanId);

    const Model = mongoose.model('Test', schema);

    return Model.create({ name: 'test' }).
      then(() => Promise.all([
        Model.find().lean(),
        Model.findOne().lean(),
        Model.findOneAndUpdate({}, { name: 'test' }).lean()
      ])).
      then(results => {
        const [findRes, findOneRes, findOneAndUpdateRes] = results;
        assert.equal(findRes[0].id, findRes[0]._id.toHexString());
        assert.equal(findOneRes.id, findOneRes._id.toHexString());
        assert.equal(findOneAndUpdateRes.id,
          findOneAndUpdateRes._id.toHexString());
      });

@IslandRhythms
Copy link
Contributor

It will run correctly but the typing error is there.

import * as mongoose from 'mongoose';
import * as assert from 'assert';
const mongooseLeanId = require('mongoose-lean-id');

const testSchema = new mongoose.Schema({
  name: String
});

testSchema.plugin(mongooseLeanId);

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  await Test.create({ name: 'test' });
  const findRes = await Test.find().lean();
  const findOneRes = await Test.findOne().lean();
  const findOneAndUpdateRes = await Test.findOneAndUpdate({}, { name: 'test' }).lean();
  assert.equal(findRes[0].id, findRes[0]._id.toHexString());
  assert.equal(findOneRes.id, findOneRes._id.toHexString());
  assert.equal(findOneAndUpdateRes.id,
  findOneAndUpdateRes._id.toHexString());
  console.log('done');
}

run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants