Merge branch 'master' of github.com:spotify/backstage

This commit is contained in:
Nikita Nek Dudnik
2020-05-19 14:03:48 +02:00
6 changed files with 206 additions and 17 deletions
+1
View File
@@ -24,6 +24,7 @@
"helmet": "^3.22.0",
"knex": "^0.21.1",
"morgan": "^1.10.0",
"sqlite3": "^4.2.0",
"uuid": "^8.0.0",
"winston": "^3.2.1",
"yaml": "^1.9.2",
@@ -59,4 +59,20 @@ describe('Database', () => {
/Found no location/,
);
});
it('instead of adding second location with the same target, returns existing one', async () => {
// Prepare
const catalog = new Database(database);
const input: AddDatabaseLocation = { type: 'a', target: 'b' };
const output1: DatabaseLocation = await catalog.addLocation(input);
// Try to insert the same location
const output2: DatabaseLocation = await catalog.addLocation(input);
const locations = await catalog.locations();
// Output is the same
expect(output2).toEqual(output1);
// Locations contain only one record
expect(locations).toEqual([output1]);
});
});
@@ -60,14 +60,29 @@ export class Database {
}
async addLocation(location: AddDatabaseLocation): Promise<DatabaseLocation> {
const id = uuidv4();
const { type, target } = location;
await this.database<DatabaseLocation>('locations').insert({
id,
type,
target,
return await this.database.transaction<DatabaseLocation>(async (tx) => {
const existingLocation = await tx<DatabaseLocation>('locations')
.where({
target: location.target,
})
.select();
if (existingLocation?.[0]) {
return existingLocation[0];
}
const id = uuidv4();
const { type, target } = location;
await tx<DatabaseLocation>('locations').insert({
id,
type,
target,
});
return (
await tx<DatabaseLocation>('locations').where({ id }).select()
)![0];
});
return await this.location(id);
}
async removeLocation(id: string): Promise<void> {