Options
All
  • Public
  • Public/Protected
  • All
Menu

DataTables Editor base class for creating editable tables.

Editor class instances are capable of servicing all of the requests that DataTables and Editor will make from the client-side - specifically:

  • Get data
  • Create new record
  • Edit existing record
  • Delete existing records

The Editor instance is configured with information regarding the database table fields that you which to make editable, and other information needed to read and write to the database (table name for example!).

This documentation is very much focused on describing the API presented by these DataTables Editor classes. For a more general overview of how the Editor class is used, and how to install Editor on your server, please refer to the Editor manual.

export

Hierarchy

  • default
    • Editor

Index

Constructors

  • new Editor(db?: Knex<any, any[]>, table?: string | string[], pkey?: string | string[]): Editor
  • Creates an instance of Editor.

    Parameters

    • db: Knex<any, any[]> = null
    • table: string | string[] = null
    • pkey: string | string[] = null

    Returns Editor

Properties

Action: typeof Action = Action
version: string = '2.0.10'

Methods

  • data(): IDtResponse
  • Get the data constructed in this instance.

    Returns IDtResponse

    Data object

  • db(): Knex<any, any[]>
  • db(db: Knex<any, any[]>): Editor
  • Get the database connection assigned to the instance.

    Returns Knex<any, any[]>

    Knex db interface

  • Set the database connection.

    Parameters

    • db: Knex<any, any[]>

    Returns Editor

    Self for chaining

  • dbTransaction(): Knex<any, any[]>
  • Get the current transaction

    Returns Knex<any, any[]>

  • debug(): boolean
  • debug(set: boolean): Editor
  • debug(message: any): Editor
  • Get the debug setting for this instance

    Returns boolean

    Debug enabled (true) or not

  • Set the debug setting for this instance

    Parameters

    • set: boolean

      Debug flag

    Returns Editor

    Self for chaining

  • Add a debug message

    Parameters

    • message: any

      Message to add

    Returns Editor

    Self for chaining

  • doValidate(): boolean
  • doValidate(doValidate: boolean): Editor
  • Get the validate flag

    Returns boolean

  • Enable / disable validation. This would be used with after the validate method if you call that before process().

    Parameters

    • doValidate: boolean

      true (default) = perform validation, false don't.

    Returns Editor

    Self for chaining

  • Add a new field to the Editor instance

    Parameters

    • field: Field

      Field instance to add=

    Returns Editor

    Editor instance

  • Get a field

    Parameters

    • name: string

      Field name to get

    Returns Field

    Field instance

  • Get the fields assigned to this instance.

    Returns Field[]

    Array of fields

  • Add one or more fields to the instance.

    Parameters

    • Rest ...fields: Field[]

      Fields to add

    Returns Editor

    Self for chaining

  • idPrefix(): string
  • idPrefix(idPrefix: string): Editor
  • Get the id prefix.

    Typically primary keys are numeric and this is not a valid ID value in an HTML document - is also increases the likelihood of an ID clash if multiple tables are used on a single page. As such, a prefix is assigned to the primary key value for each row, and this is used as the DOM ID, so Editor can track individual rows.

    Returns string

    id prefix

  • Get the id prefix.

    Parameters

    • idPrefix: string

      Prefix to use.

    Returns Editor

    Self for chaining

  • inData(): IDtRequest
  • Get the data that is being processed by the Editor instance. This is only useful once the process() method has been called, and is available for use in validation and formatter methods.

    Returns IDtRequest

    Data that has been passed into {@link Editor.process()}

  • Get the configured Mjoin instances.

    Note that for the majority of use cases you will want to use the leftJoin() method. It is significantly easier to use if you are just doing a simple left join!

    The list of Join instances that Editor will join the parent table to (i.e. the one that the Editor.table and Editor.fields methods refer to in this class instance).

    Returns Mjoin[]

    Array of Mjoin instances

  • Add one or more Mjoin instances.

    Parameters

    • Rest ...join: Mjoin[]

      Mjoin instance to add.

    Returns Editor

    Self for chaining.

  • leftJoin(table: string, condition: Function): Editor
  • leftJoin(table: string, field1: string, operator: string, field2: string): Editor
  • Add a left join condition to the Editor instance, allowing it to operate over multiple tables. Multiple leftJoin() calls can be made for a single Editor instance to join multiple tables.

    In this form the method will take a function as the second parameter which is a Knex callback function allowing a complex join expression to be built.

    Parameters

    • table: string

      Table name to do a join onto

    • condition: Function

    Returns Editor

    Self for chaining

  • Add a left join condition to the Editor instance, allowing it to operate over multiple tables. Multiple leftJoin() calls can be made for a single Editor instance to join multiple tables.

    A left join is the most common type of join that is used with Editor so this method is provided to make its use very easy to configure. Its parameters are basically the same as writing an SQL left join statement, but in this case Editor will handle the create, update and remove requirements of the join for you:

    • Create - On create Editor will insert the data into the primary table and then into the joined tables - selecting the required data for each table.
    • Edit - On edit Editor will update the main table, and then either update the existing rows in the joined table that match the join and edit conditions, or insert a new row into the joined table if required.
    • Remove - On delete Editor will remove the main row and then loop over each of the joined tables and remove the joined data matching the join link from the main table.

    Please note that when using join tables, Editor requires that you fully qualify each field with the field's table name. SQL can result table names for ambiguous field names, but for Editor to provide its full CRUD options, the table name must also be given. For example the field first_name in the table users would be given as users.first_name.

    Parameters

    • table: string

      Table name to do a join onto

    • field1: string

      Field from the parent table to use as the join link

    • operator: string

      Join condition (=, '<`, etc)

    • field2: string

      Field from the child table to use as the join link

    Returns Editor

    Self for chaining

  • leftJoinRemove(): boolean
  • leftJoinRemove(remove: boolean): Editor
  • Get the left join remove value.

    Returns boolean

    Value

  • Indicate if a remove should be performed on left joined tables when deleting from the parent row. Note that this is disabled by default and will be removed completely in v2. Use ON DELETE CASCADE in your database instead.

    Parameters

    • remove: boolean

      Value

    Returns Editor

    Self for chaining

  • on(name: string, callback: Function): Editor
  • Add an event listener. The Editor class will trigger an number of events that some action can be taken on.

    Parameters

    • name: string

      Event name

    • callback: Function

      Event callback function that will be executed when the event occurs.

    Returns Editor

    Self for chaining.

  • pkey(): string[]
  • pkey(pkey: string | string[]): Editor
  • Get the primary key value.

    The primary key must be known to Editor so it will know which rows are being edited / deleted upon those actions. The default value is ['id'].

    Returns string[]

    Array of column names

  • Set the primary key value(s)

    Parameters

    • pkey: string | string[]

    Returns Editor

    Self for chaining.

  • pkeyToObject(value: string, flat?: boolean, pkey?: string[]): object
  • Convert a primary key combined value to an array of field values.

    Parameters

    • value: string

      The id that should be split apart

    • flat: boolean = false
    • pkey: string[] = null

    Returns object

    Array of field values that the id was made up of

  • pkeyToValue(row: object, flat?: boolean): string
  • Convert a primary key array of field values to a combined value.

    Parameters

    • row: object

      The row of data that the primary key value should be extracted from.

    • flat: boolean = false

    Returns string

    The created primary key value.

  • process(data: IDtRequest, files?: IUpload): Promise<Editor>
  • Process a request from the Editor client-side to get / set data.

    Parameters

    • data: IDtRequest

      Form data sent from the client-side - e.g. req.body

    • files: IUpload = null

    Returns Promise<Editor>

    Promise that is fulfilled when Editor has completed its processing - result is the Editor instance.

  • readTable(): string[]
  • readTable(table: string | string[]): Editor
  • Get CRUD read table name.

    Returns string[]

    Configured read table name

  • Set CRUD read table name. If this method is used, Editor will create from the table name(s) given rather than those given by Editor->table(). This can be a useful distinction to allow a read from a VIEW (which could make use of a complex SELECT) while writing to a different table.

    Parameters

    • table: string | string[]

      Database table name to use for reading from

    Returns Editor

    Self for chaining

  • schema(): string
  • schema(schema: string): Editor
  • Get the database schema.

    This is used if you are using multiple schema's in your database. By default Editor will not specify a schema, so the default search path will be used. This allows that to be overridden.

    Returns string

    Schema

  • Set the database schema

    Parameters

    • schema: string

      Schema to use.

    Returns Editor

    Self for chaining

  • table(): string[]
  • table(table: string | string[]): Editor
  • Get the table name.

    The table name designated which DB table Editor will use as its data source for working with the database. Table names can be given with an alias, which can be used to simplify larger table names. The field names would also need to reflect the alias, just like an SQL query. For example: users as a.

    Returns string[]

    Configured table name

  • Set the table name.

    Parameters

    • table: string | string[]

      Database table name to use

    Returns Editor

    Self for chaining

  • transaction(): boolean
  • transaction(transaction: boolean): Editor
  • Get transaction support status

    When enabled (which it is by default) Editor will use an SQL transaction to ensure data integrity while it is performing operations on the table. This can be optionally disabled using this method, if required by your database configuration.

    Returns boolean

    true is transactions are enabled, false otherwise.

  • Set transaction support state

    Parameters

    • transaction: boolean

      Set the transaction status

    Returns Editor

    Self for chaining

  • tryCatch(): boolean
  • tryCatch(tryCatch: boolean): Editor
  • Get the try/catch status.

    Editor uses a try/catch in the Editor.process method, and it can be useful to disable this for debugging, but its not recommended you do that in production.

    Returns boolean

    Try / catch status.

  • Set the try/catch state.

    Parameters

    • tryCatch: boolean

      Value to set. true will enable, false disable.

    Returns Editor

    Self for chaining.

  • validate(errors: IDtError[], http: IDtRequest): Promise<boolean>
  • Perform validation on a data set.

    Note that validation is performed on data only when the action is create or edit. Additionally, validation is performed on the wire data - i.e. that which is submitted from the client, without formatting. Any formatting required by setFormatter is performed after the data from the client has been validated.

    Parameters

    • errors: IDtError[]

      Output array to which field error information will be written. Each element in the array represents a field in an error condition. These elements are themselves arrays with two properties set; name and status.

    • http: IDtRequest

      The format data to check

    Returns Promise<boolean>

    true if the data is valid, false if not.

  • validator(): IGlobalValidator[]
  • validator(fn: IGlobalValidator): Editor
  • Get any global validator that has been set.

    Returns IGlobalValidator[]

    Global validator

  • Set a global validator. This will be triggered for the create, edit and remove actions performed from the client-side.

    Parameters

    • fn: IGlobalValidator

      Function to execute when validating the input data.

    Returns Editor

    Self for chaining

  • where(): any[]
  • where(...cond: any[]): Editor
  • Get the array of conditions applied to the method.

    Returns any[]

    Knex where conditions.

  • Set a condition for the queries Editor will perform. Editor uses Knex to connect to the database, and exposes the knex object using this method so you can add any conditions you like that are supported by Knex.

    Parameters

    • Rest ...cond: any[]

      Knex query condition

    Returns Editor

    Self for chaining.

  • Clear out the where conditions already applied to this instance

    Returns Editor

    Self for chaining.

  • write(writeVal: any): boolean | Editor
  • Getter/Setter for this._write which is used to decide which actions to allow

    Parameters

    • writeVal: any

      Value for this._write

    Returns boolean | Editor

  • action(http: IDtRequest): Action
  • Determine the request type from an HTTP request.

    static

    Parameters

    • http: IDtRequest

      HTTP request - normally request.body. Note that if you are using body-parser you should use { extended: true } as its options to ensure that nested properties are correctly resolved.

    Returns Action

    Indicates what action the request is

Generated using TypeDoc