Created
November 19, 2022 05:10
-
-
Save kuc-arc-f/f26243dc1a4039078c89d283b98eacf9 to your computer and use it in GitHub Desktop.
sqlite, localStorage save
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
import LibConfig from '../lib/LibConfig'; | |
import LibSqlite from '../lib/LibSqlite'; | |
// | |
const LibStorage = { | |
/** | |
* save: db save | |
* @param | |
* | |
* @return | |
*/ | |
save: async function(db : any): Promise<void> | |
{ | |
try{ | |
const uint8Array = db.export(); | |
const base64 = this.base64encode(uint8Array); | |
//console.log(base64); | |
const key = LibConfig.STORAGE_KEY_DB; | |
localStorage.setItem(key, base64); | |
} catch (e) { | |
console.error(e); | |
throw new Error('Error , save'); | |
} | |
}, | |
/** | |
* get: get save | |
* @param | |
* | |
* @return | |
*/ | |
get: async function(): Promise<any> | |
{ | |
try{ | |
let ret = null; | |
const key = LibConfig.STORAGE_KEY_DB; | |
console.log("key=", key); | |
const base64= localStorage.getItem(key); | |
if(base64 === null) { | |
return ret; | |
} | |
//console.log(base64); | |
const u8 = this.base64decode(base64); | |
const SQL = await LibSqlite.getSql(); | |
const db = new SQL.Database(u8); | |
ret = db; | |
return ret; | |
} catch (e) { | |
console.error(e); | |
throw new Error('Error , get'); | |
} | |
}, | |
/** | |
* base64encode | |
* @param data:Uint8Array | |
* | |
* @return | |
*/ | |
base64encode: function(data:Uint8Array): any | |
{ | |
//@ts-ignore | |
return btoa([...data].map(n => String.fromCharCode(n)).join("")); | |
}, | |
/** | |
* base64encode | |
* @param data:string | |
* | |
* @return | |
*/ | |
base64decode: function(data:string): any | |
{ | |
//@ts-ignore | |
return new Uint8Array([...atob(data)].map(s => s.charCodeAt(0))); | |
}, | |
} | |
export default LibStorage; |
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
import * as React from 'react'; | |
//import { useEffect, useState } from 'react'; | |
import { useEffect } from 'react'; | |
import LibSqlite from '../lib/LibSqlite'; | |
import LibStorage from '../lib/LibStorage'; | |
function Page() { | |
useEffect(() => { | |
(async() => { | |
//console.log(result); | |
})() | |
}, []); | |
// | |
const dbSave = async function (){ | |
const db = await LibSqlite.getDb(); | |
const sql = ` | |
INSERT INTO Post(title, content, CategoryId, createdAt, updatedAt) | |
VALUES | |
( | |
't1', | |
'c1', | |
'ca11', | |
DATETIME('now','localtime'), | |
DATETIME('now','localtime') | |
); | |
`; | |
await db.exec(sql); | |
await LibStorage.save(db); | |
} | |
// | |
const dbGet = async function (){ | |
const db = await LibStorage.get(); | |
if(db === null) { | |
alert("Error, db is null") | |
return; | |
} | |
await LibSqlite.setImportDb(db); | |
} | |
// | |
const test = async function (){ | |
const db = await LibSqlite.getDb(); | |
let res = JSON.stringify(db.exec("SELECT sqlite_version();")); | |
console.log(res); | |
res = JSON.stringify(db.exec(`SELECT * FROM Post;`)); | |
console.log(res) | |
} | |
// | |
return ( | |
<div className="container"> | |
<h3>Test </h3> | |
<hr /> | |
<p>welcome, about</p> | |
<hr /> | |
<button onClick={() => dbSave()}>save | |
</button> | |
<hr /> | |
<button onClick={() => dbGet()}>get | |
</button> | |
<hr /> | |
<button onClick={() => test()}>test | |
</button> | |
</div> | |
); | |
} | |
export default Page; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment