Skip to content

Commit

Permalink
Merge remote-tracking branch 'waterlinejs/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinzolotarev committed Apr 4, 2016
2 parents d7bf707 + 89ef99f commit ef658bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
[![Dependency Status][daviddm-image]][daviddm-url]
[![Code Climate][codeclimate-image]][codeclimate-url]

A [Waterline](https://github.com/balderdashy/waterline) adapter for
[PostgreSQL](http://www.postgresql.org/), with [PostGIS](http://postgis.net/)
support.
A Waterline adapter for [PostgreSQL](http://www.postgresql.org/), with [PostGIS](http://postgis.net/) support.

## Features
- 100% re-write of the original
[sails-postgresql](https://github.com/balderdashy/sails-postgresql) adapter in [ES6](https://en.wikipedia.org/wiki/ECMAScript#Harmony.2C_6th_Edition)
- 100% re-write of the original sails-postgresql adapter in ES6. And *way* faster.
- Uses [knex.js](http://knexjs.org/) for query building and connection pooling
- Utilizes native array/json aggregation functions in PostgreSQL 9.2+ to
optimize query performance
- PostGIS 2.1+ Support

## Compatibility
- Waterline v0.11 and later
- PostgreSQL 9.2 and later
- PostgreSQL 9.4 and later

## Install

Expand Down Expand Up @@ -62,7 +57,7 @@ module.exports.connections = {
MIT

## Maintained By
##### [<img src='http://i.imgur.com/zM0ynQk.jpg' height='34px'>](http://balderdash.io)
[<img src='http://i.imgur.com/Y03Jgmf.png' height='64px'>](http://langa.io)

[waterline-version-image]: https://goo.gl/goisO1
[waterline-url]: http://sailsjs.org
Expand Down
2 changes: 1 addition & 1 deletion lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ const Adapter = {
let schema = cxn.collections[join.child]

_.each(result, row => {
row[alias] = Util.castResultRows(_.uniqBy(row[alias], pk), schema)
row[alias] = Util.castResultRows(_.compact(_.uniqBy(row[alias], pk)), schema)
})
})

Expand Down
2 changes: 1 addition & 1 deletion lib/spatial.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const SpatialUtil = {
},

isSpatialColumn (definition) {
if (!definition.dbType) return false
if (!definition || !definition.dbType) return false

let [ $, dbType, geoType, srid ] = SpatialUtil.spatialTypeRegex.exec(definition.dbType) || [ ]
return dbType === 'geometry'
Expand Down
39 changes: 25 additions & 14 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ const Util = {
castResultValues (values, schema) {
return _.mapValues(values, (value, attr) => {
let definition = schema.definition[attr]
if (!definition) return value

if (SpatialUtil.isSpatialColumn(definition)) {
try {
Expand Down Expand Up @@ -445,14 +446,24 @@ const Util = {

buildKnexJoins (cxn, { joins }, query) {
_.each(joins, join => {
let subquery = Util.buildKnexJoinSubquery(cxn, join)
let parentAlias = Util.getParentAlias(join)
let alias = Util.getSubqueryAlias(join)
query.leftJoin(
cxn.knex.raw(`(${subquery}) as "${alias}"`),
`${alias}.${join.childKey}`,
`${parentAlias}.${join.parentKey}`
)
let subquery = Util.buildKnexJoinSubquery(cxn, join)

if (join.select === false) {
query.innerJoin(
cxn.knex.raw(`"${join.child}" as "${alias}"`),
`${alias}.${join.childKey}`,
`${parentAlias}.${join.parentKey}`
)
}
else {
query.leftJoin(
cxn.knex.raw(`(${subquery}) as "${alias}"`),
`${alias}.${join.childKey}`,
`${parentAlias}.${join.parentKey}`
)
}
})
},

Expand Down Expand Up @@ -519,23 +530,23 @@ const Util = {
let start = (criteria.skip || 0) + 1
let end = (criteria.limit || (Util.PG_MAX_INT - start)) + start - 1

return `
array_to_json(
(array_remove(array_agg("${subqueryAlias}".* order by ${orderBy}), null))[${start}:${end}]
) as "${asColumn}"
`
if (!criteria.skip && !criteria.limit) {
return `json_agg("${subqueryAlias}".* order by ${orderBy}) as "${asColumn}"`
}

return `array_to_json((array_agg("${subqueryAlias}".* order by ${orderBy}))[${start}:${end}]) as "${asColumn}"`
})
},

/**
* Parse and validate a Postgres "select version()" result
*/
validateVersion ([ major, minor, patch ]) {
if (major < 9 || (major === 9 && minor < 2)) {
if (major < 9 || (major === 9 && minor < 4)) {
throw new Error(`
PostgreSQL ${major}.${minor}.${patch} detected. This adapter requires PostgreSQL 9.2 or higher.
PostgreSQL ${major}.${minor}.${patch} detected. This adapter requires PostgreSQL 9.4 or higher.
Please either:
1. Upgrade your Postgres server to at least 9.2.0 -or-
1. Upgrade your Postgres server to at least 9.4.0 -or-
2. Use the sails-postgresql adapter instead: https://www.npmjs.com/package/sails-postgresql
`)
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "waterline-postgresql",
"description": "PostgreSQL Adapter for Sails and Waterline",
"version": "0.14.0",
"version": "0.14.3",
"author": "Travis Webb <tjwebb@balderdash.io>",
"url": "http://github.com/waterlinejs/postgresql-adapter",
"keywords": [
Expand All @@ -27,8 +27,8 @@
"knex": "^0.10.0",
"knex-postgis": "^0.1.8",
"lodash": "^4.6",
"pg": "^4.4",
"pg-query-stream": "^0.7.0",
"pg": "^4.5",
"pg-query-stream": "^1.0.0",
"waterline-errors": "^0.10.1",
"waterline-sequel": "^0.5.0"
},
Expand Down

0 comments on commit ef658bb

Please sign in to comment.