Skip to content

Commit

Permalink
Merge pull request #51 from AbdulAhadArain/develop
Browse files Browse the repository at this point in the history
added migration file and updated queries for sorting
  • Loading branch information
AbdulAhadArain authored Oct 22, 2024
2 parents 6e24ac2 + c409773 commit c8a463f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"main": "src/server.ts",
"license": "MIT",
"scripts": {
"dev": "cross-env NODE_ENV=development nodemon && ts-node src/server.ts"
"dev": "cross-env NODE_ENV=development nodemon && ts-node src/server.ts",
"migration": "cross-env NODE_ENV=development ts-node src/migration/timestampToDate.ts"
},
"devDependencies": {
"@types/cors": "^2.8.13",
Expand Down
120 changes: 120 additions & 0 deletions src/migration/timestampToDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import mongoose from 'mongoose';
import config from '../config/config';
import {
QuantumPortalBlockModel,
QuantumPortalTransactionModel,
} from '../models';

const migrateTimestampsForBlocks = async () => {
console.log('Blocks Migration started.');
try {
const docCount = await QuantumPortalBlockModel.countDocuments({
dateTimestamp: { $exists: false },
}).exec();
const limit = 100;
console.log(`Total documents to migrate: ${docCount}`);

for (let page = 1; page <= Math.ceil(docCount / limit); page++) {
console.log(`Processing page ${page} of ${Math.ceil(docCount / limit)}`);
// Fetch all documents where dateTimestamp is not set
const docs = await QuantumPortalBlockModel.find({
dateTimestamp: { $exists: false },
})
.limit(limit)
.skip((page - 1) * limit)
.exec();

for (const doc of docs) {
try {
// Convert the string timestamp to a Date object
const date = new Date(doc.timestamp);
console.log(`Document ${doc._id} timestamp: ${doc.timestamp}`);
if (date) {
// Update the document with the new Date field
await QuantumPortalBlockModel.updateOne(
{ _id: doc._id },
{ $set: { dateTimestamp: date } },
{ new: true },
);
console.log(`Document ${doc._id} updated successfully.`);
} else {
console.error(
`Invalid date for document ${doc._id}: ${doc.timestamp}`,
);
}
} catch (error) {
console.error(`Error processing document ${doc._id}:`, error);
}
}
}
console.log('Blocks Migration completed successfully.');
} catch (err) {
console.error('Error during migration:', err);
}
};
const migrateTimestampsForTransactions = async () => {
console.log('Transactions Migration started.');
try {
const docCount = await QuantumPortalTransactionModel.countDocuments({
dateTimestamp: { $exists: false },
}).exec();
const limit = 100;
console.log(`Total documents to migrate: ${docCount}`);

for (let page = 1; page <= Math.ceil(docCount / limit); page++) {
console.log(`Processing page ${page} of ${Math.ceil(docCount / limit)}`);
// Fetch all documents where dateTimestamp is not set
const docs = await QuantumPortalTransactionModel.find({
dateTimestamp: { $exists: false },
})
.limit(limit)
.skip((page - 1) * limit)
.exec();

for (const doc of docs) {
try {
// Convert the string timestamp to a Date object
const date = new Date(doc.timestamp);
console.log(`Document ${doc._id} timestamp: ${doc.timestamp}`);
if (date) {
// Update the document with the new Date field
await QuantumPortalTransactionModel.updateOne(
{ _id: doc._id },
{ $set: { dateTimestamp: date } },
{ new: true },
);
console.log(`Document ${doc._id} updated successfully.`);
} else {
console.error(
`Invalid date for document ${doc._id}: ${doc.timestamp}`,
);
}
} catch (error) {
console.error(`Error processing document ${doc._id}:`, error);
}
}
}
console.log('Transactions Migration completed successfully.');
} catch (err) {
console.error('Error during migration:', err);
}
};

mongoose
.connect(config.mongoose.url)
.then(async () => {
console.info('Connected to MongoDB');
migrateTimestampsForTransactions().then(() => {
console.info('Transactions Migration completed.');
migrateTimestampsForBlocks().then(() => {
console.info('Blocks Migration completed.');
mongoose.connection.close();
process.exit();
});
});
})
.catch(err => {
console.error(
`MongoDB connection error. Please make sure MongoDB is running. ${err}`,
);
});
3 changes: 2 additions & 1 deletion src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ export const getRecentBlocks = async (
limit: number,
): Promise<any> => {
const docsPromise = await QuantumPortalBlockModel.find()
.sort({ timestamp: -1 })
.sort({ dateTimestamp: -1 })
.skip((page - 1) * limit)
.limit(limit);

const countPromise = QuantumPortalTransactionModel.countDocuments().exec();

const [totalResults, results] = await Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion src/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const getTransferTokensTxs = async (
query.$or = [{ from: address }, { to: address }];
}
const docsPromise = QuantumPortalTransactionModel.find(query)
.sort({ timestamp: -1 })
.sort({ dateTimestamp: -1 })
.skip((page - 1) * limit)
.limit(limit);
const countPromise =
Expand Down

0 comments on commit c8a463f

Please sign in to comment.