|
| 1 | +import app from "../../data_stores.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "data_stores-update-ttl", |
| 5 | + name: "Update record expiration", |
| 6 | + description: "Update the expiration time for a record in your [Pipedream Data Store](https://pipedream.com/data-stores/).", |
| 7 | + version: "0.0.1", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + app, |
| 11 | + dataStore: { |
| 12 | + propDefinition: [ |
| 13 | + app, |
| 14 | + "dataStore", |
| 15 | + ], |
| 16 | + }, |
| 17 | + key: { |
| 18 | + propDefinition: [ |
| 19 | + app, |
| 20 | + "key", |
| 21 | + ({ dataStore }) => ({ |
| 22 | + dataStore, |
| 23 | + }), |
| 24 | + ], |
| 25 | + description: "Select the key for the record you'd like to update the expiration time.", |
| 26 | + }, |
| 27 | + ttlOption: { |
| 28 | + type: "string", |
| 29 | + label: "Expiration Type", |
| 30 | + description: "Choose a common expiration time or specify a custom value", |
| 31 | + options: [ |
| 32 | + { |
| 33 | + label: "Custom value", |
| 34 | + value: "custom", |
| 35 | + }, |
| 36 | + { |
| 37 | + label: "No expiration (remove expiry)", |
| 38 | + value: "0", |
| 39 | + }, |
| 40 | + { |
| 41 | + label: "1 hour", |
| 42 | + value: "3600", |
| 43 | + }, |
| 44 | + { |
| 45 | + label: "1 day", |
| 46 | + value: "86400", |
| 47 | + }, |
| 48 | + { |
| 49 | + label: "1 week", |
| 50 | + value: "604800", |
| 51 | + }, |
| 52 | + { |
| 53 | + label: "30 days", |
| 54 | + value: "2592000", |
| 55 | + }, |
| 56 | + { |
| 57 | + label: "90 days", |
| 58 | + value: "7776000", |
| 59 | + }, |
| 60 | + { |
| 61 | + label: "1 year", |
| 62 | + value: "31536000", |
| 63 | + }, |
| 64 | + ], |
| 65 | + reloadProps: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + async additionalProps() { |
| 69 | + const props = {}; |
| 70 | + if (this.ttlOption === "custom") { |
| 71 | + props.ttl = { |
| 72 | + type: "integer", |
| 73 | + label: "Custom TTL (seconds)", |
| 74 | + description: "The number of seconds until this record expires and is automatically deleted. Use 0 to remove expiration.", |
| 75 | + min: 0, |
| 76 | + max: 63072000, // 2 years (safe upper limit) |
| 77 | + }; |
| 78 | + } |
| 79 | + return props; |
| 80 | + }, |
| 81 | + async run({ $ }) { |
| 82 | + const { |
| 83 | + key, ttlOption, ttl, |
| 84 | + } = this; |
| 85 | + |
| 86 | + // Determine TTL value to use |
| 87 | + const ttlValue = ttlOption === "custom" |
| 88 | + ? ttl |
| 89 | + : parseInt(ttlOption, 10); |
| 90 | + |
| 91 | + if (!await this.dataStore.has(key)) { |
| 92 | + $.export("$summary", `No record found with key \`${key}\`.`); |
| 93 | + return { |
| 94 | + success: false, |
| 95 | + message: `No record found with key ${key}`, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + if (ttlValue === 0) { |
| 100 | + // Remove expiration |
| 101 | + await this.dataStore.setTtl(key, null); |
| 102 | + $.export("$summary", `Successfully removed expiration for key \`${key}\`.`); |
| 103 | + return { |
| 104 | + success: true, |
| 105 | + key, |
| 106 | + ttl: null, |
| 107 | + message: "Expiration removed", |
| 108 | + }; |
| 109 | + } else { |
| 110 | + // Update TTL |
| 111 | + await this.dataStore.setTtl(key, ttlValue); |
| 112 | + $.export("$summary", `Successfully updated expiration for key \`${key}\` (expires in ${this.app.formatTtl(ttlValue)}).`); |
| 113 | + return { |
| 114 | + success: true, |
| 115 | + key, |
| 116 | + ttl: ttlValue, |
| 117 | + ttlFormatted: this.app.formatTtl(ttlValue), |
| 118 | + }; |
| 119 | + } |
| 120 | + }, |
| 121 | +}; |
0 commit comments