feat: experimental generate resource

This commit is contained in:
arashsheyda
2023-04-23 00:25:11 +03:00
parent a95cb78c6a
commit 5de7715356
17 changed files with 617 additions and 57 deletions

24
build.config.ts Normal file
View File

@ -0,0 +1,24 @@
import { defineBuildConfig } from 'unbuild'
export default defineBuildConfig({
entries: [
'src/module',
// Chunking
'src/types',
],
externals: [
'nuxt',
'nuxt/schema',
'vite',
'@nuxt/kit',
'@nuxt/schema',
// Type only
'vue',
'vue-router',
'unstorage',
'nitropack',
],
rollup: {
inlineDependencies: true,
},
})

View File

@ -0,0 +1,261 @@
<script lang="ts" setup>
interface ColumnInterface {
name: string
type: string
required: boolean
unique: boolean
default: any
}
const emit = defineEmits(['refresh'])
const router = useRouter()
const schema = ref(true)
const bread = reactive({
browse: {
active: true,
type: 'index',
},
read: {
active: true,
type: 'show',
by: '_id',
},
edit: {
active: true,
type: 'put',
by: '_id',
},
add: {
active: true,
type: 'create',
},
delete: {
active: true,
type: 'delete',
by: '_id',
},
})
const hasBread = computed({
get() {
return bread.browse.active || bread.read.active || bread.edit.active || bread.add.active || bread.delete.active
},
set(value: boolean) {
bread.browse.active = value
bread.read.active = value
bread.edit.active = value
bread.add.active = value
bread.delete.active = value
},
})
const collection = ref('')
const fields = ref<ColumnInterface[]>([
{
name: 'name',
type: 'string',
required: true,
unique: false,
default: '',
},
{
name: 'slug',
type: 'string',
required: true,
unique: true,
default: '',
},
])
const allFields = computed(() => {
return [{ name: '_id', type: 'ObjectId', required: true, unique: true, default: '' }, ...fields.value]
})
const fieldsLabels = ['name', 'type', 'required', 'unique', 'default']
const mongoTypes = [
'string',
'number',
'boolean',
'date',
'object',
'array',
'ObjectId',
]
function addField(index: number) {
fields.value.splice(index + 1, 0, {
name: '',
type: 'string',
required: false,
unique: false,
default: '',
})
}
function removeField(index: number) {
fields.value.splice(index, 1)
}
const convertedBread = computed(() => {
const breads: any = []
// add active breads
// eslint-disable-next-line unused-imports/no-unused-vars
for (const [key, value] of Object.entries(bread) as any) {
if (value.active) {
breads.push({
type: value.type,
by: value?.by,
})
}
}
return breads
})
const formattedFields = computed(() => {
return fields.value.map((field) => {
for (const [key, value] of Object.entries(field)) {
if (!value) {
// @ts-expect-error - no need for type checking
delete field[key]
}
}
return field
})
})
async function generate() {
await rpc.generateResource(
{
name: collection.value,
fields: schema.value ? formattedFields.value : undefined,
},
convertedBread.value,
).then(() => {
emit('refresh')
router.push(`/?table=${collection.value}`)
})
}
// TODO: remove this
const toggleSchema = computed({
get() {
if (hasBread.value)
return schema.value = true
return schema.value
},
set(value: boolean) {
schema.value = value
if (!schema.value)
return hasBread.value = false
hasBread.value = true
},
})
</script>
<template>
<div relative h-full of-hidden>
<div sticky top-0 px8 py4 n-bg-base>
<div mb2 flex items-center>
<span text-2xl font-bold mt1>
<NCheckbox v-model="hasBread" n="green">
BREAD
|
</NCheckbox>
</span>
<span flex gap4 mt2 ml2>
<NCheckbox v-model="bread.browse.active" n="blue">
Browse
</NCheckbox>
<div flex gap2>
<NCheckbox v-model="bread.read.active" n="cyan">
Read
</NCheckbox>
<NSelect v-if="bread.read.active" v-model="bread.read.by">
<option v-for="field in allFields" :key="field.name" :value="field.name">
{{ field.name }}
</option>
</NSelect>
</div>
<div flex gap2>
<NCheckbox v-model="bread.edit.active" n="purple">
Edit
</NCheckbox>
<NSelect v-if="bread.edit.active" v-model="bread.edit.by">
<option v-for="field in allFields" :key="field.name" :value="field.name">
{{ field.name }}
</option>
</NSelect>
</div>
<NCheckbox v-model="bread.add.active" n="green">
Add
</NCheckbox>
<div flex gap2>
<NCheckbox v-model="bread.delete.active" n="red">
Delete
</NCheckbox>
<NSelect v-if="bread.delete.active" v-model="bread.delete.by">
<option v-for="field in allFields" :key="field.name" :value="field.name">
{{ field.name }}
</option>
</NSelect>
</div>
</span>
</div>
<div flex gap4>
<NTextInput v-model="collection" flex-auto placeholder="Collection name" />
<NCheckbox v-model="toggleSchema" n="green">
Generate Schema
</NCheckbox>
</div>
</div>
<div v-if="schema" px8 h-full of-auto>
<div grid="~ cols-1" gap-2 mt4 mb4>
<div grid="~ cols-6" text-center>
<div v-for="label in fieldsLabels" :key="label">
{{ label }}
</div>
<div>
Actions
</div>
</div>
<div v-for="(column, index) in fields" :key="index" grid="~ cols-6" items-center text-center gap4>
<div>
<NTextInput v-model="column.name" />
</div>
<div>
<NSelect v-model="column.type">
<option v-for="mongoType of mongoTypes" :key="mongoType" :value="mongoType">
{{ mongoType }}
</option>
</NSelect>
</div>
<div>
<NCheckbox v-model="column.required" n="green" />
</div>
<div>
<NCheckbox v-model="column.unique" n="cyan" />
</div>
<div>
<NTextInput v-if="column.type === 'string'" v-model="column.default" type="string" n="orange" />
<NTextInput v-else-if="column.type === 'number'" v-model="column.default" type="number" n="orange" />
<NCheckbox v-else-if="column.type === 'boolean'" v-model="column.default" n="orange" />
<NTextInput v-else-if="column.type === 'date'" v-model="column.default" type="date" n="orange" />
<NTextInput v-else-if="column.type === 'ObjectId'" placeholder="no-default" disabled n="orange" />
<NTextInput v-else v-model="column.default" n="orange" />
</div>
<div flex justify-center gap2>
<NIconButton icon="carbon-add" n="cyan" @click="addField(index)" />
<NIconButton icon="carbon-delete" n="red" @click="removeField(index)" />
</div>
</div>
</div>
</div>
<NButton absolute right-8 bottom-8 px8 py2 icon="carbon-magic-wand-filled" n="green" @click="generate">
Create
</NButton>
</div>
</template>
<style></style>

View File

@ -1,3 +1,4 @@
<!-- eslint-disable no-console -->
<script lang="ts" setup> <script lang="ts" setup>
const props = defineProps({ const props = defineProps({
collection: { collection: {
@ -10,9 +11,15 @@ const documents = computedAsync(async () => {
return await rpc.listDocuments(props.collection) return await rpc.listDocuments(props.collection)
}) })
const schema = computedAsync<any>(async () => {
return await rpc.resourceSchema(props.collection)
})
const fields = computed(() => { const fields = computed(() => {
if (documents.value && documents.value.length > 0) if (documents.value && documents.value.length > 0)
return Object.keys(documents.value[0]) return Object.keys(documents.value[0])
if (schema.value)
return Object.keys(schema.value)
return [] return []
}) })
@ -34,14 +41,21 @@ const filtered = computed(() => {
}) })
function addDocument() { function addDocument() {
// TODO: validate & show errors
if (editing.value) if (editing.value)
return return
editing.value = true editing.value = true
selectedDocument.value = {} selectedDocument.value = {}
for (const field of fields.value) { if (schema.value) {
if (field !== '_id') for (const field of Object.keys(schema.value))
selectedDocument.value[field] = '' selectedDocument.value[field] = ''
} }
else {
for (const field of fields.value) {
if (field !== '_id')
selectedDocument.value[field] = ''
}
}
const parent = dbContainer.value?.parentElement const parent = dbContainer.value?.parentElement
parent?.scrollTo(0, parent.scrollHeight) parent?.scrollTo(0, parent.scrollHeight)
@ -78,10 +92,22 @@ async function deleteDocument(document: any) {
rpc.deleteDocument(props.collection, document._id) rpc.deleteDocument(props.collection, document._id)
documents.value = await rpc.listDocuments(props.collection) documents.value = await rpc.listDocuments(props.collection)
} }
const fieldRefs = ref<any>([])
function handleClickOutside(event: any) {
if (editing.value && selectedDocument.value) {
const isClickOutside = fieldRefs.value.every((ref: any) => !ref.contains(event.target))
if (isClickOutside) {
editing.value = false
selectedDocument.value = null
}
}
}
// useEventListener('click', handleClickOutside)
</script> </script>
<template> <template>
<div ref="dbContainer"> <div ref="dbContainer" :class="{ 'h-full': !documents?.length }">
<Navbar v-model:search="search" sticky top-0 px4 py2 backdrop-blur z-10> <Navbar v-model:search="search" sticky top-0 px4 py2 backdrop-blur z-10>
<template #actions> <template #actions>
<NButton icon="carbon:add" n="green" @click="addDocument"> <NButton icon="carbon:add" n="green" @click="addDocument">
@ -93,7 +119,7 @@ async function deleteDocument(document: any) {
<span>{{ documents?.length }} documents in total</span> <span>{{ documents?.length }} documents in total</span>
</div> </div>
</Navbar> </Navbar>
<table v-if="documents?.length" w-full mb10 :class="{ 'editing-mode': editing }"> <table v-if="documents?.length || selectedDocument" w-full mb10 :class="{ 'editing-mode': editing }">
<thead> <thead>
<tr> <tr>
<th v-for="field of fields" :key="field" text-start> <th v-for="field of fields" :key="field" text-start>
@ -105,8 +131,9 @@ async function deleteDocument(document: any) {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<!-- hover-bg-green hover-bg-opacity-5 hover-text-green cursor-pointer -->
<tr v-for="document in filtered" :key="document._id" :class="{ isEditing: editing && selectedDocument._id === document._id }"> <tr v-for="document in filtered" :key="document._id" :class="{ isEditing: editing && selectedDocument._id === document._id }">
<td v-for="field of fields" :key="field" hover-bg-green hover-bg-opacity-5 hover-text-green cursor-pointer @dblclick="editDocument(document)"> <td v-for="field of fields" :key="field" ref="fieldRefs" @dblclick="editDocument(document)">
<template v-if="editing && selectedDocument._id === document._id"> <template v-if="editing && selectedDocument._id === document._id">
<input v-model="selectedDocument[field]" :disabled="field === '_id'"> <input v-model="selectedDocument[field]" :disabled="field === '_id'">
</template> </template>

View File

@ -0,0 +1,52 @@
<script setup lang="ts">
const props = defineProps<{
modelValue?: boolean
navbar?: HTMLElement
autoClose?: boolean
}>()
const emit = defineEmits<{
(e: 'close'): void
}>()
const el = ref<HTMLElement>()
const { height: top } = useElementSize(() => props.navbar, undefined, { box: 'border-box' })
onClickOutside(el, () => {
if (props.modelValue && props.autoClose)
emit('close')
}, {})
</script>
<script lang="ts">
export default {
inheritAttrs: false,
}
</script>
<template>
<Transition
enter-active-class="duration-200 ease-in"
enter-from-class="transform translate-x-1/1"
enter-to-class="opacity-100"
leave-active-class="duration-200 ease-out"
leave-from-class="opacity-100"
leave-to-class="transform translate-x-1/1"
>
<div
v-if="modelValue"
ref="el"
border="l base"
flex="~ col gap-1"
fixed bottom-0 right-0 z-10 z-20 of-auto text-sm backdrop-blur-lg
:style="{ top: `${top}px` }"
v-bind="$attrs"
>
<NIconButton absolute right-2 top-2 z-20 text-xl icon="carbon-close" @click="$emit('close')" />
<div relative h-full w-full of-auto>
<slot />
</div>
</div>
</Transition>
</template>

View File

@ -1,5 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
const route = useRoute() const route = useRoute()
const router = useRouter()
const selectedCollection = ref() const selectedCollection = ref()
// TODO: check connection // TODO: check connection
@ -21,13 +22,26 @@ onMounted(() => {
if (route.query.table) if (route.query.table)
selectedCollection.value = route.query.table selectedCollection.value = route.query.table
}) })
async function dropCollection(table: any) {
await rpc.dropCollection(table.name)
collections.value = await rpc.listCollections()
if (selectedCollection.value === table.name) {
selectedCollection.value = undefined
router.push({ name: 'index' })
}
}
async function refresh() {
collections.value = await rpc.listCollections()
}
</script> </script>
<template> <template>
<PanelLeftRight :min-left="13" :max-left="20"> <PanelLeftRight :min-left="13" :max-left="20">
<template #left> <template #left>
<div py1 px4> <div px4>
<Navbar v-model:search="search" :placeholder="`${collections?.length ?? '-'} collection in total`" mt1> <Navbar v-model:search="search" :placeholder="`${collections?.length ?? '-'} collection in total`" mt2>
<div flex items-center gap2> <div flex items-center gap2>
<NIconButton w-full mb1.5 icon="carbon-reset" title="Refresh" /> <NIconButton w-full mb1.5 icon="carbon-reset" title="Refresh" />
<NIconButton w-full mb1.5 icon="carbon-data-base" title="Connection Name" :class="connected ? 'text-green-5' : 'text-orange-5'" /> <NIconButton w-full mb1.5 icon="carbon-data-base" title="Connection Name" :class="connected ? 'text-green-5' : 'text-orange-5'" />
@ -40,14 +54,27 @@ onMounted(() => {
<NIcon icon="carbon-db2-database" /> <NIcon icon="carbon-db2-database" />
{{ table.name }} {{ table.name }}
</span> </span>
<!-- TODO: --> <div flex gap2>
<NIconButton icon="carbon-overflow-menu-horizontal" /> <NIconButton block n="red" icon="carbon-delete" @click="dropCollection(table)" />
<!-- <NIconButton icon="carbon-overflow-menu-horizontal" /> -->
</div>
</NuxtLink> </NuxtLink>
</div> </div>
</div> </div>
</template> </template>
<template #right> <template #right>
<DatabaseDetail v-if="selectedCollection" :collection="selectedCollection" /> <DatabaseDetail v-if="selectedCollection" :collection="selectedCollection" />
<div v-else class="n-panel-grids-center">
<div class="n-card n-card-base" px6 py2>
<span op75 flex items-center>
<NIcon icon="carbon:db2-buffer-pool" mr2 />
Select a collection to start
</span>
</div>
</div>
</template> </template>
</PanelLeftRight> </PanelLeftRight>
<DrawerRight v-model="drawer" style="width: calc(80.5%);" auto-close @close="drawer = false">
<CreateResource @refresh="refresh" />
</DrawerRight>
</template> </template>

15
module.cjs Normal file
View File

@ -0,0 +1,15 @@
/* eslint-disable eslint-comments/no-unlimited-disable */
/* eslint-disable */
module.exports = function (...args) {
const nuxt = this.nuxt || args[1]
let _a
let version = (nuxt == null ? void 0 : nuxt._version) || (nuxt == null ? void 0 : nuxt.version) || ((_a = nuxt == null ? void 0 : nuxt.constructor) == null ? void 0 : _a.version) || ''
version = version.replace(/^v/g, '')
// Nuxt DevTools is not compatible with Nuxt 2, disabled
if (version.startsWith('2.')) {
return
}
return import('./dist/module.mjs').then(m => m.default.call(this, ...args))
}
const _meta = module.exports.meta = require('./dist/module.json')
module.exports.getMeta = () => Promise.resolve(_meta)

View File

@ -11,24 +11,28 @@
"exports": { "exports": {
".": { ".": {
"types": "./dist/types.d.ts", "types": "./dist/types.d.ts",
"require": "./dist/module.cjs", "require": "./module.cjs",
"import": "./dist/module.mjs" "import": "./dist/module.mjs"
} },
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.mjs"
},
"./*": "./*"
}, },
"main": "./dist/module.cjs", "main": "./module.cjs",
"types": "./dist/types.d.ts", "types": "./dist/types.d.ts",
"files": [ "files": [
"dist" "dist"
], ],
"scripts": { "scripts": {
"prepack": "nuxt-module-build && npm run build:client", "build": "pnpm dev:prepare && pnpm build:module && pnpm build:client",
"build:client": "nuxi generate client", "build:client": "nuxi generate client",
"dev:client": "nuxi dev client --port 3300", "build:module": "nuxt-build-module",
"dev": "npm run play:dev", "dev": "nuxi dev playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare client", "dev:prepare": "nuxi prepare client",
"play:dev": "nuxi dev playground", "dev:prod": "npm run build && pnpm dev",
"play:prod": "npm run prepack && nuxi dev playground", "release": "npm run lint && npm run test && npm run build && changelogen --release && npm publish && git push --follow-tags",
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
"lint": "eslint .", "lint": "eslint .",
"test": "vitest run", "test": "vitest run",
"test:watch": "vitest watch" "test:watch": "vitest watch"

View File

@ -1,5 +0,0 @@
import { User } from '~/server/models/user.schema'
export default defineEventHandler(() => {
return User.find()
})

View File

@ -1,18 +0,0 @@
import { defineMongooseModel } from '#nuxt/mongoose'
export const User = defineMongooseModel({
name: 'User',
schema: {
name: {
type: String,
required: true,
},
},
})
// export const User = defineMongooseModel('User', {
// name: {
// type: String,
// required: true,
// },
// })

View File

@ -1,4 +1,4 @@
import { addServerPlugin, addTemplate, createResolver, defineNuxtModule, logger } from '@nuxt/kit' import { addImportsDir, addServerPlugin, addTemplate, createResolver, defineNuxtModule, logger } from '@nuxt/kit'
import { pathExists } from 'fs-extra' import { pathExists } from 'fs-extra'
import { tinyws } from 'tinyws' import { tinyws } from 'tinyws'
import { defu } from 'defu' import { defu } from 'defu'
@ -22,6 +22,8 @@ export default defineNuxtModule<ModuleOptions>({
setup(options, nuxt) { setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url) const { resolve } = createResolver(import.meta.url)
addImportsDir(resolve('./runtime/composables'))
if (!options.uri) if (!options.uri)
console.warn('Missing `MONGODB_URI` in `.env`') console.warn('Missing `MONGODB_URI` in `.env`')

View File

@ -0,0 +1,8 @@
import type { mongo } from 'mongoose'
import { connection } from 'mongoose'
export function useMongoose(): { db: mongo.Db } {
return {
db: connection?.db,
}
}

View File

@ -1,39 +1,38 @@
import { logger } from '@nuxt/kit' import { logger } from '@nuxt/kit'
import { ObjectId } from 'mongodb' import mongoose from 'mongoose'
import { connection as MongooseConnection, connect } from 'mongoose'
import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types' import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types'
export function setupDatabaseRPC({ nuxt }: NuxtDevtoolsServerContext): any { export function setupDatabaseRPC({ nuxt }: NuxtDevtoolsServerContext): any {
// TODO: // TODO:
connect('mongodb://127.0.0.1:27017/arcane') mongoose.connect('mongodb://127.0.0.1:27017/arcane')
return { return {
async createCollection(name: string) { async createCollection(name: string) {
return await MongooseConnection.db.createCollection(name) return await mongoose.connection.db.createCollection(name)
}, },
async listCollections() { async listCollections() {
return await MongooseConnection.db.listCollections().toArray() return await mongoose.connection.db.listCollections().toArray()
}, },
async getCollection(name: string) { async getCollection(name: string) {
return MongooseConnection.db.collection(name) return mongoose.connection.db.collection(name)
}, },
async dropCollection(name: string) { async dropCollection(name: string) {
return await MongooseConnection.db.collection(name).drop() return await mongoose.connection.db.collection(name).drop()
}, },
async createDocument(collection: string, data: any) { async createDocument(collection: string, data: any) {
return await MongooseConnection.db.collection(collection).insertOne(data) return await mongoose.connection.db.collection(collection).insertOne(data)
}, },
async listDocuments(collection: string) { async listDocuments(collection: string) {
return await MongooseConnection.db.collection(collection).find().toArray() return await mongoose.connection.db.collection(collection).find().toArray()
}, },
async getDocument(collection: string, document: {}) { async getDocument(collection: string, document: {}) {
return await MongooseConnection.db.collection(collection).findOne({ document }) return await mongoose.connection.db.collection(collection).findOne({ document })
}, },
async updateDocument(collection: string, data: any) { async updateDocument(collection: string, data: any) {
const { _id, ...rest } = data const { _id, ...rest } = data
try { try {
return await MongooseConnection.db.collection(collection).findOneAndUpdate({ _id: new ObjectId(_id) }, { $set: rest }) return await mongoose.connection.db.collection(collection).findOneAndUpdate({ _id: new mongoose.Types.ObjectId(_id) }, { $set: rest })
} }
catch (error) { catch (error) {
logger.log(error) logger.log(error)
@ -41,7 +40,7 @@ export function setupDatabaseRPC({ nuxt }: NuxtDevtoolsServerContext): any {
} }
}, },
async deleteDocument(collection: string, id: string) { async deleteDocument(collection: string, id: string) {
return await MongooseConnection.db.collection(collection).deleteOne({ _id: new ObjectId(id) }) return await mongoose.connection.db.collection(collection).deleteOne({ _id: new mongoose.Types.ObjectId(id) })
}, },
} satisfies Partial<ServerFunctions> } satisfies Partial<ServerFunctions>
} }

View File

@ -8,6 +8,7 @@ import { parse, stringify } from 'flatted'
import type { Nuxt } from 'nuxt/schema' import type { Nuxt } from 'nuxt/schema'
import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types' import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
import { setupDatabaseRPC } from './database' import { setupDatabaseRPC } from './database'
import { setupResourceRPC } from './resource'
export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any { export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
const serverFunctions = {} as ServerFunctions const serverFunctions = {} as ServerFunctions
@ -63,6 +64,7 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
Object.assign(serverFunctions, { Object.assign(serverFunctions, {
...setupDatabaseRPC(ctx), ...setupDatabaseRPC(ctx),
...setupResourceRPC(ctx),
} satisfies Partial<ServerFunctions>) } satisfies Partial<ServerFunctions>)
const wsClients = new Set<WebSocket>() const wsClients = new Set<WebSocket>()

View File

@ -0,0 +1,77 @@
import fs from 'fs-extra'
import { resolve } from 'pathe'
import mongoose from 'mongoose'
import type { Collection, NuxtDevtoolsServerContext, Resource, ServerFunctions } from '../types'
import { generateApiRoute, generateSchemaFile } from '../utils/schematics'
import { capitalize, pluralize, singularize } from '../utils/formatting'
export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any {
return {
async generateResource(collection: Collection, resources: Resource[]) {
const singular = singularize(collection.name).toLowerCase()
const plural = pluralize(collection.name).toLowerCase()
const dbName = capitalize(singular)
if (collection.fields) {
if (!fs.existsSync(resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`))) {
fs.ensureDirSync(resolve(nuxt.options.serverDir, 'models'))
fs.writeFileSync(
resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`),
generateSchemaFile(dbName, collection.fields),
)
}
const model = { name: dbName, path: `${singular}.schema` }
fs.ensureDirSync(resolve(nuxt.options.serverDir, `api/${plural}`))
// create resources
// TODO: fix this
resources.forEach((route: any) => {
let fileName = ''
if (route.type === 'index')
fileName = 'index.get.ts'
if (route.type === 'create')
fileName = 'create.post.ts'
if (route.type === 'show')
fileName = `[_${route.by}].get.ts`.replace('_', '')
if (route.type === 'put')
fileName = `[_${route.by}].put.ts`.replace('_', '')
if (route.type === 'delete')
fileName = `[_${route.by}].delete.ts`.replace('_', '')
if (!fs.existsSync(resolve(nuxt.options.serverDir, `api/${plural}`, fileName))) {
const content = generateApiRoute(route.type, { model, by: route.by })
fs.writeFileSync(resolve(nuxt.options.serverDir, 'api', plural, fileName), content)
}
})
}
// create collection if not exists
if (!mongoose.connection.modelNames().includes(dbName))
await mongoose.connection.db.createCollection(plural)
// create rows and columns
},
async resourceSchema(collection: string) {
// get schema file if exists
const singular = singularize(collection).toLowerCase()
if (fs.existsSync(resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`))) {
const schemaPath = resolve(nuxt.options.serverDir, 'models', `${singular}.schema.ts`)
const content = fs.readFileSync(schemaPath, 'utf-8').match(/schema: \{(.|\n)*\}/g)
if (content) {
const schemaString = content[0].replace('schema: ', '').slice(0, -3)
// eslint-disable-next-line no-eval
const schema = eval(`(${schemaString})`)
return schema
}
}
},
} satisfies Partial<ServerFunctions>
}

View File

@ -1,18 +1,32 @@
export interface ServerFunctions { export interface ServerFunctions {
// collections // Database - collections
createCollection(name: string): Promise<any> createCollection(name: string): Promise<any>
listCollections(): Promise<any> listCollections(): Promise<any>
getCollection(name: string): Promise<any> getCollection(name: string): Promise<any>
dropCollection(name: string): Promise<any> dropCollection(name: string): Promise<any>
// documents // Database - documents
createDocument(collection: string, data: any): Promise<any> createDocument(collection: string, data: any): Promise<any>
listDocuments(collection: string): Promise<any> listDocuments(collection: string): Promise<any>
getDocument(collection: string, id: string): Promise<any> getDocument(collection: string, id: string): Promise<any>
updateDocument(collection: string, data: any): Promise<any> updateDocument(collection: string, data: any): Promise<any>
deleteDocument(collection: string, id: string): Promise<any> deleteDocument(collection: string, id: string): Promise<any>
// Resource - api-routes & models
generateResource(collection: Collection, resources: Resource[]): Promise<any>
resourceSchema(collection: string): Promise<any>
} }
export interface ClientFunctions { export interface ClientFunctions {
refresh(type: string): void refresh(type: string): void
} }
export interface Collection {
name: string
fields?: {}[]
}
export interface Resource {
type: string
by?: string
}

22
src/utils/formatting.ts Normal file
View File

@ -0,0 +1,22 @@
import plrz from 'pluralize'
export function normalizeToKebabOrSnakeCase(str: string) {
const STRING_DASHERIZE_REGEXP = /\s/g
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g
return str
.replace(STRING_DECAMELIZE_REGEXP, '$1-$2')
.toLowerCase()
.replace(STRING_DASHERIZE_REGEXP, '-')
}
export function pluralize(str: string) {
return plrz.plural(str)
}
export function singularize(str: string) {
return plrz.singular(str)
}
export function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}

49
src/utils/schematics.ts Normal file
View File

@ -0,0 +1,49 @@
import { capitalize } from './formatting'
export function generateSchemaFile(name: string, fields: any) {
name = capitalize(name)
// TODO: fix spacing
const outputObject = JSON.stringify(
fields.reduce((acc: any, curr: any) => {
const { name, ...rest } = curr
acc[name] = rest
return acc
}, {}),
null, 2)
.replace(/"([^"]+)":/g, '$1:')
.replace(/"(\w+)":/g, '$1:')
.replace(/\s*"\w+":/g, match => match.trim())
.replace(/"string"/g, '\'string\'')
return `import { defineMongooseModel } from '#nuxt/mongoose'
export const ${name}Schema = defineMongooseModel({
name: '${name}',
schema: ${outputObject},
})
`
}
export function generateApiRoute(action: string, { model, by }: { model: { name: string; path: string }; by?: string }) {
const modelName = capitalize(model.name)
const schemaImport = `import { ${modelName}Schema } from '../../models/${model.path}'\n\n`
const operation = {
index: `return await ${modelName}Schema.find({})`,
create: `return await new ${modelName}Schema(body).save()`,
show: `return await ${modelName}Schema.findOne({ ${by}: event.context.params?.${by} })`,
put: `return await ${modelName}Schema.findOneAndUpdate({ ${by}: event.context.params?.${by} }, body, { new: true })`,
delete: `return await ${modelName}Schema.findOneAndDelete({ ${by}: event.context.params?.${by} })`,
}[action]
const main = `try {
${operation}
}
catch (error) {
return error
}`
return `${schemaImport}export default defineEventHandler(async (event) => {
${(action === 'create' || action === 'put') ? `const body = await readBody(event)\n ${main}` : main}
})
`
}