Skip to content

Commit 287fa03

Browse files
authored
Merge pull request #17 from adityadarma/dev
Add capture log query
2 parents e315286 + 3aa2b13 commit 287fa03

File tree

8 files changed

+91
-78
lines changed

8 files changed

+91
-78
lines changed

.github/workflows/release.yml

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,52 @@ name: Publish Package to npmjs
33
on:
44
push:
55
branches:
6-
- release
6+
- main
7+
8+
permissions:
9+
contents: write
10+
pages: write
711

812
jobs:
9-
build:
13+
test:
1014
runs-on: ubuntu-latest
1115
steps:
1216
- uses: actions/checkout@v4
1317
- name: Install
1418
run: npm install
15-
- name: Run build
16-
run: npm run build
19+
- name: Run lint
20+
run: npm run lint
1721

1822
release:
1923
runs-on: ubuntu-latest
20-
needs: [build]
24+
needs: [test]
2125
permissions:
22-
contents: read
26+
contents: write
2327
id-token: write
2428
steps:
2529
- name: Checkout repository
26-
uses: actions/checkout@v4
30+
uses: actions/checkout@v3
31+
with:
32+
token: ${{ secrets.GITHUB_TOKEN }}
2733
# Setup .npmrc file to publish to npm
2834
- name: Setup Node.JS
2935
uses: actions/setup-node@v4
3036
with:
3137
node-version: '20.x'
3238
registry-url: 'https://registry.npmjs.org'
33-
- name: Install dependencies
34-
run: npm install
35-
- name: Build package
36-
run: npm run build
37-
- name: Publish package
38-
run: npm publish --provenance --access public
39+
- name: git config
40+
run: |
41+
git config user.name "${GITHUB_ACTOR}"
42+
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
43+
- name: Init npm config
44+
run: npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN
3945
env:
40-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
41-
42-
tags:
43-
runs-on: ubuntu-latest
44-
needs: [release]
45-
steps:
46-
- uses: actions/checkout@v4
46+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4747
- name: Install dependencies
4848
run: npm install
49-
- name: Get version from package.json
50-
id: get_version
51-
run: |
52-
VERSION=$(node -p "require('./package.json').version")
53-
echo "VERSION=$VERSION" >> $GITHUB_ENV
54-
- name: Setup GitHub credentials for pushing tags
55-
run: |
56-
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/adityadarma/adonis-datatables.git
57-
git checkout release
58-
- name: Create GitHub tag
59-
run: |
60-
git config user.name "github-actions[bot]"
61-
git config user.email "github-actions[bot]@users.noreply.github.com"
62-
git tag -a "v${{ env.VERSION }}" -m "Release version ${{ env.VERSION }}"
63-
git push origin "v${{ env.VERSION }}"
49+
- name: Publish package
50+
run: npm run release -- --ci
6451
env:
52+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
6553
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66-
# - name: Get merged commits from last tag to current
67-
# id: logs
68-
# run: |
69-
# LAST_TAG=$(git describe --tags --abbrev=0)
70-
# echo "Last tag: $LAST_TAG"
71-
# LOGS=$(git log ${LAST_TAG}..HEAD --oneline)
72-
# echo "LOGS=$LOGS" >> $GITHUB_ENV
73-
74-
# - name: Create GitHub release
75-
# uses: actions/create-release@v1
76-
# with:
77-
# tag_name: "v${{ env.VERSION }}"
78-
# release_name: "Release v${{ env.VERSION }}"
79-
# body: ${{ env.LOGS }}
80-
# draft: false
81-
# prerelease: false
82-
# env:
83-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Test Pipeline
22

33
on:
4-
push:
4+
pull_request:
55
branches:
66
- main
77

.npmrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
package-lock=false
2-
//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}
3-
registry=https://registry.npmjs.org/
4-
always-auth=true

package.json

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"test": "c8 npm run quick:test",
3737
"prebuild": "npm run lint && npm run clean",
3838
"build": "npm run clean && tsc",
39-
"postbuild": "npm run copy:templates",
40-
"release": "np",
39+
"postbuild": "npm run copy:templates && npm run index:commands",
40+
"release": "release-it",
4141
"version": "npm run build",
4242
"prepublishOnly": "npm run build"
4343
},
@@ -48,6 +48,11 @@
4848
],
4949
"author": "Aditya Darma <adhit.boys1@gmail.com>",
5050
"license": "MIT",
51+
"homepage": "https://github.com/adityadarma/adonis-datatables#readme",
52+
"repository": {
53+
"type": "git",
54+
"url": "https://github.com/adityadarma/adonis-datatables"
55+
},
5156
"devDependencies": {
5257
"@adonisjs/assembler": "^7.7.0",
5358
"@adonisjs/core": "^6.12.0",
@@ -57,6 +62,7 @@
5762
"@adonisjs/tsconfig": "^1.3.0",
5863
"@japa/assert": "^3.0.0",
5964
"@japa/runner": "^3.1.4",
65+
"@release-it/conventional-changelog": "^9.0.0",
6066
"@swc/core": "^1.6.3",
6167
"@types/lodash": "^4.17.7",
6268
"@types/node": "^20.14.5",
@@ -68,6 +74,7 @@
6874
"luxon": "^3.4.4",
6975
"np": "^10.0.6",
7076
"prettier": "^3.3.3",
77+
"release-it": "^17.10.0",
7178
"ts-node": "^10.9.2",
7279
"typescript": "^5.4.5"
7380
},
@@ -82,13 +89,32 @@
8289
},
8390
"publishConfig": {
8491
"access": "public",
85-
"tag": "latest"
86-
},
87-
"np": {
88-
"message": "chore(release): %s",
8992
"tag": "latest",
90-
"branch": "main",
91-
"anyBranch": false
93+
"provenance": true
94+
},
95+
"release-it": {
96+
"git": {
97+
"requireCleanWorkingDir": true,
98+
"requireUpstream": true,
99+
"commitMessage": "chore(release): ${version}",
100+
"tagAnnotation": "v${version}",
101+
"push": true,
102+
"tagName": "v${version}"
103+
},
104+
"github": {
105+
"release": false
106+
},
107+
"npm": {
108+
"publish": true,
109+
"skipChecks": true
110+
},
111+
"plugins": {
112+
"@release-it/conventional-changelog": {
113+
"preset": {
114+
"name": "angular"
115+
}
116+
}
117+
}
92118
},
93119
"c8": {
94120
"reporter": [
@@ -102,9 +128,5 @@
102128
"eslintConfig": {
103129
"extends": "@adonisjs/eslint-config/package"
104130
},
105-
"prettier": "@adonisjs/prettier-config",
106-
"repository": {
107-
"type": "git",
108-
"url": "https://github.com/adityadarma/adonis-datatables"
109-
}
131+
"prettier": "@adonisjs/prettier-config"
110132
}

providers/datatables_provider.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
import type { ApplicationService } from '@adonisjs/core/types'
22
import Datatables from '../src/datatables.js'
3+
import { DbQueryEventNode } from '@adonisjs/lucid/types/database'
4+
5+
declare module '@adonisjs/core/types' {
6+
export interface EventsList {
7+
'db:query': DbQueryEventNode
8+
}
9+
}
310

411
export default class DatatablesProvider {
512
constructor(protected app: ApplicationService) {}
613

714
/**
815
* Register bindings to the container
916
*/
10-
register() {}
11-
12-
/**
13-
* The container bindings have booted
14-
*/
15-
async boot() {
17+
register() {
1618
this.app.container.bind('datatables', () => {
1719
const engines: Record<string, any> = this.app.config.get(`datatables.engines`)
1820

1921
return new Datatables(engines)
2022
})
2123
}
2224

25+
/**
26+
* The container bindings have booted
27+
*/
28+
async boot() {}
29+
2330
/**
2431
* The application has been booted
2532
*/

src/datatable_abstract.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import DataProcessor from './processor.js'
77
import Helper from './utils/helper.js'
88
import { DataTable } from './types/index.js'
99
import app from '@adonisjs/core/services/app'
10+
import emitter from '@adonisjs/core/services/emitter'
1011

1112
export abstract class DataTableAbstract implements DataTable {
1213
protected ctx!: HttpContext
@@ -55,8 +56,18 @@ export abstract class DataTableAbstract implements DataTable {
5556

5657
protected $dataObject: boolean = true
5758

59+
protected $queryLogging: Record<string, any>[] = []
60+
5861
constructor() {
5962
this.config = new Config(app.config.get('datatables'))
63+
64+
emitter.on('db:query', (query) => {
65+
this.$queryLogging.push({
66+
query: query.sql,
67+
bindings: query.bindings,
68+
time: `${query.duration}ms`,
69+
})
70+
})
6071
}
6172

6273
static canCreate(_source: any) {
@@ -198,6 +209,8 @@ export abstract class DataTableAbstract implements DataTable {
198209
for (const [key, value] of this.config.jsonHeaders().entries()) {
199210
response.append(key, value)
200211
}
212+
213+
emitter.clearListeners('db:query')
201214
return response.json(output)
202215
}
203216

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

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

211225
return output
212226
}
213227

214228
protected errorResponse(exception: Exception) {
229+
emitter.clearListeners('db:query')
230+
215231
if (!this.ctx) {
216232
return
217233
}

src/datatables.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import ObjectDataTable from './engines/object_datatable.js'
55
import { Collection } from 'collect.js'
66
import { DatabaseQueryBuilderContract, Dictionary } from '@adonisjs/lucid/types/querybuilder'
77
import { LucidModel, ModelQueryBuilderContract } from '@adonisjs/lucid/types/model'
8+
import { DataTableAbstract } from './datatable_abstract.js'
89

910
export default class Datatables {
1011
constructor(protected engines: Record<string, any>) {}
1112

12-
of<T extends any>(...source: any): T {
13+
of<T extends DataTableAbstract>(...source: any): T {
1314
for (const engine of Object.values(this.engines)) {
1415
const canCreate = engine.canCreate as Function
1516

src/engines/database_datatable.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ export default class DatabaseDataTable extends DataTableAbstract {
327327
}
328328

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

0 commit comments

Comments
 (0)