欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PostgreSQL Node.js實(shí)現(xiàn)函數(shù)計(jì)算方法示例

 更新時(shí)間:2019年02月12日 09:30:24   作者:阿賀呀  
這篇文章主要給大家介紹了關(guān)于PostgreSQL Node.js實(shí)現(xiàn)函數(shù)計(jì)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

由于工作需要,設(shè)計(jì)到了阿里云的彈性計(jì)算,這里便記錄下來

技術(shù)棧

  • node.js
  • postgresql
  • nodemailer

controller +  services

編寫postgresql lib

不管異常還是正常都返回resolve,在resolve中處理結(jié)果,通過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語句參數(shù)
 * @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 = {
 // 數(shù)據(jù)庫配置
 database: {
 DATABASE: 'databasename',
 USERNAME: 'root',
 PASSWORD: '123456',
 PORT: '3433',
 HOST: 'localhost',
 },
};

module.exports = config;

Controller

BaseController

首先編寫一個(gè)基類,用于封裝一些通用的方法

const pool = require('../lib/postgre'); // 導(dǎo)入封裝好的mysql庫
const { query } = pool; // 導(dǎo)入query方法
class BaseController {
 constructor() {
 }
 // 查詢表內(nèi)所有數(shù)據(jù)(非刪除)
 async list() {
 const sql = `select * from ${this.table}`;
 return await query(sql);
 }
 async excute(sql, vals = []) {
 // 執(zhí)行方法
 return await query(sql, vals);
 }
 // log 方法
 log({func, err}) {
 console.log(`excute function[${func}] occured error : ${err.message || err}`);
 }
}

module.exports = BaseController;

InqueryController

具體的業(yè)務(wù)邏輯Controller類

const BaseController = require('./BaseController'); // 獲得基類
// 繼承基類
class InqueryController extends BaseController {
 constructor() {
 super();
 this.table = 'data_table'; // 賦值table
 }
 // 可以重寫基類的方法,如果有業(yè)務(wù)需要
 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`;
 // 統(tǒng)一在基類調(diào)用sql參數(shù)
 return await this.excute(sql, vals);
 }
 
}
module.exports = InqueryController;

Service

BaseService

統(tǒng)一封裝的方法,基類

// 需要綁定this的方法
const funcs = [
 'list',
]
class BaseService {
 constructor() {
 this.controller = null;
 // 循環(huán)遍歷綁定this, 在koa綁定route的時(shí)可用到
 funcs.forEach(item => {
  this[item] = this[item].bind(this)
 });
 }

 // 查詢方法
 async list(ctx) {
 if (!ctx) {
  return await this.controller.list();
 }
 // controller返回的是一個(gè)對(duì)象,success(成功為true, 失敗為false), data(成功則有此數(shù)據(jù)), err(失敗則有此對(duì)象)
 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

具體的業(yè)務(wù)邏輯

// 導(dǎo)入基類
const BaseService = require('./BaseService');
// 導(dǎo)入對(duì)應(yīng)的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()
 // 綁定對(duì)應(yīng)的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

函數(shù)計(jì)算的邏輯

const inqueryService = require('./services/InqueryService'); 
exports.handler = async function(event, context, callback) {
 const result = await inqueryService.unsendUser();
 callback(null, result);
};

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Node.js搭建Https服務(wù)過程詳解

    Node.js搭建Https服務(wù)過程詳解

    這篇文章主要為大家介紹了Node.js搭建Https服務(wù)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 在Node.js中實(shí)現(xiàn)視頻收藏功能

    在Node.js中實(shí)現(xiàn)視頻收藏功能

    在構(gòu)建視頻分享平臺(tái)時(shí),視頻的收藏功能是用戶互動(dòng)的重要組成部分,本文將介紹如何在Node.js應(yīng)用中實(shí)現(xiàn)視頻收藏功能,包括數(shù)據(jù)模型的創(chuàng)建、業(yè)務(wù)邏輯的實(shí)現(xiàn)以及接口的驗(yàn)證測(cè)試,需要的朋友可以參考下
    2024-04-04
  • nodeJS模塊簡(jiǎn)單用法示例

    nodeJS模塊簡(jiǎn)單用法示例

    這篇文章主要介紹了nodeJS模塊簡(jiǎn)單用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了nodejs模塊定義、引入、注冊(cè)、啟動(dòng)等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • 在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法

    在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法

    這篇文章主要介紹了在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • node.js使用 http-proxy 創(chuàng)建代理服務(wù)器操作示例

    node.js使用 http-proxy 創(chuàng)建代理服務(wù)器操作示例

    這篇文章主要介紹了node.js使用 http-proxy 創(chuàng)建代理服務(wù)器,結(jié)合實(shí)例形式分析了node.js使用 http-proxy 創(chuàng)建代理服務(wù)器原理、具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • node.js中PC端微信小程序包解密的處理思路

    node.js中PC端微信小程序包解密的處理思路

    我們使用node.js去寫一個(gè)解碼的程序。根據(jù)上邊加密的流程,我們首先讀取加密文件,把前6個(gè)字節(jié)的固定字符串去除,這篇文章主要介紹了node.js中PC端微信小程序包解密,需要的朋友可以參考下
    2021-12-12
  • express如何使用session與cookie的方法

    express如何使用session與cookie的方法

    本篇文章主要介紹了express如何使用session與cookie的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Node.js爬蟲如何獲取天氣和每日問候詳解

    Node.js爬蟲如何獲取天氣和每日問候詳解

    這篇文章主要給大家介紹了關(guān)于Node.js爬蟲如何獲取天氣和每日問候的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Node.js爬蟲具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • node文件上傳功能簡(jiǎn)易實(shí)現(xiàn)代碼

    node文件上傳功能簡(jiǎn)易實(shí)現(xiàn)代碼

    本篇文章主要介紹了node文件上傳功能簡(jiǎn)易實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • nodejs之get/post請(qǐng)求的幾種方式小結(jié)

    nodejs之get/post請(qǐng)求的幾種方式小結(jié)

    下面小編就為大家?guī)硪黄猲odejs之get/post請(qǐng)求的幾種方式小結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07

最新評(píng)論