javascript upsert使用knex(nodejs)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript upsert使用knex(nodejs)相关的知识,希望对你有一定的参考价值。
'use strict'
const path = require('path');
const knex = require(path.join(__dirname, '../../../modules/db'));
const _ = require('lodash');
const upsert = (dbName, tableName, insertData, onDuplicateKeySql) => {
if (_.isEmpty(dbName)) {
return Promise.reject('No such database');
}
if (_.isEmpty(insertData)) {
//插入数据集为空,则直接返回成功
return Promise.resolve('Success');
}
let sql = knex
.withSchema(dbName)
.insert(insertData)
.into(tableName).toString();
//添加空行
sql += ` ${onDuplicateKeySql}`;
// logger.sql(sql);
return knex.raw(sql);
};
//example
const insertStaffMemberService = (dbName, data) => {
let onDuplicateKeySql = ' ON DUPLICATE KEY UPDATE ' +
'staffId = values(staffId),storeId = values(storeId),' +
'memberServiceId = values(memberServiceId)';
return dbUtils.upsert(dbName, 'StaffMemberService', data, onDuplicateKeySql)
}
//db
'use strict';
const path = require('path');
const config = require(path.join(__dirname,'../config/config.json'));
const dbConfig = config.mysql;
const debugMode = config.debugMode;
const knex = require('knex');
module.exports = knex({
client: 'mysql',
connection: {
host : dbConfig.host,
port : dbConfig.port,
user : dbConfig.user,
password : dbConfig.password,
supportBigNumbers: dbConfig.supportBigNumbers,
bigNumberStrings: dbConfig.bigNumberStrings,
multipleStatements: true
},
pool: { min: 0, max: dbConfig.connectionLimit },
debug: debugMode
});
以上是关于javascript upsert使用knex(nodejs)的主要内容,如果未能解决你的问题,请参考以下文章
javascript salvando usuario com knex
javascript knex交易
javascript Criandoapituraçãoparautilizar o knex
javascript Inserindo algo no banco com knex
将原始 SQL 转换为 Bookshelf/Knex
Knex.js 和 MySQL:将整数转换为布尔值以进行批量选择