TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy

Overview

Atlas SDK

atlas_sdk is a TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy

Discord server

Links

Import

Replace LATEST_VERSION with current latest version

import {
  Bson,
  MongoClient,
} from "https://deno.land/x/atlas_sdk@LATEST_VERSION/mod.ts";

Connect

const client = new MongoClient({
  appId: "YOUR_APP_ID", // e.g. "data-omosm"
  dataSource: "YOUR_CLUSTER_NAME", // e.g. "Cluster0"
  apiKey: "YOUR_API_KEY",
});

Access Collection

// Defining schema interface
interface UserSchema {
  _id: ObjectId | string;
  username: string;
  password: string;
}

const db = client.database("test");
const users = db.collection<UserSchema>("users");

Insert

const insertId = await users.insertOne({
  username: "user1",
  password: "pass1",
});

const insertIds = await users.insertMany([
  {
    username: "user1",
    password: "pass1",
  },
  {
    username: "user2",
    password: "pass2",
  },
]);

Find

const user1 = await users.findOne({ _id: insertId });

const all_users = await users.find({ username: { $ne: null } });

// find by ObjectId
const user1_id = await users.findOne({
  _id: new ObjectId("SOME OBJECTID STRING"),
});

Count

const count = await users.countDocuments({ username: { $ne: null } });

const estimatedCount = await users.estimatedDocumentCount({
  username: { $ne: null },
});

Aggregation

const docs = await users.aggregate([
  { $match: { username: "many" } },
  { $group: { _id: "$username", total: { $sum: 1 } } },
]);

Update

const { matchedCount, modifiedCount, upsertedId } = await users.updateOne(
  { username: { $ne: null } },
  { $set: { username: "USERNAME" } },
);

const { matchedCount, modifiedCount, upsertedId } = await users.updateMany(
  { username: { $ne: null } },
  { $set: { username: "USERNAME" } },
);

Replace

const { matchedCount, modifiedCount, upsertedId } = await users.replaceOne(
  { username: "a" },
  {
    username: "user1",
    password: "pass1",
  }, // new document
);

Delete

const deleteCount = await users.deleteOne({ _id: insertId });

const deleteCount2 = await users.deleteMany({ username: "test" });
You might also like...

A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

Wren Wren is a small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity. convenient aliases for HTTP responses au

Dec 12, 2022

⚡⚡NextJS Full NFT Marketplace Application Running with Alchemy SDK. Deploy In Minutes with Smart Contracts Attached

⚡⚡NextJS Full NFT Marketplace Application Running with Alchemy SDK. Deploy In Minutes with Smart Contracts Attached

+ Alchemy SDK Easy Polygon NFT Marketplace NextJS App Be sure to watch my Youtube video so you can learn and follow along! ** THE FILES ATTACHED TO TH

Nov 16, 2022

The iofod SDK provides developers with the ability to interact with the main iofod interface within the Web worker, enabling rapid development of iofod extensions through the SDK.

iofod-sdk English | 简体中文 The iofod SDK provides developers with the ability to interact with the main iofod interface within the Web worker, enabling

Oct 17, 2022

Movehat is a TypeScript SDK for Move on Sui built on top of Sui's TypeScript SDK and our fork of Ian Macalinao's `move-ts`.

Movehat Movehat is a TypeScript SDK for Move on Sui built on top of Sui's TypeScript SDK and our fork of Ian Macalinao's move-ts. Movehat aspires to b

Sep 30, 2022

End-to-end typesafe APIs with tRPC.io in SvelteKit applications

✨ tRPC-SvelteKit End-to-end typesafe APIs with tRPC.io in SvelteKit applications. No code generation, run-time bloat, or build pipeline. ❤️ 🇺🇦 See b

Dec 29, 2022

The /r/place Atlas is a project aiming to catalog all the artworks created during Reddit's 2022 /r/place event.

The 2022 Place Atlas The /r/place Atlas is a project aiming to catalog all the artworks created during Reddit's 2022 /r/place event. This project was

Dec 28, 2022

typesafe nodejs client for transit.land

typesafe nodejs client for transit.land

TransitLand Graphql Client About Since the Transitland released its new and shiny GraphQL API, it is possible to query the API using GraphQL. The clie

Jan 9, 2023

A technology stack solution using the AWS Serverless architecture.Atlas stack for building applications focused on generating value.

Atlas A technology stack solution using the AWS Serverless architecture.Atlas stack for building applications focused on generating value. Description

Dec 15, 2022

Quick T3 Stack with SvelteKit for rapid deployment of highly performant typesafe web apps.

Quick T3 Stack with SvelteKit for rapid deployment of highly performant typesafe web apps.

create-t3svelte-app Just Build npx create-t3svelte-app Outline Get Building npm yarn More Info 🛠 Early Version Note Prisma Requirements Available Tem

Dec 26, 2022
Comments
  • Add export statements for classes Database and Collection for usage as types

    Add export statements for classes Database and Collection for usage as types

    In my own project I created the following class to have every database-related functionality in one place:

    import { MongoClient, ObjectId } from "$atlas_sdk/mod.ts";
    
    export class MongoDBDatabase {
      client: MongoClient;
      db;
      users;
    
      constructor() {
        this.client = new MongoClient({ ...  });
        this.db = this.client.database("db");
        this.users = this.db.collection<UserSchema>("users");
      }
    
      async insertUser(email: string, username: string) {
        const result = await this.users.insertOne({
          email,
          username
        });
        return result;
      }
    
      async getUserByEmail(email: string) {
        ...
      }
    
      ...
    }
    

    I was able to set the correct type for client as MongoClient, but since export statements were missing I couldn't set the types for db and users to Database and Collection respectively:

    import { MongoClient, Database, Collection, ... } from "$atlas_sdk/mod.ts";
    
    ...
    export class MongoDBDatabase {
      client: MongoClient;
      db: Database;
      users: Collection;
      ...
    }
    
    opened by arthur-ver 1
  • UUID is not available

    UUID is not available

    import { MongoClient, UUID } from "https://deno.land/x/[email protected]/mod.ts";
    
    const client = new MongoClient({
      endpoint: "https://data.mongodb-api.com/app/YOUR_APP_ID/endpoint/data/v1",
      dataSource: "YOUR_CLUSTER_NAME",
      auth: {
        apiKey: "YOUR_API_KEY",
      },
    });
    
    await client.database("sampleDatabase").collection("sampleCollection").insertOne({
      sampleUUID: new UUID(),
    })
    

    Error when trying to create document with uuid

    error: Uncaught BSONTypeError: Unrecognized or invalid _bsontype: UUID
            throw new BSONTypeError(
                  ^
        at serializeDocument (https://deno.land/x/[email protected]/src/extended_json.ts:318:15)
        at serializeValue (https://deno.land/x/[email protected]/src/extended_json.ts:253:12)
        at serializeDocument (https://deno.land/x/[email protected]/src/extended_json.ts:300:22)
        at serializeValue (https://deno.land/x/[email protected]/src/extended_json.ts:253:12)
        at serializeDocument (https://deno.land/x/[email protected]/src/extended_json.ts:300:22)
        at serializeValue (https://deno.land/x/[email protected]/src/extended_json.ts:253:12)
        at Module.stringify (https://deno.land/x/[email protected]/src/extended_json.ts:469:15)
        at Collection.callApi (https://deno.land/x/[email protected]/client.ts:216:19)
        at Collection.insertOne (https://deno.land/x/[email protected]/client.ts:81:17)
    
    opened by narumincho 2
  • fix collection type definition: `_id` field should be optional on inserts

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

    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

    help wanted good first issue 
    opened by Bestulo 1
Releases(v1.0.3)
  • v1.0.3(Sep 12, 2022)

    What's Changed

    • Add export statements for classes Database and Collection for usage as types by @arthur-ver in https://github.com/erfanium/atlas_sdk/pull/6
    • Error fix "Expected 3 arguments, but got 2." for updateOne(), updateMany() and replaceOne() by @arthur-ver in https://github.com/erfanium/atlas_sdk/pull/7
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/8
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/9
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/10
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/11
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/12
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/13

    New Contributors

    • @arthur-ver made their first contribution in https://github.com/erfanium/atlas_sdk/pull/6

    Full Changelog: https://github.com/erfanium/atlas_sdk/compare/v1.0.2...v1.0.3

    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Jul 21, 2022)

    What's Changed

    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/3
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/4
    • Update dependencies by @github-actions in https://github.com/erfanium/atlas_sdk/pull/5

    New Contributors

    • @github-actions made their first contribution in https://github.com/erfanium/atlas_sdk/pull/3

    Full Changelog: https://github.com/erfanium/atlas_sdk/compare/v1.0.1...v1.0.2

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Jul 10, 2022)

  • v0.3.0(Jul 3, 2022)

    What's Changed

    BREAKING: MongoClient constructor has changed. please read README.md email/password and custom JWT auth methods are now supported

    New Contributors

    • @pagoru made their first contribution in https://github.com/erfanium/atlas_sdk/pull/2

    Full Changelog: https://github.com/erfanium/atlas_sdk/compare/v0.2.0...v0.3.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(May 25, 2022)

  • v0.1.2(Apr 21, 2022)

  • v0.1.1(Apr 16, 2022)

Opinionated collection of TypeScript definitions and utilities for Deno and Deno Deploy. With complete types for Deno/NPM/TS config files, constructed from official JSON schemas.

Schemas Note: You can also import any type from the default module, ./mod.ts deno.json import { type DenoJson } from "https://deno.land/x/[email protected]

deno911 2 Oct 12, 2022
Online Inventory Control System for an apparel manufacturing company "CASANOVA" (Pvt) Ltd. Technology stack: Node.js, Express.js, MongoDB Atlas, React.js (MERN Stack).

Project Name - Online Inventory Control System for an apparel manufacturing company "CASANOVA". The project was given a "A" grade. Group Leader - IT20

Pasindu Rukshan 1 Dec 26, 2021
Serverless Framework + typescript + mongoDB atlas, sample

sls_ts6 Version: 0.9.1 Author : Kouji Nakashima / kuc-arc-f.com date : 2022/01/21 update : Summary Serverless Framework + typescript + mongoDB atlas,

Kouji Nakashima 1 Jan 22, 2022
This repo is accompanying a tutorial that is meant to be a simple introduction to vector search JavaScript engineers who use MongoDB Atlas.

hello-vector-search A simple JavaScript program to run from your computer to vectorize the sample_mflix.movies collection: vectorize_collection.js. A

Marcus 7 Oct 23, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Typesafe API for processing iterable data in TypeScript and JavaScript.

Stream API Type-safe API for processing iterable data in TypeScript and JavaScript similarly to Java 8 Stream API, LINQ or Kotlin Sequences. Unlike Rx

Norbert Szilagyi 31 Aug 4, 2022
This is a simple boilerplate for a Deno website, deployed with Deno Deploy.

Simple Deno Website Boilerplate This is a simple website boilerplate built using Deno and deployed using Deno Deploy. Demo at simple-deno-website-boil

Bruno Bernardino 15 Dec 3, 2022
🛣️ A tiny and fast http request router designed for use with deno and deno deploy

Rutt Rutt is a tiny http router designed for use with deno and deno deploy. It is written in about 200 lines of code and is pretty fast, using an exte

Denosaurs 26 Dec 10, 2022