Cache - IndexedDB - Dexie.js

Classes

  • Dexie
  • DexieError
  • Collection
  • IndexSpec
  • Promise
  • Table
  • TableSchema
  • Transaction
  • Version
  • WhereClause

Operators & filters

  • WhereClause (Table query for screening on the primary key or index)
  • Collection (operation data in the table)

Quick Reference (Quick Reference)

Declare Database (database statement)

var db = new Dexie("MyDatabase");
db.version(1).stores({
    friends: "++id, name, age, *tags",
    gameSessions: "id, score"
});

Schema Syntax (syntax table mode)

  • ++Auto-incremented primary key (auto-increment primary key)
  • &Unique (unique index)
  • *Multi-entry index (the value of the array index by an index of an array)
  • [A+B]Compound index (joint index?)

Upgrade (upgrade)

db.version(1).stores({
    friends: "++id,name,age,*tags",
    gameSessions: "id,score"
});

db.version(2).stores({
    friends: "++id, [firstName+lastName], yearOfBirth, *tags", // Change indexes(改变索引)
    gameSessions: null // Delete table(删除表)

}).upgrade(tx => {
    // Will only be executed if a version below 2 was installed.(当前浏览器数据库版本低于2时触发)
    return tx.friends.modify(friend => {
        friend.firstName = friend.name.split(' ')[0];
        friend.lastName = friend.name.split(' ')[1];
        friend.birthDate = new Date(new Date().getFullYear() - friend.age, 0);
        delete friend.name;
        delete friend.age;
    });
});

Class Binding (binding class)

class Friend {
    // Prototype method
    save() {
        return db.friends.put(this); // Will only save own props.
    }

    // Prototype property
    get age() { // 这里个get 是类中的一个关键字,new Friend().age时会调用该函数
        return moment(Date.now()).diff (this.birthDate, 'years'); // moment是一个日期处理库
    }
}

db.friends.mapToClass(Friend);

Add Items (add)

await db.friends.add({name: "Josephine", age: 21});

await db.friends.bulkAdd([
  {name: "Foo", age: 31},
  {name: "Bar", age: 32}
]);

Update Items (updated)

// 根据对象更新
await db.friends.put({id: 4, name: "Foo", age: 33});

await db.friends.bulkPut([
    {id: 4, name: "Foo2", age: 34},
    {id: 5, name: "Bar2", age: 44}
]);

// 根据主键更新
await db.friends.update(4, {name: "Bar"});

// 根据搜索结果更新
await db.customers
    .where("age")
    .inAnyRange([ [0, 18], [65, Infinity] ])
    .modify({discount: 0.5});

Delete items (deleted)

Guess you like

Origin www.cnblogs.com/qq3279338858/p/10980944.html