1
0
Fork 0
mirror of synced 2024-07-02 04:50:44 +12:00
budibase/packages/server/src/api/controllers/model.js

125 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
2020-05-19 03:22:09 +12:00
const newid = require("../../db/newid")
2020-04-09 03:57:27 +12:00
2020-04-13 22:47:53 +12:00
exports.fetch = async function(ctx) {
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ctx.user.instanceId)
2020-05-07 21:53:34 +12:00
const body = await db.query("database/by_type", {
2020-04-13 22:47:53 +12:00
include_docs: true,
2020-05-07 21:53:34 +12:00
key: ["model"],
})
ctx.body = body.rows.map(row => row.doc)
2020-04-13 22:47:53 +12:00
}
2020-06-04 03:10:03 +12:00
exports.find = async function(ctx) {
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ctx.user.instanceId)
2020-06-04 03:10:03 +12:00
const model = await db.get(ctx.params.id)
ctx.body = model
}
exports.save = async function(ctx) {
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ctx.user.instanceId)
2020-06-17 07:29:18 +12:00
const modelToSave = {
type: "model",
2020-05-19 03:22:09 +12:00
_id: newid(),
2020-08-19 04:14:26 +12:00
views: {},
2020-06-17 07:29:18 +12:00
...ctx.request.body,
2020-05-15 02:12:30 +12:00
}
2020-09-15 02:41:20 +12:00
// rename record fields when table column is renamed
2020-06-26 09:34:38 +12:00
const { _rename } = modelToSave
if (_rename) {
const records = await db.query(`database/all_${modelToSave._id}`, {
2020-06-26 09:43:10 +12:00
include_docs: true,
})
2020-06-26 09:34:38 +12:00
const docs = records.rows.map(({ doc }) => {
doc[_rename.updated] = doc[_rename.old]
delete doc[_rename.old]
return doc
})
await db.bulkDocs(docs)
delete modelToSave._rename
}
2020-09-15 02:41:20 +12:00
// update schema of non-statistics views when new columns are added
2020-09-15 02:40:45 +12:00
for (let view in modelToSave.views) {
const modelView = modelToSave.views[view]
if (!modelView) continue
if (modelView.schema.group || modelView.schema.field) continue
modelView.schema = modelToSave.schema
}
2020-06-17 07:29:18 +12:00
const result = await db.post(modelToSave)
modelToSave._rev = result.rev
const { schema } = ctx.request.body
for (let key in schema) {
// model has a linked record
if (schema[key].type === "link") {
// create the link field in the other model
2020-06-12 04:28:19 +12:00
const linkedModel = await db.get(schema[key].modelId)
2020-06-26 07:35:03 +12:00
linkedModel.schema[modelToSave.name] = {
2020-06-23 08:30:23 +12:00
name: modelToSave.name,
type: "link",
2020-06-17 07:29:18 +12:00
modelId: modelToSave._id,
constraints: {
2020-06-12 04:28:19 +12:00
type: "array",
2020-06-26 09:43:10 +12:00
},
}
2020-06-12 04:28:19 +12:00
await db.put(linkedModel)
}
}
2020-05-07 21:53:34 +12:00
const designDoc = await db.get("_design/database")
2020-04-13 22:47:53 +12:00
designDoc.views = {
...designDoc.views,
2020-06-17 07:29:18 +12:00
[`all_${modelToSave._id}`]: {
map: `function(doc) {
2020-06-17 07:29:18 +12:00
if (doc.modelId === "${modelToSave._id}") {
2020-06-26 07:04:58 +12:00
emit(doc._id);
}
2020-05-07 21:53:34 +12:00
}`,
},
}
await db.put(designDoc)
2020-05-15 02:12:30 +12:00
ctx.status = 200
2020-06-17 07:29:18 +12:00
ctx.message = `Model ${ctx.request.body.name} saved successfully.`
ctx.body = modelToSave
2020-04-09 03:57:27 +12:00
}
2020-04-10 03:53:48 +12:00
exports.destroy = async function(ctx) {
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ctx.user.instanceId)
2020-06-12 04:28:19 +12:00
const modelToDelete = await db.get(ctx.params.modelId)
await db.remove(modelToDelete)
2020-06-12 05:11:56 +12:00
2020-05-15 02:12:30 +12:00
const modelViewId = `all_${ctx.params.modelId}`
// Delete all records for that model
2020-05-07 21:53:34 +12:00
const records = await db.query(`database/${modelViewId}`)
await db.bulkDocs(
2020-06-26 07:04:58 +12:00
records.rows.map(record => ({ _id: record.id, _deleted: true }))
2020-05-07 21:53:34 +12:00
)
// Delete linked record fields in dependent models
for (let key in modelToDelete.schema) {
2020-06-12 04:28:19 +12:00
const { type, modelId } = modelToDelete.schema[key]
if (type === "link") {
2020-06-12 04:28:19 +12:00
const linkedModel = await db.get(modelId)
delete linkedModel.schema[modelToDelete.name]
await db.put(linkedModel)
}
}
// delete the "all" view
2020-05-07 21:53:34 +12:00
const designDoc = await db.get("_design/database")
delete designDoc.views[modelViewId]
await db.put(designDoc)
2020-05-15 02:12:30 +12:00
ctx.status = 200
ctx.message = `Model ${ctx.params.modelId} deleted.`
2020-04-10 03:53:48 +12:00
}