Skip to content

Commit 4c69831

Browse files
committed
converted to file based db
1 parent ede0dbf commit 4c69831

File tree

4 files changed

+40
-108
lines changed

4 files changed

+40
-108
lines changed

data/.gitkeep

Whitespace-only changes.

models/portfolio.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const tradeConfig = require("../config/trading").config;
22

33
module.exports = {
4+
name: "portfolio",
45
create: (id) => {
56
return {
67
_id: id,
78
active: true,
8-
live: false,
9+
live: tradeConfig.live,
910
balance: tradeConfig.balance,
1011
positions: {},
1112
pending: {},

seed.js

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,7 @@ const manifest = {
1010
databases: [
1111
common.db.portfolios,
1212
common.db.trades
13-
],
14-
views: {
15-
[common.db.trades]: {
16-
all: {
17-
map: function(doc) { emit(doc._id.split("x").shift(), null); }
18-
},
19-
sold: {
20-
map: function(doc) { if (doc.profit.amount) { emit(doc._id.split("x").shift(), null); } }
21-
}
22-
},
23-
[common.db.users]: {
24-
all_enabled: {
25-
map: function(doc) { if (doc.enabled === true) { emit(doc._id, doc.priority); } }
26-
}
27-
}
28-
29-
}
13+
]
3014
};
3115

3216
co(function* co() {
@@ -37,19 +21,21 @@ co(function* co() {
3721
yield createDatabase(db);
3822
}
3923

40-
common.log("info", ".:. Creating Views .:.");
41-
for (const key in manifest.views) {
42-
// creates the view
43-
yield createView(key);
24+
common.log("info", ".:. Seeding Trading Portfolio .:.");
25+
try {
26+
yield db.removeDocument(portfolioModel.name, common.db.portfolios);
27+
common.log("info", `* Document '${portfolioModel.name}' deleted!`);
28+
} catch (err) {
29+
throw new Error(err.stack);
30+
common.log("warn", `* Deleting '${portfolioModel.name}' failed!`);
4431
}
4532

46-
common.log("info", ".:. Seeding Trading Portfolio .:.");
4733
try {
48-
const portfolio = portfolioModel.create("portfolio");
34+
const portfolio = portfolioModel.create(portfolioModel.name);
4935
yield db.saveDocument(portfolio, common.db.portfolios);
50-
common.log("info", "* Document 'portfolio' created!");
36+
common.log("info", `* Document '${portfolioModel.name}' created!`);
5137
} catch (err) {
52-
common.log("warn", "! Seeding 'portfolio' failed");
38+
common.log("warn", `* Saving '${portfolioModel.name}' failed!`);
5339
}
5440
common.log("info", ".:. Seeding Complete! .:.");
5541
}).catch((err) => {
@@ -67,12 +53,3 @@ function* createDatabase(name) {
6753
}
6854
return confirmation;
6955
}
70-
71-
function* createView(name) {
72-
const result = yield db.saveView("listing", manifest.views[name], `${name}`);
73-
if (result.error === true) {
74-
common.log("warn", `! View '${name}' creation failed!`);
75-
} else {
76-
common.log("info", `* View '${name}' created!`);
77-
}
78-
}

services/db.js

Lines changed: 27 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
const co = require("co");
22
const Promise = require("bluebird");
3-
const cradle = Promise.promisifyAll(require("cradle"));
4-
5-
// A custom Error just for database problems.
6-
function CouchDBError(message) {
7-
this.name = "CouchDBError";
8-
this.message = (message || "");
9-
}
10-
CouchDBError.prototype = Error.prototype;
11-
12-
// Connects to a database and returns the DB object.
13-
const connectToDatabase = (dbName) => {
14-
try {
15-
return new(cradle.Connection)().database(dbName);
16-
} catch (err) {
17-
throw new CouchDBError(`DB: Get: Connection to database [${dbName}] failed`);
18-
}
19-
};
3+
const fs = Promise.promisifyAll(require("fs"));
204

215
exports.createDatabase = function* createDatabase(database) {
226
try {
23-
const db = connectToDatabase(database);
24-
const confirmation = yield db.createAsync();
25-
confirmation.error = false;
7+
yield fs.mkdirAsync(`${__dirname}/../data/${database}`);
8+
const confirmation = {
9+
error: false
10+
};
2611
return confirmation;
2712
} catch (err) {
2813
return {
@@ -34,9 +19,10 @@ exports.createDatabase = function* createDatabase(database) {
3419

3520
exports.deleteDatabase = function* deleteDatabase(database) {
3621
try {
37-
const db = connectToDatabase(database);
38-
const confirmation = yield db.destroyAsync();
39-
confirmation.error = false;
22+
yield fs.rmdirAsync(`${__dirname}/../data/${database}`);
23+
const confirmation = {
24+
error: false
25+
};
4026
return confirmation;
4127
} catch (err) {
4228
return {
@@ -49,80 +35,48 @@ exports.deleteDatabase = function* deleteDatabase(database) {
4935
// Grabs a document from a database in CouchDB.
5036
exports.getDocument = function* getDocument(id, database) {
5137
try {
52-
const db = connectToDatabase(database);
53-
const doc = yield db.getAsync(id);
54-
doc.error = false;
55-
return doc;
38+
const file = yield fs.readFileAsync(`${__dirname}/../data/${database}/${id}.json`);
39+
const result = {
40+
error: false,
41+
data: JSON.parse(file)
42+
};
43+
return result;
5644
} catch (err) {
5745
return {
5846
error: true,
59-
message: `DB: Get of [${id}] failed`
47+
message: `DB: Get of [${database}/${id}] failed`
6048
};
6149
}
6250
};
6351

6452
// Saves a document in a database in CouchDB.
6553
exports.saveDocument = function* saveDocument(document, database) {
6654
try {
67-
const db = connectToDatabase(database);
68-
const returnVal = yield db.saveAsync(document._id, document);
69-
document.error = false;
70-
return document;
55+
yield fs.writeFileAsync(`${__dirname}/../data/${database}/${document._id}.json`, JSON.stringify(document, null, 2));
56+
const confirmation = {
57+
error: false
58+
};
59+
return confirmation;
7160
} catch (err) {
72-
throw new Error(err.stack);
7361
return {
7462
error: true,
75-
message: `DB: Save of [${document._id}] failed`
63+
message: `DB: Save of [${database}/${id}] failed`
7664
};
7765
}
7866
};
7967

8068
// Removes a document in a database in CouchDB.
8169
exports.removeDocument = function* removeDocument(id, database) {
8270
try {
83-
const db = connectToDatabase(database);
84-
const returnVal = yield db.removeAsync(id);
85-
returnVal.error = false;
86-
return returnVal;
87-
} catch (err) {
88-
return {
89-
error: true,
90-
message: `DB: Delete of [${id}] failed`
91-
};
92-
}
93-
};
94-
95-
// Gets a view from a database in CouchDB.
96-
exports.runView = function* runView(path, key, database, opts = {}) {
97-
try {
98-
const db = connectToDatabase(database);
99-
const returnVal = {};
100-
if (key !== null) {
101-
opts.key = key;
102-
}
103-
returnVal.results = yield db.viewAsync(path, opts);
104-
returnVal.error = false;
105-
return returnVal;
106-
} catch (err) {
107-
return {
108-
error: true,
109-
message: `DB: View of [${path}] failed`
71+
yield fs.unlinkAsync(`${__dirname}/../data/${database}/${id}.json`);
72+
const confirmation = {
73+
error: false
11074
};
111-
}
112-
};
113-
114-
// Saves a view to a database in CouchDB
115-
// Saves a document in a database in CouchDB.
116-
exports.saveView = function* saveView(id, view, database) {
117-
try {
118-
const db = connectToDatabase(database);
119-
const document = yield db.saveAsync(`_design/${id}`, view);
120-
document.error = false;
121-
return document;
75+
return confirmation;
12276
} catch (err) {
12377
return {
12478
error: true,
125-
message: `DB: Save of [${id}] view failed`
79+
message: `DB: Delete of [${database}/${id}] failed`
12680
};
12781
}
12882
};

0 commit comments

Comments
 (0)