Giter Site home page Giter Site logo

athennaio / database Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 1.87 MB

๐Ÿ›ข๏ธ The Athenna database handler for SQL/NoSQL.

Home Page: https://athenna.io

License: MIT License

TypeScript 99.91% Edge 0.09%
athenna database factories migrations mongodb mysql nodejs nosql orm postgresql

database's Introduction

Database ๐Ÿ›ข๏ธ

The Athenna database handler for SQL/NoSQL.

GitHub followers GitHub stars

Buy Me A Coffee

GitHub language count Repository size License Commitizen

Links

For project documentation click here. If something is not clear in the documentation please open an issue in the documentation repository

Contributing

If you want to contribute to this project, first read the CONTRIBUTING.MD file. It will be a pleasure to receive your help.


With ๐Ÿ’œ by Athenna community

database's People

Contributors

jlenon7 avatar snyk-bot avatar txsoura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

database's Issues

[Feature]: Remove DB_AUTO_CONNECT logic and auto connect the database as operations are called

๐Ÿš€ Feature Proposal

It would be very nice if we could remove the DB_AUTO_CONNECT env variable logic of the DatabaseProvider and auto connect the database driver when some operation is called.

Motivation

This would help a lot in the performance of Artisan commands that does not use the database. Also, when the connection with database is not working, Artisan tries to auto connect to database because of DB_AUTO_CONNECT logic and fails.

Example

The database would be connected this way:

DB.connection('postgres').isConnected() // false

await DB.connection('postgres')
    .table('users')
    .select('id')
    .where('id', 1)
    .findMany() // [...]

DB.connection('postgres').isConnected() // true

[Feature]: Add force flag in commands

๐Ÿš€ Feature Proposal

Commands that affect saved data, should be ignored in the production environment if it has no force flag.

The run/revert migration commands and database wipe command, should not be easily run in production, these commands could be found at the above paths of the project:

Motivation

To prevent production data lost.

Example

If NODE_ENV=production, the above command should throw an exception with an explanation to the developer that he needs to use --force flag to run this command in production:

node artisan migration:run 

If NODE_ENV=production, the above command should work as expected:

node artisan migration:run --force

[Feature]: Implement `make:crud` command

๐Ÿš€ Feature Proposal

This command will be responsible to create an entire CRUD and implement the following:

  • controller
  • service
  • migration
  • model

Points to keep in mind:

Athenna needs to validate if @athenna/http exists to be able to create the controller, otherwise skip only the controller creation.

When running node artisan make:crud, Athenna needs to keep prompting the user
the columns and relations that he wants to add to his model, this data will be used
to craft all the templates.

Motivation

Make it easy to create a full CRUD on Athenna.

Example

No response

[Feature]: Implement `@Before...()` and `@After...()` annotations

๐Ÿš€ Feature Proposal

Implement the following annotations to be used in models:

  • @BeforeCreate()
  • @AfterCreate()
  • @BeforeUpdate()
  • @AfterUpdate()
  • @BeforeSave()
  • @AfterSave()
  • @BeforeDelete()
  • @AfterDelete()
  • @BeforeFind()
  • @AfterFind()

Motivation

Give more control to the developer over database operations.

Example

No response

[Feature]: Implement when method

๐Ÿš€ Feature Proposal

Implement when method in QueryBuilder class.

Motivation

Sometimes you may want certain query clauses to apply to a query based on another condition. For instance, you may only want to apply a where statement if a given input value is present on the incoming HTTP request. You may accomplish this using when method.

The when method only executes the given closure when the first argument is true. If the first argument is false, the closure will not be executed. So, in the example above, the closure given to when method will only be invoked if the role field is present on the incoming request and evaluates to true.

Example

const role = request.payload('role')

await DB
    .table('users')
    .when(role, (query, role) => query.where('roleId', role))
    .findMany()

[Feature]: Implement events to be called before and after database operations

๐Ÿš€ Feature Proposal

Implement events to be called before and after database operations.

Motivation

These events are going to be very useful for making modifications in the payload before or after some database operations. We can have for example these:

  • beforeCreate
  • afterCreate
  • beforeUpdate
  • afterUpdate
  • beforeDelete
  • afterDelete
  • beforeSave
  • afterSave
  • beforeFind
  • afterFind

All of before... methods should receive all the arguments used to call create, createMany, etc. This way, the developer can make changes in the payloads before executing his operation. If any of before... methods return null value, the database operation must be canceled.

All of after... methods should receive all the data returned by the database operation. This way, the developer can make changes in the return payloads before create and createMany return the value.

Example

import { hash } from 'bcryptjs'
import { Is } from '@athenna/common'
import { Model } from '@athenna/database'

export class Flight extends Model {
  /**
   * Execute everytime before creating some value in database.
   *
   * @return {Promise<any>}
   */
  static async beforeCreate(data) {
    const executor = async (data) => {
       data.password = await hash(data.password)
    }

    if (Is.Array(data)) {
      data = await Promise.all(data.map(d => executor(d)))

      return
    }

    await executor(data)
  }
}

[Refactor]: Use Edge template engine instead of EJS in commands

๐Ÿš€ Feature Proposal

Use Edge template engine to create artisan "make" commands instead of EJS.

Motivation

The @athenna/mail package will use Edge template engine to write email templates. To keep the same pattern and behavior from @athenna/mail we are going to use the Edge template engine in Artisan make commands.

Example

No response

[Bug]: Configure database commands in configurer

Version

4.1.0

Steps to reproduce

Run node artisan configure @athenna/database

Expected behavior

I expect to see all database commands configured in .athennarc.json

Actual behavior

Commands like make:model are not configured in .athennarc.json.

Additional context

No response

Environment

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1 Pro
    Memory: 3.30 GB / 16.00 GB
    Shell: 5.9 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 21.1.0 - ~/.nvm/versions/node/v21.1.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v21.1.0/bin/yarn
    npm: 10.2.0 - ~/.nvm/versions/node/v21.1.0/bin/npm
    bun: 1.0.7 - ~/.bun/bin/bun

[Feature]: Update version to 3.0.0

๐Ÿš€ Feature Proposal

The version of this package needs to be updated to 3.0.0.

Motivation

We are going to start versioning the Athenna packages using Semver. All packages of Athenna will be updated to version 3.0.0 to start following Semver rules.

Example

No response

[Bug]: Mongoose and Knex installed for the same project

Version

1.4.6

Steps to reproduce

Just use the @athenna/cli to run the athenna install:database command.

Expected behavior

I expect that when I want to use the @athenna/database to access only SQL databases, that Mongoose dependency is not installed. Also, I want to manage my own version of Mongoose and Knex in my project.

Actual behavior

Mongoose and Knex are installed, no mater which type of database he selects using the install:database command.

Additional context

No response

Environment

System:
    OS: Linux 5.14 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
    Memory: 6.19 GB / 15.03 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm

[Feature]: Implement latest and oldest methods

๐Ÿš€ Feature Proposal

Implement latest and oldest methods in QueryBuilder class.

Motivation

The latest and oldest methods will allow the developer to easily order results by date. By default, the result will be ordered by the table's createdAt column. Or, he may pass the column name that you wish to sort by.

Example

await Database
    .table('users')
    .latest()
    .findMany()

await Database
    .table('users')
    .oldest()
    .findMany()

await Database
    .table('users')
    .latest('deletedAt')
    .findMany()

[Feature]: Implement the static getter attributes in models

๐Ÿš€ Feature Proposal

Implement the static getter attributes in models.

Motivation

This getter will be responsible for setting default attributes for your model. He needs to be called in create, createMany and createOrUpdate methods before executing them.

If the value has been already set in the method signature, then we cannot override it using the attributes. These properties should only be used when the value does not exist in the payloads.

Example

Implementation of attributes in the model:

import { Uuid } from '@athenna/common'
import { Model } from '@athenna/database'

export class Flight extends Model {
  /**
   * Set the default attributes of your model.
   *
   * @return {any}
   */
  static get attributes() {
      return {
          id: Uuid.generate()
      }
  }

  /*...*/
}

Usage of attributes:

import { Flight } from '#app/Models/Flight'

const flight = await Flight.create()

console.log(flight.id) // -> 4b68c460-60f5-11ed-9b6a-0242ac120002

attributes is implemented, but the developer is setting his own property in the method signature:

import { Flight } from '#app/Models/Flight'

// Setting my own id attribute
const flight = await Flight.create({ id: '299dabf8-60f4-11ed-9b6a-0242ac120002' })

console.log(flight.id) // -> 299dabf8-60f4-11ed-9b6a-0242ac120002

[Bug]: When URL got protocol undefined application exits without logs in MongoDriver

Version

3.0.9

Steps to reproduce

Set protocol as undefined and try to connect in a Mongo database.

Expected behavior

I expect to see some error of what happened.

Actual behavior

The application exists without any information, for some reason, process.on('uncaughtException') and the global try catch of @athenna/core` are not catching the exception.

Additional context

No response

Environment

System:
    OS: Linux 5.14 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
    Memory: 2.34 GB / 15.03 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm

[Feature]: Implement MongoDB Driver

๐Ÿš€ Feature Proposal

Implementation of MongoDB Driver for database package.

Motivation

Meet the needs of developers using the MongoDB database.

Example

No response

[Feature]: Implement "model:show" command

๐Ÿš€ Feature Proposal

Captura de Tela 2022-07-30 aฬ€s 12 17 31

Motivation

This command will add some useful information about the state of a database model of an Athenna application.

Example

node artisan model:show

[Refactor]: Use Knex query builder

๐Ÿš€ Feature Proposal

Refactor database package to use Knex query builder to create our own ORM.

Motivation

We are having some limitations with TypeORM. Because of that, we are going to use Knex query builder to create our own ORM and implement this limitations.

Example

No response

[Feature]: Implement SQLite Driver

๐Ÿš€ Feature Proposal

Implementation of SQLite Driver for database package.

Motivation

Meet the needs of developers using the SQLite database.

Example

No response

[Bug]: Data that is going to be created or updated does not have a default value

Version

3.0.10

Steps to reproduce

await User.create() or await User.create(undefined)

Expected behavior

I expect that a default value is set because I could use the default values set inside the model.

Actual behavior

An error is throw when trying to verify the primary key of the data.

Additional context

No response

Environment

System:
    OS: macOS 13.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 306.48 MB / 16.00 GB
    Shell: 5.8 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm

[Bug]: Athenna packages being used as dependency

Version

1.5.0

Steps to reproduce

Same as issue #44 of @athenna/logger but with multiple modules:

  • @athenna/ioc
  • @athenna/config
  • @athenna/logger
  • @athenna/artisan
  • @athenna/common

Expected behavior

I expect that when I install the version 1.0.0 of the packages above, that @athenna/database package uses the same version.

Actual behavior

Two versions of the packages are installed, specifics for @athenna/database and the other for the project. This means that I can't choose which version of the packages I want to use globally in the project.

Additional context

No response

Environment

System:
    OS: Linux 5.14 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
    Memory: 6.19 GB / 15.03 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.