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 🙏

© 2026 – Pkg Stats / Ryan Hefner

monking-mongodb

v1.0.0

Published

provide model level for monking using mongodb

Readme

monking-mongodb

依赖于 monking,为其提供 model 层的能力。

Installation

$ # 强烈建议使用 yarn 安装依赖
$ yarn add monking-mongodb

monking >= 1.1.0

Configuration Middleware

monking-mongodb 的 model 层的能力通过中间件来提供,其作为 monking 的一个扩展而存在,需要将其配置到 config 中,如下:

// config/default.js

export default {
    middlewares: ['monking-mongodb/lib/middleware']
};

Using

使用 mongoose 实现 server 端数据持久化功能。monking-mongodb 提供的中间件,会将 model 依赖注入,但是考虑到 mongoose 需要提前定义 schema 和 生成单例的 model,于是做了如下约定:index.js 根据schema 生成 model,生成的 model 会挂载在 monking.model 上面,model.js 来做增删改查的操作。

// server/model/user/index.js

export default ({createSchema, createModel, Schema}) => {
    const schema = createSchema({
        name: String,
        age: String
    });

    return createModel('user', schema);
};

这里的 createSchema 和 createModel 只是对 new mongoose.Schema 和 mongoose.model 进行了封装,Schema 为 mongoose.Schema,用于创建 Schema 特有的数据类型。

// server/model/user/model.js

export default class UserModel {
    constructor (monking) {
        this.User = monking.model.user;
    }

    async add (name, age) {
        const user = new this.User({ name, age });
        return await user.save();
    }

    async get () {
        return await this.User.find().exec();
    }
};

monking、context 和logger 做了依赖注入,monking-mongodb 中间件将 userModel 做了依赖注入。

Expose Config

monking-mongodb 提供了一些默认的 mongoose 的配置,用户可以自行覆盖默认配置,如下:

export default {
    mongodb: {
        url: 'mongodb://localhost:27017/monking-mongodb',
        options: {
            useNewUrlParser: true,
            useFindAndModify: false
        },
        defaultSchema: {
            options: {
                id: true,
                toJSON: {
                    getters: true,
                    virtuals: true
                },
                toObject: {
                    getters: true,
                    virtuals: true
                },
                timestamps: {
                    createdAt: 'createTime',
                    updatedAt: 'updatedTime'
                }
            },
            schema: {

            }
        }
    }
};

monking-mongodb 作为 monking 的一个插件存在,配置文件需要配置到 config 中,以支持 monking 引入。

// config/default.js

export default {
    pluginConfig: ['monking-mongodb/lib/config']
};

License

MIT