rapid-db
Version:
Rapid Database Interface
38 lines (33 loc) • 1.07 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('db');
const moment = require('moment');
/**
* 生成一个新的订单编号并返回
*
* 参数
* type: 最多2位数字序号类型,用于增加特殊性(可选)
* db: 数据库对象,如果为空,则使用全局 _unique_db
* 返回
* 新的订单编号,保证在系统内唯一
*
* 订单编号格式
* YYYYMMDDHHmmss + 2位类型(可选) + 4位序号
*/
const new_order_id = async (type, db) => {
db = db || _unique_db;
if (!db) {
throw new Error('数据库对象无效,检查是否已初始化数据库');
}
const results = await db.one(`
update rapid_serials set order_id = order_id + 1
returning order_id
`);
const order_id = '000000' + results.order_id;
const prefix = moment().format('YYYYMMDDHHmmss');
const middle = type && type.substr(0, 2) || '';
const suffix = order_id.substr(order_id.length - 4);
return prefix + middle + suffix;
}
module.exports = new_order_id;