Javascript CRUD Admin for Spring -Data HATEOAS backend

We use Spring-Boot Spring-Data Backend, which uses annotated JPARepositories @RepositoryRestResource. We would like to manage the tables in these repositories (e.g. CRUD) through the javascript interface without having to do the work of coding it. We explored various alternatives such as LightAdmin, JHipster and ng-admin.

We could not get LightAdmin to work, because it uses a much older version of Spring-Data than what we are running. It is incompatible with the latest and largest release of Spring-Data.

We tried JHipster, but it raises all the services and controllers that we don't want, because @RepositoryRestResource provides this for you for free.

We tried using ng-admin, but this does not work very well in the HATEOAS context; we had to display too much to make it work only partially.

So my question is this. Is there a product similar to ng-admin, JHipster and LightAdmin that will allow us to easily CRUD in our @RepositoryRestResourceJPA repositories , so that we do not need to write the code for the CRUD template?

+5
source share
1 answer
may be you can use my awesome code :


db.js 

const sqlite3 = require("sqlite3").verbose()
const db = new sqlite3.Database("./book.db")

module.exports = db

setup.js

const db = require("./db.js")

const query = [
    '
    CREATE TABLE IF NOT EXISTS
    contacts (
        contactID  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        name TEXT,
        company TEXT,
        telephone TEXT,
        email TEXT UNIQUE
    )',
    '
    CREATE TABLE IF NOT EXISTS
    groups (
        groupID  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        name TEXT
    )',
    '
    CREATE TABLE IF NOT EXISTS
    group_contacts (
        group_contactsID  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        contactID INTEGER NOT NULL,
        groupID INTEGER NOT NULL,
        FOREIGN KEY(contactID) REFERENCES contacts(contactID),
        FOREIGN KEY(groupID) REFERENCES groups(groupID)

    )'
]

for (let i = 0; i < query.length; i++){
    db.run(query[i], (err) =>{
        console.log(err)
    })
}

index.js

const controller = require("./controller.js")
let input = process.argv.slice(2)
let cmd = input[0]
let data = input.slice(1)

switch(cmd){
    case "createContact":
    controller.createContact(data)
    break

controller.js

const contact = require("./model/contact.js")
const group = require("./model/group.js")
const groupContact = require("./model/contact-group.js")
const view = require("./view.js")
class Controller{
    static createContact(data){
        contact.create(data, (err, output) => {
            if (err){
                view.createError(err)
            } else {
                view.createSuccess(output)
            }
        })
    }
    static readContact(data){
        contact.read(data, (err, output) => {
            if (err){
                view.readError(err)
            } else {
                view.readSuccess(output)
            }
        })  
    }
    static updateContact(data){
        contact.update(data, (err) => {
            if (err){
                view.updateError(err)
            } else {
                view.updateSuccess(data)
            }
        })
    }
    static deleteContact(data){
        contact.delete(data, (err) => {
            if (err){
                view.deleteError(err)
            } else {
                view.deleteSuccess(data[0])
            }
        })
    }
}
module.exports = Controller

model.js

const db = require("../db.js")
class Contact{
    constructor(name, company, phone, email){
        this.name = name
        this.company = company
        this.telephone = phone
        this.email = email
    }
    static create(data, cb){
        db.serialize((err) => {
            if (err){
                cb(err, null)
            } else {
                let newContact = new Contact(data[0], data[1], data[2], data[3])
                let query = '
                INSERT INTO contacts (name, company, telephone, email) 
                VALUES ('${data[0]}', '${data[1]}', '${data[2]}', '${data[3]}')'
                db.run(query, (err) => {
                    if (err){
                        cb(err, null)
                    } else {
                        cb(null, newContact)
                    }
                })
            }
        })
    }
    static update(data, cb){
        db.serialize((err) => {
            if (err){
                cb(err, null)
            } else {
                let query = '
                UPDATE contacts SET ${data[1]} = '${data[2]}' WHERE contactID = ${data[0]}'
                db.get(query, (err) => {
                    if (err){
                        cb(err)
                    } else {
                        cb(null)
                    }
                })
            }
        })
    }
    static read(data, cb){
        db.serialize((err) => {
            if (err){
                cb(err, null)
            } else {
                let query = '
                SELECT contactID, name, company, telephone, email FROM contacts WHERE contactID = ${data[0]}'
                db.get(query, (err, result) => {
                    if (err){
                        cb(err, null)
                    } else {
                        cb(null, result)
                    }
                })
            }
        })
    }
    static delete(data, cb){
        db.serialize((err) => {
            if (err){
                cb(err, null)
            } else {
                let query = '
                DELETE FROM contacts WHERE contactID = ${data[0]}'
                db.get(query, (err) => {
                    if (err){
                        cb(err)
                    } else {
                        cb(null)
                    }
                })
            }
        })
    }
}

module.exports = Contact

view.js


class View {
    static createError(err){
        console.log(err)
        console.log('=====> ERROR')
    }
    static createSuccess(output){
        console.log(output)
        console.log('=====> SUCCESS')
    }
    static readError(err){
        console.log(err)
        console.log('=====> ERROR')
    }
    static readSuccess(output){
        console.log(output)
        console.log('=====> SUCCESS')
    }
    static updateError(err){
        console.log(err)
        console.log('=====> ERROR')
    }
    static updateSuccess(output){
        console.log(output)
        console.log('=====> SUCCESS')
    }
    static deleteError(err){
        console.log(err)
        console.log('=====> ERROR')
    }
    static deleteSuccess(output){
        console.log(output)
        console.log('=====> SUCCESS')
    }
    static undefined(output){
        console.log(output)
        console.log('=====> UNDEFINED')
    }

}

module.exports = View
0
source

Source: https://habr.com/ru/post/1624884/


All Articles