db2graphql
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -0,0 +0,0 @@ { |
@@ -10,2 +10,3 @@ const knex = require('knex'); | ||
await api.connect(); // Connects to database and extracts database schema | ||
api.withBuilder(); | ||
const schema = api.getSchema(); | ||
@@ -12,0 +13,0 @@ const resolvers = api.getResolvers(); |
@@ -0,0 +0,0 @@ # Create user |
var db2g = require('./src/db2g'); | ||
module.exports = db2g; |
{ | ||
"name": "db2graphql", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Generate Graphql schema based on existing relational database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,0 +0,0 @@ # db2graphql |
/** | ||
* Available types | ||
*/ | ||
const availableTypes = ['string', 'integer', 'bigInteger', 'text', 'float', 'decimal', 'boolean', 'date', 'datetime', 'time', 'timestamp', 'binary', 'json', 'jsonb', 'uuid']; | ||
/** | ||
* PostgreSQL dialect adapter | ||
@@ -17,2 +22,9 @@ */ | ||
/** | ||
* Get available types from the RDMS | ||
*/ | ||
static getAvailableTypes() { | ||
return availableTypes; | ||
} | ||
/** | ||
* Get Graphql type from database column data type | ||
@@ -19,0 +31,0 @@ * |
@@ -126,2 +126,84 @@ const PostgreSQL = require('../src/adapters/postgres'); | ||
} | ||
/** | ||
* Adds the schema builder queries | ||
*/ | ||
withBuilder() { | ||
// Add getSchema | ||
this.addQuery('getSchema: String'); | ||
this.addResolver('Query', 'getSchema', async (root, args, context) => { | ||
return JSON.stringify(this.dbSchema); | ||
}); | ||
// Add addSchemaColumn | ||
const queryAlterColumn = ` | ||
addSchemaColumn( | ||
tablename: String! | ||
columnname: String! | ||
type: String! | ||
foreign: String | ||
): Boolean | ||
`; | ||
this.addQuery(queryAlterColumn); | ||
this.addResolver('Query', 'addSchemaColumn', async (root, args, context) => { | ||
const { resolver, db } = context.ioc; | ||
const types = resolver.dbDriver.constructor.getAvailableTypes(); | ||
if (types.indexOf(args.type) === -1) return false; | ||
await db.schema.table(args.tablename, table => { | ||
table[args.type](args.columnname); | ||
if (args.foreign) { | ||
table.foreign(args.columnname).references(args.foreign) | ||
} | ||
}); | ||
return true; | ||
}); | ||
// Add dropSchemaColumn | ||
const queryDropColumn = ` | ||
dropSchemaColumn( | ||
tablename: String! | ||
columnname: String! | ||
): Boolean | ||
`; | ||
this.addQuery(queryDropColumn); | ||
this.addResolver('Query', 'dropSchemaColumn', async (root, args, context) => { | ||
const { db } = context.ioc; | ||
await db.schema.table(args.tablename, table => { | ||
table.dropColumn(args.columnname); | ||
}); | ||
return true; | ||
}); | ||
// Add addSchemaTable | ||
const queryAddTable = ` | ||
addSchemaTable( | ||
tablename: String! | ||
primary: String! | ||
type: String! | ||
increments: Boolean | ||
): Boolean | ||
`; | ||
this.addQuery(queryAddTable); | ||
this.addResolver('Query', 'addSchemaTable', async (root, args, context) => { | ||
const { resolver, db } = context.ioc; | ||
const types = resolver.dbDriver.constructor.getAvailableTypes(); | ||
if (types.indexOf(args.type) === -1) return false; | ||
await db.schema.createTable(args.tablename, (table) => { | ||
if (args.increments) table.increments(args.primary); | ||
else table[args.type](args.primary).primary(); | ||
}) | ||
return true; | ||
}); | ||
// Add dropSchemaTable | ||
const dropSchemaTable = 'dropSchemaTable(tablename: String!): Boolean'; | ||
this.addQuery(dropSchemaTable); | ||
this.addResolver('Query', 'dropSchemaTable', async (root, args, context) => { | ||
const { db } = context.ioc; | ||
await db.schema.dropTable(args.tablename); | ||
return true; | ||
}); | ||
} | ||
} | ||
@@ -128,0 +210,0 @@ |
@@ -0,0 +0,0 @@ const utils = require('../utils/utils'); |
@@ -0,0 +0,0 @@ const utils = require('../utils/utils'); |
@@ -0,0 +0,0 @@ |
@@ -324,1 +324,6 @@ const PostgreSQL = require('../../src/adapters/postgres'); | ||
}); | ||
test('it should return the available database column types', () => { | ||
const result = PostgreSQL.getAvailableTypes(); | ||
expect(result instanceof Array).toEqual(true); | ||
}); |
const db2g = require('../src/db2g'); | ||
const Postgres = require('../src/adapters/postgres'); | ||
@@ -46,2 +47,17 @@ const dbSchema = {"bar": {"__pk": "foo_id", "__reverse": [], "foo_id": {"__foreign": {"columnname": "id", "schemaname": "public", "tablename": "foo"}, "data_type": "bigint", "is_nullable": undefined, "name": "foo_id"}, "id": {"data_type":"bigint", "is_nullable": undefined, "name": "id"}}, "foo": {"__pk": null, "__reverse": [{"columnname": "id", "fcolumnname": "foo_id", "fschemaname": undefined, "ftablename": "bar"}], "id": {"data_type": "bigint", "is_nullable":undefined, "name": "id"}}}; | ||
const MockKnexFluidInterface = async (name, cb) => { | ||
const fns = { | ||
primary: () => fns, | ||
increments: () =>fns, | ||
dropColumn: () => fns, | ||
foreign: () => fns, | ||
references: () => fns | ||
}; | ||
const types = Postgres.getAvailableTypes(); | ||
types.map(t => { | ||
fns[t] = () => fns; | ||
}); | ||
cb(fns); | ||
}; | ||
const config = { | ||
@@ -51,2 +67,7 @@ client: 'pg' | ||
const MockKnex = { | ||
schema: { | ||
createTable: MockKnexFluidInterface, | ||
dropTable: async () => {}, | ||
table: MockKnexFluidInterface | ||
}, | ||
connection: () => { | ||
@@ -208,1 +229,46 @@ return { | ||
}); | ||
test('it should add the builder', async (done) => { | ||
const api = new db2g(); | ||
api.withBuilder(); | ||
const result = api.getResolvers(); | ||
expect(typeof result).toEqual('object'); | ||
expect(typeof result.Query).toEqual('object'); | ||
expect(typeof result.Query.getSchema).toEqual('function'); | ||
expect(typeof result.Query.addSchemaTable).toEqual('function'); | ||
expect(typeof result.Query.dropSchemaTable).toEqual('function'); | ||
expect(typeof result.Query.addSchemaColumn).toEqual('function'); | ||
expect(typeof result.Query.dropSchemaColumn).toEqual('function'); | ||
done(); | ||
}); | ||
test('it should run builder queries without errors', async (done) => { | ||
const api = new db2g(MockKnex); | ||
await api.connect(); | ||
api.withBuilder(); | ||
const resolvers = api.getResolvers(); | ||
await resolvers.Query.getSchema(null, {}, {}); | ||
let result = await resolvers.Query.addSchemaTable(null, {}, {}); | ||
expect(result).toEqual(false); | ||
result = await resolvers.Query.addSchemaTable(null, { type: "integer" }, {}); | ||
expect(result).toEqual(true); | ||
result = await resolvers.Query.addSchemaTable(null, { type: "integer", increments: true }, {}); | ||
expect(result).toEqual(true); | ||
result = await resolvers.Query.dropSchemaTable(null, {}, {}); | ||
expect(result).toEqual(true); | ||
result = await resolvers.Query.addSchemaColumn(null, {}, {}); | ||
expect(result).toEqual(false); | ||
result = await resolvers.Query.addSchemaColumn(null, { type: "integer" }, {}); | ||
expect(result).toEqual(true); | ||
result = await resolvers.Query.addSchemaColumn(null, { type: "integer", foreign: "foo.bar" }, {}); | ||
expect(result).toEqual(true); | ||
result = await resolvers.Query.dropSchemaColumn(null, {}, {}); | ||
expect(result).toEqual(true); | ||
done(); | ||
}); |
@@ -0,0 +0,0 @@ const Compiler = require('../../src/graphql/compiler'); |
@@ -0,0 +0,0 @@ const Resolver = require('../../src/graphql/resolver'); |
@@ -0,0 +0,0 @@ const mocks = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ |
@@ -0,0 +0,0 @@ const { toCamelCase } = require('../../src/utils/utils'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
146416
2311