๋ฌธ์„œ ๋ฉ”๋‰ด
๋ฌธ์„œ ํ™ˆ
/ /
Atlas Device SDK
/ /

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์˜ ๊ฐœ ๋ชฉ๋ก์„ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

๋Œ์•„๊ฐ€๊ธฐ

์ฝ๊ธฐ

๋‹ค์Œ

์‚ญ์ œ