Skip to content

Commit

Permalink
db version of Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
acav-twilio committed Jun 22, 2020
1 parent f838c62 commit 6b48905
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 112 deletions.
13 changes: 9 additions & 4 deletions expert-db/expert-add → expert-db/expert-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ exports.handler = function(context, event, callback) {
client.sync.services(sync_id)
.syncMaps('experts')
.syncMapItems
.create({key: expert.number , data: expert_json })
.create({key: expert.number, data: expert_json })
.then(sync_map_item => {
console.log(sync_map_item.key)
callback(sync_map_item.key);
callback(null,sync_map_item.key);
})
.catch(e => {
console.log(e);
callback(false);

if(e.code==54208)
callback(null,expert.number);
else
callback(e);

});
//callback(null, response);
};
};
6 changes: 3 additions & 3 deletions expert-db/expert-fetch → expert-db/expert-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ exports.handler = function(context, event, callback) {
.fetch()
.then(sync_map_item => {
console.log(sync_map_item.key)
callback(JSON.stringify(sync_map_item.data));
callback(null,JSON.stringify(sync_map_item.data));
})
.catch(e => {
console.log(e);
callback(false);
callback(null,false);
});

//callback(null, response);
};
};
6 changes: 3 additions & 3 deletions expert-db/expert-list → expert-db/expert-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ exports.handler = function(context, event, callback) {
console.log(s.key);
array.push(s.data);
})
callback(JSON.stringify(array));
callback(null,JSON.stringify(array));

})
.catch(e => {
console.log(e);
callback(false);
callback(null,false);
});

};
};
File renamed without changes.
60 changes: 0 additions & 60 deletions expert-db/expert-table-create

This file was deleted.

60 changes: 60 additions & 0 deletions expert-db/expert-table-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
exports.handler = function(context, event, callback) {

// take the sync_id
//create map with name "experts""
//what do we return???

const sync_id = event['service'];

console.log('CONTEXT:');
for(var property in context) {
console.log(property + "=" + context[property]);
}

console.log('EVENT:');
for(var proper in event) {
console.log(proper + "=" + event[proper]);
}
let response = "{}";

/* fetch service hosting our DB */
const client = context.getTwilioClient();


client.sync.services(sync_id)
.syncMaps
.create({ uniqueName: 'experts'})
.then(sync_map => {
//happy path
console.log(sync_map.sid);
response = "{experts_id: '"+ sync_map.sid +"'}";
callback(null,response);
})
.catch(e => {
//manage unhapy path
console.log(e);
if(e=="Error: Unique name already exists")
{
client.sync.services(sync_id)
.syncMaps
.list({limit: 20})
.then(syncMaps => {
syncMaps.forEach(s => {
console.log(s.sid)
if(s.uniqueName == "experts")
callback(null,"{experts_id: '"+ s.sid +"'}");
});
callback(null,JSON.stringify(syncMaps)); //it should not get here
}

)
.catch(e => {
//manage unhapy path
console.log(e);
callback(e);
});
}
else
callback(e);
});
};
File renamed without changes.
42 changes: 0 additions & 42 deletions expert-db/expert-update

This file was deleted.

42 changes: 42 additions & 0 deletions expert-db/expert-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
exports.handler = function(context, event, callback) {

// take the sync_id
//create map with name "experts""
//what do we return???

const sync_id = event['service'];
const expert_json = event['expert'];
const expert = JSON.parse(expert_json);

//HAPPY PATH

console.log('CONTEXT:');
for(var property in context) {
console.log(property + "=" + context[property]);
}

console.log('EVENT:');
for(var proper in event) {
console.log(proper + "=" + event[proper]);
}
let response = "{}";

/* fetch service hosting our DB */
const client = context.getTwilioClient();


client.sync.services(sync_id)
.syncMaps('experts')
.syncMapItems(expert.number)
.update({data: expert_json })
.then(sync_map_item => {
console.log(sync_map_item.key)
callback(null,sync_map_item.key);
})
.catch(e => {
console.log(e);
callback(null,false);
});

//callback(null, response);
};

0 comments on commit 6b48905

Please sign in to comment.