当前位置:网站首页>Client development (electron) data store

Client development (electron) data store

2022-06-25 15:35:00 Xiao Xin

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

File read and write operations :

  • Determine the file directory :

    const dataPath = path.join(app.getPath('userData'), 'data.json')
     Copy code 
  • File is written to :

    fs.writeFileSync(
      dataPath,
      JSON.stringify({ username: "admin", version: "0.0.1" }),
      { encoding: "utf-8" }
    );
     Copy code 
  • File read :

    const content = fs.readFileSync(dataPath, { encoding: "utf-8" });
    console.log("[ content ] >", content);
     Copy code 

Third party libraries use :

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 code 
  • install 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 .

原网站

版权声明
本文为[Xiao Xin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202192241134677.html