]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/database-utils.ts
Add rate limit to registration and API endpoints
[github/Chocobozzz/PeerTube.git] / server / helpers / database-utils.ts
index 9b861a88ccd3c417bf963ae659979ab57c28482b..39c74b2fdec9d87e791abb3de9be7b44dbc19083 100644 (file)
@@ -21,17 +21,21 @@ function retryTransactionWrapper <T, A> (
   arg1: A
 ): Promise<T>
 
+function retryTransactionWrapper <T> (
+  functionToRetry: () => Promise<T> | Bluebird<T>
+): Promise<T>
+
 function retryTransactionWrapper <T> (
   functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
   ...args: any[]
 ): Promise<T> {
   return transactionRetryer<T>(callback => {
-    functionToRetry.apply(this, args)
+    functionToRetry.apply(null, args)
         .then((result: T) => callback(null, result))
         .catch(err => callback(err))
   })
   .catch(err => {
-    logger.error('Cannot execute %s with many retries.', functionToRetry.toString(), { err })
+    logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
     throw err
   })
 }
@@ -58,13 +62,20 @@ function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model
   const obj = baseInstance.toJSON()
 
   for (const key of Object.keys(obj)) {
-    instanceToUpdate.set(key, obj[key])
+    instanceToUpdate[key] = obj[key]
   }
 }
 
+function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
+  Object.keys(savedFields).forEach(key => {
+    instance[key] = savedFields[key]
+  })
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  resetSequelizeInstance,
   retryTransactionWrapper,
   transactionRetryer,
   updateInstanceWithAnother