CRUD - ์ ๋ฐ์ดํธ - Node.js SDK
์ด ํ์ด์ง์ ๋ด์ฉ
๊ฐ์ฒด ์ ๋ฐ์ดํธ
๋ค๋ฅธ JavaScript ๊ฐ์ฒด๋ฅผ ์ ๋ฐ์ดํธํ๋ ๊ฒ๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์ Realm ๊ฐ์ฒด์ ์์ฑ์ ์ถ๊ฐ, ์์ , ์ญ์ ํ ์ ์์ต๋๋ค.
// Open a transaction. realm.write(() => { // Get a dog to update. const dog = realm.objects("Dog")[0]; // Update some properties on the instance. // These changes are saved to the realm. dog.name = "Maximilian"; dog.age += 1; });
ํ
๊ด๋ จ ๊ฐ์ฒด ๋ฐ ํฌํจ๋ ๊ฐ์ฒด ์ ๋ฐ์ดํธ
ํฌํจ๋ ๊ฐ์ฒด ๋๋ ๊ด๋ จ ๊ฐ์ฒด ์ ์์ฑ์ ์ ๋ฐ์ดํธํ๋ ค๋ฉด ์ ํ๊ธฐ๋ฒ ๋๋ ๋๊ดํธ ํ๊ธฐ๋ฒ์ ์ฌ์ฉ ํ์ฌ ์์ฑ์ ์์ ํฉ๋๋ค. ๋ง์น ์ผ๋ฐ ์ค์ฒฉ ๊ฐ์ฒด์ ์๋ ๊ฒ์ฒ๋ผ ๋ณด์ ๋๋ค.
๊ฐ์ฒด ์ ์ํธ
๊ฐ์ฒด๋ฅผ ์
์ํธํ๋ ค๋ฉด ์
๋ฐ์ดํธ ๋ชจ๋๋ฅผ modified
๋ก ์ค์ ํ๊ณ Realm.create() ๋ฅผ ํธ์ถํฉ๋๋ค. ์ด ์์
์ ์ง์ ๋ ๊ธฐ๋ณธ ํค๋ฅผ ์ฌ์ฉํ์ฌ ์ ๊ฐ์ฒด๋ฅผ ์ฝ์
ํ๊ฑฐ๋ ํด๋น ๊ธฐ๋ณธ ํค๊ฐ ์ด๋ฏธ ์๋ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
realm.write(() => { // Add a new person to the realm. Since nobody with ID 1234 // has been added yet, this adds the instance to the realm. person = realm.create( "Person", { _id: 1234, name: "Joe", age: 40 }, "modified" ); // If an object exists, setting the third parameter (`updateMode`) to // "modified" only updates properties that have changed, resulting in // faster operations. person = realm.create( "Person", { _id: 1234, name: "Joseph", age: 40 }, "modified" ); });
์ปฌ๋ ์ ๋๋ ์ ๋ฐ์ดํธ
๊ฐ์ฒด ์ปฌ๋ ์ ์ ์ ๋ฐ์ดํธ๋ฅผ ์ ์ฉํ๋ ค๋ฉด ์ปฌ๋ ์ ์ ๋ฐ๋ณตํฉ๋๋ค(์:for...of). ๋ฃจํ์์ ๊ฐ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ์ ๋ฐ์ดํธํฉ๋๋ค.
realm.write(() => { // Create someone to take care of some dogs. const person = realm.create("Person", { name: "Ali" }); // Find dogs younger than 2. const puppies = realm.objects("Dog").filtered("age < 2"); // Loop through to update. for (const puppy of puppies) { // Give all puppies to Ali. puppy.owner = person; } });
์ฐธ๊ณ
์ญ๊ด๊ณ
Dog.owner
์ Person.dogs
์ ์ญ๊ด๊ณ ๋๋ถ์ Realm์ Ali๋ฅผ ๊ฐ์์ง์ ์ฃผ์ธ์ผ๋ก ์ค์ ํ ๋๋ง๋ค Ali์ ๊ฐ ๋ชฉ๋ก์ ์๋์ผ๋ก ์
๋ฐ์ดํธํฉ๋๋ค.