Skip to content

Commit

Permalink
feat: add ability to pass in objectTypeId via options
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegraw committed Feb 4, 2021
1 parent 4b6a8a4 commit 0655214
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/palantir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
};

Expand Down Expand Up @@ -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);
Expand All @@ -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
}
Expand All @@ -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);
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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, '_');
}

/*!
Expand Down

0 comments on commit 0655214

Please sign in to comment.