zoukankan      html  css  js  c++  java
  • [Node.js] Use Realm Object Database with Node.js

    Realm is an ACID compliant object database. In this lesson, you will learn how to install Realm, define schemas for your data, perform CRUD operations and persist your data to the filesystem.

    var Realm = require('realm');
    
    const PetShema = {
        name: 'Pet',
        properties: {
            name: 'string',
            species: 'string',
            age: 'int',
            hasFur: 'bool'
        }
    };
    
    const PersonSchema = {
        name: 'Person',
        primaryKey: 'id',
        properties: {
            id: 'int',
            name: 'string',
            birthday: 'date',
            pet: {type: 'Pet'}
        }
    };
    
    let realm = new Realm({
        path: './people.realm',
        schema: [PetShema, PersonSchema]
    });
    
    realm.write(() => {
        realm.create('Person', {
            id: 0,
            name: 'Bpb',
            birthday: new Date('2001-10-10'),
            pet: {
                name: 'Fluffy',
                species: 'Cat',
                age: 4,
                hasFur: true
            }
        })
    });
    
    realm.write(() => {
    
        realm.create('Person', {
            id: 1,
            name: 'Tom',
            birthday: new Date('1989-12-17'),
            pet: {
                name: 'Umi',
                species: 'Dog',
                age: 5,
                hasFur: true
            }
        });
    });
    
    
    let people = realm.objects('Person');
    let pets = realm.objects('Pet');
    
    console.log("people", JSON.stringify(people, null, 2));
    console.log("pets", JSON.stringify(pets, null, 2));
    
    let filteredPets = pets.filtered('age < 5');
    console.log("filteredPets", JSON.stringify(filteredPets, null, 2));
    
    let multiFilterPets = pets.filtered('hasFur = true AND name BEGINSWITH "F"');
    console.log("multiFilterPets", JSON.stringify(multiFilterPets, null, 2));
    
    let sortedAge = pets.sorted('age');
    console.log("sortedAge", JSON.stringify(sortedAge, null, 2));
    
    // Overwrite the existing Person
    realm.write(() => {
        realm.create('Person', {id: 0, name: 'Jake'}, true)
    });
    console.log("Person", JSON.stringify(people, null, 2));
    
    // delete
    let person = realm.objects('Person').filtered('id = 0');
    realm.write(() => {
        realm.delete(person);
    });
    console.log("After delete", JSON.stringify(people, null, 2));

    The data is stored in the local files:

    Github

  • 相关阅读:
    CodeForces
    bzoj 2257: [Jsoi2009]瓶子和燃料
    【NOIP2009】Hankson 的趣味题
    51Nod 1203 JZPLCM
    bzoj 3751: [NOIP2014]解方程
    UOJ #11. 【UTR #1】ydc的大树
    Tenka1 Programmer Contest D
    bzoj 5000: OI树
    bzoj 1407: [Noi2002]Savage
    bzoj 3551: [ONTAK2010]Peaks加强版
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6111469.html
Copyright © 2011-2022 走看看