-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): support database export and import (#1540)
* feat: support cli export db * feat: support database import and export
- Loading branch information
Showing
19 changed files
with
1,589 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { databaseControllerExport, databaseControllerImport } from "../../api/custom"; | ||
import { AppSchema } from "../../schema/app"; | ||
import * as fs from 'fs' | ||
import { getEmoji } from "../../util/print"; | ||
import * as path from 'path' | ||
import { exist } from "../../util/file"; | ||
import * as FormData from 'form-data' | ||
|
||
export async function exportDB(exportPath: string): Promise<void> { | ||
const appSchema = AppSchema.read() | ||
|
||
const res = await databaseControllerExport(appSchema.appid) | ||
// get absolute path | ||
const absPath = path.resolve(exportPath) | ||
res.pipe(fs.createWriteStream(path.join(absPath, `${appSchema.appid}-db.gz`))) | ||
console.log(`${getEmoji('✅')} database export success`) | ||
} | ||
|
||
export async function importDB(sourceAppid, importPath: string): Promise<void> { | ||
const appSchema = AppSchema.read() | ||
|
||
// get absolute path | ||
const absPath = path.resolve(importPath) | ||
if (!exist(absPath)) { | ||
console.log(`${getEmoji('❌')} import database file not exist`) | ||
return | ||
} | ||
|
||
const formData = new FormData(); | ||
formData.append('file', fs.createReadStream(absPath), 'database.gz') | ||
formData.append('sourceAppid', sourceAppid) | ||
await databaseControllerImport(appSchema.appid, formData) | ||
console.log(`${getEmoji('✅')} database import success`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { request } from "../util/request"; | ||
|
||
|
||
export async function databaseControllerExport(appid: string):Promise<any> { | ||
return request({ | ||
url: `/v1/apps/${appid}/databases/export`, | ||
method: 'GET', | ||
responseType: 'stream', | ||
}) | ||
} | ||
|
||
export async function databaseControllerImport(appid: string, data: any):Promise<any> { | ||
return request({ | ||
url: `/v1/apps/${appid}/databases/import`, | ||
method: 'PUT', | ||
data: data, | ||
headers: { | ||
'Content-Type': 'multipart/form-data' | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.