{"id":13454852,"url":"https://github.com/mlaanderson/database-js","last_synced_at":"2025-04-09T10:09:29.267Z","repository":{"id":44930308,"uuid":"97738515","full_name":"mlaanderson/database-js","owner":"mlaanderson","description":"Common Database Interface for Node","archived":false,"fork":false,"pushed_at":"2024-07-07T01:30:00.000Z","size":119,"stargazers_count":76,"open_issues_count":3,"forks_count":16,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-02T09:07:47.719Z","etag":null,"topics":["database","excel","firebase","ini","javascript","javascript-library","json","msaccess","mysql","node","node-module","postgresql","sql","sqlite","wrapper"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mlaanderson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-19T16:28:44.000Z","updated_at":"2025-03-22T17:31:40.000Z","dependencies_parsed_at":"2024-10-11T08:00:52.350Z","dependency_job_id":"38c82cea-5f80-4e43-b15b-a7e1ea0e0d52","html_url":"https://github.com/mlaanderson/database-js","commit_stats":{"total_commits":129,"total_committers":10,"mean_commits":12.9,"dds":0.6666666666666667,"last_synced_commit":"2b08710ce6031b25a2ab982f59f2bd9a1f65a137"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlaanderson%2Fdatabase-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlaanderson%2Fdatabase-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlaanderson%2Fdatabase-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlaanderson%2Fdatabase-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mlaanderson","download_url":"https://codeload.github.com/mlaanderson/database-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018061,"owners_count":21034048,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","excel","firebase","ini","javascript","javascript-library","json","msaccess","mysql","node","node-module","postgresql","sql","sqlite","wrapper"],"created_at":"2024-07-31T08:00:58.561Z","updated_at":"2025-04-09T10:09:29.243Z","avatar_url":"https://github.com/mlaanderson.png","language":"JavaScript","readme":"# database-js\n\n[![Build Status](https://travis-ci.org/mlaanderson/database-js.svg?branch=master)](https://travis-ci.org/mlaanderson/database-js)\n[![npm version](https://badge.fury.io/js/database-js.svg)](https://badge.fury.io/js/database-js)\n[![Mentioned in Awesome Node.js](https://awesome.re/mentioned-badge.svg)](https://github.com/sindresorhus/awesome-nodejs)\n![downloads](https://img.shields.io/npm/dw/database-js)\n\n\u003e Wrapper for multiple databases with a JDBC-like connection\n\nDatabase-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. 😉\n\nDatabase-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.\n\n## Contents\n\n* [Install](#install)\n* [Usage](#usage)\n* [Examples](//github.com/mlaanderson/database-js/wiki/Examples)\n* [API](//github.com/mlaanderson/database-js/wiki/API)\n* [Drivers](//github.com/mlaanderson/database-js/wiki/Drivers)\n* [In the Browser](//github.com/mlaanderson/database-js/wiki/Browsers)\n\n## Install\n\n```shell\nnpm install database-js\n```\n\n## Drivers\n\n| Driver (wrapper) | Note | Installation |\n| ---------------- | ---- | ------------ |\n| [ActiveX Data Objects](//github.com/mlaanderson/database-js-adodb) | *Windows only* | `npm i database-js-adodb` |\n| [CSV files](//github.com/mlaanderson/database-js-csv) | | `npm i database-js-csv` |\n| [Excel files](//github.com/mlaanderson/database-js-xlsx) | | `npm i database-js-xlsx` |\n| [Firebase](//github.com/mlaanderson/database-js-firebase) | | `npm i database-js-firebase` |\n| [INI files](//github.com/mlaanderson/database-js-ini) | | `npm i database-js-ini` |\n| [JSON files](//github.com/thiagodp/database-js-json) | | `npm i database-js-json` |\n| [MySQL](//github.com/mlaanderson/database-js-mysql) | prior to MySQL v8 | `npm i database-js-mysql` |\n| [MySQL2](//github.com/esteban-serfe/database-js-mysql2/) | MySQL v8+ | `npm i database-js-mysql2` |\n| [MS SQL Server](https://github.com/thiagodp/database-js-mssql) | | `npm i database-js-mssql` |\n| [PostgreSQL](//github.com/mlaanderson/database-js-postgres) | | `npm i database-js-postgres` |\n| [SQLite3](//github.com/thiagodp/database-js-sqlite3) | | `npm i database-js-sqlite3` |\n| [SQLite](//github.com/mlaanderson/database-js-sqlite) | | `npm i database-js-sqlite` |\n\n[See here](//github.com/mlaanderson/database-js/wiki/Drivers#implementing-a-new-driver) how to add a new driver.\n\n## Usage\n\nUsage _without_ async/await:\n\n```javascript\nvar Connection = require('database-js').Connection;\n\n// CONNECTION\nvar conn =\n\tnew Connection(\"sqlite:///path/to/test.sqlite\");               // SQLite\n\t// new Connection(\"mysql://user:password@localhost/test\");     // MySQL\n\t// new Connection(\"postgres://user:password@localhost/test\");  // PostgreSQL\n\t// 👉 Change the connection string according to the database driver\n\n// QUERY\nvar stmt1 = conn.prepareStatement(\"SELECT * FROM city WHERE name = ?\");\nstmt1.query(\"New York\")\n\t.then( function (results) {\n\t\tconsole.log(results); // Display the results\n\t} ).catch( function (reason) {\n\t\tconsole.log(reason); // Some problem while performing the query\n\t} );\n\n// COMMAND\nvar stmt2 = conn.prepareStatement(\"INSERT INTO city (name, population) VALUES (?, ?)\");\nstmt2.execute(\"Rio de Janeiro\", 6747815)\n\t.then( function() { console.log( 'Inserted.' ); } )\n\t.catch( function(reason) { console.log('Error: ' + reason); } );\n\n// ANOTHER COMMAND\nvar stmt3 = conn.prepareStatement(\"UPDATE city SET population = population + ? WHERE name = ?\");\nstmt3.execute(1, \"Rio de Janeiro\")\n\t.then( function() { console.log( 'Updated.' ); } )\n\t.catch( function(reason) { console.log('Error: ' + reason); } );\n\n// CLOSING THE CONNECTION\nconn.close()\n\t.then( function() { console.log('Closed.'); } )\n\t.catch( function(reason) { console.log('Error: ' + reason); } );\n```\n\n### Async / await\n\nUsing async/await:\n\n```javascript\nconst Connection = require('database-js').Connection;\n\n(async () =\u003e {\n\tlet conn;\n\ttry {\n\t\t// CONNECTION\n\t\tconn = new Connection('mysql://user:password@localhost/test');\n\n\t\t// QUERY\n\t\tconst stmt1 = conn.prepareStatement('SELECT * FROM city WHERE name = ?');\n\t\tconst results = await stmt1.query('New York');\n\t\tconsole.log(results);\n\n\t\t// COMMAND 1\n\t\tconst stmt2 = conn.prepareStatement('INSERT INTO city (name, population) VALUES (?,?)');\n\t\tawait stmt1.execute('Rio de Janeiro', 6747815);\n\n\t\t// COMMAND 2\n\t\tconst stmt2 = conn.prepareStatement('UPDATE city SET population = population + ? WHERE name = ?');\n\t\tawait stmt1.execute(1, 'Rio de Janeiro');\n\t} catch (reason) {\n\t\tconsole.log(reason);\n\t} finally {\n\t\ttry {\n\t\t\tawait conn.close();\n\t\t} catch (err) {\n\t\t\tconsole.log(err);\n\t\t}\n\t}\n})();\n```\n\n## Basic API\n\n```ts\nclass Connection {\n\n\t/** Creates and prepares a statement with the given SQL. */\n\tprepareStatement(sql: string): PreparedStatement;\n\n\t/** Closes the underlying connection. */\n\tclose(): Promise\u003cvoid\u003e;\n\n\t/** Indicates whether the underlying driver support transactions. */\n\tisTransactionSupported(): boolean;\n\n\t/** Returns true if the underlying driver is in a transaction, false otherwise. */\n\tinTransaction(): boolean;\n\n\t/**\n\t * Starts a transaction (if supported).\n\t *\n\t * Transactions can fail to start if another transaction is already running or\n\t * if the driver does not support transactions.\n\t */\n\tbeginTransaction(): Promise\u003cboolean\u003e;\n\n\t/**\n\t * Commits a transaction (if supported).\n\t *\n\t * Transactions can fail to commit if no transaction was started, or if the driver\n\t * does not support transactions.\n\t */\n\tcommit(): Promise\u003cboolean\u003e;\n\n\t/**\n\t * Cancels a transaction (if supported).\n\t *\n\t * Transaction can fail to be rolled back no transaction was started, or if the driver\n\t * does not support transactions.\n\t */\n\trollback(): Promise\u003cboolean\u003e;\n}\n```\n\n```ts\nclass PreparedStatement {\n\t/**\n\t * Performs the prepared SQL query with the given arguments.\n\t * Returns a Promise with an array of rows.\n\t */\n\tquery(...args: any): Promise\u003cArray\u003cany\u003e\u003e;\n\n\t/** Executes the prepared SQL statement with the given arguments. */\n\texecute(... args): Promise\u003cany\u003e;\n}\n```\n\n\n## See also\n\n- [Wiki](https://github.com/mlaanderson/database-js/wiki) for more examples and how to use a connection pool.\n\n- [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).\n\n\n## License\n\n[MIT](LICENSE)\n","funding_links":[],"categories":["Packages","Repository","包","目录","Database","sqlite"],"sub_categories":["Database","数据库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmlaanderson%2Fdatabase-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmlaanderson%2Fdatabase-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmlaanderson%2Fdatabase-js/lists"}