Nodejs连接12种数据库例子集合

美酒爱尔兰的博客 - CSDN博客

转自:https:

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. client.execute('select key from system.local', function (err, result) {

  4. console.log(result.rows[0])

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. bucket.insert('document-key', { name: 'Matt', shoeSize: 13 }, function (err, result) {

  4. var n1ql = 'SELECT d.* FROM `bucketName` d WHERE shoeSize = $1'

  5. var query = N1qlQuery.fromString(n1ql)

  6. bucket.query(query, [13], function (err, result) {

CouchDB

Module: nano Installation

$ npm install nano

Example

  1. var nano = require('nano')('http://localhost:5984')

  2. var books = nano.db.use('books')

  3. books.insert({ name: 'The Art of war' }, null, function (err, body) {

  4. books.list(function (err, body) {

LevelDB

Module: levelup Installation

$ npm install level levelup leveldown

Example

  1. var levelup = require('levelup')

  2. var db = levelup('./mydb')

  3. db.put('name', 'LevelUP', function (err) {

  4. if (err) return console.log('Ooops!', err)

  5. db.get('name', function (err, value) {

  6. if (err) return console.log('Ooops!', err)

  7. console.log('name=' + value)

MySQL

Module: mysql Installation

$ npm install mysql

Example

  1. var mysql = require('mysql')

  2. var connection = mysql.createConnection({

  3. connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {

  4. console.log('The solution is: ', rows[0].solution)

MongoDB

Module: mongodb Installation

$ npm install mongodb

Example

  1. var MongoClient = require('mongodb').MongoClient

  2. MongoClient.connect('mongodb://localhost:27017/animals', function (err, db) {

  3. db.collection('mammals').find().toArray(function (err, result) {

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. apoc.query('match (n) return n').exec().then(

PostgreSQL

Module: pg-promise Installation

$ npm install pg-promise

Example

  1. var pgp = require('pg-promise')()

  2. var db = pgp('postgres://username:password@host:port/database')

  3. db.one('SELECT $1 AS value', 123)

  4. console.log('DATA:', data.value)

  5. .catch(function (error) {

  6. console.log('ERROR:', error)

Redis

Module: redis Installation

$ npm install redis

Example

  1. var client = require('redis').createClient()

  2. client.on('error', function (err) {

  3. console.log('Error ' + err)

  4. client.set('string key', 'string val', redis.print)

  5. client.hset('hash key', 'hashtest 1', 'some value', redis.print)

  6. client.hset(['hash key', 'hashtest 2', 'some other value'], redis.print)

  7. client.hkeys('hash key', function (err, replies) {

  8. console.log(replies.length + ' replies:')

  9. replies.forEach(function (reply, i) {

  10. console.log(' ' + i + ': ' + reply)

SQL Server

Module: tedious Installation

$ npm install tedious

Example

  1. var Connection = require('tedious').Connection;

  2. var Request = require('tedious').Request;

  3. userName: 'your_username',

  4. password: 'your_password',

  5. var connection = new Connection(config);

  6. connection.on('connect', function(err) {

  7. function executeStatement() {

  8. request = new Request("select 123, 'hello world'", function(err, rowCount) {

  9. console.log(rowCount + ' rows');

  10. request.on('row', function(columns) {

  11. columns.forEach(function(column) {

  12. if (column.value === null) {

  13. console.log(column.value);

  14. connection.execSql(request);

SQLite

Module: sqlite3 Installation

$ npm install sqlite3

Example

  1. var sqlite3 = require('sqlite3').verbose()

  2. var db = new sqlite3.Database(':memory:')

  3. db.serialize(function () {

  4. db.run('CREATE TABLE lorem (info TEXT)')

  5. var stmt = db.prepare('INSERT INTO lorem VALUES (?)')

  6. for (var i = 0; i < 10; i++) {

  7. db.each('SELECT rowid AS id, info FROM lorem', function (err, row) {

  8. console.log(row.id + ': ' + row.info)

ElasticSearch

Module: elasticsearch Installation

$ npm install elasticsearch

Example

  1. var elasticsearch = require('elasticsearch')

  2. var client = elasticsearch.Client({

  3. fields: ['title', 'description']

  4. }).then(function (response) {

  5. var hits = response.hits.hits

  6. console.trace(error.message)

本文由 黑白世界4648 第一时间收藏到GET,原文来自 → blog.csdn.net

「GetParty」

关注微信号,推送好文章

微信中长按图片即可关注

更多精选文章

评论
微博一键登入