chore: cleanups

Co-authored-by: Ben Lambert <ben@blam.sh>
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-05-04 16:28:39 +02:00
parent 3492eccef2
commit ea6af51269
2 changed files with 19 additions and 26 deletions
@@ -14,24 +14,17 @@
* limitations under the License.
*/
import { v4 as uuid } from 'uuid';
import { BackgroundContext, Context, TransactionValue } from './Context';
import { DatabaseManager } from './database/DatabaseManager';
import { DefaultLocationStore } from './DefaultLocationStore';
const createLocationStore = async () => {
const knex = await DatabaseManager.createTestDatabaseConnection();
const db = await DatabaseManager.createDatabase(knex);
await DatabaseManager.createDatabase(knex);
const connection = { applyMutation: jest.fn() };
const store = new DefaultLocationStore(knex);
await store.connect(connection);
const withContext = async (handler: (ctx: Context) => Promise<any>) => {
return await db.transaction(async tx => {
const ctx = TransactionValue.in(new BackgroundContext(), tx as any);
return await handler(ctx);
});
};
return { store, connection, db, withContext };
return { store, connection };
};
describe('DefaultLocationStore', () => {
@@ -76,22 +76,7 @@ export class DefaultLocationStore implements LocationStore, EntityProvider {
}
async listLocations(): Promise<Location[]> {
return await this.locations(this.db);
}
private async locations(dbOrTx: Knex | Knex.Transaction) {
const locations = await dbOrTx<DbLocationsRow>('locations').select();
return (
locations
// TODO(blam): We should create a mutation to remove this location for everyone
// eventually when it's all done and dusted
.filter(({ type }) => type !== 'bootstrap')
.map(item => ({
id: item.id,
target: item.target,
type: item.type,
}))
);
return await this.locations();
}
async getLocation(id: string): Promise<Location> {
@@ -141,7 +126,7 @@ export class DefaultLocationStore implements LocationStore, EntityProvider {
async connect(connection: EntityProviderConnection): Promise<void> {
this._connection = connection;
const locations = await this.locations(this.db);
const locations = await this.locations();
const entities = locations.map(location => {
return locationSpecToLocationEntity(location);
@@ -152,4 +137,19 @@ export class DefaultLocationStore implements LocationStore, EntityProvider {
entities,
});
}
private async locations(dbOrTx: Knex.Transaction | Knex = this.db) {
const locations = await dbOrTx<DbLocationsRow>('locations').select();
return (
locations
// TODO(blam): We should create a mutation to remove this location for everyone
// eventually when it's all done and dusted
.filter(({ type }) => type !== 'bootstrap')
.map(item => ({
id: item.id,
target: item.target,
type: item.type,
}))
);
}
}