Socket
Socket
Sign inDemoInstall

polyclay

Package Overview
Dependencies
45
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

lib/levelup.js

1

index.js

@@ -7,1 +7,2 @@ exports.Model = require('./lib/polyclay').Model;

exports.RedisAdapter = require('./lib/redis');
exports.LevelupAdapter = require('./lib/levelup');

2

lib/persistence.js

@@ -60,3 +60,3 @@ // General storage interface.

if (modelfunc.prototype.__types[name].indexOf('text') === 0)
body = body.toString('utf8');
body = body ? body.toString('utf8') : '';
self.__attachments[name].body = body;

@@ -63,0 +63,0 @@ callback(null, body);

@@ -161,6 +161,2 @@ // Redis storage interface.

RedisAdapter.prototype.constructMany = function(documents)
{
};
var attachpat = /^attach:(.*)/;

@@ -167,0 +163,0 @@ RedisAdapter.prototype.inflate = function(payload)

{
"name": "polyclay",
"version": "1.1.0",
"description": "a schema-enforcing model class for node with optional couch persistence",
"version": "1.2.0",
"description": "a schema-enforcing model class for node with optional key-value store persistence",
"main": "index.js",

@@ -30,9 +30,10 @@ "directories":

"async": "*",
"cradle": "git://github.com/ceejbot/cradle.git",
"lodash": "*",
"redis": "*"
"lodash": "*"
},
"optionalDependencies":
{
"hiredis": "*"
"cradle": "git://github.com/ceejbot/cradle.git",
"levelup": "*",
"hiredis": "*",
"redis": "*"
},

@@ -50,5 +51,7 @@ "devDependencies":

"model",
"mapper",
"couchdb",
"couch",
"redis",
"levelup",
"schema"

@@ -58,4 +61,3 @@ ],

"license": "MIT",
"readmeFilename": "README.md",
"gitHead": "3d5f3c31e340aa452f7833c4b5beea5025bc58ae"
"readmeFilename": "README.md"
}
# Polyclay
Polymer modeling clay for node.js. A model schema definition with type validations, dirty-state tracking, and rollback. Models are optionally persistable to CouchDB using [cradle](https://github.com/cloudhead/cradle). Polyclay gives you the safety of type-enforcing properties without making you write a lot of boilerplate. New! You can now persist in [Redis](http://redis.io/) if your data fits in memory and you need very fast access to it.
Polymer modeling clay for node.js. A model schema definition with type validations, dirty-state tracking, and rollback. Models are optionally persistable to CouchDB using [cradle](https://github.com/cloudhead/cradle), [Redis](http://redis.io/), or [LevelUP](https://github.com/rvagg/node-levelup). Polyclay gives you the safety of type-enforcing properties without making you write a lot of boilerplate.
Current version: __1.0.1__
Current version: __1.2.0__

@@ -68,3 +68,3 @@ [![Build Status](https://secure.travis-ci.org/ceejbot/polyclay.png)](http://travis-ci.org/ceejbot/polyclay)

*Reference* properties are pointers to other Couch-persisted objects. When Polyclay builds a reference property, it provides two sets of getter/setters. First, it defines a `model.reference_id` property, which is a string property that tracks the `_id` of the referred-to object. It also defines `model.reference()` and `model.set_reference()` functions, used to define the js property `model.reference`. This provides runtime-only access to the pointed-to object. Inflating it later is an exercise for the application using this model and the persistence layer.
*Reference* properties are pointers to other polyclay-persisted objects. When Polyclay builds a reference property, it provides two sets of getter/setters. First, it defines a `model.reference_id` property, which is a string property that tracks the `key` of the referred-to object. It also defines `model.reference()` and `model.set_reference()` functions, used to define the js property `model.reference`. This provides runtime-only access to the pointed-to object. Inflating it later is an exercise for the application using this model and the persistence layer.

@@ -87,7 +87,7 @@ In this example, widgets have an `owner` property that points to another object:

widget.owner = marvin; // marvin is an object we have already from somewhere else
assert(widget.owner_id === marvin._id);
assert(widget.owner_id === marvin.key);
assert(widget.owner === marvin);
widget.save(function(err)
{
var id = widget._id.
var id = widget.key.
Widget.get(id, function(err, dbwidget)

@@ -97,3 +97,3 @@ {

// but not the full marvin object
assert(dbwidget.owner_id === marvin._id);
assert(dbwidget.owner_id === marvin.key);
assert(dbwidget.owner === undefined);

@@ -140,3 +140,3 @@ });

## Persisting in CouchDB or Redis
## Persisting in CouchDB, Redis, or LevelUP

@@ -172,3 +172,3 @@ Once you've built a polyclay model, you can mix persistence methods into it:

};
RedisModelFunc.setStorage(options, polyclay.RedisAdapter);
ModelFunction.setStorage(options, polyclay.RedisAdapter);
```

@@ -178,2 +178,15 @@

For LevelUP:
```javascript
var options =
{
dbpath: '/path/to/leveldb/dir',
dbname: 'widgets'
};
ModelFunction.setStorage(options, polyclay.LevelupAdapter);
```
The Levelup object is available at `obj.adapter.db`. The attachments data store is available at `obj.adapter.attachdb`.
### Defining views

@@ -197,3 +210,3 @@

After you call `Widget.provision()`, the 'widgets' database will exist. It will have a design document named "_design/widgets" with the two views above defined.
After you call `Widget.provision()`, the 'widgets' database will exist. It will have a design document named "_design/widgets" with the two views above defined. Does nothing for Redis- or LevelUP-backed models.

@@ -204,3 +217,3 @@ ### Persistence class methods

Create the database the model expects to use in couch. Create any views for the db that are specified in the `design` field.
Create the database the model expects to use in couch. Create any views for the db that are specified in the `design` field. Does nothing for Redis and LevelUP.

@@ -217,7 +230,7 @@ `ModelFunction.get(id, function(err, object))`

Takes a list of couch response documents produced by calls to couch views, and uses them to inflate objects. You will use this class method when writing wrappers for couch views. For a simple example, see class Comment's findByOwner() method below.
Takes a list of couch response documents produced by calls to couch views, and uses them to inflate objects. You will use this class method when writing wrappers for couch views. For a simple example, see class Comment's findByOwner() method below. (Not exercised by the other adapters.)
`ModelFunction.destroyMany(idArray, function(err, couchResponseArray))`
`ModelFunction.destroyMany(idArray, function(err, response))`
Takes a list of object ids to remove. Responds with err if any failed, and an array of responses from couch.
Takes a list of object ids to remove. Responds with err if any failed, and an array of responses from couch or redis.

@@ -227,13 +240,13 @@

`obj.save(function(err, couchResponse))`
`obj.save(function(err, response))`
Save the model to the db. Works on new objects as well as updated objects that have already been persisted. If the object was not given an `_id` property before the call, the property will be filled in with whatever couch chose. Does nothing if the object is not marked as dirty.
Save the model to the db. Works on new objects as well as updated objects that have already been persisted. If the object was not given a `key` property before the call, the property will be filled in with whatever couch chose. The LevelUP and Redis adapters both demand that you provide a key before calling save(). Does nothing if the object is not marked as dirty.
`obj.destroy(function(err, wasDestroyed))`
Removed the object from couch and set its `destroyed` flag. The object must have an `_id`.
Removed the object from couch and set its `destroyed` flag. The object must have a `key`.
`obj.merge(hash, function(err, couchResponse))`
`obj.merge(hash, function(err, response))`
Update the model with fields in the supplied hash, then save the result to couch.
Update the model with fields in the supplied hash, then save the result to the backing store.

@@ -246,3 +259,3 @@ `obj.removeAttachment(name, function(err, wasRemoved))`

Initialize a model from data returned by couchdb. You are unlikely to call this, but it's available.
Initialize a model from data returned by the backing store. You are unlikely to call this, but it's available.

@@ -256,8 +269,8 @@

The prototype will have `get_name` and `set_name` functions added to it, wrapped into the property *name*. Also, `fetch_name` will be defined to fetch the attachment data asynchronously from couch. Attachment data is saved when the model is saved, not when it is set using the property.
The prototype will have `get_name` and `set_name` functions added to it, wrapped into the property *name*. Also, `fetch_name` will be defined to fetch the attachment data asynchronously from backing storage. Attachment data is saved when the model is saved, not when it is set using the property.
You can also save and remove attachments directly:
`obj.saveAttachment(name, function(err, couchResponse))`
`obj.removeAttachment(name, function(err, couchresponse))`
`obj.saveAttachment(name, function(err, response))`
`obj.removeAttachment(name, function(err, response))`

@@ -276,3 +289,3 @@ A simple example:

{
// attachment is now persisted in couch.
// attachment is now persisted in storage.
// Also, obj's _rev has been updated.

@@ -292,6 +305,6 @@ obj.avatar = null;

`afterLoad()`: after a document has been loaded from couch & a model instantiated
`beforeSave()`: before a document is saved to couch in `save()`
`afterSave()`: after a save to couch has succeeded, before callback
`beforeDestroy()`: before deleting a model from couch in `destroy()`
`afterLoad()`: after a document has been loaded from storage & a model instantiated
`beforeSave()`: before a document is saved to storage in `save()`
`afterSave()`: after a save to storage has succeeded, before callback
`beforeDestroy()`: before deleting a model from storage in `destroy()`

@@ -386,3 +399,3 @@ ## Mixins

if (typeof owner === 'object')
owner = owner._id;
owner = owner.key;

@@ -397,6 +410,10 @@ Comment.adapter.db.view('comments/by_owner', { key: owner }, function(err, documents)

polyclay.mixin(Comment, HasTimestamps); // as defined above
polyclay.persist(Comment);
polyclay.persist(Comment, '_id');
var cradleconn = new cradle.Connection();
Comment.setStorage(cradleconn, 'comments');
var opts =
{
connection: new cradle.Connection(),
dbname: 'comments'
};
Comment.setStorage(opts, polyclay.CouchAdapter);
Comment.provision(function(err, response)

@@ -424,22 +441,4 @@ {

## TODO
* Implement the original model builder using mixin machinery (augment mixins)
* Improve rollback behavior & write some vicious tests for it
* Implement saving already-persisted objects by means of merge()
* Rethink that enumerable implementation; probably should just denormalize enums to make them less fragile
* Consider skipping attachment fields when doing a basic model get with redis
* Mime types on attachments are a mess
* Should add a way to specify a key/id attribute name to generalize away from couchdb a bit ✓
* How much work would a generic key/value store version be? ✓
* Documentation ✓
* Persistence layer is tangled with model layer in a couple of places ✓
* Add mixins to the official library & document ✓
* Clean up attachments API ✓
* Settle on one of destroy/remove/delete (probably destroy) ✓
* Nuke the underscore in `_init` ✓
## License
MIT

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc