Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@chieforz/json-file-database
Advanced tools
It's not necessary to use a real database in some small projects, and I often use JSON files as databases. However, just using JSON
and fs
is very inconvenient, so this project is made.
Click here.
Pure TypeScript. This helps you make fewer mistakes about types.
Whenever you change the collection, it will start a timer to save the data. If some changes happen during specified delay, it will restart the timer. This is so-called debouncing.
It uses binary-search(for collections typed array
) or AVL-tree(for collections typed avl
) to maintain the data. This means their best time complexity would be O(log n)
.
First, download it by npm
(or yarn
, pnpm
, etc.).
npm i json-file-database
And then use it in your project like this:
import { connect } from 'json-file-database'
/**
* The type of elements must have an `id` property
* to make them unique and sorted in the collection.
*/
type User = { id: number, name: string }
/**
* Connect to the database.
* If there is not specified file yet, it will create one after running this program.
*/
const db = connect({
file: './db.json',
init: {
users: [
{ id: 1, name: 'San Zhang' },
{ id: 2, name: 'Si Li' },
{ id: 3, name: 'Wu Wang' },
]
}
})
/**
* Specify the type of elements is `User`.
*
* You can go to the documentations to see how to customize all
* options, including the `comparator` to compare the elements.
*/
const users = db<User>('users')
/**
* Find the element with its id.
*/
console.log('The user whose id is 1:', users.find(1))
/**
* Find all elements that match given condition.
*/
console.log('All users whose id <= 2 are:', users.findAll(u => u.id <= 2))
/**
* Check whether this collection has the element.
*/
console.log('Whether the collection has a user whose id is 5:', users.has(5))
/**
* Insert an element and return whether it has been inserted.
*/
console.log('Insert a user whose id is 2:', users.insert({ id: 2, name: 'Liu Zhao' }))
/**
* List all elements.
*
* You can also use `[...users]` or `for...of` because
* it has implemented Iterable.
*/
console.log('All users are:', Array.from(users))
/**
* Remove the element and return whether it has been removed.
*/
console.log('Remove the user whose id is 1:', users.remove(1))
/**
* Remove all elements that match the condition, and return the number of them.
*/
console.log('Remove all users whose id < 3, the number of them is:',
users.removeAll(u => u.id < 3))
/**
* Update the element with id, and return whether it has been updated.
*/
console.log('Update the user whose id is 3:', users.update(3, { name: 'Liu Zhao' }))
At the first time you run, it will create a new file db.json
and all outputs are expected. However, when you try to run it again, the outputs are not the same.
This is because if there is no file, it will use the init
property when you try to connect the database; otherwise it will read it directly.
You should use it only when you have to insert data very frequently.
Here is an example to use it:
const users = db<User>({
name: 'users',
type: 'avl'
})
Normally, using array-based collection is enough, because saving AVL-based collection needs to convert it to an array(in JSON), which may be expensive cost when there is large data.
Like the previous question, inserting or removing data is not friendly to array-based collection. However, find
is usually what happens the most.
Operation | Array-based | AVL-based |
---|---|---|
Insert | O(n) | O(log n) |
Update | O(log n) | O(log n) |
Find | O(log n) | O(log n) |
Has (id) | O(log n) | O(log n) |
Has (condition) | O(n) | O(n) |
Remove | O(n) | O(log n) |
Remove All | O(n^2) | O(n*log n) |
Find All | O(n) | O(n) |
Save | O(1) | O(n) |
Besides, the space complexity of AVL-based collection to be saved is S(n)
because a new array will be created(as it should be stored to a JSON file).
Yes, there are some tests for performance. Here is the result on my machine:
However, performance depends on many factors, and we can't judge it simply by the running time. But we can see that array-based collection is not good at removing elements for sure :(.
Additionally, we can see that linear search(search with condition) is really slow, so you should avoid using them if it is not necessary.
FAQs
Lightweight Database on NodeJS by JSON Files
The npm package @chieforz/json-file-database receives a total of 0 weekly downloads. As such, @chieforz/json-file-database popularity was classified as not popular.
We found that @chieforz/json-file-database demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.