cool-data-manager
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -17,5 +17,7 @@ const Promise = require('bluebird'); | ||
.then(mutation => { | ||
// console.log('mutation', mutation); | ||
return this.client.mutate(`{${mutation}}`, this.options) | ||
}) | ||
.then(result => { | ||
// console.log('result', result); | ||
return result[`create${this.entityInfo.entityName}`]; | ||
@@ -35,3 +37,3 @@ }) | ||
input: buildInputFieldList(this.entityInfo.fields, entity), | ||
output: buildOutputFieldList(this.entityInfo.fields) | ||
output: buildOutputFieldList(this.entityInfo.fields, this.options) | ||
}) | ||
@@ -38,0 +40,0 @@ .then(fields => { |
const Promise = require('bluebird'); | ||
const validateRequiredFields = require('./validateRequiredFields'); | ||
const valueWrappers = require('./valueWrappers'); | ||
const CoolRelation = require('../../../coolRelation'); | ||
const CoolCollection = require('../../../coolCollection'); | ||
@@ -16,8 +18,41 @@ function buildInputList(fields, entity) { | ||
if (entity[fieldName] !== undefined) { | ||
const type = fields[fieldName].type; | ||
const value = valueWrappers[type](entity[fieldName]); | ||
return acc.concat(`${fieldName}: ${value}, | ||
`); | ||
const field = fields[fieldName]; | ||
const type = field.type; | ||
if (type instanceof CoolRelation){ | ||
const subEntityInfo = type.coolDataManager.entityInfo; | ||
return buildInputList(subEntityInfo.fields, entity[fieldName]) | ||
.then(subQuery => { | ||
return acc.concat(`${fieldName}: { | ||
${subQuery} | ||
}, | ||
`); | ||
}) | ||
} else if (type instanceof CoolCollection) { | ||
const subEntityInfo = type.coolDataManager.entityInfo; | ||
return Promise.mapSeries( | ||
entity[fieldName], | ||
subEntity => { | ||
return buildInputList(subEntityInfo.fields, subEntity) | ||
.then(subEntity => { | ||
return `{ | ||
${subEntity} | ||
},` | ||
}) | ||
} | ||
) | ||
.then(subQuery => { | ||
return acc.concat(`${fieldName}: [ | ||
${subQuery} | ||
], | ||
`); | ||
}); | ||
} else { | ||
const value = valueWrappers[type](entity[fieldName]); | ||
return Promise.resolve(acc.concat(`${fieldName}: ${value}, | ||
`)) | ||
} | ||
} else { | ||
return acc; | ||
return Promise.resolve(acc); | ||
} | ||
@@ -24,0 +59,0 @@ }, |
const Promise = require('bluebird'); | ||
const clog = require('fbkt-clog'); | ||
const CoolRelation = require('../../../coolRelation'); | ||
const CoolCollection = require('../../../coolCollection'); | ||
function buildOutputList(fields) | ||
function buildOutputList(fields, options) | ||
{ | ||
const fieldsArray = fields instanceof Array ? fields : Object.keys(fields); | ||
const getFields = (options || {}).getFields || []; | ||
const fieldsArray = Object.keys(fields).filter(field => { | ||
return getFields.length > 0 ? | ||
getFields.indexOf(field) !== -1 : | ||
true; | ||
}); | ||
@@ -10,4 +18,18 @@ return Promise.reduce( | ||
(acc, fieldName) => { | ||
return acc.concat(` ${fieldName}, | ||
`); | ||
const field = fields[fieldName]; | ||
if (field.type instanceof CoolRelation || field.type instanceof CoolCollection) { | ||
const subEntityInfo = field.type.coolDataManager.entityInfo; | ||
return buildOutputList(subEntityInfo.fields, options) | ||
.then(subQuery => { | ||
return acc.concat(`${fieldName} { | ||
${subQuery} | ||
}, | ||
`); | ||
}) | ||
// } else if (field.type instanceof CoolCollection) { | ||
// | ||
} else { | ||
return Promise.resolve(acc.concat(` ${fieldName}, | ||
`)); | ||
} | ||
}, | ||
@@ -14,0 +36,0 @@ '' |
@@ -28,6 +28,4 @@ const Promise = require('bluebird'); | ||
buildQuery() { | ||
const fields = this.options.getFields || this.entityInfo.fields; | ||
return Promise.props({ | ||
output: buildOutputFieldList(fields, this.options) | ||
output: buildOutputFieldList(this.entityInfo.fields, this.options) | ||
}) | ||
@@ -34,0 +32,0 @@ .then(fields => { |
@@ -10,3 +10,3 @@ const ClientWrapper = require('./clientWrapper'); | ||
class graphCoolEntityManager{ | ||
class CoolDataManager{ | ||
constructor(entityInfo, client, options){ | ||
@@ -64,2 +64,2 @@ this.entityInfo = entityInfo; | ||
module.exports = graphCoolEntityManager; | ||
module.exports = CoolDataManager; |
@@ -32,3 +32,3 @@ const Promise = require('bluebird') | ||
input: buildInputFieldList(this.entityInfo.fields, entity), | ||
output: buildOutputFieldList(this.entityInfo.fields) | ||
output: buildOutputFieldList(this.entityInfo.fields, this.options) | ||
}) | ||
@@ -35,0 +35,0 @@ .then(fields => { |
{ | ||
"name": "cool-data-manager", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,13 @@ 'use strict'; | ||
dateTimeData: moment.utc().format(), | ||
otherThing: { | ||
stringData: 'OTHER THING DATA' | ||
}, | ||
yetAnotherThings: [ | ||
{ | ||
stringData: 'YET MORE' | ||
}, | ||
{ | ||
stringData: 'Still Yet MOre evEN' | ||
} | ||
] | ||
// jsonData: { | ||
@@ -29,5 +40,7 @@ // id: 1, | ||
entityManager.createOne(testEntity) | ||
entityManager.createOne(testEntity, { | ||
verbose: true | ||
}) | ||
.then(thing => { | ||
// clog('THING', thing); | ||
clog('THING', thing); | ||
expect(thing).to.be.an('object'); | ||
@@ -40,2 +53,4 @@ expect(thing.stringData).to.equal(testEntity.stringData); | ||
expect(moment.utc(thing.dateTimeData).format()).to.equal(testEntity.dateTimeData); | ||
expect(thing.otherThing).to.be.an('object'); | ||
expect(thing.otherThing.stringData).to.equal(testEntity.otherThing.stringData); | ||
// expect(thing.jsonData.id).to.equal(testEntity.jsonData.id); | ||
@@ -42,0 +57,0 @@ // expect(thing.jsonData.child.id).to.equal(testEntity.jsonData.child.id); |
const CoolDataManager = require('../../coolDataManager'); | ||
const CoolRelation = require('../../coolRelation'); | ||
const CoolCollection = require('../../coolCollection'); | ||
const client = require('../client'); | ||
const OtherThing = require('../otherThing'); | ||
const YetAnotherThing = require('../yetAnotherThing'); | ||
const entityInfo = { | ||
@@ -31,2 +37,8 @@ entityName: 'Thing', | ||
}, | ||
otherThing: { | ||
type: new CoolRelation(OtherThing) | ||
}, | ||
yetAnotherThings: { | ||
type: new CoolCollection(YetAnotherThing) | ||
} | ||
// jsonData: { | ||
@@ -33,0 +45,0 @@ // type: 'json' |
28333
27
886