Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Editor

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
class

Editor

extends

{NestedData}

Hierarchy

Index

Constructors

constructor

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

    Parameters

    • Default value db: knex = null
    • Default value table: string | string[] = null
    • Default value pkey: string | string[] = null

    Returns Editor

Properties

Static Action

Action: Action = Action

Static version

version: string = "1.8.1"

Methods

data

db

  • db(): knex
  • db(db: knex): Editor
  • Get the database connection assigned to the instance.

    Returns knex

    Knex db interface

  • Set the database connection.

    Parameters

    • db: knex

    Returns Editor

    Self for chaining

debug

  • 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

field

  • field(nameOrField: Field | string): this | Field
  • Get or field by name, or add a field instance.

    Parameters

    • nameOrField: Field | string

      Field instance to add, or field name to get

    Returns this | Field

    Editor instance returned if adding a field, Field instance returned if getting a field.

fields

  • 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

  • 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

  • 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()}

join

  • 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

  • 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.

    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

  • 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

  • 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

  • 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

  • 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

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

    Returns object

    Array of field values that the id was made up of

pkeyToValue

  • 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.

    • Default value flat: boolean = false

    Returns string

    The created primary key value.

process

  • 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

    • Default value files: IUpload = null

    Returns Promise<Editor>

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

readTable

  • 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

table

  • 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

  • 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

  • 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

  • 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

  • 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

    Returns Editor

    Self for chaining

where

  • 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.

Static 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