Skip to content

Commit a81f591

Browse files
committed
Add example apps for preset usage and audit plugin
Introduces two new example applications: one demonstrating how to use a preset ('app-using-preset') and another showing a custom audit log plugin ('plugin-audit'). Each example includes configuration, TypeScript source, and supporting files. Also updates the project-management tsconfig to exclude its config file.
1 parent b9c50c2 commit a81f591

12 files changed

Lines changed: 201 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ObjectQL } from '@objectql/core';
2+
import * as path from 'path';
3+
4+
const db = new ObjectQL({
5+
connection: `sqlite://${path.join(__dirname, 'preset.sqlite3')}`,
6+
// Load the project-management capabilities as a preset
7+
presets: ['@example/project-management']
8+
});
9+
10+
export default db;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@example/app-using-preset",
3+
"version": "1.0.0",
4+
"private": true,
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"repl": "objectql repl"
9+
},
10+
"dependencies": {
11+
"@example/project-management": "workspace:*"
12+
},
13+
"devDependencies": {
14+
"@objectql/core": "workspace:*",
15+
"@objectql/types": "workspace:*",
16+
"@objectql/cli": "workspace:*",
17+
"@objectql/driver-knex": "workspace:*",
18+
"sqlite3": "^5.1.7",
19+
"typescript": "^5.3.0"
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import db from '../objectql.config';
2+
3+
async function main() {
4+
await db.init();
5+
6+
console.log('--- Objects loaded from Preset ---');
7+
const objects = db.metadata.list('object');
8+
console.log(objects.map(o => o.name));
9+
10+
// We can access 'project' because it was loaded from the preset
11+
const projectRepo = db.createContext({}).object('project');
12+
13+
// Create a project using the preset's definition
14+
const project = await projectRepo.create({
15+
name: 'Preset Usage Demo',
16+
description: 'Created via App Using Preset'
17+
});
18+
19+
console.log('--- Created Project ---');
20+
console.log(project);
21+
}
22+
23+
main().catch(console.error);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"rootDir": ".",
6+
"composite": true
7+
},
8+
"include": ["src/**/*", "objectql.config.ts"]
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ObjectQL } from '@objectql/core';
2+
import * as path from 'path';
3+
import { AuditLogPlugin } from './src/audit.plugin';
4+
5+
const db = new ObjectQL({
6+
connection: `sqlite://${path.join(__dirname, 'audit.sqlite3')}`,
7+
source: ['src'],
8+
plugins: [
9+
new AuditLogPlugin()
10+
]
11+
});
12+
13+
export default db;

examples/plugin-audit/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@example/plugin-audit",
3+
"version": "0.1.0",
4+
"private": true,
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"repl": "objectql repl"
9+
},
10+
"dependencies": {
11+
"sqlite3": "^5.1.7"
12+
},
13+
"devDependencies": {
14+
"@objectql/cli": "workspace:*",
15+
"@objectql/core": "workspace:*",
16+
"@objectql/types": "workspace:*",
17+
"@objectql/driver-knex": "workspace:*",
18+
"typescript": "^5.3.0"
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ObjectQLPlugin, IObjectQL, MutationHookContext } from '@objectql/types';
2+
3+
export class AuditLogPlugin implements ObjectQLPlugin {
4+
name = 'audit-log';
5+
6+
setup(app: IObjectQL) {
7+
console.log('[AuditLogPlugin] Setting up...');
8+
9+
// 1. Listen to all 'afterCreate' events
10+
app.on('afterCreate', '*', async (ctx) => {
11+
// Narrow down context type or use assertion since 'afterCreate' is Mutation
12+
const mutationCtx = ctx as MutationHookContext;
13+
const userId = mutationCtx.user?.id || 'Guest';
14+
console.log(`[Audit] Created ${mutationCtx.objectName} (ID: ${mutationCtx.id}) by User ${userId}`);
15+
});
16+
17+
// 2. Listen to all 'afterDelete' events
18+
app.on('afterDelete', '*', async (ctx) => {
19+
const mutationCtx = ctx as MutationHookContext;
20+
console.log(`[Audit] Deleted ${mutationCtx.objectName} (ID: ${mutationCtx.id})`);
21+
});
22+
}
23+
}

examples/plugin-audit/src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import db from '../objectql.config';
2+
3+
async function main() {
4+
await db.init();
5+
6+
const noteRepo = db.createContext({ userId: 'u001' }).object('note');
7+
8+
console.log('--- Creating Note ---');
9+
// Should trigger [Audit] log
10+
const note = await noteRepo.create({
11+
content: 'Hello Project Plugin!'
12+
});
13+
14+
console.log('--- Deleting Note ---');
15+
// Should trigger [Audit] log
16+
await noteRepo.delete(note.id);
17+
}
18+
19+
main().catch(console.error);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: note
2+
fields:
3+
content:
4+
type: text
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"rootDir": ".",
6+
"composite": true
7+
},
8+
"include": ["src/**/*", "objectql.config.ts"]
9+
}

0 commit comments

Comments
 (0)