[React Native] - Realm Database

June 07, 2021 |

 

REACT NATIVE - REALM DATABASE

Ref:
1. https://docs.mongodb.com/realm-legacy/docs/javascript/latest.html#examples
2. To do list example: https://hellokoding.com/todo-app-with-react-native-realm/

1. Open Realms

//Schema
const PersonSchema = {
  name: 'Person',
  properties: {
    realName:    'string', // required property
    displayName: 'string?', // optional property
    birthday:    {type: 'date', optional: true}, // optional property
  }
};
// Get the default Realm with support for our objects
Realm.open({schema: [Car, Person]})
  .then(realm => {
    // ...use the realm instance here
  })
  .catch(error => {
    // Handle the error here if something went wrong
  });
// Open a realm at another path
Realm.open({
  path: 'anotherRealm.realm',
  schema: [CarSchema]
}).then(/* ... */);


2. Get the current schema version

// Update to to the new schema
Realm.open({schema: [UpdatedPersonSchema], schemaVersion: 1});
//Get current version
const currentVersion = Realm.schemaVersion(Realm.defaultPath);


2. List of properties

realm.write(() => {
  let charlie = realm.create('Person', {
    name: 'Charlie',
    testScores: [100.0]
  });

  // Charlie had an excused absense for the second test and was allowed to skip it
  charlie.testScores.push(null);

  // And then he didn't do so well on the third test
  charlie.testScores.push(70.0);
});


3. Relationship
* To-One Relationship

const PersonSchema = {
  name: 'Person',
  properties: {
    // The following property definitions are equivalent
    car: {type: 'Car'},
    van: 'Car',
  }
};

realm.write(() => {
  const nameString = person.car.name;
  person.car.miles = 1100;

  // create a new Car by setting the property to an object
  // with all of the required fields
  person.van = {make: 'Ford', model: 'Transit'};

  // set both properties to the same car instance
  person.car = person.van;
});

* To-Many Relationships

const PersonSchema = {
  name: 'Person',
  properties: {
    // The following property definitions are equivalent
    cars: {type: 'list', objectType: 'Car'},
    vans: 'Car[]'
  }
}

let carList = person.cars;

// Add new cars to the list
realm.write(() => {
  carList.push({make: 'Honda', model: 'Accord', miles: 100});
  carList.push({make: 'Toyota', model: 'Prius', miles: 200});
});

let secondCar = carList[1].model;  // access using an array index


4. Query

* Get max value in Collection List.
For example: you want to get max of id Person in List. you can do like that

const personList = realm ? realm.objects('Personal') : null;

let idIdx = 0;
if (personList != null && personList.length != 0) {
   idIdx = personList.max('id');
   idIdx++;
}

* Passed para to query
Each subsequent argument is used by the placeholders (e.g. $0, $1, $2, …) in the query.

let person = personList.filtered('id=$0', id);


is updating















Read more…

[English] - TOEIC

June 03, 2021 |

 


Tổng hợp ngữ pháp trong TOEIC

I. Từ vựng
1. adhered (v) /əd'hiə/ dính chặt vào, tham gia, giữ vững
2. breached (v)/bri:tʃ/ vi phạm
3. unfavorable (a) Không thuận lợi
4. come as no surpise : chẳng có gì ngạc nhiên
5. come across: tình cờ gặp
6. tobe + about + to : chuẩn bị làm gì
7. A wide variety : Rất nhiều
8. tailored (a) phù hợp


II. Ngữ pháp
1. Đứng sau danh từ
    Prior to + N ": trước đó
    Rather than + V_ing/N : thay vì
    Owning to + N
2. Since/Because + clause
3. With/toward: không đi với ngày tháng

4. Cấu trúc đảo ngữ câu điều kiện (if)

If + S1 + V (hiện tại), S2 + will/may/might/should/can… + V (infinitive)
=> Should + S1 + (not)+ V (hiện tại), S2 + will/may/might/should/can… + V (infinitive)


If + S1 + V (quá khứ), S2 + would/might/could… + V (infinitive)
=> Were + S1 + (not) + O, S2 + would/might/could… + V (infinitive)

If + S1 + had + past participle, S2 + would/might/could… + have + past participle
=> Had + S1 + (not) + past participle, S2 + would/might/could… + have + past participle


5. many/much/very/more
Many: đi với danh từ đếm được
Much: đi với danh từ không đếm được
More: dùng trong câu so sánh
Very: đứng trước tính từ

6. If so : trước dấu "." và sau dấu ","   : Nếu vậy
Ex: They think she may try to phone. If so, someone must stay here.

7. As ... as
N/Adj/Phrase/Clause + as well as + N/Adj/Phrase/Clause.
as well as + V:
ex: John can ride the motorbike as well as ride the car.

N + as well as + N:
ex: My mom as well as my dad, both expected me to graduate.

as far as : theo như
ex: as far as the latest announcement, we will be off 4 consecutive days.

as good as : gần như
ex: as well as no one is in here.

as much as: gần như là, hầu như là, dường như
ex: after studying hard, Mike as well as finished the knowledge.

as long as : miễn là
ex: as soon as I received the test results, I immediately informed my mother.

as early  as: ngay từ khi
ex: I fell in love with Anna as early as I met her.


Read more…