Skip to content

Commit

Permalink
feat: add schema and is_schema options to db create method (#15)
Browse files Browse the repository at this point in the history
* feat: add schema and is_schema options to db create method

* feat: add new fields to formatted response

* feat: throw error if is_schema nd schema used
  • Loading branch information
notrab authored Apr 3, 2024
1 parent 4873515 commit 9bbe95b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/database.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it, expect, vi, beforeEach } from "vitest";

import { DatabaseClient } from "./database";

vi.mock("./client", () => ({
TursoClient: { request: vi.fn() },
}));

describe("DatabaseClient", () => {
let client: DatabaseClient;

beforeEach(() => {
client = new DatabaseClient({ org: "turso", token: "abc" });
vi.resetAllMocks();
});

it("should throw an error when both is_schema and schema are provided", async () => {
await expect(
client.create("test", {
is_schema: true,
schema: "test",
} as unknown as (typeof client.create.arguments)[1])
).rejects.toThrow("'is_schema' and 'schema' cannot both be provided");
});
});
22 changes: 21 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export interface Database {
type: string;
version: string;
group?: string;
sleeping: boolean;
allow_attach: boolean;
block_reads: boolean;
block_writes: boolean;
schema?: string;
is_schema: boolean;
}

export interface ApiDatabaseResponse
Expand Down Expand Up @@ -64,6 +70,10 @@ export interface DatabaseInstance {
hostname: string;
}

type MultiDBSchemaOptions =
| { is_schema: boolean; schema?: never }
| { is_schema?: never; schema: string };

export class DatabaseClient {
constructor(private config: TursoConfig) {}

Expand Down Expand Up @@ -94,8 +104,12 @@ export class DatabaseClient {
url?: string;
timestamp?: string | Date;
};
}
} & MultiDBSchemaOptions
): Promise<DatabaseCreateResponse> {
if (options?.is_schema !== undefined && options?.schema !== undefined) {
throw new Error("'is_schema' and 'schema' cannot both be provided");
}

if (options?.seed) {
if (options.seed.type === "database" && !options.seed.name) {
throw new Error("Seed name is required when type is 'database'");
Expand Down Expand Up @@ -250,6 +264,12 @@ export class DatabaseClient {
type: db.type,
version: db.version,
group: db.group,
sleeping: db.sleeping,
allow_attach: db.allow_attach,
block_reads: db.block_reads,
block_writes: db.block_writes,
schema: db.schema,
is_schema: db.is_schema,
};
}

Expand Down

0 comments on commit 9bbe95b

Please sign in to comment.