@mongez/gnz
Version:
Generator Z, the next generation of scaffolding tools.
63 lines (55 loc) • 2.41 kB
JavaScript
'use strict';var converters=require('../../utils/converters.js');require('os');var prettifier=require('../../utils/prettifier.js');async function generateModelContent(options) {
//
const { collection, className, defaultValue = {}, outputClass, outputClassPath, columns = {}, embedded = ["id"], } = options;
const outputImport = outputClass
? `import {${outputClass}} from "${outputClassPath}";`
: "";
const content = `
import { Model, type Casts, type Document, type ModelSync } from "@warlock.js/cascade";
${outputImport}
export class ${className} extends Model {
/**
* Collection name
*/
public static collection = "${collection}";
${outputClass
? `
/**
* Output class
*/
public static output = ${outputClass};
`
: ""}
/**
* List of models to sync with
* To sync with a single embedded document use: [User.sync("city")],
* this will update the city sub-document to all users when city model is updated.
* To sync with multiple embedded documents use: [Post.syncMany("keywords")],
* This will update the keywords sub-document to all posts when keywords model is updated.
*/
public syncWith: ModelSync[] = [];
/**
* Default value for model data
* Works only when creating new records
*/
public defaultValue: Document = ${converters.toJson(defaultValue)};
/**
* Cast data types before saving documents into database
*/
protected casts: Casts = ${converters.toJson(columns)};
/**
* Define what columns should be used when model document is embedded in another document.
* Make sure to set only the needed columns to reduce the document size.
* This is useful for better performance when fetching data from database.
*/
public embedded = ${JSON.stringify(embedded)};
}
`;
return await prettifier.format.typescript(content);
}
async function generateModelIndexContent(options) {
const content = `
export * from "./${options.fileName}";
`;
return await prettifier.format.typescript(content);
}exports.generateModelContent=generateModelContent;exports.generateModelIndexContent=generateModelIndexContent;//# sourceMappingURL=template.js.map