|
| 1 | +import assert from "node:assert"; |
| 2 | +import path from "node:path"; |
| 3 | +import { type CreateField, connect, type FieldDataEntryInput, type FieldDataInput } from "framer-api"; |
| 4 | +import { type FieldType, loadCsv } from "./load-csv.ts"; |
| 5 | + |
| 6 | +// Configuration |
| 7 | + |
| 8 | +const projectUrl = process.env["EXAMPLE_PROJECT_URL"]; |
| 9 | +assert(projectUrl, "EXAMPLE_PROJECT_URL environment variable is required"); |
| 10 | + |
| 11 | +const csvPath = process.env["CSV_PATH"] ?? path.join(import.meta.dirname, "../data/sample-products.csv"); |
| 12 | +const collectionName = process.env["COLLECTION_NAME"] ?? "Products"; |
| 13 | + |
| 14 | +const { columns, rows, fieldTypes } = loadCsv(csvPath); |
| 15 | + |
| 16 | +assert(columns.includes("slug"), "CSV must contain a 'slug' column"); |
| 17 | + |
| 18 | +// The `using` keyword is used to ensure that the Framer client is closed after the block is executed. |
| 19 | +// If you don't use the `using` keyword, you need to manually close the client using `await framer.disconnect()`. |
| 20 | +// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using |
| 21 | +using framer = await connect(projectUrl); |
| 22 | + |
| 23 | +// Find or Create Collection |
| 24 | + |
| 25 | +const existingCollections = await framer.getCollections(); |
| 26 | +let collection = existingCollections.find((c) => c.name === collectionName); |
| 27 | + |
| 28 | +if (!collection) { |
| 29 | + collection = await framer.createCollection(collectionName); |
| 30 | +} |
| 31 | + |
| 32 | +// Add Missing Fields |
| 33 | + |
| 34 | +const existingFields = await collection.getFields(); |
| 35 | +const existingFieldNames = new Set(existingFields.map((f) => f.name.toLowerCase())); |
| 36 | + |
| 37 | +const fieldsToCreate = columns |
| 38 | + .filter((column) => column !== "slug" && !existingFieldNames.has(column.toLowerCase())) |
| 39 | + .map( |
| 40 | + (column): CreateField => ({ |
| 41 | + type: fieldTypes.get(column) ?? "string", |
| 42 | + name: column, |
| 43 | + }), |
| 44 | + ); |
| 45 | + |
| 46 | +if (fieldsToCreate.length > 0) { |
| 47 | + await collection.addFields(fieldsToCreate); |
| 48 | +} |
| 49 | + |
| 50 | +// Build Items & Import |
| 51 | + |
| 52 | +const fields = await collection.getFields(); |
| 53 | +const fieldNameToId = new Map(fields.map((f) => [f.name.toLowerCase(), f.id])); |
| 54 | + |
| 55 | +const existingItems = await collection.getItems(); |
| 56 | +const slugToExistingId = new Map(existingItems.map((item) => [item.slug, item.id])); |
| 57 | + |
| 58 | +const items = rows.map((row) => { |
| 59 | + const fieldData: FieldDataInput = {}; |
| 60 | + |
| 61 | + for (const column of columns) { |
| 62 | + if (column === "slug") continue; |
| 63 | + |
| 64 | + const fieldId = fieldNameToId.get(column.toLowerCase()); |
| 65 | + if (!fieldId) continue; |
| 66 | + |
| 67 | + const value = row[column] ?? ""; |
| 68 | + const fieldType = fieldTypes.get(column) ?? "string"; |
| 69 | + fieldData[fieldId] = toFieldData(value, fieldType); |
| 70 | + } |
| 71 | + |
| 72 | + const slug = row["slug"]; |
| 73 | + assert(slug && slug.length > 0, "slug is required and must be non-empty"); |
| 74 | + const existingId = slugToExistingId.get(slug); |
| 75 | + |
| 76 | + return { id: existingId, slug, fieldData }; |
| 77 | +}); |
| 78 | + |
| 79 | +await collection.addItems(items); |
| 80 | + |
| 81 | +console.log(`Imported ${items.length} items`); |
| 82 | + |
| 83 | +function toFieldData(value: string, type: FieldType): FieldDataEntryInput { |
| 84 | + switch (type) { |
| 85 | + case "boolean": |
| 86 | + return { type: "boolean" as const, value: value.toLowerCase() === "true" }; |
| 87 | + case "number": |
| 88 | + return { type: "number" as const, value: parseFloat(value) || 0 }; |
| 89 | + case "string": |
| 90 | + return { type: "string" as const, value }; |
| 91 | + } |
| 92 | +} |
0 commit comments