fix: auto-import models

This commit is contained in:
arashsheyda
2023-04-24 22:15:44 +03:00
parent 01f2c149fa
commit d152597155
2 changed files with 7 additions and 11 deletions

View File

@ -13,10 +13,10 @@ export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any {
const dbName = capitalize(singular) const dbName = capitalize(singular)
if (collection.fields) { if (collection.fields) {
if (!fs.existsSync(resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`))) { if (!fs.existsSync(resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`))) {
fs.ensureDirSync(resolve(nuxt.options.serverDir, 'models')) fs.ensureDirSync(resolve(nuxt.options.serverDir, 'utils/models'))
fs.writeFileSync( fs.writeFileSync(
resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`), resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`),
generateSchemaFile(dbName, collection.fields), generateSchemaFile(dbName, collection.fields),
) )
} }
@ -57,14 +57,11 @@ export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any {
// create rows and columns // create rows and columns
}, },
async resourceSchema(collection: string) { async resourceSchema(collection: string) {
// get schema file if exists // TODO: use magicast
const singular = singularize(collection).toLowerCase() const singular = singularize(collection).toLowerCase()
const schemaPath = resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`)
if (fs.existsSync(resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`))) { if (fs.existsSync(schemaPath)) {
const schemaPath = resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`)
const content = fs.readFileSync(schemaPath, 'utf-8').match(/schema: \{(.|\n)*\}/g) const content = fs.readFileSync(schemaPath, 'utf-8').match(/schema: \{(.|\n)*\}/g)
if (content) { if (content) {
const schemaString = content[0].replace('schema: ', '').slice(0, -3) const schemaString = content[0].replace('schema: ', '').slice(0, -3)
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval

View File

@ -26,7 +26,6 @@ export const ${name}Schema = defineMongooseModel({
export function generateApiRoute(action: string, { model, by }: { model: { name: string; path: string }; by?: string }) { export function generateApiRoute(action: string, { model, by }: { model: { name: string; path: string }; by?: string }) {
const modelName = capitalize(model.name) const modelName = capitalize(model.name)
const schemaImport = `import { ${modelName}Schema } from '../../models/${model.path}'\n\n`
const operation = { const operation = {
index: `return await ${modelName}Schema.find({})`, index: `return await ${modelName}Schema.find({})`,
create: `return await new ${modelName}Schema(body).save()`, create: `return await new ${modelName}Schema(body).save()`,
@ -42,7 +41,7 @@ export function generateApiRoute(action: string, { model, by }: { model: { name:
return error return error
}` }`
return `${schemaImport}export default defineEventHandler(async (event) => { return `export default defineEventHandler(async (event) => {
${(action === 'create' || action === 'put') ? `const body = await readBody(event)\n ${main}` : main} ${(action === 'create' || action === 'put') ? `const body = await readBody(event)\n ${main}` : main}
}) })
` `