Skip to content

Commit

Permalink
Merge pull request #394 from pste/master
Browse files Browse the repository at this point in the history
Handler for with(out) time zone time(stamp) types
  • Loading branch information
bajtos committed Nov 22, 2019
2 parents 6d62762 + e23b639 commit 2e99944
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ See [LoopBack types](http://loopback.io/doc/en/lb3/LoopBack-types.html) for de
<td>Number</td>
</tr>
<tr>
<td>DATE<br>TIMESTAMP<br>TIME</td>
<td>DATE<br>TIMESTAMP<br>TIMESTAMP WITH TIME ZONE<br>TIMESTAMP WITHOUT TIME ZONE<br>TIME</td>TIME WITH TIME ZONE</td>TIME WITHOUT TIME ZONE</td>
<td>Date</td>
</tr>
<tr>
Expand Down
4 changes: 4 additions & 0 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,11 @@ function mixinDiscovery(PostgreSQL) {
return 'Number';
case 'DATE':
case 'TIMESTAMP':
case 'TIMESTAMP WITHOUT TIME ZONE':
case 'TIMESTAMP WITH TIME ZONE':
case 'TIME':
case 'TIME WITHOUT TIME ZONE':
case 'TIME WITH TIME ZONE':
return 'Date';
case 'POINT':
return 'GeoPoint';
Expand Down
24 changes: 24 additions & 0 deletions test/postgresql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,30 @@ describe('Discover LDL schema from a table', function() {
});
});

describe('Discover and map correctly database types', function() {
it('should handle character varying, date, timestamp with time zone, timestamp without time zone', function(done) {
db.discoverSchema('customer', {owner: 'strongloop'}, function(err, schema) {
if (err) {
console.error(err);
done(err);
} else {
assert(schema.properties.username);
assert(schema.properties.username.type === 'String');
assert(schema.properties.dateofbirth);
assert(schema.properties.dateofbirth.type === 'Date');
assert(schema.properties.dateofbirth.postgresql.dataType === 'date');
assert(schema.properties.lastlogin);
assert(schema.properties.lastlogin.type === 'Date');
assert(schema.properties.lastlogin.postgresql.dataType === 'timestamp with time zone');
assert(schema.properties.created);
assert(schema.properties.created.type === 'Date');
assert(schema.properties.created.postgresql.dataType === 'timestamp without time zone');
done();
}
});
});
});

describe('Discover and build models', function() {
it('should build a model from discovery', function(done) {
db.discoverAndBuildModels('GeoPoint', {schema: 'strongloop'}, function(err, schema) {
Expand Down
26 changes: 25 additions & 1 deletion test/postgresql.timestamp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ describe('Timestamps', function() {
dbDefault: 'now()',
},
},
timestampWithTypeNoZone: {
type: 'Date',
postgresql: {
dataType: 'TIMESTAMP WITHOUT TIME ZONE',
dbDefault: 'now()',
},
},
timestampWithPrecision: {
type: 'Date',
postgresql: {
Expand All @@ -52,7 +59,24 @@ describe('Timestamps', function() {

it('create instance', function(done) {
PostWithTimestamps.create(
{timestampFromJs: new Date()}, function(err, p) {
{
timestampDefault: new Date,
timestampWithType: new Date,
timestampWithTypeNoZone: new Date,
timestampWithPrecision: new Date,
timestampFromJs: new Date,

}, function(err, p) {
should.not.exist(err);
should.exist(p);
done();
},
);
});

it('should handle default value on creation', function(done) {
PostWithTimestamps.create(
{timestampWithTypeNoZone: new Date()}, function(err, p) {
should.not.exist(err);
should.exist(p);
done();
Expand Down
14 changes: 8 additions & 6 deletions test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,10 @@ CREATE TABLE customer (
credentials character varying(1024),
challenges character varying(1024),
status character varying(1024),
created date,
lastupdated date
dateofbirth date,
lastlogin timestamp with time zone,
created timestamp without time zone,
lastupdated timestamp without time zone
);


Expand Down Expand Up @@ -574,10 +576,10 @@ SELECT pg_catalog.setval('"GeoPoint_id_seq"', 2, true);
-- Data for Name: customer; Type: TABLE DATA; Schema: strongloop; Owner: strongloop
--

INSERT INTO customer VALUES ('612', 'bat', '[email protected]', '$2a$10$BEG18wcYQn7TRkFIc59EB.vmnsEwqJWMlYM4DNG73iZb.MKA1rjAC', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
INSERT INTO customer VALUES ('613', 'baz', '[email protected]', '$2a$10$jkSYF2gLMdI4CwVQh8AStOs0b24lDu9p8jccnmri/0rvhtwsicm9C', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
INSERT INTO customer VALUES ('610', 'foo', '[email protected]', '$2a$10$tn1hN7Xv6x74cCB7tVfwkeaaJTd4/6q4RbCMzgmAJeWe40xqrRSui', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
INSERT INTO customer VALUES ('611', 'bar', '[email protected]', '$2a$10$a8mCol6d5vQXm6vubqXl8e5V66StEg6E8vzjQqPpoyk95Vm3smpiK', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
INSERT INTO customer VALUES ('612', 'bat', '[email protected]', '$2a$10$BEG18wcYQn7TRkFIc59EB.vmnsEwqJWMlYM4DNG73iZb.MKA1rjAC', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '1970-01-01', NULL, NULL, NULL);
INSERT INTO customer VALUES ('613', 'baz', '[email protected]', '$2a$10$jkSYF2gLMdI4CwVQh8AStOs0b24lDu9p8jccnmri/0rvhtwsicm9C', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '1980-02-02', NULL, NULL, NULL);
INSERT INTO customer VALUES ('610', 'foo', '[email protected]', '$2a$10$tn1hN7Xv6x74cCB7tVfwkeaaJTd4/6q4RbCMzgmAJeWe40xqrRSui', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '1990-10-20', NULL, NULL, NULL);
INSERT INTO customer VALUES ('611', 'bar', '[email protected]', '$2a$10$a8mCol6d5vQXm6vubqXl8e5V66StEg6E8vzjQqPpoyk95Vm3smpiK', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '2000-12-31', NULL, NULL, NULL);


--
Expand Down

0 comments on commit 2e99944

Please sign in to comment.