Skip to content

Commit 3f60fb7

Browse files
committed
feat: query to fetch unique columns
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 23e3874 commit 3f60fb7

5 files changed

+86
-56
lines changed

lib/discovery.js

+26
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,32 @@ function mixinDiscovery(PostgreSQL) {
335335
return sql;
336336
};
337337

338+
/**
339+
* Discover unique keys for a given table
340+
* @param {String} table The table name
341+
* @param {Object} options The options for discovery
342+
*/
343+
344+
/*!
345+
* Retrieves a list of column names that have unique key index
346+
* @param schema
347+
* @param table
348+
* @returns {string}
349+
*/
350+
PostgreSQL.prototype.buildQueryUniqueKeys = function(schema, table) {
351+
const sql = 'SELECT a.attname AS "columnName", n.nspname AS "owner", c.relname AS "tableName" '
352+
+ 'FROM pg_index i '
353+
+ 'JOIN pg_class c ON c.oid = i.indrelid '
354+
+ 'JOIN pg_namespace n ON n.oid = c.relnamespace '
355+
+ 'JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(i.indkey) '
356+
+ 'WHERE i.indisunique = true '
357+
+ 'AND i.indisprimary = false '
358+
+ 'AND n.nspname = \'' + schema + '\' '
359+
+ 'AND c.relname = \'' + table + '\' '
360+
+ 'ORDER BY a.attnum';
361+
return sql;
362+
};
363+
338364
/**
339365
* Discover foreign keys that reference to the primary key of this table
340366
* @param {String} table The table name

package-lock.json

+46-54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"bluebird": "^3.4.6",
3434
"chalk": "^4.0.0",
3535
"debug": "^4.1.1",
36-
"loopback-connector": "^6.0.0",
36+
"loopback-connector": "^6.2.1",
3737
"pg": "^8.0.2",
3838
"strong-globalize": "^6.0.0",
3939
"uuid": "^11.0.0"
@@ -44,7 +44,7 @@
4444
"eslint": "^8.0.0",
4545
"eslint-config-loopback": "^13.1.0",
4646
"lodash": "^4.17.4",
47-
"loopback-datasource-juggler": "^5.0.0",
47+
"loopback-datasource-juggler": "^5.1.5",
4848
"mocha": "^11.0.0",
4949
"rc": "^1.0.0",
5050
"should": "^13.2.3",

test/postgresql.discover.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ describe('Discover LDL schema from a table', function() {
394394
});
395395
});
396396

397+
describe('Discover unique properties', function() {
398+
it('should validate unique key for user', function(done) {
399+
db.discoverSchema('user', {owner: 'strongloop'}, function(err, schema) {
400+
console.log('This is our err: ', err);
401+
console.log('This is our schema: ', schema);
402+
assert(schema.properties.email.index.unique, true);
403+
done(null, schema);
404+
});
405+
});
406+
});
407+
397408
describe('Discover and map correctly database types', function() {
398409
it('should handle character varying, date, timestamp with time zone, timestamp without time zone', function(done) {
399410
db.discoverSchema('customer', {owner: 'strongloop'}, function(err, schema) {

test/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ CREATE TABLE "user" (
276276
email character varying(100)
277277
);
278278

279+
ALTER TABLE "user" ADD CONSTRAINT user_email_unique UNIQUE (email);
279280
--
280281
-- Name: GeoPoint_id_seq; Type: SEQUENCE; Schema: strongloop; Owner: strongloop
281282
--

0 commit comments

Comments
 (0)