|
| 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 | +} |
0 commit comments