Pluggable Datasource
TypeScript interfaces for an abstract datasource that can be "plugged" with
different implementations. Includes classes for a mock datasource that can be
used during development and demos.
$ npm install @boundstate/pluggable-ds
Datasource interfaces
Define the interfaces for records, repos, and the datasource that you will be implementing.
person.ts
import { Record, Relation, RepoFindById, RepoPersist, RepoRemoveById } from '@boundstate/pluggable-ds';
import { Address } from './address';
export interface PersonAttributes {
id: string | null;
firstName: string;
lastName: string;
address: Relation<Address>;
}
export class Person implements Record, PersonAttributes {
id: string | null;
firstName: string;
lastName: string;
address: Relation<Address>;
}
export interface PersonRepo extends
RepoFindById<Person>,
RepoPersist<PersonAttributes, Person>,
RepoRemoveById<Person> {}
datasource.ts
import { Address } from './address';
import { Person } from './person';
export interface Datasource {
addresses: AddressRepo;
people: PersonRepo;
}
Mock implementation
Extend the MockRepo
and MockDatasource
classes to create a mock implementation of your datasource:
mock-person-repo.ts
import { MockRepo } from '@boundstate/pluggable-ds';
import { PersonAttributes, PersonRepo, Person } from './person';
export class MockPersonRepo extends MockRepo<PersonAttributes, Person, {}, {}> implements PersonRepo {
constructor() {
super({ RecordClass: Person });
}
}
mock-datasource.ts
import { MockDatasource } from '@boundstate/pluggable-ds';
import { Datasource } from './datasource';
import { MockAddressRepo } from './mock-address-repo';
import { MockPersonRepo } from './mock-person-repo';
export class MyMockDatasource extends MockDatasource implements Datasource {
addresses = this.register('addresses', MockAddressRepo);
people = this.register('people', MockPersonRepo);
}