Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-krysiak committed Nov 23, 2018
0 parents commit ba4cf4a
Show file tree
Hide file tree
Showing 44 changed files with 10,552 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"test": {
"plugins": [ "istanbul" ]
}
}
}
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
51 changes: 51 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = {
'env': {
'es6': true,
'node': true,
'mocha': true
},
'extends': 'airbnb',
'parserOptions': {
'ecmaVersion': 2018,
'sourceType': 'module'
},
'rules': {
'indent': [
'error',
2
],
'linebreak-style': [
'error',
'unix'
],
'quotes': [
'error',
'single'
],
'semi': [
'error',
'never'
],
'import/no-unresolved': 'off',
'no-underscore-dangle': 'off',
'guard-for-in': 'off',
'no-restricted-syntax': 'off',
'no-await-in-loop': 'off',
},
overrides: [
{
files: ['*-test.js', '*.spec.js'],
rules: {
'no-unused-expressions': 'off',
'func-names': 'off',
'prefer-arrow-callback': 'off'
}
}
],
globals: {
'expect': true,
'factory': true,
'sandbox': true,
'server': true
}
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.nyc_output
docs
coverage
.DS_store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Admin Bro
32 changes: 32 additions & 0 deletions admin/backend/adapters/abstract/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const NotImplementedError = require('../../utils/not-implemented-error')

/**
* Representation of an ORM/database AdminBro
*/
class AbstractDatabase {
/**
* Return name of the database
* @return {String}
*/
name() {
throw new NotImplementedError()
}

/**
* returns array of all models (collections/tables) in the database
* @return {AbstractModel[]}
*/
models() {
throw new NotImplementedError()
}

/**
* returns model for given name
* @return {AbstractModel}
*/
find(modelName) {
throw new NotImplementedError()
}
}

module.exports = AbstractDatabase
11 changes: 11 additions & 0 deletions admin/backend/adapters/abstract/instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AbstractInstance {
constructor(args) {
this.args = args
}

save(params) {

}
}

module.exports = AbstractInstance
51 changes: 51 additions & 0 deletions admin/backend/adapters/abstract/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const NotImplementedError = require('../../utils/not-implemented-error')

/**
* Representation of a ORM Model in AdminBro
*/
class AbstractModel {
/**
* Return name of the model
* @return {String}
*/
name() {
throw new NotImplementedError()
}

/**
* returns array of properties
* @return {AbstractProperty[]}
*/
properties() {
throw new NotImplementedError()
}

/**
* returns property object for given field
* @return {AbstractProperty}
*/
property(name) {
throw new NotImplementedError()
}

/**
* Returns number of elements for given model
* @return {[type]} [description]
*/
count() {
throw new NotImplementedError()
}

/**
* Returns actual records for given model
* @param {Object} where query
* @param {Number} options.limit how many records should be taken
* @param {Number} options.offset offset
* @return {Object[]} list of all records
*/
find(where, { limit=20, offset=0 }) {
throw new NotImplementedError()
}
}

module.exports = AbstractModel
32 changes: 32 additions & 0 deletions admin/backend/adapters/abstract/property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const TITLE_COLUMN_NAMES = ['title', 'name', 'subject']

class AbstractProperty {

/**
* Name of the property
* @return {[type]} [description]
*/
name() {

}

/**
* Return type of a property
* @return {String} One of available property types:
* [id, string, object, float, number, boolean,
* text, date]
*/
type() {

}

/**
* When properties are nested - parent property should have its children
* @return {AbstractProperty[]} [description]
*/
childProperties() {
return null
}
}

module.exports = AbstractProperty
10 changes: 10 additions & 0 deletions admin/backend/adapters/database-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const MongooseDatabase = require('./mongoose/database')

const DatabaseFactory = (database) => {
if (database.constructor.name === 'Mongoose') {
return database.connections.map(connection => new MongooseDatabase(connection))
}
throw new Error(`unsupported database type ${database}`)
}

module.exports = DatabaseFactory
26 changes: 26 additions & 0 deletions admin/backend/adapters/mongoose/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const AbstractDatabase = require('../abstract/database')
const Model = require('./model')

/**
* Adapter for mongoose database
*/
class Database extends AbstractDatabase {
constructor(connection) {
super(connection)
this.connection = connection
}

models() {
return Model.all(this.connection)
}

name() {
return this.connection.name
}

find(modelName) {
return Model.find(this.connection, modelName)
}
}

module.exports = Database
6 changes: 6 additions & 0 deletions admin/backend/adapters/mongoose/instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const AbstractInstance = require('../abstract/instance')

class Instance extends AbstractInstance {
}

module.exports = Instance
73 changes: 73 additions & 0 deletions admin/backend/adapters/mongoose/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const AbstractModel = require('../abstract/model')
const Instance = require('./instance')
const Property = require('./property')

/**
* Adapter for mongoose model
*/
class Model extends AbstractModel {
/**
* Return all available models for given connection
* @param {Object} mongooseConnection mongoose connection object
* @return {Model[]} list of all models in given mongo database
*
* @example
* const mongoose = require('mongoose')
*
* const connection = await mongoose.connect(process.env.MONGO_URL)
* Model.all(connection)
*/
static all(mongooseConnection) {
return mongooseConnection.modelNames().map(name => Model.find(mongooseConnection, name))
}

/**
* Return Model object for given collection name
* @param {Object} mongooseConnection mongoose connection object
* @param {String} name name of mongoose model
* @return {Model} model adapter for given mongodb model
*
* @example
* const mongoose = require('mongoose')
*
* const connection = await mongoose.connect(process.env.MONGO_URL)
* Model.all(connection)
*/
static find(mongooseConnection, name) {
const mongoModel = mongooseConnection.model(name)
return new Model(mongoModel)
}

constructor(mongoModel) {
super(mongoModel)
this.model = mongoModel
}

async count() {
return this.model.countDocuments()
}

async find(query, { limit = 20, offset = 0 }) {
const raw = await this.model.find({}).skip(offset).limit(limit)
return raw.map(m => new Instance(m))
}

name() {
return this.model.modelName
}

properties() {
const properties = []
for (const [name, path] of Object.entries(this.model.schema.paths)) {
const prop = new Property(name, path)
properties.push(prop)
}
return properties
}

property(name) {
return new Property(name, this.model.schema.paths[name])
}
}

module.exports = Model
11 changes: 11 additions & 0 deletions admin/backend/adapters/mongoose/property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const AbstractProperty = require('../abstract/property')

class Property extends AbstractProperty {
constructor(name, mongoosePath) {
super()
this.name = name
this.path = mongoosePath
}
}

module.exports = Property
8 changes: 8 additions & 0 deletions admin/backend/utils/not-implemented-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class NotImplementedError extends Error {
constructor(args) {
super(args)
this.message = 'You have to implement this'
}
}

module.exports = NotImplementedError
31 changes: 31 additions & 0 deletions admin/backend/utils/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const pug = require('pug')
const sass = require('node-sass')
const { promisify } = require('util')

class Renderer {
constructor(view, data) {
this.view = view
this.data = data

this.styles_path = 'admin/frontend/styles/index.css.sass'
this.views_path = 'admin/frontend/views/'
}

async styles() {
const style = await promisify(sass.render)({
file: this.styles_path,
})
return style.css.toString('utf-8')
}

async render() {
const data = {
adminStyles: await this.styles(),
...this.data,
}
const viewFunction = pug.compileFile(`${this.views_path}${this.view}.pug`)
return viewFunction(data)
}
}

module.exports = Renderer
Empty file.
Loading

0 comments on commit ba4cf4a

Please sign in to comment.