The jsDbList
module declares two classes:
It is a class that serves as a dashboard for all information related to the execution context of a session.
The properties of a Context
object are assigned by jsApplication
when accepting the execution of a route. Among these, we distinguish:
The code of the used database (used to invoke the getDbInfo function).
Instance of the DbDescriptor
for the connected database (a singleton per database).
Function that creates a class implementing the PostData interface. The default implementation returns an instance of BusinessPostData.
Function that creates an instance of the GetDataInvoke
class associated with the current context. This class can be invoked by metadata, which is shared between the client and the server.
When used on the server side, metadata will reference the class present in client/components/metadata
. When used on the client side, metadata will reference the getData
class associated with appMeta
. However, in the metadata, this mechanism is transparent when used, and it is sufficient to use the instance as (this).getData
, where this
is the metadata itself. The class used on the client side exposes the same interface as the one used on the server side, so the code can run interchangeably.
This does not preclude the application from using the GetData class in classes not shared with the client.
SqlFormatter associated with the connected database; it should not be necessary unless manually composing SQL commands.
Connection
associated with the current database. The Connection
is a lower-level class compared to DataAccess
. For details, consult the relevant documentation: jsSqlDriver and jsMySqlDriver.
Environment associated with the request.
DataAccess associated with the request.
Overall, the Context
has all the information needed to operate on the database and all knowledge about the connected user. In each middleware, it is accessible in the property:
req.app.locals.context
This class knows the structure of tables and views and can be used, for example, to create DataTables to add to DataSets or to know what fields each table has, what type they are, and what the keys are.
Main methods of DbDescriptor
:
table(tableName, tableDescriptor)
: reads or sets theTableDescriptor
of a table or view. If attempting to read aTableDescriptor
not previously set, thetableDescriptor
method of the underlyingConnection
is invoked to obtain the desired information directly from the system tables of the database.createTable(tableName)
: creates aDataTable
with the columns of a specific table or view.forgetTable(tableName)
: deletes information about aDataTable
so that it can potentially be recalculated on the next request.
The init
method of DbDescriptor
takes the name of a file as input, which is usually encrypted but can also be unencrypted depending on the parameters:
encryptedFileName
: name of the encrypted file to readfileName
: name of the file to possibly create decryptedencrypt
: if true, the read file is to be encrypteddecrypt
: if true, a copy of the encrypted file is to be created in clear textkey
,iv
,pwd
: parameters for decrypting or encrypting the file
This method reads the information about connections (of type DbInfo
, which will be explained shortly) in bulk.
It is still possible to use the getDbInfo/setDbInfo/delDbInfo
methods to manage the information read from the file.
The configuration file is automatically updated with the changes.
It is a class that describes a table and exposes:
columnNames()
: array of column namescolumn(columnName)
: descriptor of the given columndescribeTable(t)
: adds the columns of this table to theDataTable
t, setting the name of the column and the propertiesctype
,is_nullable
,max_length
for each column. It also sets the key of theDataTable
.getKey()
: array of key column names
Column descriptor. It has the following fields:
name
{string} - field nametype
{string} - db typectype
{string} - JavaScript typemax_length
{number} - maximum size in bytes if a stringprecision
{number} - total number of digits for decimalsscale
{number} - number of decimal places for decimalsis_nullable
{boolean} - true if it allows nullpk
{boolean} - true if it is a primary key
Methods to read / write / delete / check the presence of database access information (DbInfo
)
DbInfo
is a structure like the following:
{
server: "server name",
useTrustedConnection: true/false, if true user/pwd are not used
user: "user to connect to the db",
pwd: "password to connect to the db",
database: "database name",
sqlModule: 'jsMySqlDriver' or 'jsSqlServerDriver'
}
and is required for the jsDbList
module to know how to build connections (Connection
).
Function that, given a dbCode
, returns the Connection
. For its operation, it requires that setDbInfo
has been previously called for that dbCode
. Note that there is no Connection
class; it only represents an abstract interface.
The returned instance will actually be of type SqlServerConnection
or MySqlConnection
(or other) depending on the type of database.
Function that, given a dbCode
, returns the DbDescriptor
. The DbDescriptor
is placed in a cache, where there is only one for each dbCode
. If it is not already present, it calculates it, but it requires that setDbInfo
has been previously invoked for that dbCode
or that the requested connection was in the configuration file used to initialize the dbList
.
Function that, given a dbCode
, gets a connection (DataAccess
). Closing the connection is the responsibility of the caller. For correct operation, it requires that setDbInfo
has been previously called or that the requested connection was in the configuration file used to initialize the dbList
.