Skip to content

Resource

Resources are long-lived infrastructure clients like database pools, cache connections, API clients, or configuration objects.

class Database extends Vla.Resource {
static readonly unwrap = "db"
db = this.devStable("prisma", () => new PrismaClient())
}
class UserRepo extends Vla.Repo {
// Thanks to unwrap, this injects the 'db' property directly
db = this.inject(Database)
async findAll() {
return this.db.user.findMany()
}
}
  • Database connections
  • Cache clients
  • Configuration singletons

singleton - Single instance created and cached forever.

static readonly scope = 'singleton'
static readonly unwrap?: PropertyKey

The unwrap static property allows injectors to receive a specific property instead of the entire Resource instance.

class Database extends Vla.Resource {
static readonly unwrap = "client"
client = new PrismaClient()
}
class UserRepo extends Vla.Repo {
// Injects the 'client' property directly, not the Database instance
db = this.inject(Database)
async findAll() {
return this.db.user.findMany() // this.db is PrismaClient
}
}
  • Scope: singleton (single instance forever)
  • Purpose: Database connections, cache clients, configuration
  • Can inject: Other Resources, Contexts
  • Best practices: Use for expensive-to-create resources; implement cleanup methods

Resources can be injected cross-module.