Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from ndaidong/dev
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
ndaidong authored May 28, 2017
2 parents 1c2f241 + 98c7e86 commit c16131d
Show file tree
Hide file tree
Showing 25 changed files with 812 additions and 3,599 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: node_js
node_js:
- "7.1"
- "6.8"
- "6.5"
- "6.0"
- "7.10"
- "7.6"
after_success:
- npm run coveralls
134 changes: 83 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Flat-file based data storage
[![Build Status](https://travis-ci.org/ndaidong/flat-db.svg?branch=master)](https://travis-ci.org/ndaidong/flat-db)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/flat-db/badge.svg?branch=master&noop)](https://coveralls.io/github/ndaidong/flat-db?branch=master)
[![Dependency Status](https://gemnasium.com/badges/github.com/ndaidong/flat-db.svg)](https://gemnasium.com/github.com/ndaidong/flat-db)
[![Known Vulnerabilities](https://snyk.io/test/npm/flat-db/badge.svg)](https://snyk.io/test/npm/flat-db)
[![NSP Status](https://nodesecurity.io/orgs/techpush/projects/ba89614a-f3d3-42e3-9aa1-dbdd9096a01c/badge)](https://nodesecurity.io/orgs/techpush/projects/ba89614a-f3d3-42e3-9aa1-dbdd9096a01c)

# Setup

Expand All @@ -20,11 +20,11 @@ npm install flat-db --save
// configure path to storage dir
FlatDB.configure({
path: 'storage'
dir: './storage'
});
// add collection
var Movie = FlatDB.addCollection('movies');
var Movie = new FlatDB.Collection('movies');
// add item into "Movie" collection
let key = Movie.add({
Expand Down Expand Up @@ -53,26 +53,77 @@ npm install flat-db --save

### FlatDB
- .configure(Object options)
- .getConfigs()
- .addCollection(String collectionName, Object schema) // schema will be supported in future
- .getCollection(String collectionName)
- .emptyCollection(String collectionName)
- .removeCollection(String collectionName)
- .reset() // remove all collections

FlatDB.addCollection() and FlatDB.getCollection() return a Collection instance with the following methods:
#### FlatDB.Collection(String name, Object schema) constructor

#### Collection instance
```
let Movie = new FlatDB.Collection('movies', {
title: '',
year: 0
});
```

The schema is optional. If the schema was defined, any new item were added would be founded based this schema's structure and data type.

#### FlatDB.Collection class instance
- .add(Object item)
- .update(String itemKey, Object updates)
- .get([String itemKey])
- .remove(String itemKey)

```
let key = Movie.add({
// movie data
});
console.log(key);
```

It's possible to add multi items in the same time:

```
let keys = Movie.add([
{
// movie data
},
{
// movie data
}
]);
console.log(keys);
```

- .get([String key])

```
let movie = Movie.get(key);
console.log(movie);
```

- .update(String key, Object updates)

```
let movie = Movie.update(key, {
// mutual data
});
console.log(movie);
```

- .remove(String key)

```
let result = Movie.remove(key);
console.log(result); // true if removed
```

- .find()

Returns the Finder instance

```
let MovieFinder = Movie.find();
console.log(MovieFinder);
```

##### Collection Finder

When you call Collection.find(), it would return a CollectionFinder instance with the following chaining methods:
##### Collection Finder instance

- .equals(String property, String | Number value)
- .notEqual(String property, String | Number value)
Expand All @@ -83,19 +134,19 @@ When you call Collection.find(), it would return a CollectionFinder instance wit
- .matches(String property, RegExp value)
- .run()

The result by run() is a Promise.

Examples:

```
// configure storage
FlatDB.configure({
path: 'storage/'
dir: 'storage/'
});
// add a collection names "movies"
let Movie = FlatDB.addCollection('movies');
let Movie = new FlatDB.Collection('movies', {
});
// add some movies to collection
let entries = [
Expand All @@ -117,43 +168,24 @@ let entries = [
}
];
entries.forEach((item) => {
Movie.add(item);
});
// now we can find the expected movies
Movie
.find()
.matches('title', /The/)
.gt('imdb', 7)
.run().then((results) => {
console.log(results);
});
Movie.add(entries);
// or create a CollectionFinder instance to use later
let MovieFinder = Movie.find();
// find items with imdb > 6 and title contains "God"
let results = Movie
.find()
.gt('imdb', 6)
.matches('title', /God/)
.run();
console.log(results);
// find items which have "re" in the title
MovieFinder
.matches('title', /re/i)
.run().then((results) => {
console.log(results);
});
results = Movie.find().matches('title', /re/i).run();
console.log(results);
// find items with imdb < 7.1
MovieFinder
.lt('imdb', 7.1)
.run().then((results) => {
console.log(results);
});
results = Movie.find().lt('imdb', 7.1).run();
console.log(results);
// find items with imdb > 6 and title contains "God"
MovieFinder
.gt('imdb', 6)
.matches('title', /God/)
.run().then((results) => {
console.log(results);
});
```

# Test
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
* @ndaidong
**/

exports = module.exports = require('./src/main');
exports.version = require('./package').version;
var main = require('./src/main');
main.version = require('./package').version;
module.exports = main;
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.2",
"version": "2.0.0",
"name": "flat-db",
"description": "Flat-file based data storage",
"homepage": "https://www.npmjs.com/package/flat-db",
Expand All @@ -10,32 +10,32 @@
"author": "@ndaidong",
"main": "./index.js",
"engines": {
"node": ">= 6.0"
"node": ">= 7.6"
},
"scripts": {
"lint": "eslint ./src ./test",
"snyk": "snyk test",
"nsp": "nsp check --output checkstyle",
"tape": "nyc tape test/start.js | tap-spec",
"report": "nyc report --reporter=lcov",
"pretest": "npm run lint && npm run snyk",
"pretest": "npm run lint && npm run nsp",
"test": "npm run tape",
"posttest": "npm run report",
"coveralls": "npm test && cat ./coverage/lcov.info | coveralls",
"try": "node ./test/manual"
},
"dependencies": {
"bellajs": "6.x.x",
"mkdirp": "0.x.x",
"promise-wtf": "1.x.x"
"debug": "2.x.x",
"mkdirp": "0.x.x"
},
"devDependencies": {
"coveralls": "latest",
"eslint": "latest",
"eslint-config-ggc": "latest",
"nyc": "latest",
"snyk": "latest",
"tap-spec": "latest",
"tape": "latest"
"coveralls": "2.x.x",
"eslint": "3.x.x",
"eslint-config-ggc": "1.x.x",
"nsp": "2.x.x",
"nyc": "10.x.x",
"tap-spec": "4.x.x",
"tape": "4.x.x"
},
"keywords": [
"storage",
Expand Down
Loading

0 comments on commit c16131d

Please sign in to comment.