Skip to content

Add capture log query #17

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

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 24 additions & 53 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Test Pipeline

on:
push:
pull_request:
branches:
- main

Expand Down
3 changes: 0 additions & 3 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
package-lock=false
//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/
always-auth=true
48 changes: 35 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -48,6 +48,11 @@
],
"author": "Aditya Darma <adhit.boys1@gmail.com>",
"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",
Expand All @@ -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",
Expand All @@ -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"
},
Expand All @@ -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": [
Expand All @@ -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"
}
19 changes: 13 additions & 6 deletions providers/datatables_provider.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
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) {}

/**
* Register bindings to the container
*/
register() {}

/**
* The container bindings have booted
*/
async boot() {
register() {
this.app.container.bind('datatables', () => {
const engines: Record<string, any> = this.app.config.get(`datatables.engines`)

return new Datatables(engines)
})
}

/**
* The container bindings have booted
*/
async boot() {}

/**
* The application has been booted
*/
Expand Down
16 changes: 16 additions & 0 deletions src/datatable_abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,8 +56,18 @@ export abstract class DataTableAbstract implements DataTable {

protected $dataObject: boolean = true

protected $queryLogging: Record<string, any>[] = []

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) {
Expand Down Expand Up @@ -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)
}

Expand All @@ -206,12 +219,15 @@ export abstract class DataTableAbstract implements DataTable {
}

protected showDebugger(output: Record<string, any>): Record<string, any> {
output['queries'] = this.$queryLogging
output['input'] = this.request.all()

return output
}

protected errorResponse(exception: Exception) {
emitter.clearListeners('db:query')

if (!this.ctx) {
return
}
Expand Down
3 changes: 2 additions & 1 deletion src/datatables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>) {}

of<T extends any>(...source: any): T {
of<T extends DataTableAbstract>(...source: any): T {
for (const engine of Object.values(this.engines)) {
const canCreate = engine.canCreate as Function

Expand Down
1 change: 0 additions & 1 deletion src/engines/database_datatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ export default class DatabaseDataTable extends DataTableAbstract {
}

async dataResults(): Promise<Record<string, any>[]> {
console.log(this.query.toQuery())
return await this.query
}

Expand Down
Loading