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

dokyu

v0.4.0

Published

A nice little orm for mongodb

Downloads

10

Readme

dokyu

A little ORM for mongodb: npm install dokyu

Basic Usage


const { Document } = require('dokyu');

Document.connect("mongodb://localhost:27017/example");

class MyDocument extends Document("my_collection") {
  constructor(name) {
    super();
    this.name = name;
  }
  greet() { return "Hello, "+this.name; }
}
MyDocument.createIndexes({ name: 1 }, { unique: true });

var doc = new MyDocument("Arthur");
doc.email = "[email protected]";
await doc.save(); // this will add the '_id' field to doc

var doc2 = await MyDocument.getOrCreate({ email: "[email protected]" })
doc2.name // will be "Arthur", from the database
doc2.greet() // will be "Hello, Arthur" from the prototype

API

  • Document.connect( [name], url )

    The url should be something like: "mongodb://localhost:27017/db_name".

    Any connection options supported by mongo can be given as URL params, "?replSet=rs0"

  • Document( collectionName, [opts] ) → class

    This creates a new base class, suitable only for extending.

    For instance, given class Foo extends Document("foos"), all instances of Foo will read/write to the "foos" collection, using whatever database you pass to Document.connect.

  • MyDocument.count( query ) → Promise(number)

    The count value is the number of documents matching the query.

    const count = await MyDocument.count( query );
    assert('number' == typeof count)
  • MyDocument.findOne( query ) → Promise(doc)

    The doc value is the first matching instance of MyDocument.

    const doc = await MyDocument.findOne({ name: "Adam" })
    doc.greet(); # "Hello, Adam"
  • MyDocument.getOrCreate( fields ) → Promise(MyDocument)

    Example: const doc = await MyDocument.getOrCreate({ name: "Adam" });.

    In this example, if the query findOne({ name: "Adam" }) returns no matches, then the query is converted to a new MyDocument object and returned.

    The query should be limited to only simple matches, with names and values intended for direct inclusion in a new document. If you need more complex query features like ranges, use findOne(query) directly.

  • MyDocument.find( query, opts ) → Promise(cursor)

    The cursor given here is a proxy cursor that emits objects of the proper type.

    • projection(fields)cursor, limit the fields fetched on each item.
    • next()MyDocument, return a Promise that resolves to the next item, once it's available, or null if the cursor is empty.
    • skip(n)cursor, skip some items in the cursor.
    • limit(n)cursor, read at most n items.
    • filter(qry)cursor, only emit items that match the query object.
    • sort(keys)cursor, sort the results of the cursor.
    • count()number, return the number of items available to the cursor.
    • each(cb)cursor, calls cb(doc) for every result, each doc is an instance of MyDocument.
    • toArray(cb)cursor, calls cb(array), with an array of MyDocument instances.
    • [Symbol.asyncIterator], the cursor can be read in a for await (const item in cursor) loop.
  • MyDocument.updateOne( query, update, [opts] ) → Promise(UpdateResult)

  • MyDocument.updateMany( query, update, [opts] ) → Promise(UpdateResult)

  • MyDocument.deleteOne( query, [opts] ) → Promise(DeleteResult)

  • MyDocument.deleteMany( query, [opts] ) → Promise(DeleteResult)

  • MyDocument.remove( query, [opts] ) → Promise(DeleteResult)

    Deprecated. Same as deleteMany.

  • MyDocument.createIndexes( fields, opts ) → Promise(IndexResult)

    Example: MyDocument.createIndexes({ name: 1 }, { unique: true }).