Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SingleReference<OutputType, FormatParams>

A reference to other item, in the same or other collection. Can point at items from only one, specified collection. Items with field of this type can be filtered by fields of the items it points at. Examples below.

Params

  • target_collection - ()=>Collection - the collection that this field points at
  • filter - any - (optional) if provided, only items matching this filter can be referenced

Filtering

Items from a collection with this type of field can be filtered by fields of the items they point at. For example, let's assume we have a collection of People:

```
Collection.fromDefinition(
  app,
  {
   name: "people",
   fields: [
      field("name", FieldTypes.Text),
      field("age", FieldTypes.Int),
      field("best_friend", FieldTypes.SingleReference, {target_collection: ()=>app.collections.people})
   ]
);
```

Then we can perform a search for all people who have a best_friend named "John":

{filter: {best_friend: {name: "John"}}}

Type parameters

  • OutputType

  • FormatParams

Hierarchy

  • Field<string>
    • SingleReference

Index

Constructors

constructor

Properties

collection

collection: Collection

The collection this field is attached to

display_hints

display_hints: any

The display hints specified for this field

filter

filter: any

get_target_collection

get_target_collection: () => Collection

Type declaration

handles_large_data

handles_large_data: boolean = false

Whether or not the field handles large data

todo:

see if there's any viability in storing this

name

name: string

the name of the field

required

required: boolean

Whether or not this field should always have a value. Creating a resource with a value missing for a required field will throw an error

Methods

decode

  • Reverse to the Field.encode function. Takes what's inside the database and returns the value in a given format

    Parameters

    Returns Promise<OutputType | null>

encode

  • Decides how to store the given value in the database, based on the context and previous value of the field

    Parameters

    Returns Promise<any>

filterToQuery

  • filterToQuery(context: Context, filter: any): Promise<{ $eq: any } | { $in: string[] }>
  • Parameters

    Returns Promise<{ $eq: any } | { $in: string[] }>

fullTextSearchEnabled

  • fullTextSearchEnabled(): Promise<boolean>
  • Whether or not the db should create a fulltext index on this field

    Returns Promise<boolean>

getAggregationStages

  • getAggregationStages(context: Context, query: Parameters<Field["getAggregationStages"]>[1]): Promise<({ $lookup: object } | { $match: object })[] | { $match: object }[] | { $match: object }[]>
  • Parameters

    • context: Context
    • query: Parameters<Field["getAggregationStages"]>[1]

    Returns Promise<({ $lookup: object } | { $match: object })[] | { $match: object }[] | { $match: object }[]>

getAttachmentLoader

getDefaultValue

getSpecification

  • getSpecification(): { display_hints: any; name: string; type: string }
  • Return a summary of this field

    Returns { display_hints: any; name: string; type: string }

    • display_hints: any
    • name: string
    • type: string

getTypeName

  • getTypeName(): string
  • Returns string

getValuePath

  • getValuePath(): Promise<string>
  • Value path is where inside a single record should the DB look for the field's value when filtering resources. Some fields use complex objects for storage and overwrite this method, and thanks to that they don't have to reimplement Field.getAggregationStages

    Returns Promise<string>

hasDefaultValue

  • hasDefaultValue(): boolean
  • Whether or not a field has a default value - that is, a value given to the field if no value is provided

    Returns boolean

hasIndex

  • hasIndex(): boolean
  • Returns boolean

init

  • init(_: App): Promise<void>
  • Runs when the app is being started. Hooks can be set up within this function

    Parameters

    Returns Promise<void>

isOldValueSensitive

  • Whether or not any of the methods of the field depend on the previous value of the field

    Parameters

    Returns boolean

isProperValue

  • isProperValue(context: Context, input: string): Promise<{ reason?: undefined | string; valid: boolean }>
  • Parameters

    Returns Promise<{ reason?: undefined | string; valid: boolean }>

setParams

  • setParams(params: { filter?: any; target_collection: () => Collection }): void
  • Parameters

    Returns void

Static fromDefinition

  • fromDefinition<T>(app: App, collection: Collection, definition: { display_hints?: any; name: string; params?: Parameters<InstanceType<FieldClass>["setParams"]>[0]; required?: undefined | false | true; type: T }): Field<any, any, any>
  • Create a new instance of Field based on the definition. **It's recommended to use FieldDefinitionHelper instead

    Type parameters

    Parameters

    • app: App
    • collection: Collection
    • definition: { display_hints?: any; name: string; params?: Parameters<InstanceType<FieldClass>["setParams"]>[0]; required?: undefined | false | true; type: T }
      • Optional display_hints?: any
      • name: string
      • Optional params?: Parameters<InstanceType<FieldClass>["setParams"]>[0]
      • Optional required?: undefined | false | true
      • type: T

    Returns Field<any, any, any>

Static invalid

Static valid

Generated using TypeDoc