/
save-to-storage.js
55 lines (39 loc) · 1.52 KB
/
save-to-storage.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
module.exports = {
friendlyName: 'Save to Storage',
description: 'Save a key-value pair in string format to the persistent data store. This is compatible with PhoneGap/Cordova',
extendedDescription: 'This machinepack provides an easy-to-use key/value pair-based database using the HTML5 localStorage model. With some HTTP requests you should be able to effectively use this as a replacement for cookies on PhoneGap/Cordova-based apps.',
inputs: {
key: {
example: 'user',
description: 'The name of the key you wish to save to the unique session.',
required: true
},
value: {
example: 'NileFrater',
description: 'The value of the key you wish to save to the datastore. Only supports strings.',
required: true
}
},
defaultExit: 'success',
exits: {
error: {
description: 'An unexpected error occurred.'
},
success: {
example: {
friendlyName: 'then',
desription: 'Successfully saved data to the persistent datastore.'
}
}
},
fn: function (inputs, exits) {
// Making sure we don't wipe the local storage everytime we write to it.
if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
}
localStorage.setItem(inputs.key, inputs.value);
// Let the server know the data has been saved successfully and return the approproate exit.
return exits.success(inputs.value);
}
};