Skip to content

Commit

Permalink
test: add new test application under app
Browse files Browse the repository at this point in the history
This app will be launched for different adapters and plugins.
It's goal will be to test all kind of interactions for multiple
plugin/adapter configurations. And it is written in typescript
so types will be checked as well
  • Loading branch information
wojtek-krysiak committed Sep 24, 2020
1 parent 1c2ad8f commit a121c91
Show file tree
Hide file tree
Showing 21 changed files with 5,558 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ node_modules
src/frontend/assets/scripts

.adminbro

# example app 1
example-app/cypress/videos
example-app/cypress/screenshots

# example app 2
app/cypress/videos
app/cypress/screenshots
app/build
.env
3 changes: 3 additions & 0 deletions app/.env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_USER="adminbro"
POSTGRES_PASSWORD="adminbro"
POSTGRES_DATABASE="adminbro-app"
21 changes: 21 additions & 0 deletions app/build/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const admin_bro_1 = __importDefault(require("admin-bro"));
const sequelize_1 = __importDefault(require("@admin-bro/sequelize"));
const user_resource_1 = require("./admin/resources/user/user-resource");
const sequelize_2 = require("./databases/sequelize");
const express_1 = require("./plugins/express");
const options_1 = require("./admin/options");
admin_bro_1.default.registerAdapter(sequelize_1.default);
const run = async () => {
await sequelize_2.connect();
const admin = new admin_bro_1.default(Object.assign(Object.assign({}, options_1.options), { resources: [{
options: user_resource_1.UserResource,
resource: sequelize_2.models.User,
}] }));
express_1.listen(admin);
};
run();
Empty file added app/index.js
Empty file.
8 changes: 8 additions & 0 deletions app/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ignore": ["cypress/**/*.js"],
"ext": "js",
"watch": [
"../lib",
"./build"
]
}
33 changes: 33 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "app",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "tsc",
"start": "yarn build && node build/index.js",
"dev": "concurrently \"yarn build --watch\" \"nodemon node build/src/index.js\"",
"cypress:open": "cypress open",
"cypress:run": "cypress run"
},
"devDependencies": {
"@types/express": "^4.17.7",
"concurrently": "^5.2.0",
"cypress": "^4.11.0",
"nodemon": "^2.0.4",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"dependencies": {
"@admin-bro/express": "^3.0.0",
"@admin-bro/sequelize": "1.0.0-beta.5",
"admin-bro": "^3.1.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-formidable": "^1.2.0",
"express-session": "^1.17.1",
"morgan": "^1.10.0",
"pg": "^8.3.0",
"sequelize": "^6.3.5"
}
}
7 changes: 7 additions & 0 deletions app/src/admin/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AdminBroOptions } from 'admin-bro'

const rootPath = '/admin'

export const options: AdminBroOptions = {
rootPath,
}
8 changes: 8 additions & 0 deletions app/src/admin/resources/user/user-resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ResourceOptions } from 'admin-bro'

export const UserResource: ResourceOptions = {
navigation: {
name: 'ala',
icon: 'User',
},
}
1 change: 1 addition & 0 deletions app/src/databases/models.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type AvailableModels = 'User'
27 changes: 27 additions & 0 deletions app/src/databases/sequelize/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Sequelize } from 'sequelize'

const {
POSTGRES_USER,
POSTGRES_PASSWORD,
POSTGRES_DATABASE,
POSTGRES_PORT,
POSTGRES_HOST,
} = process.env
const sequelizeUrl = [
'postgres://', POSTGRES_USER, ':', POSTGRES_PASSWORD, '@', POSTGRES_HOST, ':', POSTGRES_PORT,
'/', POSTGRES_DATABASE,
].join('')

export const sequelize = new Sequelize(sequelizeUrl)

export const connect = async () => {
try {
await sequelize.authenticate()
console.log('Connection has been established successfully.')
} catch (error) {
console.error('Unable to connect to the database:', error)
}

await sequelize.sync({ force: true })
return sequelize
}
2 changes: 2 additions & 0 deletions app/src/databases/sequelize/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './connect'
export * from './models'
6 changes: 6 additions & 0 deletions app/src/databases/sequelize/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AvailableModels } from '../models.type'
import { UserModel } from './user-model'

export const models: Record<AvailableModels, any> = {
User: UserModel,
}
21 changes: 21 additions & 0 deletions app/src/databases/sequelize/user-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DataTypes, Model } from 'sequelize'
import { sequelize } from './connect'

export interface UserInterface extends Model {
id: string;
firstName: string;
lastName?: string;
}

export const UserModel = sequelize.define<UserInterface>('User', {
// Model attributes are defined here
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
},
}, {
// Other model options go here
})
26 changes: 26 additions & 0 deletions app/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import AdminBro from 'admin-bro'
import AdminBroSequelize from '@admin-bro/sequelize'
import { UserResource } from './admin/resources/user/user-resource'

import { connect, models } from './databases/sequelize'
import { listen } from './plugins/express'
import { options } from './admin/options'

AdminBro.registerAdapter(AdminBroSequelize)

const run = async (): Promise<void> => {
await connect()

const admin = new AdminBro({
...options,
resources: [{
options: UserResource,
resource: models.User,
}],
})

listen(admin)
}

run()
23 changes: 23 additions & 0 deletions app/src/plugins/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { buildRouter } from '@admin-bro/express'

import AdminBro from 'admin-bro'
import express from 'express'

const PORT = 3000

export const listen = (admin: AdminBro, port = PORT) => {
const router = buildRouter(admin)
const app = express()

app.use(admin.options.rootPath, router)


app.use((error, req, res, next) => {
if (error) {
console.error(error)
}
next(error)
})

app.listen(PORT, () => console.log('app is listening on http://localhost:3000/admin'))
}
25 changes: 25 additions & 0 deletions app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./build",
"target": "es2017",
"esModuleInterop": true,
"jsx": "react",
"strictNullChecks": true,
"strictPropertyInitialization": false,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"moduleResolution": "node",
"module": "commonjs",
"rootDir": "./",
"types": ["cypress"],
"paths": {
"react": ["node_modules/@types/react"],
"admin-bro": ["node_modules/admin-bro"],
},
"baseUrl": "."
},
"include": ["./src/**/*"]
}
Loading

0 comments on commit a121c91

Please sign in to comment.