Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0dfad07399 | |||
| e6b8b7d0fa | |||
| 0cdba0d764 | |||
| 9c0e0baf95 | |||
| 925d382f2f | |||
| 3ff97569f5 | |||
| 52a5ae9180 | |||
| 4647fbda16 | |||
| 647d26db85 | |||
| 40b8ca91ee | |||
| d92a58b2ad | |||
| 5a43ebe06f | |||
| 56259adaf7 | |||
| 470f272183 | |||
| e2d37c2efe | |||
| 487cca530b | |||
| 9e867fc0b3 | |||
| 23ec1989f7 | |||
| 1c3608743e | |||
| 33bb3cc550 | |||
| fe1807fcd5 | |||
| aaef56cb29 | |||
| 32846c1e44 | |||
| 771e071005 | |||
| 2ba8bdadfc | |||
| 6c057d4cd7 | |||
| 00259fc865 | |||
| 3356ffd052 | |||
| 6ca94dc574 | |||
| 182b760404 | |||
| 66c08e5d52 | |||
| 774d4b6243 | |||
| 86c3c2954e | |||
| 205435e546 | |||
| cf48c2b6df | |||
| ac24680bd4 | |||
| d381058591 | |||
| cfc7ea59b8 | |||
| b4fa9f5617 | |||
| c967d0bb16 | |||
| c8bcc9c488 | |||
| f00819768e | |||
| e0f56dfa38 | |||
| ecac456a4a | |||
| 3d4d959422 |
@ -1,2 +1,5 @@
|
||||
dist
|
||||
node_modules
|
||||
docs
|
||||
.github
|
||||
.vscode
|
||||
|
||||
82
.github/workflows/studio.yml
vendored
Normal file
82
.github/workflows/studio.yml
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
|
||||
name: studio-nuxt-build
|
||||
run-name: studio nuxt build
|
||||
|
||||
on:
|
||||
# Runs on pushes targeting the default branch
|
||||
push:
|
||||
branches: ["main"]
|
||||
|
||||
# Allows you to run this workflow manually from the Actions tab
|
||||
workflow_dispatch:
|
||||
|
||||
# Add write workflow permissions
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
# Allow one concurrent deployment
|
||||
concurrency:
|
||||
group: "pages"
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
# Build job
|
||||
build-and-deploy:
|
||||
if: startsWith(github.event.head_commit.message, 'docs:')
|
||||
runs-on: ${{ matrix.os }}
|
||||
defaults:
|
||||
run:
|
||||
working-directory: docs
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
node: [18]
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Identify package manager
|
||||
id: pkgman
|
||||
run: |
|
||||
cache=`[ -f "docs/pnpm-lock.yaml" ] && echo "pnpm" || ([ -f "docs/package-lock.json" ] && echo "npm" || ([ -f "docs/yarn.lock" ] && echo "yarn" || echo ""))`
|
||||
package_manager=`[ ! -z "$cache" ] && echo "$cache" || echo "pnpm"`
|
||||
echo "cache=$cache" >> $GITHUB_OUTPUT
|
||||
echo "package_manager=$package_manager" >> $GITHUB_OUTPUT
|
||||
|
||||
- uses: pnpm/action-setup@v2.2.4
|
||||
if: ${{ steps.pkgman.outputs.package_manager == 'pnpm' }}
|
||||
name: Install pnpm
|
||||
id: pnpm-install
|
||||
with:
|
||||
version: 7
|
||||
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
version: ${{ matrix.node }}
|
||||
cache: ${{ steps.pkgman.outputs.cache }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: ${{ steps.pkgman.outputs.package_manager }} install
|
||||
|
||||
- name: Install @nuxthq/studio
|
||||
run: ${{ steps.pkgman.outputs.package_manager }} add -D @nuxthq/studio
|
||||
|
||||
- name: Create .nuxtrc
|
||||
run: echo 'modules[]=@nuxthq/studio' > .nuxtrc
|
||||
|
||||
- name: Generate
|
||||
run: ${{ steps.pkgman.outputs.package_manager }} nuxi generate
|
||||
env:
|
||||
NUXT_PUBLIC_STUDIO_API_URL: https://api.nuxt.studio
|
||||
NUXT_PUBLIC_STUDIO_TOKENS: 6222c5e49391ad99e20e3c1b1a475c656436e8b0877c32021147d8727205c440
|
||||
|
||||
- name: Add .nojekyll file
|
||||
run: touch .output/public/.nojekyll
|
||||
|
||||
# Deployment job
|
||||
- name: Deploy 🚀
|
||||
uses: JamesIves/github-pages-deploy-action@v4
|
||||
with:
|
||||
folder: docs/.output/public
|
||||
13
.vscode/settings.json
vendored
13
.vscode/settings.json
vendored
@ -1,16 +1,3 @@
|
||||
{
|
||||
"editor.tabSize": 2,
|
||||
"files.exclude": {
|
||||
"**/.git": true,
|
||||
"**/.svn": true,
|
||||
"**/.hg": true,
|
||||
"**/CVS": true,
|
||||
"**/.DS_Store": true,
|
||||
"**/Thumbs.db": true,
|
||||
"**/node_modules": true,
|
||||
"**/.nuxt": true,
|
||||
"**/.output": true,
|
||||
"**/dist": true,
|
||||
"**/.nuxtrc": true
|
||||
}
|
||||
}
|
||||
|
||||
134
CHANGELOG.md
134
CHANGELOG.md
@ -1,6 +1,140 @@
|
||||
# Changelog
|
||||
|
||||
|
||||
## v0.0.9
|
||||
|
||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.8...v0.0.9)
|
||||
|
||||
### 🚀 Enhancements
|
||||
|
||||
- Add possibility to type mongoose model ([#8](https://github.com/arashsheyda/nuxt-mongoose/pull/8))
|
||||
- Mongoose schema hooks ([d92a58b](https://github.com/arashsheyda/nuxt-mongoose/commit/d92a58b))
|
||||
|
||||
### 🩹 Fixes
|
||||
|
||||
- Update connection message in logger ([#10](https://github.com/arashsheyda/nuxt-mongoose/pull/10))
|
||||
|
||||
### 📖 Documentation
|
||||
|
||||
- **utils:** Add hooks and improve documentation ([647d26d](https://github.com/arashsheyda/nuxt-mongoose/commit/647d26d))
|
||||
- **utils:** Fix model options type ([3ff9756](https://github.com/arashsheyda/nuxt-mongoose/commit/3ff9756))
|
||||
- Add types link ([9c0e0ba](https://github.com/arashsheyda/nuxt-mongoose/commit/9c0e0ba))
|
||||
|
||||
### 🏡 Chore
|
||||
|
||||
- Fix defineNitroPlugin patch ([#9](https://github.com/arashsheyda/nuxt-mongoose/pull/9))
|
||||
- Example env ([40b8ca9](https://github.com/arashsheyda/nuxt-mongoose/commit/40b8ca9))
|
||||
- Fix defineNitroPlugin patch " ([#9](https://github.com/arashsheyda/nuxt-mongoose/pull/9))
|
||||
- Update dependencies ([925d382](https://github.com/arashsheyda/nuxt-mongoose/commit/925d382))
|
||||
- Lint ([0cdba0d](https://github.com/arashsheyda/nuxt-mongoose/commit/0cdba0d))
|
||||
- Fix tsconfig for build time ([e6b8b7d](https://github.com/arashsheyda/nuxt-mongoose/commit/e6b8b7d))
|
||||
|
||||
### ❤️ Contributors
|
||||
|
||||
- Arash Sheyda <sheidaeearash1999@gmail.com>
|
||||
- Arash
|
||||
- Amir-al-mohamad111
|
||||
- Oumar Barry ([@oumarbarry](http://github.com/oumarbarry))
|
||||
|
||||
## v0.0.8
|
||||
|
||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.7...v0.0.8)
|
||||
|
||||
|
||||
### 🚀 Enhancements
|
||||
|
||||
- Documentation ([6ca94dc](https://github.com/arashsheyda/nuxt-mongoose/commit/6ca94dc))
|
||||
|
||||
### 🩹 Fixes
|
||||
|
||||
- Module options type ([182b760](https://github.com/arashsheyda/nuxt-mongoose/commit/182b760))
|
||||
- Run studio action only for docs ([33bb3cc](https://github.com/arashsheyda/nuxt-mongoose/commit/33bb3cc))
|
||||
- Resource generator modelsDir output ([1c36087](https://github.com/arashsheyda/nuxt-mongoose/commit/1c36087))
|
||||
|
||||
### 💅 Refactors
|
||||
|
||||
- Reuse vite's websocket ([aaef56c](https://github.com/arashsheyda/nuxt-mongoose/commit/aaef56c))
|
||||
|
||||
### 📖 Documentation
|
||||
|
||||
- Remove devtools ([6c057d4](https://github.com/arashsheyda/nuxt-mongoose/commit/6c057d4))
|
||||
- **devtools:** Demo video ([2ba8bda](https://github.com/arashsheyda/nuxt-mongoose/commit/2ba8bda))
|
||||
- Style ([23ec198](https://github.com/arashsheyda/nuxt-mongoose/commit/23ec198))
|
||||
- Favicon ([9e867fc](https://github.com/arashsheyda/nuxt-mongoose/commit/9e867fc))
|
||||
|
||||
### 🏡 Chore
|
||||
|
||||
- Playground example ([3356ffd](https://github.com/arashsheyda/nuxt-mongoose/commit/3356ffd))
|
||||
- **deployment:** Add workflow file ([00259fc](https://github.com/arashsheyda/nuxt-mongoose/commit/00259fc))
|
||||
- Update cover ([487cca5](https://github.com/arashsheyda/nuxt-mongoose/commit/487cca5))
|
||||
|
||||
### ❤️ Contributors
|
||||
|
||||
- Arash
|
||||
- Arash Sheyda <sheidaeearash1999@gmail.com>
|
||||
- Arashsheyda <sheidaeearash1999@gmail.com>
|
||||
|
||||
## 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)
|
||||
|
||||
18
README.md
18
README.md
@ -1,4 +1,4 @@
|
||||

|
||||

|
||||
|
||||
<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
|
||||
@ -83,17 +86,6 @@ export const User = defineMongooseModel({
|
||||
})
|
||||
```
|
||||
|
||||
### useMongoose
|
||||
|
||||
This composable returns the Mongoose DB instance. Example usage:
|
||||
|
||||
```vue
|
||||
<script lang="ts" setup>
|
||||
const mongoose = useMongoose()
|
||||
const user = await mongoose.db.collection('users').findOne()
|
||||
</script>
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[MIT License](./LICENSE)
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
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,
|
||||
},
|
||||
})
|
||||
@ -81,8 +81,9 @@ function editDocument(document: any) {
|
||||
async function saveDocument(document: any, create = true) {
|
||||
const method = create ? rpc.createDocument : rpc.updateDocument
|
||||
const newDocument = await method(props.collection, document)
|
||||
// TODO: show toast
|
||||
if (newDocument?.error)
|
||||
return alert(newDocument.error.message)
|
||||
return
|
||||
|
||||
if (create) {
|
||||
if (!documents.value.length) {
|
||||
@ -105,8 +106,9 @@ function discardEditing() {
|
||||
|
||||
async function deleteDocument(document: any) {
|
||||
const newDocument = await rpc.deleteDocument(props.collection, document._id)
|
||||
// TODO: show toast
|
||||
if (newDocument.deletedCount === 0)
|
||||
return alert('Failed to delete document')
|
||||
return
|
||||
|
||||
documents.value = documents.value.filter((doc: any) => doc._id !== document._id)
|
||||
}
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
import { createBirpc } from 'birpc'
|
||||
import { parse, stringify } from 'flatted'
|
||||
import { createHotContext } from 'vite-hot-client'
|
||||
import type { ClientFunctions, ServerFunctions } from '../../src/types'
|
||||
import { PATH_ENTRY } from '../../src/constants'
|
||||
|
||||
const RECONNECT_INTERVAL = 2000
|
||||
import { WS_EVENT_NAME } from '../../src/constants'
|
||||
|
||||
export const wsConnecting = ref(true)
|
||||
export const wsError = ref<any>()
|
||||
export const wsConnectingDebounced = useDebounce(wsConnecting, 2000)
|
||||
|
||||
let connectPromise = connectWS()
|
||||
const connectPromise = connectVite()
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
let onMessage: Function = () => {}
|
||||
|
||||
export const clientFunctions = {
|
||||
@ -19,9 +20,11 @@ export const extendedRpcMap = new Map<string, any>()
|
||||
|
||||
export const rpc = createBirpc<ServerFunctions>(clientFunctions, {
|
||||
post: async (d) => {
|
||||
(await connectPromise).send(d)
|
||||
(await connectPromise).send(WS_EVENT_NAME, d)
|
||||
},
|
||||
on: (fn) => {
|
||||
onMessage = fn
|
||||
},
|
||||
on: (fn) => { onMessage = fn },
|
||||
serialize: stringify,
|
||||
deserialize: parse,
|
||||
resolver(name, fn) {
|
||||
@ -35,35 +38,22 @@ export const rpc = createBirpc<ServerFunctions>(clientFunctions, {
|
||||
onError(error, name) {
|
||||
console.error(`[nuxt-devtools] RPC error on executing "${name}":`, error)
|
||||
},
|
||||
timeout: 120_000,
|
||||
})
|
||||
|
||||
async function connectWS() {
|
||||
const wsUrl = new URL(`ws://host${PATH_ENTRY}`)
|
||||
wsUrl.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
wsUrl.host = 'localhost:3000'
|
||||
async function connectVite() {
|
||||
const hot = await createHotContext()
|
||||
|
||||
const ws = new WebSocket(wsUrl.toString())
|
||||
ws.addEventListener('message', e => onMessage(String(e.data)))
|
||||
ws.addEventListener('error', (e) => {
|
||||
console.error(e)
|
||||
wsError.value = e
|
||||
if (!hot)
|
||||
throw new Error('Unable to connect to devtools')
|
||||
|
||||
hot.on(WS_EVENT_NAME, (data) => {
|
||||
onMessage(data)
|
||||
})
|
||||
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
|
||||
// TODO:
|
||||
// hot.on('vite:connect', (data) => {})
|
||||
// hot.on('vite:disconnect', (data) => {})
|
||||
|
||||
return ws
|
||||
return hot
|
||||
}
|
||||
|
||||
12
docs/.gitignore
vendored
Normal file
12
docs/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
node_modules
|
||||
*.iml
|
||||
.idea
|
||||
*.log*
|
||||
.nuxt
|
||||
.vscode
|
||||
.DS_Store
|
||||
coverage
|
||||
dist
|
||||
sw.*
|
||||
.env
|
||||
.output
|
||||
2
docs/.npmrc
Normal file
2
docs/.npmrc
Normal file
@ -0,0 +1,2 @@
|
||||
shamefully-hoist=true
|
||||
strict-peer-dependencies=false
|
||||
42
docs/app.config.ts
Normal file
42
docs/app.config.ts
Normal file
@ -0,0 +1,42 @@
|
||||
export default defineAppConfig({
|
||||
docus: {
|
||||
title: 'Nuxt Mongoose',
|
||||
description: 'A Nuxt module for simplifying the use of Mongoose in your project.',
|
||||
image: '/cover.jpg',
|
||||
socials: {
|
||||
twitter: 'arash_sheyda',
|
||||
github: 'arashsheyda/nuxt-mongoose',
|
||||
},
|
||||
github: {
|
||||
dir: 'docs/content',
|
||||
branch: 'main',
|
||||
repo: 'nuxt-mongoose',
|
||||
owner: 'arashsheyda',
|
||||
edit: true,
|
||||
},
|
||||
aside: {
|
||||
level: 0,
|
||||
collapsed: false,
|
||||
exclude: [],
|
||||
},
|
||||
main: {
|
||||
padded: true,
|
||||
fluid: true,
|
||||
},
|
||||
header: {
|
||||
logo: true,
|
||||
showLinkIcon: true,
|
||||
exclude: [],
|
||||
fluid: true,
|
||||
},
|
||||
footer: {
|
||||
fluid: true,
|
||||
iconLinks: [
|
||||
{
|
||||
href: 'https://nuxt.com',
|
||||
icon: 'simple-icons:nuxtdotjs',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
18
docs/components/content/Logo.vue
Normal file
18
docs/components/content/Logo.vue
Normal file
@ -0,0 +1,18 @@
|
||||
<template>
|
||||
<svg id="NuxtMongooseLogo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 235.5 33.2">
|
||||
<rect width="32" height="32" rx="7.5" style="fill:#023430" />
|
||||
<path d="M21.4,13.4C20.1,7.6,17.3,6.1,16.6,5a10.1,10.1,0,0,1-.7-1.5,1.6,1.6,0,0,1-.6,1.2,14.1,14.1,0,0,0-4.9,10.5c-.3,6.1,4.5,9.9,5.1,10.3a1.4,1.4,0,0,0,1.4-.2,12.1,12.1,0,0,0,4.5-11.9" style="fill:#10aa50" />
|
||||
<path d="M16.1,22.2a17.8,17.8,0,0,1-.5,3.3s.2,1.5.3,3h.5a30.6,30.6,0,0,1,.5-3.2C16.3,25,16.1,23.6,16.1,22.2Z" style="fill:#b8c4c2" />
|
||||
<path d="M16.9,25.3c-.6-.3-.8-1.7-.8-3.1s.2-4.3.1-6.5,0-10.7-.3-12.1L16.6,5c.7,1.1,3.5,2.6,4.8,8.4A12,12,0,0,1,16.9,25.3Z" style="fill:#12924f" />
|
||||
<text transform="translate(32.8 25.6)" style="font-size:25.818214416503906px;font-family:var(--font-sans);font-weight:700">
|
||||
<tspan fill="currentColor" x="4" y="0">Nuxt</tspan>
|
||||
<tspan x="67.8" y="0" style="fill:#10aa50">Mongoose</tspan>
|
||||
</text>
|
||||
</svg>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
.NuxtMongooseLogo {
|
||||
font-family: serif;
|
||||
}
|
||||
</style>
|
||||
36
docs/content/0.index.md
Normal file
36
docs/content/0.index.md
Normal file
@ -0,0 +1,36 @@
|
||||
---
|
||||
title: Home
|
||||
navigation: false
|
||||
layout: page
|
||||
main:
|
||||
fluid: false
|
||||
---
|
||||
|
||||
:ellipsis{right=0px width=75% blur=150px}
|
||||
|
||||
::block-hero
|
||||
---
|
||||
cta:
|
||||
- Get started
|
||||
- /getting-started/setup
|
||||
secondary:
|
||||
- Open on GitHub →
|
||||
- https://github.com/arashsheyda/nuxt-mongoose
|
||||
---
|
||||
|
||||
#title
|
||||
Nuxt [Mongoose]{style="color: var(--color-primary-500)"}
|
||||
|
||||
#description
|
||||
A Nuxt module for simplifying the use of [Mongoose](https://mongoosejs.com/) in your project.
|
||||
|
||||
#support
|
||||
::terminal
|
||||
---
|
||||
content:
|
||||
- npm i nuxt-mongoose -D
|
||||
---
|
||||
::
|
||||
::
|
||||
|
||||
<!-- TODO: features -->
|
||||
56
docs/content/1.getting-started/1.setup.md
Normal file
56
docs/content/1.getting-started/1.setup.md
Normal file
@ -0,0 +1,56 @@
|
||||
# Setup
|
||||
|
||||
A Nuxt module for simplifying the use of Mongoose in your project.
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install `nuxt-mongoose` to your dependencies.
|
||||
|
||||
::code-group
|
||||
|
||||
```bash [pnpm]
|
||||
pnpm add nuxt-mongoose -D
|
||||
```
|
||||
|
||||
```bash [npm]
|
||||
npm install nuxt-mongoose -D
|
||||
```
|
||||
|
||||
```bash [yarn]
|
||||
yarn add nuxt-mongoose -D
|
||||
```
|
||||
|
||||
::
|
||||
|
||||
2. Add `nuxt-mongoose` to the `modules` section of your `nuxt.config` file.
|
||||
|
||||
```ts [nuxt.config]
|
||||
export default defineNuxtConfig({
|
||||
modules: [
|
||||
'nuxt-mongoose',
|
||||
],
|
||||
})
|
||||
```
|
||||
|
||||
::alert{ type=success }
|
||||
That's it! You can now use Mongoose in your Nuxt app ✨
|
||||
::
|
||||
|
||||
|
||||
## Options
|
||||
|
||||
You can configure the module by adding a `mongoose` section to your `nuxt.config` file.
|
||||
read more about [Mongoose options](/getting-started/configuration).
|
||||
|
||||
```ts [nuxt.config]
|
||||
export default defineNuxtConfig({
|
||||
mongoose: {
|
||||
// Options
|
||||
},
|
||||
})
|
||||
```
|
||||
If you want to configure only the `uri` just add `MONGODB_URI` in your `.env` file.
|
||||
|
||||
```env
|
||||
MONGODB_URI=YOUR_MONGO_URI
|
||||
```
|
||||
21
docs/content/1.getting-started/3.configuration.md
Normal file
21
docs/content/1.getting-started/3.configuration.md
Normal file
@ -0,0 +1,21 @@
|
||||
# Configuration
|
||||
|
||||
Configure Nuxt Mongoose with the `mongoose` property.
|
||||
|
||||
```ts [nuxt.config]
|
||||
export default defineNuxtConfig({
|
||||
mongoose: {
|
||||
uri: 'process.env.MONGODB_URI',
|
||||
options: {},
|
||||
modelsDir: 'models',
|
||||
devtools: true,
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
| **Key** | **Type** | **Default** | **Description** |
|
||||
| ---------------------------- | ---------- | ----------------------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| `uri` | `string` | process.env.MONGODB_URI | Connection Uri String |
|
||||
| `options` | `ConnectOptions` | { } | Connection Options |
|
||||
| `modelsDir` | `string` | models | The models(schema) directory located in `server` for auto-import |
|
||||
| `devtools` | `boolean` | true | Enable Mongoose module in [`Nuxt Devtools`](https://github.com/nuxt/devtools) |
|
||||
2
docs/content/1.getting-started/_dir.yml
Normal file
2
docs/content/1.getting-started/_dir.yml
Normal file
@ -0,0 +1,2 @@
|
||||
icon: tabler:brand-mongodb
|
||||
navigation.redirect: /getting-started/setup
|
||||
69
docs/content/2.api/1.utils.md
Normal file
69
docs/content/2.api/1.utils.md
Normal file
@ -0,0 +1,69 @@
|
||||
# Utils
|
||||
|
||||
Discover all available utils.
|
||||
|
||||
## `defineMongooseModel`
|
||||
|
||||
This function creates a new Mongoose model with schema. Example usage:
|
||||
|
||||
::code-group
|
||||
|
||||
```ts [named parameters]
|
||||
export const User = defineMongooseModel({
|
||||
name: 'User',
|
||||
schema: {
|
||||
email: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
},
|
||||
options: {
|
||||
|
||||
},
|
||||
hooks(schema) {
|
||||
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
```ts [positional parameters]
|
||||
import { defineMongooseModel } from '#nuxt/mongoose'
|
||||
|
||||
export const User = defineMongooseModel('User', {
|
||||
email: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
}, {
|
||||
|
||||
}, (schema) => {
|
||||
|
||||
})
|
||||
```
|
||||
|
||||
::
|
||||
|
||||
|
||||
| **Key** | **Type** | **Require** | **Description** |
|
||||
| ---------------------------- | ----------- | ----------- | ----- |
|
||||
| `name` | `string` | true | Name of Model |
|
||||
| `schema` | [`SchemaDefinition`](https://mongoosejs.com/docs/schematypes.html) | true | Schema Definition of Model |
|
||||
| `options` | [`SchemaOptions`](https://mongoosejs.com/docs/guide.html#options) | false | Schema Options for Model |
|
||||
| `hooks` | [`(schema: Schema<T>) => void`](https://mongoosejs.com/docs/middleware.html) | false | Schema Hooks Function to customize Model |
|
||||
|
||||
|
||||
|
||||
|
||||
## `defineMongooseConnection`
|
||||
This function creates a new Mongoose connection.
|
||||
- `nuxt-mongoose` provides a default connection for you, it auto-register a plugin in nitro, so you don't need to use this function unless you want to create a new connection. more info [here](https://github.com/arashsheyda/nuxt-mongoose/blob/main/src/runtime/server/plugins/mongoose.db.ts).
|
||||
|
||||
Example usage:
|
||||
|
||||
```ts
|
||||
import { defineMongooseConnection } from '#nuxt/mongoose'
|
||||
|
||||
export const connection = defineMongooseConnection('mongodb://127.0.0.1/nuxt-mongoose')
|
||||
```
|
||||
8
docs/content/2.api/2.devtools.md
Normal file
8
docs/content/2.api/2.devtools.md
Normal file
@ -0,0 +1,8 @@
|
||||
# Devtools (beta)
|
||||
|
||||
`nuxt-mongoose` comes with a [Nuxt Devtools](https://github.com/nuxt/devtools) module that allows you to manage your collections and generate api-endpoints & schemas...
|
||||
|
||||
Here is a demo video:
|
||||
|
||||
:video-player{src="https://www.youtube.com/watch?v=hK0npSfr_Vs"}
|
||||
|
||||
2
docs/content/2.api/_dir.yml
Normal file
2
docs/content/2.api/_dir.yml
Normal file
@ -0,0 +1,2 @@
|
||||
title: API
|
||||
icon: tabler:book
|
||||
19
docs/nuxt.config.ts
Normal file
19
docs/nuxt.config.ts
Normal file
@ -0,0 +1,19 @@
|
||||
export default defineNuxtConfig({
|
||||
extends: '@nuxt-themes/docus',
|
||||
|
||||
app: {
|
||||
head: {
|
||||
link: [
|
||||
{
|
||||
rel: 'icon',
|
||||
type: 'image/x-icon',
|
||||
href: '/mongoose-icon.svg',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
modules: [
|
||||
'@nuxthq/studio',
|
||||
],
|
||||
})
|
||||
17
docs/package.json
Normal file
17
docs/package.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "docus-starter",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "nuxi dev",
|
||||
"build": "nuxi build",
|
||||
"generate": "nuxi generate",
|
||||
"preview": "nuxi preview",
|
||||
"lint": "eslint ."
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nuxt-themes/docus": "^1.12.0",
|
||||
"@nuxthq/studio": "^0.13.2",
|
||||
"nuxt": "^3.5.0"
|
||||
}
|
||||
}
|
||||
6655
docs/pnpm-lock.yaml
generated
Normal file
6655
docs/pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
BIN
docs/public/cover.jpg
Normal file
BIN
docs/public/cover.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 46 KiB |
1
docs/public/mongoose-icon.svg
Normal file
1
docs/public/mongoose-icon.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 256 256"><g fill="none"><rect width="256" height="256" fill="#023430" rx="60"/><path fill="#10AA50" d="M171.173 107.591c-10.537-46.481-32.497-58.855-38.099-67.602A99.398 99.398 0 0 1 126.949 28c-.296 4.13-.84 6.73-4.35 9.862c-7.047 6.283-36.977 30.673-39.496 83.486c-2.347 49.242 36.2 79.605 41.292 82.744c3.916 1.927 8.685.041 11.012-1.728c18.581-12.752 43.969-46.75 35.786-94.773"/><path fill="#B8C4C2" d="M128.545 177.871c-.97 12.188-1.665 19.27-4.129 26.235c0 0 1.617 11.603 2.753 23.894h4.019a223.446 223.446 0 0 1 4.384-25.732c-5.203-2.56-6.827-13.702-7.027-24.397Z"/><path fill="#12924F" d="M135.565 202.275c-5.258-2.429-6.779-13.806-7.013-24.404a499.824 499.824 0 0 0 1.136-52.545c-.276-9.194.13-85.158-2.265-96.28a92.425 92.425 0 0 0 5.651 10.936c5.602 8.754 27.569 21.128 38.099 67.609c8.203 47.941-17.047 81.849-35.608 94.684Z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 926 B |
@ -1,26 +1 @@
|
||||
<?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">
|
||||
<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
|
||||
"/>
|
||||
</svg>
|
||||
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 235.5 33.2"><rect width="32" height="32" rx="7.5" style="fill:#023430"/><path d="M21.4,13.4C20.1,7.6,17.3,6.1,16.6,5a10.1,10.1,0,0,1-.7-1.5,1.6,1.6,0,0,1-.6,1.2,14.1,14.1,0,0,0-4.9,10.5c-.3,6.1,4.5,9.9,5.1,10.3a1.4,1.4,0,0,0,1.4-.2,12.1,12.1,0,0,0,4.5-11.9" style="fill:#10aa50"/><path d="M16.1,22.2a17.8,17.8,0,0,1-.5,3.3s.2,1.5.3,3h.5a30.6,30.6,0,0,1,.5-3.2C16.3,25,16.1,23.6,16.1,22.2Z" style="fill:#b8c4c2"/><path d="M16.9,25.3c-.6-.3-.8-1.7-.8-3.1s.2-4.3.1-6.5,0-10.7-.3-12.1L16.6,5c.7,1.1,3.5,2.6,4.8,8.4A12,12,0,0,1,16.9,25.3Z" style="fill:#12924f"/><text transform="translate(32.8 25.6)" style="font-size:25.818214416503906px;font-family:OpenSans-Bold, Open Sans;font-weight:700">Nuxt <tspan x="70.8" y="0" style="fill:#10aa50">Mongoose</tspan></text></svg>
|
||||
|
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 851 B |
8
docs/renovate.json
Normal file
8
docs/renovate.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": [
|
||||
"@nuxtjs"
|
||||
],
|
||||
"lockFileMaintenance": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
18
docs/tokens.config.ts
Normal file
18
docs/tokens.config.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { defineTheme } from 'pinceau'
|
||||
|
||||
export default defineTheme({
|
||||
color: {
|
||||
primary: {
|
||||
50: '#e8f5e9',
|
||||
100: '#c8e6c9',
|
||||
200: '#a5d6a7',
|
||||
300: '#81c784',
|
||||
400: '#66bb6a',
|
||||
500: '#10a74f',
|
||||
600: '#10a74f',
|
||||
700: '#388e3c',
|
||||
800: '#2e7d32',
|
||||
900: '#1b5e20',
|
||||
},
|
||||
},
|
||||
})
|
||||
3
docs/tsconfig.json
Normal file
3
docs/tsconfig.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "./.nuxt/tsconfig.json"
|
||||
}
|
||||
52
package.json
52
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "nuxt-mongoose",
|
||||
"type": "module",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.9",
|
||||
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
@ -14,10 +14,6 @@
|
||||
"require": "./module.cjs",
|
||||
"import": "./dist/module.mjs"
|
||||
},
|
||||
"./types": {
|
||||
"types": "./dist/types.d.ts",
|
||||
"import": "./dist/types.mjs"
|
||||
},
|
||||
"./*": "./*"
|
||||
},
|
||||
"main": "./module.cjs",
|
||||
@ -37,39 +33,37 @@
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest watch"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"mongoose": "^7.0.3",
|
||||
"nuxt": "^3.4.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/devtools-kit": "^0.4.1",
|
||||
"@nuxt/kit": "^3.4.1",
|
||||
"@nuxt/devtools-kit": "^0.6.7",
|
||||
"@nuxt/kit": "^3.6.2",
|
||||
"@types/fs-extra": "^11.0.1",
|
||||
"birpc": "^0.2.11",
|
||||
"birpc": "^0.2.12",
|
||||
"defu": "^6.1.2",
|
||||
"flatted": "^3.2.7",
|
||||
"fs-extra": "^11.1.1",
|
||||
"mongoose": "^7.3.2",
|
||||
"ofetch": "^1.1.1",
|
||||
"pathe": "^1.1.1",
|
||||
"pluralize": "^8.0.0",
|
||||
"sirv": "^2.0.2",
|
||||
"tinyws": "^0.1.0",
|
||||
"sirv": "^2.0.3",
|
||||
"vite-hot-client": "^0.2.1",
|
||||
"ws": "^8.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^0.38.4",
|
||||
"@nuxt/devtools": "^0.4.1",
|
||||
"@nuxt/devtools-ui-kit": "^0.4.1",
|
||||
"@nuxt/module-builder": "^0.3.0",
|
||||
"@nuxt/schema": "^3.4.1",
|
||||
"@nuxt/test-utils": "^3.4.1",
|
||||
"@types/pluralize": "^0.0.29",
|
||||
"@types/ws": "^8.5.4",
|
||||
"changelogen": "^0.5.3",
|
||||
"eslint": "^8.38.0",
|
||||
"mongoose": "^7.0.4",
|
||||
"nuxt": "^3.4.1",
|
||||
"sass": "^1.62.0",
|
||||
"sass-loader": "^13.2.2",
|
||||
"@antfu/eslint-config": "^0.39.7",
|
||||
"@nuxt/devtools": "^0.6.7",
|
||||
"@nuxt/devtools-ui-kit": "^0.6.7",
|
||||
"@nuxt/module-builder": "^0.4.0",
|
||||
"@nuxt/schema": "^3.6.2",
|
||||
"@nuxt/test-utils": "^3.6.2",
|
||||
"@types/pluralize": "^0.0.30",
|
||||
"@types/ws": "^8.5.5",
|
||||
"changelogen": "^0.5.4",
|
||||
"eslint": "^8.44.0",
|
||||
"nuxt": "^3.6.2",
|
||||
"sass": "^1.63.6",
|
||||
"sass-loader": "^13.3.2",
|
||||
"splitpanes": "^3.1.5",
|
||||
"vitest": "^0.30.1"
|
||||
"vitest": "^0.33.0"
|
||||
}
|
||||
}
|
||||
1
playground/.env.example
Normal file
1
playground/.env.example
Normal file
@ -0,0 +1 @@
|
||||
MONGODB_URI="mongodb://127.0.0.1:27017/nuxt-mongoose"
|
||||
8
playground/server/api/users/[_id].delete.ts
Normal file
8
playground/server/api/users/[_id].delete.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
return await UserSchema.findOneAndDelete({ _id: event.context.params?._id })
|
||||
}
|
||||
catch (error) {
|
||||
return error
|
||||
}
|
||||
})
|
||||
8
playground/server/api/users/[_id].get.ts
Normal file
8
playground/server/api/users/[_id].get.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
return await UserSchema.findOne({ _id: event.context.params?._id })
|
||||
}
|
||||
catch (error) {
|
||||
return error
|
||||
}
|
||||
})
|
||||
9
playground/server/api/users/[_id].put.ts
Normal file
9
playground/server/api/users/[_id].put.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
try {
|
||||
return await UserSchema.findOneAndUpdate({ _id: event.context.params?._id }, body, { new: true })
|
||||
}
|
||||
catch (error) {
|
||||
return error
|
||||
}
|
||||
})
|
||||
9
playground/server/api/users/create.post.ts
Normal file
9
playground/server/api/users/create.post.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
try {
|
||||
return await new UserSchema(body).save()
|
||||
}
|
||||
catch (error) {
|
||||
return error
|
||||
}
|
||||
})
|
||||
8
playground/server/api/users/index.get.ts
Normal file
8
playground/server/api/users/index.get.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export default defineEventHandler(async () => {
|
||||
try {
|
||||
return await UserSchema.find({})
|
||||
}
|
||||
catch (error) {
|
||||
return error
|
||||
}
|
||||
})
|
||||
26
playground/server/models/user.schema.ts
Normal file
26
playground/server/models/user.schema.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { defineMongooseModel } from '#nuxt/mongoose'
|
||||
|
||||
export const UserSchema = defineMongooseModel({
|
||||
name: 'User',
|
||||
schema: {
|
||||
name: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
email: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
unique: false,
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
hooks(schema) {
|
||||
schema.pre('save', function (this, next) {
|
||||
this.password = `hash.${this.password}.${Math.random()}`
|
||||
next()
|
||||
})
|
||||
},
|
||||
})
|
||||
6468
pnpm-lock.yaml
generated
6468
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,3 @@
|
||||
export const PATH = '/__nuxt_mongoose__'
|
||||
export const PATH_ENTRY = `${PATH}/entry`
|
||||
export const PATH_CLIENT = `${PATH}/client`
|
||||
export const WS_EVENT_NAME = 'nuxt:devtools:mongoose:rpc'
|
||||
|
||||
@ -1,14 +1,26 @@
|
||||
import { addImportsDir, addServerPlugin, addTemplate, createResolver, defineNuxtModule, logger } from '@nuxt/kit'
|
||||
import {
|
||||
addImportsDir,
|
||||
addServerPlugin,
|
||||
addTemplate,
|
||||
addVitePlugin,
|
||||
createResolver,
|
||||
defineNuxtModule,
|
||||
logger,
|
||||
} from '@nuxt/kit'
|
||||
import { pathExists } from 'fs-extra'
|
||||
import { tinyws } from 'tinyws'
|
||||
import { join } from 'pathe'
|
||||
import { defu } from 'defu'
|
||||
import sirv from 'sirv'
|
||||
import { $fetch } from 'ofetch'
|
||||
import { version } from '../package.json'
|
||||
|
||||
import { PATH_CLIENT, PATH_ENTRY } from './constants'
|
||||
import { PATH_CLIENT } from './constants'
|
||||
import type { ModuleOptions } from './types'
|
||||
|
||||
import { setupRPC } from './server-rpc'
|
||||
|
||||
export type { ModuleOptions }
|
||||
|
||||
export default defineNuxtModule<ModuleOptions>({
|
||||
meta: {
|
||||
name: 'nuxt-mongoose',
|
||||
@ -18,20 +30,32 @@ export default defineNuxtModule<ModuleOptions>({
|
||||
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 as any
|
||||
|
||||
if (nuxt.options.dev) {
|
||||
$fetch('https://registry.npmjs.org/nuxt-mongoose/latest').then((release) => {
|
||||
if (release.version > version)
|
||||
logger.info(`A new version of Nuxt Mongoose (v${release.version}) is available: https://github.com/arashsheyda/nuxt-mongoose/releases/latest`)
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
addImportsDir(resolve('./runtime/composables'))
|
||||
|
||||
if (!options.uri)
|
||||
console.warn('Missing `MONGODB_URI` in `.env`')
|
||||
if (!options.uri) {
|
||||
logger.warn('Missing `MONGODB_URI` in `.env`')
|
||||
return
|
||||
}
|
||||
|
||||
// Public runtimeConfig
|
||||
nuxt.options.runtimeConfig.public.mongoose = defu(nuxt.options.runtimeConfig.public.mongoose || {}, {
|
||||
// Runtime Config
|
||||
runtimeConfig.mongoose = defu(runtimeConfig.mongoose || {}, {
|
||||
uri: options.uri,
|
||||
options: options.options,
|
||||
devtools: options.devtools,
|
||||
modelsDir: options.modelsDir,
|
||||
})
|
||||
|
||||
// Setup devtools UI
|
||||
@ -43,11 +67,11 @@ export default defineNuxtModule<ModuleOptions>({
|
||||
}
|
||||
|
||||
const clientPath = distResolve('./client')
|
||||
const { middleware: rpcMiddleware } = setupRPC(nuxt, options)
|
||||
const { vitePlugin } = setupRPC(nuxt, options)
|
||||
|
||||
addVitePlugin(vitePlugin)
|
||||
|
||||
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 }))
|
||||
})
|
||||
@ -91,6 +115,16 @@ 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'))
|
||||
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
import type { mongo } from 'mongoose'
|
||||
import { connection } from 'mongoose'
|
||||
|
||||
export function useMongoose(): { db: mongo.Db } {
|
||||
return {
|
||||
db: connection?.db,
|
||||
}
|
||||
}
|
||||
@ -5,20 +5,30 @@ 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 mongoose.connect(mongooseUri, { ...mongooseOptions })
|
||||
logger.info('Connected to database')
|
||||
logger.success('Connected to MongoDB database')
|
||||
}
|
||||
catch (err) {
|
||||
logger.error('Error connecting to database', err)
|
||||
logger.error('Error connecting to MongoDB database', err)
|
||||
}
|
||||
}
|
||||
|
||||
export function defineMongooseModel(nameOrOptions: string | { name: string; schema: SchemaDefinition; options?: SchemaOptions }, schema?: SchemaDefinition, options?: SchemaOptions): Model<any> {
|
||||
export function defineMongooseModel<T>(
|
||||
nameOrOptions: string | {
|
||||
name: string
|
||||
schema: SchemaDefinition
|
||||
options?: SchemaOptions
|
||||
hooks?: (schema: mongoose.Schema<T>) => void
|
||||
},
|
||||
schema?: SchemaDefinition,
|
||||
options?: SchemaOptions,
|
||||
hooks?: (schema: mongoose.Schema<T>) => void,
|
||||
): Model<T> {
|
||||
let name: string
|
||||
if (typeof nameOrOptions === 'string') {
|
||||
name = nameOrOptions
|
||||
@ -27,10 +37,13 @@ export function defineMongooseModel(nameOrOptions: string | { name: string; sche
|
||||
name = nameOrOptions.name
|
||||
schema = nameOrOptions.schema
|
||||
options = nameOrOptions.options
|
||||
hooks = nameOrOptions.hooks
|
||||
}
|
||||
|
||||
const newSchema = new mongoose.Schema({
|
||||
...schema,
|
||||
}, { ...options })
|
||||
return mongoose.model(name, newSchema)
|
||||
const newSchema = new mongoose.Schema<T>(schema, options as any)
|
||||
|
||||
if (hooks)
|
||||
hooks(newSchema)
|
||||
|
||||
return mongoose.model<T>(name, newSchema)
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
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 { Plugin } from 'vite'
|
||||
import type { Nuxt } from 'nuxt/schema'
|
||||
import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
|
||||
import { WS_EVENT_NAME } from '../constants'
|
||||
import { setupDatabaseRPC } from './database'
|
||||
import { setupResourceRPC } from './resource'
|
||||
|
||||
@ -68,14 +68,30 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
|
||||
} 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()
|
||||
|
||||
const vitePlugin: Plugin = {
|
||||
name: 'nuxt:devtools:rpc',
|
||||
configureServer(server) {
|
||||
server.ws.on('connection', (ws) => {
|
||||
wsClients.add(ws)
|
||||
const channel: ChannelOptions = {
|
||||
post: d => ws.send(d),
|
||||
on: fn => ws.on('message', fn),
|
||||
post: d => ws.send(JSON.stringify({
|
||||
type: 'custom',
|
||||
event: WS_EVENT_NAME,
|
||||
data: d,
|
||||
})),
|
||||
on: (fn) => {
|
||||
ws.on('message', (e) => {
|
||||
try {
|
||||
const data = JSON.parse(String(e)) || {}
|
||||
if (data.type === 'custom' && data.event === WS_EVENT_NAME) {
|
||||
// console.log(data.data)
|
||||
fn(data.data)
|
||||
}
|
||||
}
|
||||
catch {}
|
||||
})
|
||||
},
|
||||
serialize: stringify,
|
||||
deserialize: parse,
|
||||
}
|
||||
@ -90,14 +106,12 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
|
||||
c.splice(index, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
middleware,
|
||||
vitePlugin,
|
||||
...ctx,
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@ import { generateApiRoute, generateSchemaFile } from '../utils/schematics'
|
||||
import { capitalize, pluralize, singularize } from '../utils/formatting'
|
||||
|
||||
export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any {
|
||||
const runtimeConfig = nuxt.options.runtimeConfig as any
|
||||
|
||||
return {
|
||||
// TODO: maybe separate functions
|
||||
async generateResource(collection: Collection, resources: Resource[]) {
|
||||
@ -13,9 +15,9 @@ export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any
|
||||
const dbName = capitalize(singular)
|
||||
|
||||
if (collection.fields) {
|
||||
const schemaPath = resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`)
|
||||
const schemaPath = resolve(nuxt.options.serverDir, runtimeConfig.mongoose.modelsDir, `${singular}.schema.ts`)
|
||||
if (!fs.existsSync(schemaPath)) {
|
||||
fs.ensureDirSync(resolve(nuxt.options.serverDir, 'utils/models'))
|
||||
fs.ensureDirSync(resolve(nuxt.options.serverDir, runtimeConfig.mongoose.modelsDir))
|
||||
fs.writeFileSync(schemaPath, generateSchemaFile(dbName, collection.fields))
|
||||
}
|
||||
|
||||
@ -25,9 +27,9 @@ export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any
|
||||
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`,
|
||||
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'
|
||||
@ -51,7 +53,7 @@ export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any
|
||||
async resourceSchema(collection: string) {
|
||||
// TODO: use magicast
|
||||
const singular = singularize(collection).toLowerCase()
|
||||
const schemaPath = resolve(nuxt.options.serverDir, 'utils/models', `${singular}.schema.ts`)
|
||||
const schemaPath = resolve(nuxt.options.serverDir, runtimeConfig.mongoose.modelsDir, `${singular}.schema.ts`)
|
||||
if (fs.existsSync(schemaPath)) {
|
||||
const content = fs.readFileSync(schemaPath, 'utf-8').match(/schema: \{(.|\n)*\}/g)
|
||||
if (content) {
|
||||
|
||||
@ -4,4 +4,5 @@ export interface ModuleOptions {
|
||||
uri: string
|
||||
devtools: boolean
|
||||
options?: ConnectOptions
|
||||
modelsDir?: string
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ export interface ClientFunctions {
|
||||
|
||||
export interface Collection {
|
||||
name: string
|
||||
fields?: {}[]
|
||||
fields?: object[]
|
||||
}
|
||||
|
||||
export interface Resource {
|
||||
|
||||
@ -11,5 +11,5 @@ export interface NuxtDevtoolsServerContext {
|
||||
|
||||
refresh: (event: keyof ServerFunctions) => void
|
||||
|
||||
extendServerRpc: <ClientFunctions = {}, ServerFunctions = {}>(name: string, functions: ServerFunctions) => BirpcGroup<ClientFunctions, ServerFunctions>
|
||||
extendServerRpc: <ClientFunctions = object, ServerFunctions = object>(name: string, functions: ServerFunctions) => BirpcGroup<ClientFunctions, ServerFunctions>
|
||||
}
|
||||
|
||||
1
types.d.ts
vendored
1
types.d.ts
vendored
@ -1 +0,0 @@
|
||||
export * from './dist/types'
|
||||
Reference in New Issue
Block a user