Cassandra
Module: cassandra-driver Installation
$ npm install cassandra-driver
Example
1 var cassandra = require('cassandra-driver')2 var client = new cassandra.Client({ contactPoints: ['localhost'] })3 4 client.execute('select key from system.local', function (err, result) {5 if (err) throw err6 console.log(result.rows[0])7 })
Couchbase
Module: couchnode Installation
$ npm install couchbase
Example
1 var couchbase = require('couchbase') 2 var bucket = (new couchbase.Cluster('http://localhost:8091')).openBucket('bucketName') 3 4 // add a document to a bucket 5 bucket.insert('document-key', { name: 'Matt', shoeSize: 13 }, function (err, result) { 6 if (err) { 7 console.log(err) 8 } else { 9 console.log(result)10 }11 })12 13 // get all documents with shoe size 1314 var n1ql = 'SELECT d.* FROM `bucketName` d WHERE shoeSize = $1'15 var query = N1qlQuery.fromString(n1ql)16 bucket.query(query, [13], function (err, result) {17 if (err) {18 console.log(err)19 } else {20 console.log(result)21 }22 })
CouchDB
Module: nano Installation
$ npm install nano
Example
1 var nano = require('nano')('http://localhost:5984') 2 nano.db.create('books') 3 var books = nano.db.use('books') 4 5 // Insert a book document in the books database 6 books.insert({ name: 'The Art of war' }, null, function (err, body) { 7 if (err) { 8 console.log(err) 9 } else {10 console.log(body)11 }12 })13 14 // Get a list of all books15 books.list(function (err, body) {16 if (err) {17 console.log(err)18 } else {19 console.log(body.rows)20 }21 })
LevelDB
Module: levelup Installation
$ npm install level levelup leveldown
Example
1 var levelup = require('levelup') 2 var db = levelup('./mydb') 3 4 db.put('name', 'LevelUP', function (err) { 5 if (err) return console.log('Ooops!', err) 6 7 db.get('name', function (err, value) { 8 if (err) return console.log('Ooops!', err) 9 10 console.log('name=' + value)11 })12 })
Oracle
Module: oracle Installation
$ npm install oracledb
1 var oracledb = require('oracledb'); 2 var config = { 3 user:'******', //用户名 4 password:'******', //密码 5 //IP:数据库IP地址,PORT:数据库端口,SCHEMA:数据库名称 6 connectString : "IP:PORT/SCHEMA" 7 }; 8 oracledb.getConnection( 9 config,10 function(err, connection)11 {12 if (err) {13 console.error(err.message);14 return;15 }16 //查询某表十条数据测试,注意替换你的表名17 connection.execute("SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM 你的表名) A WHERE ROWNUM <= 10 ) WHERE RN >= 0",18 function(err, result)19 {20 if (err) {21 console.error(err.message);22 doRelease(connection);23 return;24 }25 //打印返回的表结构26 console.log(result.metaData);27 //打印返回的行数据28 console.log(result.rows);29 });30 });31 32 function doRelease(connection)33 {34 connection.close(35 function(err) {36 if (err) {37 console.error(err.message);38 }39 });40 }
MySQL
Module: mysql Installation
$ npm install mysql
Example
1 var mysql = require('mysql') 2 var connection = mysql.createConnection({ 3 host : 'localhost', 4 user : 'dbuser', 5 password : 's3kreee7', 6 database : 'my_db' 7 }); 8 9 connection.connect()10 11 connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {12 if (err) throw err13 14 console.log('The solution is: ', rows[0].solution)15 })16 17 connection.end()
MongoDB
Module: mongodb Installation
$ npm install mongodb
Example
1 var MongoClient = require('mongodb').MongoClient 2 3 MongoClient.connect('mongodb://localhost:27017/animals', function (err, db) { 4 if (err) throw err 5 6 db.collection('mammals').find().toArray(function (err, result) { 7 if (err) throw err 8 9 console.log(result)10 })11 })
If you want an object model driver for MongoDB, look at Mongoose.
Neo4j
Module: apoc Installation
$ npm install apoc
Example
1 var apoc = require('apoc') 2 3 apoc.query('match (n) return n').exec().then( 4 function (response) { 5 console.log(response) 6 }, 7 function (fail) { 8 console.log(fail) 9 }10 )
PostgreSQL
Module: pg-promise Installation
$ npm install pg-promise
Example
1 var pgp = require('pg-promise')(/*options*/) 2 var db = pgp('postgres://username:password@host:port/database') 3 4 db.one('SELECT $1 AS value', 123) 5 .then(function (data) { 6 console.log('DATA:', data.value) 7 }) 8 .catch(function (error) { 9 console.log('ERROR:', error)10 })
Redis
Module: redis Installation
$ npm install redis
Example
1 var client = require('redis').createClient() 2 3 client.on('error', function (err) { 4 console.log('Error ' + err) 5 }) 6 7 client.set('string key', 'string val', redis.print) 8 client.hset('hash key', 'hashtest 1', 'some value', redis.print) 9 client.hset(['hash key', 'hashtest 2', 'some other value'], redis.print)10 11 client.hkeys('hash key', function (err, replies) {12 console.log(replies.length + ' replies:')13 14 replies.forEach(function (reply, i) {15 console.log(' ' + i + ': ' + reply)16 })17 18 client.quit()19 })
SQL Server
Module: tedious Installation
$ npm install tedious
Example
1 var Connection = require('tedious').Connection; 2 var Request = require('tedious').Request; 3 4 var config = { 5 userName: 'your_username', // update me 6 password: 'your_password', // update me 7 server: 'localhost' 8 } 9 10 var connection = new Connection(config);11 12 connection.on('connect', function(err) {13 if (err) {14 console.log(err);15 } else {16 executeStatement();17 }18 });19 20 function executeStatement() {21 request = new Request("select 123, 'hello world'", function(err, rowCount) {22 if (err) {23 console.log(err);24 } else {25 console.log(rowCount + ' rows');26 }27 connection.close();28 });29 30 request.on('row', function(columns) {31 columns.forEach(function(column) {32 if (column.value === null) {33 console.log('NULL');34 } else {35 console.log(column.value);36 }37 });38 });39 40 connection.execSql(request);41 }
SQLite
Module: sqlite3 Installation
$ npm install sqlite3
Example
1 var sqlite3 = require('sqlite3').verbose() 2 var db = new sqlite3.Database(':memory:') 3 4 db.serialize(function () { 5 db.run('CREATE TABLE lorem (info TEXT)') 6 var stmt = db.prepare('INSERT INTO lorem VALUES (?)') 7 8 for (var i = 0; i < 10; i++) { 9 stmt.run('Ipsum ' + i)10 }11 12 stmt.finalize()13 14 db.each('SELECT rowid AS id, info FROM lorem', function (err, row) {15 console.log(row.id + ': ' + row.info)16 })17 })18 19 db.close()
ElasticSearch
Module: elasticsearch Installation
$ npm install elasticsearch
Example
1 var elasticsearch = require('elasticsearch') 2 var client = elasticsearch.Client({ 3 host: 'localhost:9200' 4 }) 5 6 client.search({ 7 index: 'books', 8 type: 'book', 9 body: {10 query: {11 multi_match: {12 query: 'express js',13 fields: ['title', 'description']14 }15 }16 }17 }).then(function (response) {18 var hits = response.hits.hits19 }, function (error) {20 console.trace(error.message)21 }) 来自 express 框架里面的文档
转自 https://blog.csdn.net/m0_37450089/article/details/78727782