diff --git a/README.md b/README.md index a2f9134..201d9cd 100644 --- a/README.md +++ b/README.md @@ -113,4 +113,90 @@ result = { } } */ +``` + +The only control you have is fragments. So this library provides base fragments with all table fields without relations. Of course you need them, so you have many ways to do so. +```javascript + +//here is an example of simple query +var [err, response] = orm.query({ + user: {} +}) + +//So here some examples with fields key +var [err, response] = orm.query({ + user: { + fields: ` + name + posts { + title + } + `, + + //or + fields: [ + 'name', + { + key: 'posts', + values: [ + 'title' + ] + } + ], + + //or + fields: { + name: null, + posts: { + children: { + title: null + } + } + } + } +}) + +//or we can create new Fragment +let newFragment = new Fragment({ + name: 'some_unique_name', + table: 'user', + fields: ` + name + posts { + title + } + `//any from abobe +}) +var [err, response] = orm.query({ + user: { + fragment: newFragment + } +}) + +//or even better, we can extend user fragments and use it anytime +orm.table('user').createFragment('some_unique_name', ` + name + posts { + title + } +`) +var [err, response] = orm.query({ + user: { + fragment: 'some_unique_name' + } +}) + +//of course we can use other fragments to create new one +let baseUserFragment = orm.table('user').fragment('base') +let basePostFragment = orm.table('post').fragment('base') + +orm.table('user').createFragment('some_unique_name', [ + baseUserFragment.gqlFields(), + { + key: 'posts', + values: [ + basePostFragment.gqlFields(), + ] + } +]) ``` \ No newline at end of file diff --git a/package.json b/package.json index eacad35..f06c8ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hasura-om", - "version": "0.0.2", + "version": "0.0.6", "description": "Hasura object mapping", "main": "src/index.js", "scripts": { diff --git a/src/gql/fragment.js b/src/gql/fragment.js index 901098f..536531e 100644 --- a/src/gql/fragment.js +++ b/src/gql/fragment.js @@ -12,12 +12,16 @@ class Fragment { if (!this.params.table) throw new Error('table is required'); - this.gqlFields = fieldsToGql(this.params.fields); + this._gqlFields = fieldsToGql(this.params.fields); + } + + gqlFields() { + return this._gqlFields; } toString() { let fragmentName = `${this.params.name}_fragment_${this.params.table}`; - let fields = this.gqlFields; + let fields = this._gqlFields; return { name: fragmentName, diff --git a/src/index.js b/src/index.js index e66f56d..c336082 100644 --- a/src/index.js +++ b/src/index.js @@ -90,6 +90,12 @@ class Hasura { this.INITIATED = true; } + table(name) { + if (typeof this.tables[name] == 'undefined') throw new Error(`table ${name} not found`); + + return this.tables[name]; + } + /* { [table_name]: { diff --git a/src/table.js b/src/table.js index c348907..fcf9cfd 100644 --- a/src/table.js +++ b/src/table.js @@ -18,6 +18,12 @@ class Table { this.fragments = {}; } + field(name) { + if (typeof this.fields[name] == 'undefined') throw new Error(`field ${name} not found`); + + return this.fields[name]; + } + setField(params) { if (!params.name) throw new Error('name is required'); if (!params.type) throw new Error('type is required'); @@ -35,6 +41,12 @@ class Table { this.createFragment(); } + fragment(name) { + if (typeof this.fragments[name] == 'undefined') throw new Error(`fragment ${name} not found`); + + return this.fragments[name]; + } + createFragment(name = 'base', fields = false) { this.fragments[name] = new Fragment({ table: this.params.name, diff --git a/tests/table.js b/tests/table.js new file mode 100644 index 0000000..e169f23 --- /dev/null +++ b/tests/table.js @@ -0,0 +1,74 @@ +require('dotenv').config(); +const test = require('ava'); +const { Table } = require('../src/'); + +test('throws without params', (t) => { + t.throws( + () => { + const orm = new Table(); + }, + { instanceOf: Error }, + ); +}); + +test('still throws without params', (t) => { + t.throws( + () => { + const orm = new Table({ + name: 'ufhreioor', + }); + }, + { instanceOf: Error }, + ); +}); + +test('succesful contructor', (t) => { + let table = new Table({ + name: 'test', + type: 'BASE TABLE', + }); + + t.true(table instanceof Table); +}); + +test('throws adding field', (t) => { + let table = new Table({ + name: 'test', + type: 'BASE TABLE', + }); + + t.throws( + () => { + table.setField(); + }, + { instanceOf: Error }, + ); +}); + +test('succesfuly add field', (t) => { + let table = new Table({ + name: 'test', + type: 'BASE TABLE', + }); + + table.setField({ + name: 'test', + type: 'Integer', + }); + + t.is(table.field('test').type, 'Integer'); +}); + +test('field not found', (t) => { + let table = new Table({ + name: 'test', + type: 'BASE TABLE', + }); + + t.throws( + () => { + table.field('test'); + }, + { instanceOf: Error }, + ); +});