GithubHelp home page GithubHelp logo

thinkagain's Introduction

thinkagain

Build Status Test Coverage

ThinkAgain is a fork of thinky with first-class support for json-schema using ajv for validation.

Install

npm install --save thinkagain

Quick Start

const thinkagain = require('thinkagain')(/* rethinkdbdash options */);

// Create a model - the table is automatically created
let Post = thinkagain.createModel('Post', {
  type: 'object',
  properties: {
    id: { type: 'string' },
    title: { type: 'string' },
    content: { type: 'string' },
    idAuthor: { type: 'string' }
  },
  required: [ 'title' ]
});

let Author = thinkagain.createModel('Author', {
  type: 'object',
  properties: {
    id: { type: 'string' },
    name: { type: 'string' }
  },
  required: [ 'name' ]
});

// Join the models
Post.belongsTo(Author, 'author', 'idAuthor', 'id');

// Create a new post
let post = new Post({
  title: 'Hello World!',
  content: 'This is an example.'
});

// Create a new author
var author = new Author({
  name: 'Llama'
});

// Join the documents
post.author = author;

// Save everything
post.saveAll()
  .then(result => console.log(result));

/*
output:
{
  id: '0e4a6f6f-cc0c-4aa5-951a-fcfc480dd05a',
  title: 'Hello World!',
  content: 'This is an example.',
  idAuthor: '3851d8b4-5358-43f2-ba23-f4d481358901',
  author: {
    id: '3851d8b4-5358-43f2-ba23-f4d481358901',
    name: 'Llama'
  }
}
*/

Notes

Presently this is, for the most part, a drop-in replacement for thinky, with a few notable changes:

  • Table schemas explicitly use json-schema instead of thinky's home brewed schemas
  • Virtual fields are not (yet) implemented
  • All validation is asynchronous
  • All validation options (enforce_missing, enforce_extra, enforce_type) have been removed in favor of equivalents in either json-schema itself, or options in ajv.
  • Node.js 4+ is required

Credits

  • Many thanks to neumino for his great work on thinky, without which this module would not be possible.
  • Additional thanks to epoberezkin for his fantastic work on ajv.

thinkagain's People

Contributors

mbroadst avatar

Stargazers

Lynsei avatar Ming-der Wang avatar Ivan Zhuravlev avatar Ricardo Matos avatar Amir Irani avatar Martin avatar Miguel Vaello avatar Kevin avatar Andreas Urbanski avatar Martin avatar  avatar Zach Botterman avatar  avatar Tony Garcia avatar Nir Galon avatar Zaher Ghaibeh avatar Justin avatar Alexey Yakimanskiy avatar Erik Vavro avatar  avatar  avatar Patrick Lienau avatar Josh Burgess avatar Joshua Wiens avatar Alexey Golev avatar Lawrence Carvalho avatar Marshall Cottrell avatar chrismcc avatar Dan Lynch avatar Bruno Wego avatar

Watchers

Martin avatar James Cloos avatar Erik Vavro avatar  avatar

thinkagain's Issues

How to define table durability, sharding and replication?

Hi,

I tried to pass some additional options to createModel that would allow me to define table durability, sharding and replication as supported by Rethink's tableCreate. I assumed the following configuration would work:

thinkagain.createModel('MyModel', <schema>, {
      durability: 'hard',
      shards: 3,
      replicas: 1      
    }
);

but apparantly passing these options is not supported. Is there any other way to set these options?

additionalProperties:false not working

Hello there.
Great package btw. I enjoy json schema much better than the standard thinky schema lang.
I want to make sure additional properties can't be set in the db, when saving new data. I noticed you used json schema validation for this, so I've tried using the additionalProperties flag without much luck. I can still add properties that are not defined in the schema.
Shouldn't the following work?

var Subscriptions = thinkagain.thinky.createModel('subscriptions', {
    type:"object",
    "additionalProperties":false,
    properties:{
    ...
    } 
   }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.