reshuffle-redis-connector
Code | npm | Code sample
npm install reshuffle-redis-connector
Reshuffle Redis Connector
This package contains a Resshufle connector to Redis databases.
The following example sets and gets a value from Redis:
const Reshuffle = const RedisConnector = ;async { const app = const redis = app endpoint: processenvREDIS_ENDPOINT password: processenvREDIS_PASSWORD await redis console // 'value' await redis }
Configuration options
const app = const redis = app endpoint: processenvREDIS_ENDPOINT // <hostname>:<port> password: processenvREDIS_PASSWORD
Connector actions
Close action
Definition:
void
Usage:
await redis
Close all connections to the database. If an application terminates without calling close, it might hang for a few seconds until active connections time out.
Sequence action
Definition:
any any
Usage:
const res = await res
Use sequence
to perform multiple queries on the same database connection.
This assures sequential access, but not atomicity in case other connections
are open to the database.
Transaction action
Definition:
any any
Usage:
await redis
Use transaction
to run multiple queries as an atomic sequence using
Redis transaction and the MULTI
command. All operations either succed or fail together.
SDK actions
The sdk()
action provides raw access to the async version of npm redis. The following actions are provided
as covenience wrappers for it's corresponding async methods. For instance,
calling redis.sdk().append()
is equivalent to redis.append()
. You can
access the methods not listed below using the redis.sdk().
notation.
Close action
Definition:
void
Usage:
await pg
Close all connections to the database. If an application terminates without calling close, it might hang for a few seconds until active connections time out.
Query action
Definition:
sql: string, params?: any,
Usage:
await pg const family = await pg// {// rowCount: 2,// fields: [{ name: 'firstName' }, { name: 'lastName' }, { name: 'age' }],// rows: [// { firstName: 'Alice', lastName: 'Coltrane', age: 31 },// { firstName: 'John', lastName: 'Coltrane', age: 42 },// ],// } const avgResponse = await pgconst averageAge = avgResponserows0avg// 36.5
The query
action can be used to run any SQL command on the connected
database (not just SELECT
). The query is defined in the sql
string. The
optional params
can be used to generate parameterized queries, as shown in
the following example:
const age = await pg
This action returns an object with the results of the query, where
fields
is an array of all field names, as returned by the query.
Field names in a SELECT
query are column names, or are specified
with an AS
clause. Every element of rows
is uses the names in
fields
as its object keys.
Note that every call to query
may use a different database connection.
You can use the sequence or transaction actions
if such a guarantee is required.
Sequence action
Definition:
any any
Usage:
const res = await pgconst userCount = resrows0count// 3
Use sequence
to perform multiple queries on the same database connection.
This action receives a seq
function that may issue queries to the database,
all of which are guaranteed to run through the same connection. seq
gets
one argument, which is a query
function that can be used the same way as
the query action. seq
may, of course, use any JavaScript code to
implement its logic, log to the console etc.
Note that while sequence
uses the same connection to run all queries, it
does not offer the transactional guarantees offered by
transaction. You can use it for weak isolation models, or
construct transactions directly without using transaction
.
Transaction action
Definition:
any any
Usage:
await pg
Use transaction
to run multiple queries as an atomic SQL transaction.
The interface is identical to the sequence action, but all
the queries issued seq
either success or fail together. If any of the
queries fail, all queries are rolled back and an error is thrown.
Consider, for example, the following code for updating a bank account balance:
const accountId = 289const change = +1000pg
In the example above, accounts
holds current balances of accounts,
while accounts_log
holds a history of all changes made. Using transaction
ensures that both tables are always updated together.