Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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