Comparing version 1.2.0 to 1.3.0
@@ -5,4 +5,11 @@ export declare namespace collection { | ||
function insert(collection: string, newData: any): Promise<string | null>; | ||
function remove(collection: string, identifier: string): Promise<string | null>; | ||
function remove(collection: string, { key, value }: { | ||
key: string; | ||
value: any; | ||
}): Promise<string | null>; | ||
function destroy(collection: string): Promise<string>; | ||
function update(collection: string, { key, value }: { | ||
key: string; | ||
value: any; | ||
}, newData: any): Promise<string | null>; | ||
} |
@@ -72,3 +72,3 @@ "use strict"; | ||
collection_1.insert = insert; | ||
async function remove(collection, identifier) { | ||
async function remove(collection, { key, value }) { | ||
try { | ||
@@ -82,13 +82,14 @@ const dbname = process.env.DB_NAME; | ||
let data = JSON.parse(jsonData); | ||
// Find the index of the item with the given identifier | ||
const index = data.findIndex((item) => item._id === identifier); | ||
if (index !== -1) { | ||
// Find the index of the item to remove | ||
const indexToRemove = data.findIndex((item) => item[key] === value); | ||
// console.log(indexToRemove); | ||
if (indexToRemove !== -1) { | ||
// Remove the item from the array | ||
data.splice(index, 1); | ||
data.splice(indexToRemove, 1); | ||
// Write the updated data back to the file | ||
fs_1.default.writeFileSync(fileName, JSON.stringify(data)); | ||
return 'Data removed successfully.'; | ||
return `Item with ${key} '${value}' removed from collection '${collection}'.`; | ||
} | ||
else { | ||
return `Item with identifier '${identifier}' not found in collection '${collection}'.`; | ||
return `Item with ${key} '${value}' not found in collection '${collection}'.`; | ||
} | ||
@@ -125,3 +126,37 @@ } | ||
collection_1.destroy = destroy; | ||
async function update(collection, { key, value }, newData) { | ||
try { | ||
const dbname = process.env.DB_NAME; | ||
const fileName = `${dbname}/${collection}.json`; | ||
if (fs_1.default.existsSync(fileName)) { | ||
// Read the file synchronously | ||
const jsonData = fs_1.default.readFileSync(fileName, 'utf-8'); | ||
// Parse the JSON data | ||
let data = JSON.parse(jsonData); | ||
// Find the index of the item to update | ||
const indexToUpdate = data.findIndex((item) => item[key] === value); | ||
if (indexToUpdate !== -1) { | ||
// Update the _id field from the existing data | ||
newData._id = data[indexToUpdate]._id; | ||
// Update the data at the found index | ||
data[indexToUpdate] = newData; | ||
// Write the updated data back to the file | ||
fs_1.default.writeFileSync(fileName, JSON.stringify(data)); | ||
return `Item with ${key} '${value}' updated in collection '${collection}'.`; | ||
} | ||
else { | ||
return `Item with ${key} '${value}' not found in collection '${collection}'.`; | ||
} | ||
} | ||
else { | ||
return `Collection '${collection}' does not exist.`; | ||
} | ||
} | ||
catch (error) { | ||
console.error('Error updating data:', error); | ||
return null; | ||
} | ||
} | ||
collection_1.update = update; | ||
})(collection || (exports.collection = collection = {})); | ||
//# sourceMappingURL=collection.js.map |
{ | ||
"name": "sreya", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Sreya is a versatile in-memory database for Node.js applications.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -71,3 +71,3 @@ import fs from 'fs'; | ||
export async function remove(collection: string, identifier: string) { | ||
export async function remove(collection: string, { key, value }: { key: string, value: any }): Promise<string | null> { | ||
try { | ||
@@ -84,8 +84,11 @@ const dbname = process.env.DB_NAME; | ||
// Find the index of the item with the given identifier | ||
const index = data.findIndex((item: any) => item._id === identifier); | ||
// Find the index of the item to remove | ||
const indexToRemove = data.findIndex((item: any) => item[key] === value); | ||
if (index !== -1) { | ||
// console.log(indexToRemove); | ||
if (indexToRemove !== -1) { | ||
// Remove the item from the array | ||
data.splice(index, 1); | ||
data.splice(indexToRemove, 1); | ||
@@ -95,5 +98,5 @@ // Write the updated data back to the file | ||
return 'Data removed successfully.'; | ||
return `Item with ${key} '${value}' removed from collection '${collection}'.`; | ||
} else { | ||
return `Item with identifier '${identifier}' not found in collection '${collection}'.`; | ||
return `Item with ${key} '${value}' not found in collection '${collection}'.`; | ||
} | ||
@@ -125,2 +128,40 @@ } else { | ||
} | ||
export async function update(collection: string, { key, value }: { key: string, value: any }, newData: any): Promise<string | null> { | ||
try { | ||
const dbname = process.env.DB_NAME; | ||
const fileName = `${dbname}/${collection}.json`; | ||
if (fs.existsSync(fileName)) { | ||
// Read the file synchronously | ||
const jsonData = fs.readFileSync(fileName, 'utf-8'); | ||
// Parse the JSON data | ||
let data = JSON.parse(jsonData); | ||
// Find the index of the item to update | ||
const indexToUpdate = data.findIndex((item: any) => item[key] === value); | ||
if (indexToUpdate !== -1) { | ||
// Update the _id field from the existing data | ||
newData._id = data[indexToUpdate]._id; | ||
// Update the data at the found index | ||
data[indexToUpdate] = newData; | ||
// Write the updated data back to the file | ||
fs.writeFileSync(fileName, JSON.stringify(data)); | ||
return `Item with ${key} '${value}' updated in collection '${collection}'.`; | ||
} else { | ||
return `Item with ${key} '${value}' not found in collection '${collection}'.`; | ||
} | ||
} else { | ||
return `Collection '${collection}' does not exist.`; | ||
} | ||
} catch (error) { | ||
console.error('Error updating data:', error); | ||
return null; | ||
} | ||
} | ||
} |
@@ -6,16 +6,39 @@ const { connect, collection } = require('../dist/index'); | ||
console.log(collections); | ||
collection.create('user') | ||
// collection.create('user') | ||
collection.insert('user', { | ||
name: 'hi' | ||
}) | ||
let col = await collection.find('user'); | ||
// console.log(col); | ||
console.log(col); | ||
// collection.insert('user', { | ||
// name: 'hi' | ||
// }) | ||
// collection.destroy('user') | ||
collection.destroy('user') | ||
// collection.remove('user', { key: 'name', value: 'hi' }) | ||
// .then(result => console.log(result)) | ||
// .catch(error => console.error(error)); | ||
// collection.remove('user', 'lvnqqt1h6m6z1') | ||
collection.update('user', { key: '_id', value: 'lvot0o72hbtd8' }, { | ||
name: 'hello' | ||
}) | ||
} | ||
clal() | ||
clal() | ||
// let data = [ | ||
// { name: 'hi', _id: 'lvosf5m010pbs' }, | ||
// { name: 'hi', _id: 'lvosf732940sd' }, | ||
// { name: 'hi', _id: 'lvosgl8jihfzd' }, | ||
// { name: 'hi', _id: 'lvosi3334xwbx' }, | ||
// { name: 'hi', _id: 'lvosihllrfz18' }, | ||
// { name: 'hi', _id: 'lvosj10iwcw8r' }, | ||
// { name: 'hi', _id: 'lvosmjm9ghjh8' }, | ||
// { name: 'hi', _id: 'lvosn4cu85gtg' }, | ||
// { name: 'hi', _id: 'lvosne0kicoxu' } | ||
// ]; | ||
// data.forEach(item => { | ||
// console.log(item["name"]); | ||
// }); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32254
15
532
9