create-nolly-template 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,20 +1,28 @@
1
1
  export const mongodb = {
2
2
  key: 'mongodb',
3
3
  name: 'MongoDB support',
4
- description: 'Adds MongoDB support',
4
+ description: 'Adds MongoDB support via prisma directly to your Fastify application.',
5
5
  templateRoot: 'web/backend/fastify/mongodb',
6
6
  dependencies: {
7
- 'fastify-plugin': '^5.1.0',
8
- 'mongodb': '^7.1.0'
7
+ '@prisma/client': '6.19'
8
+ },
9
+ devDependencies: {
10
+ '@types/node': '^18.0.0',
11
+ 'prisma': '6.19'
9
12
  },
10
13
  patches: [
11
14
  {
12
- targetPath: 'src/utils/env.ts',
15
+ targetPath: 'tsconfig.json',
13
16
  operations: [
14
17
  {
15
18
  type: 'insertAfter',
16
- pattern: "NODE_ENV: z.enum\\(\\['development', 'production', 'test'\\]\\)\\.default\\('development'\\)",
17
- insert: "\tMONGODB_URI: z.string().default('mongodb://localhost:27017/myapp')"
19
+ pattern: "\"skipLibCheck\": true",
20
+ insert: ",\n\t\"types\": [\"node\"]"
21
+ },
22
+ {
23
+ type: 'insertAfter',
24
+ pattern: "\"src\"",
25
+ insert: ", \"prisma.config.ts\""
18
26
  }
19
27
  ]
20
28
  }
@@ -22,8 +22,8 @@ export const websocket = {
22
22
  },
23
23
  {
24
24
  type: 'insertAfter',
25
- pattern: `await autoRegister\\(app, 'routes/\\*\\*/\\.routes\\.{ts,js}\\)`,
26
- insert: ` await autoRegister(app, 'routes/**/*.ws.{ts,js}', '/ws')`
25
+ pattern: `routes\.\\{ts,js\\}\'\\)`,
26
+ insert: `await autoRegister(app, 'routes/**/*.ws.{ts,js}', '/ws')`
27
27
  }
28
28
  ]
29
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-nolly-template",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "All of my opiniated templates in one place. This is a CLI tool to create a new project based on one of my templates.",
5
5
  "license": "MIT",
6
6
  "author": "Nolly",
@@ -0,0 +1 @@
1
+ DATABASE_URL="mongodb://localhost:27017/mydatabase"
@@ -0,0 +1,25 @@
1
+ generator client {
2
+ provider = "prisma-client"
3
+ output = "../src/prisma"
4
+ }
5
+
6
+ datasource db {
7
+ provider = "mongodb"
8
+ url = env("DATABASE_URL")
9
+ }
10
+
11
+ model User {
12
+ id String @id @default(auto()) @map("_id") @db.ObjectId
13
+ email String @unique
14
+ name String?
15
+ posts Post[]
16
+ }
17
+
18
+ model Post {
19
+ id String @id @default(auto()) @map("_id") @db.ObjectId
20
+ title String
21
+ content String?
22
+ published Boolean @default(false)
23
+ author User @relation(fields: [authorId], references: [id])
24
+ authorId String @db.ObjectId
25
+ }
@@ -0,0 +1,12 @@
1
+ import 'dotenv/config'
2
+ import { defineConfig, env } from 'prisma/config'
3
+
4
+ export default defineConfig({
5
+ schema: 'prisma/schema.prisma',
6
+ migrations: {
7
+ path: 'prisma/migrations',
8
+ },
9
+ datasource: {
10
+ url: env['DATABASE_URL'],
11
+ },
12
+ })
@@ -0,0 +1,23 @@
1
+ import FastifyPlugin from 'fastify-plugin'
2
+ import { FastifyInstance } from 'fastify'
3
+ import { PrismaClient } from '../prisma/client'
4
+ import { env } from 'prisma/config'
5
+
6
+ declare module 'fastify' {
7
+ interface FastifyInstance {
8
+ database: PrismaClient
9
+ }
10
+ }
11
+
12
+ function hidePassword(): string {
13
+ const url = env('DATABASE_URL')
14
+ return url.replace(/\/\/(.*):(.*)@/, '//$1:****@')
15
+ }
16
+
17
+ export default FastifyPlugin(async (fastify: FastifyInstance) => {
18
+ const database = new PrismaClient()
19
+ await database.$connect()
20
+ fastify.log.info(`Database connected to ${hidePassword()}`)
21
+ fastify.decorate('database', database)
22
+ fastify.addHook('onClose', async (fastify) => await fastify.database.$disconnect())
23
+ })
@@ -1,53 +0,0 @@
1
- import fastifyPlugin from 'fastify-plugin'
2
- import { MongoClient, Db } from 'mongodb'
3
- import { FastifyInstance } from 'fastify'
4
- import path from 'path'
5
- import { fileURLToPath, pathToFileURL } from 'url'
6
- import fastGlob from 'fast-glob'
7
- import { env } from '../utils/env'
8
-
9
- const __filename = fileURLToPath(import.meta.url)
10
- const __dirname = path.dirname(__filename)
11
-
12
- export type ModelFactory = (db: Db, app: FastifyInstance) => any
13
-
14
- declare module 'fastify' {
15
- interface FastifyInstance {
16
- mongo: {
17
- client: MongoClient
18
- db: Db
19
- }
20
- models: Record<string, any>
21
- }
22
- }
23
-
24
- async function autoLoadModels(app: FastifyInstance, db: Db) {
25
- const pattern = path.join(__dirname, '../models/**/*.model.{ts,js}').replace(/\\/g, '/')
26
- const files = fastGlob.sync(pattern)
27
- const models: Record<string, any> = {}
28
- for (const file of files) {
29
- const module = await import(pathToFileURL(file).href)
30
- const factory: ModelFactory = module.default || module[Object.keys(module)[0]]
31
- if (typeof factory !== 'function') continue
32
- const model = factory(db, app)
33
- if (!model?.name) throw new Error(`Model in ${file} must return { name: string }`)
34
- models[model.name] = model
35
- }
36
- return models
37
- }
38
-
39
- export default fastifyPlugin(async function mongoPlugin(app) {
40
- const client = new MongoClient(env.MONGO_URI, { serverSelectionTimeoutMS: 5000 })
41
- try {
42
- await client.connect()
43
- } catch (error: any) {
44
- app.log.error('Mongo connection failed:', error)
45
- throw error
46
- }
47
- const db = client.db(env.MONGO_DB)
48
- app.decorate('mongo', { client, db })
49
- const models = await autoLoadModels(app, db)
50
- app.decorate('models', models)
51
- app.addHook('onClose', async () => { await client.close() })
52
- app.log.info(`📦 Mongo connected: ${env.MONGO_DB}`)
53
- }, { name: 'mongoPlugin' })