Skip to content

Commit 346a93a

Browse files
committed
feat: make schema/is_schema optional
1 parent 9bbe95b commit 346a93a

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/database.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ describe("DatabaseClient", () => {
2222
} as unknown as (typeof client.create.arguments)[1])
2323
).rejects.toThrow("'is_schema' and 'schema' cannot both be provided");
2424
});
25+
26+
it('throws error if seed type is "database" and name is missing', async () => {
27+
const options = { seed: { type: "database" } };
28+
// @ts-expect-error
29+
await expect(client.create("testDB", options)).rejects.toThrow(
30+
"Seed name is required when type is 'database'"
31+
);
32+
});
33+
34+
it('throws error if seed type is "dump" and url is missing', async () => {
35+
const options = { seed: { type: "dump" } };
36+
// @ts-expect-error
37+
await expect(client.create("testDB", options)).rejects.toThrow(
38+
"Seed URL is required when type is 'dump'"
39+
);
40+
});
2541
});

src/database.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ export interface DatabaseInstance {
7272

7373
type MultiDBSchemaOptions =
7474
| { is_schema: boolean; schema?: never }
75-
| { is_schema?: never; schema: string };
75+
| { is_schema?: never; schema: string }
76+
| {};
77+
78+
function hasIsSchemaOption(
79+
options: any
80+
): options is { is_schema: boolean; schema?: never } {
81+
return options !== undefined && options.is_schema !== undefined;
82+
}
83+
84+
function hasSchemaOption(
85+
options: any
86+
): options is { is_schema?: never; schema: string } {
87+
return options !== undefined && options.schema !== undefined;
88+
}
7689

7790
export class DatabaseClient {
7891
constructor(private config: TursoConfig) {}
@@ -106,7 +119,7 @@ export class DatabaseClient {
106119
};
107120
} & MultiDBSchemaOptions
108121
): Promise<DatabaseCreateResponse> {
109-
if (options?.is_schema !== undefined && options?.schema !== undefined) {
122+
if (hasIsSchemaOption(options) && hasSchemaOption(options)) {
110123
throw new Error("'is_schema' and 'schema' cannot both be provided");
111124
}
112125

0 commit comments

Comments
 (0)