Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
decoderid committed Apr 6, 2023
1 parent ecd3b0c commit 7e3c50c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
4 changes: 3 additions & 1 deletion adonis-typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare module '@ioc:Adonis/Addons/Mongoose' {
export * from 'mongoose'
import * as Mongoose from 'mongoose'
export default Mongoose
}

16 changes: 9 additions & 7 deletions commands/MakeSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseCommand, args, Kernel } from '@adonisjs/ace'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { join } from 'path'
import path from 'path'

export default class MakeTask extends BaseCommand {
public static commandName = 'make:mongoose'
Expand All @@ -21,24 +21,26 @@ export default class MakeTask extends BaseCommand {
public async generate (isInterface: Boolean): Promise<void> {
this.logger.info(isInterface ? 'Generate Interface' : 'Generate Schema')

const file = isInterface ? '../templates/interfaceTemplate.txt' : '../templates/schemaTemplate.txt'
const templatePath = join(__dirname, file)
const file = isInterface ? '/templates/interfaceTemplate.txt' : '/templates/schemaTemplate.txt'
const templatePath = path.resolve(this.application.appRoot + '/node_modules/adonis5-mongoose' + file)

const destDir = isInterface ? 'app/Schema/interfaces' : 'app/Schema'

this.generator
.addFile(isInterface ? this.name + '.d' : this.name , { pattern: 'pascalcase', form: 'singular' })
.addFile(this.name , { pattern: 'pascalcase', form: 'singular' })
.stub(templatePath)
.destinationDir('app/Schema')
.destinationDir(destDir)
.useMustache()
.appRoot(this.application.cliCwd || this.application.appRoot)

await this.generator.run()
}

public async handle (): Promise<void> {
public async run (): Promise<void> {
try {
await this.generate(false)
await this.generate(true)
} catch (e) {
console.error(e)
this.logger.error('Failed to generate collection class with error ' + e.message)
}
}
Expand Down
1 change: 1 addition & 0 deletions commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ['adonis5-mongoose/build/commands/MakeSchema']
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "adonis5-mongoose",
"version": "1.0.0",
"description": "Adonis5 Mongoose Provider",
"main": "index.js",
"author": "decoder.id",
"main": "build/providers/MongooseProvider.js",
"author": "it@decoder.id",
"license": "MIT",
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
Expand Down Expand Up @@ -56,7 +56,8 @@
"build/adonis-typings",
"build/commands",
"build/providers",
"build/templates"
"build/templates",
"build/src"
],
"adonisjs": {
"commands": [
Expand Down
16 changes: 10 additions & 6 deletions providers/MongooseProvider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { Mongoose } from 'mongoose'
import Env from '@ioc:Adonis/Core/Env'
import * as mongoose from 'mongoose'

export default class MongooseProvider {
public static needsApplication = true

constructor (protected app: ApplicationContract) {}

public register () {
const mongoose = new Mongoose()
const Logger = this.app.container.use('Adonis/Core/Logger')
const env = this.app.container.use('Adonis/Core/Env')

mongoose.connect(Env.get('MONGO_URI', 'mongodb://localhost/test'))
mongoose.connect(env.get('MONGODB_URI')).then(() => {
Logger.info('DB Connection: OK')
}).catch(err => {
Logger.error(err, 'DB Error')
})

this.app.container.singleton('Mongoose', () => mongoose)
this.app.container.singleton('Adonis/Addons/Mongoose', () => mongoose)
}

public async boot () {
Expand All @@ -24,6 +28,6 @@ export default class MongooseProvider {
}

public async shutdown () {
await this.app.container.use('Mongoose').disconnect()
await this.app.container.use('Adonis/Addons/Mongoose').disconnect()
}
}
2 changes: 1 addition & 1 deletion templates/interfaceTemplate.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Document } from '@ioc:Adonis/Addons/Mongoose'

export interface AuthInterface extends Document {
export interface Interface{{ filename }} extends Document {
name: String
}
6 changes: 3 additions & 3 deletions templates/schemaTemplate.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema, model } from '@ioc:Adonis/Addons/Mongoose'
import { {{ filename }} } from './{{filename}}.d.ts'
import { Interface{{ filename }} } from './interfaces/{{filename}}'

const schema = new Schema<{{ filename }}>(
const schema = new Schema<Interface{{ filename }}>(
{
name: {
type: String,
Expand All @@ -16,4 +16,4 @@ const schema = new Schema<{{ filename }}>(
}
)

export default model<{{ filename }}>('{{ filename }}', schema)
export default model<Interface{{ filename }}>('{{ filename }}', schema)

0 comments on commit 7e3c50c

Please sign in to comment.