Skip to content
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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
*.log
.idea
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# RAML Store



## Overview

raml-store provides a simple storage API plus a persistence plugin which enables you to run the [RAML API Designer](https://github.com/mulesoft/api-designer) locally (rather than use the APIHub cloud service) and still be able to manage and collaborate on your design.
Expand All @@ -19,25 +17,14 @@ To start mongodb as background process:

`cd /usr/local/mongodb` (mongodb installation directory)


`mongod --fork --logpath /var/log/mongodb.log`


### Installing MongoDB Node.js Driver
From the top-level directory (e.g. raml-store):

`npm install mongodb`

### Installing Express
### Installing Express and MongoDB Node.js Driver
From the top-level directory (e.g. raml-store):

`npm install `







## Running the Service
From the top-level directory (e.g. raml-store):

Expand Down
127 changes: 127 additions & 0 deletions lib/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
var mongo = require('mongodb');

var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('ramldb', server);

db.open(function (err, db) {
if (!err) {
console.log("Connected to 'ramldb' database");
db.collection('files', {strict: true}, function (err, collection) {
if (err) {
console.log("The 'files' collection doesn't exist. Use POST to add RAML files...");
populateDB();
}
});
}
});

exports.findById = function (req, res) {
var id = req.params.id;
console.log('Retrieving file: ' + id);
db.collection('files', function (err, collection) {
collection.findOne({'_id': new BSON.ObjectID(id)}, function (err, item) {
delete item._id;
res.header("Access-Control-Allow-Origin", "*");
res.send(item);
});
});
};

exports.findAll = function (req, res) {
var filelist = new Object();
db.collection('files', function (err, collection) {
collection.find({}, function (err, resultCursor) {
resultCursor.each(function (err, item) {
if (item != null) {
console.log('Item : ' + item._id + ' : ' + JSON.stringify(item));
filelist[item._id] = item;
delete filelist[item._id]._id;
console.log(JSON.stringify(filelist));
}
else {
res.header("Access-Control-Allow-Origin", "*");
res.send(JSON.stringify(filelist));
}
});
});
});

};

exports.addFile = function (req, res) {
var file = req.body;
console.log('Adding file : ' + JSON.stringify(file));

db.collection('files', function (err, collection) {
collection.insert(file, {safe: true}, function (err, result) {
if (err) {
res.send({'error': 'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.header("Access-Control-Allow-Origin", "*");
res.send(result[0]);
}
});
});
}

exports.updateFile = function (req, res) {
var id = req.params.id;
var file = req.body;
console.log('Updating file: ' + id);
console.log(JSON.stringify(file));

db.collection('files', function (err, collection) {
collection.update({'_id': new BSON.ObjectID(id)}, file, {safe: true}, function (err, result) {
if (err) {
console.log('Error updating file : ' + err);
res.send({'error': 'An error has occurred'});
} else {
console.log('' + result + ' document(s) updated');
res.header("Access-Control-Allow-Origin", "*");
res.send('{"status":"success","id":"' + id + '","message":"The file was successfully updated."}');
}
});
});
}

exports.deleteFile = function (req, res) {
var id = req.params.id;
console.log('Deleting file: ' + id);
db.collection('files', function (err, collection) {
collection.remove({'_id': new BSON.ObjectID(id)}, {safe: true}, function (err, result) {
if (err) {
res.send({'error': 'An error has occurred - ' + err});
} else {
console.log('' + result + ' document(s) deleted');
res.send(req.body);
}
});
});


}

/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateDB = function () {

var files = [
{
name: "ExampleRAML",
path: "%2F",
contents: "gggggg"
}
];

db.collection('files', function (err, collection) {
collection.insert(files, {safe: true}, function (err, result) {
});
});

};
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "raml-files",
"description": "Remote RAML File Store API",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x"
}
"name": "raml-files",
"description": "Remote RAML File Store API",
"version": "0.0.2",
"private": true,
"dependencies": {
"express": "3.x",
"mongodb": "1.x"
}
}
125 changes: 0 additions & 125 deletions routes/files.js

This file was deleted.

Loading