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) {
const tenantId = tenancy.isMultiTenant() ? tenancy.getTenantId() : null
const baseAppId = generateAppID(tenantId)
const appId = generateDevAppID(baseAppId)
return await context.doInAppContext(appId, async () => {
const db = context.getAppDB()
await db.put({
_id: "_design/database",
// view collation information, read before writing any complex views:
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
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 }
async function createInstance(
appId: string,
template: any,
includeSampleData: boolean
) {
const db = context.getAppDB()
await db.put({
_id: "_design/database",
// view collation information, read before writing any complex views:
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
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 }
}
async function addDefaultTables(db: Database) {
@ -250,82 +249,90 @@ async function performAppCreate(ctx: BBContext) {
instanceConfig.file = ctx.request.files.templateFile
}
const includeSampleData = isQsTrue(ctx.request.body.sampleData)
const instance = await createInstance(instanceConfig, includeSampleData)
const appId = instance._id
const db = context.getAppDB()
const tenantId = tenancy.isMultiTenant() ? tenancy.getTenantId() : null
const appId = generateDevAppID(generateAppID(tenantId))
let newApplication: App = {
_id: DocumentType.APP_METADATA,
_rev: undefined,
appId,
type: "app",
version: packageJson.version,
componentLibraries: ["@budibase/standard-components"],
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",
},
}
return await context.doInAppContext(appId, async () => {
const instance = await createInstance(
appId,
instanceConfig,
includeSampleData
)
const db = context.getAppDB()
// If we used a template or imported an app there will be an existing doc.
// 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]
}
})
// Migrate navigation settings and screens if required
if (existing) {
const navigation = await migrateAppNavigation()
if (navigation) {
newApplication.navigation = navigation
}
let newApplication: App = {
_id: DocumentType.APP_METADATA,
_rev: undefined,
appId,
type: "app",
version: packageJson.version,
componentLibraries: ["@budibase/standard-components"],
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",
},
}
} catch (err) {
// Nothing to do
}
const response = await db.put(newApplication, { force: true })
newApplication._rev = response.rev
// If we used a template or imported an app there will be an existing doc.
// 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 */
if (!env.isTest()) {
await createApp(appId)
}
// Migrate navigation settings and screens if required
if (existing) {
const navigation = await migrateAppNavigation()
if (navigation) {
newApplication.navigation = navigation
}
}
} catch (err) {
// Nothing to do
}
await cache.app.invalidateAppMetadata(appId, newApplication)
return newApplication
const response = await db.put(newApplication, { force: true })
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) {