Skip to content

Commit

Permalink
fix: override empty file
Browse files Browse the repository at this point in the history
Fixes #844
  • Loading branch information
Belphemur committed Jan 23, 2025
1 parent 25b1064 commit 024169f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/adapter/data/JsonAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class JsonAdapter implements IAdapter<any> {

async readAsync(): Promise<any> {
const data = await this.adapter.readAsync();
if (data == null) {
if (data == null || data === '') {
await this.writeAsync({});
return {};
}
Expand Down
13 changes: 13 additions & 0 deletions test/adapter/adapters.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {FileAdapter} from "../../src/adapter/file/FileAdapter";
import * as fs from "fs";
import * as fsPromise from 'node:fs/promises'
import {JsonAdapter} from "../../src/adapter/data/JsonAdapter";
import {IAdapter} from "../../src/adapter/IAdapter";
import {ConfigWithAdapter} from "../../src/lib/JsonDBConfig";
Expand Down Expand Up @@ -106,6 +107,18 @@ describe('Adapter', () => {

})

test('should override empty file when loading', async () => {
const filename = "data/emptyFile.json";
let fh = await fsPromise.open(filename, 'a');
await fh.close();
const adapter = new JsonAdapter(new FileAdapter(filename, false), false);
await adapter.readAsync();

const fileExists = await checkFileExists(filename);
expect(fileExists).toBeTruthy();
await fsPromise.rm(filename);
})

test('should serialize and deserialize dates', async () => {
const adapter = new JsonAdapter(new MemoryAdapter(), false);
const data = {
Expand Down

0 comments on commit 024169f

Please sign in to comment.