Fixing issue with apps not being created correctly due to the recent update to context.

This commit is contained in:
mike12345567 2023-01-27 13:37:36 +00:00
parent 4ed239e5fd
commit 6e3f87c798
1 changed files with 116 additions and 109 deletions

View File

@ -112,46 +112,45 @@ function checkAppName(
} }
} }
async function createInstance(template: any, includeSampleData: boolean) { async function createInstance(
const tenantId = tenancy.isMultiTenant() ? tenancy.getTenantId() : null appId: string,
const baseAppId = generateAppID(tenantId) template: any,
const appId = generateDevAppID(baseAppId) includeSampleData: boolean
return await context.doInAppContext(appId, async () => { ) {
const db = context.getAppDB() const db = context.getAppDB()
await db.put({ await db.put({
_id: "_design/database", _id: "_design/database",
// view collation information, read before writing any complex views: // view collation information, read before writing any complex views:
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification // https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
views: {}, views: {},
})
// NOTE: indexes need to be created before any tables/templates
// add view for linked rows
await createLinkView()
await createRoutingView()
await createAllSearchIndex()
// replicate the template data to the instance DB
// this is currently very hard to test, downloading and importing template files
if (template && template.templateString) {
const { ok } = await db.load(stringToReadStream(template.templateString))
if (!ok) {
throw "Error loading database dump from memory."
}
} else if (template && template.useTemplate === "true") {
await sdk.backups.importApp(appId, db, template)
} else {
// create the users table
await db.put(USERS_TABLE_SCHEMA)
if (includeSampleData) {
// create ootb stock db
await addDefaultTables(db)
}
}
return { _id: appId }
}) })
// NOTE: indexes need to be created before any tables/templates
// add view for linked rows
await createLinkView()
await createRoutingView()
await createAllSearchIndex()
// replicate the template data to the instance DB
// this is currently very hard to test, downloading and importing template files
if (template && template.templateString) {
const { ok } = await db.load(stringToReadStream(template.templateString))
if (!ok) {
throw "Error loading database dump from memory."
}
} else if (template && template.useTemplate === "true") {
await sdk.backups.importApp(appId, db, template)
} else {
// create the users table
await db.put(USERS_TABLE_SCHEMA)
if (includeSampleData) {
// create ootb stock db
await addDefaultTables(db)
}
}
return { _id: appId }
} }
async function addDefaultTables(db: Database) { async function addDefaultTables(db: Database) {
@ -250,82 +249,90 @@ async function performAppCreate(ctx: BBContext) {
instanceConfig.file = ctx.request.files.templateFile instanceConfig.file = ctx.request.files.templateFile
} }
const includeSampleData = isQsTrue(ctx.request.body.sampleData) const includeSampleData = isQsTrue(ctx.request.body.sampleData)
const instance = await createInstance(instanceConfig, includeSampleData) const tenantId = tenancy.isMultiTenant() ? tenancy.getTenantId() : null
const appId = instance._id const appId = generateDevAppID(generateAppID(tenantId))
const db = context.getAppDB()
let newApplication: App = { return await context.doInAppContext(appId, async () => {
_id: DocumentType.APP_METADATA, const instance = await createInstance(
_rev: undefined, appId,
appId, instanceConfig,
type: "app", includeSampleData
version: packageJson.version, )
componentLibraries: ["@budibase/standard-components"], const db = context.getAppDB()
name: name,
url: url,
template: templateKey,
instance,
tenantId: tenancy.getTenantId(),
updatedAt: new Date().toISOString(),
createdAt: new Date().toISOString(),
status: AppStatus.DEV,
navigation: {
navigation: "Top",
title: name,
navWidth: "Large",
navBackground: "var(--spectrum-global-color-gray-100)",
links: [
{
url: "/home",
text: "Home",
},
],
},
theme: "spectrum--light",
customTheme: {
buttonBorderRadius: "16px",
},
}
// If we used a template or imported an app there will be an existing doc. let newApplication: App = {
// Fetch and migrate some metadata from the existing app. _id: DocumentType.APP_METADATA,
try { _rev: undefined,
const existing: App = await db.get(DocumentType.APP_METADATA) appId,
const keys: (keyof App)[] = [ type: "app",
"_rev", version: packageJson.version,
"navigation", componentLibraries: ["@budibase/standard-components"],
"theme", name: name,
"customTheme", url: url,
"icon", template: templateKey,
] instance,
keys.forEach(key => { tenantId: tenancy.getTenantId(),
if (existing[key]) { updatedAt: new Date().toISOString(),
// @ts-ignore createdAt: new Date().toISOString(),
newApplication[key] = existing[key] status: AppStatus.DEV,
} navigation: {
}) navigation: "Top",
title: name,
// Migrate navigation settings and screens if required navWidth: "Large",
if (existing) { navBackground: "var(--spectrum-global-color-gray-100)",
const navigation = await migrateAppNavigation() links: [
if (navigation) { {
newApplication.navigation = navigation url: "/home",
} text: "Home",
},
],
},
theme: "spectrum--light",
customTheme: {
buttonBorderRadius: "16px",
},
} }
} catch (err) {
// Nothing to do
}
const response = await db.put(newApplication, { force: true }) // If we used a template or imported an app there will be an existing doc.
newApplication._rev = response.rev // Fetch and migrate some metadata from the existing app.
try {
const existing: App = await db.get(DocumentType.APP_METADATA)
const keys: (keyof App)[] = [
"_rev",
"navigation",
"theme",
"customTheme",
"icon",
]
keys.forEach(key => {
if (existing[key]) {
// @ts-ignore
newApplication[key] = existing[key]
}
})
/* istanbul ignore next */ // Migrate navigation settings and screens if required
if (!env.isTest()) { if (existing) {
await createApp(appId) const navigation = await migrateAppNavigation()
} if (navigation) {
newApplication.navigation = navigation
}
}
} catch (err) {
// Nothing to do
}
await cache.app.invalidateAppMetadata(appId, newApplication) const response = await db.put(newApplication, { force: true })
return newApplication newApplication._rev = response.rev
/* istanbul ignore next */
if (!env.isTest()) {
await createApp(appId)
}
await cache.app.invalidateAppMetadata(appId, newApplication)
return newApplication
})
} }
async function creationEvents(request: any, app: App) { async function creationEvents(request: any, app: App) {