Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. var fetch = require("node-fetch");
  2. var CloudKit = require("./cloudkit.js");
  3.  
  4.  
  5. // Get TIME
  6. var d = new Date();
  7. var m = d.getMinutes()/10;
  8. var t = d.getHours() + ":" + Math.floor(d.getMinutes()/10) + "0";
  9.  
  10. // Get WEEKDAY
  11. var weekday = ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"];
  12. var w = weekday[d.getDay()].toUpperCase();
  13.  
  14. CloudKit.configure({
  15. services: { fetch: fetch },
  16. containers: [{
  17. containerIdentifier: 'CONTAINER',
  18. apiTokenAuth : {
  19. apiToken : "API_TOKEN",
  20. persist : true
  21. },
  22. environment: 'development',
  23. }]
  24. });
  25.  
  26. var container = CloudKit.getDefaultContainer();
  27. var publicDB = container.publicCloudDatabase;
  28.  
  29. var query = {
  30. recordType: "Availability",
  31. filterBy: [
  32. {comparator: "EQUALS", fieldName: "weekday", fieldValue: { value: "THURSDAY"}},
  33. {comparator: "EQUALS", fieldName: "time", fieldValue: {value: "13:00"}},
  34. ]
  35. };
  36.  
  37. // Set the actual weekday and time for the query
  38. query['filterBy'][0].fieldValue.value = w;
  39. query['filterBy'][1].fieldValue.value = t;
  40.  
  41. function demoPerformQuery() {
  42. publicDB.performQuery(query).then(function(response){
  43. var records = response.records;
  44. var numberOfRecords = records.length;
  45.  
  46. if(numberOfRecords == 0) {
  47. // No registers in CloudKit, create them!
  48.  
  49. var record = {
  50. recordName : "Test Record",
  51. recordType: "Availability",
  52. fields: {
  53. "time" : {value: "XX:XX"}
  54. }
  55. };
  56.  
  57. publicDB.saveRecords(record).then(function(response) {
  58.  
  59. if(response.hasErrors) {
  60. var ckError = response.errors[0];
  61. throw ckError;
  62. } else {
  63. console.log(response);
  64. var record = response.records[0];
  65. }
  66. });
  67. } else {
  68. records.forEach(function(record) {
  69. var fields = record.fields;
  70. var monday = fields['freeBikes'];
  71. console.log(monday.value);
  72. });
  73. }
  74. }).catch(function(error){
  75. console.log(error)
  76. })
  77. }
  78.  
  79. demoPerformQuery();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement