Coder Social home page Coder Social logo

documentsdb's Introduction

DocumentsDB

Pub license GitHub stars

Persistent embedded document-oriented NoSQL database for Dart and Flutter. 100% Dart.

CAUTION This plugin is still in development. Use at your own risk. If you notice any bugs you can create an issue on GitHub. You're also welcome to contribute using pull requests. Please open an issue before spending time on any pull request.

NB The project was originally created by netz-chat on https://github.com/netz-chat/objectdb/blob/master/README.md.

How to use

final path = Directory.current.path + '/my.db';

// create database instance and open
final db = DocumentsDB(path);
db.open();

// insert document into database
db.insert({'name': {'first': 'Some', 'last': 'Body'}, 'age': 18, 'active': true);
db.insert({'name': {'first': 'Someone', 'last': 'Else'}, 'age': 25, 'active': false);

// update documents
db.update({Op.gte: {'age': 80}}, {'active': false});

// remove documents
db.remove({'active': false});

// search documents in database
var result = await db.find({'active': true, 'name.first': 'Some'});

// 'tidy up' the db file
db.tidy();

// close db
await db.close();

Methods

  • Future<DocumentsDB> db.open([bool tidy = true]) opens database
  • Future<void> db.tidy() 'tidy up' the .db file
  • Future<void> db.close() closes database (should be awaited to ensure all queries have been executed)

find

  • Future<List<Map>> db.find(Map query) List with all matched documents
  • Future<Map> db.first(Map query) first matched document
  • Future<Map> db.last(Map query) last matched document

insert

  • Future<ObjectId> db.insert(Map document) insert single document
  • Future<List<ObjectId>> db.insertMany(List<Map> documents) insert many documents

update

  • Future<int> db.update(Map query, Map changes, [bool replace = false]) update documents that mach query with changes (optionally replace whole document)

remove

  • Future<int> db.remove(Map query) remove documents that match query

Query

// Match fields in subdocuments
{Op.gte: {
    'birthday.year': 18
}}

// or-operator
{Op.or:{
    'active': true,
    Op.inArray: {'group': ['admin', 'moderator']}
}}

// not equal to
{Op.not: {'active': false}}

NOTE Querying arrays is not supportet yet.

Operators

Logical

  • and (default operator on first level)
  • or
  • not

Comparison

  • lt, lte: less than, less than or equal
  • gt, gte: greater than, greater than or equal
  • inList, notInList: value in list, value not in list

Modify

  • set: set value
  • max, min: set max or min int value
  • increment, multiply: increment/multiply by
  • unset: unset key/value
  • rename: rename key
  • currentDate, set current timestamp
  • add: push value
  • addAll: push many value
  • addToSet: push value if not exists
  • insert: insert value at any position in List
  • insertAll: insert many value at any position in List
  • pop, remove last value in List
  • remove, remove value
  • removeAt, remove value at any position
  • removeWhere, remove value where callback return true
  • clear: clear list/Map
  • slice: sublist List
  • sort : sort List
{Op.set: {'path.to.key': 'value'}} // set entry['path']['to']['key'] = 'value' (path will be created if not exists)
{Op.max: {'path.to.key': 200}} // set value 200 if value is greater than 200
{Op.min: {'path.to.key': 200}} // set value 200 if value is smaller than 200
{Op.increment: {'path.to.key': -5}} // increment value by negative 5
{Op.multiply: {'path.to.key': 2}} // multiply value by 2
{Op.unset: {'path.to.key': true}} // unset key/value at entry['path']['to']['key'] if exists
{Op.rename: {'path.to.key': 'new_key'}} // new value will be at entry['path']['to']['new_key']


db.update({
  'age': RegExp('[18-20]'),
  Op.gt: {'duration': 500},
}, {
  Op.max: {'stats.score': 100},
  Op.increment: {'stats.level': -5},
});

Examples

// query
var result = db.find({
    'active': true,
    Op.or: {
        Op.inList: {'state': ['Florida', 'Virginia', 'New Jersey']},
        Op.gte: {'age': 30},
    }
});

// same as
var match = (result['active'] == true && (['Florida', 'Virginia', 'New Jersey'].contains(result['state']) || result['age'] >= 30));

Todo's

  • regex match
  • encryption
  • querying arrays
  • benchmarks
  • indexing

License

See License

documentsdb's People

Contributors

developerandre avatar

Stargazers

Everton avatar Andy Chentsov avatar  avatar

Watchers

James Cloos avatar  avatar

documentsdb's Issues

How slow is it ?

Would be interesting to bench it.
Pure dart db is probably slow but possibly fine for a single user on a mobile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.