|
| 1 | +import app from "../../xata.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + props: { |
| 5 | + app, |
| 6 | + endpoint: { |
| 7 | + propDefinition: [ |
| 8 | + app, |
| 9 | + "endpoint", |
| 10 | + ], |
| 11 | + }, |
| 12 | + workspace: { |
| 13 | + propDefinition: [ |
| 14 | + app, |
| 15 | + "workspace", |
| 16 | + ], |
| 17 | + }, |
| 18 | + database: { |
| 19 | + propDefinition: [ |
| 20 | + app, |
| 21 | + "database", |
| 22 | + (c) => ({ |
| 23 | + workspace: c.workspace, |
| 24 | + }), |
| 25 | + ], |
| 26 | + }, |
| 27 | + branch: { |
| 28 | + propDefinition: [ |
| 29 | + app, |
| 30 | + "branch", |
| 31 | + (c) => ({ |
| 32 | + endpoint: c.endpoint, |
| 33 | + database: c.database, |
| 34 | + }), |
| 35 | + ], |
| 36 | + }, |
| 37 | + table: { |
| 38 | + propDefinition: [ |
| 39 | + app, |
| 40 | + "table", |
| 41 | + (c) => ({ |
| 42 | + endpoint: c.endpoint, |
| 43 | + database: c.database, |
| 44 | + branch: c.branch, |
| 45 | + }), |
| 46 | + ], |
| 47 | + reloadProps: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + async additionalProps(props) { |
| 51 | + const { |
| 52 | + endpoint, |
| 53 | + database, |
| 54 | + branch, |
| 55 | + table, |
| 56 | + } = this; |
| 57 | + |
| 58 | + const description = "The keys and values of the data that will be recorded in the database."; |
| 59 | + |
| 60 | + if (endpoint && database && branch && table) { |
| 61 | + const { columns } = await this.app.listColumns({ |
| 62 | + endpoint, |
| 63 | + database, |
| 64 | + branch, |
| 65 | + table, |
| 66 | + }); |
| 67 | + if (columns?.length) { |
| 68 | + let descriptionWithColumns = `${description} Available Columns:`; |
| 69 | + for (const column of columns) { |
| 70 | + descriptionWithColumns += ` \`${column.name}\``; |
| 71 | + } |
| 72 | + props.recordData.description = descriptionWithColumns; |
| 73 | + return {}; |
| 74 | + } |
| 75 | + } |
| 76 | + props.recordData.description = description; |
| 77 | + return {}; |
| 78 | + }, |
| 79 | + methods: { |
| 80 | + async formatRecordData() { |
| 81 | + const recordData = this.recordData; |
| 82 | + const { columns } = await this.app.listColumns({ |
| 83 | + endpoint: this.endpoint, |
| 84 | + database: this.database, |
| 85 | + branch: this.branch, |
| 86 | + table: this.table, |
| 87 | + }); |
| 88 | + if (!columns?.length) { |
| 89 | + return this.recordData; |
| 90 | + } |
| 91 | + for (const column of columns) { |
| 92 | + if (!recordData[column.name] || typeof recordData[column.name] !== "string") { |
| 93 | + continue; |
| 94 | + } |
| 95 | + if ((column.type === "int" || column.type === "float")) { |
| 96 | + recordData[column.name] = +recordData[column.name]; |
| 97 | + } |
| 98 | + if (column.type === "bool") { |
| 99 | + recordData[column.name] = !(recordData[column.name] === "false" || recordData[column.name === "0"]); |
| 100 | + } |
| 101 | + if (column.type === "multiple" || column.type === "vector") { |
| 102 | + recordData[column.name] = JSON.parse(recordData[column.name]); |
| 103 | + } |
| 104 | + } |
| 105 | + return recordData; |
| 106 | + }, |
| 107 | + }, |
| 108 | +}; |
0 commit comments