# Class Table

A class representing database tables and their properties.

Use this to create or delete tables and their properties, or to query or change table rows.

To query, update, insert or delete rows in an existing table, first you should find the table object with Table.findOne.

#### Example

    Table.findOne({name: "Customers"}) // find the table with name "Customers"
    Table.findOne("Customers") // find the table with name "Customers" (shortcut)
    Table.findOne({ id: 5 }) // find the table with id=5
    Table.findOne(5) // find the table with id=5 (shortcut)

Table.findOne is synchronous (no need to await), But the functions that query and manipulate (such as Table.insertRow, Table.getRows, Table.updateRow, Table.deleteRows) rows are mostly asyncronous, so you can put the await in front of the whole expression

#### Example

To count the number of rows in the customer table

    const nrows = await Table.findOne("Customers").countRows()

For further examples, see the Table test suite

## Querying table rows

There are several methods you can use to retrieve rows in the database:

- Table.countRows To count the number of rows, optionally matching a criterion
- Table.getRows To retrieve multiple rows matching a criterion
- Table.getRow To retrieve a single row matching a criterion
- Table.getJoinedRows To retrieve rows together with joinfields and aggregations

These functions all take `Where` expressions which are JavaScript objects describing the criterion to match to. Some examples:

- `{}`: Match all rows
- `{ name: "Jim" }`: Match all rows with name="Jim"
- `{ name: { ilike: "im"} }`: Match all rows where name contains "im" (case insensitive)
- `{ name: /im/ }`: Match all rows with name matching regular expression "im"
- `{ age: { lt: 18 } }`: Match all rows with age<18
- `{ age: { lt: 18, equal: true } }`: Match all rows with age<=18
- `{ age: { gt: 18, lt: 65} }`: Match all rows with 18<age<65
- `{ name: { or: ["Harry", "Sally"] } }`: Match all rows with name="Harry" or "Sally"
- `{ or: [{ name: "Joe"}, { age: 37 }] }`: Match all rows with name="Joe" or age=37
- `{ not: { id: 5 } }`: All rows except id=5
- `{ id: { in: [1, 2, 3] } }`: Rows with id 1, 2, or 3
- `{ id: { not: { in: [1, 2, 3] } } }`: Rows with id any value except 1, 2, or 3

For further examples, see the mkWhere test suite

## Updating a Row

There are two nearly identical functions for updating rows depending on how you want failures treated

- Table.updateRow Update a row, throws an exception if update is invalid
- Table.tryUpdateRow Update a row, return an error message if update is invalid

## Inserting a new Row

There are two nearly identical functions for inserting a new row depending on how you want failures treated

- Table.insertRow insert a row, throws an exception if it is invalid
- Table.tryInsertRow insert a row, return an error message if it is invalid

## Deleting rows

Use Table.deleteRows to delete any number (zero, one or many) of rows matching a criterion. It uses the same `where` expression as the functions for querying rows

#### Implements

- AbstractTable

- Defined in packages/saltcorn-data/models/table.ts:228
- Defined in packages/saltcorn-data/models/table.ts:4478

##### Index

### Constructors

constructor

### Properties

constraints
description?
external
fields
has_sync_info
id?
is_user_group
min_role_read
min_role_write
name
ownership_field_id?
ownership_formula?
provider_cfg?
provider_name?
versioned

### Accessors

composite_pk_names
pk_name
pk_type
santized_name
sql_name
to_json

### Methods

aggregationQuery
check_table_constraints
compress_history
countRows
delete
delete_url
deleteRows
distinctValues
dump_history_to_json
dump_to_json
enable_fkey_constraints
field_options
get_child_relations
get_history
get_join_field_options
get_models
get_parent_relations
get_relation_data
get_relation_options
getField
getFields
getForeignKeys
getForeignTables
getFormulaExamples
getJoinedQuery
getJoinedRow
getJoinedRows
getRow
getRows
getTags
import_csv_file
import_json_file
import_json_history_file
insert_history_row
insertRow
is_owner
latestSyncInfo
latestSyncInfos
move_include_fts_to_search_context
normalise_error_message
owner_fieldname
owner_fieldname_from_fields
ownership_formula_where
ownership_options
processSelectOptions
read_state_strict
redo_row_changes
rename
repairCompositePrimary
resetSequence
restore_row_version
slug_options
to_provided_table
toggleBool
tryInsertRow
tryUpdateRow
undo_row_changes
update
updateRow
updateWhereWithOwnership
allSlugOptions
analyze_all_indexed_tables
create
create_from_csv
createInDb
find
find_with_external
findOne
state_refresh

## Constructors

### constructor

- new Table(o: TableCfg): Table

  Table constructor

  #### Parameters

  - o: TableCfg

  #### Returns Table

  - Defined in packages/saltcorn-data/models/table.ts:278

## Properties

### constraints

constraints: TableConstraint[]

An array of TableConstraints for this table

- Defined in packages/saltcorn-data/models/table.ts:264

### `Optional` description

description?: string

A description of the purpose of the table

- Defined in packages/saltcorn-data/models/table.ts:258

### external

external: boolean

If true this is an external table (not a database table)

- Defined in packages/saltcorn-data/models/table.ts:255

### fields

fields: Field[]

An array of Fields in this table

Implementation of AbstractTable.fields

- Defined in packages/saltcorn-data/models/table.ts:261

### has_sync_info

has_sync_info: boolean

Whether sync info for mobile apps is enabled for this table

- Defined in packages/saltcorn-data/models/table.ts:252

### `Optional` id

id?: number

The table ID

Implementation of AbstractTable.id

- Defined in packages/saltcorn-data/models/table.ts:233

### is_user_group

is_user_group: boolean

Is this a user group? If yes it will appear as options in the ownership dropdown

- Defined in packages/saltcorn-data/models/table.ts:267

### min_role_read

min_role_read: number

Minimum role to read

Implementation of AbstractTable.min_role_read

- Defined in packages/saltcorn-data/models/table.ts:236

### min_role_write

min_role_write: number

Minimum role to write

Implementation of AbstractTable.min_role_write

- Defined in packages/saltcorn-data/models/table.ts:239

### name

name: string

The table name

Implementation of AbstractTable.name

- Defined in packages/saltcorn-data/models/table.ts:230

### `Optional` ownership_field_id

ownership_field_id?: number | null

The ID of the ownership field

Implementation of AbstractTable.ownership_field_id

- Defined in packages/saltcorn-data/models/table.ts:242

### `Optional` ownership_formula

ownership_formula?: string

A formula to denote ownership. This is a JavaScript expression which must evaluate to true if the user is the owner

Implementation of AbstractTable.ownership_formula

- Defined in packages/saltcorn-data/models/table.ts:246

### `Optional` provider_cfg

provider_cfg?: Row

Configuration for the table provider for this table

- Defined in packages/saltcorn-data/models/table.ts:273

### `Optional` provider_name

provider_name?: string

Name of the table provider for this table (not a database table)

- Defined in packages/saltcorn-data/models/table.ts:270

### versioned

versioned: boolean

Version history enabled for this table

- Defined in packages/saltcorn-data/models/table.ts:249

## Accessors

### composite_pk_names

- get composite_pk_names(): string[] | null

  #### Returns string[] | null

  - Defined in packages/saltcorn-data/models/table.ts:1968

### pk_name

- get pk_name(): string

  Get primary key field name

  #### Returns string

  - Defined in packages/saltcorn-data/models/table.ts:1978

### pk_type

- get pk_type(): Type

  #### Returns Type

  - Defined in packages/saltcorn-data/models/table.ts:1986

### santized_name

- get santized_name(): string

  get sanitized name of table

  #### Returns string

  - Defined in packages/saltcorn-data/models/table.ts:670

### sql_name

- get sql_name(): string

  Get Table SQL Name

  #### Returns string

  Implementation of AbstractTable.sql_name

  - Defined in packages/saltcorn-data/models/table.ts:865

### to_json

- get to_json(): {
  external: boolean;
  fields: any[];
  id: number
  | undefined;
  min_role_read: number;
  min_role_write: number;
  name: string;
  ownership_field_id: number | null | undefined;
  ownership_formula: string | undefined;
  provider_cfg: Row | undefined;
  provider_name: string | undefined;
  versioned: boolean;
  }

  #### Returns {
  external: boolean;
  fields: any[];
  id: number | undefined;
  min_role_read: number;
  min_role_write: number;
  name: string;
  ownership_field_id: number | null | undefined;
  ownership_formula: string | undefined;
  provider_cfg: Row | undefined;
  provider_name: string | undefined;
  versioned: boolean;
  }

  - Defined in packages/saltcorn-data/models/table.ts:297

## Methods

### aggregationQuery

- aggregationQuery(
  aggregations: {
  [nm: string]: {
  aggregate: string;
  field?: string;
  valueFormula?: string;
  };
  },
  options?: { groupBy?: string | string[]; where?: Where } & ForUserRequest,
  ): Promise<Row>

  Returns aggregations for this table, possibly on a subset by where-expression

  #### Parameters

  - aggregations: { [nm: string]: { aggregate: string; field?: string; valueFormula?: string } }
  - `Optional` options: { groupBy?: string | string[]; where?: Where } & ForUserRequest

  #### Returns Promise<Row>

  - Defined in packages/saltcorn-data/models/table.ts:3893

### check_table_constraints

- check_table_constraints(row0: Row): string | undefined

  Check table constraints against a row object. Will return a string With an error message if the table constraints are violated, `undefined` if the row does not violate any constraints

  #### Parameters

  - row0: Row

  #### Returns string | undefined

  - Defined in packages/saltcorn-data/models/table.ts:2006

### compress_history

- compress_history(
  options: number | { delete_unchanged?: boolean; interval_secs?: number },
  ): Promise<void>

  Compress history by minimal interval and/or deleting unchanged rows. Can be called with options object, or just minimal interval for legacy code

  #### Parameters

  - options: number | { delete_unchanged?: boolean; interval_secs?: number }

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2803

### countRows

- countRows(where?: Where, opts?: SelectOptions & ForUserRequest): Promise<number>

  Count the number of rows in db table. The argument is a where-expression with conditions the counted rows should match. countRows returns the number of matching rows wrapped in a promise.

  #### Parameters

  - `Optional` where: Where
  - `Optional` opts: SelectOptions & ForUserRequest

  #### Returns Promise<number>

  #### Example

      const bookTable = Table.findOne({name: "books"})

      // Count the total number of rows in the books table
      const totalNumberOfBooks = await bookTable.countRows({})

      // Count the number of books where the cover_color field has the value is "Red"
      const numberOfRedBooks = await bookTable.countRows({cover_color: "Red"})

      // Count number of books with more than 500 pages
      const numberOfLongBooks = await bookTable.countRows({pages: {gt: 500}})

  - Defined in packages/saltcorn-data/models/table.ts:1325

### delete

- delete(only_forget?: boolean): Promise<void>

  Drop current table

  #### Parameters

  - only_forget: boolean = false

    boolean - if true that only

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:834

### delete_url

- delete_url(row: Row, moreQuery?: string): string

  #### Parameters

  - row: Row
  - `Optional` moreQuery: string

  #### Returns string

  - Defined in packages/saltcorn-data/models/table.ts:1960

### deleteRows

- deleteRows(
  where: Where,
  user?: AbstractUser,
  noTrigger?: boolean,
  resultCollector?: any,
  ): Promise<void>

  Delete rows from table. The first argument is a where expression indicating the conditions for the rows to be deleted

  #### Parameters

  - where: Where

    condition

  - `Optional` user: AbstractUser

    optional user, if null then no authorization will be checked

  - `Optional` noTrigger: boolean
  - `Optional` resultCollector: any

  #### Returns Promise<void>

  #### Example

      // delete all books where author = "Friedrich Nietzsche"
      await Table.findOne({name: "books"}).deleteRows({author: "Friedrich Nietzsche"})

  - Defined in packages/saltcorn-data/models/table.ts:994

### distinctValues

- distinctValues(fieldnm: string, whereObj?: Where): Promise<any[]>

  Return distinct Values for column in table ????

  #### Parameters

  - fieldnm: string
  - `Optional` whereObj: Where

  #### Returns Promise<any[]>

  - Defined in packages/saltcorn-data/models/table.ts:1339

### dump_history_to_json

- dump_history_to_json(filePath: string): Promise<void>

  #### Parameters

  - filePath: string

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:3138

### dump_to_json

- dump_to_json(filePath: string): Promise<void>

  #### Parameters

  - filePath: string

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:3130

### enable_fkey_constraints

- enable_fkey_constraints(): Promise<void>

  Enable constraints

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2956

### field_options

- field_options(
  nrecurse?: number,
  fieldWhere?: (f: Field) => boolean,
  prefix?: string,
  ): Promise<string[]>

  #### Parameters

  - nrecurse: number = 0
  - fieldWhere: (f: Field) => boolean = ...
  - prefix: string = ""

  #### Returns Promise<string[]>

  - Defined in packages/saltcorn-data/models/table.ts:3821

### get_child_relations

- get_child_relations(allow_join_aggregations?: boolean): Promise<ChildRelations>

  Get child relations for table

  #### Parameters

  - `Optional` allow_join_aggregations: boolean

  #### Returns Promise<ChildRelations>

  - Defined in packages/saltcorn-data/models/table.ts:3851

### get_history

- get_history(id?: PrimaryKeyValue): Promise<Row[]>

  Get table history data

  #### Parameters

  - `Optional` id: PrimaryKeyValue

  #### Returns Promise<Row[]>

  - Defined in packages/saltcorn-data/models/table.ts:2944

### get_join_field_options

- get_join_field_options(
  allow_double?: boolean,
  allow_triple?: boolean,
  ): Promise<JoinFieldOption[]>

  get join-field-options joined from a field in this table

  #### Parameters

  - `Optional` allow_double: boolean
  - `Optional` allow_triple: boolean

  #### Returns Promise<JoinFieldOption[]>

  - Defined in packages/saltcorn-data/models/table.ts:3624

### get_models

- get_models(where?: string | Where): Promise<any>

  Get Models tbd why this function in this file - needs to models

  #### Parameters

  - `Optional` where: string | Where

  #### Returns Promise<any>

  - Defined in packages/saltcorn-data/models/table.ts:482

### get_parent_relations

- get_parent_relations(
  allow_double?: boolean,
  allow_triple?: boolean,
  ): Promise<ParentRelations>

  Get parent relations for table

  #### Parameters

  - `Optional` allow_double: boolean
  - `Optional` allow_triple: boolean

  #### Returns Promise<ParentRelations>

  - Defined in packages/saltcorn-data/models/table.ts:3741

### get_relation_data

- get_relation_data(unique?: boolean): Promise<RelationData[]>

  get relation-data joined from a field of another table

  #### Parameters

  - unique: boolean = true

  #### Returns Promise<RelationData[]>

  - Defined in packages/saltcorn-data/models/table.ts:3719

### get_relation_options

- get_relation_options(): Promise<RelationOption[]>

  get relation-options joined from a field of another table

  #### Returns Promise<RelationOption[]>

  - Defined in packages/saltcorn-data/models/table.ts:3700

### getField

- getField(path: string): Field | undefined

  Get a field, possibly by relation

  #### Parameters

  - path: string

  #### Returns Field | undefined

  - Defined in packages/saltcorn-data/models/table.ts:2563

### getFields

- getFields(): Field[]

  Get Fields list for table

  #### Returns Field[]

  - Defined in packages/saltcorn-data/models/table.ts:2547

### getForeignKeys

- getForeignKeys(): Field[]

  get foreign keys, without the 'File' type

  #### Returns Field[]

  array of FK Fields

  - Defined in packages/saltcorn-data/models/table.ts:2555

### getForeignTables

- getForeignTables(): Promise<AbstractTable[]>

  #### Returns Promise<AbstractTable[]>

  Implementation of AbstractTable.getForeignTables

  - Defined in packages/saltcorn-data/models/table.ts:4344

### getFormulaExamples

- getFormulaExamples(typename: string): string[]

  #### Parameters

  - typename: string

  #### Returns string[]

  - Defined in packages/saltcorn-data/models/table.ts:4357

### getJoinedQuery

- getJoinedQuery(
  opts?: {
  aggregations?: { [nm: string]: AggregationOptions };
  joinFields?: JoinFields;
  starFields?: boolean;
  where?: Where;
  } & SelectOptions & ForUserRequest & { ignoreExternal?: boolean },
  ): Promise<
  Partial<JoinOptions> & {
  notAuthorized?: boolean;
  sql?: string;
  values?: Value[];
  },
  >

  #### Parameters

  - opts: {
    aggregations?: { [nm: string]: AggregationOptions };
    joinFields?: JoinFields;
    starFields?: boolean;
    where?: Where;
    } & SelectOptions & ForUserRequest & { ignoreExternal?: boolean } = {}

  #### Returns Promise<
  Partial<JoinOptions> & {
  notAuthorized?: boolean;
  sql?: string;
  values?: Value[];
  },
  >

  - Defined in packages/saltcorn-data/models/table.ts:3965

### getJoinedRow

- getJoinedRow(opts?: any): Promise<Row | null>

  #### Parameters

  - `Optional` opts: any = {}

  #### Returns Promise<Row | null>

  - Defined in packages/saltcorn-data/models/table.ts:4182

### getJoinedRows

- getJoinedRows(
  opts?: {
  aggregations?: { [nm: string]: AggregationOptions };
  joinFields?: JoinFields;
  starFields?: boolean;
  where?: Where;
  } & SelectOptions & ForUserRequest & { ignoreExternal?: boolean },
  ): Promise<Row[]>

  Get rows along with joined and aggregated fields. The argument to `getJoinedRows` is an object with several different possible fields, all of which are optional

  - `where`: A Where expression indicating the criterion to match
  - `joinFields`: An object with the joinfields to retrieve
  - `aggregations`: An object with the aggregations to retrieve
  - `orderBy`: A string with the name of the field to order by
  - `orderDesc`: If true, descending order
  - `limit`: A number with the maximum number of rows to retrieve
  - `offset`: The number of rows to skip in the result before returning rows

  #### Parameters

  - `Optional` opts: {
    aggregations?: { [nm: string]: AggregationOptions };
    joinFields?: JoinFields;
    starFields?: boolean;
    where?: Where;
    } & SelectOptions & ForUserRequest & { ignoreExternal?: boolean } = {}

  #### Returns Promise<Row[]>

  #### Example

      const patients = Table.findOne({ name: "patients" });
      const patients_rows = await patients.getJoinedRows({
           where: { age: { gt: 65 } },
           orderBy: "id",
           aggregations: {
             avg_temp: {
               table: "readings",
               ref: "patient_id",
               field: "temperature",
               aggregate: "avg",
            },
           },
           joinFields: {
             pages: { ref: "favbook", target: "pages" },
             author: { ref: "favbook", target: "author" },
           },
      });

  - Defined in packages/saltcorn-data/models/table.ts:4225

### getRow

- getRow(
  where?: Where,
  selopts?: SelectOptions & ForUserRequest,
  ): Promise<Row | null>

  Get one row from the table in the database. The matching row will be returned in a promise - use await to read the value. If no matching rule can be found, null will be returned. If more than one row matches, the first found row will be returned.

  The first argument to get row is a where-expression With the conditions the returned row should match.

  The second document is optional and is an object that can modify the search. This is mainly useful in case there is more than one matching row for the where-expression in the first argument and you want to give an explicit order. For example, use `{orderBy: "name"}` as the second argument to pick the first row by the name field, ordered ascending. `{orderBy: "name", orderDesc: true}` to order by name, descending

  This is however rare and usually getRow is run with a single argument of a Where expression that uniquely determines the row to return, if it exisits.

  #### Parameters

  - where: Where = {}
  - selopts: SelectOptions & ForUserRequest = {}

  #### Returns Promise<Row | null>

  #### Example

      const bookTable = Table.findOne({name: "books"})

      // get the row in the book table with id = 5
      const myBook = await bookTable.getRow({id: 5})

      // get the row for the last book published by Leo Tolstoy
      const myBook = await bookTable.getRow({author: "Leo Tolstoy"}, {orderBy: "published", orderDesc: true})

  - Defined in packages/saltcorn-data/models/table.ts:1191

### getRows

- getRows(where?: Where, selopts?: SelectOptions & ForUserRequest): Promise<Row[]>

  Get all matching rows from the table in the database.

  The arguments are the same as for getRow. The first argument is where-expression with the conditions to match, and the second argument is an optional object and allows you to set ordering and limit options. Keywords that can be used in the second argument are orderBy, orderDesc, limit and offset.

  getRows will return an array of rows matching the where-expression in the first argument, wrapped in a Promise (use await to read the array).

  #### Parameters

  - where: Where = {}
  - selopts: SelectOptions & ForUserRequest = {}

  #### Returns Promise<Row[]>

  #### Example

      const bookTable = Table.findOne({name: "books"})

      // get the rows in the book table with author = "Henrik Pontoppidan"
      const myBooks = await bookTable.getRows({author: "Henrik Pontoppidan"})

      // get the 3 most recent books written by "Henrik Pontoppidan" with more than 500 pages
      const myBooks = await bookTable.getRows({author: "Henrik Pontoppidan", pages: {gt: 500}}, {orderBy: "published", orderDesc: true})

  - Defined in packages/saltcorn-data/models/table.ts:1250

### getTags

- getTags(): Promise<AbstractTag[]>

  #### Returns Promise<AbstractTag[]>

  Implementation of AbstractTable.getTags

  - Defined in packages/saltcorn-data/models/table.ts:4336

### import_csv_file

- import_csv_file(
  filePath: string,
  options?: {
  delimiter?: string;
  extra_row_values?: Row;
  method?: "copy" | "Auto" | "row-by-row";
  no_table_write?: boolean;
  no_transaction?: boolean;
  overwrite_csv_fields?: boolean;
  recalc_stored?: boolean;
  skip_first_data_row?: boolean;
  },
  ): Promise<ResultMessage>

  Import CSV file to existing table

  #### Parameters

  - filePath: string
  - `Optional` options: {
    delimiter?: string;
    extra_row_values?: Row;
    method?: "copy" | "Auto" | "row-by-row";
    no_table_write?: boolean;
    no_transaction?: boolean;
    overwrite_csv_fields?: boolean;
    recalc_stored?: boolean;
    skip_first_data_row?: boolean;
    }

  #### Returns Promise<ResultMessage>

  - Defined in packages/saltcorn-data/models/table.ts:3156

### import_json_file

- import_json_file(
  filePath: string,
  skip_first_data_row?: boolean,
  ): Promise<ResultMessage>

  Import JSON table description

  #### Parameters

  - filePath: string
  - `Optional` skip_first_data_row: boolean

  #### Returns Promise<ResultMessage>

  - Defined in packages/saltcorn-data/models/table.ts:3561

### import_json_history_file

- import_json_history_file(filePath: string): Promise<void>

  #### Parameters

  - filePath: string

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:3549

### insert_history_row

- insert_history_row(v0: Row, retry?: number): Promise<void>

  #### Parameters

  - v0: Row
  - retry: number = 0

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:1759

### insertRow

- insertRow(
  v_in0: Row,
  user?: AbstractUser,
  resultCollector?: object,
  noTrigger?: boolean,
  syncTimestamp?: Date,
  ): Promise<any>

  Insert row into the table. By passing in the user as the second argument, it will check write rights. If a user object is not supplied, the insert goes ahead without checking write permissions.

  Returns the primary key value of the inserted row.

  This will throw an exception if the row does not conform to the table constraints. If you would like to insert a row with a function that can return an error message, use Table.tryInsertRow instead.

  #### Parameters

  - v_in0: Row
  - `Optional` user: AbstractUser
  - `Optional` resultCollector: object
  - `Optional` noTrigger: boolean
  - `Optional` syncTimestamp: Date

  #### Returns Promise<any>

  #### Example

      await Table.findOne("People").insertRow({ name: "Jim", age: 35 })

  - Defined in packages/saltcorn-data/models/table.ts:2078

### is_owner

- is_owner(user: AbstractUser | undefined, row: Row): boolean

  Check if user is owner of row

  #### Parameters

  - user: AbstractUser | undefined

    user

  - row: Row

    table row

  #### Returns boolean

  - Defined in packages/saltcorn-data/models/table.ts:516

### latestSyncInfo

- latestSyncInfo(id: PrimaryKeyValue): Promise<Row | null>

  #### Parameters

  - id: PrimaryKeyValue

  #### Returns Promise<Row | null>

  - Defined in packages/saltcorn-data/models/table.ts:1781

### latestSyncInfos

- latestSyncInfos(ids: PrimaryKeyValue[]): Promise<Row[] | null>

  #### Parameters

  - ids: PrimaryKeyValue[]

  #### Returns Promise<Row[] | null>

  - Defined in packages/saltcorn-data/models/table.ts:1786

### move_include_fts_to_search_context

- move_include_fts_to_search_context(): Promise<void>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:4420

### normalise_error_message

- normalise_error_message(msg: string): string

  #### Parameters

  - msg: string

  #### Returns string

  - Defined in packages/saltcorn-data/models/table.ts:2499

### owner_fieldname

- owner_fieldname(): string | null

  Get owner column name

  #### Returns string | null

  - Defined in packages/saltcorn-data/models/table.ts:504

### owner_fieldname_from_fields

- owner_fieldname_from_fields(fields?: Field[] | null): string | null

  Get owner column name

  #### Parameters

  - `Optional` fields: Field[] | null

    fields list

  #### Returns string | null

  null or owner column name

  - Defined in packages/saltcorn-data/models/table.ts:494

### ownership_formula_where

- ownership_formula_where(user: AbstractUser): Where

  #### Parameters

  - user: AbstractUser

  #### Returns Where

  - Defined in packages/saltcorn-data/models/table.ts:3937

### ownership_options

- ownership_options(): Promise<{ label: string; value: string }[]>

  get Ownership options user interface...

  #### Returns Promise<{ label: string; value: string }[]>

  - Defined in packages/saltcorn-data/models/table.ts:539

### processSelectOptions

- processSelectOptions(
  selopts?: SelectOptions & ForUserRequest,
  ): SelectOptions & ForUserRequest

  #### Parameters

  - selopts: SelectOptions & ForUserRequest = {}

  #### Returns SelectOptions & ForUserRequest

  - Defined in packages/saltcorn-data/models/table.ts:1288

### read_state_strict

- read_state_strict(state: Row): string | Row

  #### Parameters

  - state: Row

  #### Returns string | Row

  - Defined in packages/saltcorn-data/models/table.ts:3094

### redo_row_changes

- redo_row_changes(id: PrimaryKeyValue, user?: AbstractUser): Promise<void>

  Redo row changes

  #### Parameters

  - id: PrimaryKeyValue
  - `Optional` user: AbstractUser

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2771

### rename

- rename(new_name: string): Promise<void>

  Rename table

  #### Parameters

  - new_name: string

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2866

### repairCompositePrimary

- repairCompositePrimary(): Promise<void>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:4363

### resetSequence

- resetSequence(): Promise<void>

  Reset Sequence

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:872

### restore_row_version

- restore_row_version(
  id: PrimaryKeyValue,
  version: number,
  user?: AbstractUser,
  ): Promise<void>

  Restore Row Version

  #### Parameters

  - id: PrimaryKeyValue
  - version: number
  - `Optional` user: AbstractUser

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2716

### slug_options

- slug_options(): Promise<{ label: string; steps: SlugStepType[] }[]>

  #### Returns Promise<{ label: string; steps: SlugStepType[] }[]>

  - Defined in packages/saltcorn-data/models/table.ts:4288

### to_provided_table

- to_provided_table(): any

  #### Returns any

  - Defined in packages/saltcorn-data/models/table.ts:313

### toggleBool

- toggleBool(
  id: PrimaryKeyValue,
  field_name: string,
  user?: AbstractUser,
  ): Promise<void>

  ????

  #### Parameters

  - id: PrimaryKeyValue
  - field_name: string
  - `Optional` user: AbstractUser

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:1951

### tryInsertRow

- tryInsertRow(
  v: Row,
  user?: AbstractUser,
  resultCollector?: object,
  ): Promise<{ error: string } | { success: PrimaryKeyValue }>

  Try to Insert row

  #### Parameters

  - v: Row
  - `Optional` user: AbstractUser
  - `Optional` resultCollector: object

  #### Returns Promise<{ error: string } | { success: PrimaryKeyValue }>

  - Defined in packages/saltcorn-data/models/table.ts:2478

### tryUpdateRow

- tryUpdateRow(
  v: Row,
  id: PrimaryKeyValue,
  user?: AbstractUser,
  resultCollector?: object,
  extraArgs?: any,
  ): Promise<ResultMessage>

  Try to Update row

  #### Parameters

  - v: Row
  - id: PrimaryKeyValue
  - `Optional` user: AbstractUser
  - `Optional` resultCollector: object
  - `Optional` extraArgs: any

  #### Returns Promise<ResultMessage>

  - Defined in packages/saltcorn-data/models/table.ts:1925

### undo_row_changes

- undo_row_changes(id: PrimaryKeyValue, user?: AbstractUser): Promise<void>

  Undo row chnages

  #### Parameters

  - id: PrimaryKeyValue
  - `Optional` user: AbstractUser

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2739

### update

- update(new_table_rec: Partial<Table>): Promise<void>

  Update Table description in _sc_table Also creates / drops history table for table

  #### Parameters

  - new_table_rec: Partial<Table>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2902

### updateRow

- updateRow(
  v_in: Row,
  id_in: Row | PrimaryKeyValue,
  user?: AbstractUser,
  noTrigger?:
  | boolean
  | {
  additionalTriggerValues?: Row;
  autoRecalcIterations?: number;
  extraArgs?: any;
  noTrigger?: boolean;
  restore_of_version?: number;
  resultCollector?: object;
  syncTimestamp?: Date;
  },
  resultCollector?: object,
  restore_of_version?: number,
  syncTimestamp?: Date,
  additionalTriggerValues?: Row,
  autoRecalcIterations?: number,
  extraArgs?: any,
  ): Promise<string | void>

  Update a single row in the table database.

  The first two arguments are mandatory. The first is an object with the new values to set in the row. The second argument is the value of the primary key of the row to update. Typically this is the id field of an existing row object

  #### Parameters

  - v_in: Row

    columns with values to update

  - id_in: Row | PrimaryKeyValue
  - `Optional` user: AbstractUser

    user

  - `Optional` noTrigger:
    | boolean
    | {
    additionalTriggerValues?: Row;
    autoRecalcIterations?: number;
    extraArgs?: any;
    noTrigger?: boolean;
    restore_of_version?: number;
    resultCollector?: object;
    syncTimestamp?: Date;
    }
  - `Optional` resultCollector: object
  - `Optional` restore_of_version: number
  - `Optional` syncTimestamp: Date
  - `Optional` additionalTriggerValues: Row
  - `Optional` autoRecalcIterations: number
  - `Optional` extraArgs: any

  #### Returns Promise<string | void>

  #### Example

      const bookTable = Table.findOne({name: "books"})

      // get the row in the book table for Moby Dick
      const moby_dick = await bookTable.getRow({title: "Moby Dick"})

      // Update the read field to true and the rating field to 5
      await bookTable.updateRow({read: true, rating: 5}, moby_dick.id)

      // if you want to update more than one row, you must first retrieve all the rows and
      // then update them individually

      const allBooks = await bookTable.getRows()
      for(const book of allBooks) {
        await bookTable.updateRow({price: book.price*0.8}, book.id)
      }

  - Defined in packages/saltcorn-data/models/table.ts:1410

### updateWhereWithOwnership

- updateWhereWithOwnership(
  where: Where,
  user?: AbstractUser,
  forRead?: boolean,
  ): { notAuthorized?: boolean } | undefined

  update Where with Ownership

  #### Parameters

  - where: Where
  - `Optional` user: AbstractUser
  - `Optional` forRead: boolean

  #### Returns { notAuthorized?: boolean } | undefined

  - Defined in packages/saltcorn-data/models/table.ts:894

### `Static` allSlugOptions

- allSlugOptions(): Promise<
  { [nm: string]: { label: string; steps: SlugStepType[] }[] },
  >

  #### Returns Promise<{ [nm: string]: { label: string; steps: SlugStepType[] }[] }>

  - Defined in packages/saltcorn-data/models/table.ts:4320

### `Static` analyze_all_indexed_tables

- analyze_all_indexed_tables(): Promise<void>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:1750

### `Static` create

- create(
  name: string,
  options?: SelectOptions | TablePack,
  id?: number,
  ): Promise<Table>

  Create table

  #### Parameters

  - name: string

    table name

  - options: SelectOptions | TablePack = {}

    table fields

  - `Optional` id: number

    optional id, if set, no '_sc_tables' entry is inserted

  #### Returns Promise<Table>

  table

  - Defined in packages/saltcorn-data/models/table.ts:713

### `Static` create_from_csv

- create_from_csv(name: string, filePath: string): Promise<ResultMessage>

  Table Create from CSV

  #### Parameters

  - name: string
  - filePath: string

  #### Returns Promise<ResultMessage>

  - Defined in packages/saltcorn-data/models/table.ts:2967

### `Static` createInDb

- createInDb(table: Table): Promise<void>

  Create the table structure generates a CREATE TABLE cmd with all field and runs it TODO field defaults for pg

  #### Parameters

  - table: Table

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:795

### `Static` find

- find(where?: Where, selectopts?: SelectOptions): Promise<Table[]>

  Find Tables

  #### Parameters

  - `Optional` where: Where

    where condition

  - selectopts: SelectOptions = ...

    options

  #### Returns Promise<Table[]>

  table list

  - Defined in packages/saltcorn-data/models/table.ts:388

### `Static` find_with_external

- find_with_external(where0?: Where, selectopts?: SelectOptions): Promise<Table[]>

  Find Tables including external tables

  #### Parameters

  - where0: Where = {}
  - selectopts: SelectOptions = ...

  #### Returns Promise<Table[]>

  - Defined in packages/saltcorn-data/models/table.ts:444

### `Static` findOne

- findOne(where: string | number | Where | Table): Table | null

  Find one Table

  #### Parameters

  - where: string | number | Where | Table

    where condition

  #### Returns Table | null

  table or null

  - Defined in packages/saltcorn-data/models/table.ts:349

### `Static` state_refresh

- state_refresh(noSignal?: boolean): Promise<void>

  #### Parameters

  - `Optional` noSignal: boolean

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/table.ts:2931