Skip to main content

Entities & Columns

@Entity(options?)

Marks a class as a persistable entity.

@Entity({collation: {locale: 'en', strength: 2}})
export class Pet extends BaseMongoEntity {
@Column()
name: string = '';
}

options.name overrides the collection/table name (defaults to the snake_cased class name); options.collation sets a MongoDB collation.

@DataStore(name)

Indicates which datastore that entities of this class will be stored in. Note that the name argument must correspond to a configured entry of the same name in the datastores list in the server configuration (covered in the next section: Datastores & Connections).

@Entity({collation: {locale: 'en', strength: 2}})
@DataStore('mongo')
export class Pet extends BaseMongoEntity {
@Column()
name: string = '';
}

@Column(options?)

Marks a property as a persisted column/field.

export interface ColumnOptions {
name?: string; // override the stored field name
nullable?: boolean;
primary?: boolean;
isObjectId?: boolean; // MongoDB ObjectId fields, e.g. `_id`
}
@Column({isObjectId: true})
@Nullable
_id?: any;

@PrimaryColumn(options?)

Shorthand for @Column({...options, primary: true}), used for identifier fields:

@Identifier
@Index('uid', {unique: true})
@PrimaryColumn()
uid: string = uuid.v4();

@Index and @Unique

@Index has three forms depending on whether you're indexing a single property or a class-level compound index:

// Property-level
@Index() // unnamed single-field index
@Index('myIndexName') // named
@Index({unique: true}) // unnamed, unique

// Class-level compound index
@Index(['firstName', 'lastName'])
@Index('fullName', ['firstName', 'lastName'], {unique: true})

IndexOptions also accepts sparse, background, and expireAfterSeconds (MongoDB-specific, e.g. for TTL indexes).

@Unique is shorthand for an index with {unique: true}, in the same property-level or class-level forms:

@Unique('email')

@RequiresScope

RequiresScope(scope: string | string[]) marks a property as readable only by clients whose token carries at least one of the given OAuth-style scopes. When the requesting user doesn't have a matching scope, the property is silently removed from the object rather than the request being rejected.

@Column()
@RequiresScope('profile:email')
contacts: Contact[] = [];

This is enforced automatically inside RepoUtils.find()/findOne() via ObjectUtils.deleteScopedProps — no extra wiring needed in your route. It's how @rapidrest/auth's Profile model protects contacts and preferences behind profile:email and profile:preferences scopes.

This is unrelated to the route-level @RequiresScope documented in Authorization, which gates an entire endpoint rather than a single field.

A complete example

import {BaseMongoEntity, PersistenceDecorators, ModelDecorators} from '@rapidrest/service-core';
const {Entity, Column, Index} = PersistenceDecorators;
const {DataStore, Identifier} = ModelDecorators;

@Entity()
@DataStore('mongo')
export class Pet extends BaseMongoEntity {
@Identifier
@Index()
@Column()
name: string = '';

@Column()
species: string = '';
}