All Projects → mike-eason → oledb

mike-eason / oledb

Licence: MIT license
A small promised based module which uses edge.js to connect to an OLE DB, ODBC or SQL database.

Programming Languages

javascript
184084 projects - #8 most used programming language
C#
18002 projects

Projects that are alternatives of or similar to oledb

db.rstudio.com
Website dedicated to all things R and Databases
Stars: ✭ 13 (-53.57%)
Mutual labels:  odbc
qt-sql-example
Example Qt application that connects to SQL Server and displays a table from the database
Stars: ✭ 29 (+3.57%)
Mutual labels:  odbc
JetEntityFrameworkProvider
Microsoft Access (Jet) Entity Framework provider
Stars: ✭ 77 (+175%)
Mutual labels:  oledb
retryx
Promise-based retry workflow library.
Stars: ✭ 21 (-25%)
Mutual labels:  promise
kuromojin
Provide a high-level wrapper for kuromoji.js. Cache/Promise API
Stars: ✭ 64 (+128.57%)
Mutual labels:  promise
routeros-client
Abstraction layer over the node-routeros API
Stars: ✭ 63 (+125%)
Mutual labels:  promise
buzy
Async queue manager for node and browser
Stars: ✭ 23 (-17.86%)
Mutual labels:  promise
freenom
🌐 Feenom Promise/Async/Await
Stars: ✭ 19 (-32.14%)
Mutual labels:  promise
ws-promise
A tiny, Promise-based WebSocket protocol allowing request-response usage in ECMAScript
Stars: ✭ 20 (-28.57%)
Mutual labels:  promise
organiser
An organic web framework for organized web servers.
Stars: ✭ 58 (+107.14%)
Mutual labels:  promise
dataloader
Dataloader is a generic utility for batch data loading with caching, works great with GraphQL
Stars: ✭ 114 (+307.14%)
Mutual labels:  promise
apns2
Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens
Stars: ✭ 66 (+135.71%)
Mutual labels:  promise
reactphp-child-process-promise
No description or website provided.
Stars: ✭ 12 (-57.14%)
Mutual labels:  promise
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-46.43%)
Mutual labels:  promise
wise-river
Object streaming the way it should be.
Stars: ✭ 33 (+17.86%)
Mutual labels:  promise
Forbind
Functional chaining and promises in Swift
Stars: ✭ 44 (+57.14%)
Mutual labels:  promise
webpack2-polyfill-plugin
Insert polyfills (such as Promise) for Webpack 2
Stars: ✭ 18 (-35.71%)
Mutual labels:  promise
combine-promises
Like Promise.all(array) but with an object instead of an array.
Stars: ✭ 181 (+546.43%)
Mutual labels:  promise
sack.vfs
Node addon which adds a virtual file system interface; websockets; json(6) parsing; sql support(sqlite,odbc); javascript sched_yield; ssl certificate generation; more...
Stars: ✭ 29 (+3.57%)
Mutual labels:  odbc
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (+10.71%)
Mutual labels:  promise

oledb.js

npm version license tips

A small promise based module which uses Edge-JS to connect and execute queries for a OLE DB, ODBC or SQL database.

Example

const connectionString = '...';

const oledb = require('oledb');
const db = oledb.oledbConnection(connectionString);

let command = 'select * from account;';

db.query(command)
.then(result => {
    console.log(result);
},
err => {
    console.error(err);
});

The result will look like this:

{
    query: 'select count(*) from account where name = @p1',
    type: 'query',
    params: [
        {
            name: 'p1',
            value: 'Mike',
            direction: 0,
            isNullable: false,
            precision: null,
            scale: null,
            size: null
        }
    ],
    'result': [
        [
            {
                id: 1,
                name: 'Bob'
            }
        ]
    ]
}

Installation

npm install oledb --save

This module is a proxy that uses ADO.NET to call .NET code and therefore requires the .NET Framework to be installed.

Options

The module exposes three functions to initialize database connections:

  • oledb.oledbConnection(connectionString) - Initializes a connection to an OLE DB database.
  • oledb.odbcConnection(connectionString) - Initializes a connection to an ODBC database.
  • oledb.sqlConnection(connectionString) - Initializes a connection to an SQL database.

Promises

There are a number available promises that can be used to send commands and queries to a database connection:

  • .query(command, [parameters]) - Executes a query and returns an is the result set returned by the query as an Array.
  • .execute(command, [parameters]) - Executes a query command and returns an is the the number of rows affected.
  • .scalar(command, [parameters]) - Executes a query and returns an is the first column of the first row in the result set returned by the query. All other columns and rows are ignored.
  • .procedure(command, [parameters]) - Excutes a stored procedure and returns the number of rows affected.
  • .procedureScalar(command, [parameters]) - Excutes a stored procedure and returns the result.
  • .transaction(commands) - Excutes an array of commands in a single transaction and returns the result of each.

Each parameter is described below:

  • command - The string query command to be executed.
  • parameters - An Array of parameter values. This is an optional parameter.
  • commands - A parameter used for transactions, see the Transactions section below.

Query Parameters

Parameters are also supported and use positional parameters that are marked with a question mark (?) OR named parameters, i.e @parameter1. Here is an example:

let command = `
    update account
    set
        firstname = ?
    where
        id = ?;
`;

let parameters = [ 'Bob', 69 ];

db.execute(command, parameters)
.then(rowsAffected => {
    console.log(rowsAffected);
},
err => {
    console.error(err);
});

Query Parameter Options

There are a number of additional options for query parameters, a query parameter can either be a single value or an object:

let parameters = [
    'Bob',  //Declare a single parameter value. Defaults to: { name: '@p1', value: 'Bob' }
    //Or use an object to specify additional options...
    {
        name: 'myParameter',    //OPTIONAL - Parameter name. Defaults to index based parameter names, i.e @p1, @p2, @p3 ect. Note that the @ symbols are optional.
        value: 123,             //OPTIONAL - Defaults to null.
        direction: string,      //OPTIONAL - The parameter direction, (Input, Input/Output, Output, Return Value). See oledb.PARAMETER_DIRECTIONS enum.
        isNullable: bool,       //OPTIONAL - Whether to treat the paramter as non-nullable.
        precision: byte,        //OPTIONAL - The precision of the parameter value in bytes.
        scale: byte,            //OPTIONAL - The scale of the parameter value in bytes.
        size: byte              //OPTIONAL - The size of the parameter value in bytes.
    }
];

Multiple Data Sets

The .query promise has support for multiple data sets that can be returned in a single query. Here is an example:

let command = `
    select * from account;
    select * from address;
`;

db.query(command)
.then(results => {
    console.log(results[0]); //1st query result
    console.log(results[1]); //2nd query result
},
err => {
    console.error(err);
});

Stored Procedures

Stored procedures can be executed using the .procedure function with optional parameters and return value. Here is an example:

let procedureName = `addNumbers`;

let parameters = [1, 2];

db.procedure(procedureName, parameters)
.then(result => {
    console.log(result);
},
err => {
    console.error(err);
});

Stored Procedure Return Values

You can use a return value or output parameter with the .procedure function. The parameter might look like this:

{
    name: 'sum',
    direction: oledb.PARAMETER_DIRECTIONS.OUTPUT
}

for more options, see Query Parameter Options section.

Here is an example:

let procedureName = `addNumbers`;

let parameters = [
    {
        name: 'num1',
        value: 1
    },
    {
        name: 'num2',
        value: 2
    },
    {
        name: 'sum',
        direction: oledb.PARAMETER_DIRECTIONS.OUTPUT
    }
];

db.procedure(procedureName, parameters)
.then(result => {
    console.log(result);
    console.log(result.params[2].value);    //The output value returned by addNumbers stored procedure.
},
err => {
    console.error(err);
});

Transactions

The .transaction promise will execute multiple commands in a single transaction, this is useful for if you want to insert records across different tables and need to ensure that they all are inserted successfully, or not at all. All query types are supported, including procedure. Here is an example:

let commands = [
    {
        query: 'insert into account (name) values (?)',
        params: [ 'Bob' ]
    },
    {
        query: 'select * from account where name = ?',
        type: oledb.COMMAND_TYPES.QUERY,
        params: [ 'Bob' ]
    }
];

db.transaction(commands)
.then(results => {
    console.log(results); //An array of query results.
},
err => {
    console.log(err);
});

Note: The result field will contain an array of results if using a query command as multiple query results are supported by each executed query. See Multiple Data Sets above.

All commands must follow the following structure:

{
    query: string,      //REQUIRED - The query string
    params: Array,      //OPTIONAL - The query parameters
    type: string        //OPTIONAL - The query type, use one of the oledb.COMMAND_TYPES enumerations. Defaults to 'command'.
}

$prev Parameter

With transactions, you can use the special '$prev' parameter to inject the previous command's result into the next executing query. For example:

let commands = [
    //First query, executes a stored procedure and returns an account id.
    {
        query: 'insert_account (@name)',
        params: [ 
            {
                name: 'name',
                value: 'Bob'
            },
            {
                name: 'accountId',
                direction: oledb.PARAMETER_DIRECTIONS.RETURN_VALUE
            }
        ],
        type: oledb.COMMAND_TYPES.PROCEDURE
    },
    //Second query, executes a select query with the returned value from the previous query.
    {
        query: 'select * from account where id = @accountId',
        type: oledb.COMMAND_TYPES.QUERY,
        params: [
            {
                name: 'accountId',
                value: '$prev'      //Note: This value must be a string.
            }
        ]
    }
];

db.transaction(commands)
.then(results => {
    console.log(results[0]); //Insert stored procedure result. Returns the ID of the account.
    console.log(results[1]); //Select query result. Returns the account 'Bob' record.
},
err => {
    console.log(err);
});

License

This project is licensed under MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].