Comparing version 0.1.11 to 0.1.12
@@ -101,2 +101,34 @@ var anyDB = require('any-db'); | ||
/** | ||
* Returns a map result from a query. | ||
* @param {!String} keyColumn the column to use as a key for the map. | ||
* @param {!String|Array|Function} mapper can be:<ul> | ||
* <li>the name of the column to use as a value;</li> | ||
* <li>an array of column names. The value will be an object with the property names from this array mapped to the | ||
* column values from the array;</li> | ||
* <li>a function that takes the row as an argument and returns a value.</li> | ||
* @param {!Function} callback called when the operation ends. Takes an error and the result. | ||
* @param {Function=} filter takes a row and returns a value indicating whether the row should be inserted in the | ||
* result. | ||
*/ | ||
extQuery.getMap = function(keyColumn, mapper, callback, filter) { | ||
filter = filter || function() { return true; }; | ||
if (typeof mapper === 'string') mapper = function(row) { return row[mapper]; }; | ||
else if (typeof mapper === 'object') mapper = function(row) { | ||
var obj = {}; | ||
for (var j = 0; j < mapper.length; j++) obj[mapper[j]] = row[mapper[j]]; | ||
result[row[keyColumn]] = obj; | ||
}; | ||
return this.exec(function(err, data) { | ||
if (err) return callback(err); | ||
var result = {}; | ||
for (var i = 0; i < data.length; i++) if (filter(data[i])) result[data[i][keyColumn]] = mapper(data[i]); | ||
callback(null, result); | ||
}); | ||
}; | ||
queryMethods.forEach(function (key) { | ||
@@ -103,0 +135,0 @@ extQuery[key] = function () { |
{ | ||
"name": "anydb-sql", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "anydb-sql combines node-anydb and node-sql", | ||
@@ -11,3 +11,3 @@ "main": "anydb-sql.js", | ||
"type": "git", | ||
"url": "git://github.com/spion/anydb-sql.git" | ||
"url": "git://github.com/doxout/anydb-sql.git" | ||
}, | ||
@@ -22,5 +22,4 @@ "author": "spion", | ||
"bugs": { | ||
"url": "https://github.com/spion/anydb-sql/issues" | ||
"url": "https://github.com/doxout/anydb-sql/issues" | ||
}, | ||
"gitHead": "c4b3c68facd77fb5f9c3987f5dee8767065794f7", | ||
"directories": { | ||
@@ -27,0 +26,0 @@ "test": "test" |
# anydb-sql | ||
anydb-sql combines [node-anydb](https://github.com/grncdr/node-any-db) | ||
and [node-sql](https://github.com/brianc/node-sql) into a single package. | ||
and [node-sql](https://github.com/brianc/node-sql) into a single package full | ||
of awesomeness. | ||
# examples and usage: | ||
Initializing an instance also creates a connection pool. The url argument | ||
is the same as in node-anydb | ||
Initializing an instance also creates a connection pool. The url argument is | ||
the same as in node-anydb | ||
@@ -29,22 +30,30 @@ ```js | ||
Queries have the addtional methods: | ||
## extra methods | ||
Queries have all the methods as in node-sql, plus the additional methods: | ||
* exec(function(err, rows)) - executes the query and calls the callback | ||
with an array of rows | ||
* all - same as exec | ||
* get(function(err, row)) - executes the query and returns the first result | ||
* all - same as exec | ||
* getMap(keyColumn, mapper, function(err, map), filter) - executes the query and maps the result to an object | ||
* execWithin(transaction, function(err, rows)) - execute within a transaction | ||
If you omit the callback from the additional methods, an eventemitter will be | ||
returned instead (like in anydb). | ||
Use regular node-sql queries then chain one of the additional | ||
methods at the end: | ||
Use regular node-sql queries then chain one of the additional methods at the | ||
end: | ||
```js | ||
user.where({email: user.email}).get(function(err, users) { | ||
// users[0].name, | ||
user.where({email: user.email}).get(function(err, user) { | ||
// user.name, | ||
}); | ||
``` | ||
Join queries have somewhat different results at the moment. | ||
The format of the result is the same as with anydb | ||
## joins and subobjects | ||
Join queries can be constructed using node-sql. The format of the results is | ||
the same as with anydb | ||
```js | ||
@@ -54,29 +63,16 @@ user.select(user.name, post.content) | ||
.where(post.date.gt(yesterday)) | ||
.where(user.id.equals(id)) | ||
.get(function(err, res) { | ||
// res['name'] and res['content'] | ||
.all(function(err, userposts) { | ||
// res[0].name and res[0].content | ||
}); | ||
``` | ||
Create a transactions and execute queries within it | ||
When creating join queries, you can generate sub-objects in the result by | ||
specifying aliases that contain dots in the name: | ||
```js | ||
var tx = db.begin() | ||
user.insert({name: 'blah'}).returning(user.id).execWithin(tx); | ||
user.insert({name: 'bleh'}).returning(user.id).execWithin(tx); | ||
tx.commit(); | ||
``` | ||
Transactions have the same API as anydb tranactions | ||
In join queries you can create sub-objects in the result by specifying aliases | ||
that contain dots in the name: | ||
```js | ||
user.select(user.name.as('user.name'), post.content.as('post.content')) | ||
.from(user.join(post).on(user.id.equals(post.userId))) | ||
.where(post.date.gt(yesterday)) | ||
.where(user.id.equals(id)) | ||
.get(function(err, res) { | ||
// res.user.name and res.post.content | ||
.all(function(err, res) { | ||
// res[0].user.name and res[0].post.content | ||
}); | ||
@@ -86,3 +82,3 @@ ``` | ||
Or you can use db.allOf to select all columns and get them in sub-objects: | ||
or you can use db.allOf to do the above for all the table's columns : | ||
@@ -93,15 +89,25 @@ ```js | ||
.where(post.date.gt(yesterday)) | ||
.where(user.id.equals(id)) | ||
.get(function(err, res) { | ||
// contains res.user.name, res.post.content and all | ||
// other columns from both tables in two subobjects | ||
// per row. | ||
.all(function(err, res) { | ||
// contains res[0].user.name, res[0].post.content and all | ||
// other columns from both tables in two subobjects per row. | ||
}); | ||
``` | ||
## transactions | ||
Create a transactions and execute queries within it | ||
```js | ||
var tx = db.begin() | ||
user.insert({name: 'blah'}).returning(user.id).execWithin(tx); | ||
user.insert({name: 'bleh'}).returning(user.id).execWithin(tx); | ||
tx.commit(); | ||
``` | ||
Finally you can close the connection pool | ||
Transactions have the same API as anydb tranactions | ||
# db.close and custom queries | ||
You can close the connection pool | ||
```js | ||
@@ -121,3 +127,1 @@ db.close(); | ||
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
51135
17
271
123
1