-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpersistence_api_aws.js
66 lines (62 loc) · 2.24 KB
/
persistence_api_aws.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const DatbaseDriver = require('s3-db');
const database = new DatbaseDriver({
db: {
name: 'devup-serverless-demo',
environment: 'dev',
allowDrop: true
},
provider: {
name: 'aws-s3',
region: 'us-west-2'
}
});
module.exports.SaveUser = (event, context, callback) => {
var response = {
statusCode: 200,
headers: { "Content-Type": "application/json" },
};
database.getCollection('users', { id: { propertyName: 'name' } })
.catch(reason => {
console.error(reason);
console.log('Creating Collection users ...');
return database.createCollection('users');
})
.then(collection => {
console.log('Attempting to Save Record to Collection: ' + collection.getName());
return collection.saveDocument(JSON.parse(event.body));
})
.catch(reason => {
console.error('Unable to Save Document: ' + reason);
response.statusCode = 400;
response.body = JSON.stringify(reason);
return callback(reason, response);
})
.then(document => {
console.log('Save Complete!');
response.body = JSON.stringify(document);
return callback(null, response);
});
};
module.exports.FetchUser = (event, context, callback) => {
console.log('Finding User For Name:' + event.pathParameters.id);
database.getCollection('users', { id: { propertyName: 'name' } })
.then(collection => collection.getDocument(event.pathParameters.id))
.then(document => {
console.info('User Name: ' + document.name);
var response = {
statusCode: "200",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(document)
};
return callback(null, response);
})
.catch((reason) => {
console.error("User Document Not Found For Id" + event.pathParameters.id);
var response = {
statusCode: "404",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(reason)
};
return callback(reason, response);
});
};