func-stateless-redis-sdk-nodejs
v1.0.6
Published
UOS Func Stateless CRUD Redis Nodejs SDK
Downloads
4
Readme
UOS Func Stateless CRUD Redis Nodejs SDK
用法
1.安装 UOS Func Stateless CRUD Redis Nodejs SDK
npm install func-stateless-redis-sdk-nodejs
2.引用本 SDK 并连接 Redis Client
context.database = require('func-stateless-redis-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 redis databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 redis 数据库的话,请在代码中指定要用于连接的数据库的 databaseId。
try {
const databaseId = '';
const databasePassword = '';
const database = await context.database(databaseId, databasePassword);
const client = await database.connection();
} catch (error) {
console.error(error);
}
3.使用 Redis Client 操作数据库
例:
try {
try {
await client.set('someKey', 'someValue');
const result = await client.get('someKey');
console.log('db query result:', result);
} catch (error) {
console.error('failed to query data, error:' + error);
} finally {
// Please note: redis client should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
// 注意:redis客户端需要被手动释放,无论查询过程是否发生错误
await client.release();
}
// 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-redis-sdk-nodejs').database;
// If you do not specify a databaseId, the system 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 redis, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 redis 的话,请在代码中指定要用于连接的 databaseId。
const databaseId = '';
const databasePassword = '';
try {
const database = await context.database(databaseId, databasePassword);
const client = await database.connection();
try {
await client.set('someKey', 'someValue');
const result = await client.get('someKey');
console.log('db query result:', result);
} catch (error) {
console.error('failed to query data, error:' + error);
} finally {
// Please note: redis client should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
// 注意:redis客户端需要被手动释放,无论查询过程是否发生错误
await client.release();
}
// test end pool
await database.endConnection();
console.log('closed the connection pool');
} catch (error) {
console.error(error);
}
};