adds multiple selection delete functionality

This commit is contained in:
kevmodrome 2020-09-28 15:32:06 +02:00
parent 0a398c43c3
commit d94a9b193b
4 changed files with 154 additions and 97 deletions

View File

@ -61,73 +61,11 @@ exports.patch = async function(ctx) {
}
exports.save = async function (ctx) {
const db = new CouchDB(ctx.user.instanceId)
const record = ctx.request.body
record.modelId = ctx.params.modelId
if (!record._rev && !record._id) {
record._id = newid()
if (ctx.request.body.type === 'delete') {
await bulkDelete(ctx)
} else {
await saveRecords(ctx)
}
const model = await db.get(record.modelId)
const validateResult = await validate({
record,
model,
})
if (!validateResult.valid) {
ctx.status = 400
ctx.body = {
status: 400,
errors: validateResult.errors,
}
return
}
const existingRecord = record._rev && (await db.get(record._id))
if (existingRecord) {
const response = await db.put(record)
record._rev = response.rev
record.type = "record"
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} updated successfully.`
return
}
record.type = "record"
const response = await db.post(record)
record._rev = response.rev
// create links in other tables
for (let key in record) {
if (model.schema[key] && model.schema[key].type === "link") {
const linked = await db.allDocs({
include_docs: true,
keys: record[key],
})
// add this record to the linked records in attached models
const linkedDocs = linked.rows.map(row => {
const doc = row.doc
return {
...doc,
[model.name]: doc[model.name]
? [...doc[model.name], record._id]
: [record._id],
}
})
await db.bulkDocs(linkedDocs)
}
}
emitEvent(`record:save`, ctx, record)
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} created successfully`
}
exports.fetchView = async function (ctx) {
@ -218,3 +156,95 @@ async function validate({ instanceId, modelId, record, model }) {
}
return { valid: Object.keys(errors).length === 0, errors }
}
async function bulkDelete(ctx) {
const { records } = ctx.request.body
console.log(records)
const db = new CouchDB(ctx.user.instanceId)
await db.bulkDocs(
records.map(record => ({ ...record, _deleted: true })), console.log)
// await db.bulkDocs(
// records.rows.map(record => ({ _id: record.id, _deleted: true }))
// )
// const record = await db.get(ctx.params.recordId)
// if (record.modelId !== ctx.params.modelId) {
// ctx.throw(400, "Supplied modelId doesn't match the record's modelId")
// return
// }
// ctx.body = await db.remove(ctx.params.recordId, ctx.params.revId)
ctx.status = 200
// // for automations
// ctx.record = record
// emitEvent(`record:delete`, ctx, record)
}
async function saveRecords(ctx) {
const db = new CouchDB(ctx.user.instanceId)
const record = ctx.request.body
record.modelId = ctx.params.modelId
if (!record._rev && !record._id) {
record._id = newid()
}
const model = await db.get(record.modelId)
const validateResult = await validate({
record,
model,
})
if (!validateResult.valid) {
ctx.status = 400
ctx.body = {
status: 400,
errors: validateResult.errors,
}
return
}
const existingRecord = record._rev && (await db.get(record._id))
if (existingRecord) {
const response = await db.put(record)
record._rev = response.rev
record.type = "record"
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} updated successfully.`
return
}
record.type = "record"
const response = await db.post(record)
record._rev = response.rev
// create links in other tables
for (let key in record) {
if (model.schema[key] && model.schema[key].type === "link") {
const linked = await db.allDocs({
include_docs: true,
keys: record[key],
})
// add this record to the linked records in attached models
const linkedDocs = linked.rows.map(row => {
const doc = row.doc
return {
...doc,
[model.name]: doc[model.name]
? [...doc[model.name], record._id]
: [record._id],
}
})
await db.bulkDocs(linkedDocs)
}
}
emitEvent(`record:save`, ctx, record)
ctx.body = record
ctx.status = 200
ctx.message = `${model.name} created successfully`
}

View File

@ -29,7 +29,7 @@ router
)
.post(
"/api/:modelId/records/validate",
authorized(WRITE_MODEL, ctx => ctx.params.modelId),
authorized(WRITE_MODEL),
recordController.validate
)
.delete(

View File

@ -37,7 +37,7 @@
"dependencies": {
"@beyonk/svelte-googlemaps": "^2.2.0",
"@budibase/bbui": "^1.34.6",
"@budibase/svelte-ag-grid": "^0.0.10",
"@budibase/svelte-ag-grid": "^0.0.11",
"@fortawesome/fontawesome-free": "^5.14.0",
"@svelteschool/svelte-forms": "^0.7.0",
"britecharts": "^2.16.1",

View File

@ -20,6 +20,7 @@
let dataLoaded = false
let data
let columnDefs
let selectedRows = []
onMount(async () => {
const jsonModel = await _bb.api.get(`/api/models/${datasource.modelId}`)
@ -72,11 +73,33 @@
const json = await response.json()
console.log(json)
}
const deleteRecords = async () => {
console.log(_bb.api)
const response = await _bb.api.post(`/api/${datasource.name}/records`, {
records: selectedRows,
type: "delete",
})
data = data.filter(record => !selectedRows.includes(record))
selectedRows = []
}
</script>
<div class="container">
<div class="controls">
<button>Add Row</button>
{#if selectedRows.length > 0}
<button on:click={deleteRecords}>
Delete {selectedRows.length} row(s)
</button>
{/if}
</div>
{#if dataLoaded}
<AgGrid {data} {columnDefs} on:update={handleUpdate} />
<AgGrid
{data}
{columnDefs}
on:update={handleUpdate}
on:select={({ detail }) => (selectedRows = detail)} />
<InputForm fields={columnDefs} on:submit={handleSubmit} />
{/if}
</div>
@ -90,4 +113,8 @@
display: grid;
grid-template-columns: repeat(2);
}
.controls {
display: flex;
flex-direction: row;
}
</style>