func-stateless-postgresql-sdk-nodejs
v1.1.1
Published
UOS Func Stateless CRUD PostgreSQL Nodejs SDK
Downloads
6
Readme
UOS Func Stateless CRUD PostgreSQL Nodejs SDK
用法
1.安装 UOS Func Stateless CRUD PostgreSQL Nodejs SDK
npm install func-stateless-postgresql-sdk-nodejs
2.引用本 SDK 并连接 PostgreSQL Client
context.database = require('func-stateless-postgresql-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 postgresql databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 postgresql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId。
try {
const databaseId = '';
const databasePassword = '';
// 以模板数据库为例
const database = await context.database(databaseId, databasePassword, 'template1');
const connection = await database.connection();
} catch (error) {
console.error(error);
}
3.使用 PostgreSQL Client 操作数据库
例:
try {
try {
const result = await connection.query('SELECT tablename FROM pg_tables');
console.log('db query result:', result.rows);
} catch (error) {
console.error('failed to query data, error:' + error);
} finally {
// Please note: postgresql 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.
// 注意:postgresql 客户端需要被手动释放,无论查询过程是否发生错误
await connection.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-postgresql-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 postgresql databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 postgresql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId。
const databaseId = '';
const databasePassword = '';
try {
// 以模板数据库为例
const database = await context.database(databaseId, databasePassword, 'template1');
const connection = await database.connection();
try {
const result = await connection.query('SELECT tablename FROM pg_tables');
console.log('db query result:', result.rows);
} catch (error) {
console.error('failed to query data, error:' + error);
} finally {
// Please note: psql 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.
// 注意:postgresql 客户端需要被手动释放,无论查询过程是否发生错误
await connection.release();
}
// test end pool
await database.endConnection();
console.log('closed the connection pool');
} catch (error) {
console.error(error);
}
};