Skip to content

Commit 4d4653e

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate database (#89)
1 parent 3c3e567 commit 4d4653e

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/database.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import type { Faker } from '.';
2+
3+
export class Database {
4+
constructor(private readonly faker: Faker) {
5+
// Bind `this` so namespaced is working correctly
6+
for (const name of Object.getOwnPropertyNames(Database.prototype)) {
7+
if (name === 'constructor' || typeof this[name] !== 'function') {
8+
continue;
9+
}
10+
this[name] = this[name].bind(this);
11+
}
12+
13+
// TODO @Shinigami92 2022-01-11: We should find a better strategy as assigning this property to a function
14+
// @ts-expect-error
15+
this.column.schema = {
16+
description: 'Generates a column name.',
17+
sampleResults: ['id', 'title', 'createdAt'],
18+
};
19+
// @ts-expect-error
20+
this.type.schema = {
21+
description: 'Generates a column type.',
22+
sampleResults: ['byte', 'int', 'varchar', 'timestamp'],
23+
};
24+
// @ts-expect-error
25+
this.collation.schema = {
26+
description: 'Generates a collation.',
27+
sampleResults: ['utf8_unicode_ci', 'utf8_bin'],
28+
};
29+
// @ts-expect-error
30+
this.engine.schema = {
31+
description: 'Generates a storage engine.',
32+
sampleResults: ['MyISAM', 'InnoDB'],
33+
};
34+
}
35+
36+
/**
37+
* column
38+
*
39+
* @method faker.database.column
40+
*/
41+
column() {
42+
return this.faker.random.arrayElement(
43+
this.faker.definitions.database.column
44+
);
45+
}
46+
47+
/**
48+
* type
49+
*
50+
* @method faker.database.type
51+
*/
52+
type() {
53+
return this.faker.random.arrayElement(this.faker.definitions.database.type);
54+
}
55+
56+
/**
57+
* collation
58+
*
59+
* @method faker.database.collation
60+
*/
61+
collation() {
62+
return this.faker.random.arrayElement(
63+
this.faker.definitions.database.collation
64+
);
65+
}
66+
67+
/**
68+
* engine
69+
*
70+
* @method faker.database.engine
71+
*/
72+
engine() {
73+
return this.faker.random.arrayElement(
74+
this.faker.definitions.database.engine
75+
);
76+
}
77+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Database } from './database';
12
import { Datatype } from './datatype';
23
import { _Date } from './date';
34
import { Fake } from './fake';
@@ -174,7 +175,7 @@ export class Faker {
174175
readonly animal = new (require('./animal'))(this);
175176
readonly commerce = new (require('./commerce'))(this);
176177
readonly company = new (require('./company'))(this);
177-
readonly database = new (require('./database'))(this);
178+
readonly database: Database = new Database(this);
178179
readonly date: _Date = new _Date(this);
179180
readonly finance = new (require('./finance'))(this);
180181
readonly git: Git = new Git(this);

0 commit comments

Comments
 (0)