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 |
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'] |
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 |
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 |
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 |
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'
})