diff --git a/src/generator.js b/src/generator.js index 7c4f480..b5dc8df 100644 --- a/src/generator.js +++ b/src/generator.js @@ -153,9 +153,18 @@ export const loadModels = configuration => { 'key', ); - Index(foreignKeyIndexName, [attributeName], { unique: false })( - Skeleton, - ); + let unique = false; + // checking if there is a defined index for the foreign key + if (entity.indexes) { + const definedForeignKeyIndex = entity.indexes.filter(index => + _.isEqual(index.attributes, [attributeName]), + ); + if (definedForeignKeyIndex && definedForeignKeyIndex.length > 0) { + unique = definedForeignKeyIndex[0].type === INDEX_UNIQUE; + } + } + + Index(foreignKeyIndexName, [attributeName], { unique })(Skeleton); foreignKeyIndices.push(foreignKeyIndexName); } diff --git a/test/__snapshots__/integration.spec.js.snap b/test/__snapshots__/integration.spec.js.snap index f6e74cc..e14c57a 100644 --- a/test/__snapshots__/integration.spec.js.snap +++ b/test/__snapshots__/integration.spec.js.snap @@ -2,11 +2,33 @@ exports[`postgres should check the generated indexes: indexList 1`] = ` Array [ - "PK_865a0f2e22c140d261b1df80eb1", - "board_created_by_key", - "board_is_private_key", - "board_name_key", - "board_owner_key", - "board_updated_by_key", + Object { + "indexname": "PK_865a0f2e22c140d261b1df80eb1", + "unique": true, + }, + Object { + "indexname": "board_created_by_key", + "unique": false, + }, + Object { + "indexname": "board_is_private_key", + "unique": false, + }, + Object { + "indexname": "board_name_key", + "unique": true, + }, + Object { + "indexname": "board_owner_key", + "unique": false, + }, + Object { + "indexname": "board_updated_by_key", + "unique": false, + }, + Object { + "indexname": "board_vip_key", + "unique": true, + }, ] `; diff --git a/test/data/boards.csv b/test/data/boards.csv index b2f4b2e..2809df8 100644 --- a/test/data/boards.csv +++ b/test/data/boards.csv @@ -1,50 +1,50 @@ -Reiciendis quaerat,37,0 -Et eum,46,1 -Occaecati adipisci vel,79,1 -Delectus qui maxime,42,0 -Cumque impedit est,55,1 -Ut et,42,1 -Vel ullam,72,1 -Illo sit quia,59,1 -Veritatis nihil cum,52,1 -Sit aut,71,1 -Aut incidunt consequatur,81,0 -Tenetur porro,23,0 -Suscipit aut cum,84,0 -Id cum,26,0 -Sunt nobis,60,0 -Est adipisci inventore,73,0 -Est et,24,0 -In ipsum est,75,0 -Rerum ad,48,0 -Sunt quo,10,0 -Quia sapiente,62,0 -Excepturi amet animi,88,1 -Aut molestiae,12,0 -Non iusto unde,30,1 -Libero similique vitae,38,0 -Blanditiis cumque nisi,29,0 -Mollitia qui,14,0 -Deleniti esse itaque,13,0 -Ut provident assumenda,73,0 -Fuga accusantium voluptatem,6,0 -Culpa facilis,20,0 -Aliquam voluptates,78,1 -Suscipit quas,47,1 -Voluptas dolor,71,0 -Voluptatum necessitatibus molestias,17,0 -Consectetur blanditiis consequatur,40,0 -Voluptate tempora veritatis,42,0 -Nesciunt nihil enim,37,0 -Expedita qui,61,1 -Dignissimos est perferendis,65,0 -Enim dolore,27,0 -Quo ut rerum,37,1 -Sapiente laborum non,61,1 -Qui rem ut,9,0 -Sed necessitatibus facilis,85,1 -Et architecto,63,1 -Accusamus sequi,23,0 -Nobis totam,41,1 -Odit qui,26,1 -Sed assumenda repellendus,84,1 +Reiciendis quaerat,37,0,103 +Et eum,46,1,62 +Occaecati adipisci vel,79,1,50 +Delectus qui maxime,42,0,10 +Cumque impedit est,55,1,4 +Ut et,42,1,102 +Vel ullam,72,1,9 +Illo sit quia,59,1,21 +Veritatis nihil cum,52,1,61 +Sit aut,71,1,73 +Aut incidunt consequatur,81,0,12 +Tenetur porro,23,0,83 +Suscipit aut cum,84,0,89 +Id cum,26,0,26 +Sunt nobis,60,0,72 +Est adipisci inventore,73,0,14 +Est et,24,0,55 +In ipsum est,75,0,57 +Rerum ad,48,0,71 +Sunt quo,10,0,59 +Quia sapiente,62,0,93 +Excepturi amet animi,88,1,22 +Aut molestiae,12,0,51 +Non iusto unde,30,1,85 +Libero similique vitae,38,0,91 +Blanditiis cumque nisi,29,0,65 +Mollitia qui,14,0,45 +Deleniti esse itaque,13,0,48 +Ut provident assumenda,73,0,42 +Fuga accusantium voluptatem,6,0,19 +Culpa facilis,20,0,15 +Aliquam voluptates,78,1,82 +Suscipit quas,47,1,96 +Voluptas dolor,71,0,69 +Voluptatum necessitatibus molestias,17,0,3 +Consectetur blanditiis consequatur,40,0,81 +Voluptate tempora veritatis,42,0,36 +Nesciunt nihil enim,37,0,52 +Expedita qui,61,1,41 +Dignissimos est perferendis,65,0,107 +Enim dolore,27,0,17 +Quo ut rerum,37,1,7 +Sapiente laborum non,61,1,70 +Qui rem ut,9,0,29 +Sed necessitatibus facilis,85,1,74 +Et architecto,63,1,109 +Accusamus sequi,23,0,32 +Nobis totam,41,1,35 +Odit qui,26,1,101 +Sed assumenda repellendus,84,1,16 diff --git a/test/integration.spec.js b/test/integration.spec.js index cd8536a..8f410ae 100644 --- a/test/integration.spec.js +++ b/test/integration.spec.js @@ -26,13 +26,14 @@ describe('postgres', () => { const storageInstance = StorageTypePostgres.getStorageInstance(); const manager = storageInstance.manager; const indexes = await manager.query(` - select indexname + select + indexname, + indexdef ILIKE '%UNIQUE%' AS unique from pg_indexes where tablename = 'board' order by indexname `); - const indexList = indexes.map(i => i.indexname); - expect(indexList).toMatchSnapshot('indexList'); + expect(indexes).toMatchSnapshot('indexList'); }); }); diff --git a/test/loadData.js b/test/loadData.js index 904bdca..b240e5b 100644 --- a/test/loadData.js +++ b/test/loadData.js @@ -27,10 +27,11 @@ export const loadData = async () => { const boards = readRows('boards'); - await asyncForEach(boards, async ([name, userId, isPrivate]) => { + await asyncForEach(boards, async ([name, userId, isPrivate, vip]) => { const payload = { name, isPrivate: isPrivate === '1', + vip, }; await mutate(Board, 'build', payload, null, asUser(userId)); diff --git a/test/models/Board.js b/test/models/Board.js index de58253..9c65261 100644 --- a/test/models/Board.js +++ b/test/models/Board.js @@ -44,6 +44,10 @@ export const Board = new Entity({ type: INDEX_GENERIC, attributes: ['owner'], }), + new Index({ + type: INDEX_UNIQUE, + attributes: ['vip'], + }), ], mutations: ({ createMutation }) => [ @@ -52,13 +56,13 @@ export const Board = new Entity({ name: 'build', description: 'build a new board', type: MUTATION_TYPE_CREATE, - attributes: ['name', 'isPrivate'], + attributes: ['name', 'isPrivate', 'vip'], }), new Mutation({ name: 'update', description: 'update a board', type: MUTATION_TYPE_UPDATE, - attributes: ['name', 'isPrivate'], + attributes: ['name', 'isPrivate', 'vip'], }), ], @@ -86,6 +90,12 @@ export const Board = new Entity({ }, }, + vip: { + type: Profile, + description: 'VIP guest of the board', + required: true, + }, + isPrivate: { type: DataTypeBoolean, description: 'It is a private board', diff --git a/test/mutation.spec.js b/test/mutation.spec.js index 564e3c6..8fd4852 100644 --- a/test/mutation.spec.js +++ b/test/mutation.spec.js @@ -114,6 +114,7 @@ describe('mutation', () => { const payload = { name: 'New Board', isPrivate: false, + vip: 1, }; await mutate(Board, 'build', payload, null, asUser(99)); diff --git a/test/testUtils.js b/test/testUtils.js index 793ce43..c159aa4 100644 --- a/test/testUtils.js +++ b/test/testUtils.js @@ -35,6 +35,10 @@ export const removeDynamicData = (entity, payload) => { delete ret.registeredAt; } + if (entity.name === 'Board') { + delete ret.vip; + } + return ret; };