mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							856ba78a5f
						
					
				
				
					commit
					0ce4b87d85
				
			| @@ -14,6 +14,9 @@ module.exports = function (module) { | ||||
| 		} | ||||
|  | ||||
| 		const writeData = helpers.serializeData(data); | ||||
| 		if (!Object.keys(writeData).length) { | ||||
| 			return; | ||||
| 		} | ||||
| 		try { | ||||
| 			if (isArray) { | ||||
| 				const bulk = module.client.collection('objects').initializeUnorderedBulkOp(); | ||||
| @@ -39,9 +42,18 @@ module.exports = function (module) { | ||||
|  | ||||
| 		const writeData = data.map(helpers.serializeData); | ||||
| 		try { | ||||
| 			const bulk = module.client.collection('objects').initializeUnorderedBulkOp(); | ||||
| 			keys.forEach((key, i) => bulk.find({ _key: key }).upsert().updateOne({ $set: writeData[i] })); | ||||
| 			let bulk; | ||||
| 			keys.forEach((key, i) => { | ||||
| 				if (Object.keys(writeData[i]).length) { | ||||
| 					if (!bulk) { | ||||
| 						bulk = module.client.collection('objects').initializeUnorderedBulkOp(); | ||||
| 					} | ||||
| 					bulk.find({ _key: key }).upsert().updateOne({ $set: writeData[i] }); | ||||
| 				} | ||||
| 			}); | ||||
| 			if (bulk) { | ||||
| 				await bulk.execute(); | ||||
| 			} | ||||
| 		} catch (err) { | ||||
| 			if (err && err.message.startsWith('E11000 duplicate key error')) { | ||||
| 				return await module.setObjectBulk(keys, data); | ||||
|   | ||||
| @@ -11,7 +11,9 @@ module.exports = function (module) { | ||||
| 		if (data.hasOwnProperty('')) { | ||||
| 			delete data['']; | ||||
| 		} | ||||
|  | ||||
| 		if (!Object.keys(data).length) { | ||||
| 			return; | ||||
| 		} | ||||
| 		await module.transaction(async (client) => { | ||||
| 			const dataString = JSON.stringify(data); | ||||
| 			async function setOne(key) { | ||||
|   | ||||
| @@ -41,7 +41,11 @@ module.exports = function (module) { | ||||
| 			return; | ||||
| 		} | ||||
| 		const batch = module.client.batch(); | ||||
| 		keys.forEach((k, i) => batch.hmset(k, data[i])); | ||||
| 		keys.forEach((k, i) => { | ||||
| 			if (Object.keys(data[i]).length) { | ||||
| 				batch.hmset(k, data[i]); | ||||
| 			} | ||||
| 		}); | ||||
| 		await helpers.execBatch(batch); | ||||
| 		cache.del(keys); | ||||
| 	}; | ||||
|   | ||||
| @@ -72,7 +72,7 @@ describe('Hash methods', () => { | ||||
| 			}); | ||||
| 		}); | ||||
|  | ||||
| 		it('should set multiple keys to different okjects', async () => { | ||||
| 		it('should set multiple keys to different objects', async () => { | ||||
| 			const keys = ['bulkKey1', 'bulkKey2']; | ||||
| 			const data = [{ foo: '1' }, { baz: 'baz' }]; | ||||
|  | ||||
| @@ -80,6 +80,39 @@ describe('Hash methods', () => { | ||||
| 			const result = await db.getObjects(keys); | ||||
| 			assert.deepStrictEqual(result, data); | ||||
| 		}); | ||||
|  | ||||
| 		it('should not error if object is empty', async () => { | ||||
| 			const keys = ['bulkKey3', 'bulkKey4']; | ||||
| 			const data = [{ foo: '1' }, { }]; | ||||
|  | ||||
| 			await db.setObjectBulk(keys, data); | ||||
| 			const result = await db.getObjects(keys); | ||||
| 			assert.deepStrictEqual(result, [{ foo: '1' }, null]); | ||||
| 		}); | ||||
|  | ||||
| 		it('should not error if object is empty', async () => { | ||||
| 			const keys = ['bulkKey5']; | ||||
| 			const data = [{ }]; | ||||
|  | ||||
| 			await db.setObjectBulk(keys, data); | ||||
| 			const result = await db.getObjects(keys); | ||||
| 			assert.deepStrictEqual(result, [null]); | ||||
| 		}); | ||||
|  | ||||
| 		it('should not error if object is empty', async () => { | ||||
| 			const keys = ['bulkKey6', 'bulkKey7']; | ||||
| 			const data = {}; | ||||
|  | ||||
| 			await db.setObject(keys, data); | ||||
| 			const result = await db.getObjects(keys); | ||||
| 			assert.deepStrictEqual(result, [null, null]); | ||||
| 		}); | ||||
|  | ||||
| 		it('should not error if object is empty', async () => { | ||||
| 			await db.setObject('emptykey', {}); | ||||
| 			const result = await db.getObject('emptykey'); | ||||
| 			assert.deepStrictEqual(result, null); | ||||
| 		}); | ||||
| 	}); | ||||
|  | ||||
| 	describe('setObjectField()', () => { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user