Horváth, Győző
senior lecturer
horvath.gyozo@inf.elte.hu
Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government
fs.readFile()
fs.writeFile()
fs.readFileSync()
fs.writeFileSync()
"Embedded datastore for node.js"
npm install --save nedb
var Datastore = require('nedb'),
db = new Datastore({ filename: 'path/to/datafile', autoload: true });
//Inserting document(s)
db.insert(doc, function (err, newDoc) { // Callback is optional
// newDoc is the newly inserted document, including its _id
// newDoc has no key called notToBeSaved since its value was undefined
});
//Finding document(s)
db.find({ foo: 'bar' }, function (err, docs) {
// docs is an array containing documents Mars, Earth, Jupiter
// If no document is found, docs is equal to []
});
db.findOne({ _id: 'id1' }, function (err, doc) {
// doc is the document Mars
// If no document is found, doc is null
});
//Replacing a document
db.update({ planet: 'Jupiter' }, { planet: 'Pluton'}, {}, function (err, numReplaced) {
// numReplaced = 1
// The doc #3 has been replaced by { _id: 'id3', planet: 'Pluton' }
// Note that the _id is kept unchanged, and the document has been replaced
});
//Deleting a document
db.remove({ _id: 'id2' }, {}, function (err, numRemoved) {
// numRemoved = 1
});
NoSQL database management system
Document-based
JSON/BSON documents
_id
primary keyServer
<path to>\mongod.exe --dbpath "d:\test\mongo db data"
Client
<path to>\mongo.exe
> help
> use <dbname>
> db.<collection>.insert({ "apple" : "red" })
> db.<collection>.find()
Installation
npm install --save mongodb
Usage (e.g. in a REST API)
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var db;
MongoClient.connect('mongodb://localhost:27017/snippets', function (err, database) {
if (err) throw err;
db = database;
console.log('Database connected');
});
router
.get('/', function(req, res, next) {
db.collection('snippets').find().toArray(function (err, docs) {
res.json(docs);
})
})
.post('/', function(req, res) {
db.collection('snippets').insert(req.body);
res.end();
});
module.exports = router;
Schema --> Model --> Instance
↓ ↓
collection document
find
, remove
, update
save
, validate
Rewrite the MongoClient example using Mongoose!
var SnippetSchema = new mongoose.Schema({
title: String,
code: String,
type: String,
date: {type: Date, default: Date.now }
});
var SnippetModel = mongoose.model('Snippet', SnippetSchema);
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/snippets');
var db = mongoose.connection;
var Snippet = require('../models/snippet');
router
.get('/', function(req, res, next) {
Snippet.find({}, function (err, docs) {
res.json(docs);
});
})
.post('/', function(req, res) {
var snippet = new Snippet(req.body);
snippet.save();
res.json(snippet);
})
.put('/:id', function (req, res) {
var id = req.params.id;
var doc = req.body;
Snippet.findByIdAndUpdate(id, doc, function (err) {
res.json(doc);
});
})
.delete('/:id', function (req, res) {
var id = req.params.id;
Snippet.findByIdAndRemove(id, function(err, doc) {
res.json(doc);
})
});
localStorage
Collection of key-value pairs (object)
Synchronous API
//Storing/writing
localStorage.apple = 'red';
localStorage['anything'] = 42;
//Taking out/reading (even after reloading)
console.log(localStorage.apple);
console.log(localStorage['anything']);
String format is preferred
//Storing a complex data structure
var x = [1, 3, 5, 7];
localStorage.setItem('data', JSON.stringify(x));
var x2 = JSON.parse( localStorage.getItem('data') );
// Importing
import LocalStorage from 'backbone.localstorage';
// Collections
var SomeCollection = Backbone.Collection.extend({
localStorage: new LocalStorage("SomeCollection"),
// ...
});
Save the data to the browser in the code snippet application using localStorage (there is no need for fixtures anymore)!
return Backbone.Collection.extend({
localStorage: new Backbone.LocalStorage("CodeSnippets"),
model: CodeSnippet,
// ...
});