Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Common Database Interface for Node

License

NotificationsYou must be signed in to change notification settings

mlaanderson/database-js

Repository files navigation

Build Statusnpm versionMentioned in Awesome Node.jsdownloads

Wrapper for multiple databases with a JDBC-like connection

Database-js implements a common, promise-based interface for SQL database access. Inspired by JDBC, it uses connection strings to identify the database driver. Wrappers around native database drivers provide a unified interface to handle databases. Thus, you can change the target database by modifying the connection string. 😉

Database-js has built-in prepared statements, even if the underlying driver does not support them. It is built on Promises, so it works well with ES7 async code.

Contents

Install

npm install database-js

Drivers

Driver (wrapper)NoteInstallation
ActiveX Data ObjectsWindows onlynpm i database-js-adodb
CSV filesnpm i database-js-csv
Excel filesnpm i database-js-xlsx
Firebasenpm i database-js-firebase
INI filesnpm i database-js-ini
JSON filesnpm i database-js-json
MySQLprior to MySQL v8npm i database-js-mysql
MySQL2MySQL v8+npm i database-js-mysql2
MS SQL Servernpm i database-js-mssql
PostgreSQLnpm i database-js-postgres
SQLite3npm i database-js-sqlite3
SQLitenpm i database-js-sqlite

See here how to add a new driver.

Usage

Usagewithout async/await:

varConnection=require('database-js').Connection;// CONNECTIONvarconn=newConnection("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// QUERYvarstmt1=conn.prepareStatement("SELECT * FROM city WHERE name = ?");stmt1.query("New York").then(function(results){console.log(results);// Display the results}).catch(function(reason){console.log(reason);// Some problem while performing the query});// COMMANDvarstmt2=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 COMMANDvarstmt3=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 CONNECTIONconn.close().then(function(){console.log('Closed.');}).catch(function(reason){console.log('Error: '+reason);});

Async / await

Using async/await:

constConnection=require('database-js').Connection;(async()=>{letconn;try{// CONNECTIONconn=newConnection('mysql://user:password@localhost/test');// QUERYconststmt1=conn.prepareStatement('SELECT * FROM city WHERE name = ?');constresults=awaitstmt1.query('New York');console.log(results);// COMMAND 1conststmt2=conn.prepareStatement('INSERT INTO city (name, population) VALUES (?,?)');awaitstmt1.execute('Rio de Janeiro',6747815);// COMMAND 2conststmt2=conn.prepareStatement('UPDATE city SET population = population + ? WHERE name = ?');awaitstmt1.execute(1,'Rio de Janeiro');}catch(reason){console.log(reason);}finally{try{awaitconn.close();}catch(err){console.log(err);}}})();

Basic API

classConnection{/** 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>;}
classPreparedStatement{/** * 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

License

MIT

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp