Dear, Hello everyone , I am a “ Front end Xiaoxin ”, Engaged in front-end development for a long time , Android Development , Love technology , Go farther and farther on the road of programming ~
Electron It's a use JavaScript、HTML and CSS Building a framework for desktop applications . The embedded Chromium and Node.js To The binary Electron Allows you to keep a JavaScript Code base and create stay Windows Cross platform applications running on macOS and Linux—— No need for local development Experience .
Preface :
Data storage is also an essential function in application development , stay Electron Data persistence to local files is supported in development , Media and services provided by the browser SQLite In the database ,SQLite As a lightweight relational data storage, it is also widely used in mobile terminal development .
Local file storage :
Storage directory :
- Because the file directory of the impassable system is not unified ,Electron Provides proprietary API To make it easier for us to get the catalog (
app.getPath("userData");) Common user directories :
- desktop、documents、downloads、pictures、music、video
Special file directory :
- temp Corresponding system temporary folder path .
- ·exe The path corresponding to the currently executing program .
- ·appData Directory corresponding to application user personalization data .
- userData yes appData Path followed by the application name , yes appData The subpath of . The application name here is that the developer is package.json As defined in name The value of the property .
System default directory :
- The home directory of the current user :
require('os').homedir();/C:\Users\<username> - Default temporary file directory :
require('os').homedir();/C:\Users\<username>\AppData\Local\Temp
- The home directory of the current user :
File read and write operations :
Determine the file directory :
const dataPath = path.join(app.getPath('userData'), 'data.json') Copy codeFile is written to :
fs.writeFileSync( dataPath, JSON.stringify({ username: "admin", version: "0.0.1" }), { encoding: "utf-8" } ); Copy codeFile read :
const content = fs.readFileSync(dataPath, { encoding: "utf-8" }); console.log("[ content ] >", content); Copy code
Third party libraries use :
- lowdb:github.com/typicode/lo…, Easy to use local JSON Database extension .
- electron-store:github.com/sindresorhu…, Specially for Electron Designed access user configuration , Application status , Cache extension .(github.com/sindresorhu…)
SQLite data storage :
install node-sqlite3 Expand :
npm install sqlite3 --build-from-source --runtime=electron --target=13.6.9 --dist-url=https://atom.io/download/electron Copy codeinstall knexjs Expand :
npm install knex --save Copy code- 《 because sqlite Not installed successfully , Supplement after commissioning 》 This place is really a pit , Have you stepped on the pit? Leave an address
summary :
That's what happened Electron Develop ways to store data , Different data can be stored in different ways , It can be flexibly used in practical development , The way of browser storage is no longer introduced .
Welcome to my official account. “ Front end Xiaoxin ”, Original technical articles are pushed at the first time .









