Skip to content

Commit a5b3888

Browse files
authored
feat: Add MongoDB ObjectId generation (#616)
1 parent 20f33e6 commit a5b3888

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/database.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,15 @@ export class Database {
5959
this.faker.definitions.database.engine
6060
);
6161
}
62+
63+
/**
64+
* Returns a MongoDB [ObjectId](https://docs.mongodb.com/manual/reference/method/ObjectId/) string.
65+
*
66+
* @example
67+
* faker.database.mongodbObjectId() // 'e175cac316a79afdd0ad3afb'
68+
*/
69+
mongodbObjectId(): string {
70+
// strip the "0x" from the hexadecimal output
71+
return this.faker.datatype.hexadecimal(24).replace('0x', '').toLowerCase();
72+
}
6273
}

test/database.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const seededRuns = [
99
type: 'smallint',
1010
collation: 'utf8_bin',
1111
engine: 'MEMORY',
12+
mongodbObjectId: '8be4abdd39321ad7d3fe01ff',
1213
},
1314
},
1415
{
@@ -18,6 +19,7 @@ const seededRuns = [
1819
type: 'time',
1920
collation: 'utf8_general_ci',
2021
engine: 'MyISAM',
22+
mongodbObjectId: '5c346ba075bd57f5a62b82d7',
2123
},
2224
},
2325
{
@@ -27,13 +29,20 @@ const seededRuns = [
2729
type: 'geometry',
2830
collation: 'cp1250_general_ci',
2931
engine: 'ARCHIVE',
32+
mongodbObjectId: 'eadb42f0e3f4a973fab0aeef',
3033
},
3134
},
3235
];
3336

3437
const NON_SEEDED_BASED_RUN = 5;
3538

36-
const functionNames = ['column', 'type', 'collation', 'engine'];
39+
const functionNames = [
40+
'column',
41+
'type',
42+
'collation',
43+
'engine',
44+
'mongodbObjectId',
45+
];
3746

3847
describe('database', () => {
3948
afterEach(() => {
@@ -95,6 +104,13 @@ describe('database', () => {
95104
expect(faker.definitions.database.type).toContain(type);
96105
});
97106
});
107+
108+
describe('mongodbObjectId', () => {
109+
it('should generate a MongoDB ObjectId value', () => {
110+
const generateObjectId = faker.database.mongodbObjectId();
111+
expect(generateObjectId).toBeTypeOf('string');
112+
});
113+
});
98114
}
99115
});
100116
});

0 commit comments

Comments
 (0)