Skip to content

Commit

Permalink
Merge pull request #53 from dolthub/taylor/save-db
Browse files Browse the repository at this point in the history
Save connections
  • Loading branch information
tbantle22 authored Nov 27, 2023
2 parents df8ae5d + b05fd76 commit d7da2d7
Show file tree
Hide file tree
Showing 29 changed files with 542 additions and 447 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ images/
**/*.test.tsx

**/.yarn/cache
**/.yarn/install-state.gz
**/.yarn/install-state.gz
**/store
119 changes: 0 additions & 119 deletions DockerREADME.md

This file was deleted.

2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ FROM --platform=linux/amd64 node:18.13.0-alpine3.17 as graphql-base
FROM graphql-base AS graphql-deps
WORKDIR /app/graphql-server

VOLUME /app/graphql-server/store

# Copy the package.json and yarn files
COPY --chown=node:node graphql-server/.yarn ./.yarn
COPY --chown=node:node graphql-server/yarn.lock graphql-server/package.json graphql-server/.yarnrc.yml ./
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ Navigate to http://localhost:3000 to enter your database information.
You can find more in-depth instructions on
[DockerHub](https://hub.docker.com/r/dolthub/dolt-workbench).

### Saving connection information between runs

If you want to save connection information between Docker runs, you can mount a local
directory to the `store` directory in `/app/graphql-server` in the container.

```
% docker run -p 9002:9002 -p 3000:3000 -v ~/path/to/store:/app/graphql-server/store dolthub/dolt-workbench:latest
```

## Getting started from source

First, clone this repository.
Expand Down
1 change: 1 addition & 0 deletions graphql-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ lerna-debug.log*
!.yarn/sdks
!.yarn/versions
node_modules
store
12 changes: 10 additions & 2 deletions graphql-server/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ type CommitList {
list: [Commit!]!
}

type DatabaseConnection {
connectionUrl: String!
name: String!
hideDoltFeatures: Boolean
useSSL: Boolean
}

type DoltDatabaseDetails {
isDolt: Boolean!
hideDoltFeatures: Boolean!
Expand Down Expand Up @@ -266,7 +273,7 @@ type Query {
defaultBranch(databaseName: String!): Branch
commits(offset: Int, databaseName: String!, refName: String, afterCommitId: String, twoDot: Boolean, excludingCommitsFromRefName: String): CommitList!
currentDatabase: String
hasDatabaseEnv: Boolean!
storedConnections: [DatabaseConnection!]!
databases: [String!]!
doltDatabaseDetails: DoltDatabaseDetails!
diffStat(databaseName: String!, fromRefName: String!, toRefName: String!, refName: String, type: CommitDiffType, tableName: String): DiffStat!
Expand Down Expand Up @@ -317,7 +324,8 @@ enum DiffRowType {
type Mutation {
createBranch(databaseName: String!, newBranchName: String!, fromRefName: String!): Branch!
deleteBranch(databaseName: String!, branchName: String!): Boolean!
addDatabaseConnection(url: String, useEnv: Boolean, hideDoltFeatures: Boolean, useSSL: Boolean): String
addDatabaseConnection(connectionUrl: String!, name: String!, hideDoltFeatures: Boolean, useSSL: Boolean): String
removeDatabaseConnection(name: String!): Boolean!
createDatabase(databaseName: String!): Boolean!
resetDatabase: Boolean!
loadDataFile(tableName: String!, refName: String!, databaseName: String!, importOp: ImportOperation!, fileType: FileType!, file: Upload!, modifier: LoadDataModifier): Boolean!
Expand Down
4 changes: 2 additions & 2 deletions graphql-server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { GraphQLModule } from "@nestjs/graphql";
import { TerminusModule } from "@nestjs/terminus";
import { DataSourceModule } from "./dataSources/dataSource.module";
import { FileStoreModule } from "./fileStore/fileStore.module";
import resolvers from "./resolvers";

@Module({
imports: [
ConfigModule.forRoot({ envFilePath: ".development.env" }),
GraphQLModule.forRoot<ApolloDriverConfig>({
autoSchemaFile: "schema.gql",
context: ctx => ctx,
driver: ApolloDriver,
}),
DataSourceModule,
FileStoreModule,
TerminusModule,
],
providers: resolvers,
Expand Down
16 changes: 16 additions & 0 deletions graphql-server/src/databases/database.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Field, ObjectType } from "@nestjs/graphql";

@ObjectType()
export class DatabaseConnection {
@Field()
connectionUrl: string;

@Field()
name: string;

@Field({ nullable: true })
hideDoltFeatures?: boolean;

@Field({ nullable: true })
useSSL?: boolean;
}
65 changes: 36 additions & 29 deletions graphql-server/src/databases/database.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ConfigService } from "@nestjs/config";
import {
Args,
ArgsType,
Expand All @@ -9,15 +8,17 @@ import {
Resolver,
} from "@nestjs/graphql";
import { DataSourceService } from "../dataSources/dataSource.service";
import { FileStoreService } from "../fileStore/fileStore.service";
import { DBArgs } from "../utils/commonTypes";
import { DatabaseConnection } from "./database.model";

@ArgsType()
class AddDatabaseConnectionArgs {
@Field({ nullable: true })
url?: string;
@Field()
connectionUrl: string;

@Field({ nullable: true })
useEnv?: boolean;
@Field()
name: string;

@Field({ nullable: true })
hideDoltFeatures?: boolean;
Expand All @@ -35,11 +36,17 @@ class DoltDatabaseDetails {
hideDoltFeatures: boolean;
}

@Resolver()
@ArgsType()
class RemoveDatabaseConnectionArgs {
@Field()
name: string;
}

@Resolver(_of => DatabaseConnection)
export class DatabaseResolver {
constructor(
private readonly dss: DataSourceService,
private readonly configService: ConfigService,
private readonly fileStoreService: FileStoreService,
) {}

@Query(_returns => String, { nullable: true })
Expand All @@ -53,9 +60,9 @@ export class DatabaseResolver {
}
}

@Query(_returns => Boolean)
async hasDatabaseEnv(): Promise<boolean> {
return !!this.configService.get("DATABASE_URL");
@Query(_returns => [DatabaseConnection])
async storedConnections(): Promise<DatabaseConnection[]> {
return this.fileStoreService.getStore();
}

@Query(_returns => [String])
Expand Down Expand Up @@ -93,31 +100,31 @@ export class DatabaseResolver {
async addDatabaseConnection(
@Args() args: AddDatabaseConnectionArgs,
): Promise<string | undefined> {
if (args.useEnv) {
const url = this.configService.get("DATABASE_URL");
if (!url) throw new Error("DATABASE_URL not found in env");
const hideDoltFeatures = this.configService.get("HIDE_DOLT_FEATURES");
const useSSL = this.configService.get("USE_SSL");
await this.dss.addDS({
connectionUrl: url,
hideDoltFeatures: !!hideDoltFeatures && hideDoltFeatures === "true",
useSSL: useSSL !== undefined ? useSSL === "true" : true,
});
} else if (args.url) {
await this.dss.addDS({
connectionUrl: args.url,
hideDoltFeatures: !!args.hideDoltFeatures,
useSSL: !!args.useSSL,
});
} else {
throw new Error("database url not provided");
}
const workbenchConfig = {
connectionUrl: args.connectionUrl,
hideDoltFeatures: !!args.hideDoltFeatures,
useSSL: !!args.useSSL,
};
await this.dss.addDS(workbenchConfig);

this.fileStoreService.addItemToStore({
...workbenchConfig,
name: args.name,
});

const db = await this.currentDatabase();
if (!db) return undefined;
return db;
}

@Mutation(_returns => Boolean)
async removeDatabaseConnection(
@Args() args: RemoveDatabaseConnectionArgs,
): Promise<boolean> {
this.fileStoreService.removeItemFromStore(args.name);
return true;
}

@Mutation(_returns => Boolean)
async createDatabase(@Args() args: DBArgs): Promise<boolean> {
const qr = this.dss.getQR();
Expand Down
5 changes: 5 additions & 0 deletions graphql-server/src/fileStore/fileStore.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Module } from "@nestjs/common";
import { FileStoreService } from "./fileStore.service";

@Module({ providers: [FileStoreService], exports: [FileStoreService] })
export class FileStoreModule {}
Loading

0 comments on commit d7da2d7

Please sign in to comment.