Skip to content

Commit 0176b83

Browse files
committed
feat: add a format param
1 parent 0015242 commit 0176b83

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ await file.create({
7373
filename: "user1",
7474
data: { name: "John", age: 30 } // Type-checked
7575
});
76+
// File created at: basePath/users/user1.json
77+
78+
// With formatting
79+
await file.create({
80+
path: "users",
81+
filename: "user1",
82+
data: { name: "John", age: 30 },
83+
format: true // Pretty prints the JSON
84+
});
7685
```
7786

7887
### Read
@@ -106,6 +115,15 @@ await file.update({
106115
// Note: Merge is shallow - nested objects are replaced entirely
107116
// If file contains { user: { name: "John" }, status: "active" } and data { user: { age: 30 } },
108117
// it becomes { user: { age: 30 }, status: "active" } (user.name is lost, but status is kept)
118+
119+
// With formatting
120+
await file.update({
121+
path: "users",
122+
filename: "user1",
123+
data: { age: 26 },
124+
mode: "merge",
125+
format: true
126+
});
109127
```
110128

111129
### Delete

src/file.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class File<T = unknown> extends DataBuddyUtils implements FileInterface<T
2222
const workingPath = this.workingPath(path)
2323
const data = await fs.readFile(`${workingPath}/${filename}.json`, 'utf-8')
2424
return JSON.parse(data)
25-
} catch (error) {
25+
} catch (error: any) {
2626
if (error.code === 'ENOENT') {
2727
return null
2828
} else {
@@ -31,7 +31,7 @@ export class File<T = unknown> extends DataBuddyUtils implements FileInterface<T
3131
}
3232
}
3333

34-
async create({ path, filename, data }: UpsertParams<T>): Promise<ReturnData<T>> {
34+
async create({ path, filename, data, format = false }: UpsertParams<T>): Promise<ReturnData<T>> {
3535
this.validatePathAndFilename(path, filename)
3636
const sanitizedData = this.sanitizeData(data)
3737
const workingPath = this.workingPath(path)
@@ -40,18 +40,28 @@ export class File<T = unknown> extends DataBuddyUtils implements FileInterface<T
4040
throw new Error(`File ${filename} already exists in ${workingPath}`)
4141
}
4242
await fs.mkdir(workingPath, { recursive: true })
43-
await fs.writeFile(`${workingPath}/${filename}.json`, JSON.stringify(sanitizedData, null, 4))
43+
await fs.writeFile(
44+
`${workingPath}/${filename}.json`,
45+
JSON.stringify(sanitizedData, null, format ? 4 : 0)
46+
)
4447
return this.read({ path, filename })
4548
}
4649

47-
async update(params: BaseParams & { data: T; mode?: 'replace' }): Promise<ReturnData<T>>
48-
async update(params: BaseParams & { data: Partial<T>; mode: 'merge' }): Promise<ReturnData<T>>
50+
async update(
51+
params: BaseParams & { data: T; mode?: 'replace'; format?: boolean }
52+
): Promise<ReturnData<T>>
53+
async update(
54+
params: BaseParams & { data: Partial<T>; mode: 'merge'; format?: boolean }
55+
): Promise<ReturnData<T>>
4956
async update({
5057
path,
5158
filename,
5259
data,
5360
mode = 'replace',
54-
}: BaseParams & { data: T | Partial<T>; mode?: 'replace' | 'merge' }): Promise<ReturnData<T>> {
61+
format = false,
62+
}: BaseParams & { data: T | Partial<T>; mode?: 'replace' | 'merge'; format?: boolean }): Promise<
63+
ReturnData<T>
64+
> {
5565
this.validatePathAndFilename(path, filename)
5666
const workingPath = this.workingPath(path)
5767

@@ -62,15 +72,16 @@ export class File<T = unknown> extends DataBuddyUtils implements FileInterface<T
6272
let updatedData: T
6373
if (mode === 'merge') {
6474
const currentData = await this.read({ path, filename })
65-
if (!currentData) {
66-
throw new Error(`File ${filename} does not exist in ${workingPath}`)
67-
}
68-
updatedData = { ...currentData, ...data }
75+
updatedData = { ...currentData, ...data } as T
6976
} else {
7077
updatedData = data as T
7178
}
7279

73-
await fs.writeFile(`${workingPath}/${filename}.json`, JSON.stringify(updatedData, null, 4))
80+
await fs.mkdir(workingPath, { recursive: true })
81+
await fs.writeFile(
82+
`${workingPath}/${filename}.json`,
83+
JSON.stringify(updatedData, null, format ? 4 : 0)
84+
)
7485
return this.read({ path, filename })
7586
}
7687

@@ -81,7 +92,7 @@ export class File<T = unknown> extends DataBuddyUtils implements FileInterface<T
8192
try {
8293
await fs.unlink(`${workingPath}/${filename}.json`)
8394
return true
84-
} catch (error) {
95+
} catch (error: any) {
8596
if (error.code === 'ENOENT') {
8697
return false
8798
} else {

src/types/file_interface.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type ReturnData<T = unknown> = T | null
22
export type BaseParams = { path: string; filename: string }
3-
export type UpsertParams<T = unknown> = BaseParams & { data: T }
3+
export type UpsertParams<T = unknown> = BaseParams & { data: T; format?: boolean }
44

55
/**
66
* `FileInterface` is an interface that defines the structure for the `DataBuddy` class.
@@ -37,14 +37,21 @@ export interface FileInterface<T = unknown> {
3737
* It takes an object with `path`, `filename`, `data`, and optional `mode`.
3838
* It returns a Promise that resolves with the updated data from the file or null if an error occurs.
3939
*/
40-
update(params: BaseParams & { data: T; mode?: 'replace' }): Promise<ReturnData<T>>
41-
update(params: BaseParams & { data: Partial<T>; mode: 'merge' }): Promise<ReturnData<T>>
40+
update(
41+
params: BaseParams & { data: T; mode?: 'replace'; format?: boolean }
42+
): Promise<ReturnData<T>>
43+
update(
44+
params: BaseParams & { data: Partial<T>; mode: 'merge'; format?: boolean }
45+
): Promise<ReturnData<T>>
4246
update({
4347
path,
4448
filename,
4549
data,
4650
mode,
47-
}: BaseParams & { data: T | Partial<T>; mode?: 'replace' | 'merge' }): Promise<ReturnData<T>>
51+
format,
52+
}: BaseParams & { data: T | Partial<T>; mode?: 'replace' | 'merge'; format?: boolean }): Promise<
53+
ReturnData<T>
54+
>
4855

4956
/**
5057
* The `delete` method is used to delete a file.

0 commit comments

Comments
 (0)