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
59 lines (58 loc) • 1.94 kB
JavaScript
import AbstractCreator from './creator.js';
/** Hidden column class for internal use. */
class Column {
constructor(name, type, ...constraints) {
this.name = name;
this.type = type;
this.constraints = constraints;
}
}
/** Model that will be created in your Prisma schema.
*
* When resolving conflicts, this model will be displayed as `codeSchemas:[ModelName]` so you can differentiate between .schema files and code generated models.
*
* For additional functionality, you can use the same format (`codeSchemas:[ModelName].[columnName]`) to remap columns using the Automatic Remapper.
*/
export default class Model extends AbstractCreator {
constructor(creator, name) {
super();
this.creator = creator;
this._name = name;
this.attributes = [];
this.columns = [];
}
/** Change this model's name. */
name(name) {
this._name = name;
return this;
}
/** Create a new column. */
column(name, type, ...constraints) {
this.columns.push(new Column(name, type, ...constraints));
return this;
}
/**Add constraints to this model. */
constraints(...constraints) {
this.attributes.push(...constraints);
return this;
}
model(name) {
return this.creator.pushModel(this).model(name);
}
enum(name) {
return this.creator.pushModel(this).enum(name);
}
/** You should not call this method yourself. */
beforeBuild() {
if (this.attributes.length == 0)
return this;
while (this.columns.filter(column => column.type == "").length > 0)
this.columns.splice(this.columns.indexOf(this.columns.filter(column => column.type == "")[0]), 1);
for (const attr of this.attributes)
this.column(attr, "");
return this;
}
build() {
return this.creator.pushModel(this).build();
}
}