キャッシュ - のIndexedDB - Dexie.js

クラス

  • Dexie
  • DexieError
  • コレクション
  • IndexSpec
  • 約束する
  • TableSchema
  • トランザクション
  • バージョン
  • WhereClause

演算子&フィルター

  • WhereClause(主キーまたはインデックスにスクリーニングするためのテーブル作成クエリ)
  • コレクション(表中の運転データ)

クイックリファレンス(クイックリファレンス)

データベースを宣言します(database文)

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

スキーマ構文(シンタックステーブルモード)

  • ++自動インクリメントの主キー(自動インクリメントの主キー)
  • &ユニーク(一意のインデックス)
  • *マルチエントリインデックス(配列のインデックスで配列インデックスの値)
  • [A+B]複合インデックス(共同インデックス?)

アップグレード(アップグレード)

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 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);

(追加)項目を追加

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

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

アップデート項目(更新)

// 根据对象更新
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});

アイテム(削除)を削除します。

おすすめ

転載: www.cnblogs.com/qq3279338858/p/10980944.html