Giter Site home page Giter Site logo

cocycles / electron-storage Goto Github PK

View Code? Open in Web Editor NEW
127.0 10.0 12.0 22 KB

Simply save/load json files to/from file system in electron applications

License: MIT License

JavaScript 100.00%
electron storage fs file-system node userdata electron-storage

electron-storage's Introduction

electron-storage

simple storage managing module for electron

Electron saves data in app.getPath("userData") folder, which is different in every os. electron-storage gives simple methods to get and set json files to this directory.

  • Creates subdirectories if needed - that means you can write movies/StarWars.json as path, a movies folder will be created and a StarWars.json file inside.
  • Supports callbacks and promises.
  • The data inserted can be a javascript object, or stringified json.
  • You don't have to write .json in the end of a file path, it will add it for you.

NPM [Package Quality](http://packagequality.com/#?package=electron- storage)

npm version license issues forks stars twitter

Installation

$ npm install --save electron-storage

usage

const storage = require('electron-storage');

API

get

get a json file from storage.

storage.get(filePath, cb)

storage.get(filePath, (err, data) => {
  if (err) {
    console.error(err)
  } else {
    console.log(data);
  }
});

storage.get(filePath)

storage.get(filePath)
.then(data => {
  console.log(data);
})
.catch(err => {
  console.error(err);
});

set

set a json file to storage.

storage.set(filePath, data, cb)

storage.set(filePath, data, (err) => {
  if (err) {
    console.error(err)
  }
});

storage.set(filePath, data)

storage.set(filePath, data)
.then(() => {
  console.log('The file was successfully written to the storage');
})
.catch(err => {
  console.error(err);
});

isPathExists

check if a file or directory exists.

// you have to write .json suffix for json files.
// this method works on directories as well, if you don't write `.json` suffix it checks for a directory.

storage.isPathExists(path, cb)

storage.isPathExists(path, (itDoes) => {
  if (itDoes) {
    console.log('pathDoesExists !')
  }
});

storage.isPathExists(path)

storage.isPathExists(path)
.then(itDoes => {
  if (itDoes) {
    console.log('pathDoesExists !')
  }
});

remove

remove a file or a directory from storage

storage.remove(path, cb)

storage.remove(path, err => {
  if (err) {
    console.log(err)
  }
});

storage.remove(path)

storage.remove(path)
.then(err => {
  if (err) {
    console.log(err)
  }
});

Development

npm run build for creating es5 files in dist folder

Contribute

Contributions are welcome! please open issues and pull request :)

License

The MIT License (MIT) Copyright (c)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

electron-storage's People

Contributors

davej avatar ran-y avatar ranyitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

electron-storage's Issues

Question: why are filePaths joined to "userData" folder

why are filePaths joined to "userData" folder?
is it possible to avoid this and save, or load a file from, for example, the user's Desktop? or a custom-defined path relative to the filesystem root?

return path.join(userData, filePath);

Thanks!

isPathExists doesn't append .json on path

I noticed when trying to use isPathExists, it requires adding the .json to the path name unlike the set and get methods. I'm assuming that's on purpose but it doesn't seem consistent with the other methods.

Maybe that should be clarified in the README?

default filePath on macosx?

Hi, where is file created on MacOSX if no path is specified?

I am searching it after i set() it but i can't actually find it....

thanks

storage.get

I have the following code.
` var filePath = "Settings.json"

    storage.set(filePath, "{'balance': 1000.21, 'num':100, 'is_vip':true, 'name':'foo'}")
        .then(data => {
            console.log(data);
        })
        .catch(err => {
            console.error(err);
        });

    storage.get(filePath, (err, data) => {
        if (err) {
            console.error(err)
        } else {
            console.log(data);
        }
    });`

The data is written to the Settings.json under C:\Users\thardes2\AppData\Roaming\MyApp, but when I try to get them with the get function I get the following error:

The file in path C:\Users\thardes2\AppData\Roaming\MyApp/Settings.json is not a valid json file

So the format of the path is wrong. As you can see, I am just using the example provided in the README.
Any idea or solution to this?

Thank you :-)

Not a Valid JSON file when suing get method.

Hi there. I get this error on macOS.

var data = new Object();
    data.name = name;
    data.address = address;

    storage.set('customers.json', data)
    .then(() => {
      console.log('Data saved successfully!')
    })
    .catch((err) => {
      console.log(err);
    });

It writes to the file but then when I use the get function,

var oldData = storage.get('customers.json')
    .then(d => {
      console.log(d);
    })
    .catch(err => {
      console.log(err);
    });

It gives me that the error.

And here is what the console gives:

Error: The file in path /Users/syncster31/Library/Application Support/ASMS/customers.json is not a valid json file
at /Users/syncster31/Documents/Programming/Electron/my-app/node_modules/electron-storage/dist/index…:37
at tryToString (fs.js:449)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:436)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.