From 065521456b4e7cd4f7a8f676b324b2d6ea8b011c Mon Sep 17 00:00:00 2001 From: mdegraw Date: Wed, 3 Feb 2021 23:52:07 -0700 Subject: [PATCH] feat: add ability to pass in objectTypeId via options --- lib/palantir.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/palantir.js b/lib/palantir.js index 8fd4b0c..a48616b 100644 --- a/lib/palantir.js +++ b/lib/palantir.js @@ -67,10 +67,14 @@ PalantirConnector.prototype.create = function(modelName, data, options, callback const pkPropertyName = this.getPrimaryKey(modelName); const uniquePropName = this.getUniquePropertyName(modelName); const pkPropertyValue = md5(standardizeName(data[uniquePropName])); - const palantirProperties = Object.assign(this.toPalantirProperties(modelName, data), {policy: this.settings.policy}); + const props = this.toPalantirProperties(modelName, data); + const palantirProperties = options.noPolicy ? props : Object.assign( + props, + {policy: this.settings.policy}, + ); const body = { locator: { - typeId: this.settings.objectType, + typeId: options.objectTypeId || this.settings.objectType, primaryKey: { [pkPropertyName]: pkPropertyValue } @@ -101,7 +105,7 @@ PalantirConnector.prototype.all = function(modelName, filter, options, callback) debug('get object by id'); const pkPropertyName = this.getPrimaryKey(modelName); const body = { - typeId: this.settings.objectType, + typeId: options.objectTypeId || this.settings.objectType, primaryKey: { [pkPropertyName]: id } @@ -120,7 +124,7 @@ PalantirConnector.prototype.all = function(modelName, filter, options, callback) const pageSize = options && options.pageSize >= 0 ? options.pageSize : 10000; const url = `${PALANTIR_PATHS.objectSearch}?pageSize=${pageSize}`; const body = { - objectTypes: [this.settings.objectType], + objectTypes: [options.objectTypeId || this.settings.objectType], filter: this.buildWhere(modelName, filter.where) }; @@ -158,7 +162,7 @@ PalantirConnector.prototype.count = function count( callback ) { debug('count', modelName, where); - const body = {objectTypes: [this.settings.objectType]}; + const body = {objectTypes: [options.objectTypeId || this.settings.objectType]}; body.filter = this.buildWhere(modelName, where); const url = `${PALANTIR_PATHS.objectSearch}?pageSize=0`; debug('request: ', 'POST', url, body); @@ -184,7 +188,7 @@ PalantirConnector.prototype.destroyAll = function(modelName, where, options, cal if (id) { const body = { locator: { - typeId: this.settings.objectType, + typeId: options.objectTypeId || this.settings.objectType, primaryKey: { [pkPropertyName]: id } @@ -197,8 +201,8 @@ PalantirConnector.prototype.destroyAll = function(modelName, where, options, cal } }; axios.post(PALANTIR_PATHS.objectLocator, body) - .then((response) => { - callback(); + .then(() => { + callback(null, {count: 1}); }) .catch(err => { callback(err); @@ -208,7 +212,7 @@ PalantirConnector.prototype.destroyAll = function(modelName, where, options, cal const body = _.map(results, (result) => { return { locator: { - typeId: this.settings.objectType, + typeId: options.objectTypeId || this.settings.objectType, primaryKey: { [pkPropertyName]: result.id } @@ -252,7 +256,7 @@ PalantirConnector.prototype.replaceById = function replace( const palantirProperties = _.omit(this.toPalantirProperties(modelName, data), pkPropertyName); const body = { locator: { - typeId: this.settings.objectType, + typeId: options.objectTypeId || this.settings.objectType, primaryKey: { [pkPropertyName]: id } @@ -295,7 +299,7 @@ PalantirConnector.prototype.update = PalantirConnector.prototype.updateAll = fun const body = _.map(results, (result) => { return { locator: { - typeId: this.settings.objectType, + typeId: options.objectTypeId || this.settings.objectType, primaryKey: { [pkPropertyName]: result.id } @@ -321,10 +325,10 @@ PalantirConnector.prototype.update = PalantirConnector.prototype.updateAll = fun }; function standardizeName(name) { - return name.toLowerCase() - .replace('ncgca', '') - .trim() - .replace(/ /g, '_'); + return _.toString(name).toLowerCase() + .replace('ncgca', '') + .trim() + .replace(/ /g, '_'); } /*!