atlas_sdk icon indicating copy to clipboard operation
atlas_sdk copied to clipboard

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

Open Bestulo opened this issue 3 years ago • 1 comments

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 avatar Oct 26 '22 18:10 Bestulo

@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.

erfanium avatar Oct 27 '22 05:10 erfanium