Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #59 from viddagrava/master
Browse files Browse the repository at this point in the history
Fixing defined indices on foreign keys
  • Loading branch information
chriskalmar committed Oct 8, 2020
2 parents 8f08b04 + 9d3c524 commit 71919b9
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 65 deletions.
15 changes: 12 additions & 3 deletions src/generator.js
Expand Up @@ -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);
}

Expand Down
34 changes: 28 additions & 6 deletions test/__snapshots__/integration.spec.js.snap
Expand Up @@ -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,
},
]
`;
100 changes: 50 additions & 50 deletions 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
7 changes: 4 additions & 3 deletions test/integration.spec.js
Expand Up @@ -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');
});
});
3 changes: 2 additions & 1 deletion test/loadData.js
Expand Up @@ -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));
Expand Down
14 changes: 12 additions & 2 deletions test/models/Board.js
Expand Up @@ -44,6 +44,10 @@ export const Board = new Entity({
type: INDEX_GENERIC,
attributes: ['owner'],
}),
new Index({
type: INDEX_UNIQUE,
attributes: ['vip'],
}),
],

mutations: ({ createMutation }) => [
Expand All @@ -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'],
}),
],

Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions test/mutation.spec.js
Expand Up @@ -114,6 +114,7 @@ describe('mutation', () => {
const payload = {
name: 'New Board',
isPrivate: false,
vip: 1,
};

await mutate(Board, 'build', payload, null, asUser(99));
Expand Down
4 changes: 4 additions & 0 deletions test/testUtils.js
Expand Up @@ -35,6 +35,10 @@ export const removeDynamicData = (entity, payload) => {
delete ret.registeredAt;
}

if (entity.name === 'Board') {
delete ret.vip;
}

return ret;
};

Expand Down

0 comments on commit 71919b9

Please sign in to comment.