npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-local-mongoose

v1.1.2

Published

A mongoose-esq wrapper for localStorage

Downloads

7

Readme

React Local Mongoose

Travis Build Status npm version

A mongoose-esque wrapper for localStorage

NB: This package is not affiliated with Mongoose in any way. It is simply an attempt to match their API.

This package has been heavily influenced by LocalDB by kucukkanat.

Installation

Install with npm or yarn:

npm i --save react-local-mongoose

yarn add react-local-mongoose

Defining a model

import LocalDb from 'react-local-mongoose';

const schema = {
  pathName: { type[, required[, unique[, ref ]  ] ] }
};

const model = new LocalDb(schema, collectionName);

Arguments

  • schema Object: Contains path names with settings. Required
    • pathName (Object|Array): settings for each path. Can only be an array of references at this time.
      • type Constructor: data type for path (eg ObjectID, String, Number, Boolean etc). Required
      • required (Boolean|String): indicates whether field is required. If string is provided, it will be used as an error message.
      • unique (Boolean|String): indicates whether field should be unique. If string is provided, it will be used as an error message.
      • ref String: indicates the name of the referenced model.
  • collectionName: String used to set collection in localStorage (eg: 'User', 'Post', 'Cheese' etc). Required

Return value

A new instance of LocalDb which behaves like a mongoose model

Example

import LocalDB, { ObjectID } from 'react-local-mongoose';

const cheeseSchema = {
  name: { type: String, required: 'This field is required', unique: 'That name is already taken' },
  origin: { type: ObjectID, ref: 'Country', required: true }, // reference to Country model
  strength: { type: Number, required: true },
  tastingNotes: { type: String },
  image: { type: String }
};

const Cheese = new LocalDB(cheeseSchema, 'Cheese');

export default Cheese;

Querying Methods

NB: In all examples Cheese is a model created using the example above

find

Cheese.find(query)

Arguments

  • query Object: An object defining search params. Uses sift for mongodb-esque queries

Return value

A promise which resolves all documents that match the query.


findOne

Cheese.findOne(query)

Arguments

  • query Object: An object defining search params. Uses sift for mongodb-esque queries

Return value

A promise which resolves with the first document that matches the query.


findById

Cheese.findById(id)

Arguments

  • id String: The id of the document to find.

Return value

A promise which resolves with the found document or null. If the document contains references, they will be populated.

Examples

Cheese.find({ strength: 4 })
  .then(records => console.log(records)); // returns an array of all records that have a strength of 4

Cheese.findOne({ name: 'Gorgonzola' })
  .then(record => console.log(record)); // returns the first record with the name of 'Gorgonzola'

Cheese.findById('5a36be1b15301600007f38f7')
  .then(record => console.log(record)); // returns the record with the id of '5a36be1b15301600007f38f7'

create

Cheese.create(data)

Arguments

  • data (Object|Array): An object containing the data to be stored, or an array containing objects to be stored. Data will be validated before saving to localStorage.

Return value

A promise which resolves with the stored documents, and rejects with an error containing validation error messages.

Example

Cheese.create({ name: 'Gorgonzola', strength: 4 })
  .catch(err => console.log(err.errors)); // { name: 'That name is already taken', origin: 'Path `origin` is required' }

Country.find({ $or: [{ name: 'Italy' }, { name: 'Netherlands'} ] })
  .then(countries => {
    return Cheese.create([
      { name: 'Gorgonzola', origin: countries[0], strength: 4 },
      { name: 'Edam', origin: countries[1], strength: 2 }
    ])
  })

  .then(records => console.log(records));
/*
  [
    { _id: '5a36be1b15301600007f38f7', 'name: 'Gorgonzola', origin: '5a37d33656e4ce00008a9b31', strength: 4 },
    { _id: '5a36be1b15301600007f38f8', name: 'Edam', origin: '5a37d33656e4ce00008a9b26', strength: 2 }
  ]
*/

update

Cheese.update(query, data)

Arguments

  • query Object: An object defining search params. Uses sift for mongodb-esque queries
  • data (Object|Array): An object containing the data to be updated.

Return value

A promise which resolves with the updated documents, and rejects with an error containing validation error messages.

Example

Cheese.update({ name: 'Gorgonzola' }, { strength: 2 })
  .then(records => console.log(records))); // [{ name: 'Gorgonzola', origin: '5a37d33656e4ce00008a9b31', strength: 2 }]

findByIdAndUpdate

Cheese.findByIdAndUpdate(id, data)

Arguments

  • id String: The id of the document to find.
  • data (Object|Array): An object containing the data to be updated.

Return value

A promise which resolves with the updated document, and rejects with an error containing validation error messages.

Example

Cheese.findByIdAndUpdate('5a36be1b15301600007f38f7', { strength: 2 })
  .then(records => console.log(records))); // { name: 'Gorgonzola', origin: '5a37d33656e4ce00008a9b31', strength: 2 }

remove

Cheese.remove(query)

Arguments

  • query Object: An object defining search params. Uses sift for mongodb-esque queries

Return value

A promise which resolves with null.

Example

Cheese.remove({ name: 'Gorgonzola' })
  .then(() => console.log('Record removed'));

findByIdAndRemove

Cheese.findByIdAndRemove(id)

Arguments

  • id String: The id of the document to remove.

Return value

A promise which resolves with null.

Example

Cheese.findByIdAndRemove('5a36be1b15301600007f38f7')
  .then(() => console.log('Record removed'));

drop

Cheese.drop()

Return value

drop always returns true.

Example

Cheese.drop(); // true

Limitations

Storage capacity

The amount of data that can be stored is depended on device and browser. For more info check out these resources:

Embedded / Referenced Data

Mongoose and Mongo allow for embedded schemas and references to other collections, with population functionality. This is not and will never be a full implementation of mongodb or mongoose on the client-side. Complex data structures are better persisted to an actual database.

I have added the ability to set references to models, however their use is limited. References can be set by passing an id or array of ids, or a record or an array of records. (see the examples above).

All find methods will populate records accordingly.

Embedded records are not supported at this time. It requires recursively validating the data, and populating. Again I may add it in the future.

Document methods, virtuals and other advanced Mongoose features

Again, if you need mongoose's advanced feature set, best use mongoose in conjunction with an API, and make requests via AJAX.

Contributing

Contributions are very welcome.

  • Fork the repository
  • yarn install or npm i to install the dependencies
  • yarn link or npm link to make your local fork available to other local projects
  • yarn link react-local-mongoose or npm link react-local-mongoose in a local React project to test your updates
  • When you're happy make a PR!

To do

  • ~~Write tests~~
  • Add embedded / ~~referenced~~ functionality
  • Cross-browser compatibility testing
  • Mobile device testing