Skip to content

Commit

Permalink
file read write - import only the required functions (#8)
Browse files Browse the repository at this point in the history
* Importing only the needed functions
* 0.1.20
  • Loading branch information
yusufshakeel authored Dec 30, 2024
1 parent 8fcea27 commit 2aefa72
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Minimalistic JSON database.

[![Build Status](https://github.com/yusufshakeel/minivium/actions/workflows/ci.yml/badge.svg)](https://github.com/yusufshakeel/minivium/actions/workflows/ci.yml)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yusufshakeel/minivium)
[![npm version](https://img.shields.io/badge/npm-0.1.19-blue.svg)](https://www.npmjs.com/package/minivium)
[![npm version](https://img.shields.io/badge/npm-0.1.20-blue.svg)](https://www.npmjs.com/package/minivium)
[![npm Downloads](https://img.shields.io/npm/dm/minivium.svg)](https://www.npmjs.com/package/minivium)

![img.webp](assets/img.webp)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minivium",
"version": "0.1.19",
"version": "0.1.20",
"description": "Minimalistic JSON database.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
16 changes: 8 additions & 8 deletions src/core/File.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import path from 'path';

export class FileSync {
Expand All @@ -13,33 +13,33 @@ export class FileSync {
}

private checkExistsSync(collectionName: string) {
if (!fs.existsSync(this.getFilePath(collectionName))) {
if (!existsSync(this.getFilePath(collectionName))) {
throw new Error(`File '${collectionName}' does not exist`);
}
}

createSync(collectionName: string, content: string): void {
fs.writeFileSync(this.getFilePath(collectionName), content, 'utf8');
writeFileSync(this.getFilePath(collectionName), content, 'utf8');
}

readSync(collectionName: string): string {
this.checkExistsSync(collectionName);
return fs.readFileSync(this.getFilePath(collectionName), 'utf8');
return readFileSync(this.getFilePath(collectionName), 'utf8');
}

writeSync(collectionName: string, content: string): void {
this.checkExistsSync(collectionName);
fs.writeFileSync(this.getFilePath(collectionName), content, 'utf8');
writeFileSync(this.getFilePath(collectionName), content, 'utf8');
}

createFileIfNotExistsSync(collectionName: string): void {
if(!fs.existsSync(this.getFilePath(collectionName))) {
fs.writeFileSync(this.getFilePath(collectionName), '[]', 'utf8');
if(!existsSync(this.getFilePath(collectionName))) {
writeFileSync(this.getFilePath(collectionName), '[]', 'utf8');
}
}

deleteFileSync(collectionName: string): void {
this.checkExistsSync(collectionName);
fs.unlinkSync(this.getFilePath(collectionName));
unlinkSync(this.getFilePath(collectionName));
}
}

0 comments on commit 2aefa72

Please sign in to comment.