2.4.0 • Published 1 year ago

@lu7766lu7766/adonis-repository v2.4.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Adonis Repository

adonisjs lucid is very strong, but it do too much and hard to extend, so i refer nestJs's repository, and build this to share model responsibility

📦 Installing

Simply run the following commands on your shell

npm install @lu7766lu7766/adonis-repository
node ace invoke @lu7766lu7766/adonis-repository

📌 Example

Inject in constructor

// Controller.ts
import { inject } from "@adonisjs/fold"
import { InjectRepository, Repository } from "@ioc:Adonis/Repository"
import User from "App/Models/User"
@inject()
class Controller {
  constructor(@InjectRepository(User) private repo: Repository<typeof User>) {}
}

Inject in class

// Controller.ts
import { InjectRepository, Repository } from "@ioc:Adonis/Repository"
import User from "App/Models/User"
class Controller {
  @InjectRepository(User)
  private repo: Repository<typeof User>
}

features

// Controller.ts
import { inject } from "@adonisjs/fold"
import { InjectRepository, Repository } from "@ioc:Adonis/Repository"
import User from "App/Models/User"
@inject()
class Controller {
  constructor(@InjectRepository(User) private repo: Repository<typeof User>) {}

  getList() {
    return this.repo
      .query()
      .pager({ page: 1; perPage: 20 })
      .sort({ sortKey: "created_ad"; sortType: "desc" })
      .condiction({
        id: [1, 3, 5, 7], // whereIn("id", [1, 3, 5, 7])
        is_active: true, // where("is_active", true)
        subQuery(query) {
          // custom query
        },
      })
  }

  getTotal() {
    return this.repo.getTotal() // select count(*) from users // reutrn number
  }

  getOne() {
    return this.repo.find(5) // select * from users where id = 5
  }

  query() {
    return this.repo.query() // same as User.query
  }

  // findFail
  // findBy
  // findByOrFail
  // firstOrCreate
  // create
  // createMany
  // updateOrCreate
  // updateOrCreateManyByKey
  // merge
  // save
  // mergeSave
  // delete
  // deleteBy
}

📝 Contributing

If you find any issue, bug or missing feature, please kindly create an issue or submit a pull request.

2.4.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.0

1 year ago

2.2.0

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.2.0

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago