Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error in group decorator #130

Closed
makaias opened this issue Sep 12, 2023 · 6 comments
Closed

Type error in group decorator #130

makaias opened this issue Sep 12, 2023 · 6 comments

Comments

@makaias
Copy link

makaias commented Sep 12, 2023

Whenever I try to create a group which uses decorator for database access the following problem happens:
The group is working as intended as long as I don't need to access the database.
When it comes to the database access, the instance (in this example the BooksDatabase instance) produces the following error:
TypeError: db.getBooks is not a function. (In 'db.getBooks()', 'db.getBooks' is undefined)

The error only happens in groups, when I try the same with a db decorator on the main Elysia instance it works without any issues.
Is there any workaround for this, or any step I missed?
Thanks for the help! :)

Demo code for the issue:

import { swagger } from "@elysiajs/swagger";
import Database from "bun:sqlite";
import { Elysia, t } from "elysia";

interface Book {
  id?: number;
  name: string;
  author: string;
}

class BooksDatabase {
  private db: Database;

  constructor() {
    this.db = new Database("books.db");
    this.init()
      .then(() => console.log("Database initialized"))
      .catch(console.error);
  }

  async getBooks() {
    return this.db.query("SELECT * FROM books").all();
  }

  async init() {
    return this.db.run(
      "CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, author TEXT)"
    );
  }
}

const bookService = new Elysia({ prefix: "/books" })
  .model({
    book: t.Object({
      name: t.String(),
      author: t.String(),
    }),
  })
  .decorate("db", new BooksDatabase())
  .get("/", async ({ db }) => await getAllBooks(db));

const getAllBooks = async (db: BooksDatabase) => {
  try {
    const books = await db.getBooks();
    return { success: true, books };
  } catch (e) {
    console.log(e);
    return { success: false };
  }
};

const app = new Elysia()
  .model({
    book: t.Object({
      name: t.String(),
      author: t.String(),
    }),
  })
  .use(swagger())
  .group("/v1", (app) => app.use(bookService))
  .listen(8080);

package.json:

  "name": "elysiaTest",
  "version": "1.0.50",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "bun run --watch src/index.ts"
  },
  "dependencies": {
    "@elysiajs/cookie": "^0.6.1",
    "@elysiajs/cors": "^0.6.0",
    "@elysiajs/swagger": "^0.6.2",
    "elysia": "latest"
  },
  "devDependencies": {
    "bun-types": "latest"
  },
  "module": "src/index.js"
}```
 
@gmenih
Copy link

gmenih commented Sep 20, 2023

This is the same issue as #143

@SaltyAom
Copy link
Member

Should be fixed in #103.

Let me know if the problem stills persists

@mathipalm
Copy link

Tested with example above on "elysia": "^0.7.1" and i still have the same issue @SaltyAom

@SaltyAom
Copy link
Member

SaltyAom commented Sep 20, 2023

Can't replicate with above code on my end, maybe Bun doesn't update the cache?

This is specifically resolved in 0.7.1, can you try running:

rm -rf node_modules && rm bun.lockb && bun i

And see if the problem still persists.

If so, can you go into the node_modules/elysia/package.json and see if the version is correctly set to 0.7.1

@mathipalm
Copy link

mathipalm commented Sep 20, 2023

@SaltyAom my bad works now!

@makaias
Copy link
Author

makaias commented Sep 23, 2023

@SaltyAom it works now, thank you!

@makaias makaias closed this as completed Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants