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

Constructors

  • Creates an instance of Editor.

    Parameters

    • Optional db: Knex<any, any[]> = null

      Database connection object

    • Optional table: string | string[] = null

      The table name in the database to read and write information from and to. Can be given here or with the 'table' method.

    • Optional pkey: string | string[] = null

      Primary key column name in the table given in

    Returns Editor

Properties

Action: typeof Action = Action
version: string = '2.3.2'

Methods

  • Get the data constructed in this instance.

    Returns

    Data object

    Returns IDtResponse

  • Get the database connection assigned to the instance.

    Returns

    Knex db interface

    Returns Knex<any, any[]>

  • Set the database connection.

    Returns

    Self for chaining

    Parameters

    • db: Knex<any, any[]>

    Returns Editor

  • Get the current transaction

    Returns Knex<any, any[]>

  • Get the debug setting for this instance

    Returns

    Debug enabled (true) or not

    Returns boolean

  • Set the debug setting for this instance

    Returns

    Self for chaining

    Parameters

    • set: boolean

      Debug flag

    Returns Editor

  • Add a debug message

    Returns

    Self for chaining

    Parameters

    • message: any

      Message to add

    Returns 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().

    Returns

    Self for chaining

    Parameters

    • doValidate: boolean

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

    Returns Editor

  • Add a new field to the Editor instance

    Returns

    Editor instance

    Parameters

    • field: Field

      Field instance to add=

    Returns Editor

  • Get a field

    Returns

    Field instance

    Parameters

    • name: string

      Field name to get

    Returns Field

  • Get the fields assigned to this instance.

    Returns

    Array of fields

    Returns Field[]

  • Add one or more fields to the instance.

    Returns

    Self for chaining

    Parameters

    • Rest ...fields: Field[]

      Fields to add

    Returns 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

    id prefix

    Returns string

  • Get the id prefix.

    Returns

    Self for chaining

    Parameters

    • idPrefix: string

      Prefix to use.

    Returns Editor

  • 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

    Data that has been passed into ()

    Returns IDtRequest

  • 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 table and fields methods refer to in this class instance).

    Returns

    Array of Mjoin instances

    Returns Mjoin[]

  • Add one or more Mjoin instances.

    Returns

    Self for chaining.

    Parameters

    • Rest ...join: Mjoin[]

      Mjoin instance to add.

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

    Returns

    Self for chaining

    Parameters

    • table: string

      Table name to do a join onto

    • condition: Function

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

    Returns

    Self for chaining

    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

  • Get the left join remove value.

    Returns

    Value

    Returns boolean

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

    Returns

    Self for chaining

    Parameters

    • remove: boolean

      Value

    Returns Editor

  • Add an event listener. The Editor class will trigger an number of events that some action can be taken on.

    Returns

    Self for chaining.

    Parameters

    • name: string

      Event name

    • callback: Function

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

    Returns 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

    Array of column names

    Returns string[]

  • Set the primary key value(s)

    Returns

    Self for chaining.

    Parameters

    • Optional pkey: string | string[]

      Primary key column name. Use an array of strings if using a compound key.

    Returns Editor

  • Convert a primary key combined value to an array of field values.

    Returns

    Array of field values that the id was made up of

    Parameters

    • value: string

      The id that should be split apart

    • Optional flat: boolean = false

      Flag to indicate if the returned array should be flat (useful for where conditions) or nested for join tables.

    • Optional pkey: string[] = null

      The primary key name - will use the instance value if not given

    Returns object

  • Convert a primary key array of field values to a combined value.

    Returns

    The created primary key value.

    Parameters

    • row: object

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

    • Optional flat: boolean = false

      Flag to indicate if the given array is flat (useful for where conditions) or nested for join tables.

    Returns string

  • Process a request from the Editor client-side to get / set data.

    Returns

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

    Parameters

    • data: IDtRequest

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

    • Optional files: IUpload = null

      File information, used for upload requests - e.g. req.files

    Returns Promise<Editor>

  • Get CRUD read table name.

    Returns

    Configured read table name

    Returns string[]

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

    Returns

    Self for chaining

    Parameters

    • table: string | string[]

      Database table name to use for reading from

    Returns 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

    Schema

    Returns string

  • Set the database schema

    Returns

    Self for chaining

    Parameters

    • schema: string

      Schema to use.

    Returns 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

    Configured table name

    Returns string[]

  • Set the table name.

    Returns

    Self for chaining

    Parameters

    • table: string | string[]

      Database table name to use

    Returns 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

    true is transactions are enabled, false otherwise.

    Returns boolean

  • Set transaction support state

    Returns

    Self for chaining

    Parameters

    • transaction: boolean

      Set the transaction status

    Returns Editor

  • Get the try/catch status.

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

    Returns

    Try / catch status.

    Returns boolean

  • Set the try/catch state.

    Returns

    Self for chaining.

    Parameters

    • tryCatch: boolean

      Value to set. true will enable, false disable.

    Returns Editor

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

    Returns

    true if the data is valid, false if not.

    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>

  • Get any global validator that has been set.

    Returns

    Global validator

    Returns IGlobalValidator[]

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

    Returns

    Self for chaining

    Parameters

    • fn: IGlobalValidator

      Function to execute when validating the input data.

    Returns Editor

  • Get the array of conditions applied to the method.

    Returns

    Knex where conditions.

    Returns any[]

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

    Returns

    Self for chaining.

    Parameters

    • Rest ...cond: any[]

      Knex query condition

    Returns Editor

  • Clear out the where conditions already applied to this instance

    Returns

    Self for chaining.

    Returns 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

  • Determine the request type from an HTTP request.

    Static

    Returns

    Indicates what action the request is

    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

Generated using TypeDoc