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

  • 相关阅读:
    url路由配置及渲染方式
    django类视图介绍与类视图装饰器
    什么是数据类型
    python代码的编写和运行
    python环境搭建
    python教程(目录)
    编程语言概念
    面向对象入门
    编程语言
    Tornado框架实现图形验证码功能
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6111469.html
Copyright © 2011-2022 走看看