Skip to content

Commit

Permalink
Adding parseInt8 option
Browse files Browse the repository at this point in the history
  • Loading branch information
aheuermann committed Feb 29, 2016
1 parent c61641c commit 8c4afbc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
16 changes: 15 additions & 1 deletion lib/adapters/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ var pg = require("pg"),
stream = require("stream"),
PassThroughStream = stream.PassThrough,
pipeAll = require("../utils").pipeAll,
hashPick = comb.hash.pick;
hashPick = comb.hash.pick,
isSafeInteger = Number.isSafeInteger || require('is-safe-integer');

var getPatio = function () {
return patio || (patio = require("../index.js"));
Expand Down Expand Up @@ -77,6 +78,19 @@ var byteaParser = function (val) {
};

PgTypes.setTypeParser(17, "text", byteaParser);

PgTypes.setTypeParser(20, 'text', function (val) {
if (!getPatio().parseInt8) {
return val;
}

var i = parseInt(val, 10);
if (!isSafeInteger(i)) {
throw new Error(format("The value '%s' cannot be represented by a javascript number.", val));
}
return i;
});

var timestampOrig = PgTypes.getTypeParser(1114, "text");
//PgTypes.setTypeParser(25, "text", byteaParser);
PgTypes.setTypeParser(1114, "text", function (val) {
Expand Down
25 changes: 24 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ var Patio = singleton([EventEmitter, Time], {

__inImportOfModels: false,

__parseInt8: false,


/**
* A singleton class that acts as the entry point for all actions performed in patio.
Expand All @@ -69,6 +71,7 @@ var Patio = singleton([EventEmitter, Time], {
*
* patio.camelize = true;
* patio.quoteIdentifiers=false;
* patio.parseInt8=false
* patio.defaultPrimaryKeyType = "integer" //"bigint"
*
* patio.createModel("my_table");
Expand Down Expand Up @@ -677,6 +680,11 @@ var Patio = singleton([EventEmitter, Time], {
/**@ignore*/
underscore: function () {
return this.__underscore;
},

/**@ignore*/
parseInt8: function () {
return this.__parseInt8;
}

},
Expand Down Expand Up @@ -848,6 +856,21 @@ var Patio = singleton([EventEmitter, Time], {
this.__underscore = underscore;
},

/**
* Sets whether bigint types should be parsed to a number. An error will be thrown if set and the number is
* set and the number is greater than 2^53 or less than -2^53.
*
* @ignoreCode
* @field
* @type Boolean
*
* @example
* patio.parseInt8 = true
* */
parseInt8: function (value) {
this.__parseInt8 = value;
},

/**
* Set the default primary key type when not specified for all databases by default. By default,
* patio uses "integer".
Expand All @@ -873,4 +896,4 @@ patio.__Patio = Patio;
var adapters = Database.ADAPTERS;
for (var i in adapters) {
patio[i] = adapters[i];
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"comb-proxy": "~1.0.0",
"commander": "~2.9.0",
"hive-cache": "0.0.3",
"is-safe-integer": "1.0.1",
"mysql": "2.10.0",
"pg": "4.4.3",
"pg-query-stream": "1.0.0",
Expand Down
62 changes: 61 additions & 1 deletion test/adapters/postgres.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var it = require('it'),
patio = require("../../lib"),
sql = patio.SQL,
comb = require("comb"),
config = require("../test.config.js");
config = require("../test.config.js"),
PgTypes = require('pg-types');

if (process.env.PATIO_DB === "pg") {
it.describe("patio.adapters.Postgres", function (it) {
Expand Down Expand Up @@ -545,6 +546,65 @@ if (process.env.PATIO_DB === "pg") {
});
});

it.describe("parseInt8", function (it) {

it.beforeEach(function () {
return db.forceCreateTable("posts", function () {
this.postId("bigint");
});
});

it.afterAll(function () {
patio.parseInt8 = false;
return db.dropTable("posts");
});

it.should("parse bigints on select if true", function () {
var ds = db.from("posts");
patio.parseInt8 = true;
return ds.insert({postId: "9007199254740991"})
.chain(function () {
return ds.one();
}).chain(function (post) {
assert.strictEqual(post.postId, 9007199254740991);
});
});

it.should("parse negative bigints as well", function () {
var ds = db.from("posts");
patio.parseInt8 = true;
return ds.insert({postId: "-9007199254740991"})
.chain(function () {
return ds.one();
}).chain(function (post) {
assert.strictEqual(post.postId, -9007199254740991);
});
});

it.should("not parse bigints if false", function () {
var ds = db.from("posts");
patio.parseInt8 = false;
return ds.insert({postId: "9007199254740992"})
.chain(function () {
return ds.one();
}).chain(function (post) {
assert.strictEqual(post.postId, "9007199254740992");
});
});

it.should("should throw if bigint is outside the range of max safe integer", function () {
patio.parseInt8 = true;
var bigIntParser = PgTypes.getTypeParser(20, 'text');
assert.throws(function () {
bigIntParser("9007199254740992");
}, /The value \'9007199254740992\' cannot be represented by a javascript number\./);
assert.throws(function () {
bigIntParser("-9007199254740992");
}, /The value \'\-9007199254740992\' cannot be represented by a javascript number\./);
});

});

it.should("support opclass specification", function () {
return db.createTable("posts", function () {
this.title("text");
Expand Down

0 comments on commit 8c4afbc

Please sign in to comment.