Skip to content

Commit

Permalink
style: change object-curly-spacing eslint rule to always
Browse files Browse the repository at this point in the history
  • Loading branch information
langpavel committed Jan 19, 2019
1 parent 13de916 commit 8aac2e6
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 170 deletions.
22 changes: 9 additions & 13 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@
// disallow use of void operator
"no-void": 0,
// disallow usage of configurable warning terms in comments: e.g. todo
"no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }],
"no-warning-comments": [
0,
{ "terms": ["todo", "fixme", "xxx"], "location": "start" }
],
// disallow use of the with statement
"no-with": 2,
// require use of the second argument for parseInt()
Expand Down Expand Up @@ -177,13 +180,13 @@
"import/export": 2,
"import/named": 2,
"import/namespace": 2,
"import/no-unresolved": [2, {"commonjs": true, "amd": true}],
"new-cap": [2, {"capIsNew": false, "newIsCap": true}],
"import/no-unresolved": [2, { "commonjs": true, "amd": true }],
"new-cap": [2, { "capIsNew": false, "newIsCap": true }],
"no-console": 1,
"no-shadow": 1,
"no-unused-vars": 1,
"no-use-before-define": [2, "nofunc"],
"object-curly-spacing": [2, "never"],
"object-curly-spacing": [2, "always"],
"padded-blocks": 0,
"spaced-comment": 1,
"strict": [2, "never"],
Expand All @@ -193,17 +196,10 @@
"require": false
},
"settings": {
"import/ignore": [
"node_modules",
"lib",
"\\.json$"
],
"import/ignore": ["node_modules", "lib", "\\.json$"],
"import/parser": "babel-eslint",
"import/resolve": {
"extensions": [
".js",
".json"
]
"extensions": [".js", ".json"]
}
}
}
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import debug from './debug';
import makeAsyncApi from './makeAsyncApi';
import SqlTag from './sql';

export {sqlStr, literal, identifier} from './sql';
export { sqlStr, literal, identifier } from './sql';
export const SQL = SqlTag;

function checkAsyncFunction(asyncFunc) {
Expand Down Expand Up @@ -60,10 +60,12 @@ export default class PgAsync {
setDriver(driver) {
if (typeof driver === 'string')
switch (driver) {
case '': case 'pg':
case '':
case 'pg':
driver = pgDriver;
break;
case 'native': case 'pg.native':
case 'native':
case 'pg.native':
driver = pgDriver.native;
break;
default:
Expand Down Expand Up @@ -98,7 +100,7 @@ export default class PgAsync {
done: () => {
debug('Client released');
done();
}
},
});
});
});
Expand All @@ -107,7 +109,7 @@ export default class PgAsync {
async connect(asyncFunc) {
checkAsyncFunction(asyncFunc);

const {client, done} = await this.getClient();
const { client, done } = await this.getClient();
try {
const api = makeAsyncApi(client);
const result = await asyncFunc(api);
Expand All @@ -123,7 +125,7 @@ export default class PgAsync {
async transaction(asyncFunc) {
checkAsyncFunction(asyncFunc);

return await this.connect(async (client) => {
return await this.connect(async client => {
client.checkSerialAccess = true;
await client.startTransaction();
try {
Expand Down
43 changes: 29 additions & 14 deletions src/makeAsyncApi.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Promise from 'bluebird';
import debug from './debug';
import SQL, {SqlFragment} from './sql';
import SQL, { SqlFragment } from './sql';

const makeAsyncApi = client => {

let inQuery = false;

function query(sql, ...values) {
Expand All @@ -18,7 +17,11 @@ const makeAsyncApi = client => {

inQuery = true;
return new Promise((resolve, reject) => {
debug('query params: %s query: %j', JSON.stringify(values).slice(0,60), sql);
debug(
'query params: %s query: %j',
JSON.stringify(values).slice(0, 60),
sql,
);
client.query(sql, values, (err, result) => {
inQuery = false;
if (err) {
Expand All @@ -38,18 +41,22 @@ const makeAsyncApi = client => {
if (inQuery)
throw new Error(
'Commands on same client should be called serially. ' +
'Do you forget `await`?');
'Do you forget `await`?',
);
return _queryArgs(sql, values);
};

query.rows = (sql, ...values) => query.rowsArgs(sql, values);
query.rowsArgs = (sql, values) => query.queryArgs(sql, values).then(r => r.rows);
query.rowsArgs = (sql, values) =>
query.queryArgs(sql, values).then(r => r.rows);

query.row = (sql, ...values) => query.rowArgs(sql, values);
query.rowArgs = async (sql, values) => {
const result = await query.queryArgs(sql, values);
if (result.rowCount !== 1)
throw new Error(`SQL: Expected exactly one row result but ${result.rowCount} returned`);
throw new Error(
`SQL: Expected exactly one row result but ${result.rowCount} returned`,
);
return result.rows[0];
};

Expand All @@ -63,14 +70,17 @@ const makeAsyncApi = client => {
};
values = sql.values;
} else {
opts = typeof(sql) === 'string'
? {text: sql, rowMode: 'array'}
: {...sql, rowMode: 'array'};
opts =
typeof sql === 'string'
? { text: sql, rowMode: 'array' }
: { ...sql, rowMode: 'array' };
}

const result = await query.rowArgs(opts, values);
if (result.length !== 1)
throw new Error(`SQL: Expected exactly one column but ${result.length} returned`);
throw new Error(
`SQL: Expected exactly one column but ${result.length} returned`,
);
return result[0];
};

Expand All @@ -93,17 +103,22 @@ const makeAsyncApi = client => {

query._end = () => {
const queryArgs = _queryArgs;
_queryArgs = () => { throw new Error('Client was released.'); };
return new Promise((resolve) => {
_queryArgs = () => {
throw new Error('Client was released.');
};
return new Promise(resolve => {
if (inQuery)
throw new Error(
'Client shutting down but query is pending. ' +
'Do you forget `await`?');
'Do you forget `await`?',
);

if (query.inTransaction) {
query.inTransaction = false;
queryArgs('ROLLBACK', []);
throw new Error('Transaction started manually but not closed. Automatic rollback');
throw new Error(
'Transaction started manually but not closed. Automatic rollback',
);
}
resolve();
});
Expand Down
42 changes: 17 additions & 25 deletions src/sql.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {inspect} from 'util';
import { inspect } from 'util';
import pg from 'pg';
import {prepareValue} from 'pg/lib/utils';
import { prepareValue } from 'pg/lib/utils';

let SQL; // eslint-disable-line prefer-const

export class SqlFragment {

// SQL`insert into $quote${tableName} $values${keyValue}`;
constructor(templateParts, templateValues) {
this._parts = [];
Expand All @@ -18,7 +17,7 @@ export class SqlFragment {

let i = 0;

const addText = (str) => {
const addText = str => {
currentFragment.push(str);
};

Expand All @@ -29,7 +28,7 @@ export class SqlFragment {
text.push(fragment);
};

const addValue = (value) => {
const addValue = value => {
flushText();
this.values.push(value);
text.push('$', argIndex++);
Expand All @@ -41,15 +40,14 @@ export class SqlFragment {
if (typeof value === 'undefined')
throw new Error(
`Expected something, but got undefined. ` +
`Value after SQL fragment: ${text.join('')}${templateParts[i]}`);
`Value after SQL fragment: ${text.join('')}${templateParts[i]}`,
);

while (parts.length > 1)
value = SQL.transform(parts.pop(), value);
while (parts.length > 1) value = SQL.transform(parts.pop(), value);

addText(parts[0]);

if (value && value.toSQL)
value = value.toSQL();
if (value && value.toSQL) value = value.toSQL();

if (value instanceof SqlFragment) {
const nestedValuesLength = value.values.length;
Expand All @@ -74,8 +72,7 @@ export class SqlFragment {

// this is for log/debugging only
toString() {
if (this._asString)
return this._asString;
if (this._asString) return this._asString;

const text = [];
const length = this.values.length;
Expand All @@ -92,11 +89,11 @@ export class SqlFragment {
}
}

SQL = (parts, ...values) => { // eslint-disable-line prefer-const
SQL = (parts, ...values) => {
// eslint-disable-line prefer-const
// only one argument, called manually
if (!Array.isArray(parts) && values.length === 0) {
if (parts instanceof SqlFragment)
return parts;
if (parts instanceof SqlFragment) return parts;

parts = [parts];
}
Expand Down Expand Up @@ -125,8 +122,7 @@ SQL.registerTransform = (...names) => {
SQL.transform = (name, value) => {
name = name.trim().toLowerCase();
const transform = SQL._transforms[name];
if (!transform)
throw new Error(`Unknown transform: "${name}"`);
if (!transform) throw new Error(`Unknown transform: "${name}"`);
return transform(value);
};

Expand All @@ -142,28 +138,24 @@ export function sqlStr(str) {

// returns quoted identifier
export function identifier(name) {
if (!name)
throw new Error(`Expected nonempty string, got ${inspect(name)}`);
if (!name) throw new Error(`Expected nonempty string, got ${inspect(name)}`);

return SQL(escapeIdentifier(name));
}

// returns quoted literal
export function literal(value) {
if (value instanceof SqlFragment)
return value;
if (value instanceof SqlFragment) return value;

if (typeof value === 'undefined')
throw new Error(`Expected something, but got undefined.`);

if (value === null)
return SQL.NULL;
if (value === null) return SQL.NULL;

if (value.toPostgres)
return SQL(escapeLiteral(value.toPostgres(prepareValue)));

if (value.toSQL)
return SQL(value.toSQL());
if (value.toSQL) return SQL(value.toSQL());

return SQL(escapeLiteral(value.toString()));
}
Expand Down
10 changes: 7 additions & 3 deletions test/driver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from 'chai';
import { expect } from 'chai';
import Pg from '../src/index';

describe('pg-async driver', () => {
Expand All @@ -12,8 +12,12 @@ describe('pg-async driver', () => {
});

it('should be `pg.native` module by if "native" string used', () => {
expect(new Pg(null, 'native').getDriver()).to.be.equal(require('pg').native);
expect(new Pg(null, 'pg.native').getDriver()).to.be.equal(require('pg').native);
expect(new Pg(null, 'native').getDriver()).to.be.equal(
require('pg').native,
);
expect(new Pg(null, 'pg.native').getDriver()).to.be.equal(
require('pg').native,
);
});

it('should throw if unknown driver used', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/exports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from 'chai';
import Pg, {sqlStr, literal, identifier} from '../src/index';
import { expect } from 'chai';
import Pg, { sqlStr, literal, identifier } from '../src/index';

const pg = new Pg;
const pg = new Pg();

describe('pg-async exports', () => {
it('pgAsync', () => expect(Pg).to.be.a('function'));
Expand Down
Loading

0 comments on commit 8aac2e6

Please sign in to comment.