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

nedb-models

v4.0.1

Published

A simple and useful model wrapper for nedb (using nedb-promises).

Downloads

33

Readme

nedb-models

nedb-models is a simple and extensible model wrapper for nedb using nedb-promises.

Check out the docs!

IMPORTANT

As of nedb-promises 5.0.0 nedb package has been replaced with a fork of the original package, @seald-io/nedb to solve some vulnerability issues originating from nedb!

Contents

Installation

npm install nedb-models

Usage

Basics

A model class has almost all methods nedb's Datastore does, like Model.find(), Model.findOne, etc.

Check out the docs to see all of them!

const { Model } = require('nedb-models')

class Book extends Model {
    static datastore() {
        return {
            filename: 'books.db'
        }
            
        // same as
        // return 'books.db'
    }
    
    async sell() {
        this.soldAt = Date.now()
        return await this.save()
    } 
}

Book.find()
// [ Book { ... }, Book { ... } ]

// find the first book
let book = Book.findOne()
// Book { ... }

// sell it
book.sell().then(book => {
    // Book { soldAt: XXXXXX, ... }
})

Extensions

nedb-models includes three extensions that you can use right out of the box:

To implement them in your model, all you have to do is:

const { Model, Timestamps, SoftDeletes, Encryption } = require('nedb-models')

class Book extends Model {
    ...
}

Book.use(SoftDeletes)
// or Book.use([Timestamps, SoftDeletes])
// or Book.use(Timestamps)
// or Book.use(Encryption)

// now that your model is using SoftDeletes, 
// you can do funky stuff like:
Book.findRemoved()
Book.forceRemove({ ... })
Book.restore({ ... })

Creating extensions

Let's take a look at how an extension is built:

class Timestamps extends Extension {
    apply() {
        let __class = this.__class

        /**
         * In this case we have to add the `timestampData: true`
         * property to the datastore options.
         *
         * Since the datastore options are returned by a static
         * method we need to overwrite this method while
         * accessing it.
         * This is where tha last parameter of (in this case) `setStatic`
         * comes in handy. If you set this to true, and the second parameter
         * is a function, the module will pass the original value
         * to your function and then override it with the returned value.
         */
        this.setStatic('datastore', datastore => {
            /**
             * Don't use arrow functions when overwriting methods (static or instance),
             * because arrow functions don't handle the context (`this`) well.
             */
            return function () {
                /**
                 * In the new function first we retrieve the original options.
                 * We do that inside the new function to keep the possibility
                 * of dynamic properties.
                 */
                let options = datastore.call(__class)

                /**
                 * We add the property while also checking
                 * the type of the original options.
                 */
                if ( ! options) {
                    options = { timestampData: true }
                } else if ('string' === typeof options) {
                    options = {
                        filename: options,
                        timestampData: true
                    }
                } else if ('object' === typeof options) {
                    options = augmenter(options)({
                        timestampData: true
                    })
                }

                return options
            }
        }, true)

        /**
         * Finally we return true to show that
         * applying the extension was successful.
         */
        return true
    }
}

Check out the docs!

What's new

2.2.0

What's coming

  • Tests (for SoftRemoves and Encryption)