Support of MongoDB for TypeScript

CAST supports MongoDB via its com.castsoftware.typescriptexternal link extension. Details about the support provided for TypeScript source code is discussed below.

Supported Libraries

The following libraries are supported:

  • MongoDB
  • Mongoose
  • Prisma

Objects

Icon Description
Node.js MongoDB connection
Node.js unknown MongoDB connection
Node.js MongoDB collection
Node.js unknown MongoDB collection

Supported MongoDB

Link Type Source and destination of link Supported APIs
useInsertLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • db.collection.insert
  • db.collection.insertMany
  • db.collection.insertOne
useUpdateLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • db.collection.bulkWrite
  • db.collection.findAndModify
  • db.collection.findOneAndReplace
  • db.collection.findOneAndUpdate
  • db.collection.replaceOne
  • db.collection.update
  • db.collection.updateMany
  • db.collection.updateOne
useDeleteLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • db.collection.deleteMany
  • db.collection.deleteOne
  • db.collection.drop
  • db.collection.findOneAndDelete
  • db.collection.remove
useSelectLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • db.collection.aggregate
  • db.collection.count
  • db.collection.countDocuments
  • db.collection.createIndex
  • db.collection.createIndexes
  • db.collection.distinct
  • db.collection.dropIndex
  • db.collection.dropIndexes
  • db.collection.ensureIndex
  • db.collection.estimatedDocumentCount
  • db.collection.find
  • db.collection.findOne
  • db.collection.isCapped
  • db.collection.mapReduce
  • db.collection.reIndex
  • db.collection.stats
  • db.collection.watch

Example

Taking the following codes:

import * as http from 'http';
import {MongoClient} from 'mongodb';

const server = http.createServer(async (req, res) => {
  await new Promise((resolve, reject) => {
    MongoClient.connect('mongodb://localhost:2701/mongodb', function(err: any, client: any) {
      client.db('admin').collection('docs').findOne().then(
        (resDB: any) => {
          res.end('${resDB}');
          client.close();
        },
        (err: any) => {
          res.end('${err}');
          client.close();
        },
      );
    });
  });
});

In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useSelect’ link from function ‘Anonymous1’ to the collection ‘docs’:

Supported Mongoose

Link Type Source and destination of link Supported APIs
useInsertLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • model.create
  • model.insertMany
useUpdateLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • model.aggregate
  • model.findByIdAndUpdate
  • model.findOneAndUpdate
  • model.findOneAndReplace
  • model.replaceOne
  • model.updateOne
  • model.updateMany
useDeleteLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • model.deleteOne
  • model.deleteMany
  • model.findByIdAndDelete
  • model.findOneAndDelete
useSelectLink Between TypeScript Function/Method/Module and Node.js MongoDB collection
  • model.exists
  • model.find
  • model.findOne
  • model.findById
  • model.where

Example

Taking the following codes:

import * as mongoose from 'mongoose'

class DbService {
  private db: any;
  private dbConn: any = null;
  constructor (conn: any = connection) {
    this.dbConn = conn
  };
  connect (): Promise<string> {
    mongoose.connect('mongodb://localhost:2701/mongoose', this.dbConn.options)
      .then(success => {
        this.db = mongoose.connection
      })
  }
  async findbyId (tprid, id?) {
    response = await this.db.models.Engagement.find(query);
  }
}

In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useSelect’ link from method ‘findbyId’ to the collection ‘Engagement’:

Supported Prisma

See Node.js - Prisma support - MongoDB database.