xlsx-mongo-api
is a RESTful API built with Node.js and Express that allows you to import and add data from XLSX files to MongoDB. It serves as a convenient interface for utilizing the xlsx-mongo
package.
- Import data from XLSX file to MongoDB
- Add data from XLSX file to existing MongoDB collection
- Dynamic generation of MongoDB schema based on XLSX file headers
- Configurable MongoDB connection URL and collection name
- Support for handling large XLSX files
- Node.js and npm installed on your machine
- MongoDB server up and running
- XLSX files with data to import
- POST /import
- Description: Import data from an XLSX file to MongoDB
- Request Parameters:
- file (multipart/form-data): XLSX file to import
- collectionName (optional): Name of the collection to import/add the data to.
If not provided, it will use the filename as the collection name.
- file (multipart/form-data): XLSX file to import
- Response: JSON response with a success message or error message
- POST /add
- Description: Add data from an XLSX file to an existing MongoDB collection
- Request Parameters:
- file (multipart/form-data): XLSX file to import
- collectionName (optional): Name of the collection to import/add the data to.
If not provided, it will use the filename as the collection name.
- file (multipart/form-data): XLSX file to import
- Response: JSON response with a success message or error message
-
Start the server by running npm start.
-
Send a POST request to https://xlsxmongoapi.kurizu.repl.co/import with the following parameters:
-
file: The XLSX file to import.
-
collectionName (optional): The name of the collection to import/add the data to.
If not provided, it will use the filename as the collection name. -
Create a
.env
file in the root directory of the project and add the following environment variables:
MONGO_URL=<your-mongodb-connection-url>
MONGO_COLLECTION=<your-mongodb-collection-name>
Example using Axios:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const filePath = path.join('Test.xlsx');
require('dotenv').config();
// Import data from XLSX file to MongoDB
const importFile = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('mongoURL', process.env.MONGO_URL);
formData.append('collectionName', process.env.MONGO_COLLECTION);
const response = await axios.post('https://xlsxmongoapi.kurizu.repl.co/import', formData, {
headers: formData.getHeaders(),
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
importFile();
// Add data from XLSX file to existing MongoDB collection
const addData = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('path', filePath);
formData.append('collectionName', 'test2');
formData.append('mongoURL', process.env.MONGO_URL);
const response = await axios.post('https://xlsxmongoapi.kurizu.repl.co/add', formData, {
headers: formData.getHeaders(),
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
addData();
Example using RapidAPI:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const filePath = path.join('Test.xlsx');
require('dotenv').config();
// Import data from XLSX file to MongoDB
const importFile = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('mongoURL', process.env.MONGO_URL);
formData.append('collectionName', process.env.MONGO_COLLECTION);
const response = await axios.post('https://xlsxmongoapi.kurizu.repl.co/import', formData, {
headers: {
'content-type': `multipart/form-data; boundary=${formData._boundary}`,
'x-rapidapi-key': 'YOUR-RAPIDAPI-KEY',
'x-rapidapi-host': 'xlsx-mongo-api.p.rapidapi.com',
useQueryString: true,
},
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
importFile();
// Add data from XLSX file to existing MongoDB collection
const addData = async () => {
try {
const formData = new FormData();
formData.append('file', fs.createReadStream('Test.xlsx'));
formData.append('path', filePath);
formData.append('collectionName', 'test2');
formData.append('mongoURL', process.env.MONGO_URL);
const response = await axios.post('https://xlsxmongoapi.kurizu.repl.co/add', formData, {
headers: {
'content-type': `multipart/form-data; boundary=${formData._boundary}`,
'x-rapidapi-key': 'YOUR-RAPIDAPI-KEY',
'x-rapidapi-host': 'xlsx-mongo-api.p.rapidapi.com',
useQueryString: true,
},
});
console.log(response.data);
} catch (error) {
console.error(error.response.data);
}
};
addData();
Check https://github.com/crizmo/xlsx-mongo-api/blob/main/tests/test.js for detailed usage examples.
- Clone the repository:
git clone https://github.com/crizmo/xlsx-mongo-api.git
- Install dependencies:
npm install
- Create a
.env
file in the root directory of the project and add the following environment variables:
MONGO_URL=<your-mongodb-connection-url>
MONGO_COLLECTION=<your-mongodb-collection-name>
- Start the server:
npm start
Contributions are welcome! If you have any ideas, suggestions, or bug fixes, please open an issue or a pull request.
Distributed under the MIT License. See LICENSE for more information. Contact
Package Maintainer: crizmo on GitHub Project Link: https://github.com/crizmo/xlsx-mongo-api