Skip to content

Instantly share code, notes, and snippets.

@bushev
Created February 5, 2018 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bushev/03d7d6ffedcea227415d81362e0ae392 to your computer and use it in GitHub Desktop.
Save bushev/03d7d6ffedcea227415d81362e0ae392 to your computer and use it in GitHub Desktop.
NeDB - how to start

Короче сама бд и api к ней: https://github.com/louischatriot/nedb

Как юзать

Cоздаешь 2 файлика
  1. package.json:
{
  "name": "my-first-nodejs-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Tolyan",
  "license": "MIT",
  "dependencies": {
    "nedb": "1.8.0"
  }
}
  1. index.js
const NeDB = require('nedb');

const db  = {};
db.users  = new NeDB({filename: './users.db', autoload: true});
db.orders = new NeDB({filename: './orders.db', autoload: true});

// Дававить документ в бд
db.users.insert({price: 123, name: 'Sasha'}, function (err, newDoc) {
  if (err) throw err;
  
  // newDoc is the newly inserted document, including its _id
  console.log('Added: ');
  console.log(newDoc);
});

setTimeout(() => { // Просто подождем пока закончится запись

  // Поискать документ в бд
  db.users.find({name: 'Sasha'}, function (err, docs) {
    if (err) throw err;

    // Если нашел то вернет документы в массиве docs
    console.log('Found: ');
    console.log(docs);
  });
  
}, 500);

Потом в папке с файлами пишешь: npm install

И запускаешь код: node index.js

Все :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment