Skip to content

Commit

Permalink
script enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
harishv7 committed Dec 24, 2024
1 parent 539a9c1 commit 03319e7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
7 changes: 3 additions & 4 deletions apps/studio/prisma/scripts/moh-tosp/backupCollectionById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function backupCollection(

// Parse blob content and write to a file
const blobBuffer = blob.content // Assuming blob.content is a buffer
const blobJsonPath = path.join(backupDir, `${child.title}.json`)
const blobJsonPath = path.join(backupDir, `${child.permalink}.json`)
await fs.writeFile(blobJsonPath, JSON.stringify(blobBuffer, null, 2))
}

Expand All @@ -72,9 +72,8 @@ export async function backupCollection(

// Run the backup
// NOTE: TODO: Put in the collection ID to backup
const collectionId = "5"
const backupDirectory =
"/Users/harishv/Documents/Code/isomer/isomer-next/test-backup-tosp/backup"
const collectionId = "0"
const backupDirectory = "/Users/XYZ/<your-path>"

await backupCollection(collectionId, backupDirectory).catch((err) => {
if (err instanceof Error) {
Expand Down
36 changes: 25 additions & 11 deletions apps/studio/prisma/scripts/moh-tosp/createCollectionFromLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ const logger = new FileLogger("./createCollectionFromLocal.log")
export const createCollectionFromLocal = async (
contentDir: string,
siteId: number,
indexPageName: string, // should be placed outside the folder
indexPageTitle: string, // title of the index page
collectionName: string,
nameOfNewCollectionToCreate: string,
) => {
logger.info(`Reading from ${contentDir}`)
const jsonFilePath = path.join(contentDir, "cost-financing.json")
const folderPath = path.join(contentDir, "cost-financing-original")
const jsonFilePath = path.join(contentDir, indexPageName)
const folderPath = path.join(contentDir, collectionName)

try {
await db.transaction().execute(async (tx) => {
// Step 1: Create a new collection with title "cost-financing-new"
const collection = await tx
.insertInto("Resource")
.values({
title: "cost-financing-new",
permalink: "cost-financing-new",
title: nameOfNewCollectionToCreate,
permalink: nameOfNewCollectionToCreate,
siteId: siteId,
type: "Collection",
state: "Draft",
Expand All @@ -35,7 +39,7 @@ export const createCollectionFromLocal = async (
const collectionId = collection.id
logger.info(`Collection created with ID: ${collectionId}`)

// Step 2: Insert "cost-financing.json" as an IndexPage with permalink "_index"\
// Step 2: Insert "cost-financing.json" as an IndexPage with permalink "_index"
const jsonFileContent = await fs.readFile(jsonFilePath, "utf-8")
const indexPageBlob = await tx
.insertInto("Blob")
Expand All @@ -48,7 +52,7 @@ export const createCollectionFromLocal = async (
const indexPage = await tx
.insertInto("Resource")
.values({
title: "cost-financing-new",
title: nameOfNewCollectionToCreate,
permalink: "_index",
siteId: siteId,
type: "IndexPage",
Expand Down Expand Up @@ -106,7 +110,7 @@ export const createCollectionFromLocal = async (
.values({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
title: parsedFileContent.page.title,
permalink: file,
permalink: file.replace(/\.json$/, ""), // remove the .json at the back on permalinks
siteId: siteId, // Replace with appropriate site ID
type: "CollectionPage",
parentId: collectionId,
Expand Down Expand Up @@ -135,7 +139,17 @@ export const createCollectionFromLocal = async (
}

// NOTE: TODO: Update the content directory and siteId here before usage!
const contentDir =
"/Users/harishv/Documents/Code/isomer/isomer-next/test-backup-tosp/content"
const siteId = 1
await createCollectionFromLocal(contentDir, siteId)
const contentDir = "/Users/XYZ/<your-path>"
const indexPagePath = "cost-financing.json"
const indexPageTitle = "Cost financing"
const collectionName = "cost-financing"
const nameOfNewCollectionToCreate = "cost-financing-new" // will also be the permalink
const siteId = 0
await createCollectionFromLocal(
contentDir,
siteId,
indexPagePath,
indexPageTitle,
collectionName,
nameOfNewCollectionToCreate,
)
4 changes: 2 additions & 2 deletions apps/studio/prisma/scripts/moh-tosp/deleteCollectionById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ export const deleteCollectionById = async (
}
}

const collectionIdToDelete = "7249"
const siteId = 1
const collectionIdToDelete = "0"
const siteId = 0
await deleteCollectionById(collectionIdToDelete, siteId)
4 changes: 2 additions & 2 deletions apps/studio/prisma/scripts/moh-tosp/publishDraftCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ export const publishCollectionById = async (
}

// NOTE: TODO: Put in the publisher ID and collection ID to publish
const publisherId = "mblrtd177gju657mf20m32jo"
const collectionId = "7249"
const publisherId = "xyz"
const collectionId = "0"
await publishCollectionById(publisherId, collectionId)

0 comments on commit 03319e7

Please sign in to comment.