Class: Table

Table()

Represents a table.

A table will be initialized after first crud operation. This can be an existing one or a new one.

Missing fields specified in select / where clause are atomatically created. Missing indexes specified in where clause are automatically created.

Constructor

new Table()

Properties:
Name Type Description
column Array.<string>

column names

indexes Array.<string>

index names

name string

table name

db Object

sqlite-async connection instance

initialized bool

is true if table exists, columns are loaded and indexes are loaded

Source:

Methods

find(whereopt, columnNamesopt) → {Array.<Object>}

Find multiple records

Parameters:
Name Type Attributes Default Description
where Object <optional>
null

E.g: {'id__like': '%a%'}

columnNames Array <optional>
null

E.g: ['id', 'name']

Source:
Returns:

Array of found records

Type
Array.<Object>
Example
await this.find({
     'id__gte': 5,
     'name__like': '%John',
     '_orderBy': '-id',
     '_limit': 50,
     '_offset': 10
})

findOne(whereopt, columnNamesopt) → {Object|null}

Find one record

Parameters:
Name Type Attributes Default Description
where Object <optional>
{}

E.g: {'id': 1}

columnNames Array.<string> <optional>
null
Source:
Returns:
  • Record

    Type
    Object
  • if no matches

    Type
    null
Example
await this.findOne({
     'name': 'John'
}))

insert({'columnName':'value'}) → {number}

Creates new record. Supported field types:

  • string
  • integer
  • boolean will be converted to int
  • date will be converted to string
Parameters:
Name Type Description
{'columnName':'value'} Object
Source:
Returns:

primary key of created record

Type
number
Example
console.info('Created pk', await this.insert({'stringColumn': 'value', 'numberColumn': 5}})

update(data, where, where)

Parameters:
Name Type Description
data Object

record

where Object

values that should match

where Array.<string>

field names of values that should match

Source:
Examples

Update with column name array as where clause

const changedRecordCount = await this.update({
     'id': 1
     'name': 'John'
},['id'])

Update with string as where clause

const changedRecordCount = await this.update({
     'id': 1
     'name': 'John'
},'id')

Update with object as where clause

const changedRecordCount = await this.update({
     'id': 1
     'name': 'John'
},{'id': 1})

Update without where clause

// Update with no where clause
const changedRecordCount = await this.update({
     'id': 1
     'name': 'John'
})