Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,33 @@ function mixinMigration(PostgreSQL) {
return colDefault ? (' DEFAULT ' + colDefault) : '';
};

// override this function from base connector to allow postgres connector to
// accept dataPrecision and dataScale as column specific properties
PostgreSQL.prototype.columnDataType = function(model, property) {
var columnMetadata = this.columnMetadata(model, property);
var colType = columnMetadata && columnMetadata.dataType;
if (colType) {
colType = colType.toUpperCase();
}
var prop = this.getModelDefinition(model).properties[property];
if (!prop) {
return null;
}
var colLength = columnMetadata && columnMetadata.dataLength || prop.length || prop.limit;
var colPrecision = columnMetadata && columnMetadata.dataPrecision;
var colScale = columnMetadata && columnMetadata.dataScale;
// info on setting column specific properties
// i.e dataLength, dataPrecision, dataScale
// https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html
if (colType) {
if (colLength) return colType + '(' + colLength + ')';
if (colPrecision && colScale) return colType + '(' + colPrecision + ',' + colScale + ')';
if (colPrecision) return colType + '(' + colPrecision + ')';
return colType;
}
return this.buildColumnType(prop);
};

PostgreSQL.prototype.buildColumnType = function buildColumnType(propertyDefinition) {
var p = propertyDefinition;
switch (p.type.name) {
Expand Down
82 changes: 81 additions & 1 deletion test/postgresql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require('./init');
var async = require('async');
var should = require('should');

var Post, db, created;
var Post, Expense, db, created;

describe('lazyConnect', function() {
it('should skip connect phase (lazyConnect = true)', function(done) {
Expand Down Expand Up @@ -69,6 +69,68 @@ describe('postgresql connector', function() {
created = new Date();
});

describe('Explicit datatype', function() {
before(function(done) {
db = getDataSource();

Expense = db.define('Expense', {
id: {
type: Number,
id: true,
required: true,
postgresql: {
dataType: 'NUMERIC',
dataPrecision: 3,
},
},
description: {
type: String,
},
amount: {
type: Number,
required: true,
postgresql: {
dataType: 'DECIMAL',
dataPrecision: 10,
dataScale: 2,
},
},
});
db.automigrate(done);
});

it('create instance with explicit datatype', function(done) {
Expense.create(data, function(err, result) {
should.not.exist(err);
should.exist(result);
should.equal(result.length, data.length);
done();
});
});

it('find instance with a decimal datatype', function(done) {
Expense.find({where: {amount: 159.99}}, function(err, result) {
should.not.exist(err);
should.exist(result);
should.equal(result.length, 1);
// need to parseFloat the amount value since it is returned as a string
// because loopback does not have a known "decimal" datatype
should.deepEqual(parseFloat(result[0].__data.amount), data[0].amount);
done();
});
});

it('find instance with a numeric datatype', function(done) {
Expense.find({where: {id: 258}}, function(err, result) {
should.not.exist(err);
should.exist(result);
should.equal(result.length, 1);
should.deepEqual(parseInt(result[0].__data.id), data[2].id);
done();
});
});
});

it('should run migration', function(done) {
db.automigrate('PostWithBoolean', function() {
done();
Expand Down Expand Up @@ -562,6 +624,24 @@ describe('Serial properties', function() {
});
});

var data = [
{
id: 1,
description: 'Expense 1',
amount: 159.99,
},
{
id: 200,
description: 'Expense 2',
amount: 10,
},
{
id: 258,
description: 'Expense 3',
amount: 12.49,
},
];

// FIXME: The following test cases are to be reactivated for PostgreSQL
/*

Expand Down