Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

database-js

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

database-js - npm Package Compare versions

Comparing version 3.0.10 to 3.0.11

61

lib/connection.js
var ConnectionObject = require('./connectionObject');
var SqlObject = require('./sqlObject');
var QuoteString = require('./quoteString');
var ParseConnection = require('./parseConnection');
var Prepare = require('./prepare');
var Join = require('./join');
var Statement = require('./statement');

@@ -59,5 +55,5 @@ var PreparedStatement = require('./preparedStatement');

/**
* Creates a statement with the passed SQL.
*
* @param {string} sql
* Creates a statement with the given SQL.
*
* @param {string} sql
* @returns {Statement}

@@ -67,10 +63,9 @@ * @memberof Connection

createStatement(sql) {
let statement = new Statement(this, sql);
return statement;
return new Statement(this, sql);
}
/**
* Creates and prepares a statement with the passed SQL.
*
* @param {string} sql
* Creates and prepares a statement with the given SQL.
*
* @param {string} sql
* @returns {PreparedStatement}

@@ -80,4 +75,3 @@ * @memberof Connection

prepareStatement(sql) {
let statement = new PreparedStatement(this, sql);
return statement;
return new PreparedStatement(this, sql);
}

@@ -87,3 +81,3 @@

* Closes the underlying connection.
*
*
* @returns {Promise<boolean>}

@@ -94,12 +88,8 @@ * @memberof Connection

return new Promise((resolve, reject) => {
if ( 'function' !== typeof this.connection.close ) {
return resolve( true ); // nothing to do
if (typeof this.connection.close !== 'function') {
return resolve(true); // nothing to do
}
this.connection.close().then(() => {
resolve(true);
}).catch((reason) => {
reject(reason);
});
this.connection.close()
.then(() => resolve(true))
.catch(reason => reject(reason));
});

@@ -109,4 +99,4 @@ }

/**
* Indicates whether the underlying driver can support transactions;
*
* Indicates whether the underlying driver support transactions.
*
* @returns {boolean}

@@ -123,5 +113,4 @@ * @memberof Connection

/**
* Returns true if the underlying driver is in a transaction, false
* if it does not support transactions or is not in a transaction.
*
* Returns true if the underlying driver is in a transaction, false otherwise.
*
* @returns {boolean}

@@ -140,5 +129,5 @@ * @memberof Connection

* false if it was not started. Transactions can fail to start if
* another transaction is already running or if the driver does
* another transaction is already running or if the driver does
* not support transactions.
*
*
* @returns {Promise<boolean>}

@@ -148,3 +137,3 @@ * @memberof Connection

beginTransaction() {
if (!this.isTransactionSupported()) {
if (! this.isTransactionSupported()) {
return Promise.resolve(false);

@@ -160,3 +149,3 @@ }

* transactions.
*
*
* @returns {Promise<boolean>}

@@ -166,3 +155,3 @@ * @memberof Connection

commit() {
if (!this.inTransaction()) {
if (! this.inTransaction()) {
return Promise.resolve(false);

@@ -179,3 +168,3 @@ }

* transactions.
*
*
* @returns {Promise<boolean>}

@@ -185,3 +174,3 @@ * @memberof Connection

rollback() {
if (!this.inTransaction()) {
if (! this.inTransaction()) {
return Promise.resolve(false);

@@ -188,0 +177,0 @@ }

@@ -1,4 +0,2 @@

var Connection = require('./connection');
var PooledConnection = require('./pooledConnection');
var ParseConnection = require('./parseConnection');

@@ -9,3 +7,3 @@ class DynamicPool {

* Constructs a dynamic pool.
*
*
* @param {string|ConnectionObject} conn Connection string or object.

@@ -25,3 +23,3 @@ * @param {int} poolSize Pool size (optional).

/** @type {class} */
this.__driver = driver;
this.__driver = driver;
/** @type {Array<PooledConnection>} */

@@ -48,3 +46,3 @@ this.__available = [];

* @private
* @param {PooledConnection} connection
* @param {PooledConnection} connection
* @memberof StaticPool

@@ -102,3 +100,3 @@ */

* Grabs an available connection from the pool
*
*
* @returns {PooledConnection}

@@ -118,3 +116,3 @@ * @memberof StaticPool

* Closes the underlying connections and empties the pool
*
*
* @returns {Promise<Array<boolean>>}

@@ -121,0 +119,0 @@ * @memberof StaticPool

var SqlObject = require('./sqlObject');
var QuoteString = require('./quoteString');
var Prepare = require('./prepare');
var Join = require('./join');

@@ -10,3 +8,2 @@ var m_parent = Symbol('parent');

class Statement {

@@ -16,4 +13,3 @@ constructor(connection, sql) {

this[m_sql] = sql;
if ((sql === undefined) || (sql === null)) {
if (! sql) {
throw new Error("sql is not set");

@@ -54,6 +50,5 @@ }

/**
* Executes the SQL query. If any parameters are required, they
* will be passed to the query here.
*
* @param {any} args
* Executes the SQL query with the given arguments.
*
* @param {any} args
* @returns {Promise<array>}

@@ -65,4 +60,3 @@ * @memberof Statement

let sqlString;
if (this.preparedSql !== null) {
if (this.preparedSql) {
sqlString = this.preparedSql.map(args);

@@ -72,10 +66,5 @@ } else {

}
this.parent.__connection.query(sqlString)
.then((response) => {
resolve(response);
})
.catch((reason) => {
reject(reason);
});
.then(response => resolve(response))
.catch(reason => reject(reason));
});

@@ -85,4 +74,4 @@ }

/**
* Prepares the statement for use with parameters.
*
* Prepares the statement for use with arguments.
*
* @memberof Statement

@@ -95,6 +84,5 @@ */

/**
* Executes the SQL statement. If any parameters are required, they
* will be passed in here.
*
* @param {any} args
* Executes the SQL statement with the given arguments.
*
* @param {any} args
* @returns {Promise<any>}

@@ -104,7 +92,5 @@ * @memberof Statement

execute(... args) {
var self = this;
return new Promise((resolve, reject) => {
let sqlString;
if (this.preparedSql !== null) {
if (this.preparedSql) {
sqlString = this.preparedSql.map(args);

@@ -115,8 +101,4 @@ } else {

this.parent.__connection.execute(sqlString)
.then((result) => {
resolve(result);
})
.catch((reason) => {
reject(reason);
});
.then(result => resolve(result))
.catch(reason => reject(reason));
});

@@ -123,0 +105,0 @@ }

@@ -1,4 +0,2 @@

var Connection = require('./connection');
var PooledConnection = require('./pooledConnection');
var ParseConnection = require('./parseConnection');

@@ -12,3 +10,3 @@ var AvailablePool = {};

* Constructs a static pool.
*
*
* @param {string|ConnectionObject} conn Connection string or object.

@@ -18,3 +16,3 @@ * @param {int} poolSize Pool size (optional).

* @memberof StaticPool
*/
*/
constructor(conn, poolSize = 10, driver = undefined) {

@@ -36,3 +34,3 @@

/** @type {class} */
this.__driver = driver;
this.__driver = driver;
/** @type {Array<PooledConnection>} */

@@ -58,3 +56,3 @@ this.__available = AvailablePool[this.__url];

/**
* @param {PooledConnection} connection
* @param {PooledConnection} connection
* @private

@@ -102,3 +100,3 @@ * @memberof StaticPool

/**
* The prefered number of connections in the pool
* The preferred number of connections in the pool
* @returns {number}

@@ -114,3 +112,3 @@ * @readonly

* Grabs an available connection from the pool
*
*
* @returns {PooledConnection}

@@ -130,3 +128,3 @@ * @memberof StaticPool

* Closes the underlying connections and empties the pool
*
*
* @returns {Promise<Array<boolean>>}

@@ -133,0 +131,0 @@ * @memberof StaticPool

{
"name": "database-js",
"version": "3.0.10",
"description": "Common Database Interface",
"version": "3.0.11",
"description": "Common database interface for JavaScript",
"main": "index.js",
"scripts": {
"test": "node test.js"
"test": "echo \"Automated tests will be added soon.\""
},

@@ -53,3 +53,3 @@ "repository": {

},
"homepage": "https://github.com/mlaanderson/database-js#readme"
"homepage": "https://github.com/mlaanderson/database-js"
}

@@ -6,2 +6,3 @@ # database-js

[![Mentioned in Awesome Node.js](https://awesome.re/mentioned-badge.svg)](https://github.com/sindresorhus/awesome-nodejs)
![downloads](https://img.shields.io/npm/dw/database-js)

@@ -51,30 +52,34 @@ > Wrapper for multiple databases with a JDBC-like connection

// 👉 Change the connection URL according to the database you need to connect
// CONNECTION
var conn =
new Connection("sqlite:///path/to/test.sqlite"); // SQLite
// new Connection("mysql://user:password@localhost/test"); // MySQL
// new Connection("postgres://user:password@localhost/test"); // PostgreSQL
// new Connection( < ANOTHER URL HERE > ); // see the drivers
new Connection("sqlite:///path/to/test.sqlite"); // SQLite
// new Connection("mysql://user:password@localhost/test"); // MySQL
// new Connection("postgres://user:password@localhost/test"); // PostgreSQL
// 👉 Change the connection string according to the database driver
var statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
statement.query("South Dakota")
.then((results) => {
// QUERY
var stmt1 = conn.prepareStatement("SELECT * FROM city WHERE name = ?");
stmt1.query("New York")
.then( function (results) {
console.log(results); // Display the results
conn.close() // Close the database connection
.then(() => {
process.exit(0); // Success!
}).catch((reason) => {
console.log(reason); // Some problem when closing the connection
process.exit(1);
});
}).catch((reason) => {
} ).catch( function (reason) {
console.log(reason); // Some problem while performing the query
conn.close() // Close the connection
.then(() => {
process.exit(0); // Success!
}).catch((reason) => {
console.log(reason); // Some problem when closing the connection
process.exit(1);
});
});
} );
// COMMAND
var stmt2 = conn.prepareStatement("INSERT INTO city (name, population) VALUES (?, ?)");
stmt2.execute("Rio de Janeiro", 6747815)
.then( function() { console.log( 'Inserted.' ); } )
.catch( function(reason) { console.log('Error: ' + reason); } );
// ANOTHER COMMAND
var stmt3 = conn.prepareStatement("UPDATE city SET population = population + ? WHERE name = ?");
stmt3.execute(1, "Rio de Janeiro")
.then( function() { console.log( 'Updated.' ); } )
.catch( function(reason) { console.log('Error: ' + reason); } );
// CLOSING THE CONNECTION
conn.close()
.then( function() { console.log('Closed.'); } )
.catch( function(reason) { console.log('Error: ' + reason); } );
```

@@ -86,18 +91,31 @@

```javascript
var Connection = require('database-js').Connection;
const Connection = require('database-js').Connection;
(async function() {
let conn, statement, results;
(async () => {
let conn;
try {
conn = new Connection("sqlite:///path/to/test.sqlite"); // Just change the connection URL for a different database
statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
results = await statement.query("South Dakota");
// CONNECTION
conn = new Connection("mysql://user:password@localhost/test");
// QUERY
const stmt1 = conn.prepareStatement("SELECT * FROM city WHERE name = ?");
const results = await stmt1.query("New York");
console.log(results);
// COMMAND 1
const stmt2 = conn.prepareStatement("INSERT INTO city (name, population) VALUES (?,?)");
await stmt1.execute("Rio de Janeiro", 6747815);
// COMMAND 2
const stmt2 = conn.prepareStatement("UPDATE city SET population = population + ? WHERE name = ?");
await stmt1.execute(1, "Rio de Janeiro");
} catch (reason) {
console.log(reason);
} finally {
if (conn) {
await conn.close();
}
process.exit(0);
try {
await conn.close();
} catch (err) {
console.log(err);
}
}

@@ -107,8 +125,70 @@ })();

## Basic API
See the source code for the full API.
```ts
class Connection {
/** Creates and prepares a statement with the given SQL. */
prepareStatement(sql: string): PreparedStatement;
/** Closes the underlying connection. */
close(): Promise<void>;
/** Indicates whether the underlying driver support transactions. */
isTransactionSupported(): boolean;
/** Returns true if the underlying driver is in a transaction, false otherwise. */
inTransaction(): boolean;
/**
* Starts a transaction (if supported).
*
* Transactions can fail to start if another transaction is already running or
* if the driver does not support transactions.
*/
beginTransaction(): Promise<boolean>;
/**
* Commits a transaction (if supported).
*
* Transactions can fail to commit if no transaction was started, or if the driver
* does not support transactions.
*/
commit(): Promise<boolean>;
/**
* Cancels a transaction (if supported).
*
* Transaction can fail to be rolled back no transaction was started, or if the driver
* does not support transactions.
*/
rollback(): Promise<boolean>;
}
```
```ts
class PreparedStatement {
/**
* Performs the prepared SQL query with the given arguments.
* Returns a Promise with an array of rows.
*/
query(...args: any): Promise<Array<any>>;
/** Executes the prepared SQL statement with the given arguments. */
execute(... args): Promise<any>;
}
```
## See also
[codeceptjs-dbhelper](https://github.com/thiagodp/codeceptjs-dbhelper) - Allows to use [database-js](https://github.com/mlaanderson/database-js) inside [CodeceptJS](https://github.com/codeception/codeceptjs/) to setup tests that access databases.
- [Wiki](https://github.com/mlaanderson/database-js/wiki) for more examples and how to use a connection pool.
- [codeceptjs-dbhelper](https://github.com/thiagodp/codeceptjs-dbhelper) - Allows to use [database-js](https://github.com/mlaanderson/database-js) inside [CodeceptJS](https://github.com/codeception/codeceptjs/) tests (as a helper).
## License
[MIT](LICENSE)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc