erfanium / erfanium/atlas_sdk

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

Open
#18 1 comment 0 reactions 0 assignees View on GitHub
good first issue help wanted
Dominant language
TypeScript
Stars
41
Forks
5
PR merge metrics
No merged PRs in 30d

Description

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

code:

```ts
import {
MongoClient,
ObjectId,
} from "https://deno.land/x/atlas_sdk@v1.0.3/mod.ts";
import { config } from "https://deno.land/x/dotenv@v3.2.0/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");

// error is here
users.insertOne({ // <---
name: "John Doe",
email: "johndoe@gmail.com",
});
```

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:

```ts
users.insertOne({
name: "John Doe",
email: "johndoe@gmail.com",
_id: new ObjectId(),
});
```

### Update:
Works:

![image](https://user-images.githubusercontent.com/23301025/198102730-3eb674bb-8cc9-4774-802e-dd737f2d4d0b.png)

Contributor guide

No contributing guide indexed for this repository

Research direction

Trace the collection generic and insertOne type definition used by the TypeScript SDK, starting from the failing call shown in the issue. Make the _id field optional for inserts while preserving generated or supplied IDs, then verify that the documented example type-checks without requiring _id.

Written by the indexing model from the issue text.

Assessment

Tech stack
deno, mongodb, typescript
Domain
api, databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.