-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from NotoSgnikusnsi/feat/save-data
feat: 保存ボタンを押したとき,データをIndexedDBに保存する
- Loading branch information
Showing
3 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
type Place = { | ||
id: string; | ||
img: string; | ||
location: { | ||
lat: number; | ||
lon: number; | ||
}; | ||
}; | ||
|
||
const dbName = "test_ashiato"; | ||
let db: IDBDatabase | null = null; | ||
|
||
const openDB = (): Promise<IDBDatabase> => { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(dbName, 1); | ||
|
||
request.onupgradeneeded = (event) => { | ||
const db = (event.target as IDBOpenDBRequest).result; | ||
|
||
const objStore = db.createObjectStore("places", { keyPath: "id" }); | ||
objStore.createIndex("img", "img", { unique: true }); | ||
objStore.createIndex("location", "location", { unique: false }); | ||
}; | ||
|
||
request.onerror = (event) => { | ||
console.error("データベースにアクセスできません!!!"); | ||
reject(event); | ||
}; | ||
|
||
request.onsuccess = function (event) { | ||
db = (event.target as IDBOpenDBRequest).result; | ||
console.log("データベースにアクセスしました!!!"); | ||
resolve(db); | ||
}; | ||
}); | ||
}; | ||
|
||
const addRecord = (db: IDBDatabase, record: Place): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction("places", "readwrite"); | ||
const objStore = transaction.objectStore("places"); | ||
|
||
const request = objStore.add(record); | ||
|
||
request.onerror = function () { | ||
console.error("Error adding record"); | ||
reject(); | ||
}; | ||
|
||
request.onsuccess = function () { | ||
console.log("Record added successfully"); | ||
resolve(); | ||
}; | ||
}); | ||
}; | ||
|
||
const fetchAllRecords = (db: IDBDatabase): Promise<Place[]> => { | ||
const records: Place[] = []; | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction("places", "readonly"); | ||
const objStore = transaction.objectStore("places"); | ||
|
||
const request = objStore.openCursor(); | ||
|
||
request.onerror = function () { | ||
console.error("Error getting records"); | ||
reject(); | ||
}; | ||
|
||
request.onsuccess = function (e) { | ||
const cursor = (e.target as IDBRequest<IDBCursorWithValue>).result; | ||
if (cursor) { | ||
records.push(cursor.value); | ||
cursor.continue(); | ||
} else { | ||
console.log("No more records"); | ||
resolve(records); | ||
} | ||
}; | ||
}); | ||
}; | ||
|
||
export { openDB, addRecord, fetchAllRecords }; | ||
export type { Place }; |