| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Utils functions for BookShelf | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Find primary key | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:31:34 +01:00
										 |  |  |   getPK: (collectionIdentity, collection, models) => { | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |     // This is not a Bookshelf collection, only the name.
 | 
					
						
							|  |  |  |     if (_.isString(collectionIdentity) && !_.isUndefined(models)) { | 
					
						
							| 
									
										
										
										
											2016-11-07 16:31:34 +01:00
										 |  |  |       const PK = _.findKey(_.get(models, collectionIdentity + '.attributes'), o => { | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |         return o.hasOwnProperty('primary'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!_.isEmpty(PK)) { | 
					
						
							|  |  |  |         return PK; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 15:50:33 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |       if (_.isObject(collection)) { | 
					
						
							|  |  |  |         return collection.forge().idAttribute || 'id'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       // Collection undefined try to get the collection based on collectionIdentity
 | 
					
						
							|  |  |  |       if (typeof strapi !== 'undefined') { | 
					
						
							| 
									
										
										
										
											2017-12-13 16:28:50 +01:00
										 |  |  |         collection = _.get(strapi, `bookshelf.collections.${collectionIdentity}`); | 
					
						
							| 
									
										
										
										
											2016-03-22 15:50:33 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 15:50:33 +01:00
										 |  |  |       // Impossible to match collectionIdentity before, try to use idAttribute
 | 
					
						
							|  |  |  |       if (_.isObject(collection)) { | 
					
						
							|  |  |  |         return collection.forge().idAttribute || 'id'; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 'id'; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Find primary key | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:31:34 +01:00
										 |  |  |   getCount: type => { | 
					
						
							|  |  |  |     return strapi.bookshelf.collections[type].forge().count().then(count => count); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-04 22:09:19 +02:00
										 |  |  | }; |