https://github.com/ruslankonev/just-json
Small json-driven flat-files database system
https://github.com/ruslankonev/just-json
flat-file json json-db just-json
Last synced: 2 months ago
JSON representation
Small json-driven flat-files database system
- Host: GitHub
- URL: https://github.com/ruslankonev/just-json
- Owner: ruslankonev
- Created: 2016-06-16T15:33:12.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-02-21T09:32:19.000Z (over 9 years ago)
- Last Synced: 2025-10-26T21:39:59.206Z (8 months ago)
- Topics: flat-file, json, json-db, just-json
- Language: JavaScript
- Homepage:
- Size: 56.6 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# just-json
## Root object props & methods
```js
DB = {
schemas: {
// ...listing of schemas
},
config(opts) {
// GET or SET database configs
let opts = {
path: 'stores/db',
schema: './_schemas.json'
}
},
select(collection, schema),
removeCollection(name),
emptyCollection(name),
reset()
}
```
## Collection methods
* `add(item)` — add record to collection
* `save(item)` — alias for add method
* `get(id, fields)` — retrieve record from collection
* `update(id, data)` — update record with [id]
* `remove(id)` — remove record with [id]
* `sync(data)` — synchronize/replace data in current collection
* `count()` — get count of records in current collection
* `find()` — Promise-based method `.find().run().then()` with finder model. Search methods see bellow.
## Finder methods
* `equals(key, val)`
* `notEqual(key, val)`
* `gt(key, val)`
* `gte(key, val)`
* `lt(key, val)`
* `lte(key, val)`
* `matches(key, reg)`
* `paginate(count, sort)`
* `one()`
* `run()`
## Usage example
**_schemas.json**
```json
{
"currencies": {
"title": "Валюты",
"fields": [
"tag|text|Тег",
"name|text|Название"
]
},
"currates": {
"title": "Ставки валют",
"fields": [
"currency|select|Валюта",
"rate|text|Ставка",
"created_at|date|Дата"
],
"dependencies": {
"currency": "currencies:tag,name"
}
}
}
```
**app.js**
```js
const DB = require('just-json')
DB.configure({
path: 'stores/db',
schema: __dirname + '/_schemas.json'
});
// add collection
var Movie = DB.select('tosters');
// add item into "Movie" collection
let key = Movie.add({
type: 'movie',
title: 'The Godfather',
director: 'Francis Ford Coppola',
writer: 'Mario Puzo',
imdb: 9.2
});
let mov = Movie.get(key); // return value of [_id] field
console.log('\n\n', mov);
Movie.remove(key); // remove item
mov = Movie.get(key); // try to get again
console.log('\n\n', mov); // return -> null
// add a record again
let key = Movie.add({
type: 'movie',
title: 'The Godfather',
director: 'Francis Ford Coppola',
writer: 'Mario Puzo',
imdb: 9.2
});
console.log('\n\nCount is', Movie.count()); // -> 1
// test find method
Movie.find()
.matches('title', /he/i)
.matches('type', /movie/i)
.paginate(2)
.run()
.then( results => {
console.log('\n\nFind results', results);
});
```