Skip to content

Commit

Permalink
fix: add generated property to model properties
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Nov 4, 2023
1 parent a1d9576 commit f6248d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ function mixinDiscovery(PostgreSQL) {
cb(err, results);
} else {
results.map(function(r) {
// PostgreSQL returns ALWAYS in case the property is generated,
// otherwise it returns NEVER
if (r.generated === 'ALWAYS') {
r.generated = true;
} else {
r.generated = false;
}
// PostgreSQL accepts float(1) to float(24) as selecting the `real` type,
// while float(25) to float(53) select `double precision`
// https://www.postgresql.org/docs/9.4/static/datatype-numeric.html
Expand Down Expand Up @@ -166,6 +173,7 @@ function mixinDiscovery(PostgreSQL) {
if (owner) {
sql = this.paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
+ 'data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
+ ' is_generated AS "generated",'
+ ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
+ ' FROM information_schema.columns'
+ ' WHERE table_schema=\'' + owner + '\''
Expand All @@ -175,6 +183,7 @@ function mixinDiscovery(PostgreSQL) {
sql = this.paginateSQL('SELECT current_schema() AS "owner", table_name AS "tableName",'
+ ' column_name AS "columnName",'
+ ' data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
+ ' is_generated AS "generated",'
+ ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
+ ' FROM information_schema.columns'
+ (table ? ' WHERE table_name=\'' + table + '\'' : ''),
Expand Down
19 changes: 19 additions & 0 deletions test/postgresql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,25 @@ describe('Discover and map correctly database types', function() {
});
});

describe('Discover model with generated property', function() {
it('should handle generated column', function(done) {
db.discoverSchema('inventory', {owner: 'strongloop'}, function(err, schema) {
if (err) {
console.error(err);
done(err);
} else {
assert(schema.options.postgresql.schema === 'strongloop');
assert(schema.options.postgresql.table === 'inventory');
assert(schema.properties.token);
assert(schema.properties.token.generated === true);
assert(schema.properties.productId);
assert(schema.properties.productId.generated === false);
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
3 changes: 2 additions & 1 deletion test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ CREATE TABLE inventory (
product_id character varying(20),
location_id character varying(20),
available integer,
total integer
total integer,
token integer GENERATED ALWAYS AS (total * 1000) STORED,
);


Expand Down

0 comments on commit f6248d4

Please sign in to comment.