jsondb-client
Advanced tools
Comparing version
204
index.js
@@ -0,4 +1,6 @@ | ||
// jsondb-client/index.js | ||
const axios = require('axios'); | ||
class JsonDBClient { | ||
class JsonDbClient { | ||
constructor({ baseurl, username, password, database }) { | ||
@@ -9,110 +11,190 @@ this.baseurl = baseurl; | ||
this.database = database; | ||
this.axiosInstance = axios.create({ | ||
baseURL: this.baseurl, | ||
auth: { | ||
username: this.username, | ||
password: this.password | ||
} | ||
}); | ||
} | ||
getCollection(name) { | ||
return new Collection(this.baseurl, this.username, this.password, this.database, name); | ||
static setConnection(options) { | ||
return new JsonDbClient(options); | ||
} | ||
collection(name) { | ||
return new Collection(this.axiosInstance, this.database, name); | ||
} | ||
} | ||
class Collection { | ||
constructor(baseurl, username, password, database, name) { | ||
this.baseurl = `${baseurl}/${database}/${name}`; | ||
this.username = username; | ||
this.password = password; | ||
constructor(axiosInstance, database, collectionName) { | ||
this.axiosInstance = axiosInstance; | ||
this.database = database; | ||
this.collectionName = collectionName; | ||
} | ||
async request(method, endpoint, data) { | ||
async find(query = {}) { | ||
try { | ||
const response = await axios({ | ||
method, | ||
url: `${this.baseurl}/${endpoint}`, | ||
data, | ||
auth: { | ||
username: this.username, | ||
password: this.password | ||
} | ||
}); | ||
const response = await this.axiosInstance.get(`/db/${this.database}/${this.collectionName}`, { params: query }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(error.response ? error.response.data.error : error.message); | ||
throw new Error(`Failed to find documents: ${error.message}`); | ||
} | ||
} | ||
find(query) { | ||
return this.request('GET', `find`, query); | ||
async findOne(query) { | ||
try { | ||
const response = await this.axiosInstance.get(`/db/${this.database}/${this.collectionName}/findOne`, { params: query }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to find document: ${error.message}`); | ||
} | ||
} | ||
findOne(query) { | ||
return this.request('GET', `findOne`, query); | ||
async findById(id) { | ||
try { | ||
const response = await this.axiosInstance.get(`/db/${this.database}/${this.collectionName}/${id}`); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to find document by ID: ${error.message}`); | ||
} | ||
} | ||
findById(id) { | ||
return this.request('GET', `findById/${id}`); | ||
async create(document) { | ||
try { | ||
const response = await this.axiosInstance.post(`/db/${this.database}/${this.collectionName}`, document); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to create document: ${error.message}`); | ||
} | ||
} | ||
create(data) { | ||
return this.request('POST', 'create', data); | ||
async save(document) { | ||
try { | ||
const response = await this.axiosInstance.put(`/db/${this.database}/${this.collectionName}`, document); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to save document: ${error.message}`); | ||
} | ||
} | ||
save(data) { | ||
return this.request('POST', 'save', data); | ||
async update(id, update) { | ||
try { | ||
const response = await this.axiosInstance.patch(`/db/${this.database}/${this.collectionName}/${id}`, update); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to update document: ${error.message}`); | ||
} | ||
} | ||
update(id, data) { | ||
return this.request('PUT', `update/${id}`, data); | ||
async updateOne(query, update) { | ||
try { | ||
const response = await this.axiosInstance.patch(`/db/${this.database}/${this.collectionName}/updateOne`, { query, update }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to update one document: ${error.message}`); | ||
} | ||
} | ||
updateOne(query, data) { | ||
return this.request('PUT', `updateOne`, { query, data }); | ||
async updateMany(query, update) { | ||
try { | ||
const response = await this.axiosInstance.patch(`/db/${this.database}/${this.collectionName}/updateMany`, { query, update }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to update many documents: ${error.message}`); | ||
} | ||
} | ||
updateMany(query, data) { | ||
return this.request('PUT', `updateMany`, { query, data }); | ||
async findByIdAndUpdate(id, update) { | ||
try { | ||
const response = await this.axiosInstance.patch(`/db/${this.database}/${this.collectionName}/${id}`, update); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to find and update document: ${error.message}`); | ||
} | ||
} | ||
findByIdAndUpdate(id, data) { | ||
return this.request('PUT', `findByIdAndUpdate/${id}`, data); | ||
async findOneAndUpdate(query, update) { | ||
try { | ||
const response = await this.axiosInstance.patch(`/db/${this.database}/${this.collectionName}/findOneAndUpdate`, { query, update }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to find one and update document: ${error.message}`); | ||
} | ||
} | ||
findOneAndUpdate(query, data) { | ||
return this.request('PUT', `findOneAndUpdate`, { query, data }); | ||
async deleteOne(query) { | ||
try { | ||
const response = await this.axiosInstance.delete(`/db/${this.database}/${this.collectionName}/deleteOne`, { data: query }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to delete one document: ${error.message}`); | ||
} | ||
} | ||
deleteOne(query) { | ||
return this.request('DELETE', `deleteOne`, query); | ||
async deleteMany(query) { | ||
try { | ||
const response = await this.axiosInstance.delete(`/db/${this.database}/${this.collectionName}/deleteMany`, { data: query }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to delete many documents: ${error.message}`); | ||
} | ||
} | ||
deleteMany(query) { | ||
return this.request('DELETE', `deleteMany`, query); | ||
async findByIdAndDelete(id) { | ||
try { | ||
const response = await this.axiosInstance.delete(`/db/${this.database}/${this.collectionName}/${id}`); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to find and delete document by ID: ${error.message}`); | ||
} | ||
} | ||
findByIdAndDelete(id) { | ||
return this.request('DELETE', `findByIdAndDelete/${id}`); | ||
async findOneAndDelete(query) { | ||
try { | ||
const response = await this.axiosInstance.delete(`/db/${this.database}/${this.collectionName}/findOneAndDelete`, { data: query }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to find one and delete document: ${error.message}`); | ||
} | ||
} | ||
findOneAndDelete(query) { | ||
return this.request('DELETE', `findOneAndDelete`, query); | ||
async countDocuments(query = {}) { | ||
try { | ||
const response = await this.axiosInstance.get(`/db/${this.database}/${this.collectionName}/count`, { params: query }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to count documents: ${error.message}`); | ||
} | ||
} | ||
countDocuments(query) { | ||
return this.request('GET', `countDocuments`, query); | ||
async distinct(field) { | ||
try { | ||
const response = await this.axiosInstance.get(`/db/${this.database}/${this.collectionName}/distinct`, { params: { field } }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to get distinct values: ${error.message}`); | ||
} | ||
} | ||
distinct(field, query) { | ||
return this.request('GET', `distinct/${field}`, query); | ||
async aggregate(pipeline) { | ||
try { | ||
const response = await this.axiosInstance.post(`/db/${this.database}/${this.collectionName}/aggregate`, pipeline); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to aggregate documents: ${error.message}`); | ||
} | ||
} | ||
aggregate(pipeline) { | ||
return this.request('POST', `aggregate`, pipeline); | ||
async populate(field) { | ||
try { | ||
const response = await this.axiosInstance.get(`/db/${this.database}/${this.collectionName}/populate`, { params: { field } }); | ||
return response.data; | ||
} catch (error) { | ||
throw new Error(`Failed to populate field: ${error.message}`); | ||
} | ||
} | ||
populate(field, query) { | ||
return this.request('GET', `populate/${field}`, query); | ||
} | ||
exec() { | ||
return this.request('GET', `exec`); | ||
} | ||
} | ||
module.exports = JsonDBClient; | ||
module.exports = JsonDbClient; |
{ | ||
"name": "jsondb-client", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"main": "src/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
8310
97.62%175
88.17%