entity-baker
Version:
Generates simple and powerful entity classes for ORM systems, like Doctrine and/or Entity Framework.
274 lines (273 loc) • 7.52 kB
TypeScript
/**
* Stores compiler callbacks.
*/
export interface CompilerCallbacks {
/**
* Is invoked before a class is being generated.
*
* @param {string} className The name of the class.
* @param {EntityFramework} target The target framework.
*/
readonly onBeforeGenerateClass?: (className: string, target: EntityFramework) => void | PromiseLike<void>;
/**
* Is invoked after a class has been generated.
*
* @param {any} err The error (if occurred).
* @param {string} className The name of the class.
* @param {EntityFramework} target The target framework.
*/
readonly onClassGenerated?: (err: any, className: string, target: EntityFramework) => void | PromiseLike<void>;
}
/**
* Settings / description of an entity (class).
*/
export interface EntityClass {
/**
* Table columns.
*/
readonly columns?: EntityColumnDescriptions;
/**
* The (custom) name of the underlying name.
*/
readonly table?: string;
}
/**
* Stores method names for columns.
*/
export declare type EntityClassMethodNames = {
[columnName: string]: string;
};
/**
* Describes an entity column.
*/
export interface EntityColumn {
/**
* Is auto generated value or not.
*/
readonly auto?: boolean;
/**
* Is ID value or not.
*/
readonly id?: boolean;
/**
* Can be (null) or not.
*/
readonly 'null'?: boolean;
/**
* The data type.
*/
readonly type?: string;
}
/**
* An entity column description entry.
*/
export declare type EntityColumnDescriptionEntry = string | EntityColumn;
/**
* Entity column descriptions.
*/
export declare type EntityColumnDescriptions = {
[columnName: string]: EntityColumnDescriptionEntry;
};
/**
* A storage of entity columns.
*/
export declare type EntityColumnStorage = {
[columnName: string]: EntityColumn;
};
/**
* Options for an entity compiler.
*/
export interface EntityCompilerOptions {
/**
* Callbacks
*/
readonly callbacks?: CompilerCallbacks;
/**
* The custom working directory.
*/
readonly cwd?: string;
/**
* Special options for Doctrine.
*/
readonly doctrine?: {
/**
*The directory where to store the XML files.
*/
readonly xmlOutDir?: string;
};
/**
* The file with the entity descriptions.
*/
readonly file?: EntityFile;
/**
* The output directory.
*/
readonly outDir?: string;
/**
* The target framework / system.
*/
readonly target: EntityFramework;
}
/**
* Result of a compile operation of an entity compiler.
*/
export interface EntityCompilerResult {
}
/**
* Entity descriptions.
*/
export declare type EntityDescriptions = {
[className: string]: EntityClass;
};
/**
* An entity file.
*/
export interface EntityFile {
/**
* Entity descriptions.
*/
readonly entities?: EntityDescriptions;
/**
* The namespace for the classes to use.
*/
readonly 'namespace'?: string;
}
/**
* List of known frameworks.
*/
export declare enum EntityFramework {
/**
* Doctrine (PHP)
*/
Doctrine = 1,
/**
* Microsoft's Entity Framework
*/
EntityFramework = 2,
/**
* Microsoft's Entity Framework Core
*/
EntityFrameworkCore = 3,
}
/**
* Context for generating a class.
*/
export interface GenerateClassContext {
/**
* Sorted list of column names.
*/
readonly columnNames: string[];
/**
* The columns.
*/
readonly columns: EntityColumnStorage;
/**
* The entity / class description.
*/
readonly entity: EntityClass;
/**
* Method names.
*/
readonly methods: EntityClassMethodNames;
/**
* The class name.
*/
readonly name: string;
/**
* The namespace.
*/
readonly 'namespace': string[];
/**
* Compiler options.
*/
readonly options: EntityCompilerOptions;
/**
* The output directory.
*/
readonly outDir: string;
}
/**
* The default name of an entity file.
*/
export declare const DEFAULT_ENTITY_FILE = "entities.json";
export declare const TYPE__DEFAULT = "";
export declare const TYPE_BIGINT = "bigint";
export declare const TYPE_BIN = "bin";
export declare const TYPE_BINARY = "binary";
export declare const TYPE_BLOB = "blob";
export declare const TYPE_BOOL = "bool";
export declare const TYPE_BOOLEAN = "boolean";
export declare const TYPE_DATE = "date";
export declare const TYPE_DATETIME = "datetime";
export declare const TYPE_DATETIME_TZ = "datetimetz";
export declare const TYPE_FLOAT = "float";
export declare const TYPE_DECIMAL = "decimal";
export declare const TYPE_GUID = "guid";
export declare const TYPE_INT = "int";
export declare const TYPE_INT16 = "int16";
export declare const TYPE_INT32 = "int32";
export declare const TYPE_INTEGER = "integer";
export declare const TYPE_INT64 = "int64";
export declare const TYPE_JSON = "json";
export declare const TYPE_SMALLINT = "smallint";
export declare const TYPE_STR = "str";
export declare const TYPE_STRING = "string";
export declare const TYPE_TEXT = "text";
export declare const TYPE_TIME = "time";
export declare const TYPE_UINT16 = "int16";
export declare const TYPE_UINT32 = "int32";
export declare const TYPE_UINT64 = "int64";
export declare const TYPE_UUID = "uuid";
/**
* An entity compiler.
*/
export declare class EntityCompiler {
readonly options: EntityCompilerOptions;
/**
* Initializes a new instance of that class.
*
* @param {EntityCompilerOptions} [options] Options for compilation operations.
*/
constructor(options?: EntityCompilerOptions);
/**
* Compiles entities.
*
* @return {Promise<EntityCompilerResult>} The promise with the result.
*/
compile(): Promise<EntityCompilerResult>;
/**
* Compiles entities.
*
* @param {string[]} ns The namespace without dots.
* @param {EntityDescriptions} entities The entities.
* @param {CompilerCallbacks} callbacks Callbacks.
* @param {string} outDir The output directory.
*/
protected compileEntities(ns: string[], entities: EntityDescriptions, callbacks: CompilerCallbacks, outDir: string): Promise<void>;
}
/**
* Compiles entities.
*
* @param {EntityCompilerOptions} [opts] Options for the operation.
*
* @return {Promise<EntityCompilerResult>} The promise with the result.
*/
export declare function compile(opts?: EntityCompilerOptions): Promise<EntityCompilerResult>;
/**
* Parses a value for a class or for use in a class.
*
* @param {any} val The input value.
*
* @return {string|false} The parsed name or (false) if invalid.
*/
export declare function parseForClass(val: any): string | false;
/**
* Converts a data type from a entity file to a CLR type.
*
* @param {string} type The entity type.
* @param {Function} canBeNull The function that provides if value can be (null) or not.
* @param {Function} isID The function that provides if value is an ID value or not.
*
* @return {string} The CLR type.
*/
export declare function toClrType(type: string, canBeNull: () => boolean, isID: () => boolean): string;