better id generation for builds
This commit is contained in:
parent
47821b4bf0
commit
ba62e2d7b4
79
db.js
79
db.js
@ -1,6 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var nlevel = require('nlevel'),
|
var Steppy = require('twostep').Steppy,
|
||||||
|
_ = require('underscore'),
|
||||||
|
nlevel = require('nlevel'),
|
||||||
ldb = nlevel.db('path/to/db/ignored/for/memdown', {
|
ldb = nlevel.db('path/to/db/ignored/for/memdown', {
|
||||||
db: require('memdown'),
|
db: require('memdown'),
|
||||||
valueEncoding: 'json'
|
valueEncoding: 'json'
|
||||||
@ -14,42 +16,55 @@ exports.builds = new nlevel.DocsSection(ldb, 'builds', {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
exports.builds.idGenerator = getNextId;
|
exports.builds._beforePut = function(docs, callback) {
|
||||||
|
generateIds(this, docs, callback);
|
||||||
// TODO: move to nlevel
|
|
||||||
var superPut = nlevel.DocsSection.prototype.put;
|
|
||||||
nlevel.DocsSection.prototype.put = function(docs, callback) {
|
|
||||||
var self = this;
|
|
||||||
if (!Array.isArray(docs)) docs = [docs];
|
|
||||||
if (this.idGenerator && docs[0] && 'id' in docs[0] === false) {
|
|
||||||
if (docs.every(function(doc) { return 'id' in doc === false; })) {
|
|
||||||
this.idGenerator(function(err, id) {
|
|
||||||
if (err) return callback(err);
|
|
||||||
docs.forEach(function(doc) {
|
|
||||||
doc.id = id;
|
|
||||||
id++;
|
|
||||||
});
|
|
||||||
superPut.call(self, docs, callback);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return callback(new Error(
|
|
||||||
'Documents with id and without should not be ' +
|
|
||||||
'mixed on put when id generator is set'
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return superPut.call(this, docs, callback);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function getNextId(callback) {
|
function generateIds(section, docs, callback) {
|
||||||
this.find({
|
Steppy(
|
||||||
start: {createDate: ''}, limit: 1, reverse: true
|
function() {
|
||||||
}, function(err, docs) {
|
if (isAllDocsWithId(docs)) {
|
||||||
callback(err, !err && docs[0] && ++docs[0].id || 1);
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
var mixedIdsError = checkForMixedIds(docs);
|
||||||
|
if (mixedIdsError) throw mixedIdsError;
|
||||||
|
|
||||||
|
section.find({
|
||||||
|
start: {createDate: ''}, limit: 1, reverse: true
|
||||||
|
}, this.slot());
|
||||||
|
},
|
||||||
|
function(err, lastDocs) {
|
||||||
|
var id = lastDocs[0] && ++lastDocs[0].id || 1;
|
||||||
|
|
||||||
|
_(docs).each(function(doc) {
|
||||||
|
doc.id = id;
|
||||||
|
id++;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.pass(null);
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAllDocsWithId(docs) {
|
||||||
|
return _(docs).all(function(doc) {
|
||||||
|
return 'id' in doc;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkForMixedIds(docs) {
|
||||||
|
var isAllWithoutId = _(docs).all(function(doc) {
|
||||||
|
return 'id' in doc === false;
|
||||||
|
});
|
||||||
|
if (!isAllWithoutId) {
|
||||||
|
return new Error(
|
||||||
|
'Documents with id and without should not be mixed'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function pickId(doc) {
|
function pickId(doc) {
|
||||||
return {id: doc.id};
|
return {id: doc.id};
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"data.io": "0.3.0",
|
"data.io": "0.3.0",
|
||||||
"jade": "1.9.2",
|
"jade": "1.9.2",
|
||||||
"nlevel": "1.0.0",
|
"nlevel": "1.0.1",
|
||||||
"node-static": "0.7.6",
|
"node-static": "0.7.6",
|
||||||
"socket.io": "1.3.5",
|
"socket.io": "1.3.5",
|
||||||
"twostep": "0.4.1",
|
"twostep": "0.4.1",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user