{"id":242,"date":"2020-06-28T13:07:51","date_gmt":"2020-06-28T04:07:51","guid":{"rendered":"http:\/\/localhost:8000\/?p=242"},"modified":"2021-01-16T13:08:08","modified_gmt":"2021-01-16T04:08:08","slug":"dexie-js","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/06\/dexie-js.html","title":{"rendered":"IndexedDB\u3092\u3064\u304b\u3046\u306a\u3089Dexie.js\u304c\u4fbf\u5229"},"content":{"rendered":"

\u30d6\u30e9\u30a6\u30b6\u3067\u30c7\u30fc\u30bf\u3092\u4fdd\u7ba1\u3059\u308b\u5834\u5408\u3001Web Storage\uff08sessionStorage\u3084localStorage \uff09\u3092\u4f7f\u3046\u3053\u3068\u304c\u591a\u304b\u3063\u305f\u3068\u601d\u3044\u307e\u3059\u304c\u3001IndexedDB\u304c\u767b\u5834\u3057\u305f\u3053\u3068\u3067\u3042\u3048\u3066Web Storage\u3092\u4f7f\u3046\u7406\u7531\u304c\u306a\u304f\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n

IndexedDB\u3092\u4f7f\u3046\u5834\u5408\u3001Dexie.js<\/a>\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3060\u3044\u3076\u5b9f\u88c5\u304c\u30b9\u30c3\u30ad\u30ea\u3059\u308b\u306e\u3067\u3001\u4f7f\u3044\u65b9\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

Web Storage\u3068IndexedDB\u306e\u9055\u3044<\/h2>\n

\u305d\u3082\u305d\u3082\u672c\u5f53\u306bIndexedDB\u3092\u4f7f\u3046\u306e\u304c\u826f\u3044\u306e\u304b\u3001\u3068\u3044\u3046\u7591\u554f\u304c\u3042\u308b\u306e\u3067\u5ff5\u306e\u305f\u3081\u3001Web Storage<\/a>\u3068 IndexedDB<\/a>\u306e\u9055\u3044\u3092\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002\u8abf\u3079\u305f\u5168\u3066\u306e\u70b9\u3067IndexedDB\u306e\u65b9\u304c\u826f\u3044\u306e\u3067\u3001\u3084\u306f\u308aIndexedDB\u4e00\u629e\u3067\u3059\u306d\u3002<\/p>\n\n\n\n\n\n\n\n\n\n
\u9805\u76ee<\/th>\nWeb Storage<\/th>\nIndexedDB<\/th>\n<\/tr>\n<\/thead>\n
\u578b<\/td>\n\u6587\u5b57\u5217\u306e\u307f<\/td>\nboolean\u3001\u6570\u5024\u3001\u6587\u5b57\u5217\u3001date\u306a\u3069\u8272\u3005<\/td>\n<\/tr>\n
RDB\u306e\u30c6\u30fc\u30d6\u30eb<\/td>\n\u306a\u3057\u3002key-value\u3067\u5168\u3066\u30d5\u30e9\u30c3\u30c8\u306b\u7ba1\u7406<\/td>\nRDB\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u76f8\u5f53\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b9\u30c8\u30a2\u304c\u3042\u308a\u3001\u305d\u306e\u4e0b\u306b\u30ec\u30b3\u30fc\u30c9\u3092\u683c\u7d0d\u3059\u308b<\/td>\n<\/tr>\n
\u5bb9\u91cf\u5236\u9650<\/td>\nOrigin\u3042\u305f\u308a10MB<\/td>\n\u30c7\u30a3\u30b9\u30af\u30b5\u30a4\u30ba\u306b\u4f9d\u5b58\uff08\u666e\u901a\u306f10MB\u3088\u308a\u306f\u5927\u304d\u3044\uff09<\/td>\n<\/tr>\n
devtool\u3067\u306e\u5909\u66f4<\/td>\n\u8ffd\u52a0\u30fb\u5909\u66f4\u30fb\u524a\u9664\u306a\u3069\u306a\u3093\u3067\u3082\u3067\u304d\u308b<\/td>\n\u30af\u30ea\u30a2\u3084\u524a\u9664\u306f\u3067\u304d\u308b\u304c\u8ffd\u52a0\u5909\u66f4\u306f\u3067\u304d\u306a\u3044<\/td>\n<\/tr>\n
\u30a2\u30af\u30bb\u30b9\u7bc4\u56f2<\/td>\n\u540c\u4e00\u30aa\u30ea\u30b8\u30f3\u5185\u3002JavaScript\u304b\u3089\u5168\u3066\u306e\u30ad\u30fc\u3092\u53d6\u5f97\u3067\u304d\u308b<\/td>\n\u540c\u4e00\u30aa\u30ea\u30b8\u30f3\u5185\u3002\u305f\u3060\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3068\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u77e5\u3089\u306a\u3044\u3068\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u203bchrome\u306e\u30b1\u30fc\u30b9\u3067\u8abf\u3079\u3066\u307e\u3059\u3002\u9593\u9055\u3063\u3066\u305f\u3089\u3059\u3044\u307e\u305b\u3093\u3002<\/p>\n

Dexie\u306e\u4f7f\u3044\u65b9<\/h2>\n

Dexie<\/a>\u306fIndexedDB\u5229\u7528\u3092\u975e\u5e38\u306b\u697d\u306b\u3057\u3066\u304f\u308c\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002\u81ea\u5206\u3068\u3057\u3066\u306f\u3001IndexedDB\u306e\u30ec\u30b3\u30fc\u30c9\u3092Typescript\u306e\u30af\u30e9\u30b9\u306bO\/R Mapping\u3057\u3066\u304f\u308c\u308b\u70b9\u304c\u975e\u5e38\u306b\u3042\u308a\u304c\u305f\u3044\u3068\u601d\u3044\u307e\u3057\u305f\u3002<\/p>\n

\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n

npm\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n

npm install dexie<\/code><\/pre>\n

DB\u5b9a\u7fa9\u304a\u3088\u3073\u30b9\u30bf\u30fc\u30c8<\/h3>\n

dexie.ts<\/code>\u3092\u4f5c\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067DB\u5b9a\u7fa9\u304a\u3088\u3073\u30b9\u30bf\u30fc\u30c8\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n

import Dexie from 'dexie'\n\n\/\/ \u30b9\u30ad\u30fc\u30de\u306e\u4fee\u6b63\u304c\u3042\u308b\u5834\u5408\u3001\u3053\u306e\u5024\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u3042\u308a\nconst SCHEMA_VERSION = 1\n\n\/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b9\u30c8\u30a2\u306b\u5bfe\u5fdc\u3059\u308b\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3002interface\u3067\u3082OK\u3002\u5b9f\u969b\u306b\u306fmodel\u306f\u5225\u30d5\u30a1\u30a4\u30eb\u306b\u5b9a\u7fa9\u3057\u3066\u3042\u308a\u307e\u3059\nexport class AccessCountModel {\n  key: number\n  readCount: number\n  updateCount: number\n  deleteCount: number\n  constructor(key: number, readCount = 0, updateCount = 0, deleteCount = 0) {\n    this.key = key\n    this.readCount = readCount\n    this.updateCount = updateCount\n    this.deleteCount = deleteCount\n  }\n}\ninterface HogeDatabase extends Dexie {\n  accessCounts: Dexie.Table<AccessCountModel, number> \/\/ \u3053\u3053\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b9\u30c8\u30a2\u3068\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u5bfe\u5fdc\u3065\u3051\u3066\u3044\u308b\u3002number\u306f\u30ad\u30fc\u306e\u578b\n}\n\nconst dexieDb = new Dexie('hoge-database') as HogeDatabase\ndexieDb.version(SCHEMA_VERSION).stores({\n  accessCounts: 'key,readCount,updateCount,deleteCount', \/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b9\u30c8\u30a2\u306e\u5b9a\u7fa9\n})\nexport default dexieDb<\/code><\/pre>\n

repository\u30af\u30e9\u30b9\u306e\u4f5c\u6210<\/h3>\n

\u76f4\u63a5\u3001dexieDb\u3092\u4f7f\u3063\u3066\u3082\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u6bce\u56dethen<\/code>\u3084catch<\/code>\u3092\u66f8\u304f\u306e\u3082\u5927\u5909\u306a\u306e\u3067\u3001repository\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n

import dexieDb, { AccessCountModel } from '.\/dexie'\n\nexport class AccessCountRepository {\n  \/\/ \u30c7\u30fc\u30bf\u53d6\u5f97\n  async get(key: number): Promise<AccessCountModel | undefined> {\n    return dexieDb.accessCounts\n      .get(key)\n      .then(async (count: AccessCountModel | undefined) => {\n        return count\n      })\n      .catch((error) => {\n        throw new Error(`Error getting access count:, ${error}`)\n      })\n  }\n\n  \/\/ \u30c7\u30fc\u30bf\u66f4\u65b0\n  async save(accessCount: AccessCountModel): Promise<AccessCountModel> {\n    await dexieDb.accessCounts.update(accessCount.key, accessCount)\n    return await this.get(accessCount.key)\n  }\n}<\/code><\/pre>\n

\u5229\u7528<\/h3>\n

\u5b9f\u969b\u306e\u5229\u7528\u7b87\u6240\u3067\u306f\u3001repository\u30af\u30e9\u30b9\u3092import\u3057\u3066\u4f7f\u3044\u307e\u3059\u3002<\/p>\n

import { AccessCountRepository } '.\/repositories'\nimport { AccessCountModel } from '.\/dexie'\n\nclass Hoge {\n  accessCountRepository = new AccessCountRepository()\n  async fuga(): Promise<void> {\n    const key = 3600\n    const accessCount: AccessCountModel = await this.accessCountRepository.get(key)\n    console.log(accessCount)\n  }\n}<\/code><\/pre>\n

CDN\u5229\u7528<\/h2>\n

dexie\u306fCDN\u3082\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u305d\u3061\u3089\u3092\u4f7f\u3046\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002
\nhtml\u3067dexie.min.js<\/code>\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3001<\/p>\n

<body>\n    <script src="https:\/\/unpkg.com\/dexie@latest\/dist\/dexie.min.js"><\/script>\n<\/body><\/code><\/pre>\n

webpack.config.js\u306eexternals\u306bdexie<\/code>\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002global\u5909\u6570\u540d\u306fDexie<\/code>\u306a\u306e\u3067\u305d\u308c\u3092\u6307\u5b9a\u3057\u3066\u307e\u3059\u3002<\/p>\n

  externals: {\n    dexie: 'Dexie',\n  }<\/code><\/pre>\n

\u3053\u308c\u3067\u3001\u30d0\u30f3\u30c9\u30eb\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u306f\u9664\u5916\u3055\u308c\u3001CDN\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h2>\n

\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b9\u30c8\u30a2\u304c\u5b58\u5728\u3057\u306a\u3044<\/h3>\n

\u5b58\u5728\u3057\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b9\u30c8\u30a2\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n

\n

NotFoundError: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.<\/p>\n<\/blockquote>\n

\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u81ea\u5206\u306e\u7d4c\u9a13\u7684\u306b\u306f\u539f\u56e0\u306f\u4ee5\u4e0b\u306e\u4e8c\u3064\u306e\u3069\u3061\u3089\u304b\u3067\u3057\u305f\u3002<\/p>\n