rapid-db
Version:
Rapid Database Interface
51 lines (46 loc) • 1.17 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('db');
const pgp = require('../init');
const utils = require('./utils');
/**
* 构造插入交易流水 SQL 语句
*
* 参数:
* record: 见交易流水说明
* where: where 条件语句
*
* 返回:
* SQL 语句
*/
const build_update = (record, where) => {
if (!record || !where) {
throw new Error('更新交易流水参数无效');
}
let fields = {}
for (key in record) {
if (utils.all_keys.indexOf(key) >= 0) {
fields[key] = record[key];
}
}
return pgp.helpers.update(fields, null, 'rapid_trades') + ` WHERE ${where}`;
}
/**
* 更新一条新交易流水
*
* 参数
* data: 流水记录
* where: where 条件语句
* db: 数据库对象,如果为空,则使用全局 _unique_db
* 返回
* 新插入流水 UUID
*/
const update = async (record, where, db) => {
db = db || _unique_db;
if (!db) {
throw new Error('数据库对象无效,检查是否已初始化数据库');
}
await db.none(build_update(record, where));
}
module.exports = update;