Skip to content

Commit c9fd595

Browse files
authored
Merge pull request #7 from SoftwareBrothers/feat/update-logger
feat: add example app, update docs, update config options
2 parents 4083493 + a5aba67 commit c9fd595

25 files changed

+5305
-61
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
root: true,
23
parser: '@typescript-eslint/parser',
34
plugins: ['@typescript-eslint/eslint-plugin'],
45
extends: [
@@ -16,6 +17,8 @@ module.exports = {
1617
rules: {
1718
'prettier/prettier': 'error',
1819
'react/prop-types': 'off',
20+
'semi': ['error', 'always'],
21+
'quotes': ['error', 'single'],
1922
},
2023
ignorePatterns: ['node_modules', 'lib'],
2124
};

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,5 @@ dist
120120
# build files
121121
/build
122122
/lib
123-
/types
123+
/types
124+
example/.adminjs

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Or visit [AdminJS](https://github.com/SoftwareBrothers/adminjs) github page.
1212

1313
## Usage
1414

15+
See an example application in the repository: [Example App](https://github.com/SoftwareBrothers/adminjs-logger/tree/master/example)
1516

1617
## License
1718

example/.example.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SEQUELIZE_TYPE=postgres
2+
SEQUELIZE_USERNAME=postgres
3+
SEQUELIZE_PASSWORD=
4+
SEQUELIZE_HOST=localhost
5+
SEQUELIZE_PORT=5439
6+
SEQUELIZE_DATABASE=adminjs_example

example/docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '3.7'
2+
services:
3+
adminjs-example:
4+
container_name: adminjs-example
5+
image: postgres:12-alpine
6+
environment:
7+
POSTGRES_DB: adminjs_example
8+
POSTGRES_HOST_AUTH_METHOD: trust
9+
ports:
10+
- "5439:5432"
11+
volumes:
12+
- adminjs_example_db:/var/lib/postgresql/data
13+
volumes:
14+
adminjs_example_db:

example/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "adminjs-logger-example",
3+
"version": "1.0.0",
4+
"description": "An example for @adminjs/logger",
5+
"main": "index.js",
6+
"author": "Rafal Dziegielewski",
7+
"license": "MIT",
8+
"scripts": {
9+
"dev": "dotenv -c '.env' ts-node src/server.ts"
10+
},
11+
"devDependencies": {
12+
"@types/argon2": "^0.15.0",
13+
"@types/node": "^17.0.22",
14+
"ts-node": "^10.7.0",
15+
"typescript": "^4.6.2"
16+
},
17+
"dependencies": {
18+
"@adminjs/express": "^4.1.0",
19+
"@adminjs/sequelize": "^2.1.5",
20+
"adminjs": "^5.7.4",
21+
"argon2": "^0.28.5",
22+
"dotenv-cli": "^5.0.0",
23+
"express": "^4.17.3",
24+
"express-formidable": "^1.2.0",
25+
"express-session": "^1.17.2",
26+
"pg": "^8.7.3",
27+
"sequelize": "^6.17.0",
28+
"tslib": "^2.3.1"
29+
}
30+
}

example/src/item/item.model.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { DataTypes, Model, Optional } from 'sequelize';
2+
3+
import { sequelize } from '../sequelize';
4+
5+
type Item = {
6+
id: number;
7+
name: string;
8+
description: string;
9+
createdAt: Date;
10+
updatedAt: Date;
11+
};
12+
13+
type ItemCreationAttributes = Optional<Item, 'id' | 'createdAt' | 'updatedAt'>;
14+
15+
export class ItemModel extends Model<Item, ItemCreationAttributes> {
16+
declare id: number;
17+
declare name: string;
18+
declare description: string;
19+
declare createdAt: Date;
20+
declare updatedAt: Date;
21+
}
22+
23+
ItemModel.init(
24+
{
25+
id: {
26+
type: DataTypes.INTEGER,
27+
autoIncrement: true,
28+
primaryKey: true,
29+
},
30+
name: {
31+
type: new DataTypes.STRING(128),
32+
allowNull: false,
33+
},
34+
description: {
35+
type: DataTypes.TEXT,
36+
allowNull: true,
37+
},
38+
createdAt: {
39+
type: DataTypes.DATE,
40+
},
41+
updatedAt: {
42+
type: DataTypes.DATE,
43+
},
44+
},
45+
{
46+
sequelize,
47+
tableName: 'items',
48+
modelName: 'Item',
49+
timestamps: true,
50+
}
51+
);

example/src/item/item.resource.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { FeatureType, ResourceWithOptions } from 'adminjs';
2+
import { ItemModel } from './item.model';
3+
import loggerFeature from '../../../src';
4+
import loggerConfig from '../logger.config';
5+
6+
const ItemResource: ResourceWithOptions = {
7+
resource: ItemModel,
8+
options: {
9+
editProperties: ['name', 'description'],
10+
},
11+
features: [loggerFeature(loggerConfig) as FeatureType],
12+
};
13+
14+
export default ItemResource;

example/src/log/log.model.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
DataTypes,
3+
Model,
4+
Optional,
5+
NonAttribute,
6+
HasOneGetAssociationMixin,
7+
Association,
8+
} from 'sequelize';
9+
10+
import { sequelize } from '../sequelize';
11+
import { UserModel } from '../user/user.model';
12+
13+
type Log = {
14+
id: number;
15+
recordId: number;
16+
recordTitle: string;
17+
difference: Record<string, any>;
18+
userId: number;
19+
action: string;
20+
resource: string;
21+
createdAt: Date;
22+
updatedAt: Date;
23+
};
24+
25+
type LogCreationAttributes = Optional<Log, 'id' | 'createdAt' | 'updatedAt'>;
26+
27+
export class LogModel extends Model<Log, LogCreationAttributes> {
28+
declare id: number;
29+
declare recordId: number;
30+
declare recordTitle: string;
31+
declare difference: Record<string, any>;
32+
declare userId: number;
33+
declare action: string;
34+
declare resource: string;
35+
declare createdAt: Date;
36+
declare updatedAt: Date;
37+
38+
declare user?: NonAttribute<UserModel>;
39+
40+
declare getUser: HasOneGetAssociationMixin<UserModel>;
41+
42+
declare static associations: {
43+
user: Association<UserModel>;
44+
};
45+
}
46+
47+
LogModel.init(
48+
{
49+
id: {
50+
type: DataTypes.INTEGER,
51+
autoIncrement: true,
52+
primaryKey: true,
53+
},
54+
action: {
55+
type: new DataTypes.STRING(128),
56+
allowNull: false,
57+
},
58+
resource: {
59+
type: new DataTypes.STRING(128),
60+
allowNull: false,
61+
},
62+
userId: {
63+
type: DataTypes.INTEGER,
64+
allowNull: false,
65+
},
66+
recordId: {
67+
type: DataTypes.INTEGER,
68+
allowNull: false,
69+
},
70+
recordTitle: {
71+
type: new DataTypes.STRING(128),
72+
allowNull: false,
73+
},
74+
difference: {
75+
type: DataTypes.JSONB,
76+
allowNull: true,
77+
},
78+
createdAt: {
79+
type: DataTypes.DATE,
80+
},
81+
updatedAt: {
82+
type: DataTypes.DATE,
83+
},
84+
},
85+
{
86+
sequelize,
87+
tableName: 'logs',
88+
modelName: 'Log',
89+
timestamps: true,
90+
}
91+
);
92+
93+
LogModel.belongsTo(UserModel, {
94+
foreignKey: 'userId',
95+
targetKey: 'id',
96+
as: 'user',
97+
});

example/src/log/log.resource.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { LogModel } from './log.model';
2+
import { createLoggerResource } from '../../../src';
3+
import loggerConfig from '../logger.config';
4+
5+
const LogResource = createLoggerResource({
6+
resource: LogModel,
7+
featureOptions: loggerConfig,
8+
});
9+
10+
export default LogResource;

0 commit comments

Comments
 (0)