From 5707b9ed835eb59b6afb172b86c324d20df7ac71 Mon Sep 17 00:00:00 2001 From: Aditya Darma Date: Fri, 18 Oct 2024 14:45:54 +0800 Subject: [PATCH 1/2] feat: add capture data query --- providers/datatables_provider.ts | 19 +++++++++++++------ src/datatable_abstract.ts | 16 ++++++++++++++++ src/datatables.ts | 3 ++- src/engines/database_datatable.ts | 1 - 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/providers/datatables_provider.ts b/providers/datatables_provider.ts index a830495..3c8fb72 100644 --- a/providers/datatables_provider.ts +++ b/providers/datatables_provider.ts @@ -1,5 +1,12 @@ import type { ApplicationService } from '@adonisjs/core/types' import Datatables from '../src/datatables.js' +import { DbQueryEventNode } from '@adonisjs/lucid/types/database' + +declare module '@adonisjs/core/types' { + export interface EventsList { + 'db:query': DbQueryEventNode + } +} export default class DatatablesProvider { constructor(protected app: ApplicationService) {} @@ -7,12 +14,7 @@ export default class DatatablesProvider { /** * Register bindings to the container */ - register() {} - - /** - * The container bindings have booted - */ - async boot() { + register() { this.app.container.bind('datatables', () => { const engines: Record = this.app.config.get(`datatables.engines`) @@ -20,6 +22,11 @@ export default class DatatablesProvider { }) } + /** + * The container bindings have booted + */ + async boot() {} + /** * The application has been booted */ diff --git a/src/datatable_abstract.ts b/src/datatable_abstract.ts index f0adee7..d3611a6 100644 --- a/src/datatable_abstract.ts +++ b/src/datatable_abstract.ts @@ -7,6 +7,7 @@ import DataProcessor from './processor.js' import Helper from './utils/helper.js' import { DataTable } from './types/index.js' import app from '@adonisjs/core/services/app' +import emitter from '@adonisjs/core/services/emitter' export abstract class DataTableAbstract implements DataTable { protected ctx!: HttpContext @@ -55,8 +56,18 @@ export abstract class DataTableAbstract implements DataTable { protected $dataObject: boolean = true + protected $queryLogging: Record[] = [] + constructor() { this.config = new Config(app.config.get('datatables')) + + emitter.on('db:query', (query) => { + this.$queryLogging.push({ + query: query.sql, + bindings: query.bindings, + time: `${query.duration}ms`, + }) + }) } static canCreate(_source: any) { @@ -198,6 +209,8 @@ export abstract class DataTableAbstract implements DataTable { for (const [key, value] of this.config.jsonHeaders().entries()) { response.append(key, value) } + + emitter.clearListeners('db:query') return response.json(output) } @@ -206,12 +219,15 @@ export abstract class DataTableAbstract implements DataTable { } protected showDebugger(output: Record): Record { + output['queries'] = this.$queryLogging output['input'] = this.request.all() return output } protected errorResponse(exception: Exception) { + emitter.clearListeners('db:query') + if (!this.ctx) { return } diff --git a/src/datatables.ts b/src/datatables.ts index 3072746..f2811f9 100644 --- a/src/datatables.ts +++ b/src/datatables.ts @@ -5,11 +5,12 @@ import ObjectDataTable from './engines/object_datatable.js' import { Collection } from 'collect.js' import { DatabaseQueryBuilderContract, Dictionary } from '@adonisjs/lucid/types/querybuilder' import { LucidModel, ModelQueryBuilderContract } from '@adonisjs/lucid/types/model' +import { DataTableAbstract } from './datatable_abstract.js' export default class Datatables { constructor(protected engines: Record) {} - of(...source: any): T { + of(...source: any): T { for (const engine of Object.values(this.engines)) { const canCreate = engine.canCreate as Function diff --git a/src/engines/database_datatable.ts b/src/engines/database_datatable.ts index 6198f75..f93c9d7 100644 --- a/src/engines/database_datatable.ts +++ b/src/engines/database_datatable.ts @@ -327,7 +327,6 @@ export default class DatabaseDataTable extends DataTableAbstract { } async dataResults(): Promise[]> { - console.log(this.query.toQuery()) return await this.query } From 3aa2b13647b574fa7c952ad05f2b62947fd27da2 Mon Sep 17 00:00:00 2001 From: Aditya Darma Date: Fri, 18 Oct 2024 14:54:09 +0800 Subject: [PATCH 2/2] ci: update workflow --- .github/workflows/release.yml | 77 +++++++++++------------------------ .github/workflows/test.yml | 2 +- .npmrc | 3 -- package.json | 48 ++++++++++++++++------ 4 files changed, 60 insertions(+), 70 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 33f762c..c87d3fe 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,81 +3,52 @@ name: Publish Package to npmjs on: push: branches: - - release + - main + +permissions: + contents: write + pages: write jobs: - build: + test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Install run: npm install - - name: Run build - run: npm run build + - name: Run lint + run: npm run lint release: runs-on: ubuntu-latest - needs: [build] + needs: [test] permissions: - contents: read + contents: write id-token: write steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} # Setup .npmrc file to publish to npm - name: Setup Node.JS uses: actions/setup-node@v4 with: node-version: '20.x' registry-url: 'https://registry.npmjs.org' - - name: Install dependencies - run: npm install - - name: Build package - run: npm run build - - name: Publish package - run: npm publish --provenance --access public + - name: git config + run: | + git config user.name "${GITHUB_ACTOR}" + git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" + - name: Init npm config + run: npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - - tags: - runs-on: ubuntu-latest - needs: [release] - steps: - - uses: actions/checkout@v4 + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - name: Install dependencies run: npm install - - name: Get version from package.json - id: get_version - run: | - VERSION=$(node -p "require('./package.json').version") - echo "VERSION=$VERSION" >> $GITHUB_ENV - - name: Setup GitHub credentials for pushing tags - run: | - git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/adityadarma/adonis-datatables.git - git checkout release - - name: Create GitHub tag - run: | - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - git tag -a "v${{ env.VERSION }}" -m "Release version ${{ env.VERSION }}" - git push origin "v${{ env.VERSION }}" + - name: Publish package + run: npm run release -- --ci env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # - name: Get merged commits from last tag to current - # id: logs - # run: | - # LAST_TAG=$(git describe --tags --abbrev=0) - # echo "Last tag: $LAST_TAG" - # LOGS=$(git log ${LAST_TAG}..HEAD --oneline) - # echo "LOGS=$LOGS" >> $GITHUB_ENV - - # - name: Create GitHub release - # uses: actions/create-release@v1 - # with: - # tag_name: "v${{ env.VERSION }}" - # release_name: "Release v${{ env.VERSION }}" - # body: ${{ env.LOGS }} - # draft: false - # prerelease: false - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 62cbd21..d61b5e3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,7 @@ name: Test Pipeline on: - push: + pull_request: branches: - main diff --git a/.npmrc b/.npmrc index eb71b4c..43c97e7 100644 --- a/.npmrc +++ b/.npmrc @@ -1,4 +1 @@ package-lock=false -//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN} -registry=https://registry.npmjs.org/ -always-auth=true diff --git a/package.json b/package.json index f03d945..d20aaef 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,8 @@ "test": "c8 npm run quick:test", "prebuild": "npm run lint && npm run clean", "build": "npm run clean && tsc", - "postbuild": "npm run copy:templates", - "release": "np", + "postbuild": "npm run copy:templates && npm run index:commands", + "release": "release-it", "version": "npm run build", "prepublishOnly": "npm run build" }, @@ -48,6 +48,11 @@ ], "author": "Aditya Darma ", "license": "MIT", + "homepage": "https://github.com/adityadarma/adonis-datatables#readme", + "repository": { + "type": "git", + "url": "https://github.com/adityadarma/adonis-datatables" + }, "devDependencies": { "@adonisjs/assembler": "^7.7.0", "@adonisjs/core": "^6.12.0", @@ -57,6 +62,7 @@ "@adonisjs/tsconfig": "^1.3.0", "@japa/assert": "^3.0.0", "@japa/runner": "^3.1.4", + "@release-it/conventional-changelog": "^9.0.0", "@swc/core": "^1.6.3", "@types/lodash": "^4.17.7", "@types/node": "^20.14.5", @@ -68,6 +74,7 @@ "luxon": "^3.4.4", "np": "^10.0.6", "prettier": "^3.3.3", + "release-it": "^17.10.0", "ts-node": "^10.9.2", "typescript": "^5.4.5" }, @@ -82,13 +89,32 @@ }, "publishConfig": { "access": "public", - "tag": "latest" - }, - "np": { - "message": "chore(release): %s", "tag": "latest", - "branch": "main", - "anyBranch": false + "provenance": true + }, + "release-it": { + "git": { + "requireCleanWorkingDir": true, + "requireUpstream": true, + "commitMessage": "chore(release): ${version}", + "tagAnnotation": "v${version}", + "push": true, + "tagName": "v${version}" + }, + "github": { + "release": false + }, + "npm": { + "publish": true, + "skipChecks": true + }, + "plugins": { + "@release-it/conventional-changelog": { + "preset": { + "name": "angular" + } + } + } }, "c8": { "reporter": [ @@ -102,9 +128,5 @@ "eslintConfig": { "extends": "@adonisjs/eslint-config/package" }, - "prettier": "@adonisjs/prettier-config", - "repository": { - "type": "git", - "url": "https://github.com/adityadarma/adonis-datatables" - } + "prettier": "@adonisjs/prettier-config" }