mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	This commit is contained in:
		| @@ -25,9 +25,24 @@ module.exports = function (db, module) { | |||||||
| 		if (!key) { | 		if (!key) { | ||||||
| 			return callback(); | 			return callback(); | ||||||
| 		} | 		} | ||||||
|  | 		if (Array.isArray(key)) { | ||||||
|  | 			db.collection('objects').find({ _key: { $in: key } }).toArray(function (err, data) { | ||||||
|  | 				if (err) { | ||||||
|  | 					return callback(err); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				var map = {}; | ||||||
|  | 				data.forEach(function (item) { | ||||||
|  | 					map[item._key] = true; | ||||||
|  | 				}); | ||||||
|  |  | ||||||
|  | 				callback(null, key.map(key => !!map[key])); | ||||||
|  | 			}); | ||||||
|  | 		} else { | ||||||
| 			db.collection('objects').findOne({ _key: key }, function (err, item) { | 			db.collection('objects').findOne({ _key: key }, function (err, item) { | ||||||
| 				callback(err, item !== undefined && item !== null); | 				callback(err, item !== undefined && item !== null); | ||||||
| 			}); | 			}); | ||||||
|  | 		} | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	module.delete = function (key, callback) { | 	module.delete = function (key, callback) { | ||||||
|   | |||||||
| @@ -30,6 +30,26 @@ module.exports = function (db, module) { | |||||||
| 			return callback(); | 			return callback(); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		if (Array.isArray(key)) { | ||||||
|  | 			query({ | ||||||
|  | 				name: 'exists', | ||||||
|  | 				text: ` | ||||||
|  | 				SELECT o."_key" k | ||||||
|  |   				FROM "legacy_object_live" o | ||||||
|  |  				WHERE o."_key" = ANY($1::TEXT[])`, | ||||||
|  | 				values: [key], | ||||||
|  | 			}, function (err, res) { | ||||||
|  | 				if (err) { | ||||||
|  | 					return callback(err); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				callback(null, key.map(function (k) { | ||||||
|  | 					return res.rows.some(function (r) { | ||||||
|  | 						return r.k === k; | ||||||
|  | 					}); | ||||||
|  | 				})); | ||||||
|  | 			}); | ||||||
|  | 		} else { | ||||||
| 			query({ | 			query({ | ||||||
| 				name: 'exists', | 				name: 'exists', | ||||||
| 				text: ` | 				text: ` | ||||||
| @@ -45,6 +65,7 @@ SELECT EXISTS(SELECT * | |||||||
|  |  | ||||||
| 				callback(null, res.rows[0].e); | 				callback(null, res.rows[0].e); | ||||||
| 			}); | 			}); | ||||||
|  | 		} | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	module.delete = function (key, callback) { | 	module.delete = function (key, callback) { | ||||||
|   | |||||||
| @@ -1,6 +1,8 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| module.exports = function (redisClient, module) { | module.exports = function (redisClient, module) { | ||||||
|  | 	var helpers = module.helpers.redis; | ||||||
|  |  | ||||||
| 	module.flushdb = function (callback) { | 	module.flushdb = function (callback) { | ||||||
| 		redisClient.send_command('flushdb', [], function (err) { | 		redisClient.send_command('flushdb', [], function (err) { | ||||||
| 			if (typeof callback === 'function') { | 			if (typeof callback === 'function') { | ||||||
| @@ -20,9 +22,15 @@ module.exports = function (redisClient, module) { | |||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	module.exists = function (key, callback) { | 	module.exists = function (key, callback) { | ||||||
|  | 		if (Array.isArray(key)) { | ||||||
|  | 			helpers.multiKeys(redisClient, 'exists', key, function (err, data) { | ||||||
|  | 				callback(err, data && data.map(exists => exists === 1)); | ||||||
|  | 			}); | ||||||
|  | 		} else { | ||||||
| 			redisClient.exists(key, function (err, exists) { | 			redisClient.exists(key, function (err, exists) { | ||||||
| 				callback(err, exists === 1); | 				callback(err, exists === 1); | ||||||
| 			}); | 			}); | ||||||
|  | 		} | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	module.delete = function (key, callback) { | 	module.delete = function (key, callback) { | ||||||
|   | |||||||
| @@ -53,6 +53,14 @@ describe('Key methods', function () { | |||||||
| 		}); | 		}); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|  | 	it('should work for an array of keys', function (done) { | ||||||
|  | 		db.exists(['testKey', 'doesnotexist'], function (err, exists) { | ||||||
|  | 			assert.ifError(err); | ||||||
|  | 			assert.deepStrictEqual(exists, [true, false]); | ||||||
|  | 			done(); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
| 	it('should delete a key without error', function (done) { | 	it('should delete a key without error', function (done) { | ||||||
| 		db.delete('testKey', function (err) { | 		db.delete('testKey', function (err) { | ||||||
| 			assert.ifError(err); | 			assert.ifError(err); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user