func-stateless-mongodb-sdk-nodejs
v1.0.6
Published
UOS Func Stateless CRUD MongoDB Nodejs SDK
Downloads
10
Readme
UOS Func Stateless CRUD MongoDB Nodejs SDK
用法
1.安装 UOS Func Stateless CRUD MongoDB Nodejs SDK
npm install func-stateless-mongodb-sdk-nodejs
2.引用本 SDK 并连接 MongoDB Client
context.database = require('func-stateless-mongodb-sdk-nodejs').database;
// If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
// 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
// Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
// 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
// Please note: if there are multiple available mongo databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 mongo 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
try {
const databaseId = '';
const databasePassword = '';
const database = await context.database(databaseId, databasePassword);
const client = await database.connection();
} catch (error) {
console.error(error);
}
3.使用 MongoDB Client 操作数据库
例:
try {
const db = await client.db('mongo');
const collection = await db.createCollection('myCollection');
const insertResult = await collection.insertOne({
name: 'Alice',
age: 25,
});
console.log('db insert result:', insertResult.acknowledged);
const findResult = await collection.findOne({ name: 'Alice' });
console.log('db query documents:', findResult);
//test end pool
await database.endConnection();
console.log('closed the connection pool');
} catch (error) {
console.error(error);
}
4.完整操作示例
/*
环境变量(自动生成):
process.env['UOS_APP_ID'] = 8972b2ba-ffb4-4211-bbef-1274083a2c09
process.env['UOS_APP_SECRET'] = 9e131ad6-e73e-4e47-bc9d-936df2b1e70a
process.env['UOS_APP_SERVICE_SECRET'] = f4f57f44-273c-4005-ab70-d469d7b45053
*/
exports.main = async (event, context) => {
context.database = require('func-stateless-mongodb-sdk-nodejs').database;
// If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
// 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
// Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
// 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
// Please note: if there are multiple available mongo databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 mongo 数据库的话,请在代码中指定要用于连接的数据库的 databaseId。
const databaseId = '';
const databasePassword = '';
try {
const database = await context.database(databaseId, databasePassword);
const client = await database.connection();
try {
const db = await client.db('mongo');
const collection = await db.createCollection('myCollection');
const insertResult = await collection.insertOne({
name: 'Alice',
age: 25,
});
console.log('db insert result:', insertResult.acknowledged);
const findResult = await collection.findOne({ name: 'Alice' });
console.log('db query documents:', findResult);
} catch (error) {
console.error(error);
}
//test end pool
await database.endConnection();
console.log('closed the connection pool');
} catch (error) {
console.error(error);
}
};