mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 01:26:16 +01:00
PostgreSQL database driver (#5861)
* [test/database/list] Fix test list 4 being used in two different tests * [database/postgres] PostgreSQL database driver * [database/postgres] Make transactions work based on continuation scope. * [database/postgres] Implement nested transactions * eslint --fix * Add database changes from earlier this week to the PostgreSQL driver. * Fix typo * Fix postgres.incrObjectFieldBy returning undefined instead of null when given NaN * [database/postgres] Fix sortedSetsCard returning an array of strings. * Update socket.io postgres adapter * Fix PostgreSQL erroring when multiple updates are made to the same sorted set entry in a single operation. Add a test case to catch this error. * Fix lint errors. * Only prune sessions on one instance in a cluster to avoid deadlocks. They're caught and handled by the database server, but they spam the logs. * Fix arguments.slice.
This commit is contained in:
108
src/database/postgres/sorted/add.js
Normal file
108
src/database/postgres/sorted/add.js
Normal file
@@ -0,0 +1,108 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
module.exports = function (db, module) {
|
||||
var helpers = module.helpers.postgres;
|
||||
|
||||
module.sortedSetAdd = function (key, score, value, callback) {
|
||||
callback = callback || helpers.noop;
|
||||
|
||||
if (!key) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
if (Array.isArray(score) && Array.isArray(value)) {
|
||||
return sortedSetAddBulk(key, score, value, callback);
|
||||
}
|
||||
|
||||
value = helpers.valueToString(value);
|
||||
score = parseFloat(score);
|
||||
|
||||
module.transaction(function (tx, done) {
|
||||
var query = tx.client.query.bind(tx.client);
|
||||
|
||||
async.series([
|
||||
async.apply(helpers.ensureLegacyObjectType, tx.client, key, 'zset'),
|
||||
async.apply(query, {
|
||||
name: 'sortedSetAdd',
|
||||
text: `
|
||||
INSERT INTO "legacy_zset" ("_key", "value", "score")
|
||||
VALUES ($1::TEXT, $2::TEXT, $3::NUMERIC)
|
||||
ON CONFLICT ("_key", "value")
|
||||
DO UPDATE SET "score" = $3::NUMERIC`,
|
||||
values: [key, value, score],
|
||||
}),
|
||||
], function (err) {
|
||||
done(err);
|
||||
});
|
||||
}, callback);
|
||||
};
|
||||
|
||||
function sortedSetAddBulk(key, scores, values, callback) {
|
||||
if (!scores.length || !values.length) {
|
||||
return callback();
|
||||
}
|
||||
if (scores.length !== values.length) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
values = values.map(helpers.valueToString);
|
||||
scores = scores.map(function (score) {
|
||||
return parseFloat(score);
|
||||
});
|
||||
|
||||
helpers.removeDuplicateValues(values, scores);
|
||||
|
||||
module.transaction(function (tx, done) {
|
||||
var query = tx.client.query.bind(tx.client);
|
||||
|
||||
async.series([
|
||||
async.apply(helpers.ensureLegacyObjectType, tx.client, key, 'zset'),
|
||||
async.apply(query, {
|
||||
name: 'sortedSetAddBulk',
|
||||
text: `
|
||||
INSERT INTO "legacy_zset" ("_key", "value", "score")
|
||||
SELECT $1::TEXT, v, s
|
||||
FROM UNNEST($2::TEXT[], $3::NUMERIC[]) vs(v, s)
|
||||
ON CONFLICT ("_key", "value")
|
||||
DO UPDATE SET "score" = EXCLUDED."score"`,
|
||||
values: [key, values, scores],
|
||||
}),
|
||||
], function (err) {
|
||||
done(err);
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
|
||||
module.sortedSetsAdd = function (keys, score, value, callback) {
|
||||
callback = callback || helpers.noop;
|
||||
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
value = helpers.valueToString(value);
|
||||
score = parseFloat(score);
|
||||
|
||||
module.transaction(function (tx, done) {
|
||||
var query = tx.client.query.bind(tx.client);
|
||||
|
||||
async.series([
|
||||
async.apply(helpers.ensureLegacyObjectsType, tx.client, keys, 'zset'),
|
||||
async.apply(query, {
|
||||
name: 'sortedSetsAdd',
|
||||
text: `
|
||||
INSERT INTO "legacy_zset" ("_key", "value", "score")
|
||||
SELECT k, $2::TEXT, $3::NUMERIC
|
||||
FROM UNNEST($1::TEXT[]) k
|
||||
ON CONFLICT ("_key", "value")
|
||||
DO UPDATE SET "score" = $3::NUMERIC`,
|
||||
values: [keys, value, score],
|
||||
}),
|
||||
], function (err) {
|
||||
done(err);
|
||||
});
|
||||
}, callback);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user