Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nedb as a simple persistence mechanism #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
.devcontainer
.devcontainer
contacts.json
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:9-alpine

WORKDIR / app

COPY package.json .
COPY package-lock.json .

RUN npm install

COPY index.js .

EXPOSE 3000

CMD npm start
29 changes: 20 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
//Declaracion de dependencias:
var express=require('express');
var bodyParser=require('body-parser');
var DataStore=require('nedb');
var Datastore=require('nedb');
var port= 3000;
var BASE_API_PATH="/api/v1";

var DB_FILE_NAME=__dirname+"/contacts.json";


console.log("Starting API Server...");



var app= express();
app.use(bodyParser.json());

var db= new DataStore({
filename:DB_FILE_NAME,
autoload: true
})
//Inicializamos la base de datos:
db = new Datastore({
filename: DB_FILE_NAME, //nombre del fichero que hemos definido anteriormente
autoload: true
});


app.get("/", (req,res)=>{

Expand All @@ -26,12 +31,18 @@ app.get("/", (req,res)=>{
app.get(BASE_API_PATH+"/contacts", (req,res)=>{

console.log(Date()+"- GET/contacts");
db.find({}, (err,contacts)=>{

db.find({}, (err,contacts)=>{//hace una consulta de cualquier elemento gracias a {}
if(err){
console.log(Date()+"-"+err)
res.sendStatus(500)
}else{
res.send(contacts);
res.send(contacts.map((contact)=>{

delete contact._id;
return contact;
})
);
}

});
Expand All @@ -42,7 +53,7 @@ app.post(BASE_API_PATH+"/contacts", (req,res)=>{
console.log(Date()+"- POST/contacts");
var contact=req.body;

db.insert(contact,(err)=>{
db.insert(contact,(err)=>{ //insertamos un nuevo elemento en la base
if(err){
console.log(Date()+"-"+ err);
res.sendStatus(500);
Expand Down
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
"main": "index.js",
"repository": {
"type": "git",
"url":"https://github.com/kapy95/repositorio.git"
"url": "https://github.com/kapy95/repositorio.git"
},
"homepage":"https://github.com/kapy95/repositorio",
"homepage": "https://github.com/kapy95/repositorio",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Carlos",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
"express": "^4.17.1",
"nedb": "^1.8.0"
}
}