Skip to content

Instantly share code, notes, and snippets.

@bentranter
Last active August 29, 2015 14:21
Show Gist options
  • Save bentranter/471954731bbf9e00db02 to your computer and use it in GitHub Desktop.
Save bentranter/471954731bbf9e00db02 to your computer and use it in GitHub Desktop.
Thinky Schema Example

Possible error in the Thinky docs at the post example. It says that createdAt should be type.string().default(r.now()), but I think it's supposed to say type.date().default(r.now()).

See the code below for an example, it should throw an error because of line 19.

This is the error message caused by running $ node thinky-schema-example.js:

Possibly unhandled Document failed validation: Value for [createdAt] must be a string or null.
    at Object.looseType (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/util.js:304:11)
    at TypeString.validate (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/type/string.js:318:12)
    at /Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/type/object.js:136:19
    at Object.loopKeys (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/util.js:203:16)
    at TypeObject.validate (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/type/object.js:135:10)
    at model.Document._validateHook (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/document.js:256:28)
    at _syncHook (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/util.js:129:19)
    at Object.hook (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/util.js:109:12)
    at model.Document.validate (/Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/document.js:201:15)
    at /Users/Ben/js/thinky-schema-example/node_modules/thinky/lib/document.js:828:26
// Setup thinky
var thinky = require('thinky')({
host: process.env.RDB_HOST || 'localhost',
port: parseInt(process.env.RDB_PORT || 28015),
db: process.env.RDB_DB || 'example'
});
// Keep reference to RethinkDB's utilities
var r = thinky.r;
var type = thinky.type;
// This won't throw an error
var WorkingModel = thinky.createModel('Working', {
createdAt: type.date().default(r.now())
});
// This WILL throw an error
var BrokenModel = thinky.createModel('Broken', {
createdAt: type.string().default(r.now())
});
var working = new WorkingModel({});
var broken = new BrokenModel({});
working.save();
broken.save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment