Support of MongoDB for TypeScript
CAST supports MongoDB via its com.castsoftware.typescript extension. Details about the support provided for TyepScript source code is discussed below.
WARNING: This documentation page is for com.castoftware.typescript from 1.18.x and above. If you need to view documentation for older releases see here.
Supported Libraries
The following libraries are supported:
- MongoDB
- Mongoose
- Prisma
Objects
Icon | Description |
---|---|
NodeJS MongoDB database | |
NodeJS Unknown MongoDB database | |
NodeJS MongoDB collection | |
NodeJS Unknown MongoDB collection |
Supported MongoDB
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useUpdateLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useDeleteLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useSelectLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
Example
Taking the following codes:
import { MongoClient, Db, Collection, Document, BulkWriteResult } from "mongodb";
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
async function my_add_object(): Promise<void> {
const db: Db = client.db("testdb");
const foo: Collection<Document> = db.collection("FooCollection");
// Insert a single object
const result = await foo.insertOne({ name: "Alice", age: 25, active: true });
console.log("Inserted object:", result.insertedId);
}
async function my_bulkwrite(): Promise<void> {
const db: Db = client.db("testdb");
const foo: Collection<Document> = db.collection("FooCollection");
// Perform multiple operations in bulk
const result: BulkWriteResult = await foo.bulkWrite([
{ insertOne: { document: { name: "Bob", age: 30, active: false } } },
{ updateOne: { filter: { name: "Alice" }, update: { $set: { active: false } } } },
{ deleteOne: { filter: { name: "Charlie" } } }
]);
console.log("BulkWrite result:", result);
}
async function my_aggregate(): Promise<void> {
const db: Db = client.db("testdb");
const foo: Collection<Document> = db.collection("FooCollection");
// Simple aggregation: group by 'active' and count, then merge results into another collection
const pipeline = [
{ $group: { _id: "$active", count: { $sum: 1 } } },
{ $merge: { into: "TargetCollection", whenMatched: "merge", whenNotMatched: "insert" } }
];
const result = await foo.aggregate(pipeline).toArray();
console.log("Aggregation result (before merge):", result);
// Check TargetCollection contents
const target: Collection<Document> = db.collection("TargetCollection");
const merged = await target.find().toArray();
console.log("TargetCollection after merge:", merged);
}
async function main(): Promise<void> {
try {
await client.connect();
await my_add_object();
await my_bulkwrite();
await my_aggregate();
} catch (err) {
console.error("Error:", err);
} finally {
await client.close();
}
}
main().catch(console.error);
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
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useUpdateLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useDeleteLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useSelectLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
Example
Taking the following codes:
// foo-mongoose-example.ts
import { Schema, model, connect, disconnect } from 'mongoose;
const uri = "mongodb://localhost:27017/testdb";
// Define schema & model
const fooSchema = new Schema({
name: String,
age: Number,
active: Boolean,
});
const FooCollection = model("FooCollection", fooSchema);
async function my_add_object() {
// Insert a single object using the model
const doc = new FooCollection({ name: "Alice", age: 25, active: true });
await doc.save();
console.log("Inserted object:", doc._id);
}
async function my_bulkwrite() {
// Use the model's bulkWrite API
const result = await FooCollection.bulkWrite([
{ insertOne: { document: { name: "Bob", age: 30, active: false } } },
{ updateOne: { filter: { name: "Alice" }, update: { $set: { active: false } } } },
{ deleteOne: { filter: { name: "Charlie" } } },
]);
console.log("BulkWrite result:", result);
}
async function my_aggregate() {
// Simple aggregation: group by 'active' and count
const pipeline = [
{ $group: { _id: "$active", count: { $sum: 1 } } },
{
$merge: {
into: "TargetCollection",
whenMatched: "merge",
whenNotMatched: "insert",
},
},
];
const result = await FooCollection.aggregate(pipeline);
console.log("Aggregation result (before merge):", result);
}
async function main() {
try {
await connect(uri);
await my_add_object();
await my_bulkwrite();
await my_aggregate();
} finally {
await disconnect();
}
}
main().catch(console.error);
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.