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

fix collection type definition: _id field should be optional on inserts #18

Open
Bestulo opened this issue Oct 26, 2022 · 1 comment
Open
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Bestulo
Copy link

Bestulo commented Oct 26, 2022

I basically copied the example on the docs and am getting the error in the title.

code:

import {
  MongoClient,
  ObjectId,
} from "https://deno.land/x/[email protected]/mod.ts";
import { config } from "https://deno.land/x/[email protected]/mod.ts";

const client = new MongoClient({
  endpoint: `https://data.mongodb-api.com/app/${
    config().DB_APP_ID
  }/endpoint/data/v1`,
  dataSource: config().DB_DATA_SOURCE,
  auth: {
    apiKey: config().DB_API_KEY,
  },
});

interface Users {
  _id: ObjectId;
  name: string;
  email: string;
}

const db = client.database(config().DB_NAME);
const users = db.collection<Users>("users");

// error is here
users.insertOne({ // <---
  name: "John Doe",
  email: "[email protected]",
});

error:

Argument of type '{ name: string; email: string; }' is not assignable to parameter of type 'Users'.
  Property '_id' is missing in type '{ name: string; email: string; }' but required in type 'Users'.deno-ts(2345)

Update:

This was proposed by Github Copilot and seems to work:

users.insertOne({
  name: "John Doe",
  email: "[email protected]",
  _id: new ObjectId(),
});

Update:

Works:

image

@Bestulo Bestulo closed this as completed Oct 26, 2022
@erfanium erfanium reopened this Oct 27, 2022
@erfanium erfanium changed the title Property '_id' is missing in type Users fix collection type definition Oct 27, 2022
@erfanium erfanium added help wanted Extra attention is needed good first issue Good for newcomers labels Oct 27, 2022
@erfanium erfanium changed the title fix collection type definition fix collection type definition: _id field should be optional on inserts Oct 27, 2022
@erfanium
Copy link
Owner

erfanium commented Oct 27, 2022

@Bestulo Yeah, the type definition for collection methods are very basic and needs extra improvements. _id field should be optional on inserts params

Thanks for using this package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants