Mixin - aalfiann/fly-json-odm GitHub Wiki
You can create your own function to work with fly-json-odm to extend its features.
What is Mixin by definitions ?
In object-oriented programming languages, a mixin (or mix-in)1(https://en.wikipedia.org/wiki/Mixin#cite_note-:0-1)[2](/aalfiann/fly-json-odm/wiki/2)(https://en.wikipedia.org/wiki/Mixin#cite_note-:1-2)[3](/aalfiann/fly-json-odm/wiki/3)(https://en.wikipedia.org/wiki/Mixin#cite_note-:2-3)[4](/aalfiann/fly-json-odm/wiki/4)(https://en.wikipedia.org/wiki/Mixin#cite_note-:3-4) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".
Usage
Basic Example
Create a simple test() function and then inject it to fly-json-odm.
const data = [{ name: 'abc' }, { name: 'def' }];
function removeFirstElement (self) {
// get current list from nosql
const data = self.list();
// remove first element
data.shift();
// return with this new modified data;
self.set(data);
}
const nosql = new FlyJson({
// inject a new "test()" function
test: () => {
// which is execute your custom function above
removeFirstElement(nosql);
}
});
// Now your custom function "test()" already injected inside fly-json-odm class.
// You can use it for now or later.
const result = nosql.set(data).test().exec();
// output result
[{ name: 'def' }]
Multiple Function Example
Create multiple function and then inject it.
const data = [{ name: 'abc' }, { name: 'def' }];
function foo (nosql) {
const newdt = nosql.list();
newdt.push({ name: 'foo' });
nosql.set(newdt);
}
function bar (nosql) {
const newdt = nosql.list();
newdt.push({ name: 'bar' });
nosql.set(newdt);
}
const nosql = new FlyJson({
foo: () => {
foo(nosql);
},
bar: () => {
bar(nosql);
}
});
const result = nosql.set(data)
// look for name abc
.where('name', 'abc')
// add foo
.foo()
// add bar
.bar()
// execute
.exec();
// output result
[{ name: 'abc' }, { name: 'foo' }, { name: 'bar' }]
Passing Parameter Example
Create custom function with parameter and then inject it.
const data = [{ name: 'abc' }, { name: 'def' }];
function modifyData (nosql, name) {
nosql.set([{ name: 'new user: ' + name }]);
}
const nosql = new FlyJson({
// "list()" function is a built-in function that exist as default in fly-json-odm,
// but we could replace it with our custom function
list: (name) => {
modifyData(nosql, name);
}
});
const result = nosql
.set(data)
// look for name abc
.where('name', 'abc')
// we replace the built-in function "list()" with our custom new "list(name)" function
.list('bar')
// execute
.exec();
// output result
[{ name: 'new user: bar' }]