| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | module.exports = function (module) { | 
					
						
							| 
									
										
										
										
											2020-07-02 20:11:53 -04:00
										 |  |  | 	const helpers = require('./helpers'); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.flushdb = async function () { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		await module.client.dropDatabase(); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.emptydb = async function () { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		await module.client.collection('objects').deleteMany({}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		module.objectCache.resetObjectCache(); | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.exists = async function (key) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-03 23:23:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 20:40:40 -05:00
										 |  |  | 		if (Array.isArray(key)) { | 
					
						
							| 
									
										
										
										
											2020-07-03 23:23:42 -04:00
										 |  |  | 			const data = await module.client.collection('objects').find({ | 
					
						
							|  |  |  | 				_key: { $in: key }, | 
					
						
							|  |  |  | 			}, { _id: 0, _key: 1 }).toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const map = {}; | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			data.forEach(function (item) { | 
					
						
							|  |  |  | 				map[item._key] = true; | 
					
						
							| 
									
										
										
										
											2018-11-21 20:40:40 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			return key.map(key => !!map[key]); | 
					
						
							| 
									
										
										
										
											2018-11-21 20:40:40 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-03 23:23:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const item = await module.client.collection('objects').findOne({ | 
					
						
							|  |  |  | 			_key: key, | 
					
						
							|  |  |  | 		}, { _id: 0, _key: 1 }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return item !== undefined && item !== null; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 20:11:53 -04:00
										 |  |  | 	module.scan = async function (params) { | 
					
						
							|  |  |  | 		const match = helpers.buildMatchQuery(params.match); | 
					
						
							|  |  |  | 		return await module.client.collection('objects').distinct( | 
					
						
							|  |  |  | 			'_key', { _key: { $regex: new RegExp(match) } } | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.delete = async function (key) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		await module.client.collection('objects').deleteMany({ _key: key }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		module.objectCache.delObjectCache(key); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.deleteAll = async function (keys) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!Array.isArray(keys) || !keys.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		await module.client.collection('objects').deleteMany({ _key: { $in: keys } }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		module.objectCache.delObjectCache(keys); | 
					
						
							| 
									
										
										
										
											2014-09-07 03:17:51 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.get = async function (key) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-07 15:04:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const objectData = await module.client.collection('objects').findOne({ _key: key }, { projection: { _id: 0 } }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// fallback to old field name 'value' for backwards compatibility #6340
 | 
					
						
							|  |  |  | 		var value = null; | 
					
						
							|  |  |  | 		if (objectData) { | 
					
						
							|  |  |  | 			if (objectData.hasOwnProperty('data')) { | 
					
						
							|  |  |  | 				value = objectData.data; | 
					
						
							|  |  |  | 			} else if (objectData.hasOwnProperty('value')) { | 
					
						
							|  |  |  | 				value = objectData.value; | 
					
						
							| 
									
										
										
										
											2018-03-03 12:31:20 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return value; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.set = async function (key, value) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-03 23:23:42 -04:00
										 |  |  | 		await module.setObject(key, { data: value }); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.increment = async function (key) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-03 23:23:42 -04:00
										 |  |  | 		const result = await module.client.collection('objects').findOneAndUpdate({ | 
					
						
							|  |  |  | 			_key: key, | 
					
						
							|  |  |  | 		}, { $inc: { data: 1 } }, { returnOriginal: false, upsert: true }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return result && result.value ? result.value.data : null; | 
					
						
							| 
									
										
										
										
											2014-05-11 11:45:20 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.rename = async function (oldKey, newKey) { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		await module.client.collection('objects').updateMany({ _key: oldKey }, { $set: { _key: newKey } }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		module.objectCache.delObjectCache([oldKey, newKey]); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.type = async function (key) { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const data = await module.client.collection('objects').findOne({ _key: key }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		if (!data) { | 
					
						
							|  |  |  | 			return null; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		delete data.expireAt; | 
					
						
							|  |  |  | 		var keys = Object.keys(data); | 
					
						
							|  |  |  | 		if (keys.length === 4 && data.hasOwnProperty('_key') && data.hasOwnProperty('score') && data.hasOwnProperty('value')) { | 
					
						
							|  |  |  | 			return 'zset'; | 
					
						
							|  |  |  | 		} else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('members')) { | 
					
						
							|  |  |  | 			return 'set'; | 
					
						
							|  |  |  | 		} else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('array')) { | 
					
						
							|  |  |  | 			return 'list'; | 
					
						
							|  |  |  | 		} else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('data')) { | 
					
						
							|  |  |  | 			return 'string'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 'hash'; | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.expire = async function (key, seconds) { | 
					
						
							|  |  |  | 		await module.expireAt(key, Math.round(Date.now() / 1000) + seconds); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.expireAt = async function (key, timestamp) { | 
					
						
							|  |  |  | 		await module.setObjectField(key, 'expireAt', new Date(timestamp * 1000)); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-05-11 10:31:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.pexpire = async function (key, ms) { | 
					
						
							|  |  |  | 		await module.pexpireAt(key, Date.now() + parseInt(ms, 10)); | 
					
						
							| 
									
										
										
										
											2014-05-11 10:31:32 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.pexpireAt = async function (key, timestamp) { | 
					
						
							| 
									
										
										
										
											2017-05-09 15:07:59 -04:00
										 |  |  | 		timestamp = Math.min(timestamp, 8640000000000000); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		await module.setObjectField(key, 'expireAt', new Date(timestamp)); | 
					
						
							| 
									
										
										
										
											2014-05-11 10:31:32 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2017-02-18 02:30:48 -07:00
										 |  |  | }; |