PostgreSQL Node.js实现函数计算方法示例
程序员文章站
2023-02-25 10:15:18
前言
由于工作需要,设计到了阿里云的弹性计算,这里便记录下来
技术栈
node.js
postgresql
nodemailer
con...
前言
由于工作需要,设计到了阿里云的弹性计算,这里便记录下来
技术栈
- node.js
- postgresql
- nodemailer
controller + services
编写postgresql lib
不管异常还是正常都返回resolve,在resolve中处理结果,通过success字段去处理
const { pool } = require('pg'); const config = require('../config/default.js'); const { database: { host, port, database, username, password, }, } = config; const pool = new pool({ port: port, host: host, user: username, password: password, database: database, }); /** * * @param sql 接收的sql语句 * @param {array} values sql语句参数 * @return { object } { success: boolean, err || data } */ const query = async function( sql = 'select now()', values = []) { return new promise(resolve => { pool.connect((err, client, release) => { if (err) { return console.error('error acquiring client', err.stack) } const params = array.isarray(values) ? [...values] : [values]; client.query(sql, params, (error, result) => { release(); if (error) { console.error('error executing query', error.stack); resolve({ success: false, error, }); } resolve({ success: true, data: result.rows, }); }); }); }); } module.exports = { query, }
config配置文件如下
const config = { // 数据库配置 database: { database: 'databasename', username: 'root', password: '123456', port: '3433', host: 'localhost', }, }; module.exports = config;
controller
basecontroller
首先编写一个基类,用于封装一些通用的方法
const pool = require('../lib/postgre'); // 导入封装好的mysql库 const { query } = pool; // 导入query方法 class basecontroller { constructor() { } // 查询表内所有数据(非删除) async list() { const sql = `select * from ${this.table}`; return await query(sql); } async excute(sql, vals = []) { // 执行方法 return await query(sql, vals); } // log 方法 log({func, err}) { console.log(`excute function[${func}] occured error : ${err.message || err}`); } } module.exports = basecontroller;
inquerycontroller
具体的业务逻辑controller类
const basecontroller = require('./basecontroller'); // 获得基类 // 继承基类 class inquerycontroller extends basecontroller { constructor() { super(); this.table = 'data_table'; // 赋值table } // 可以重写基类的方法,如果有业务需要 async list() { const sql = `select * from ${this.table} order by created_at desc `; return await this.excute(sql); } async getunsendcustomer(vals) { const sql = `select * from ${this.table} where created_at > $1 order by created_at desc`; // 统一在基类调用sql参数 return await this.excute(sql, vals); } } module.exports = inquerycontroller;
service
baseservice
统一封装的方法,基类
// 需要绑定this的方法 const funcs = [ 'list', ] class baseservice { constructor() { this.controller = null; // 循环遍历绑定this, 在koa绑定route的时可用到 funcs.foreach(item => { this[item] = this[item].bind(this) }); } // 查询方法 async list(ctx) { if (!ctx) { return await this.controller.list(); } // controller返回的是一个对象,success(成功为true, 失败为false), data(成功则有此数据), err(失败则有此对象) const { success: flag, data, error } = await this.controller.list(); if (flag) { // success ctx.body = { data, code: 200, } } else { // failed ctx.body = { code: 500, error, }; } } } module.exports = baseservice
inqueryservice
具体的业务逻辑
// 导入基类 const baseservice = require('./baseservice'); // 导入对应的controller const controller = require('../controller/inquerycontroller'); // 获取mailsender service const mailservice = require('./mailsender'); const helper = require('../util/helper'); const funcs = [ 'unsenduser', ]; // 生成一次controller const controller = new controller(); class inqueryservice extends baseservice { constructor() { super() // 绑定对应的controller this.controller = controller; funcs.foreach(item => { this[item] = this[item].bind(this); }); } getmailopts(i) { // you can use the data from database to combine the message const message = 'hello world!'; return return { message, // 可以从配置文件读取或者oss to: 'xxxx@gmail.com', subject: 'hello world', }; } async unsenduser() { const list = await this.controller.list(); if (list.length > 0) { const mailer = new mailservice(); const errorlist = []; iteratelist.foreach(async i => { const maileroption = this.getmailopts(i); const { success, ...rest } = await mailer.sendtoauitadmin(maileroption); if (!success) { errorlist.push(rest); } }); const lastesttime = iteratelist[0].created_at; if (errorlist.length === 0) { return { code: 200, message: 'success', }; } } else { return { code: 204, message: 'no user found', }; } } } module.exports = new inqueryservice();
index.js
函数计算的逻辑
const inqueryservice = require('./services/inqueryservice'); exports.handler = async function(event, context, callback) { const result = await inqueryservice.unsenduser(); callback(null, result); };
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。