1.0.0 • Published 5 years ago
@mafgit/local-mongo v1.0.0
local-mongo
A React Library for interacting with LocalStorage using functions and methods like those provided by Mongoose.
Installation
npm install --save @mafgit/local-mongo
Then import it like:
import useLocalMongo from '@mafgit/local-mongo'
Creating a model using useLocalMongo(storeName: string, schema: object)
const User = useLocalMongo('users', {
name: { type: 'String', required: true },
age: { type: 'Number', default: 18 },
tags: { type: 'Array', default: [] },
})
In this example, we are creating a model for a user. We have created three properties, name, age and tags, and their types must be in quotes, unlike in mongoose. We can make a property required, or set a default value for it.
Accessing the store by Model.value
User.value // [{...}, {...}]
Creating a new document
User.create({ name: 'maf' })
.then(user => console.log(user))
.catch(err => console.error(err))
Updating a document
const _id = '05533684729679925'
User.findByIdAndUpdate(_id, (user) => {
return {
...user,
name: 'Abdullah'
}
})
.then(user => console.log(user))
.catch(err => console.log(err))
You can access the document in the callback function, and return the updated document in the callback.
Deleting a document
User.findByIdAndDelete(_id)
.then(() => console.log('Deleted'))
.catch(err => console.log('Could not delete the document'))
License
MIT © mafgit
1.0.0
5 years ago