lowgoose
lowdb mongoose-like wrapper
Last updated 2 years ago by 8e62726f640a .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install lowgoose 
SYNC missed versions from official npm registry.

Lowgoose

lowdb mongoose-like wrapper

npm version volkswagen status Coverage Status js-standard-style

Install

npm install lowgoose

Usage

const lowgoose = require('lowgoose')

lowgoose.open('db.json')

const Schema = new lowgoose.sch({
  name: {
    first: {
      type: String,
      required: true
    },
    last: {
      type: String,
      required: true
    }
  },
  age: {
    type: Number
  },
  email: {
    type: String,
    unique: true
  }
})

Schema.methods.fullName = function() {
  let self = this
  return `${self.name.first} ${self.name.last}`
}

const Collection = lowgoose.col('people', Schema)

let Document = new lowgoose.col.people({
  name: {
    first: 'Rick',
    last: 'Taylor'
  },
  age: 24,
  email: 'rick@google.com'
})

Document.fullName() // Rick Taylor

Why

Although lowdb is a champ all by itself, I really missed having schema's and other things I'd get from mongoose.

If you know anything about lowdb, you'll know that I haven't included almost any of the cool stuff it provides, I'm slowly adding features but also trying to keep it very mongoose-like.

Overview

Generally speaking, lowgoose tries to emulate the mongoose odm wrapper for lowdb.

Lowgoose is not production ready.

Most things will return a Document or an Error, unlike mongoose or lowdb there's currently no callbacks, chaining or promises. This isn't by design, I couldn't figure out a nice way to tie in the validator with chains or promises, feel free to make a PR to add this.

If something doesn't work the same way as it does in mongoose and isn't documented here, don't worry, it's probably just an undocumented feature.

Also, incase the names are confusing you:

sch === Schema
col === Collection
doc === Document

Open

lowgoose.open(filename)

The lowgoose.open() function will set lowgoose.database as a lowdb database instance and also return that instance.

const lowgoose = require('lowgoose')
const database = lowgoose.open('db.json')
// database === low('db.json')

You can use that reference or lowgoose.database to do all the things you normally would with lowdb.

Schema

lowgoose.sch(Schema)

Similar to mongoose, schemas are defined with objects.

Define

const Schema = new lowgoose.sch({
  name: {
    type: String,
    required: true
  },
  ...
})

Validation

Similar to mongoose, performing validation will return Error objects, not throw them.

Custom validation is not implemented, the validation process will need to be cleaned up before this is added.

type
Supports: [String, Number, Boolean, Object, Array, [String], [Number] ...]
Error: [prop].constructor.name !== type
Example: { type: Number }
unique
Supports: [true, false]
Error: [col].find({ prop: value }).length > 0
Example: { unique: true }
required
Supports: [true, false]
Error: value === undefined
Example: { required: true }
enum
Supports: [[String], [Number], [Boolean(ಠ_ಠ)]]
Error: enum.indexOf(value) < 0
Example: { enum: ['left', 'right', 'up', 'down'] }
min
Supports: [1, 2, 3, ...]
Error: value < min
Example: { min: 10 }
max
Supports: [1, 2, 3, ...]
Errors: value > max
Example: { max: 20 }
minlength
Supports: [1, 2, 3, ...]
Error: value.length < minlength
Example: { minlength: 4 }
maxlength
Supports: [1, 2, 3, ...]
Error: value.length > maxlength
Example: { maxlength: 16 }
ref
Supports: [String]
Error: [prop].constructor.name !== type
Example: { ref: 'people' }

References to other collections are tricky, there isn't any validation to check if the ref collection exists.. References support both single and array type values.

Statics

Similar to mongoose, statics are stored at lowgoose.col.myCol.prototype[static] or any reference to a lowgoose.col() function.

const Schema = new lowgoose.sch({ ... })
Schema.statics.myStatic = function() { ... }
const Collection = lowgoose.col('myCol', Schema)
Collection.myStatic()
// or
lowgoose.col.myCol.myStatic()

Methods

Similar to mongoose, methods are available at Document.prototype[method]

const Schema = new lowgoose.sch({ ... })
Schema.methods.myMethod = function() { ... }
const Collection = lowgoose.col('myCol', Schema)
let Document = new lowgoose.col.myCol({ ... })
Document.myMethod()

Collection

lowgoose.col(name, Schema)

Sort of similar to a mongoose model, use collections to group documents.

To create a collection, first define a Schema and then pass it into the lowgoose.col() function along with a name.

const Schema = new lowgoose.sch({ ... })
const Collection = lowgoose.col('myCol', Schema)

The collection above can be referenced via the Collection constant or lowgoose.col.myCol, from here you can access all of the custom and default Schema statics.

When you execute logoose.col() it sets a new default on lowdb with the collection name.

Default Statics

find(query)

Returns an array of Document objects.

Uses the lowdb filter() function.

lowgoose.col.myCol.find({ name: 'Jason' })
update(query, changes)

Returns an array of updated Document objects.

Uses the lowdb filter() function, then iterates over each Document, applies the changes and finally performs a save().

lowgoose.col.myCol.update({ age: 20 }, { teenager: false })
remove(query)

Returns an array of removed Document objects.

Uses the lowdb remove() function.

lowgoose.col.myCol.remove({ age: 20 })

Document

#####lowgoose.col[col]({ ... }) Similar to mongoose, documents are provided as objects constructed from the Schema.

Documents are not undef safe, assign properties with care.

ID

By default, a uuid will be set at Document.id, you can override the id value at any time - just make sure it's unique. You can use a Number if you want but I don't test for it so your milage may vary.

Internally this ID value is used a lot to reference documents for things like removing, updating and finding, so if you muck it up - you'll know.

Default Methods

save()

Returns the Document object.

Uses the lowdb filter() function to find and update an existing document using the Document.id or the lowdb push() function for a new document.

let Document = new lowgoose.col.myCol({ ... })
// or
let Document = lowgoose.col.myCol.find({ ... })[0]
Document.save()
remove()

Returns the removed Document object.

Uses the lowdb remove() function based on the Document.id.

let Document = new lowgoose.col.myCol({ ... })
// or
let Document = lowgoose.col.myCol.find({ ... })[0]
Document.remove()
validate()

Returns the validation result of a Document object.

Uses the internal Document.validator() function.

let Document = new lowgoose.col.myCol({ ... })
Document.validate() // Error or Document
toObject()

Returns a plain old javascript object of the Document.

Uses the recursiveDeepCopy() function from jsperf.

let Document = new lowgoose.col.myCol({ ... })
let pojo = Document.toObject()
pojo.save() // TypeError: pojo.save is not a function
populate(path || [path1, path2])

Returns the Document with populated references.

Accepts a String or [String] of paths to populate.

Uses lowdb find() based on the Schema ref value and Document.id value.

let Document = new lowgoose.col.myCol({ ... })
let Populated = Document.populate('friends')
// or
let Populated = Document.populate(['friends', 'enemies']])

Current Tags

  • 0.0.3                                ...           latest (2 years ago)

4 Versions

  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
  • 0.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (12)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |