1.0.0 • Published 9 months ago

generathor-db-testing v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Generathor-DB

Generathor-DB retrieves the structure of a database, including tables, columns, indexes, relations, and primary keys in a standardized format. This allows for the automation of code generation, such as models, controllers, migrations, and more, based on the database schema.

How does it work?

Generathor-DB requires a configuration object to connect to the database. Optionally, you can pass an array of transformers to modify the data before it is used to generate files.

import { Source } from 'generathor-db';

const source = new Source({
  type: 'mysql',
  configuration: {
    host: 'localhost',
    port: '3306',
    user: 'root',
    password: 'password',
    database: 'generathor'
  }
}, [
    (item) => {
      item.columns.forEach(column => {
        column.calculated = column.name.toUpperCase(); // Example transformation
      });
    }
]);

Parameters

VariableRequiredTypeDescription
sourceConfigurationYesSourceConfigurationConfiguration to connect to the database.
transformerNoTransformer[]Array of functions to modify items before usage.

SourceConfiguration

VariableRequiredTypeDescription
typeYes'mysql'At the moment we only support mysql.
configurationYesMySQLConfigurationConfiguration for the database.

MySQLConfiguration

See mysql2 for more information.

Transformer

Callback that receives the items and allows you to modify or add information for file generation.

Item format

This source retrieves an array of items, and each item has information about one table of the database.

VariableRequiredTypeDescription
tableYesstringName of the table.
schemaYesstringName of the schema.
columnsYesColumn[]List of columns in the table.
indexesYesIndex[]List of indexes in the table.
relationsYesRelation[]List of table relations.
primaryKeyYesobjectPrimary key of the table.
primaryKey.columnsYesstring[]List of columns in the primary key.

Column

VariableRequiredTypeDescription
nameYesstringName of the column.
nullableYesbooleanIndicates if column allows null values.
defaultYesstringDefault value of the column.
commentYesstringComment or description of the column.
typeYesstringType of column (e.g., int, varchar).
subTypeNostringSubtype of the column, if applicable.
unsignedNobooleanIndicates if the column is unsigned.
enumNostring[]List of valid values for enum types.
sizeNonumberSize of the column (if applicable).
autoincrementNobooleanIndicates if the column auto-increments.
scaleNonumberDecimal scale for numeric columns.

Index

VariableRequiredTypeDescription
typeYes'unique' or 'index'Type of index (unique or regular).
columnsYesstring[]Columns involved in the index.
indexYesstringName of the index.

Relation

VariableRequiredTypeDescription
typeYes'belongs-to' or 'has-many'Type of relationship between tables.
columnsYesstring[]Columns involved in the relationship.
referencesYesstring[]Columns referenced in the related table.
onYesobjectDetails of the related table.
on.databaseYesstringName of the database where the related table resides.
on.tableYesstringName of the related table.

TODO

  • Add support for additional database engines (PostgreSQL, SQLite, etc.).
1.0.0

9 months ago