Skip to content

Commit 2e99944

Browse files
committed
Merge pull request #394 from pste/master
Handler for with(out) time zone time(stamp) types
2 parents 6d62762 + e23b639 commit 2e99944

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ See [LoopBack types](http://loopback.io/doc/en/lb3/LoopBack-types.html) for de
359359
<td>Number</td>
360360
</tr>
361361
<tr>
362-
<td>DATE<br>TIMESTAMP<br>TIME</td>
362+
<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>
363363
<td>Date</td>
364364
</tr>
365365
<tr>

lib/discovery.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ function mixinDiscovery(PostgreSQL) {
353353
return 'Number';
354354
case 'DATE':
355355
case 'TIMESTAMP':
356+
case 'TIMESTAMP WITHOUT TIME ZONE':
357+
case 'TIMESTAMP WITH TIME ZONE':
356358
case 'TIME':
359+
case 'TIME WITHOUT TIME ZONE':
360+
case 'TIME WITH TIME ZONE':
357361
return 'Date';
358362
case 'POINT':
359363
return 'GeoPoint';

test/postgresql.discover.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,30 @@ describe('Discover LDL schema from a table', function() {
327327
});
328328
});
329329

330+
describe('Discover and map correctly database types', function() {
331+
it('should handle character varying, date, timestamp with time zone, timestamp without time zone', function(done) {
332+
db.discoverSchema('customer', {owner: 'strongloop'}, function(err, schema) {
333+
if (err) {
334+
console.error(err);
335+
done(err);
336+
} else {
337+
assert(schema.properties.username);
338+
assert(schema.properties.username.type === 'String');
339+
assert(schema.properties.dateofbirth);
340+
assert(schema.properties.dateofbirth.type === 'Date');
341+
assert(schema.properties.dateofbirth.postgresql.dataType === 'date');
342+
assert(schema.properties.lastlogin);
343+
assert(schema.properties.lastlogin.type === 'Date');
344+
assert(schema.properties.lastlogin.postgresql.dataType === 'timestamp with time zone');
345+
assert(schema.properties.created);
346+
assert(schema.properties.created.type === 'Date');
347+
assert(schema.properties.created.postgresql.dataType === 'timestamp without time zone');
348+
done();
349+
}
350+
});
351+
});
352+
});
353+
330354
describe('Discover and build models', function() {
331355
it('should build a model from discovery', function(done) {
332356
db.discoverAndBuildModels('GeoPoint', {schema: 'strongloop'}, function(err, schema) {

test/postgresql.timestamp.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ describe('Timestamps', function() {
2626
dbDefault: 'now()',
2727
},
2828
},
29+
timestampWithTypeNoZone: {
30+
type: 'Date',
31+
postgresql: {
32+
dataType: 'TIMESTAMP WITHOUT TIME ZONE',
33+
dbDefault: 'now()',
34+
},
35+
},
2936
timestampWithPrecision: {
3037
type: 'Date',
3138
postgresql: {
@@ -52,7 +59,24 @@ describe('Timestamps', function() {
5259

5360
it('create instance', function(done) {
5461
PostWithTimestamps.create(
55-
{timestampFromJs: new Date()}, function(err, p) {
62+
{
63+
timestampDefault: new Date,
64+
timestampWithType: new Date,
65+
timestampWithTypeNoZone: new Date,
66+
timestampWithPrecision: new Date,
67+
timestampFromJs: new Date,
68+
69+
}, function(err, p) {
70+
should.not.exist(err);
71+
should.exist(p);
72+
done();
73+
},
74+
);
75+
});
76+
77+
it('should handle default value on creation', function(done) {
78+
PostWithTimestamps.create(
79+
{timestampWithTypeNoZone: new Date()}, function(err, p) {
5680
should.not.exist(err);
5781
should.exist(p);
5882
done();

test/schema.sql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ CREATE TABLE customer (
303303
credentials character varying(1024),
304304
challenges character varying(1024),
305305
status character varying(1024),
306-
created date,
307-
lastupdated date
306+
dateofbirth date,
307+
lastlogin timestamp with time zone,
308+
created timestamp without time zone,
309+
lastupdated timestamp without time zone
308310
);
309311

310312

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

577-
INSERT INTO customer VALUES ('612', 'bat', '[email protected]', '$2a$10$BEG18wcYQn7TRkFIc59EB.vmnsEwqJWMlYM4DNG73iZb.MKA1rjAC', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
578-
INSERT INTO customer VALUES ('613', 'baz', '[email protected]', '$2a$10$jkSYF2gLMdI4CwVQh8AStOs0b24lDu9p8jccnmri/0rvhtwsicm9C', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
579-
INSERT INTO customer VALUES ('610', 'foo', '[email protected]', '$2a$10$tn1hN7Xv6x74cCB7tVfwkeaaJTd4/6q4RbCMzgmAJeWe40xqrRSui', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
580-
INSERT INTO customer VALUES ('611', 'bar', '[email protected]', '$2a$10$a8mCol6d5vQXm6vubqXl8e5V66StEg6E8vzjQqPpoyk95Vm3smpiK', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, NULL, NULL);
579+
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);
580+
INSERT INTO customer VALUES ('613', 'baz', '[email protected]', '$2a$10$jkSYF2gLMdI4CwVQh8AStOs0b24lDu9p8jccnmri/0rvhtwsicm9C', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '1980-02-02', NULL, NULL, NULL);
581+
INSERT INTO customer VALUES ('610', 'foo', '[email protected]', '$2a$10$tn1hN7Xv6x74cCB7tVfwkeaaJTd4/6q4RbCMzgmAJeWe40xqrRSui', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '1990-10-20', NULL, NULL, NULL);
582+
INSERT INTO customer VALUES ('611', 'bar', '[email protected]', '$2a$10$a8mCol6d5vQXm6vubqXl8e5V66StEg6E8vzjQqPpoyk95Vm3smpiK', NULL, NULL, NULL, NULL, NULL, '[]', '[]', NULL, '2000-12-31', NULL, NULL, NULL);
581583

582584

583585
--

0 commit comments

Comments
 (0)