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

Commit

Permalink
v4.0.0
Browse files Browse the repository at this point in the history
- Update coding style and format
- Update dependencies
- Enable GitHub Actions
  • Loading branch information
ndaidong committed Nov 8, 2020
1 parent e771949 commit 9d75193
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 133 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci-test.yml
@@ -0,0 +1,43 @@
# GitHub actions
# https://docs.github.com/en/free-pro-team@latest/actions

name: ci-test

on: [push, pull_request]

jobs:
test:

runs-on: ubuntu-20.04

strategy:
matrix:
node_version: [10.14.2, 14.x, 15.x]

steps:
- uses: actions/checkout@v2

- name: setup Node.js v${{ matrix.node_version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node_version }}

- name: run npm scripts
run: |
npm install
npm run lint
npm run build --if-present
npm run citest
- name: sync to coveralls
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: cache node modules
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ storage
yarn.lock
coverage.lcov
package-lock.json
pnpm-lock.yaml
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

30 changes: 16 additions & 14 deletions README.md
Expand Up @@ -2,7 +2,9 @@
Flat-file based data storage

[![NPM](https://badge.fury.io/js/flat-db.svg)](https://badge.fury.io/js/flat-db)
[![Build Status](https://travis-ci.org/ndaidong/flat-db.svg?branch=master)](https://travis-ci.org/ndaidong/flat-db)
![CI test](https://github.com/ndaidong/flat-db/workflows/ci-test/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/flat-db/badge.svg)](https://coveralls.io/github/ndaidong/flat-db)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ndaidong_flat-db&metric=alert_status)](https://sonarcloud.io/dashboard?id=ndaidong_flat-db)


# Setup
Expand Down Expand Up @@ -39,7 +41,7 @@ Example:

```js

var FlatDB = require('flat-db');
const FlatDB = require('flat-db');

// configure path to storage dir
FlatDB.configure({
Expand All @@ -48,7 +50,7 @@ FlatDB.configure({
// since now, everything will be saved under ./storage

// create Movie collection with schema
let Movie = new FlatDB.Collection('movies', {
const Movie = new FlatDB.Collection('movies', {
title: '',
imdb: 0
});
Expand All @@ -58,7 +60,7 @@ let Movie = new FlatDB.Collection('movies', {
// will be compared with this schema's structure and data type

// insert a set of movies into collection
let keys = Movie.add([
const keys = Movie.add([
{
title: 'The Godfather',
imdb: 9.2
Expand All @@ -80,7 +82,7 @@ console.log('\nkeys returned after adding multi items:');
console.log(keys);

// add a single movie
let key = Movie.add({
const key = Movie.add({
title: 'X-Men',
imdb: 8.3,
year: 2011
Expand All @@ -91,12 +93,12 @@ console.log('\nkey returned after adding single item:');
console.log(key);

// get item with given key
let movie = Movie.get(key);
const movie = Movie.get(key);
console.log(`\nget item by key ${key}:`);
console.log(movie);

// update it
let updating = Movie.update(key, {
const updating = Movie.update(key, {
title: 123456,
imdb: 8.2
});
Expand All @@ -106,33 +108,33 @@ console.log('\nupdating result:');
console.log(updating);

// remove it
let removing = Movie.remove(key);
const removing = Movie.remove(key);
console.log('\nremoving result:');
console.log(removing);

// count collection size
let count = Movie.count();
const count = Movie.count();
console.log('\ncollection size:');
console.log(count);

// get all item
let all = Movie.all();
const all = Movie.all();
console.log('\nall items:');
console.log(all);

// find items with imdb < 7.1
let results = Movie.find().lt('imdb', 7.1).run();
const results = Movie.find().lt('imdb', 7.1).run();
console.log('\nitems with imdb < 7.1:');
console.log(results);

// get 2 items since 2nd item (skip first item), which have "re" in the title
results = Movie.find().matches('title', /re/i).skip(1).limit(2).run();
const results = Movie.find().matches('title', /re/i).skip(1).limit(2).run();
console.log('\n2 items since 2nd item (skip first one), \n which have "re" in the title:');
console.log(results);


// find items with imdb > 6 and title contains "God"
results = Movie
const results = Movie
.find()
.gt('imdb', 6)
.matches('title', /God/)
Expand All @@ -144,7 +146,7 @@ console.log(results);
Movie.reset();

// count collection size after removing all
count = Movie.count();
const count = Movie.count();
console.log('\ncollection size after removing all:');
console.log(count);
```
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -3,6 +3,6 @@
* @ndaidong
**/

var main = require('./src/main');
const main = require('./src/main');
main.version = require('./package').version;
module.exports = main;
21 changes: 10 additions & 11 deletions package.json
@@ -1,5 +1,5 @@
{
"version": "3.0.0",
"version": "4.0.0",
"name": "flat-db",
"description": "Flat-file based data storage",
"homepage": "https://www.npmjs.com/package/flat-db",
Expand All @@ -10,24 +10,23 @@
"author": "@ndaidong",
"main": "./index.js",
"engines": {
"node": ">= 7.6"
"node": ">= 10.14.2"
},
"scripts": {
"lint": "eslint ./src ./test",
"lint": "eslint .",
"pretest": "npm run lint",
"test": "tap test/start.js --coverage --reporter=spec",
"test": "tap tests/start.js --coverage --reporter=spec --coverage-report=html --no-browser",
"citest": "tap tests/start.js --coverage --reporter=spec --coverage-report=lcov --no-browser",
"reset": "node reset"
},
"dependencies": {
"bellajs": "^7.2.2",
"debug": "^3.1.0",
"mkdirp": "^0.5.1",
"stabilize.js": "^2.0.0"
"bellajs": "^9.2.2",
"debug": "^4.2.0",
"mkdirp": "^1.0.4"
},
"devDependencies": {
"eslint": "^5.1.0",
"eslint-config-goes": "^1.0.0",
"tap": "^12.0.1"
"eslint-config-goes": "^1.1.8",
"tap": "^14.10.8"
},
"keywords": [
"storage",
Expand Down
1 change: 1 addition & 0 deletions reset.js
Expand Up @@ -18,6 +18,7 @@ const dirs = [

const files = [
'yarn.lock',
'pnpm-lock.yaml',
'package-lock.json',
'coverage.lcov',
];
Expand Down
50 changes: 25 additions & 25 deletions src/collection.js
Expand Up @@ -5,7 +5,7 @@

const {
time,
createId,
genid,
hasProperty,
isObject,
isArray,
Expand Down Expand Up @@ -33,13 +33,13 @@ const C = new Map();

class Collection {
constructor(name, schema = {}, forceReload = false) {
let n = normalize(name);
const n = normalize(name);
if (!n) {
throw new Error(`Invalid collection name "${name}"`);
}

if (!forceReload) {
let c = C.get(n);
const c = C.get(n);
if (c) {
return c;
}
Expand All @@ -51,20 +51,20 @@ class Collection {
this.lastModified = time();
this.entries = [];

let {
const {
dir,
ext,
} = config;

let file = fixPath(`${dir}/${n}${ext}`);
const file = fixPath(`${dir}/${n}${ext}`);

this.file = file;

this.status = 0;

let data = readFile(file);
const data = readFile(file);
if (data) {
let {
const {
schema: cschema,
lastModified,
entries,
Expand All @@ -81,7 +81,7 @@ class Collection {
}

onchange() {
let lastModified = time();
const lastModified = time();
this.lastModified = lastModified;
writeFile(this.file, {
name: this.name,
Expand All @@ -104,22 +104,22 @@ class Collection {
throw new Error('Invalid parameter. Object required.');
}

let entries = this.all();
const entries = this.all();

let schema = this.schema;
let noSchema = isEmpty(schema);
const schema = this.schema;
const noSchema = isEmpty(schema);

let addOne = (entry) => {
let id = createId(32);
const addOne = (entry) => {
const id = genid(32);
entry._id_ = id;
entry._ts_ = time();

if (!noSchema) {
let _item = Object.assign({
const _item = Object.assign({
_id_: '',
_ts_: 0,
}, schema);
for (let key in _item) {
for (const key in _item) {
if (hasProperty(entry, key) && typeof entry[key] === typeof _item[key]) {
_item[key] = entry[key];
}
Expand All @@ -130,13 +130,13 @@ class Collection {
};

if (!isArray(item)) {
let newEntry = addOne(item);
const newEntry = addOne(item);
this.entries.push(newEntry);
this.onchange();
return newEntry._id_;
}

let newEntries = item.map(addOne);
const newEntries = item.map(addOne);
this.entries = entries.concat(newEntries);
this.onchange();
return newEntries.map((a) => {
Expand All @@ -149,9 +149,9 @@ class Collection {
throw new Error('Invalid parameter. String required.');
}

let entries = this.all();
const entries = this.all();

let candidates = entries.filter((item) => {
const candidates = entries.filter((item) => {
return item._id_ === id;
});

Expand All @@ -163,17 +163,17 @@ class Collection {
throw new Error('Invalid parameter. String required.');
}

let entries = this.all();
const entries = this.all();
let changed = false;

let k = entries.findIndex((el) => {
const k = entries.findIndex((el) => {
return el._id_ === id;
});

if (k >= 0) {
let obj = entries[k];
const obj = entries[k];

for (let key in obj) {
for (const key in obj) {
if (key !== '_id_' &&
hasProperty(data, key) &&
typeof data[key] === typeof obj[key] &&
Expand Down Expand Up @@ -203,8 +203,8 @@ class Collection {
throw new Error('Invalid parameter. String required.');
}

let entries = this.all();
let k = entries.findIndex((el) => {
const entries = this.all();
const k = entries.findIndex((el) => {
return el._id_ === id;
});

Expand Down

0 comments on commit 9d75193

Please sign in to comment.