Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added multi-directory support #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

added multi-directory support #51

wants to merge 2 commits into from

Conversation

kfardanesh
Copy link

An initial start to add multi-directory support while keeping existing functionality. Looking to get help/advice from community to make code more robust and describe what functionality they would like to see. I understand that this involves a lot more edge cases and effort to maintain than the single directory so also looking to gauge interest since this is one of the more popular repos for json storage on electron.

Added capability to prefix key with directory for get(), set(), has(), remove() (i.e. "testfolder/foo" will get, set, has, or remove foo under the testfolder directory).

  • "foo" will look/create/check/remove key at "{apppath}/storage/foo.json"
  • "testfolder/foo" will look/create/check/remove key at "{apppath}/storage/testfolder/foo.json"
  • "../testfolder/foo" will look/create/check/remove key at "{apppath}/storage/testfolder/foo.json" (i.e. parent directory symbol ".." gets ignored)
  • "testfolder/testfolder/foo" will look/create/check/remove key at "{apppath}/storage/testfolder/testfolder/foo.json"

Also extended functions getAll(), keys(), clear()* to allow optional directory parameter (i.e. "testfolder" will getAll, keys or clear* under the testfolder directory)

  • keys function would return directories, so added check for file extension (if no file extension, assume folder

getMany() is tricky and needs some work to determine functionality with multi-directories.

*Currently clear() removes the directory passed to function. Can be changed.
**All current test cases pass with exception of utils test function which I commented out for time being.
***Can work on adding test cases if there is enough interest

An initial start to add directory support, looking to get help/advice from community to make code more robust.

Added capability to prefix key with directory for get, set, has, remove (i.e. "testfolder/foo" will get, set, has, or remove foo under the testfolder directory).
Also extended functions getAll, keys, clear* to allow optional directory parameter (i.e. "testfolder" will getAll, keys or clear* under the testfolder directory.

getMany is tricky and needs some work.

*Currently clear removes the directory passed to function.
**All current test cases pass with exception of utils test function since directory is different
@jviotti
Copy link
Member

jviotti commented Mar 6, 2017

Hi @kfardanesh ,

Thanks a lot for the PR, and sorry for not having time to look at this during the weekend. I'll try to do so on Monday :)

@jviotti
Copy link
Member

jviotti commented Mar 6, 2017

Let's add some functions to set and get the default settings location, for example: storage.setUserDataPath() and storage.getUserDataPath().

Having to specify the directory each time looks like unnecessary extra overhead.

@kfardanesh
Copy link
Author

I reverted my changes to your original functions and added the get and set functions for the current working directory. All original test cases still work.

A slight annoyance with most recent commit is that any new directories created under the base 'storage' folder will persist unless deleted manually.

* let userDataPath = utils.getUserDataPath();
* console.log(userDataPath);
*/
exports.getUserDataPath = function() {
return path.join(app.getPath('userData'), 'storage');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should inline 'storage' here, or create a constant within the scope of exports.getUserDataPath, given that is not used elsewhere.

/**
* @summary Get user data directory path
* @summary Get default user data directory path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, its not really the default one, given that the result depends on the directory the user set. Right?

* @function
* @private
*
* @returns {Strings} parsed user data path without parent directories
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be String.

@@ -29,18 +29,53 @@ const path = require('path');
const electron = require('electron');
const app = electron.app || electron.remote.app;

// define the defaultDataPath and current directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment its too redundant, and not contributing anything of value here.

*
* @returns {Strings} parsed user data path without parent directories
*/
function getDirectory(directory){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the security thinking here, however I don't think we have any good reason to restrict the settings path to be inside a certain directory. A good use case of this feature is that people can store settings whenever they want, so I guess we can leave out this normalization routine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a consequence, path.join(app.getPath('userData'), 'storage') should be the initial value of currentDirectory.

* @example
* utils.getUserDataPath('newDirectory');
*/
exports.setUserDataPath = function(directory) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on https://github.com/jviotti/electron-json-storage/pull/51/files#r105062802, I think we should do a sanity check here to ensure that directory is an absolute path, using path.isAbsolute(), and throw an error otherwise.

@@ -121,7 +161,7 @@ exports.getMany = function(keys, callback) {
if (error) {
return callback(error);
}

// console.log(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be deleted.

*
* storage.setDataPath('foo');
*/
exports.setDataPath = function(dir = ""){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're exposing both functions from utils, what do you think about simply moving their logic here?

@Yoann-Pearson
Copy link
Contributor

Any update on this feature? Either directory support in the keys or changing the user path. I would be interested in this functionality.

@jviotti
Copy link
Member

jviotti commented Jul 27, 2017

@stromboul I don't think the author is still working on it. You're free to take over using the suggestions in this PR!

This was referenced Jul 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants