45 Commits

Author SHA1 Message Date
66c08e5d52 chore(release): v0.0.7 2023-05-31 14:05:47 +03:00
774d4b6243 chore: update dependencies 2023-05-31 14:05:07 +03:00
86c3c2954e chore: improve logger message 2023-05-31 14:03:45 +03:00
205435e546 feat: auto-import schema files 2023-05-31 14:03:11 +03:00
cf48c2b6df fix: mongoose config 2023-05-31 12:54:07 +03:00
ac24680bd4 chore(release): v0.0.6 2023-05-16 13:46:26 +03:00
d381058591 fix: remove useMongoose composable 2023-05-16 13:44:13 +03:00
cfc7ea59b8 chore(release): v0.0.5 2023-05-12 09:15:08 +03:00
b4fa9f5617 fix: update icon link 2023-04-28 15:40:55 +03:00
c967d0bb16 fix: update icon link 2023-04-28 15:40:19 +03:00
c8bcc9c488 fix: update logo 2023-04-27 15:39:09 +03:00
f00819768e fix: add brackets 2023-04-27 00:05:13 +03:00
e0f56dfa38 fix: update readme 2023-04-26 23:44:57 +03:00
ecac456a4a chore: update dependencies 2023-04-26 23:39:39 +03:00
3d4d959422 chore(release): v0.0.4 2023-04-26 23:34:44 +03:00
d6a524f5f5 chore(release): v0.0.3 2023-04-26 22:51:02 +03:00
e9b764b72f fix: handle errors 2023-04-26 22:45:40 +03:00
260eadd837 chore: move readyState 2023-04-26 22:44:45 +03:00
ad0c5b2d40 fix: initial pagination 2023-04-26 18:34:51 +03:00
3ac731c5ee feat: initial pagination 2023-04-26 18:31:25 +03:00
f40c48370c feat: duplicate document 2023-04-26 17:45:11 +03:00
8d8eed3e75 chore(release): v0.0.2 2023-04-26 16:03:13 +03:00
e354318b55 chore: remove unused code 2023-04-26 16:01:12 +03:00
798a324f17 chore: styling 2023-04-26 15:56:48 +03:00
a2cf4656eb chore: refactor 2023-04-25 16:27:06 +03:00
cfc255c33b fix: styling 2023-04-25 16:26:19 +03:00
150954290c feat: mongodb readyState 2023-04-25 16:25:42 +03:00
58ca500acc fix: add useMongoose composable 2023-04-25 13:30:31 +03:00
d152597155 fix: auto-import models 2023-04-24 22:15:44 +03:00
01f2c149fa fix: fix mongoose connection 2023-04-23 00:30:00 +03:00
5de7715356 feat: experimental generate resource 2023-04-23 00:25:11 +03:00
a95cb78c6a fix: fix mongoose import 2023-04-23 00:23:39 +03:00
0e6efb4c81 feat: default layout 2023-04-21 13:59:45 +03:00
e43eecb6f9 chore: fix ui 2023-04-21 13:51:16 +03:00
2234826810 chore: ui 2023-04-21 13:27:43 +03:00
c2fc88292f feat: navbar component 2023-04-21 13:27:16 +03:00
ce9cad3362 feat: create document 2023-04-21 12:56:59 +03:00
904404130a feat: global styles 2023-04-21 12:22:18 +03:00
f8a073ff6b fix: fix border line 2023-04-21 12:21:32 +03:00
711d15926f feat: initial database ui 2023-04-21 12:17:38 +03:00
c8b8583ae9 feat: database server-rpc 2023-04-21 12:07:50 +03:00
85f1547150 feat: splitpanes 2023-04-21 12:05:32 +03:00
49a08979d8 feat: client rpc 2023-04-21 12:03:21 +03:00
366a07c5b0 fix: type.d.ts 2023-04-21 11:56:14 +03:00
7df0af10f2 feat: initial setup for nuxt devtools 2023-04-20 18:16:10 +03:00
40 changed files with 5721 additions and 2407 deletions

View File

@ -1,5 +1,130 @@
# Changelog
## v0.0.7
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.6...v0.0.7)
### 🚀 Enhancements
- Auto-import schema files ([205435e](https://github.com/arashsheyda/nuxt-mongoose/commit/205435e))
### 🩹 Fixes
- Mongoose config ([cf48c2b](https://github.com/arashsheyda/nuxt-mongoose/commit/cf48c2b))
### 🏡 Chore
- Improve logger message ([86c3c29](https://github.com/arashsheyda/nuxt-mongoose/commit/86c3c29))
- Update dependencies ([774d4b6](https://github.com/arashsheyda/nuxt-mongoose/commit/774d4b6))
### ❤️ Contributors
- Arashsheyda <sheidaeearash1999@gmail.com>
## v0.0.6
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.5...v0.0.6)
### 🩹 Fixes
- Remove useMongoose composable ([d381058](https://github.com/arashsheyda/nuxt-mongoose/commit/d381058))
### ❤️ Contributors
- Arashsheyda <sheidaeearash1999@gmail.com>
## v0.0.5
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.4...v0.0.5)
### 🩹 Fixes
- Update readme ([e0f56df](https://github.com/arashsheyda/nuxt-mongoose/commit/e0f56df))
- Add brackets ([f008197](https://github.com/arashsheyda/nuxt-mongoose/commit/f008197))
- Update logo ([c8bcc9c](https://github.com/arashsheyda/nuxt-mongoose/commit/c8bcc9c))
- Update icon link ([c967d0b](https://github.com/arashsheyda/nuxt-mongoose/commit/c967d0b))
- Update icon link ([b4fa9f5](https://github.com/arashsheyda/nuxt-mongoose/commit/b4fa9f5))
### 🏡 Chore
- Update dependencies ([ecac456](https://github.com/arashsheyda/nuxt-mongoose/commit/ecac456))
### ❤️ Contributors
- Arashsheyda <sheidaeearash1999@gmail.com>
- Arash
## v0.0.4
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.3...v0.0.4)
## v0.0.3
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.2...v0.0.3)
### 🚀 Enhancements
- Duplicate document ([f40c483](https://github.com/arashsheyda/nuxt-mongoose/commit/f40c483))
- Initial pagination ([3ac731c](https://github.com/arashsheyda/nuxt-mongoose/commit/3ac731c))
### 🩹 Fixes
- Initial pagination ([ad0c5b2](https://github.com/arashsheyda/nuxt-mongoose/commit/ad0c5b2))
- Handle errors ([e9b764b](https://github.com/arashsheyda/nuxt-mongoose/commit/e9b764b))
### 🏡 Chore
- Move readyState ([260eadd](https://github.com/arashsheyda/nuxt-mongoose/commit/260eadd))
### ❤️ Contributors
- Arashsheyda <sheidaeearash1999@gmail.com>
## v0.0.2
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.1...v0.0.2)
### 🚀 Enhancements
- Initial setup for nuxt devtools ([7df0af1](https://github.com/arashsheyda/nuxt-mongoose/commit/7df0af1))
- Client rpc ([49a0897](https://github.com/arashsheyda/nuxt-mongoose/commit/49a0897))
- Splitpanes ([85f1547](https://github.com/arashsheyda/nuxt-mongoose/commit/85f1547))
- Database server-rpc ([c8b8583](https://github.com/arashsheyda/nuxt-mongoose/commit/c8b8583))
- Initial database ui ([711d159](https://github.com/arashsheyda/nuxt-mongoose/commit/711d159))
- Global styles ([9044041](https://github.com/arashsheyda/nuxt-mongoose/commit/9044041))
- Create document ([ce9cad3](https://github.com/arashsheyda/nuxt-mongoose/commit/ce9cad3))
- Navbar component ([c2fc882](https://github.com/arashsheyda/nuxt-mongoose/commit/c2fc882))
- Default layout ([0e6efb4](https://github.com/arashsheyda/nuxt-mongoose/commit/0e6efb4))
- Experimental generate resource ([5de7715](https://github.com/arashsheyda/nuxt-mongoose/commit/5de7715))
- Mongodb readyState ([1509542](https://github.com/arashsheyda/nuxt-mongoose/commit/1509542))
### 🩹 Fixes
- Type.d.ts ([366a07c](https://github.com/arashsheyda/nuxt-mongoose/commit/366a07c))
- Fix border line ([f8a073f](https://github.com/arashsheyda/nuxt-mongoose/commit/f8a073f))
- Fix mongoose import ([a95cb78](https://github.com/arashsheyda/nuxt-mongoose/commit/a95cb78))
- Fix mongoose connection ([01f2c14](https://github.com/arashsheyda/nuxt-mongoose/commit/01f2c14))
- Auto-import models ([d152597](https://github.com/arashsheyda/nuxt-mongoose/commit/d152597))
- Add useMongoose composable ([58ca500](https://github.com/arashsheyda/nuxt-mongoose/commit/58ca500))
- Styling ([cfc255c](https://github.com/arashsheyda/nuxt-mongoose/commit/cfc255c))
### 🏡 Chore
- Ui ([2234826](https://github.com/arashsheyda/nuxt-mongoose/commit/2234826))
- Fix ui ([e43eecb](https://github.com/arashsheyda/nuxt-mongoose/commit/e43eecb))
- Refactor ([a2cf465](https://github.com/arashsheyda/nuxt-mongoose/commit/a2cf465))
- Styling ([798a324](https://github.com/arashsheyda/nuxt-mongoose/commit/798a324))
- Remove unused code ([e354318](https://github.com/arashsheyda/nuxt-mongoose/commit/e354318))
### ❤️ Contributors
- Arashsheyda <sheidaeearash1999@gmail.com>
## v0.0.1

View File

@ -1,4 +1,4 @@
![nuxt-mongoose](./docs/public/nuxt-mongoose.svg)
![nuxt-mongoose](https://raw.githubusercontent.com/arashsheyda/nuxt-mongoose/c967d0bb162dddc27fa510ba3b49e4f9b6b2db61/docs/public/nuxt-mongoose.svg)
<div align="center">
<h1>Nuxt Mongoose</h1>
@ -10,7 +10,7 @@
## Installation
```bash
pnpm add nuxt-mongoose mongoose
pnpm add nuxt-mongoose
```
## Usage
@ -36,10 +36,13 @@ export default defineNuxtConfig({
mongoose: {
uri: 'process.env.MONGODB_URI',
options: {},
modelsDir: 'models',
},
})
```
by default, `nuxt-mongoose` will auto-import your schemas from the `models` directory from `server` directory. You can change this behavior by setting the `modelsDir` option.
* for more information about the options, please refer to the [Mongoose documentation](https://mongoosejs.com/docs/connections.html#options). *
## API

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,
},
})

1
client/.nuxtrc Normal file
View File

@ -0,0 +1 @@
imports.autoImport=true

19
client/app.vue Normal file
View File

@ -0,0 +1,19 @@
<script lang="ts" setup>
import './styles/global.css'
</script>
<template>
<Html>
<Body h-screen>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</Body>
</Html>
</template>
<style>
#__nuxt {
height: 100%;
}
</style>

View File

@ -0,0 +1,34 @@
<script lang="ts" setup>
defineProps({
connection: {
type: Number,
default: 0,
},
})
</script>
<template>
<NPanelGrids>
<div flex="~ gap-2" animate-pulse items-center text-yellow>
<NIcon icon="carbon-flow-connection" />
Please check your mongodb connection
</div>
<div flex="~ gap-2" items-center text-light>
Your current connection is: {{ connection }}
</div>
<div absolute bottom-10 left-10 right-10 flex justify-around>
<NCard p2 text-red-5>
0: Not connected
</NCard>
<NCard p2 text-green-5>
1: Connected
</NCard>
<NCard p2 text-yellow-5>
2: Connecting
</NCard>
<NCard p2 text-orange-5>
3: Disconnecting
</NCard>
</div>
</NPanelGrids>
</template>

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>
<div sticky top-0 px8 py4 glass-effect z-1>
<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>
<div grid="~ cols-1" gap-2 my4>
<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 glass-effect fixed right-10 bottom-8 px8 py2 icon="carbon-magic-wand-filled" n="green" @click="generate">
Create
</NButton>
</div>
</template>
<style></style>

View File

@ -0,0 +1,282 @@
<script lang="ts" setup>
const props = defineProps({
collection: {
type: String,
required: true,
},
})
// TODO: save in local storage
const pagination = reactive({ limit: 20, page: 1 })
const countDocuments = computedAsync(async () => {
return await rpc.countDocuments(props.collection)
})
const documents = computedAsync(async () => {
return await rpc.listDocuments(props.collection, pagination)
})
watch(pagination, async () => {
documents.value = await rpc.listDocuments(props.collection, pagination)
})
const schema = computedAsync<any>(async () => {
return await rpc.resourceSchema(props.collection)
})
const fields = computed(() => {
if (documents.value && documents.value.length > 0)
return Object.keys(documents.value[0])
if (schema.value)
return Object.keys(schema.value)
return []
})
const search = ref('')
const editing = ref(false)
const dbContainer = ref<HTMLElement>()
const selectedDocument = ref()
const filtered = computed(() => {
if (!search.value)
return documents.value
return documents.value.filter((document: any) => {
for (const field of fields.value) {
if (document[field].toString().toLowerCase().includes(search.value.toLowerCase()))
return true
}
return false
})
})
function addDocument() {
// TODO: validate & show errors
if (editing.value)
return
editing.value = true
selectedDocument.value = {}
if (schema.value) {
for (const field of Object.keys(schema.value))
selectedDocument.value[field] = ''
}
else {
for (const field of fields.value) {
if (field !== '_id')
selectedDocument.value[field] = ''
}
}
const parent = dbContainer.value?.parentElement
parent?.scrollTo(0, parent.scrollHeight)
}
function editDocument(document: any) {
if (editing.value)
return
editing.value = true
selectedDocument.value = { ...document }
}
async function saveDocument(document: any, create = true) {
const method = create ? rpc.createDocument : rpc.updateDocument
const newDocument = await method(props.collection, document)
if (newDocument?.error)
return alert(newDocument.error.message)
if (create) {
if (!documents.value.length) {
documents.value = await rpc.listDocuments(props.collection, pagination)
return discardEditing()
}
documents.value.push({ _id: newDocument.insertedId, ...document })
}
else {
const index = documents.value.findIndex((doc: any) => doc._id === newDocument.value._id)
documents.value[index] = document
}
discardEditing()
}
function discardEditing() {
editing.value = false
selectedDocument.value = null
}
async function deleteDocument(document: any) {
const newDocument = await rpc.deleteDocument(props.collection, document._id)
if (newDocument.deletedCount === 0)
return alert('Failed to delete document')
documents.value = documents.value.filter((doc: any) => doc._id !== document._id)
}
const copy = useCopy()
</script>
<template>
<div ref="dbContainer" :class="{ 'h-full': !documents?.length }">
<Navbar v-model:search="search" sticky top-0 px4 py2 backdrop-blur z-10>
<template #actions>
<NButton icon="carbon:add" n="green" @click="addDocument">
Add Document
</NButton>
</template>
<div v-if="countDocuments" flex items-center>
<div op50>
<span v-if="search">{{ filtered.length }} matched · </span>
<span>{{ documents?.length }} of {{ countDocuments }} documents in total</span>
</div>
<div flex-auto />
<div flex gap-2>
<NSelect v-if="pagination.limit !== 0" v-model="pagination.page">
<option v-for="i in Math.ceil(countDocuments / pagination.limit)" :key="i" :value="i">
page:
{{ i }}
</option>
</NSelect>
<NSelect v-model="pagination.limit">
<option v-for="i in [1, 2, 3, 4, 5]" :key="i" :value="i * 10">
show:
{{ i * 10 }}
</option>
<option :value="0">
show all
</option>
</NSelect>
</div>
</div>
</Navbar>
<table v-if="documents?.length || selectedDocument" w-full mb10 :class="{ 'editing-mode': editing }">
<thead>
<tr>
<th v-for="field of fields" :key="field" text-start>
{{ field }}
</th>
<th text-center>
Actions
</th>
</tr>
</thead>
<tbody>
<tr v-for="document in filtered" :key="document._id" :class="{ isEditing: editing && selectedDocument._id === document._id }">
<td v-for="field of fields" :key="field" @dblclick="editDocument(document)">
<template v-if="editing && selectedDocument._id === document._id">
<input v-model="selectedDocument[field]" :disabled="field === '_id'">
</template>
<span v-else>
{{ document[field] }}
</span>
</td>
<td class="actions">
<div flex justify-center gap2 class="group">
<template v-if="editing && selectedDocument._id === document._id">
<NIconButton icon="carbon-save" @click="saveDocument(selectedDocument, false)" />
<NIconButton icon="carbon-close" @click="discardEditing" />
</template>
<template v-else>
<NIconButton icon="carbon-edit" @click="editDocument(document)" />
<NIconButton icon="carbon-delete" @click="deleteDocument(document)" />
<NIconButton icon="carbon-document-multiple-02" @click="saveDocument(document)" />
<NIconButton absolute right-4 opacity-0 group-hover="opacity-100" transition-all icon="carbon-copy" @click="copy(JSON.stringify(document))" />
</template>
</div>
</td>
</tr>
<tr v-if="editing && !selectedDocument?._id" :class="{ isEditing: editing && !selectedDocument?._id }">
<td v-for="field of fields" :key="field">
<input v-if="field !== '_id'" v-model="selectedDocument[field]" :placeholder="field">
<input v-else placeholder="ObjectId(_id)" disabled>
</td>
<td flex justify-center gap2 class="actions">
<NIconButton icon="carbon-save" @click="saveDocument(selectedDocument)" />
<NIconButton icon="carbon-close" @click="discardEditing" />
</td>
</tr>
</tbody>
</table>
<div v-else flex justify-center items-center h-full text-2xl>
<NIcon icon="carbon-document" mr1 />
No documents found
</div>
</div>
</template>
<style lang="scss">
// TODO:
.actions .n-icon {
margin: 0;
}
table {
table-layout: fixed;
tr {
width: 100%;
td, th {
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
input {
border: none;
background: transparent;
color: rgba(255, 255, 255, 0.7);
width: 100%;
&::placeholder {
color: rgba(255, 255, 255, 0.3);
}
&:focus {
outline: none;
}
}
}
th {
border-right: 1px solid #272727;
border-left: 1px solid #272727;
border-top: 1px solid #272727;
padding: 5px 10px;
}
td {
border: 1px solid #272727;
&:last-child {
border-left: none;
}
padding: 5px 10px;
color: rgba(255, 255, 255, 0.7);
&:hover {
color: #fff;
}
}
.editing-mode {
tr {
&:not(.isEditing) {
opacity: 0.3;
position: relative;
&::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000;
opacity: 0.3;
}
}
&.isEditing {
opacity: 1;
color: #fff;
input {
&::placeholder {
color: #3ede80;
}
}
}
}
}
</style>

View File

@ -0,0 +1,54 @@
<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')
}, {
ignore: ['#open-drawer-right'],
})
</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

@ -0,0 +1,43 @@
<script setup lang="ts">
defineProps({
search: {
type: String,
default: undefined,
},
noPadding: {
type: Boolean,
default: true,
},
placeholder: {
type: String,
default: 'Search...',
},
})
const emit = defineEmits<{
(event: 'update:search', value: string): void
}>()
function update(event: any) {
emit('update:search', event.target.value)
}
</script>
<template>
<div flex="~ col gap2" border="b base" flex-1 navbar-glass :class="[{ p4: !noPadding }]">
<div flex="~ gap4">
<slot name="search">
<NTextInput
:placeholder="placeholder"
icon="carbon-search"
n="primary" flex-auto
:class="{ 'px-5 py-2': !noPadding }"
:value="search"
@input="update"
/>
</slot>
<slot name="actions" />
</div>
<slot />
</div>
</template>

View File

@ -0,0 +1,36 @@
<script lang="ts" setup>
import { Pane, Splitpanes } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
const props = defineProps({
minLeft: {
type: Number,
default: 10,
},
minRight: {
type: Number,
default: 10,
},
maxLeft: {
type: Number,
default: 90,
},
maxRight: {
type: Number,
default: 90,
},
})
const size = ref(props.maxLeft)
</script>
<template>
<Splitpanes h-full of-hidden @resize="size = $event[0].size">
<Pane border="r base" h-full class="of-auto!" :size="size" :min-size="minLeft" :max-size="maxLeft">
<slot name="left" />
</Pane>
<Pane relative h-full class="of-auto!" :min-size="minRight">
<slot name="right" />
</Pane>
</Splitpanes>
</template>

View File

@ -0,0 +1,10 @@
import { useClipboard } from '@vueuse/core'
export function useCopy() {
const clipboard = useClipboard()
return (text: string) => {
clipboard.copy(text)
// TODO: show toast
}
}

69
client/composables/rpc.ts Normal file
View File

@ -0,0 +1,69 @@
import { createBirpc } from 'birpc'
import { parse, stringify } from 'flatted'
import type { ClientFunctions, ServerFunctions } from '../../src/types'
import { PATH_ENTRY } from '../../src/constants'
const RECONNECT_INTERVAL = 2000
export const wsConnecting = ref(true)
export const wsError = ref<any>()
let connectPromise = connectWS()
let onMessage: Function = () => {}
export const clientFunctions = {
// will be added in app.vue
} as ClientFunctions
export const extendedRpcMap = new Map<string, any>()
export const rpc = createBirpc<ServerFunctions>(clientFunctions, {
post: async (d) => {
(await connectPromise).send(d)
},
on: (fn) => { onMessage = fn },
serialize: stringify,
deserialize: parse,
resolver(name, fn) {
if (fn)
return fn
if (!name.includes(':'))
return
const [namespace, fnName] = name.split(':')
return extendedRpcMap.get(namespace)?.[fnName]
},
onError(error, name) {
console.error(`[nuxt-devtools] RPC error on executing "${name}":`, error)
},
})
async function connectWS() {
const wsUrl = new URL(`ws://host${PATH_ENTRY}`)
wsUrl.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
wsUrl.host = 'localhost:3000'
const ws = new WebSocket(wsUrl.toString())
ws.addEventListener('message', e => onMessage(String(e.data)))
ws.addEventListener('error', (e) => {
console.error(e)
wsError.value = e
})
ws.addEventListener('close', () => {
// eslint-disable-next-line no-console
console.log('[nuxt-devtools] WebSocket closed, reconnecting...')
wsConnecting.value = true
setTimeout(async () => {
connectPromise = connectWS()
}, RECONNECT_INTERVAL)
})
wsConnecting.value = true
if (ws.readyState !== WebSocket.OPEN)
await new Promise(resolve => ws.addEventListener('open', resolve))
// eslint-disable-next-line no-console
console.log('[nuxt-devtools] WebSocket connected.')
wsConnecting.value = false
wsError.value = null
return ws
}

View File

@ -0,0 +1,10 @@
<script lang="ts" setup>
const readyState = computedAsync(async () => await rpc.readyState())
</script>
<template>
<div h-full of-auto>
<slot v-if="readyState === 1" />
<Connection v-else :connection="readyState" />
</div>
</template>

27
client/nuxt.config.ts Normal file
View File

@ -0,0 +1,27 @@
import { resolve } from 'pathe'
import { PATH_CLIENT } from '../src/constants'
export default defineNuxtConfig({
ssr: false,
modules: [
'@nuxt/devtools-ui-kit',
],
unocss: {
shortcuts: {
'bg-base': 'bg-white dark:bg-[#151515]',
'bg-active': 'bg-gray:5',
'bg-hover': 'bg-gray:3',
'border-base': 'border-gray/20',
'glass-effect': 'backdrop-blur-6 bg-white/80 dark:bg-[#151515]/90',
'navbar-glass': 'sticky z-10 top-0 glass-effect',
},
},
nitro: {
output: {
publicDir: resolve(__dirname, '../dist/client'),
},
},
app: {
baseURL: PATH_CLIENT,
},
})

4
client/package.json Normal file
View File

@ -0,0 +1,4 @@
{
"name": "nuxt-mongoose-client",
"private": true
}

79
client/pages/index.vue Normal file
View File

@ -0,0 +1,79 @@
<script lang="ts" setup>
const route = useRoute()
const router = useRouter()
const selectedCollection = ref()
const drawer = ref(false)
const search = ref('')
const collections = computedAsync(async () => {
return await rpc.listCollections()
})
const filtered = computed(() => {
if (!search.value)
return collections.value
return collections.value.filter((c: any) => c.name.toLowerCase().includes(search.value.toLowerCase()))
})
onMounted(() => {
if (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()
drawer.value = false
}
</script>
<template>
<PanelLeftRight :min-left="13" :max-left="20">
<template #left>
<div px4>
<Navbar v-model:search="search" :placeholder="`${collections?.length ?? '-'} collection in total`" mt2>
<div flex items-center gap2>
<NIconButton w-full mb1.5 icon="carbon-reset" title="Refresh" @click="refresh" />
<NIconButton w-full mb1.5 icon="carbon-data-base" title="Default" text-green-5 />
<NIconButton id="open-drawer-right" w-full mb1.5 icon="carbon-add" title="Create Collection" @click="drawer = true" />
</div>
</Navbar>
<div grid gird-cols-1 my2 mx1>
<NuxtLink v-for="table in filtered" :key="table.name" :to="{ name: 'index', query: { table: table.name } }" flex justify-between p2 my1 hover-bg-green hover-bg-opacity-5 hover-text-green rounded-lg :class="{ 'bg-green bg-opacity-5 text-green': selectedCollection === table.name }" @click="selectedCollection = table.name">
<span>
<NIcon icon="carbon-db2-database" />
{{ table.name }}
</span>
<div flex gap2>
<NIconButton block n="red" icon="carbon-delete" @click="dropCollection(table)" />
<!-- <NIconButton icon="carbon-overflow-menu-horizontal" /> -->
</div>
</NuxtLink>
</div>
</div>
</template>
<template #right>
<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>
</PanelLeftRight>
<DrawerRight v-model="drawer" style="width: calc(80.5%);" auto-close @close="drawer = false">
<CreateResource @refresh="refresh" />
</DrawerRight>
</template>

21
client/splitpanes.d.ts vendored Normal file
View File

@ -0,0 +1,21 @@
// TODO install @types/splitpanes once updated
declare module 'splitpanes' {
import { Component } from 'vue'
export interface SplitpaneProps {
horizontal: boolean
pushOtherPanes: boolean
dblClickSplitter: boolean
firstSplitter: boolean
}
export interface PaneProps {
size: number | string
minSize: number | string
maxSize: number | string
}
export type Pane = Component<PaneProps>
export const Pane: Pane
export const Splitpanes: Component<SplitpaneProps>
}

35
client/styles/global.css Normal file
View File

@ -0,0 +1,35 @@
/* Splitpanes */
.splitpanes__splitter {
position: relative;
}
.splitpanes__splitter:before {
position: absolute;
left: 0;
top: 0;
transition: .2s ease;
content: '';
transition: opacity 0.4s;
z-index: 1;
}
.splitpanes__splitter:hover:before {
background: #8881;
opacity: 1;
}
.splitpanes--vertical>.splitpanes__splitter {
min-width: 0 !important;
width: 0 !important;
}
.splitpanes--horizontal>.splitpanes__splitter {
min-height: 0 !important;
height: 0 !important;
}
.splitpanes--vertical>.splitpanes__splitter:before {
left: -5px;
right: -4px;
height: 100%;
}
.splitpanes--horizontal>.splitpanes__splitter:before {
top: -5px;
bottom: -4px;
width: 100%;
}

View File

@ -1,26 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 25.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 914.6 296.6" style="enable-background:new 0 0 914.6 296.6;" xml:space="preserve">
viewBox="0 0 520.1 232.7" style="enable-background:new 0 0 520.1 232.7;" xml:space="preserve">
<style type="text/css">
.st0{fill:#880000;}
.st1{fill:#00DC82;}
</style>
<path class="st0" d="M620,225.4c-47.8-6.5-96.2,6.6-134.1,36.4c-9.7-24.4-21.2-48-34.1-70.8c-10.8-19.1-18.1-29.1-28.6-29.9
c-10.5-0.8-21.3,9.9-25.3,15c-2.4-6.3-11.1-27-25.7-28.6c-14.6-1.6-26.7,16.2-41.7,38c-12.2,17.8-22,37.1-29.3,57.4
c-4.9-14.1-18.2-46.7-51.4-69.3c-42.6-28.9-100.8-29.7-151.2-3.2c82.8-70,172.3-122.4,284.9-120c10.8,56.5,46.3,100.5,79.8,105.9
c-14.4-13.7-33.3-29-48.6-47.3c-13.2-15.6-21.3-34.8-21-56.6c0.2-12.6,5.8-19.8,17.5-18.2c23.3,3.5,46.3,8.9,69.3,13.6
c2.5,0.5,4.9,1.7,8.4,0.5l-23.2-11.5c0.4-1.4,0.7-2.8,1.1-4.2c114.6-5.8,221.8,26,328.4,65.5c-1.4,7.4-2.5,12.9-3.6,18.4l1.5,0.6
l7.1-14c16.3,2.2,18.3,6.4,14.1,20.7c-7.4,25.7-24.1,39.2-50,44.1c-41.3,7.9-82.2,17.2-123.5,25.3
C614.3,198.5,613.4,199.4,620,225.4z M481.1,94.6c-16.8-20-51.7-37.3-67.5-34.9c-2.3,22.4,34.4,68.2,57.8,71.7
c-2.7-4.4-5.3-8.4-7.8-12.6l6.4-3.5c-4.6-6-8.8-11.4-15.2-19.8l18.7,7.9l-6.6-13.7L481.1,94.6z M672,98.1l0.2-4.8l-90.4-17.4
l-1.3,4.5c14.2,7,27.8,16.7,42.8,20.2C639.4,104.2,656.2,103.4,672,98.1L672,98.1z M659.4,162.1l-1.6-4.3
c-20.4,8.3-41.2,15.8-61,25.3c-11.1,5.3-10.9,14-3,22.5C601.6,170.5,636.4,174.7,659.4,162.1L659.4,162.1z"/>
<path class="st1" d="M399.5,264.6H459c1.9,0,3.8-0.5,5.4-1.4c3.3-1.9,5.3-5.4,5.3-9.3c0-1.9-0.5-3.7-1.4-5.3l-40-68.7
c-0.9-1.6-2.3-2.9-3.9-3.8c-3.4-2-7.5-2-10.8,0c-1.6,0.9-3,2.3-3.9,3.8l-10.2,17.6l-20-34.4c-1-1.6-2.4-2.9-4-3.8
c-3.3-2-7.5-2-10.7,0c-1.6,0.9-3,2.3-3.9,3.8L311,248.6c-0.9,1.6-1.5,3.5-1.5,5.3c0.1,3.8,2.1,7.3,5.4,9.3c1.6,0.9,3.5,1.4,5.4,1.4
h37.4c14.7,0,25.7-6.4,33.3-19l18.2-31.4l9.8-16.7l29.3,50.4h-39.1L399.5,264.6z M357.1,247.8H331l39.1-67.1l19.5,33.6l-13.1,22.4
C371.6,244.9,365.9,247.8,357.1,247.8z"/>
<path class="st1" d="M672,98.1l0.2-4.8l-90.4-17.4l-1.3,4.5c14.2,7,27.8,16.7,42.8,20.2C639.4,104.2,656.2,103.4,672,98.1L672,98.1z
"/>
<path class="st0" d="M323.9,193.5c-47.8-6.5-96.2,6.6-134.1,36.4c-9.7-24.3-21.1-48-34.1-70.8c-10.8-19.1-18.1-29.1-28.6-29.9
s-21.3,9.9-25.3,15C94,126,82.2,113.3,71.2,114.3c-6.5,0.6-12,7.6-22.6,21.5C38.3,149.5,31,161.7,25,172.5
c-5.4,9.7-12.4,22.7-19.9,38.5C5.3,133.1,6,75,5.8,49.3c0-4.6-0.1-15.1,6.8-22.9c7.3-8.2,18.6-9.1,34.2-10.2
c9.6-0.7,23.6-0.8,40.5,2.3c1.2,12,8.2,42,31.4,72.1c14.4,18.7,31.7,31.1,48.4,33.8c-14.4-13.7-33.3-29-48.6-47.3
c-13.2-15.6-21.3-34.8-21-56.6c0.2-12.6,5.8-19.8,17.5-18.2c23.3,3.5,46.3,8.9,69.3,13.6c2.5,0.5,4.9,1.7,8.4,0.5L169.6,4.9
c0.4-1.4,0.7-2.8,1.1-4.2c114.6-5.8,221.8,26,328.4,65.5c-1.4,7.4-2.5,12.9-3.6,18.4l1.5,0.6l7.1-14c16.3,2.2,18.3,6.4,14.1,20.7
c-7.4,25.7-24.1,39.2-50,44.1c-41.3,7.9-82.2,17.2-123.5,25.3C318.2,166.6,317.3,167.5,323.9,193.5z M185,62.7
c-16.8-20-51.7-37.3-67.5-34.9c-2.3,22.4,34.4,68.2,57.8,71.7c-2.7-4.4-5.3-8.4-7.8-12.6l6.4-3.5l-15.2-19.8l18.7,7.9l-6.6-13.7
L185,62.7z M375.9,66.2l0.2-4.8L285.7,44l-1.3,4.5c14.2,7,27.8,16.7,42.8,20.2C343.4,72.3,360.2,71.5,375.9,66.2L375.9,66.2z
M363.3,130.2l-1.6-4.3c-20.4,8.3-41.2,15.8-61,25.3c-11.1,5.3-10.9,14-3,22.5C305.5,138.6,340.3,142.8,363.3,130.2z"/>
<path class="st1" d="M103.4,232.7h59.5c1.9,0,3.7-0.5,5.4-1.4c3.3-1.9,5.3-5.5,5.3-9.3c0-1.9-0.5-3.7-1.4-5.3l-40-68.7
c-0.9-1.6-2.3-2.9-3.9-3.8c-3.3-2-7.5-2-10.8,0c-1.6,0.9-2.9,2.2-3.9,3.8l-10.2,17.6l-20-34.4c-1-1.6-2.4-2.9-4-3.8
c-3.3-2-7.4-2-10.7,0c-1.6,0.9-2.9,2.2-3.9,3.8l-49.9,85.5c-3,5-1.3,11.5,3.7,14.5c1.7,1,3.6,1.5,5.6,1.5h37.4
c14.7,0,25.7-6.4,33.3-19l18.2-31.4l9.8-16.7l29.3,50.4h-39.1L103.4,232.7z M61,215.9H34.9L74,148.8l19.5,33.6l-13.1,22.4
C75.5,213,69.8,215.9,61,215.9L61,215.9z"/>
<path class="st1" d="M375.9,66.2l0.2-4.8L285.7,44l-1.3,4.5c14.2,7,27.8,16.7,42.8,20.2C343.4,72.3,360.2,71.5,375.9,66.2
L375.9,66.2z"/>
</svg>

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

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

@ -1,7 +1,7 @@
{
"name": "nuxt-mongoose",
"type": "module",
"version": "0.0.1",
"version": "0.0.7",
"description": "Nuxt 3 module for MongoDB with Mongoose",
"license": "MIT",
"repository": {
@ -11,42 +11,62 @@
"exports": {
".": {
"types": "./dist/types.d.ts",
"require": "./dist/module.cjs",
"require": "./module.cjs",
"import": "./dist/module.mjs"
}
},
"main": "./dist/module.cjs",
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.mjs"
},
"./*": "./*"
},
"main": "./module.cjs",
"types": "./dist/types.d.ts",
"files": [
"dist"
],
"scripts": {
"prepack": "nuxt-module-build",
"build": "pnpm dev:prepare && pnpm build:module && pnpm build:client",
"build:client": "nuxi generate client",
"build:module": "nuxt-build-module",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"release": "npm run lint && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
"dev:prepare": "nuxi prepare client",
"dev:prod": "npm run build && pnpm dev",
"release": "npm run lint && npm run build && changelogen --release && npm publish && git push --follow-tags",
"lint": "eslint .",
"test": "vitest run",
"test:watch": "vitest watch"
},
"peerDependencies": {
"mongoose": "^7.0.3",
"nuxt": "^3.4.1"
},
"dependencies": {
"@nuxt/kit": "^3.4.1",
"@nuxt/devtools-kit": "^0.5.5",
"@nuxt/kit": "^3.5.2",
"@types/fs-extra": "^11.0.1",
"birpc": "^0.2.11",
"defu": "^6.1.2",
"mongoose": "^7.0.3"
"flatted": "^3.2.7",
"fs-extra": "^11.1.1",
"mongoose": "^7.2.2",
"pathe": "^1.1.0",
"pluralize": "^8.0.0",
"sirv": "^2.0.3",
"tinyws": "^0.1.0",
"ws": "^8.13.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.38.4",
"@nuxt/module-builder": "^0.3.0",
"@nuxt/schema": "^3.4.1",
"@nuxt/test-utils": "^3.4.1",
"@antfu/eslint-config": "^0.38.5",
"@nuxt/devtools": "^0.5.5",
"@nuxt/devtools-ui-kit": "^0.5.5",
"@nuxt/module-builder": "^0.4.0",
"@nuxt/schema": "^3.5.2",
"@nuxt/test-utils": "^3.5.2",
"@types/pluralize": "^0.0.29",
"@types/ws": "^8.5.4",
"changelogen": "^0.5.3",
"eslint": "^8.38.0",
"nuxt": "^3.4.1",
"vitest": "^0.30.1"
"eslint": "^8.39.0",
"nuxt": "^3.5.2",
"sass": "^1.62.1",
"sass-loader": "^13.3.1",
"splitpanes": "^3.1.5",
"vitest": "^0.31.2"
}
}

View File

@ -1,7 +1,6 @@
export default defineNuxtConfig({
modules: ['../src/module'],
mongoose: {
uri: 'mongodb://127.0.0.1/nuxt-mongoose',
},
modules: [
'@nuxt/devtools',
'../src/module',
],
})

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,
// },
// })

6337
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

3
src/constants/index.ts Normal file
View File

@ -0,0 +1,3 @@
export const PATH = '/__nuxt_mongoose__'
export const PATH_ENTRY = `${PATH}/entry`
export const PATH_CLIENT = `${PATH}/client`

View File

@ -1,11 +1,23 @@
import { addServerPlugin, addTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
import {
addImportsDir,
addServerPlugin,
addTemplate,
createResolver,
defineNuxtModule,
logger,
} from '@nuxt/kit'
import { pathExists } from 'fs-extra'
import { tinyws } from 'tinyws'
import { join } from 'pathe'
import { defu } from 'defu'
import type { ConnectOptions } from 'mongoose'
import sirv from 'sirv'
export interface ModuleOptions {
uri?: string
options?: ConnectOptions
}
import { PATH_CLIENT, PATH_ENTRY } from './constants'
import type { ModuleOptions } from './types'
import { setupRPC } from './server-rpc'
export type { ModuleOptions }
export default defineNuxtModule<ModuleOptions>({
meta: {
@ -14,18 +26,59 @@ export default defineNuxtModule<ModuleOptions>({
},
defaults: {
uri: process.env.MONGODB_URI as string,
devtools: true,
options: {},
modelsDir: 'models',
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
const runtimeConfig = nuxt.options.runtimeConfig
if (!options.uri)
console.warn('Missing `MONGODB_URI` in `.env`')
addImportsDir(resolve('./runtime/composables'))
// Public runtimeConfig
nuxt.options.runtimeConfig.public.mongoose = defu(nuxt.options.runtimeConfig.public.mongoose || {}, {
if (!options.uri) {
logger.warn('Missing `MONGODB_URI` in `.env`')
return
}
// Runtime Config
runtimeConfig.mongoose = defu(runtimeConfig.mongoose || {}, {
uri: options.uri,
options: options.options,
devtools: options.devtools,
modelsDir: options.modelsDir,
})
// Setup devtools UI
const distResolve = (p: string) => {
const cwd = resolve('.')
if (cwd.endsWith('/dist'))
return resolve(p)
return resolve(`../dist/${p}`)
}
const clientPath = distResolve('./client')
const { middleware: rpcMiddleware } = setupRPC(nuxt, options)
nuxt.hook('vite:serverCreated', async (server) => {
server.middlewares.use(PATH_ENTRY, tinyws() as any)
server.middlewares.use(PATH_ENTRY, rpcMiddleware as any)
if (await pathExists(clientPath))
server.middlewares.use(PATH_CLIENT, sirv(clientPath, { dev: true, single: true }))
})
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore runtime type
nuxt.hook('devtools:customTabs', (iframeTabs) => {
iframeTabs.push({
name: 'mongoose',
title: 'Mongoose',
icon: 'skill-icons:mongodb',
view: {
type: 'iframe',
src: PATH_CLIENT,
},
})
})
// virtual imports
@ -53,7 +106,19 @@ export default defineNuxtModule<ModuleOptions>({
options.references.push({ path: resolve(nuxt.options.buildDir, 'types/nuxt-mongoose.d.ts') })
})
// Nitro auto imports
nuxt.hook('nitro:config', (_nitroConfig) => {
if (_nitroConfig.imports) {
_nitroConfig.imports.dirs = _nitroConfig.imports.dirs || []
_nitroConfig.imports.dirs?.push(
join(nuxt.options.serverDir, runtimeConfig.mongoose.modelsDir),
)
}
})
// Add server-plugin for database connection
addServerPlugin(resolve('./runtime/server/plugins/mongoose.db'))
logger.success('`nuxt-mongoose` is ready!')
},
})

View File

@ -1,17 +1,17 @@
import type { ConnectOptions, Model, SchemaDefinition, SchemaOptions } from 'mongoose'
import { Schema, connect, model } from 'mongoose'
import mongoose from 'mongoose'
import { logger } from '@nuxt/kit'
import { useRuntimeConfig } from '#imports'
export async function defineMongooseConnection({ uri, options }: { uri?: string; options?: ConnectOptions } = {}): Promise<void> {
const config = useRuntimeConfig().public.mongoose
const config = useRuntimeConfig().mongoose
const mongooseUri = uri || config.uri
const mongooseOptions = options || config.options
try {
await connect(mongooseUri, { ...mongooseOptions })
logger.info('Connected to database')
await mongoose.connect(mongooseUri, { ...mongooseOptions })
logger.info('Connected to mongoose database')
}
catch (err) {
logger.error('Error connecting to database', err)
@ -29,8 +29,8 @@ export function defineMongooseModel(nameOrOptions: string | { name: string; sche
options = nameOrOptions.options
}
const newSchema = new Schema({
const newSchema = new mongoose.Schema({
...schema,
}, { ...options })
return model(name, newSchema)
return mongoose.model(name, newSchema)
}

103
src/server-rpc/database.ts Normal file
View File

@ -0,0 +1,103 @@
import mongoose from 'mongoose'
import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types'
export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
mongoose.connect(options.uri, options.options)
return {
async readyState() {
return mongoose.connection.readyState
},
async createCollection(name: string) {
try {
return await mongoose.connection.db.createCollection(name)
}
catch (error) {
return ErrorIT(error)
}
},
async listCollections() {
try {
return await mongoose.connection.db.listCollections().toArray()
}
catch (error) {
return ErrorIT(error)
}
},
async getCollection(name: string) {
try {
return await mongoose.connection.db.collection(name).findOne()
}
catch (error) {
return ErrorIT(error)
}
},
async dropCollection(name: string) {
try {
return await mongoose.connection.db.dropCollection(name)
}
catch (error) {
return ErrorIT(error)
}
},
async createDocument(collection: string, data: any) {
const { _id, ...rest } = data
try {
return await mongoose.connection.db.collection(collection).insertOne(rest)
}
catch (error: any) {
return ErrorIT(error)
}
},
async countDocuments(collection: string) {
try {
return await mongoose.connection.db.collection(collection).countDocuments()
}
catch (error) {
return ErrorIT(error)
}
},
async listDocuments(collection: string, options: { page: number; limit: number } = { page: 1, limit: 10 }) {
const skip = (options.page - 1) * options.limit
const cursor = mongoose.connection.db.collection(collection).find().skip(skip)
if (options.limit !== 0)
cursor.limit(options.limit)
return await cursor.toArray()
},
async getDocument(collection: string, document: any) {
try {
return await mongoose.connection.db.collection(collection).findOne({ document })
}
catch (error) {
return ErrorIT(error)
}
},
async updateDocument(collection: string, data: any) {
const { _id, ...rest } = data
try {
return await mongoose.connection.db.collection(collection).findOneAndUpdate({ _id: new mongoose.Types.ObjectId(_id) }, { $set: rest })
}
catch (error) {
return ErrorIT(error)
}
},
async deleteDocument(collection: string, id: string) {
try {
return await mongoose.connection.db.collection(collection).deleteOne({ _id: new mongoose.Types.ObjectId(id) })
}
catch (error) {
return ErrorIT(error)
}
},
} satisfies Partial<ServerFunctions>
}
function ErrorIT(error: any) {
return {
error: {
message: error?.message,
code: error?.code,
},
}
}

103
src/server-rpc/index.ts Normal file
View File

@ -0,0 +1,103 @@
import type { TinyWSRequest } from 'tinyws'
import type { NodeIncomingMessage, NodeServerResponse } from 'h3'
import type { WebSocket } from 'ws'
import { createBirpcGroup } from 'birpc'
import type { ChannelOptions } from 'birpc'
import { parse, stringify } from 'flatted'
import type { Nuxt } from 'nuxt/schema'
import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
import { setupDatabaseRPC } from './database'
import { setupResourceRPC } from './resource'
export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
const serverFunctions = {} as ServerFunctions
const extendedRpcMap = new Map<string, any>()
const rpc = createBirpcGroup<ClientFunctions, ServerFunctions>(
serverFunctions,
[],
{
resolver: (name, fn) => {
if (fn)
return fn
if (!name.includes(':'))
return
const [namespace, fnName] = name.split(':')
return extendedRpcMap.get(namespace)?.[fnName]
},
onError(error, name) {
console.error(`[nuxt-devtools] RPC error on executing "${name}":`, error)
},
},
)
function refresh(event: keyof ServerFunctions) {
rpc.broadcast.refresh.asEvent(event)
}
function extendServerRpc(namespace: string, functions: any): any {
extendedRpcMap.set(namespace, functions)
return {
broadcast: new Proxy({}, {
get: (_, key) => {
if (typeof key !== 'string')
return
return (rpc.broadcast as any)[`${namespace}:${key}`]
},
}),
}
}
const ctx: NuxtDevtoolsServerContext = {
nuxt,
options,
rpc,
refresh,
extendServerRpc,
}
// @ts-expect-error untyped
nuxt.devtools = ctx
Object.assign(serverFunctions, {
...setupDatabaseRPC(ctx),
...setupResourceRPC(ctx),
} satisfies Partial<ServerFunctions>)
const wsClients = new Set<WebSocket>()
const middleware = async (req: NodeIncomingMessage & TinyWSRequest, _res: NodeServerResponse, next: Function) => {
// Handle WebSocket
if (req.ws) {
const ws = await req.ws()
wsClients.add(ws)
const channel: ChannelOptions = {
post: d => ws.send(d),
on: fn => ws.on('message', fn),
serialize: stringify,
deserialize: parse,
}
rpc.updateChannels((c) => {
c.push(channel)
})
ws.on('close', () => {
wsClients.delete(ws)
rpc.updateChannels((c) => {
const index = c.indexOf(channel)
if (index >= 0)
c.splice(index, 1)
})
})
}
else {
next()
}
}
return {
middleware,
...ctx,
}
}

View File

@ -0,0 +1,66 @@
import fs from 'fs-extra'
import { resolve } from 'pathe'
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, rpc }: NuxtDevtoolsServerContext): any {
return {
// TODO: maybe separate functions
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) {
const schemaPath = resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`)
if (!fs.existsSync(schemaPath)) {
fs.ensureDirSync(resolve(nuxt.options.serverDir, 'utils/models'))
fs.writeFileSync(schemaPath, generateSchemaFile(dbName, collection.fields))
}
const model = { name: dbName, path: `${singular}.schema` }
// create resources
const routeTypes = {
index: 'index.get.ts',
create: 'create.post.ts',
show: (by: string) => `[${by}].get.ts`,
put: (by: string) => `[${by}].put.ts`,
delete: (by: string) => `[${by}].delete.ts`,
}
resources.forEach((route: Resource) => {
const fileName = typeof routeTypes[route.type] === 'function'
? (routeTypes[route.type] as any)(route.by)
: routeTypes[route.type]
const filePath = resolve(nuxt.options.serverDir, 'api', plural, fileName)
if (!fs.existsSync(filePath)) {
fs.ensureDirSync(resolve(nuxt.options.serverDir, `api/${plural}`))
const content = generateApiRoute(route.type, { model, by: route.by })
fs.writeFileSync(filePath, content)
}
})
}
// create collection if not exists
const collections = await rpc.functions.listCollections()
if (!collections.find((c: any) => c.name === plural))
await rpc.functions.createCollection(plural)
},
async resourceSchema(collection: string) {
// TODO: use magicast
const singular = singularize(collection).toLowerCase()
const schemaPath = resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`)
if (fs.existsSync(schemaPath)) {
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>
}

3
src/types/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './rpc'
export * from './server-ctx'
export * from './module-options'

View File

@ -0,0 +1,8 @@
import type { ConnectOptions } from 'mongoose'
export interface ModuleOptions {
uri: string
devtools: boolean
options?: ConnectOptions
modelsDir?: string
}

34
src/types/rpc.ts Normal file
View File

@ -0,0 +1,34 @@
export interface ServerFunctions {
// Database - collections
readyState(): Promise<any>
createCollection(name: string): Promise<any>
listCollections(): Promise<any>
getCollection(name: string): Promise<any>
dropCollection(name: string): Promise<any>
// Database - documents
createDocument(collection: string, data: any): Promise<any>
countDocuments(collection: string): Promise<any>
listDocuments(collection: string, options: any): Promise<any>
getDocument(collection: string, id: string): Promise<any>
updateDocument(collection: string, data: any): 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 {
refresh(type: string): void
}
export interface Collection {
name: string
fields?: {}[]
}
export interface Resource {
type: 'index' | 'create' | 'show' | 'put' | 'delete'
by?: string
}

15
src/types/server-ctx.ts Normal file
View File

@ -0,0 +1,15 @@
import type { BirpcGroup } from 'birpc'
import type { Nuxt } from 'nuxt/schema'
import type { ClientFunctions, ServerFunctions } from './rpc'
import type { ModuleOptions } from './module-options'
export interface NuxtDevtoolsServerContext {
nuxt: Nuxt
options: ModuleOptions
rpc: BirpcGroup<ClientFunctions, ServerFunctions>
refresh: (event: keyof ServerFunctions) => void
extendServerRpc: <ClientFunctions = {}, ServerFunctions = {}>(name: string, functions: ServerFunctions) => BirpcGroup<ClientFunctions, ServerFunctions>
}

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)
}

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

@ -0,0 +1,48 @@
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 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 `export default defineEventHandler(async (event) => {
${(action === 'create' || action === 'put') ? `const body = await readBody(event)\n ${main}` : main}
})
`
}

View File

@ -1,3 +1,3 @@
{
"extends": "./playground/.nuxt/tsconfig.json"
"extends": "./client/.nuxt/tsconfig.json"
}

1
types.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from './dist/types'