diff --git a/components.d.ts b/components.d.ts index f2c3146f..7f2a9a61 100644 --- a/components.d.ts +++ b/components.d.ts @@ -126,10 +126,12 @@ declare module '@vue/runtime-core' { MenuLayout: typeof import('./src/components/MenuLayout.vue')['default'] MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default'] MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default'] + MongoObjectidConverter: typeof import('./src/tools/mongo-objectid-converter/mongo-objectid-converter.vue')['default'] NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default'] NCode: typeof import('naive-ui')['NCode'] NCollapseTransition: typeof import('naive-ui')['NCollapseTransition'] NConfigProvider: typeof import('naive-ui')['NConfigProvider'] + NDatePicker: typeof import('naive-ui')['NDatePicker'] NDivider: typeof import('naive-ui')['NDivider'] NEllipsis: typeof import('naive-ui')['NEllipsis'] NFormItem: typeof import('naive-ui')['NFormItem'] diff --git a/src/tools/index.ts b/src/tools/index.ts index aa861c93..78e43570 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer'; import { tool as textToUnicode } from './text-to-unicode'; import { tool as safelinkDecoder } from './safelink-decoder'; +import { tool as mongoObjectidConverter } from './mongo-objectid-converter'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as macAddressGenerator } from './mac-address-generator'; @@ -148,6 +149,7 @@ export const toolsByCategory: ToolCategory[] = [ dockerRunToDockerComposeConverter, xmlFormatter, yamlViewer, + mongoObjectidConverter, ], }, { diff --git a/src/tools/mongo-objectid-converter/index.ts b/src/tools/mongo-objectid-converter/index.ts new file mode 100644 index 00000000..1a9a0fc7 --- /dev/null +++ b/src/tools/mongo-objectid-converter/index.ts @@ -0,0 +1,12 @@ +import { Database } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'MongoDB ObjectId Converter', + path: '/mongo-objectid-converter', + description: 'Convert between MongoDB ObjectId and internal timestamp', + keywords: ['mongo', 'objectid', 'converter', 'timestamp'], + component: () => import('./mongo-objectid-converter.vue'), + icon: Database, + createdAt: new Date('2024-08-15'), +}); diff --git a/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.test.ts b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.test.ts new file mode 100644 index 00000000..28075aee --- /dev/null +++ b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest'; +import { dateFromObjectId, generateMongoFilter, objectIdFromDate, objectIdSyntaxFromDate } from './mongo-objectid-converter.service'; + +describe('mongo-objectid-converter', () => { + describe('objectIdFromDate', () => { + it('convert a Date to ObjectId', () => { + expect(objectIdFromDate(new Date(Date.UTC(2024, 0, 1)))).to.eql('659200800000000000000000'); + expect(objectIdFromDate(new Date(Date.UTC(2024, 0, 1, 12, 12, 12)))).to.eql('6592ac1c0000000000000000'); + }); + }); + describe('objectIdSyntaxFromDate', () => { + it('convert a Date to ObjectId', () => { + expect(objectIdSyntaxFromDate(new Date(Date.UTC(2024, 0, 1)))).to.eql('ObjectId("659200800000000000000000")'); + }); + }); + describe('dateFromObjectId', () => { + it('convert an ObjectId to Date', () => { + expect(dateFromObjectId('659200800000000000000000')).to.eql(new Date(Date.UTC(2024, 0, 1))); + expect(dateFromObjectId('6592ac1c0000000000000000')).to.eql(new Date(Date.UTC(2024, 0, 1, 12, 12, 12))); + }); + }); + describe('generateMongoFilter', () => { + it('convert a date to mongo query', () => { + expect(generateMongoFilter({ + date: new Date(Date.UTC(2024, 0, 1, 12, 12, 12)), + tableName: 'comments', + })).to.eql('db.comments.find({_id: {$gt: ObjectId("6592ac1c0000000000000000")}})'); + }); + }); +}); diff --git a/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.ts b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.ts new file mode 100644 index 00000000..e3ac5c06 --- /dev/null +++ b/src/tools/mongo-objectid-converter/mongo-objectid-converter.service.ts @@ -0,0 +1,23 @@ +export function objectIdFromDate(date: Date) { + return `${Math.floor(date.getTime() / 1000).toString(16)}0000000000000000`; +}; + +export function objectIdSyntaxFromDate(date: Date) { + return `ObjectId("${objectIdFromDate(date)}")`; +}; + +export function generateMongoFilter( + { + tableName, date, operator = 'gt', + }: + { + tableName: string + date: Date + operator?: 'gt' | 'lt' + }) { + return `db.${tableName}.find({_id: {$${operator}: ObjectId("${objectIdFromDate(date)}")}})`; +} + +export function dateFromObjectId(objectId: string) { + return new Date(Number.parseInt(objectId.substring(0, 8), 16) * 1000); +}; diff --git a/src/tools/mongo-objectid-converter/mongo-objectid-converter.vue b/src/tools/mongo-objectid-converter/mongo-objectid-converter.vue new file mode 100644 index 00000000..825e7bf1 --- /dev/null +++ b/src/tools/mongo-objectid-converter/mongo-objectid-converter.vue @@ -0,0 +1,68 @@ + + +