@saltcorn/badges
Advanced tools
Comparing version 0.1.1 to 0.1.2
211
index.js
@@ -1,211 +0,6 @@ | ||
const { | ||
input, | ||
div, | ||
text, | ||
script, | ||
span, | ||
style, | ||
button, | ||
} = require("@saltcorn/markup/tags"); | ||
const View = require("@saltcorn/data/models/view"); | ||
const Workflow = require("@saltcorn/data/models/workflow"); | ||
const Table = require("@saltcorn/data/models/table"); | ||
const Form = require("@saltcorn/data/models/form"); | ||
const Field = require("@saltcorn/data/models/field"); | ||
const db = require("@saltcorn/data/db"); | ||
const { | ||
stateFieldsToWhere, | ||
picked_fields_to_query, | ||
} = require("@saltcorn/data/plugin-helper"); | ||
const configuration_workflow = () => | ||
new Workflow({ | ||
steps: [ | ||
{ | ||
name: "views", | ||
form: async (context) => { | ||
const table = await Table.findOne({ id: context.table_id }); | ||
const mytable = table; | ||
const fields = await table.getFields(); | ||
const { | ||
child_field_list, | ||
child_relations, | ||
} = await table.get_child_relations(); | ||
var agg_field_opts = []; | ||
child_relations.forEach(({ table, key_field }) => { | ||
table.fields | ||
.filter((f) => !f.calculated || f.stored) | ||
.forEach((f) => { | ||
agg_field_opts.push({ | ||
name: `${table.name}.${key_field.name}.${f.name}`, | ||
label: `${table.name}.${key_field.name}→${f.name}`, | ||
}); | ||
}); | ||
}); | ||
for (const { table, key_field } of child_relations) { | ||
const keyFields = table.fields.filter( | ||
(f) => | ||
f.type === "Key" && | ||
![mytable.name, "users", "_sc_files"].includes(f.reftable_name) | ||
); | ||
for (const kf of keyFields) { | ||
const joined_table = await Table.findOne({ | ||
name: kf.reftable_name, | ||
}); | ||
if (!joined_table) continue; | ||
await joined_table.getFields(); | ||
joined_table.fields.forEach((jf) => { | ||
agg_field_opts.push({ | ||
label: `${table.name}.${key_field.name}→${kf.name}→${jf.name}`, | ||
name: `${table.name}.${key_field.name}.${kf.name}.${jf.name}`, | ||
}); | ||
}); | ||
} | ||
} | ||
return new Form({ | ||
fields: [ | ||
{ | ||
name: "relation", | ||
label: "Relation", | ||
type: "String", | ||
required: true, | ||
attributes: { | ||
options: agg_field_opts, | ||
}, | ||
}, | ||
], | ||
}); | ||
}, | ||
}, | ||
], | ||
}); | ||
const get_state_fields = async (table_id, viewname, { columns }) => [ | ||
{ | ||
name: "id", | ||
type: "Integer", | ||
required: true, | ||
}, | ||
]; | ||
const run = async (table_id, viewname, { relation }, state, extra) => { | ||
const { id } = state; | ||
if (!id) return "need id"; | ||
const relSplit = relation.split("."); | ||
if (relSplit.length < 3) { | ||
throw new Error("badges view incorrectly configured. No relation chosen"); | ||
} | ||
if (relSplit.length === 3) { | ||
const [relTableNm, relField, valField] = relSplit; | ||
const relTable = await Table.findOne({ name: relTableNm }); | ||
const rows = await relTable.getJoinedRows({ | ||
where: { [relField]: id }, | ||
}); | ||
return rows | ||
.map((row) => span({ class: "badge badge-secondary" }, row[valField])) | ||
.join(" "); | ||
} else { | ||
const [relTableNm, relField, joinFieldNm, valField] = relSplit; | ||
const table = await Table.findOne({ id: table_id }); | ||
const relTable = await Table.findOne({ name: relTableNm }); | ||
await relTable.getFields(); | ||
const joinField = relTable.fields.find((f) => f.name === joinFieldNm); | ||
const rows = await table.getJoinedRows({ | ||
where: { id }, | ||
aggregations: { | ||
_badges: { | ||
table: joinField.reftable_name, | ||
ref: "id", | ||
subselect: { | ||
field: joinFieldNm, | ||
table: relTable, | ||
whereField: relField, | ||
}, | ||
field: valField, | ||
aggregate: "ARRAY_AGG", | ||
}, | ||
}, | ||
}); | ||
return rows[0]._badges | ||
.map((b) => span({ class: "badge badge-secondary" }, b)) | ||
.join(" "); | ||
} | ||
}; | ||
const runMany = async (table_id, viewname, { relation }, state, extra) => { | ||
const tbl = await Table.findOne({ id: table_id }); | ||
const fields = await tbl.getFields(); | ||
const qstate = await stateFieldsToWhere({ fields, state }); | ||
const relSplit = relation.split("."); | ||
if (relSplit.length < 3) { | ||
throw new Error("badges view incorrectly configured. No relation chosen"); | ||
} | ||
if (relSplit.length === 3) { | ||
const [relTableNm, relField, valField] = relSplit; | ||
const rows = await tbl.getJoinedRows({ | ||
where: qstate, | ||
aggregations: { | ||
_badges: { | ||
table: relTableNm, | ||
ref: relField, | ||
field: valField, | ||
aggregate: "ARRAY_AGG", | ||
}, | ||
}, | ||
...(extra && extra.orderBy && { orderBy: extra.orderBy }), | ||
...(extra && extra.orderDesc && { orderDesc: extra.orderDesc }), | ||
}); | ||
return rows.map((row) => ({ | ||
html: row._badges | ||
.map((b) => span({ class: "badge badge-secondary" }, b)) | ||
.join(" "), | ||
row, | ||
})); | ||
} else { | ||
const [relTableNm, relField, joinFieldNm, valField] = relSplit; | ||
const relTable = await Table.findOne({ name: relTableNm }); | ||
await relTable.getFields(); | ||
const joinField = relTable.fields.find((f) => f.name === joinFieldNm); | ||
const rows = await tbl.getJoinedRows({ | ||
where: qstate, | ||
aggregations: { | ||
_badges: { | ||
table: joinField.reftable_name, | ||
ref: "id", | ||
subselect: { | ||
field: joinFieldNm, | ||
table: relTable, | ||
whereField: relField, | ||
}, | ||
field: valField, | ||
aggregate: "ARRAY_AGG", | ||
}, | ||
}, | ||
}); | ||
return rows.map((row) => ({ | ||
html: row._badges | ||
.map((b) => span({ class: "badge badge-secondary" }, b)) | ||
.join(" "), | ||
row, | ||
})); | ||
} | ||
}; | ||
const badges = require("./show"); | ||
const edit = require("./edit"); | ||
module.exports = { | ||
sc_plugin_api_version: 1, | ||
viewtemplates: [ | ||
{ | ||
name: "Badges", | ||
display_state_form: false, | ||
get_state_fields, | ||
configuration_workflow, | ||
run, | ||
runMany, | ||
}, | ||
], | ||
viewtemplates: [badges, edit], | ||
}; |
{ | ||
"name": "@saltcorn/badges", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Display badges from relationships", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12750
5
345
1