Skip to content

Commit ef45b39

Browse files
datastore: support kind schema. fixes googleapis#85 googleapis#86
1 parent d539bf0 commit ef45b39

File tree

6 files changed

+429
-112
lines changed

6 files changed

+429
-112
lines changed

lib/datastore/dataset.js

+58
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,64 @@ Dataset.prototype.key = function(keyConfig) {
129129
return new entity.Key(keyConfig);
130130
};
131131

132+
/**
133+
* Register a Kind's schema. You can later refer to the schema to validate an
134+
* entity. See `validateKind` for an example.
135+
*
136+
* @param {string} kind - Name of the kind.
137+
* @param {object} schema - Schema to register to the kind.
138+
*
139+
* @example
140+
* ds.registerKind('Person', {
141+
* name: {
142+
* type: String
143+
* },
144+
* age: {
145+
* type: datastore.int
146+
* }
147+
* });
148+
*/
149+
Dataset.prototype.registerKind = function(kind, schema) {
150+
var namespace = this.namespace;
151+
if (util.is(kind, 'object')) {
152+
namespace = kind.namespace;
153+
schema = kind.schema;
154+
kind = kind.name;
155+
}
156+
entity.registerKind(namespace, kind, schema);
157+
};
158+
159+
/**
160+
* Validate a registered Kind's schema against an entity. This can be useful
161+
* before saving a new entity to the Datastore.
162+
*
163+
* @param {string} kind - Name of the kind.
164+
* @param {object} ent - Object to validate against the kind's schema.
165+
* @return {booelan}
166+
*
167+
* @example
168+
* // See how we registered the "Person" schema in the `registerKind` example.
169+
*
170+
* ds.validateKind('Person', {
171+
* name: 'Abe Vigoda',
172+
* age: datastore.int(93)
173+
* });
174+
* // true (`name` is a String, and `age` is an integer)
175+
*
176+
* ds.validateKind('Person', {
177+
* name: 'Abe Vigoda'
178+
* });
179+
* // false (missing the `age` property)
180+
*/
181+
Dataset.prototype.validateKind = function(kind, ent) {
182+
var namespace = this.namespace;
183+
if (util.is(kind, 'object')) {
184+
namespace = kind.namespace;
185+
ent = kind.entity;
186+
kind = kind.name;
187+
}
188+
return entity.validateKind(namespace, kind, ent);
189+
};
132190

133191
/**
134192
* Create a query from the current dataset to query the specified kinds, scoped

0 commit comments

Comments
 (0)