Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

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 5205831 commit cc6efef
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
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
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 @@ -715,6 +718,11 @@ var Patio = singleton([EventEmitter, Time], {
/**@ignore*/
underscore: function () {
return this.__underscore;
},

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

},
Expand Down Expand Up @@ -886,6 +894,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 @@ -911,4 +934,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
Expand Up @@ -37,6 +37,7 @@
"pg": "4.0.0",
"pg-query-stream": "0.7.0",
"pg-types": "1.6.0",
"is-safe-integer": "1.0.1",
"validator": "~1.5.1"
},
"scripts": {
Expand Down
62 changes: 61 additions & 1 deletion test/adapters/postgres.test.js
Expand Up @@ -7,7 +7,8 @@ var it = require('it'),
when = comb.when,
serial = comb.serial,
format = comb.string.format,
hitch = comb.hitch;
hitch = comb.hitch,
PgTypes = require('pg-types');

if (process.env.PATIO_DB === "pg") {
it.describe("patio.adapters.Postgres", function (it) {
Expand Down Expand Up @@ -549,6 +550,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 cc6efef

Please sign in to comment.