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

lowdb without lodash #109

Closed
davej opened this issue May 27, 2016 · 15 comments
Closed

lowdb without lodash #109

davej opened this issue May 27, 2016 · 15 comments

Comments

@davej
Copy link

davej commented May 27, 2016

Just want to run this idea by you. I love lowdb but I have no need for the lodash features at the moment so it just adds a ton of weight for me.

What would you think about releasing the core lowdb as a separate module and the current lowdb can simply wrap the new core lib in lodash.

If that doesn't appeal to you then how about exposing a way for us to use lowdb without relying on lodash? This might be as simple as adding an index.light.js which provides a mocked empty version of lodash.

These are just ideas, feel free to close this and keep doing what you're doing because lowdb is awesome! 👍

@typicode
Copy link
Owner

Hi @davej,

Thank you for the feedback and idea, it's always appreciated :)

Is it related to v0.13.0-beta or in general?

If it's because you need to install lodash now, actually, lodash was always part of lowdb but was "hidden" in package.json dependencies. For example, if you check v0.12 you can see it: https://github.com/typicode/lowdb/blob/v0.12.5/package.json#L43

The only difference is now that it's explicit and you can have more control on lodash version used by lowdb. On the other side, the installation is a little less straightforward. So it may change, and I'm open to feedback.

What's your context by the way? Browser or Node?

@davej
Copy link
Author

davej commented May 28, 2016

It's just a general comment, not related to v0.13.0-beta. I haven't actually looked at the beta yet, must check it out.

My context is Electron. I'm trying to trim down the .app file and also improve load time when opening new windows. Lodash, is a relatively heavy lib and it's a shame to have to load the entire library when it's not being used.

Maybe I should be looking at using steno with a lightweight wrapper instead.

@typicode
Copy link
Owner

typicode commented May 29, 2016

Ok I see, thank you for the explanations.

One way would be to create a custom lodash build with only what you need
https://lodash.com/custom-builds

For example, generate a core build using lodash core include=get,set,... then pass it to lowdb:

const low = require('lowdb/lib/_index') // undocumented feature, may change without notice
const lodash = require('./lodash.custom.js')
const storage = require('./file-sync')

const db = low('db.json', { storage }, lodash)

If it works for you, I could change lowdb API to make it easier to pass a lodash build.

@davej
Copy link
Author

davej commented May 29, 2016

I don't know if I like the idea of adding a custom built lodash and then keeping it up-to-date outside of npm.

Perhaps I could do something like this though:

import runInContext from 'lodash/runInContext';
import get from 'lodash/get';
import chain from 'lodash/chain';
import wrap from 'lodash/wrap';

const db = low('db.json', { storage }, { runInContext, get, chain, wrap });

I'm using webpack so it will import only the included modules and I won't need to ship all of lodash with my app. A public API for passing a custom lodash would be great.

@typicode
Copy link
Owner

I think you can keep it up-to-date, if you add it as a devDependency and include it in your build:

$ npm install lodash-cli  --save-dev
{
  "scripts": {
    "build": "npm run lodash && npm run webpack",
    "lodash": "lodash core",
  }
}

It will just be another part of your build and if you need to update lodash, simply update lodash-cli.

I agree that using import is cleaner. However I don't know how to "tie" everything together dynamically. For example, how to make _.get available in the chain created by _.chain when they are initially two separate imports.

There's also lodash/fp that could make it possible to import only what you need but lowdb doesn't support it (need to do some tests with the API before).

@typicode
Copy link
Owner

Did some tests, and actually it doesn't seem to be that complicated to add methods dynamically.

@davej
Copy link
Author

davej commented May 30, 2016

Excellent, will my example above work?

@typicode
Copy link
Owner

I think so. You can already give it a try with the current version (feedback welcome ;))

const low = require('lowdb/lib/_index') // Undocumented feature, may change without notice
const lodash = require('lodash/core') // Smaller build
const storage = require('./file-sync')

// Add to core what you need
lodash.mixin({ get: require('lodash/get') }) // Add only one method
lodash.mixin({ get: require('lodash/set') })
lodash.mixin(require('lodash/array')) // Add all Array methods

const db = low('db.json', { storage }, lodash)

I'm not yet settled on the API for lowdb. Maybe it's possible to require even less than lodash/core, I've not tested it.

@davej
Copy link
Author

davej commented May 31, 2016

Had no luck. This is what I tried:

import low from 'lowdb/lib/_index';
import storage from 'lowdb/lib/file-async';
import lodash from 'lodash/core';
import lodashUtil from 'lodash/util'; // Should have `runInContext` func

lodash.mixin(lodashUtil);
const db = low('...', { storage }, lodash);

And the error I get is TypeError: lodash.runInContext is not a function..

@typicode
Copy link
Owner

typicode commented Jun 2, 2016

Good to know, thank you. I tried with lodash only, but didn't inject it back in lowdb.

I'll release v0.13.0 this week and in a second step, see how it would be possible to require only parts of lodash.

@typicode
Copy link
Owner

Getting there ;) there's 2 new "modes" in v0.15 that don't import lodash and can be used to create smaller bundles.
Didn't have the time to document them yet.

@davej
Copy link
Author

davej commented Feb 10, 2017

Ah cool. I ended up switching to electron-storage but I'll definitely take another look at lowdb when I'm working on a new project that needs persistence. Thanks for the great work! 👍

@bgbraga
Copy link

bgbraga commented Apr 16, 2017

Is it already documented?

@typicode
Copy link
Owner

Hi @bgbraga,

lowdb/lib/fp is documented and doesn't require full lodash. Instead, you can just pick the function you need, resulting in a smaller build:
https://github.com/typicode/lowdb/blob/master/examples/fp.md

You can also use plain JavaScript functions with it too.

There's also lowdb/lib/nano but it's not yet documented/tested and I'm not sure if it will be kept.

@typicode
Copy link
Owner

Done in v2 :)

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

No branches or pull requests

3 participants