prisma-util
Version:
Prisma Util is an easy to use tool that merges multiple Prisma schema files, allows extending of models, resolves naming conflicts both manually and automatically and provides easy access to Prisma commands and timing reports. It's mostly a plug-and-play
283 lines (282 loc) • 9.56 kB
TypeScript
declare type IntrospectionModel = {
/** The name of this model. If this parameter hasn't been modified before, it will be the table name from the database. */
name: string;
/** Add an attribute to this model.
*
* attribute - The attribute to add. You can use the `schema-creator` module for a list of attributes. */
addAttribute: (attribute: string) => void;
};
/** Current Experimental features available with Prisma Util. */
export declare const OptionalFeaturesArray: readonly ["crossFileRelations", "codeSchemas", "pgtrgm", "ignoreEmptyConditions", "customAttributeFunctions", "environmentLock", "virtualEnvironment", "middlewareContext", "deprecatedTag", "staticTake", "prismaGenerators", "refinedTypes", "enhancedIntrospection"];
export declare type OptionalFeatures = typeof OptionalFeaturesArray[number];
/** Config type for prisma-util.config.mjs.*/
export declare type ConfigType = {
/**Which .prisma files should be included. */
includeFiles: (string | ((model?: string, name?: string) => string))[];
/**Allows exclusion of specific models. */
excludeModels?: string[];
/**Base schema that provides the datasource and generator. */
baseSchema: (string | ((model?: string, name?: string) => string));
/**Whether Prisma Util should try to resolve cross-file relation conflicts. */
crossFileRelations?: boolean;
/**Optional features */
optionalFeatures?: OptionalFeatures[];
/** Allows extension of models (inheritance).
* Example:
*
* "base.prisma:Topic": "schema.prisma:Post"
*
* This example will add all of the non-ID non-relation columns from Post to Topic.
*/
extended?: {
[fileModel: string]: string;
};
/** Relation map for resolving cross-file conflicts.
* Example:
*
* "base.prisma:User": "schema.prisma:BaseUser"
*
* This example will change the type on the column from User to BaseUser. If there are name conflicts, the left one will always be replaced with the right one.
*/
relations?: {
[fileModel: string]: string;
};
/**Whether code-generated schemas should be enabled or not. */
codeSchemas?: boolean;
/**Schema generators that use the @prisma-util/schema-creator package. */
codeGenerators?: Promise<string>[];
/**pg_trgm support */
pgtrgm?: boolean;
/**Full-text search support.*/
ftsIndexes?: {
[fileModel: string]: {
type: "Gin" | "Gist";
indexes: {
language: string;
field: string;
weight: string;
}[];
};
};
/** Postgres schema. */
schema?: string;
/** Middleware generation path. */
middleware?: string;
/** Remove OR statements if the array is empty. */
ignoreEmptyConditions?: boolean;
/** Default function mappings. */
defaultFunctions?: {
[fileModelColumn: string]: (dmmf: any) => any;
};
/**Create custom attribute functions for @default. */
customAttributeFunctions?: boolean;
/**Disallow dev commands inside of production. */
environmentLock?: boolean;
/** Allow better environment handling. */
virtualEnvironment?: boolean;
/** Environment setup function. */
environment?: () => Promise<void>;
/** Add support for Middleware Context API */
middlewareContext?: boolean;
/** Project toolchain configuration. */
toolchain: {
useExtensions: boolean;
resolve: {
types: string;
};
};
/** Deprecated fields feature. */
deprecatedTag?: boolean;
/** Messages for deprecated fields. */
deprecated?: {
[fileModelColumn: string]: string;
};
/** Configure take for all queries.*/
staticTake?: boolean;
/** The staticTake configuration. */
take?: {
[model: string]: number;
} | {
$global: number;
[model: string]: number;
};
/** Prisma Generators support. */
prismaGenerators?: boolean;
/** Configured Prisma Generators. */
generators?: {
include: (string | ((generator?: string) => string))[];
run: {
[key: string]: string | (() => Promise<boolean>) | boolean;
};
};
/** Allow refining of types. */
refinedTypes?: boolean;
/** Refined type mappings. */
fieldTypes?: {
[fileModelColumn: string]: string;
};
/** Allow enhanced introspection. */
enhancedIntrospection?: boolean;
/** Matcher block. */
introspection?: {
modelPatterns: {
$static?: {
[table: string]: string | ((model: IntrospectionModel) => Promise<IntrospectionModel>);
};
$regex?: {
[tableRegex: string]: ((model: IntrospectionModel, match: string, ...groups: string[]) => Promise<IntrospectionModel>);
};
};
};
};
/** Column type for schema models. */
export declare type Column = {
name: string;
type: string;
constraints: string[];
};
/** Enum type for schemas. */
export declare type Enum = {
name: string;
values: string[];
};
/**Action type for resolving conflicts. */
export declare type Action = {
type: "skip";
item: "enum" | "model";
} | {
type: "rename";
newName: string;
item: "enum" | "model";
} | {
type: "rename-rel";
newName: string;
item: "enum" | "model";
} | {
type: "remap";
from: string;
to: string;
item: "enum" | "model";
};
/** Small parser utility to resolve conflicts. */
export default class PrismaParser {
/**Get all the columns defined for the models loaded. */
getModelColumns(): any;
/** All of the models across all .prisma files. */
models: {
[file: string]: {
[name: string]: string;
};
};
/** All of the files used by Prisma Util. */
files: string[];
/** Columns for models mapped by file-model association. */
modelColumns: {
[fileModel: string]: Column[];
};
/** The configuration file loaded by the util.*/
config: ConfigType;
/** Configuration file path for later use. */
configPath: string;
/**Generator part to be added. */
generator?: string;
/**Datasource part to be added. */
datasource?: string;
/**Map of generators that should be added and their environment requirements */
generators: {
[generatorName: string]: {
code: string;
run: boolean;
meta: {
file: string;
name: string;
};
};
};
/**New schema after conflicts are fixed. */
solutions: {
[key: string]: Action;
}[];
/**Remapper from config. */
remapper: {
[fileModel: string]: string;
};
/**Enums */
enums: {
[fileModel: string]: Enum;
};
constructor(config: ConfigType, configPath: string);
loadEnvironment(): Promise<void>;
private getEnv;
/** Load .prisma files from config and parse models.*/
load(): Promise<this>;
resetMigrations(): Promise<void>;
prepareDocumentation(): Promise<void>;
loaded: boolean;
/** Get a list of raw models.*/
getModels(): {
[file: string]: {
[name: string]: string;
};
};
/** Prisma being prisma, creates DROP DEFAULT migrations. */
fixMigrate(): Promise<boolean>;
/**Hooks to run after client generation. */
generate(): Promise<void>;
/**
* Hooks to run during generation.
*/
toolchain(): Promise<void>;
/**
* Internal generation function.
*/
private generateInternal;
/** Change migration to accomodate full-text search indexes. */
migrate(command: string): Promise<boolean | undefined>;
/** Returns all name conflicts.*/
getConflicts(): Promise<{
1: {
name: string;
type: "model" | "enum";
};
2: {
name: string;
type: "model" | "enum";
};
}[]>;
/** Get models that will be removed by the mapper. */
getShadowedModels(): string[];
/** Get models that will only server as a polyfill. */
getExtendedModels(): string[];
/** Get the model names that will be enriched by the mapper. */
getReplacementModels(): string[];
private appliedIntrospection;
/** Apply solutions to conflicts. */
applyPatches(): Promise<[string, {
[name: string]: string;
}][]>;
/** Remap column using updated names. */
remap(old: string, newMap: string): void;
/** Get relations for model */
getRelations(fileModel: string): Column[];
/** Get all relations referring to model name. */
getReferredRelations(fileModel: string): {
model: string;
column: Column;
}[];
/**Check for cross-file relation conflict fix with mapper. */
canFixCrossFileWithMapper(fileModel: string): string | false;
/** Suggest solution for resolving conflict. */
suggest(item: string, action: Action): void;
/** Write schema and delete on end. */
writeSchema(path?: string): Promise<void>;
/** Generate schema from details. */
generateSchema(): string;
/** Get the extended model names that will be enriched. */
getModelsExtended(): string[];
/** Create array columns for shadowed models. */
createColumnsForShadowedModel(fileModel: string): Column[];
/** Create array columns from parent. */
createColumnsForExtendedModel(fileModel: string): Column[];
}
export {};