Skip to main content

@ones-op/node-database

We provide a set of interfaces related to the plugin database, allowing plugin developers to access and operate the plugin database.

Requirements

ONES
v3.6.0+

API

importSQL

Initialize the database

Params

ParamDescriptionTypeRequiredDefault
sqlFileNamesql file pathstringY-

Error

errcodereasonlevelstatusCode
DB.SqlFileNameFormatInvalidSQL filename format invaild.error400
DB.SqlFileFetchingFailedSQL file fetching failed.error400
DB.CannotFindTableCannot find table content in SQL file.error400
DB.ImportSqlErrAn unknown error occurred while executing the import method, and the hardware error scenario gave the cause of the hardware error.error500

Example

import { importSQL, DBError } from '@ones-op/node-database'
import { Logger } from '@ones-op/node-logger'

export async function testImportSQL() {
try {
await importSQL('plugin.sql')
} catch (error) {
if (error instanceof DBError) {
Logger.error('error:', error.errcode, error.statusCode, error.level, error.reason)
}
}
}

select

Receives the sql statement related to the query and returns the query result, which will be throw error if the sql execution fails.

Params

ParamDescriptionTypeRequiredDefault
sqlSQL statement related to the querystringY-
argsFilling in the variable parameters for the SQL statements contain placeholders ?(supported in ONES v6.8.0+/v6.1.96+,@ones-op/node-database@0.70.1+anyN-

Returns

ParamDescriptionType
resultsql query resultRecord<string, any>[]

Error

errcodereasonlevelstatusCode
DB.QuerySqlErrAn unknown error occurred while executing the select method, and the specific error scenario gives the specific cause of the error.error500
DB.SqlSyntaxErrThe sql does not conform to the grammatical specification, and the specific error scene gives the specific error reason.error400
InvalidParameterInvalid parameter.error400

Example

import { select, DBError } from '@ones-op/node-database'
import { Logger } from '@ones-op/node-logger'

export async function select_database() {
try {
const result1 = await select('select * from email_id_map limit 10;')
const result2 = await select('select * from email_id_map where id_number = ?;', '001'))
} catch (error) {
if (error instanceof DBError) {
Logger.error('error:', error.errcode, error.statusCode, error.level, error.reason)
}
}
}

exec

Execute the relevant sql statement and no result is returned. If sql execution fails, it will throw error.

Params

ParamDescriptionTypeRequiredDefault
operateoperation type,'insert' 、'update' 、 'delete' 、'create' 、'alter' 、 'drop'stringY-
sqlsql statementstringY-
argsFilling in the variable parameters for the SQL statements contain placeholders ?(supported in ONES v6.2.19+/v6.1.86+,@ones-op/node-database@0.46.5+anyN-

Error

errcodereasonlevelstatusCode
DB.ExecSqlErrAn unknown error occurred while executing the exec method, and the specific error scenario gives the specific cause of the error.error500
DB.SqlSyntaxErrThe sql does not conform to the grammatical specification, and the specific error scene gives the specific error reason.error400
InvalidParameterInvalid parameter.error400

Example

import { exec, DBError } from '@ones-op/node-database'
import { Logger } from '@ones-op/node-logger'

export async function exec_database() {
try {
await exec(
'insert',
`INSERT INTO email_id_map(email,id_number) VALUES ("plugin1@ones.com", "001");`,
)
await exec(
'insert',
`INSERT INTO email_id_map(email,id_number) VALUES (?,?);`,
'plugin2@ones.com',
'002',
)
const args = ['plugin3@ones.com', '003']
await exec('insert', `INSERT INTO email_id_map(email,id_number) VALUES (?,?);`, ...args)
} catch (error) {
if (error instanceof DBError) {
Logger.error('error:', error.errcode, error.statusCode, error.level, error.reason)
}
}
}

count

Count related sql interface. If the sql execution is a failure, it will throw error.

Params

ParamDescriptionTypeRequiredDefault
sqlSQL statementstringY-
argsFilling in the variable parameters for the SQL statements contain placeholders ?(supported in ONES v6.8.0+/v6.1.96+,@ones-op/node-database@0.70.1+anyN-

Returns

ParamDescriptionType
resultstatistical resultsnumber

Error

errcodereasonlevelstatusCode
DB.QuerySqlErrAn unknown error occurred while executing the count method, and the specific error scenario gives the specific cause of the error.error500
DB.SqlSyntaxErrThe sql does not conform to the grammatical specification, and the specific error scene gives the specific error reason.error400
InvalidParameterInvalid parameter.error400

Example

import { count, DBError } from '@ones-op/node-database'
import { Logger } from '@ones-op/node-logger'

export async function count_database() {
try {
const cnt1 = await count('select count(*) from email_id_map;')
const cnt2 = await count('select count(*) from email_id_map where id_number = ?;', '001'))
} catch (error) {
if (error instanceof DBError) {
Logger.error('error:', error.errcode, error.statusCode, error.level, error.reason)
}
}
}

Type

DBError

ParamDescriptionType
levelerror levelstring
reasonerror reasonstring
statusCodehttp status codestring
errcodeerror codestring