Skip to content

Commit

Permalink
datastore: support kind schema. fixes googleapis#85 googleapis#86
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 30, 2014
1 parent d539bf0 commit ef45b39
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 112 deletions.
58 changes: 58 additions & 0 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,64 @@ Dataset.prototype.key = function(keyConfig) {
return new entity.Key(keyConfig);
};

/**
* Register a Kind's schema. You can later refer to the schema to validate an
* entity. See `validateKind` for an example.
*
* @param {string} kind - Name of the kind.
* @param {object} schema - Schema to register to the kind.
*
* @example
* ds.registerKind('Person', {
* name: {
* type: String
* },
* age: {
* type: datastore.int
* }
* });
*/
Dataset.prototype.registerKind = function(kind, schema) {
var namespace = this.namespace;
if (util.is(kind, 'object')) {
namespace = kind.namespace;
schema = kind.schema;
kind = kind.name;
}
entity.registerKind(namespace, kind, schema);
};

/**
* Validate a registered Kind's schema against an entity. This can be useful
* before saving a new entity to the Datastore.
*
* @param {string} kind - Name of the kind.
* @param {object} ent - Object to validate against the kind's schema.
* @return {booelan}
*
* @example
* // See how we registered the "Person" schema in the `registerKind` example.
*
* ds.validateKind('Person', {
* name: 'Abe Vigoda',
* age: datastore.int(93)
* });
* // true (`name` is a String, and `age` is an integer)
*
* ds.validateKind('Person', {
* name: 'Abe Vigoda'
* });
* // false (missing the `age` property)
*/
Dataset.prototype.validateKind = function(kind, ent) {
var namespace = this.namespace;
if (util.is(kind, 'object')) {
namespace = kind.namespace;
ent = kind.entity;
kind = kind.name;
}
return entity.validateKind(namespace, kind, ent);
};

/**
* Create a query from the current dataset to query the specified kinds, scoped
Expand Down
Loading

0 comments on commit ef45b39

Please sign in to comment.