Created
March 29, 2016 07:40
-
-
Save evilj0e/298ea4fb8e17a4b38fef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const multivarka = require('./multivarka'); | |
/** | |
* Получаем соединение | |
* @returns {Collection|MultivarkaConnection|*} | |
* @private | |
*/ | |
function _getMultivarka() { | |
var connection = connection || multivarka | |
.server('mongodb://localhost/testdb') | |
.collection('webdev2'); | |
return connection; | |
} | |
/** | |
* Делаем простой find | |
* @private | |
*/ | |
function _runFindOperation() { | |
_getMultivarka() | |
.find(function (err, data) { | |
console.log('show all ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Делаем find с equal | |
* @private | |
*/ | |
function _runEqualOperation() { | |
_getMultivarka() | |
.where('name') | |
.equal('Пётр1') | |
.find(function (err, data) { | |
console.log('equal ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Делаем find с lessThan | |
* @private | |
*/ | |
function _runLessThanOperation() { | |
_getMultivarka() | |
.where('grade') | |
.lessThan(2) | |
.find(function (err, data) { | |
console.log('less than ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Делаем find с greatThan | |
* @private | |
*/ | |
function _runGreatThanOperation() { | |
_getMultivarka() | |
.where('grade') | |
.greatThan(4) | |
.find(function (err, data) { | |
console.log('great than ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Делаем find с include | |
* @private | |
*/ | |
function include() { | |
_getMultivarka() | |
.where('group') | |
.include(['200', '100']) | |
.find(function (err, data) { | |
console.log('include ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Делаем find с not equal | |
* @private | |
*/ | |
function _runNotOperation() { | |
_getMultivarka() | |
.where('name') | |
.not() | |
.equal('Пётр6') | |
.find(function (err, data) { | |
console.log('not is ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Делаем update | |
* @private | |
*/ | |
function _runUpdateOperation() { | |
_getMultivarka() | |
.where('name') | |
.equal('Пётр6') | |
.set('group', '100') | |
.update(function (err, data) { | |
console.log('update is ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Чистим базу | |
*/ | |
function flushData() { | |
_getMultivarka() | |
.remove(function (err, data) { | |
console.log('remove ok'); | |
err && console.log(err); | |
}); | |
} | |
/** | |
* Подготавливаем данные | |
*/ | |
function prepareData() { | |
const students = [ | |
{name: 'Пётр1', group: 300, grade: 4}, | |
{name: 'Пётр2', group: 200, grade: 4}, | |
{name: 'Пётр3', group: 100, grade: 3}, | |
{name: 'Пётр4', group: 105, grade: 2}, | |
{name: 'Пётр5', group: 700, grade: 1}, | |
{name: 'Пётр6', group: 300, grade: 5} | |
]; | |
_getMultivarka().insert(students, function (err, data) { | |
console.log('insert first ok'); | |
console.log(err || data); | |
}); | |
} | |
/** | |
* Запускаем операции на выполнение | |
*/ | |
function runOperations() { | |
_runFindOperation(); | |
_runEqualOperation(); | |
_runLessThanOperation(); | |
_runGreatThanOperation(); | |
_runNotOperation(); | |
_runUpdateOperation(); | |
} | |
(function() { | |
flushData(); | |
prepareData(); | |
runOperations(); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment