Skip to content

Commit 9340a17

Browse files
authored
Merge pull request #6 from r-el/feature/advanced-create
feature/advanced create
2 parents a86a6f9 + 25617e3 commit 9340a17

File tree

8 files changed

+584
-155
lines changed

8 files changed

+584
-155
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ db.create({ name: 'John', age: 30 }, (err, result) => {
2020
console.log('Created:', result);
2121
});
2222

23-
// Read
24-
db.read('id', (err, data) => {
25-
console.log('Data:', data);
23+
// Read by ID
24+
db.findById(1, (err, item) => {
25+
console.log('Item:', item);
26+
});
27+
28+
// Read all
29+
db.readAll((err, items) => {
30+
console.log('All items:', items);
2631
});
2732

2833
// Update
29-
db.update('id', { age: 31 }, (err, result) => {
34+
db.update(1, { age: 31 }, (err, result) => {
3035
console.log('Updated:', result);
3136
});
3237

3338
// Delete
34-
db.delete('id', (err) => {
35-
console.log('Deleted');
39+
db.delete(1, (err, deletedItem) => {
40+
console.log('Deleted:', deletedItem);
3641
});
3742
```
3843

lib/constants.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Constants and configuration for JsonFileCRUD
3+
* @version 1.0.0
4+
*/
5+
6+
export const OPERATION_TYPES = {
7+
CREATE: 'create',
8+
UPDATE: 'update',
9+
DELETE: 'delete',
10+
WRITE_ALL: 'writeAll'
11+
};
12+
13+
export const ERROR_MESSAGES = {
14+
ITEM_MUST_BE_OBJECT: 'Item must be an object',
15+
DUPLICATE_ID: (idField, id) => `Item with ${idField} ${id} already exists`,
16+
NOT_FOUND: (idField, id) => `Item with ${idField} ${id} not found`,
17+
CANNOT_CHANGE_ID: (idField) => `Cannot change ${idField} field`,
18+
FILE_PATH_REQUIRED: 'File path is required',
19+
INVALID_JSON_ARRAY: 'File content is not a JSON array'
20+
};
21+
22+
export const DEFAULT_CONFIG = {
23+
ID_FIELD: 'id'
24+
};

lib/file-operations.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* File operations utilities for JsonFileCRUD
3+
* @version 1.0.0
4+
*/
5+
6+
import fs from 'fs';
7+
import { ERROR_MESSAGES } from './constants.js';
8+
9+
/**
10+
* Read all items from a JSON file
11+
* @param {string} filePath - Path to the JSON file
12+
* @param {Function} callback - Called with (error, items)
13+
*/
14+
export function readAllFromFile(filePath, callback) {
15+
fs.readFile(filePath, "utf-8", (err, content) => {
16+
if (err && err.code !== "ENOENT") {
17+
// File system error other than "file not found"
18+
callback(err);
19+
} else {
20+
// File doesn't exist or read successfully - parse as JSON array
21+
try {
22+
const items = content ? JSON.parse(content) : [];
23+
if (!Array.isArray(items)) {
24+
throw new Error(ERROR_MESSAGES.INVALID_JSON_ARRAY);
25+
}
26+
callback(null, items);
27+
} catch (error) {
28+
callback(error);
29+
}
30+
}
31+
});
32+
}
33+
34+
/**
35+
* Write items array to file
36+
* @param {string} filePath - Path to the JSON file
37+
* @param {Object[]} items - Array of items to write
38+
* @param {Function} callback - Called with (error)
39+
*/
40+
export function writeItemsToFile(filePath, items, callback) {
41+
const content = JSON.stringify(items, null, 2);
42+
fs.writeFile(filePath, content, callback);
43+
}

0 commit comments

Comments
 (0)