Skip to content

Instantly share code, notes, and snippets.

@atayahmet
Last active April 6, 2018 13:35
Show Gist options
  • Save atayahmet/7e54d9ec62cb878338974574524179fb to your computer and use it in GitHub Desktop.
Save atayahmet/7e54d9ec62cb878338974574524179fb to your computer and use it in GitHub Desktop.

Storage Class Documents

Storage class is a parent class that it can be use like orm approach. Create model class and extend to Storage parent then use it for different data stores.

Example model class:

import Storage from "./Storage";

export class FrameModel extends Storage {

  constructor() {
    super('frames')
  }
}

Note: You have define data store name to super keyword.

Instantiation:

const frame = new FrameModel;

frame.getAll().then(data => {
  
  // do something...

});

That's it.

Inheritance methods on parent

save()

Parameter: Object

Return: Promise<Any>

Create or update an item.

Example:

const item = {
  name: 'Birthday',
  type: 'Crew'
};

frame.save(item).then((err, numReplaced, upsert) => {

  // do something...

});

Note: if update the existing data, it will returns parameters in the examples.

remove()

Remove items.

Parameter: Object

Return: Promise<Any, Number> (number of deleted items)

Example:

frame.remove({_id: 'aaa44r446464'}).then((err, numDeleted) => {

  // do something...

})

bulkSave()

Bulk save operation.

Parameter: Array[]

Return: Promise<Any, Array[]>

const items = [
  {
    name: 'Birthday',
    type: 'Crew'
  },
  {
    name: 'UM Passengers',
    type: 'passenger'
  }
];

frame.blukSave(items).then((err, newDocs) => {

  // do something...

});

count()

Count items.

Parameter: Object

Return: Promise<Any, Number>

Example:

frame.count({type: 'crew'}).then((err, count) => {

  // do something...

});

find()

Find items.

Parameter: Object

Return: Promise<Any, Array[]>

Example:

frame.find({type: 'crew'}).then((err, docs) => {

  // do something...

});

store()

Access to the following methods.

.insert()

.find()

.findOne()

.count()

.update()

.upsert()

.remove()

.removeAll()

.getField()

.ensureIndex()

.removeIndex()

.store() // It will return NeDB instance

.limit()

.all()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment