-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from dolthub/taylor/file-upload
Add file upload
- Loading branch information
Showing
86 changed files
with
3,883 additions
and
60 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { registerEnumType } from "@nestjs/graphql"; | ||
|
||
export enum ImportOperation { | ||
// Create, | ||
// Overwrite, | ||
Update, | ||
// Replace, | ||
} | ||
|
||
registerEnumType(ImportOperation, { name: "ImportOperation" }); | ||
|
||
export enum FileType { | ||
Csv, | ||
Psv, | ||
// Xlsx, | ||
// Json, | ||
// Sql, | ||
// Any, | ||
} | ||
|
||
registerEnumType(FileType, { name: "FileType" }); | ||
|
||
export enum LoadDataModifier { | ||
Ignore, | ||
Replace, | ||
} | ||
|
||
registerEnumType(LoadDataModifier, { name: "LoadDataModifier" }); |
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,71 @@ | ||
import { Args, ArgsType, Field, Mutation, Resolver } from "@nestjs/graphql"; | ||
import { ReadStream } from "fs"; | ||
import { GraphQLUpload } from "graphql-upload"; | ||
import * as mysql from "mysql2/promise"; | ||
import { | ||
DataSourceService, | ||
useDBStatement, | ||
} from "../dataSources/dataSource.service"; | ||
import { TableArgs } from "../utils/commonTypes"; | ||
import { FileType, ImportOperation, LoadDataModifier } from "./table.enum"; | ||
import { Table } from "./table.model"; | ||
import { getLoadDataQuery } from "./table.queries"; | ||
|
||
export interface FileUpload { | ||
filename: string; | ||
mimetype: string; | ||
encoding: string; | ||
createReadStream: () => ReadStream; | ||
} | ||
|
||
@ArgsType() | ||
class TableImportArgs extends TableArgs { | ||
@Field(_type => ImportOperation) | ||
importOp: ImportOperation; | ||
|
||
@Field(_type => FileType) | ||
fileType: FileType; | ||
|
||
@Field(() => GraphQLUpload) | ||
file: Promise<FileUpload>; | ||
|
||
@Field(_type => LoadDataModifier, { nullable: true }) | ||
modifier?: LoadDataModifier; | ||
} | ||
|
||
@Resolver(_of => Table) | ||
export class FileUploadResolver { | ||
constructor(private readonly dss: DataSourceService) {} | ||
|
||
@Mutation(_returns => Boolean) | ||
async loadDataFile(@Args() args: TableImportArgs): Promise<boolean> { | ||
const conn = await mysql.createConnection(this.dss.getMySQLConfig()); | ||
|
||
let isDolt = false; | ||
try { | ||
const res = await conn.query("SELECT dolt_version()"); | ||
isDolt = !!res; | ||
} catch (_) { | ||
// ignore | ||
} | ||
|
||
await conn.query(useDBStatement(args.databaseName, args.refName, isDolt)); | ||
await conn.query("SET GLOBAL local_infile=ON;"); | ||
|
||
const { createReadStream, filename } = await args.file; | ||
|
||
await conn.query({ | ||
sql: getLoadDataQuery( | ||
filename, | ||
args.tableName, | ||
args.fileType, | ||
args.modifier, | ||
), | ||
infileStreamFactory: createReadStream, | ||
}); | ||
|
||
conn.destroy(); | ||
|
||
return true; | ||
} | ||
} |
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,44 @@ | ||
.container { | ||
@apply block relative pl-8 mb-2 text-primary font-semibold cursor-pointer select-none; | ||
|
||
input { | ||
@apply absolute opacity-0; | ||
} | ||
} | ||
|
||
.disabled { | ||
@apply text-ld-darkgrey cursor-default; | ||
} | ||
|
||
.checkmark { | ||
@apply absolute top-0 left-0 bg-white rounded-full mt-1 border border-primary w-4 h-4; | ||
} | ||
|
||
.container:hover input ~ .checkmark { | ||
@apply border-acc-linkblue; | ||
} | ||
|
||
.container input:checked ~ .checkmark { | ||
@apply bg-white; | ||
} | ||
.container input:focus ~ .checkmark { | ||
@apply widget-shadow-lightblue; | ||
} | ||
|
||
.container input:disabled ~ .checkmark, | ||
.container:hover input:disabled ~ .checkmark { | ||
@apply border-ld-darkgrey; | ||
} | ||
|
||
.checkmark:after { | ||
@apply absolute hidden; | ||
content: ""; | ||
} | ||
|
||
.container .checkmark:after { | ||
@apply rounded-full bg-white top-[3px] left-[3px] w-2 h-2; | ||
} | ||
|
||
.container input:checked ~ .checkmark:after { | ||
@apply block bg-primary; | ||
} |
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,50 @@ | ||
import { setup } from "@lib/testUtils.test"; | ||
import { screen } from "@testing-library/react"; | ||
import CustomRadio from "."; | ||
|
||
describe("test CustomCheckbox", () => { | ||
const mocks = [ | ||
{ name: "one", label: "one-label" }, | ||
{ name: "two", label: "two-label" }, | ||
{ name: "three", label: "three-label" }, | ||
]; | ||
|
||
mocks.forEach((mock, ind) => { | ||
it(`renders CustomRadio for of label ${mock.label}`, async () => { | ||
const checked = ind % 2 === 0; | ||
const disabled = ind === 2; | ||
const onChangeValue = jest.fn(); | ||
|
||
const { user } = setup( | ||
<CustomRadio | ||
{...mock} | ||
onChange={onChangeValue} | ||
checked={checked} | ||
className="classname" | ||
disabled={disabled} | ||
> | ||
{mock.label} | ||
</CustomRadio>, | ||
); | ||
const content = screen.getByLabelText(mock.label); | ||
expect(content).toBeVisible(); | ||
if (!disabled) { | ||
const input = screen.getByRole("radio"); | ||
if (checked) { | ||
expect(input).toBeChecked(); | ||
} else { | ||
expect(input).not.toBeChecked(); | ||
} | ||
|
||
await user.click(screen.getByLabelText(mock.label)); | ||
if (checked) { | ||
expect(onChangeValue).not.toHaveBeenCalled(); | ||
} else { | ||
expect(onChangeValue).toHaveBeenCalled(); | ||
} | ||
} else { | ||
expect(content).toBeDisabled(); | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.