Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate database #89

Merged
merged 2 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { Faker } from '.';

export class Database {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Database.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}

// TODO @Shinigami92 2022-01-11: We should find a better strategy as assigning this property to a function
// @ts-expect-error
this.column.schema = {
description: 'Generates a column name.',
sampleResults: ['id', 'title', 'createdAt'],
};
// @ts-expect-error
this.type.schema = {
description: 'Generates a column type.',
sampleResults: ['byte', 'int', 'varchar', 'timestamp'],
};
// @ts-expect-error
this.collation.schema = {
description: 'Generates a collation.',
sampleResults: ['utf8_unicode_ci', 'utf8_bin'],
};
// @ts-expect-error
this.engine.schema = {
description: 'Generates a storage engine.',
sampleResults: ['MyISAM', 'InnoDB'],
};
}

/**
* column
*
* @method faker.database.column
*/
column() {
return this.faker.random.arrayElement(
this.faker.definitions.database.column
);
}

/**
* type
*
* @method faker.database.type
*/
type() {
return this.faker.random.arrayElement(this.faker.definitions.database.type);
}

/**
* collation
*
* @method faker.database.collation
*/
collation() {
return this.faker.random.arrayElement(
this.faker.definitions.database.collation
);
}

/**
* engine
*
* @method faker.database.engine
*/
engine() {
return this.faker.random.arrayElement(
this.faker.definitions.database.engine
);
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Database } from './database';
import { Datatype } from './datatype';
import { _Date } from './date';
import { Fake } from './fake';
Expand Down Expand Up @@ -174,7 +175,7 @@ export class Faker {
readonly animal = new (require('./animal'))(this);
readonly commerce = new (require('./commerce'))(this);
readonly company = new (require('./company'))(this);
readonly database = new (require('./database'))(this);
readonly database: Database = new Database(this);
readonly date: _Date = new _Date(this);
readonly finance = new (require('./finance'))(this);
readonly git: Git = new Git(this);
Expand Down