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 #57 from viddagrava/master
Browse files Browse the repository at this point in the history
Indexes on single attribute foreign keys
  • Loading branch information
chriskalmar committed Oct 7, 2020
2 parents 25b74d0 + 5fdec08 commit cb8f8cf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/generator.js
Expand Up @@ -83,6 +83,7 @@ export const loadModels = configuration => {
const references = [];
const dataShaperMap = {};
const filterShaperMap = {};
const foreignKeyIndices = [];

const Skeleton = () => {};

Expand All @@ -107,7 +108,7 @@ export const loadModels = configuration => {
if (attribute.meta && attribute.meta.storageAttributeName) {
storageAttributeName = attribute.meta.storageAttributeName;
} else if (isViewEntity(entity)) {
storageAttributeName = attribute.name
storageAttributeName = attribute.name;
} else {
storageAttributeName = _.snakeCase(attribute.name);
}
Expand Down Expand Up @@ -144,6 +145,17 @@ export const loadModels = configuration => {
targetAttributeName: primaryAttribute.name,
targetEntityName,
});

const foreignKeyIndexName = generateIndexName(
_.snakeCase(entityName),
[_.snakeCase(attributeName)],
'key',
);

Index(foreignKeyIndexName, [attributeName], { unique: false })(
Skeleton,
);
foreignKeyIndices.push(foreignKeyIndexName);
}

ManyToOne(() => modelRegistry[targetEntityName].model, {
Expand Down Expand Up @@ -202,7 +214,9 @@ export const loadModels = configuration => {
);

const unique = index.type === INDEX_UNIQUE;
Index(indexName, index.attributes, { unique })(Skeleton);
if (!foreignKeyIndices.includes(indexName)) {
Index(indexName, index.attributes, { unique })(Skeleton);
}

constraints.unique[indexName] = {
attributes: index.attributes,
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/integration.spec.js.snap
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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",
]
`;
15 changes: 15 additions & 0 deletions test/integration.spec.js
Expand Up @@ -8,6 +8,7 @@ import { counts } from './testSetGenerator';
import { Profile } from './models/Profile';
import { Board } from './models/Board';
import { BoardMember } from './models/BoardMember';
import { StorageTypePostgres } from '../src/StorageTypePostgres';

describe('postgres', () => {
it('test data imported correctly', async () => {
Expand All @@ -20,4 +21,18 @@ describe('postgres', () => {
const memberCount = await count(BoardMember, {}, asAdmin());
expect(memberCount).toEqual(counts.joinCount + counts.inviteCount);
});

it('should check the generated indexes', async () => {
const storageInstance = StorageTypePostgres.getStorageInstance();
const manager = storageInstance.manager;
const indexes = await manager.query(`
select indexname
from pg_indexes
where tablename = 'board'
order by indexname
`);

const indexList = indexes.map(i => i.indexname);
expect(indexList).toMatchSnapshot('indexList');
});
});

0 comments on commit cb8f8cf

Please sign in to comment.