DBhelper.js 2.23 KB
// Define your models and their properties
import Realm from 'realm'

class Waste {}
Waste.schema = {
  name: 'Waste',
  properties: {
    weight: 'int',
    diff: 'int',
    date: 'float',
  }
};

// Get the default Realm with support for our objects
let realm = new Realm({schema: [Waste]});

// // Create Realm objects and write to local storage
// realm.write(() => {
//   let myWaste = realm.create('Waste', {
//     weight: 1655,
//     diff: 500,
//     date: '1576236841329.253',
//   });
// //   myWaste.miles += 2SS0; // Update a property value
// });

// // Query Realm for all cars with a high mileage
// let wastes = realm.objects('Waste').filtered('weight > 1000');

// // Will return a Results object with our 1 car
// cars.length // => 1

// // Add another car
// realm.write(() => {
//   let myCar = realm.create('Car', {
//     make: 'Ford',
//     model: 'Focus',
//     miles: 2000,
//   });
// });
// // Query results are updated in realtime
// cars.length // => 2

export function add_data(responseJson){
    for(i = 0; i < responseJson.length; i++){
        realm.write(() => {
            // responseJson[0].ras_id.S
            let myWaste = realm.create('Waste', {
                weight: Number(responseJson[i].weight.N),
                diff: Number(responseJson[i].diff.N),
                date: Number(responseJson[i].save_date.S),
            });
          });
    }

    return responseJson.length;
}                                       

export function get_now_weight(){
    let wastes = realm.objects('Waste');
    let recent = realm.objects('Waste').max('date').date;
    let recentWeight = wastes.filtered('date = 1576223063238.5166');
    //"id == %@", id!
    console.log(String(wastes.lenth));
    return recentWeight[0].weight;
}

export function get_recent_date(){
    
    return realm.objects('Waste').max("date");
}

export function get_today_data(today){
  let wastes = realm.objects('Waste');
  var list = [];
  for(var i =0; i<wastes.length; i++){
    if(get_date(wastes[i].date) == get_date(today)){
      temp = [waste[i].date, waste[i].diff, waste[i].weight]
      list.append(temp);
    } 
  }
  return list;
}

var get_date = function (date){
  return pad(date.getFullyear(),4)+pad(date.getMonth()+1, 2)+ pad(date.getDate(), 2)
}