@hozon/store-log 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/api.d.ts +1 -1
- package/lib/api.js +7 -7
- package/lib/migrations.d.ts +1 -2
- package/lib/migrations.js +6 -6
- package/lib/tables.d.ts +2 -2
- package/package.json +4 -4
package/README.md
CHANGED
package/lib/api.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Adapter } from '@hozon/adapter';
|
|
2
|
-
import type { Kysely } from '
|
|
2
|
+
import type { Kysely } from '@hozon/db';
|
|
3
3
|
import type { LogTables } from './tables.js';
|
|
4
4
|
import type { LogStore } from './types.js';
|
|
5
5
|
export declare function createLogStoreAPI(db: Kysely<LogTables>, adapter: Adapter): LogStore;
|
package/lib/api.js
CHANGED
|
@@ -39,14 +39,14 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
39
39
|
});
|
|
40
40
|
await withStoreTransaction(db, async (trx)=>{
|
|
41
41
|
for (const batch of chunk(rows, 71)){
|
|
42
|
-
await trx.insertInto('
|
|
42
|
+
await trx.insertInto('logs').values(batch).execute();
|
|
43
43
|
}
|
|
44
44
|
});
|
|
45
45
|
},
|
|
46
46
|
async queryLogs (params) {
|
|
47
47
|
if (!Number.isInteger(params.limit) || params.limit <= 0) throw new RangeError('Log limit must be a positive integer');
|
|
48
48
|
const limit = Math.min(params.limit, 1000);
|
|
49
|
-
let query = db.selectFrom('
|
|
49
|
+
let query = db.selectFrom('logs').select([
|
|
50
50
|
'timestamp',
|
|
51
51
|
'seq',
|
|
52
52
|
'data'
|
|
@@ -87,7 +87,7 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
87
87
|
};
|
|
88
88
|
},
|
|
89
89
|
async getTraceLogs (traceID) {
|
|
90
|
-
const rows = await db.selectFrom('
|
|
90
|
+
const rows = await db.selectFrom('logs').select('data').where('trace_id', '=', traceID).orderBy('timestamp').orderBy('seq').execute();
|
|
91
91
|
// addLogs enforces paired IDs for every traced row.
|
|
92
92
|
return rows.map((row)=>row.data);
|
|
93
93
|
},
|
|
@@ -96,7 +96,7 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
96
96
|
return withStoreTransaction(db, async (trx)=>{
|
|
97
97
|
let count = 0;
|
|
98
98
|
for (const ids of chunk(traceIDs)){
|
|
99
|
-
const result = await trx.deleteFrom('
|
|
99
|
+
const result = await trx.deleteFrom('logs').where('trace_id', 'in', ids).executeTakeFirstOrThrow();
|
|
100
100
|
count += Number(result.numDeletedRows);
|
|
101
101
|
}
|
|
102
102
|
return count;
|
|
@@ -105,14 +105,14 @@ export function createLogStoreAPI(db, adapter) {
|
|
|
105
105
|
async deleteBefore (time, params) {
|
|
106
106
|
return withStoreTransaction(db, async (trx)=>{
|
|
107
107
|
if (params?.keepTraceIDs === undefined || params.keepTraceIDs.length === 0) {
|
|
108
|
-
const result = await trx.deleteFrom('
|
|
108
|
+
const result = await trx.deleteFrom('logs').where('timestamp', '<', time).executeTakeFirstOrThrow();
|
|
109
109
|
return Number(result.numDeletedRows);
|
|
110
110
|
}
|
|
111
111
|
return withKeepSet(trx, {
|
|
112
|
-
table: '
|
|
112
|
+
table: 'keep_log',
|
|
113
113
|
ids: params.keepTraceIDs
|
|
114
114
|
}, async (selectKeep)=>{
|
|
115
|
-
const result = await trx.deleteFrom('
|
|
115
|
+
const result = await trx.deleteFrom('logs').where('timestamp', '<', time).where((eb)=>eb.or([
|
|
116
116
|
eb('trace_id', 'is', null),
|
|
117
117
|
eb('trace_id', 'not in', selectKeep())
|
|
118
118
|
])).executeTakeFirstOrThrow();
|
package/lib/migrations.d.ts
CHANGED
package/lib/migrations.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { sql } from '
|
|
1
|
+
import { sql } from '@hozon/db';
|
|
2
2
|
export function logStoreMigrations(ctx) {
|
|
3
3
|
return {
|
|
4
4
|
'0-init': {
|
|
5
5
|
async up (db) {
|
|
6
|
-
await db.schema.createTable('
|
|
6
|
+
await db.schema.createTable('logs').addColumn('seq', ctx.types.serial, (column)=>{
|
|
7
7
|
const primary = column.primaryKey();
|
|
8
8
|
return ctx.kind === 'sqlite' ? primary.autoIncrement() : primary;
|
|
9
9
|
}).addColumn('timestamp', ctx.types.double, (column)=>column.notNull()).addColumn('level', ctx.types.text, (column)=>column.notNull()).addColumn('category', ctx.types.text, (column)=>{
|
|
10
10
|
const category = column.notNull();
|
|
11
11
|
return ctx.kind === 'postgres' ? category.modifyFront(sql`collate "C"`) : category;
|
|
12
12
|
}).addColumn('trace_id', ctx.types.text).addColumn('span_id', ctx.types.text).addColumn('data', ctx.types.json, (column)=>column.notNull()).execute();
|
|
13
|
-
await db.schema.createIndex(
|
|
14
|
-
await db.schema.createIndex(
|
|
13
|
+
await db.schema.createIndex(`${ctx.tablePrefix}_logs_timestamp`).on('logs').column('timestamp').execute();
|
|
14
|
+
await db.schema.createIndex(`${ctx.tablePrefix}_logs_trace_timestamp_seq`).on('logs').columns([
|
|
15
15
|
'trace_id',
|
|
16
16
|
'timestamp',
|
|
17
17
|
'seq'
|
|
18
18
|
]).execute();
|
|
19
|
-
await db.schema.createIndex(
|
|
19
|
+
await db.schema.createIndex(`${ctx.tablePrefix}_logs_level_timestamp`).on('logs').columns([
|
|
20
20
|
'level',
|
|
21
21
|
'timestamp'
|
|
22
22
|
]).execute();
|
|
23
23
|
},
|
|
24
24
|
async down (db) {
|
|
25
|
-
await db.schema.dropTable('
|
|
25
|
+
await db.schema.dropTable('logs').execute();
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
};
|
package/lib/tables.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ColumnType, Generated } from '
|
|
1
|
+
import type { ColumnType, Generated } from '@hozon/db';
|
|
2
2
|
import type { LogLevel, StoredLog } from './types.js';
|
|
3
3
|
export type LogTables = {
|
|
4
|
-
|
|
4
|
+
logs: {
|
|
5
5
|
seq: Generated<number>;
|
|
6
6
|
timestamp: number;
|
|
7
7
|
level: LogLevel;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hozon/store-log",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Portable structured log store for Hozon",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@hozon/adapter": "^0.1.0",
|
|
21
|
-
"@hozon/db": "^0.1
|
|
22
|
-
"@sozai/json": "^0.3.0"
|
|
23
|
-
"kysely": "^0.29.6"
|
|
21
|
+
"@hozon/db": "^0.2.1",
|
|
22
|
+
"@sozai/json": "^0.3.0"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
26
25
|
"@hozon/node-sqlite": "^0.1.0",
|
|
27
26
|
"@types/node": "^26.6.4",
|
|
28
27
|
"del-cli": "^7.0.0",
|
|
28
|
+
"kysely": "^0.29.6",
|
|
29
29
|
"vitest": "5.0.3"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|