Issues importing a class in the provider folder of AdonisJS while creating TypeScript definitions.

Could anyone help me with an issue I'm facing while building a database wrapper for Prisma in adonis? I am trying to create a typescript definition for it, but I'm unable to import the Database class from providers/DatabaseProvider/database.ts. I've tried recreating the project multiple times, but the issue persists. Thank you smmmm contracts/DatabaseProvider/database.ts
declare module "@ioc:Evermore/Database" {
import Database from "providers/DatabseProvider/database";
const db: Database;
export default db;
}
declare module "@ioc:Evermore/Database" {
import Database from "providers/DatabseProvider/database";
const db: Database;
export default db;
}
providers/DatabaseProvider/index.ts
import type { ApplicationContract } from "@ioc:Adonis/Core/Application";

export default class DatabaseProvider {
constructor(protected app: ApplicationContract) {}

public register() {
// Register your own bindings

this.app.container.singleton("Evermore/Database", () => {
const databaseProv = require("./database").default;
return new databaseProv();
});
}

public async boot() {
// All bindings are ready, feel free to use them
}

public async ready() {
// App is ready
}

public async shutdown() {
// Cleanup, since app is going down
}
}
import type { ApplicationContract } from "@ioc:Adonis/Core/Application";

export default class DatabaseProvider {
constructor(protected app: ApplicationContract) {}

public register() {
// Register your own bindings

this.app.container.singleton("Evermore/Database", () => {
const databaseProv = require("./database").default;
return new databaseProv();
});
}

public async boot() {
// All bindings are ready, feel free to use them
}

public async ready() {
// App is ready
}

public async shutdown() {
// Cleanup, since app is going down
}
}
providers/DatabaseProvider/database.ts
import { PrismaClient } from "@prisma/client";

export default class Database {
protected DB: PrismaClient;
constructor() {
this.DB = new PrismaClient();
}
}
import { PrismaClient } from "@prisma/client";

export default class Database {
protected DB: PrismaClient;
constructor() {
this.DB = new PrismaClient();
}
}
5 Replies
Neto
Neto2y ago
did you add into the .adonisrc.json file?
"providers": [
"./providers/AppProvider",
"./providers/MongoProvider",
]
"providers": [
"./providers/AppProvider",
"./providers/MongoProvider",
]
like this
ehhh
ehhhOP2y ago
yup, this is my .adonisrc.json file
{
"typescript": true,
"commands": [
"./commands",
"@adonisjs/core/build/commands/index.js",
"@adonisjs/repl/build/commands"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
"App": "app",
"Config": "config",
"Database": "database",
"Contracts": "contracts"
},
"preloads": [
"./start/routes",
"./start/kernel"
],
"providers": [
"./providers/AppProvider",
"@adonisjs/core",
"./providers/DatabaseProvider"
],
"aceProviders": [
"@adonisjs/repl"
],
"tests": {
"suites": [
{
"name": "functional",
"files": [
"tests/functional/**/*.spec(.ts|.js)"
],
"timeout": 60000
}
]
},
"testProviders": [
"@japa/preset-adonis/TestsProvider"
]
}
{
"typescript": true,
"commands": [
"./commands",
"@adonisjs/core/build/commands/index.js",
"@adonisjs/repl/build/commands"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
"App": "app",
"Config": "config",
"Database": "database",
"Contracts": "contracts"
},
"preloads": [
"./start/routes",
"./start/kernel"
],
"providers": [
"./providers/AppProvider",
"@adonisjs/core",
"./providers/DatabaseProvider"
],
"aceProviders": [
"@adonisjs/repl"
],
"tests": {
"suites": [
{
"name": "functional",
"files": [
"tests/functional/**/*.spec(.ts|.js)"
],
"timeout": 60000
}
]
},
"testProviders": [
"@japa/preset-adonis/TestsProvider"
]
}
ehhh
ehhhOP2y ago
also to be clear this is the error that I got.
Neto
Neto2y ago
try moving out of the folder like /contracts/database.ts
// /contracts/mongoose.ts
declare module '@ioc:Mongoose' {
// Export everything from Mongoose
// Since that's what our provider is doing
export * from 'mongoose';
}

// providers/MongoProvider.ts
import type { ApplicationContract } from '@ioc:Adonis/Core/Application';
import Env from '@ioc:Adonis/Core/Env';
import mongoose from 'mongoose';

export default class MongoProvider {
constructor(protected app: ApplicationContract) {}

public register() {
mongoose.connect(Env.get('MONGO_URI'));

this.app.container.singleton('Mongoose', () => mongoose);
}

public async boot() {
// All bindings are ready, feel free to use them
}

public async ready() {
// App is ready
}

public async shutdown() {
await this.app.container.use('Mongoose').disconnect();
}
}

// any ts file
import { model, Schema } from '@ioc:Mongoose';
// /contracts/mongoose.ts
declare module '@ioc:Mongoose' {
// Export everything from Mongoose
// Since that's what our provider is doing
export * from 'mongoose';
}

// providers/MongoProvider.ts
import type { ApplicationContract } from '@ioc:Adonis/Core/Application';
import Env from '@ioc:Adonis/Core/Env';
import mongoose from 'mongoose';

export default class MongoProvider {
constructor(protected app: ApplicationContract) {}

public register() {
mongoose.connect(Env.get('MONGO_URI'));

this.app.container.singleton('Mongoose', () => mongoose);
}

public async boot() {
// All bindings are ready, feel free to use them
}

public async ready() {
// App is ready
}

public async shutdown() {
await this.app.container.use('Mongoose').disconnect();
}
}

// any ts file
import { model, Schema } from '@ioc:Mongoose';
im using mongoose, but should be the same for prisma
ehhh
ehhhOP2y ago
yup, still the same Hey, umm, I fixed it. I searched through the issues in the Adonis/core repo on GitHub, and it seems that there are some issues when using PNPM (which is what I've been using the whole time), so I switched to Yarn, and it's now working fine! Thank youuu.
Want results from more Discord servers?
Add your server