缓存 - IndexedDB - Dexie.js

Classes

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

Operators & filters

  • WhereClause(表查询时用于索引或主键上的筛选器)
  • Collection(表中数据的操作)

Quick Reference(快速参考)

Declare Database(声明数据库)

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

Schema Syntax(表模式的语法)

  • ++Auto-incremented primary key(自动递增的主键)
  • &Unique(独一无二的索引)
  • *Multi-entry index(值为数组的索引,可以通过数组中的某一项索引)
  • [A+B]Compound index(联合索引?)

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(绑定类)

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(添加)

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

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

Update Items(更新)

// 根据对象更新
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(删除)

猜你喜欢

转载自www.cnblogs.com/qq3279338858/p/10980944.html