This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rather naïve implementation of stringify only (no going back).
- Loading branch information
0 parents
commit 5edb7e6
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
hstore for Node.js | ||
================== | ||
|
||
[Node.js][] module for transforming JavaScript objects to [hstore][] | ||
format as used by [PostgreSQL][]. | ||
|
||
This can be used to store key/value pairs in a format recognized by the | ||
database, so you can query them via SQL. | ||
|
||
[Node.js]: http://nodejs.org/ | ||
[hstore]: http://www.postgresql.org/docs/current/interactive/hstore.html | ||
[PostgreSQL]: http://www.postgresql.org/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Module for converting JavaScript objects to hstore format. | ||
*/ | ||
|
||
var hstore = {}; | ||
|
||
"use strict"; | ||
|
||
/** | ||
* Convert an arbitrary value to string. | ||
*/ | ||
function convertToString(value) { | ||
switch (typeof value) { | ||
case 'boolean': | ||
return String(value); | ||
case 'null': | ||
return 'NULL'; | ||
case 'number': | ||
return isFinite(value) ? String(value) : 'NULL' | ||
case 'string': | ||
return quoteString(value); | ||
} | ||
} | ||
|
||
/** | ||
* Quote a string. | ||
*/ | ||
function quoteString(string) { | ||
// Naïve quoting of strings. Assumes there's not quotes in the string. | ||
return '"' + string + '"'; | ||
} | ||
|
||
/** | ||
* Convert JavaScript object to hstore format. | ||
*/ | ||
hstore.stringify = function (data) { | ||
pairs = []; | ||
|
||
Object.keys(data).forEach(function (key) { | ||
var value = convertToString(data[key]); | ||
|
||
if (value) { | ||
pairs.push('"' + key + '"=>' + value); | ||
} | ||
}); | ||
|
||
return pairs.join(); | ||
}; | ||
|
||
module.exports = hstore; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"author": "Mikkel Hoegh <mikkel@hoegh.org> (http://mikkel.hoegh.org/)", | ||
"name": "hstore", | ||
"description": "Library for transforming JavaScript objects to hstore format, used by the PostgreSQL RDBMS.", | ||
"keywords" : ["postgres", "postgre", "database", "rdbms"], | ||
"main" : "hstore.js", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/mikl/node-hstore.git" | ||
}, | ||
"engines": { | ||
"node": "~v0.4.10" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |