milodb 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "milodb",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "main": "src/milodb.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/src/cli.js CHANGED
@@ -77,7 +77,7 @@ async function initDatabase() {
77
77
  // Parse command line arguments
78
78
  const args = process.argv.slice(2);
79
79
 
80
- // Handle the `init` command
80
+ // Handle the init command
81
81
  if (args[0] === 'init') {
82
82
  initDatabase();
83
83
  } else {
package/src/milodb.js CHANGED
@@ -3,21 +3,26 @@ const path = require('path');
3
3
  const { encrypt, decrypt } = require('./encrypt');
4
4
 
5
5
  class MiloDB {
6
- constructor(dbPath = path.join(__dirname, 'db'), encryptData = false) {
6
+ constructor(dbPath = path.join(process.cwd(), 'db'), encryptData = false) {
7
7
  // Use the root folder for dbPath if not provided
8
8
  this.dbPath = dbPath;
9
9
  this.encryptData = encryptData;
10
10
  fs.ensureDirSync(this.dbPath); // Ensure that the DB path exists
11
11
  }
12
12
 
13
- // Create a new table (i.e., a new JSON file)
14
13
  async createTable(tableName) {
15
14
  const tablePath = path.join(this.dbPath, `${tableName}.json`);
16
- if (fs.existsSync(tablePath)) {
17
- throw new Error(`Table ${tableName} already exists.`);
15
+
16
+ try {
17
+ if (fs.existsSync(tablePath)) {
18
+ throw new Error(`Table ${tableName} already exists.`);
19
+ }
20
+ await fs.writeJson(tablePath, [], { spaces: 2 });
21
+ console.log(`Table ${tableName} created successfully.`);
22
+ } catch (err) {
23
+ console.error(`Failed to create table: ${err.message}`);
24
+ // Additional logic, like suggesting to delete/rename the table, can go here
18
25
  }
19
- await fs.writeJson(tablePath, [], { spaces: 2 });
20
- console.log(`Table ${tableName} created successfully.`);
21
26
  }
22
27
 
23
28
  // Add a record to a table