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 ofFoo
will read/write to the"foos"
collection, using whatever database you pass toDocument.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 newMyDocument
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 mostn
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
, callscb(doc)
for every result, each doc is an instance of MyDocument.toArray(cb)
→cursor
, callscb(array)
, with an array of MyDocument instances.[Symbol.asyncIterator]
, the cursor can be read in afor 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 })
.