Base Entities & Caching
Base entity classes
Extend one of these instead of writing identifier/timestamp/versioning fields by hand:
| Class | Adds |
|---|---|
SimpleEntity | just uid (a UUID primary key) |
BaseEntity | uid, dateCreated, dateModified, version (for optimistic locking) |
SimpleMongoEntity | SimpleEntity + a MongoDB _id column |
BaseMongoEntity | BaseEntity + a MongoDB _id column |
RecoverableBaseEntity | BaseEntity + a deleted soft-delete flag |
RecoverableBaseMongoEntity | RecoverableBaseEntity + a MongoDB _id column |
A typical MongoDB-backed entity:
import { BaseMongoEntity, PersistenceDecorators, ModelDecorators } from '@rapidrest/service-core';
const { Entity, Column } = PersistenceDecorators;
const { DataStore } = ModelDecorators;
@Entity()
@DataStore('mongo')
export class Pet extends BaseMongoEntity {
@Column()
name: string = '';
@Column()
species: string = '';
constructor(other?: Partial<Pet>) {
super(other);
Object.assign(this, other);
}
}
Use RecoverableBaseEntity when you want deletes to be reversible (records are flagged deleted: true rather than removed), and BaseEntity's
built-in version field gives you optimistic-locking updates for free through Auto CRUD Routes.
@Cache(ttl?)
Caches query results for an entity in Redis:
@Cache() // default TTL: 30 seconds
@DataStore('mongo')
export class Pet extends BaseMongoEntity {}
@Cache(120) // custom TTL, in seconds
@DataStore('mongo')
export class Category extends BaseMongoEntity {}
Caching only takes effect when a Redis connection named "cache" is configured. Without one, @Cache is silently a no-op rather than an error.
@TrackChanges(versions?)
Keeps historical versions of a record every time it's updated:
@TrackChanges() // keeps every version (default: -1)
@DataStore('mongo')
export class VersionedUser extends RecoverableBaseMongoEntity {}
@TrackChanges(5) // keeps only the last 5 versions
export class AuditedRecord extends BaseMongoEntity {}
@ChildEntity()
Lets multiple entity classes share a single collection/table, discriminated by type. Useful for modeling an inheritance hierarchy without a separate collection per subclass:
@Entity()
@DataStore('mongo')
export class User extends BaseMongoEntity {
@Column() name: string = '';
}
@ChildEntity()
export class Player extends User {
@Reference(Item)
items?: string[];
}
@Reference(EntityClass) marks a property as referencing another entity type by identifier, rather than embedding it directly.
@Shard(config?)
MongoDB-only: configures sharding for a collection (defaults to sharding on uid):
@Shard()
@DataStore('mongo')
export class Event extends BaseMongoEntity {}