collimator
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -5,2 +5,6 @@ # Change Log | ||
## 3.0.0 - 2015-09-13 | ||
* Migrate to ES6 | ||
* Add Node 4 to build matrix | ||
## 2.0.0 - 2015-09-10 | ||
@@ -7,0 +11,0 @@ * Fully inspect database by default when invoking `collimator` directly |
{ | ||
"name": "collimator", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Reflection & Introspection for PostgreSQL Databases", | ||
"main": "src/collimator.js", | ||
"main": "lib/collimator.js", | ||
"scripts": { | ||
@@ -18,11 +18,47 @@ "test": "gulp" | ||
"devDependencies": { | ||
"babel": "^5.8.23", | ||
"babel-core": "^5.8.24", | ||
"babel-plugin-rewire": "^0.1.22", | ||
"gulp": "~3.9.0", | ||
"gulp-jasmine": "~2.0.1", | ||
"gulp-babel": "^5.2.1", | ||
"gulp-concat": "^2.6.0", | ||
"gulp-env": "^0.2.0", | ||
"gulp-if": "^1.2.5", | ||
"gulp-istanbul": "~0.10.0", | ||
"rewire": "~2.3.4", | ||
"gulp-jasmine": "^2.0.1", | ||
"gulp-jscs": "~2.0.0", | ||
"gulp-jshint": "~1.11.2", | ||
"gulp-markdox": "warrenseymour/gulp-markdox#1ce4f61", | ||
"gulp-concat": "~2.6.0" | ||
"gulp-sourcemaps": "^1.5.2", | ||
"gulp-uglify": "^1.4.1", | ||
"isparta": "^3.0.4", | ||
"require-dir": "^0.3.0", | ||
"run-sequence": "^1.1.2", | ||
"yargs": "^3.24.0" | ||
}, | ||
"build": { | ||
"tasks": "gulp-tasks", | ||
"paths": { | ||
"src": "src/**/*.js", | ||
"sql": "src/**/*.sql", | ||
"spec": "spec/**/*.js", | ||
"coverage": "coverage", | ||
"dist": "lib" | ||
}, | ||
"watch": { | ||
"paths": ["src/**/*.js", "spec/**/*.js"], | ||
"tasks": ["test", "style", "docs"] | ||
} | ||
}, | ||
"babel": { | ||
"env": { | ||
"test": { | ||
"plugins": [ | ||
"rewire" | ||
] | ||
} | ||
} | ||
} | ||
} |
@@ -57,3 +57,3 @@ [![Build Status](https://travis-ci.org/radify/collimator.svg)](https://travis-ci.org/radify/collimator) | ||
Run the tests with: | ||
Run the tests and build with: | ||
@@ -64,3 +64,3 @@ ```bash | ||
Run unit test coverage with: | ||
Generate unit test coverage with: | ||
@@ -80,5 +80,5 @@ ```bash | ||
```bash | ||
gulp dev | ||
gulp watch | ||
``` | ||
This will watch the `src` and `spec` directories and run `gulp` automatically when a change is detected. Note that it will not run the tests until it detects a change, so you may prefer to run it with `gulp & gulp dev`. |
@@ -1,25 +0,27 @@ | ||
describe('collimator', function() { | ||
var collimator = require('../src/collimator'); | ||
var bluebird = require('bluebird'); | ||
import collimator from '../src/collimator'; | ||
import bluebird from 'bluebird'; | ||
it('exports inspectors', function() { | ||
expect(collimator.tables).toBe(require('../src/inspectors/tables')); | ||
expect(collimator.schema).toBe(require('../src/inspectors/schema')); | ||
expect(collimator.relationships).toBe(require('../src/inspectors/relationships')); | ||
}); | ||
describe('collimator', () => { | ||
it('describes the entire database when invoked directly', (done) => { | ||
var deferred = { | ||
tables: bluebird.defer(), | ||
schema: bluebird.defer(), | ||
relationships: bluebird.defer() | ||
}; | ||
it('describes the entire database when invoked directly', function(done) { | ||
var tables = bluebird.defer(); | ||
var schema = bluebird.defer(); | ||
var relationships = bluebird.defer(); | ||
var spy = { | ||
tables: jasmine.createSpy('tables').and.returnValue(deferred.tables.promise), | ||
schema: jasmine.createSpy('schema').and.returnValue(deferred.schema.promise), | ||
relationships: jasmine.createSpy('relationships').and.returnValue(deferred.relationships.promise) | ||
}; | ||
spyOn(collimator, 'tables').and.returnValue(tables.promise); | ||
spyOn(collimator, 'schema').and.returnValue(schema.promise); | ||
spyOn(collimator, 'relationships').and.returnValue(relationships.promise); | ||
collimator.__Rewire__('tables', spy.tables); | ||
collimator.__Rewire__('schema', spy.schema); | ||
collimator.__Rewire__('relationships', spy.relationships); | ||
collimator('mockDb') | ||
.then(function(result) { | ||
expect(collimator.tables).toHaveBeenCalledWith('mockDb'); | ||
expect(collimator.schema).toHaveBeenCalledWith('mockDb', 'mockTable'); | ||
expect(collimator.relationships).toHaveBeenCalledWith('mockDb', 'mockTable'); | ||
.then((result) => { | ||
expect(spy.tables).toHaveBeenCalledWith('mockDb'); | ||
expect(spy.schema).toHaveBeenCalledWith('mockDb', 'mockTable'); | ||
expect(spy.relationships).toHaveBeenCalledWith('mockDb', 'mockTable'); | ||
@@ -32,6 +34,6 @@ expect(result).toEqual([ | ||
tables.resolve([{name: 'mockTable'}]); | ||
schema.resolve('mockSchema'); | ||
relationships.resolve('mockRelationships'); | ||
deferred.tables.resolve([{name: 'mockTable'}]); | ||
deferred.schema.resolve('mockSchema'); | ||
deferred.relationships.resolve('mockRelationships'); | ||
}); | ||
}); |
@@ -1,11 +0,11 @@ | ||
describe('relationships', function() { | ||
import relationships from '../../src/inspectors/relationships'; | ||
import MockFileQuery from '../MockFileQuery'; | ||
var rewire = require('rewire'); | ||
var relationships = rewire('../../src/inspectors/relationships'); | ||
var mockQuery = require('../mockFileQuery'); | ||
relationships.__set__('query', mockQuery); | ||
describe('relationships', () => { | ||
it('queries db with `name` and returns a promise', (done) => { | ||
var query = new MockFileQuery(); | ||
relationships.__Rewire__('query', query); | ||
it('queries db with `name` and returns a promise', function(done) { | ||
mockQuery.and.callFake(function(db, query) { | ||
var queries = { | ||
query.and.callFake((db, query) => { | ||
const queries = { | ||
'./relationships/belongsTo.sql': 'mockBelongsToResult', | ||
@@ -19,7 +19,7 @@ './relationships/has.sql': 'mockHasResult', | ||
relationships('mockDatabase', 'tableName') | ||
.then(function(result) { | ||
expect(mockQuery.calls.count()).toBe(2); | ||
expect(mockQuery) | ||
.then((result) => { | ||
expect(query.calls.count()).toBe(2); | ||
expect(query) | ||
.toHaveBeenCalledWith('mockDatabase', './relationships/belongsTo.sql', {name: 'tableName'}); | ||
expect(mockQuery) | ||
expect(query) | ||
.toHaveBeenCalledWith('mockDatabase', './relationships/has.sql', {name: 'tableName'}); | ||
@@ -26,0 +26,0 @@ |
@@ -1,9 +0,8 @@ | ||
describe('schema', function() { | ||
import schema from '../../src/inspectors/schema'; | ||
import MockFileQuery from '../MockFileQuery'; | ||
var rewire = require('rewire'); | ||
var schema = rewire('../../src/inspectors/schema'); | ||
var mockQuery = require('../mockFileQuery'); | ||
schema.__set__('query', mockQuery); | ||
import { table, property, properties, required } from '../../src/inspectors/schema'; | ||
var columns = [ | ||
describe('schema', () => { | ||
const columns = [ | ||
{name: 'forename', nullable: false, default: null, type: 'character varying'}, | ||
@@ -15,10 +14,13 @@ {name: 'surname', nullable: false, default: null, type: 'character varying'}, | ||
describe('schema()', function() { | ||
it('queries db with `name`, passes result to .table(), and returns a promise', function(done) { | ||
spyOn(schema, 'table').and.returnValue('mockSchema'); | ||
describe('schema()', () => { | ||
it('queries db with `name`, passes result to .table(), and returns a promise', (done) => { | ||
var query = new MockFileQuery(); | ||
var table = jasmine.createSpy('table').and.returnValue('mockSchema'); | ||
schema.__Rewire__('query', query); | ||
schema.__Rewire__('table', table); | ||
schema('mockDatabase', 'tableName') | ||
.then(function(result) { | ||
expect(mockQuery).toHaveBeenCalledWith('mockDatabase', './schema.sql', {name: 'tableName'}); | ||
expect(schema.table).toHaveBeenCalledWith('tableName', columns); | ||
.then((result) => { | ||
expect(query).toHaveBeenCalledWith('mockDatabase', './schema.sql', {name: 'tableName'}); | ||
expect(table).toHaveBeenCalledWith('tableName', columns); | ||
expect(result).toBe('mockSchema'); | ||
@@ -28,16 +30,19 @@ }) | ||
mockQuery.deferred.resolve(columns); | ||
query.deferred.resolve(columns); | ||
}); | ||
}); | ||
describe('.table()', function() { | ||
it('returns a JSON Schema object', function() { | ||
spyOn(schema, 'properties').and.returnValue('mockPropertyObject'); | ||
spyOn(schema, 'required').and.returnValue('mockRequiredArray'); | ||
describe('.table()', () => { | ||
it('returns a JSON Schema object', () => { | ||
var properties = jasmine.createSpy('properties').and.returnValue('mockPropertyObject'); | ||
var required = jasmine.createSpy('required').and.returnValue('mockRequiredArray'); | ||
var result = schema.table('myTable', columns); | ||
schema.__Rewire__('properties', properties); | ||
schema.__Rewire__('required', required); | ||
expect(schema.properties).toHaveBeenCalledWith(columns); | ||
expect(schema.required).toHaveBeenCalledWith(columns); | ||
var result = table('myTable', columns); | ||
expect(properties).toHaveBeenCalledWith(columns); | ||
expect(required).toHaveBeenCalledWith(columns); | ||
expect(result).toEqual({ | ||
@@ -53,14 +58,14 @@ $schema: 'http://json-schema.org/draft-04/schema#', | ||
describe('.properties()', function() { | ||
it('returns an object representing schema for all columns', function() { | ||
var R = require('ramda'); | ||
describe('.properties()', () => { | ||
it('returns an object representing schema for all columns', () => { | ||
var property = jasmine.createSpy('property').and.callFake(column => ({ | ||
[column.name]: 'mockSchemaObject' | ||
})); | ||
spyOn(schema, 'property').and.callFake(function(column) { | ||
return R.createMapEntry(column.name, 'mockSchemaObject'); | ||
}); | ||
schema.__Rewire__('property', property); | ||
var result = schema.properties(columns); | ||
var result = properties(columns); | ||
columns.forEach(function(column) { | ||
expect(schema.property).toHaveBeenCalledWith(column); | ||
columns.forEach((column) => { | ||
expect(property).toHaveBeenCalledWith(column); | ||
expect(result[column.name]).toBe('mockSchemaObject'); | ||
@@ -71,5 +76,5 @@ }); | ||
describe('.property()', function() { | ||
it('returns type, keyed by column name', function() { | ||
var result = schema.property(columns[0]); | ||
describe('.property()', () => { | ||
it('returns type, keyed by column name', () => { | ||
var result = property(columns[0]); | ||
@@ -84,8 +89,8 @@ expect(result).toEqual({ | ||
describe('.required()', function() { | ||
it('returns array of columns that are not nullable and do not have a default', function() { | ||
var required = schema.required(columns); | ||
expect(required).toEqual(['forename', 'surname']); | ||
describe('.required()', () => { | ||
it('returns array of columns that are not nullable and do not have a default', () => { | ||
var result = required(columns); | ||
expect(result).toEqual(['forename', 'surname']); | ||
}); | ||
}); | ||
}); |
@@ -1,12 +0,12 @@ | ||
describe('tables', function() { | ||
import tables from '../../src/inspectors/tables'; | ||
import MockFileQuery from '../MockFileQuery'; | ||
var rewire = require('rewire'); | ||
var tables = rewire('../../src/inspectors/tables'); | ||
var mockQuery = require('../mockFileQuery'); | ||
tables.__set__('query', mockQuery); | ||
describe('tables', () => { | ||
it('queries db with a query read from a file', (done) => { | ||
var query = new MockFileQuery(); | ||
tables.__Rewire__('query', query); | ||
it('queries db with a query read from a file', function(done) { | ||
tables('mockDatabase') | ||
.then(function(result) { | ||
expect(mockQuery).toHaveBeenCalledWith('mockDatabase', './tables.sql'); | ||
.then((result) => { | ||
expect(query).toHaveBeenCalledWith('mockDatabase', './tables.sql'); | ||
expect(result).toBe('mockTables'); | ||
@@ -16,4 +16,4 @@ }) | ||
mockQuery.deferred.resolve('mockTables'); | ||
query.deferred.resolve('mockTables'); | ||
}); | ||
}); |
@@ -1,4 +0,6 @@ | ||
describe('fileQuery', function() { | ||
import fileQuery from '../../src/util/fileQuery'; | ||
describe('fileQuery', () => { | ||
var db; | ||
var fileQuery = require('../../src/util/fileQuery'); | ||
global.queryResult = { | ||
@@ -8,9 +10,9 @@ manyOrNone: 'mockManyOrNoneQueryResult' | ||
beforeEach(function() { | ||
beforeEach(() => { | ||
db = jasmine.createSpyObj('db', ['query']); | ||
}); | ||
it('runs a query from a file', function(done) { | ||
it('runs a query from a file', (done) => { | ||
fileQuery(db, './testQuery.sql') | ||
.then(function(result) { | ||
.then((result) => { | ||
expect(db.query).toHaveBeenCalledWith('SELECT * FROM test_query;\n', undefined, queryResult.manyOrNone); | ||
@@ -21,5 +23,5 @@ }) | ||
it('specifies params', function(done) { | ||
it('specifies params', (done) => { | ||
fileQuery(db, './testQuery.sql', 'mockParams') | ||
.then(function(result) { | ||
.then((result) => { | ||
expect(db.query).toHaveBeenCalledWith('SELECT * FROM test_query;\n', 'mockParams', queryResult.manyOrNone); | ||
@@ -30,5 +32,5 @@ }) | ||
it('specifies a query result mask', function(done) { | ||
it('specifies a query result mask', (done) => { | ||
fileQuery(db, './testQuery.sql', undefined, 'mockQueryResultMask') | ||
.then(function(result) { | ||
.then((result) => { | ||
expect(db.query).toHaveBeenCalledWith('SELECT * FROM test_query;\n', undefined, 'mockQueryResultMask'); | ||
@@ -35,0 +37,0 @@ }) |
@@ -1,5 +0,7 @@ | ||
'use strict'; | ||
import bluebird from 'bluebird'; | ||
import {merge} from 'ramda'; | ||
var bluebird = require('bluebird'); | ||
var R = require('ramda'); | ||
import tables from './inspectors/tables'; | ||
import schema from './inspectors/schema'; | ||
import relationships from './inspectors/relationships'; | ||
@@ -26,15 +28,11 @@ /** | ||
function collimator(db) { | ||
return collimator.tables(db) | ||
.map(function(table) { | ||
return bluebird.props(R.merge(table, { | ||
schema: collimator.schema(db, table.name), | ||
relationships: collimator.relationships(db, table.name) | ||
})); | ||
}); | ||
const inspect = table => bluebird.props(merge(table, { | ||
schema: schema(db, table.name), | ||
relationships: relationships(db, table.name) | ||
})); | ||
return tables(db).map(inspect); | ||
} | ||
collimator.tables = require('./inspectors/tables'); | ||
collimator.schema = require('./inspectors/schema'); | ||
collimator.relationships = require('./inspectors/relationships'); | ||
module.exports = collimator; | ||
export {tables, schema, relationships}; | ||
export default collimator; |
@@ -1,6 +0,4 @@ | ||
'use strict'; | ||
import bluebird from 'bluebird'; | ||
import query from '../util/fileQuery'; | ||
var bluebird = require('bluebird'); | ||
var query = require('../util/fileQuery'); | ||
/** | ||
@@ -29,4 +27,4 @@ * Inspects the foreign key constraints definted in the table specified by | ||
var queries = { | ||
belongsTo: query(db, './relationships/belongsTo.sql', {name: name}), | ||
has: query(db, './relationships/has.sql', {name: name}) | ||
belongsTo: query(db, './relationships/belongsTo.sql', {name}), | ||
has: query(db, './relationships/has.sql', {name}) | ||
}; | ||
@@ -37,2 +35,2 @@ | ||
module.exports = relationships; | ||
export default relationships; |
@@ -1,6 +0,4 @@ | ||
'use strict'; | ||
import query from '../util/fileQuery'; | ||
import {map, mergeAll, filter, pluck} from 'ramda'; | ||
var R = require('ramda'); | ||
var query = require('../util/fileQuery'); | ||
/** | ||
@@ -19,3 +17,3 @@ * Inspects the column definitions for a table specified by `name`, and returns | ||
return query(db, './schema.sql', {name: name}) | ||
.then(R.partial(schema.table, name)); | ||
.then(columns => table(name, columns)); | ||
} | ||
@@ -36,4 +34,4 @@ | ||
type: 'object', | ||
properties: schema.properties(columns), | ||
required: schema.required(columns) | ||
properties: properties(columns), | ||
required: required(columns) | ||
}; | ||
@@ -51,4 +49,4 @@ } | ||
function properties(columns) { | ||
var columnProperties = R.map(schema.property, columns); | ||
return R.mergeAll(columnProperties); | ||
var columnProperties = map(property, columns); | ||
return mergeAll(columnProperties); | ||
} | ||
@@ -64,3 +62,3 @@ | ||
function property(column) { | ||
var TYPES = { | ||
const TYPES = { | ||
bigserial: 'integer', | ||
@@ -86,7 +84,7 @@ boolean: 'boolean', | ||
var schema = { | ||
type: TYPES[column.type] | ||
return { | ||
[column.name]: { | ||
type: TYPES[column.type] | ||
} | ||
}; | ||
return R.createMapEntry(column.name, schema); | ||
} | ||
@@ -103,15 +101,10 @@ | ||
function required(columns) { | ||
var isRequired = function(column) { | ||
return column.nullable === false && column.default === null; | ||
}; | ||
const isRequired = column => | ||
column.nullable === false && column.default === null; | ||
var requiredColumns = R.filter(isRequired, columns); | ||
return R.pluck('name', requiredColumns); | ||
var requiredColumns = filter(isRequired, columns); | ||
return pluck('name', requiredColumns); | ||
} | ||
schema.table = table; | ||
schema.property = property; | ||
schema.required = required; | ||
schema.properties = properties; | ||
module.exports = schema; | ||
export default schema; | ||
export {table, property, properties, required}; |
@@ -1,5 +0,3 @@ | ||
'use strict'; | ||
import query from '../util/fileQuery'; | ||
var query = require('../util/fileQuery'); | ||
/** | ||
@@ -24,2 +22,2 @@ * Inspects the tables defined in a database, and returns a promise that will | ||
module.exports = tables; | ||
export default tables; |
'use strict'; | ||
var R = require('ramda'); | ||
var path = require('path'); | ||
var bluebird = require('bluebird'); | ||
var readFile = bluebird.promisify(require('fs').readFile); | ||
var callsite = require('callsite'); | ||
import {resolve} from 'path'; | ||
import {promisify} from 'bluebird'; | ||
import {readFile} from 'fs'; | ||
import callsite from 'callsite'; | ||
const readFileP = promisify(readFile); | ||
function fileQuery(db, file, params, resultMask) { | ||
var path = resolvePath(file); | ||
resultMask = resultMask || global.queryResult.manyOrNone; | ||
var query = R.partialRight(db.query, params, resultMask); | ||
return readFile(path, 'utf-8') | ||
.then(query); | ||
return readFileP(path, 'utf-8') | ||
.then(query => db.query(query, params, resultMask)); | ||
} | ||
@@ -22,5 +21,5 @@ | ||
var caller = stack[2]; | ||
return path.resolve(caller.getFileName(), '..', file); | ||
return resolve(caller.getFileName(), '..', file); | ||
} | ||
module.exports = fileQuery; | ||
export default fileQuery; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
54244
50
762
19
4
1