mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 13:29:13 -04:00
feat(new tool): Mongo ObjectId DateTime Converter
Fix https://github.com/CorentinTh/it-tools/discussions/789
This commit is contained in:
parent
e1b4f9aafe
commit
f7b024a74a
6 changed files with 137 additions and 0 deletions
2
components.d.ts
vendored
2
components.d.ts
vendored
|
@ -126,10 +126,12 @@ declare module '@vue/runtime-core' {
|
||||||
MenuLayout: typeof import('./src/components/MenuLayout.vue')['default']
|
MenuLayout: typeof import('./src/components/MenuLayout.vue')['default']
|
||||||
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.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']
|
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']
|
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
|
||||||
NCode: typeof import('naive-ui')['NCode']
|
NCode: typeof import('naive-ui')['NCode']
|
||||||
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
|
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
|
||||||
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
|
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
|
||||||
|
NDatePicker: typeof import('naive-ui')['NDatePicker']
|
||||||
NDivider: typeof import('naive-ui')['NDivider']
|
NDivider: typeof import('naive-ui')['NDivider']
|
||||||
NEllipsis: typeof import('naive-ui')['NEllipsis']
|
NEllipsis: typeof import('naive-ui')['NEllipsis']
|
||||||
NFormItem: typeof import('naive-ui')['NFormItem']
|
NFormItem: typeof import('naive-ui')['NFormItem']
|
||||||
|
|
|
@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
||||||
|
|
||||||
import { tool as textToUnicode } from './text-to-unicode';
|
import { tool as textToUnicode } from './text-to-unicode';
|
||||||
import { tool as safelinkDecoder } from './safelink-decoder';
|
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 pdfSignatureChecker } from './pdf-signature-checker';
|
||||||
import { tool as numeronymGenerator } from './numeronym-generator';
|
import { tool as numeronymGenerator } from './numeronym-generator';
|
||||||
import { tool as macAddressGenerator } from './mac-address-generator';
|
import { tool as macAddressGenerator } from './mac-address-generator';
|
||||||
|
@ -148,6 +149,7 @@ export const toolsByCategory: ToolCategory[] = [
|
||||||
dockerRunToDockerComposeConverter,
|
dockerRunToDockerComposeConverter,
|
||||||
xmlFormatter,
|
xmlFormatter,
|
||||||
yamlViewer,
|
yamlViewer,
|
||||||
|
mongoObjectidConverter,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
12
src/tools/mongo-objectid-converter/index.ts
Normal file
12
src/tools/mongo-objectid-converter/index.ts
Normal file
|
@ -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'),
|
||||||
|
});
|
|
@ -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")}})');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -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);
|
||||||
|
};
|
|
@ -0,0 +1,68 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { dateFromObjectId, generateMongoFilter, objectIdFromDate, objectIdSyntaxFromDate } from './mongo-objectid-converter.service';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
|
|
||||||
|
const currentTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
|
||||||
|
const objectIdInput = ref(objectIdFromDate(new Date()));
|
||||||
|
const dateOutput = computed(() =>
|
||||||
|
withDefaultOnError(() => dateFromObjectId(objectIdInput.value), 'Invalid ObjectId'),
|
||||||
|
);
|
||||||
|
|
||||||
|
const dateInput = ref(Date.now());
|
||||||
|
const dateValue = computed(() => new Date(dateInput.value));
|
||||||
|
const tableName = ref('tbl');
|
||||||
|
const objectIdOutput = computed(() =>
|
||||||
|
withDefaultOnError(() => objectIdFromDate(dateValue.value), 'Invalid Date'),
|
||||||
|
);
|
||||||
|
const objectIdSyntaxOutput = computed(() =>
|
||||||
|
withDefaultOnError(() => objectIdSyntaxFromDate(dateValue.value), 'Invalid Date'),
|
||||||
|
);
|
||||||
|
const objectIdQueryOutput = computed(() =>
|
||||||
|
withDefaultOnError(() => generateMongoFilter({ date: dateValue.value, tableName: tableName.value }), 'Invalid Date'),
|
||||||
|
);
|
||||||
|
const objectIdUTCDate = computed(() =>
|
||||||
|
dateValue.value.toISOString(),
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<c-card :title="`ObjectId to Date (${currentTimeZone})`">
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="objectIdInput"
|
||||||
|
placeholder="Put your ObjectId here..."
|
||||||
|
label="ObjectId to encode"
|
||||||
|
raw-text
|
||||||
|
mb-5
|
||||||
|
/>
|
||||||
|
|
||||||
|
<n-divider />
|
||||||
|
|
||||||
|
<textarea-copyable
|
||||||
|
:value="dateOutput instanceof Date ? dateOutput.toLocaleString(undefined, { timeZoneName: 'short' }) : dateOutput"
|
||||||
|
/>
|
||||||
|
<textarea-copyable
|
||||||
|
:value="dateOutput instanceof Date ? dateOutput.toISOString() : dateOutput"
|
||||||
|
/>
|
||||||
|
</c-card>
|
||||||
|
|
||||||
|
<c-card :title="`Date to ObjectId (${currentTimeZone})`">
|
||||||
|
<n-form-item label="Date and time" label-placement="left" mb-2 flex-1>
|
||||||
|
<n-date-picker v-model:value="dateInput" type="datetime" />
|
||||||
|
</n-form-item>
|
||||||
|
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="tableName"
|
||||||
|
placeholder="Put your Table Name here..."
|
||||||
|
label="Table Name"
|
||||||
|
label-position="left"
|
||||||
|
raw-text
|
||||||
|
mb-5
|
||||||
|
/>
|
||||||
|
|
||||||
|
<textarea-copyable :value="objectIdUTCDate" />
|
||||||
|
<textarea-copyable :value="objectIdOutput" />
|
||||||
|
<textarea-copyable :value="objectIdSyntaxOutput" />
|
||||||
|
<textarea-copyable :value="objectIdQueryOutput" />
|
||||||
|
</c-card>
|
||||||
|
</template>
|
Loading…
Add table
Add a link
Reference in a new issue