Skip to content

jd4u/gcloud-datastore-schema

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gcloud-datastore-schema

Define and follow a schema for your Datastore entities

$ npm install --save gcloud-datastore-schema
var gcloud = require('gcloud');
var schema = require('gcloud-datastore-schema');

var dataset = schema(gcloud.datastore.dataset({/*...*/}));

// New method: register
dataset.register('Person', {
  // Native JavaScript types
  name: String,
  age: Number,
  tools: Array,
  isPremiumUser: Boolean,

  // Datastore types
  gpa: gcloud.datastore.double,

  // Nested schemas
  address: {
    streetNumber: Number,
    streetName: String,
    // Nested nested schemas
    zip: {
      firstPart: Number,
      secondPart: Number
    }
  },

  // Custom validators
  fullName: function (input) {
    return input.split(' ').length > 1;
  }
});

// The original save method will now validate by default
// So let's make it mad
dataset.save({
  key: dataset.key(['Person']),
  data: {
    name: 'Doc',
    age: 8,
    tools: ['Stethoscope', 'Positive attitude'],
    gpa: gcloud.datastore.int(4.0),
    address: {
      streetNumber: 123,
      streetName: 'Main',
      zip: {
        firstPart: 12345,
        secondPart: 4444
      }
    },
    fullName: 'Doc',
    extraData: true,
    extraExtraData: false
  }
}, function (err) {
  // err:
  // {
  //   code: 'ESCHEMAVIOLATION',
  //   message: 'Schema validation failed',
  //   errors: [
  //     {
  //       kind: 'Person',
  //       errors: [
  //         'Schema definition expected property: isPremiumUser',
  //         'Schema definition violated for property: "gpa". Expected type: Double, received: Int',
  //         'Schema definition violated for property: fullName',
  //         'Unexpected properties found: extraData, extraExtraData'
  //       ]
  //     }
  //   ]
  // }
})

API Documentation

Coming soon, sorry. For now, please use the above example as a guide.

About

Define and follow a schema for your Datastore entities

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%