Coder Social home page Coder Social logo

nothinkdb's Introduction

npm version Build Status

Nothinkdb

Functional toolkit for rethinkdb.

Currently, Compitable with rethinkdb 2.2.x

  • define declarative table schema.
  • handle schema validation with joi.
  • handle default fields like id, createdAt, updatedAt.
  • ensure table, secondary index.
  • ensure unique field.
  • fully customizable 1-n, 1-1, n-1, n-m relations.
  • define, create, remove, check, query, join relations.
  • many useful query generator.
  • easily implement graphql server with nothinkdb-graphql

Install

npm install -S nothinkdb

Example

import Joi from 'joi';
import r from 'rethinkdb';
import { Table, schema } from 'nothinkdb';

const userTable = new Table({
  tableName: 'user',
  schema: () => ({
    id: schema.id,
    name: Joi.string().required(),
    isPremium: Joi.boolean().default(false),
  }),
});

async function run() {
  // open rethinkdb connection
  const connection = await r.connect({ db: 'test' });

  // sync table
  await userTable.sync(connection);
  await followingTable.sync(connection);

  // create user data
  const normalUser = userTable.create({ name: 'user1' });
  const premiumUser = userTable.create({ name: 'user2', isPremium: true });

  // insert user data into rethinkdb server
  await userTable.insert([
    normalUser,
    premiumUser,
  ]).run(connection);

  // getAll users
  const users = await userTable.query().coerceTo('array').run(connection);

  console.log(users);

  // close rethinkdb connection
  await connection.close();
}

run();

If you want to see more examples, See the Examples

API

See the API Reference.

Related Links

nothinkdb's People

Contributors

ironhee avatar

Stargazers

Burak Sormageç avatar ChulHee Lee avatar Mehmet Kose avatar Extarys avatar Paul Robello avatar David Roman avatar Pawel Biernacki avatar simdi jinkins avatar Jamie Barton avatar Alexey Yakimanskiy avatar Aleksandr Kukhta avatar Guten avatar Raitis Stengrevics avatar Reid D McKenzie avatar Brandon Bloom avatar win avatar 斯人 avatar Seho Noh avatar dalan avatar Ricardo Torres avatar Mohammad Sadegh Khoeini avatar Brad Pillow avatar  avatar Jungwoo Lee avatar Dan avatar Sunil Pai avatar Zaher Ghaibeh avatar Lorefnon avatar Josh Burgess avatar Bazyli Brzóska avatar Adrian Nilsson avatar Guido avatar Anna Stith avatar Michel avatar devdoomari avatar  avatar Jeong Seong Dae avatar

Watchers

James Cloos avatar Kieron Richardson avatar Jeong Seong Dae avatar

nothinkdb's Issues

Make sync return query

Now, Table.sync & Environment.sync receive connection as argument.
Change that functions return query, For it could be used with another ecosystem like rethinkdb-pool.

Example in readme.md does not work

When running the example in the readme as-is, I see the following error:

ValidationError: {
  "table": "user",
  "tableName" [1]: -- missing --
}

ok... after changing table to tableName, the error disappears but nothing else happens (I'm expecting to see the newly created users in the console).

The example from rethinkdb works fine.

Rooms for improvement

Structural

  • Table#create and Table#attempt are duplicate.
  • Why should Table#insert must be accompanied by Table#create? Schema that is passed in the constructor is only good for Table#create where Table#insert is just a raw rethinkdb insert. Table#insert should call Table#create with schema before inserting in the database.
  • Why should link be exposed? Suggestion:
// before
bar: hasOne(fooTable.linkedBy(barTable, 'fooId'))
bar: belongsTo(fooTable.linkTo(barTable, 'barId'))
bars: hasMany(fooTable.linkedBy(barTable, 'fooId'))
// after
bar: hasOne(barTable, 'fooId')
bar: belongsTo(barTable, 'barId')
bars: hasMany(barTable, 'fooId')
  • Table#createRelation has inconsistent spec.
// before
fooTable.createRelation('bar', foo.id, bar.id)
// after
fooTable.createRelation(foo.id, 'bar', bar)
  • Table#createRelation should accept other object instead other.id. Table#foreignKey can be any other field than pk, and for Table#createRelation of belongsTo, right.table.get(otherPk) must be fetched with foreignKey that is not necessarily pk.

Test

  • After tests are run, tables are still there. Whatever created during test must be cleared after.
  • Bi-direction relationship is not tested.
  • Table#withJoin is not tested with relations as object. I'm not even sure what it does.

Implementation Questions

  • In relations.js, what's the point of these:
apply = query => query;

query = r.branch(
  row(right.field),
  query.getAll(row(right.field), { index: left.field }),
  r.expr([]),
);

Code still works with apply and branch.

rethinkdb to rethinkdbdash ?

They are quite similar but the second offer much more options and is developped by someone on the RethinkDB team.

Could it be possible to use this instead?

Add practical examples.

  • basic relation examples
    • hasOne
    • belongsTo
    • hasMany
    • belongsToMany
  • extending examples
    • Table
  • todo example

Create Environment class.

create Environment class.

const env = new Environment();
const fooTable = env.createTable({ ... });

await env.sync()  // sync all tables
env.getTable('foos');  // same as fooTable
env.getTableList('foos');  // [...]

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.