Skip to content

Commit f6248d4

Browse files
committed
fix: add generated property to model properties
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent a1d9576 commit f6248d4

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

lib/discovery.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ function mixinDiscovery(PostgreSQL) {
135135
cb(err, results);
136136
} else {
137137
results.map(function(r) {
138+
// PostgreSQL returns ALWAYS in case the property is generated,
139+
// otherwise it returns NEVER
140+
if (r.generated === 'ALWAYS') {
141+
r.generated = true;
142+
} else {
143+
r.generated = false;
144+
}
138145
// PostgreSQL accepts float(1) to float(24) as selecting the `real` type,
139146
// while float(25) to float(53) select `double precision`
140147
// https://www.postgresql.org/docs/9.4/static/datatype-numeric.html
@@ -166,6 +173,7 @@ function mixinDiscovery(PostgreSQL) {
166173
if (owner) {
167174
sql = this.paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
168175
+ 'data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
176+
+ ' is_generated AS "generated",'
169177
+ ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
170178
+ ' FROM information_schema.columns'
171179
+ ' WHERE table_schema=\'' + owner + '\''
@@ -175,6 +183,7 @@ function mixinDiscovery(PostgreSQL) {
175183
sql = this.paginateSQL('SELECT current_schema() AS "owner", table_name AS "tableName",'
176184
+ ' column_name AS "columnName",'
177185
+ ' data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
186+
+ ' is_generated AS "generated",'
178187
+ ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
179188
+ ' FROM information_schema.columns'
180189
+ (table ? ' WHERE table_name=\'' + table + '\'' : ''),

test/postgresql.discover.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,25 @@ describe('Discover and map correctly database types', function() {
408408
});
409409
});
410410

411+
describe('Discover model with generated property', function() {
412+
it('should handle generated column', function(done) {
413+
db.discoverSchema('inventory', {owner: 'strongloop'}, function(err, schema) {
414+
if (err) {
415+
console.error(err);
416+
done(err);
417+
} else {
418+
assert(schema.options.postgresql.schema === 'strongloop');
419+
assert(schema.options.postgresql.table === 'inventory');
420+
assert(schema.properties.token);
421+
assert(schema.properties.token.generated === true);
422+
assert(schema.properties.productId);
423+
assert(schema.properties.productId.generated === false);
424+
done();
425+
}
426+
});
427+
});
428+
});
429+
411430
describe('Discover and build models', function() {
412431
it('should build a model from discovery', function(done) {
413432
db.discoverAndBuildModels('GeoPoint', {schema: 'strongloop'}, function(err, schema) {

test/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ CREATE TABLE inventory (
319319
product_id character varying(20),
320320
location_id character varying(20),
321321
available integer,
322-
total integer
322+
total integer,
323+
token integer GENERATED ALWAYS AS (total * 1000) STORED,
323324
);
324325

325326

0 commit comments

Comments
 (0)