db2graphql
Advanced tools
Comparing version 0.4.1 to 0.4.2
{ | ||
"name": "db2graphql", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Generate Graphql schema based on existing relational database", | ||
@@ -32,12 +32,12 @@ "main": "index.js", | ||
"devDependencies": { | ||
"apollo-server": "^2.6.4", | ||
"coveralls": "^3.0.4", | ||
"graphql": "^14.3.1", | ||
"apollo-server": "^2.9.4", | ||
"coveralls": "^3.0.6", | ||
"graphql": "^14.5.8", | ||
"handlebars": ">=4.0.14", | ||
"jest": "^23.6.0", | ||
"jsdoc": "^3.6.3", | ||
"knex": "^0.16.5", | ||
"knex": "^0.19.4", | ||
"mysql": "^2.17.1", | ||
"nodemon": "^1.19.1", | ||
"pg": "^7.11.0" | ||
"nodemon": "^1.19.3", | ||
"pg": "^7.12.1" | ||
}, | ||
@@ -44,0 +44,0 @@ "jest": { |
@@ -368,4 +368,4 @@ const hash = require('string-hash-64'); | ||
name: rows[j].name, | ||
is_nullable: rows[j].is_nullable, | ||
data_type: rows[j].data_type | ||
is_nullable: rows[j]['IS_NULLABLE'] || rows[j]['is_nullable'], | ||
data_type: rows[j]['DATA_TYPE'] || rows[j]['data_type'] | ||
}); | ||
@@ -427,5 +427,6 @@ } | ||
AND tc.table_schema = ? | ||
AND tc.table_name = ?; | ||
AND tc.table_name = ? | ||
AND kcu.table_name = ?; | ||
`; | ||
let rows = await this.query(sql, [schemaname, tablename]); | ||
let rows = await this.query(sql, [schemaname, tablename, tablename]); | ||
return rows.length ? rows[0].columnname : pk; | ||
@@ -432,0 +433,0 @@ } |
@@ -51,2 +51,29 @@ const PostgreSQL = require('../src/adapters/postgres'); | ||
/** | ||
* Adds a Graphql field to the schema. | ||
* If it does not exists, it gets created. | ||
* | ||
* <p>Usage example:</p> | ||
* | ||
* <pre> | ||
* addField('Users.fullname', 'String', (user) => user.firstname + user.lastname) | ||
* </pre> | ||
* | ||
* @access public | ||
* @param {String} path The field path of the resolver ie. Query.getUser | ||
* @param {String|Array} returns The Graphql returning type ie. Boolean or 'User' or ['User'] | ||
* @param {Function} resolver The resolver callback | ||
* @param {Object} [params={}] The query arguments | ||
* | ||
* @returns {DB2Graphql} The self instance for fluent interface | ||
*/ | ||
addField(path, returns, resolver, params = {}) { | ||
let segments = path.trim().split('.').filter(i => !!i); | ||
if (segments.length < 2) throw new Error('addField path must be in format Type.field'); | ||
let field = segments.pop(); | ||
let type = segments.pop(); | ||
this.add(type, field, returns, resolver, params); | ||
return this; | ||
} | ||
/** | ||
* Adds a Graphql query to the schema. | ||
@@ -79,2 +106,16 @@ * If type does not exists, it gets created. | ||
* | ||
* <pre> | ||
* function validator(type, field, parent, args, context) | ||
* function rejected(type, field, parent, args, context) | ||
* </pre> | ||
* | ||
* Where: | ||
* <pre> | ||
* - type: Graphql type | ||
* - field: Graphql field | ||
* - parent: parent resolved | ||
* - args: request arguments | ||
* - context: context information | ||
* </pre> | ||
* | ||
* @param {Function} validator The validator callback. Must return true/false | ||
@@ -81,0 +122,0 @@ * @param {Function} rejected The rejected callback. Must return resolver type/null |
@@ -52,5 +52,4 @@ const Mysql = require('../../src/adapters/mysql'); | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -82,5 +81,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -116,5 +114,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -131,5 +128,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -166,5 +162,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -191,5 +186,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -223,5 +217,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -251,5 +244,4 @@ | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}); | ||
@@ -275,3 +267,3 @@ | ||
const adapter = new Mysql(); | ||
const expected = [{ "name": "foo", "data_type": "integer" }]; | ||
const expected = [{ "name": "foo", "is_nullable": "YES", "data_type": "integer" }]; | ||
adapter.query = async () => (expected); | ||
@@ -372,3 +364,3 @@ const result = await adapter.getColumns(); | ||
"foo": { | ||
"__pk": "foo", | ||
"__pk": "bar", | ||
"__reverse": [ | ||
@@ -402,3 +394,2 @@ { | ||
}); | ||
const adapter = new Mysql(db); | ||
@@ -409,6 +400,5 @@ const result = await adapter.getSchema(schemaname); | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}, 15000); | ||
@@ -447,6 +437,5 @@ test('it should return the complete database schema as json without excluded items', async (done) => { | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}, 15000); | ||
@@ -498,7 +487,6 @@ test('it should load items from table using records ids', async (done) => { | ||
// Close connection | ||
db.destroy(() => { | ||
done(); | ||
}); | ||
}); | ||
await db.destroy(); | ||
done(); | ||
}, 15000); | ||
}); |
@@ -8,3 +8,3 @@ { | ||
"user" : "root", | ||
"password" : "", | ||
"password" : "toor", | ||
"database" : "db2graphql_test" | ||
@@ -11,0 +11,0 @@ }, |
@@ -6,4 +6,13 @@ const db2g = require('../src/db2g'); | ||
afterAll(() => db.destroy()); | ||
afterAll( async (done) => { | ||
await db.destroy(); | ||
done(); | ||
}); | ||
beforeEach(async (done) => { | ||
await db.schema.dropTableIfExists('foo'); | ||
await db.schema.dropTableIfExists('bar'); | ||
done(); | ||
}); | ||
describe('Db2graphql', () => { | ||
@@ -250,3 +259,3 @@ | ||
const api = new db2g(); | ||
api.add("Query", 'getFoo', 'Foo', (root, args, context) => {}); | ||
api.add('Query', 'getFoo', 'Foo', (root, args, context) => {}); | ||
const result = api.getSchema(); | ||
@@ -265,2 +274,17 @@ expect(result).toEqual("type Query {\n getFoo: Foo\n}"); | ||
test('it should throw exception on adding invalid field path', () => { | ||
const api = new db2g(); | ||
expect(() => { | ||
api.addField("Query.", 'Foo', (root, args, context) => {}); | ||
}).toThrow(new Error('addField path must be in format Type.field')); | ||
}); | ||
test('it should add a graphql field by path', async (done) => { | ||
const api = new db2g(); | ||
api.addField("Query.getFoo", 'Foo', (root, args, context) => {}); | ||
const result = api.getSchema(); | ||
expect(result).toEqual("type Query {\n getFoo: Foo\n}"); | ||
done(); | ||
}); | ||
test('it should add a graphql mutation with params', async (done) => { | ||
@@ -267,0 +291,0 @@ const api = new db2g(); |
@@ -7,3 +7,3 @@ const Compiler = require('../../src/graphql/compiler'); | ||
afterAll(() => db.destroy()); | ||
afterAll(async () => await db.destroy()); | ||
@@ -10,0 +10,0 @@ const dbSchema = { |
1336892
4708