Data Routing with Upstream getRouter
You can route a particular data model to a specific upstream datastore by providing
getRouter
to the upstream config of the type class.
import { Upstream } from 'ts-basis'
@dataclass({ classVersion: semver('1.0.0') })
class UserInPostgres {
static index: typeof UserUpstream.index
static upstream = {
getRouter() {
// this will only push to redis
return {
universe: 'production',
target: 'postgres',
}
}
}
id: string = '';
name: string = '';
constructor(init?: Partial<UserInRedis>) {
Object.assign(this, init);
}
}
@dataclass({ classVersion: semver('1.0.0') })
class UserInRedis {
static index: typeof UserUpstream.index
static upstream = {
getRouter() {
// this will only push to redis
return {
universe: 'production',
target: 'redis',
}
}
}
id: string = '';
name: string = '';
constructor(init?: Partial<UserInRedis>) {
Object.assign(this, init);
}
}
// Adding different datastores
Upstream.add(new UpstreamDatastorePostgres(localPostgresDsConfig), 'postgres')
Upstream.add(new UpstreamDatastoreRedis(localPostgresDsConfig), 'redis')
await push(new UserInPostgres({})) // row added to postgres
await push(new UserInRedis({})) // row added to redis