Support of MongoDB for TypeScript
CAST supports MongoDB via its com.castsoftware.typescript 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
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useUpdateLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useDeleteLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useSelectLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
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
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useUpdateLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useDeleteLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useSelectLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
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’: