Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make XataApiClient to use ES Proxies #1287

Merged
merged 19 commits into from
Jan 9, 2024
24 changes: 14 additions & 10 deletions cli/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
message: 'New workspace name'
});
if (!name) return this.error('No workspace name provided');
const workspace = await xata.api.workspaces.createWorkspace({ data: { name } });
const workspace = await xata.api.workspaces.createWorkspace({ name });
return workspace.id;
} else if (workspaces.workspaces.length === 1) {
const workspace = workspaces.workspaces[0].id;
Expand Down Expand Up @@ -309,7 +309,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
options: { allowCreate?: boolean } = {}
): Promise<{ name: string; region: string }> {
const xata = await this.getXataClient();
const { databases: dbs = [] } = await xata.api.database.getDatabaseList({ workspace });
const { databases: dbs = [] } = await xata.api.databases.getDatabaseList({ workspaceId: workspace });

if (dbs.length > 0) {
const choices = dbs.map((db) => ({
Expand Down Expand Up @@ -355,7 +355,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
} = {}
): Promise<string> {
const xata = await this.getXataClient();
const { branches = [] } = await xata.api.branches.getBranchList({ workspace, region, database });
const { branches = [] } = await xata.api.branch.getBranchList({ workspace, region, dbName: database });

const EMPTY_CHOICE = '$empty';
const CREATE_CHOICE = '$create';
Expand Down Expand Up @@ -421,7 +421,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
);
if (!name) return this.error('No database name provided');

const { regions } = await xata.api.database.listRegions({ workspace });
const { regions } = await xata.api.databases.listRegions({ workspaceId: workspace });
const { region } = await this.prompt(
{
type: 'select',
Expand All @@ -434,7 +434,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
);
if (!region) return this.error('No region selected');

const result = await xata.api.database.createDatabase({ workspace, database: name, data: { region } });
const result = await xata.api.databases.createDatabase({ workspaceId: workspace, dbName: name, region });

return { name: result.databaseName, region };
}
Expand All @@ -455,9 +455,9 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
});

if (!from) {
await xata.api.branches.createBranch({ workspace, region, database, branch: name });
await xata.api.branch.createBranch({ workspace, region, dbBranchName: `${database}:${name}` });
} else {
await xata.api.branches.createBranch({ workspace, region, database, branch: name, from });
await xata.api.branch.createBranch({ workspace, region, dbBranchName: `${database}:${name}`, from });
}

return name;
Expand Down Expand Up @@ -568,8 +568,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
const compare = await xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
schema
});

Expand All @@ -587,7 +586,12 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
});
if (!confirm) return this.exit(1);

await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits: compare.edits });
await xata.api.migrations.applyBranchSchemaEdit({
workspace,
region,
dbBranchName: `${database}:${branch}`,
edits: compare.edits
});
}
}

Expand Down
7 changes: 6 additions & 1 deletion cli/src/commands/branch/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export default class BranchCreate extends BaseCommand<typeof BranchCreate> {
const { from } = flags;

try {
const result = await xata.api.branches.createBranch({ workspace, region, database, branch, from });
const result = await xata.api.branch.createBranch({
workspace,
region,
dbBranchName: `${database}:${branch}`,
from
});

if (this.jsonEnabled()) return result;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/branch/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class BranchDelete extends BaseCommand<typeof BranchDelete> {
if (!confirm) return this.exit(1);
if (confirm !== branch) return this.error('The branch name did not match');

await xata.api.branches.deleteBranch({ workspace, region, database, branch });
await xata.api.branch.deleteBranch({ workspace, region, dbBranchName: `${database}:${branch}` });

if (this.jsonEnabled()) return {};

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/branch/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class BranchList extends BaseCommand<typeof BranchList> {
const { workspace, region, database } = await this.getParsedDatabaseURL(flags.db);

const xata = await this.getXataClient();
const { branches } = await xata.api.branches.getBranchList({ workspace, region, database });
const { branches } = await xata.api.branch.getBranchList({ workspace, region, dbName: database });

if (this.jsonEnabled()) return branches;

Expand Down
6 changes: 5 additions & 1 deletion cli/src/commands/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export default class Codegen extends BaseCommand<typeof Codegen> {
flags.db,
flags.branch
);
const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch });
const branchDetails = await xata.api.branch.getBranchDetails({
workspace,
region,
dbBranchName: `${database}:${branch}`
});
const { schema } = branchDetails;

const codegenBranch = flags['inject-branch'] ? branch : undefined;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/dbs/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class DatabasesDelete extends BaseCommand<typeof DatabasesDelete>
if (!confirm) return this.exit(1);
if (confirm !== database) return this.error('The database name did not match');

await xata.api.database.deleteDatabase({ workspace, database });
await xata.api.databases.deleteDatabase({ workspaceId: workspace, dbName: database });

if (this.jsonEnabled()) return {};

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/dbs/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class DatabasesList extends BaseCommand<typeof DatabasesList> {
(await this.getWorkspace());

const xata = await this.getXataClient();
const { databases: dbs = [] } = await xata.api.database.getDatabaseList({ workspace });
const { databases: dbs = [] } = await xata.api.databases.getDatabaseList({ workspaceId: workspace });

if (this.jsonEnabled()) return dbs;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/dbs/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class DatabasesRename extends BaseCommand<typeof DatabasesRename>
if (!confirm) return this.exit(1);
if (confirm !== database) return this.error('The database name did not match');

await xata.api.database.renameDatabase({ workspace, database, newName });
await xata.api.databases.renameDatabase({ workspaceId: workspace, dbName: database, newName });

if (this.jsonEnabled()) return {};

Expand Down
8 changes: 3 additions & 5 deletions cli/src/commands/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ export default class Diff extends BaseCommand<typeof Diff> {
? xata.api.migrations.compareBranchSchemas({
workspace,
region,
database,
branch: args.branch,
compare: args.base
dbBranchName: `${database}:${args.branch}`,
branchName: args.base
})
: xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
schema: { tables: [] },
schemaOperations
});
Expand Down
18 changes: 12 additions & 6 deletions cli/src/commands/import/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,23 @@ export default class ImportCSV extends BaseCommand<typeof ImportCSV> {
}): Promise<void> {
const xata = await this.getXataClient();
const { workspace, region, database, branch } = await this.parseDatabase();
const { schema: existingSchema } = await xata.api.branches.getBranchDetails({
const { schema: existingSchema } = await xata.api.branch.getBranchDetails({
workspace,
region,
database,
branch
dbBranchName: `${database}:${branch}`
});

const newSchema = {
tables: [
...existingSchema.tables.filter((t) => t.name !== table),
{ name: table, columns: columns.filter((c) => c.name !== 'id') }
]
};

const { edits } = await xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch: 'main',
dbBranchName: `${database}:main`,
schema: newSchema
});
if (edits.operations.length > 0) {
Expand Down Expand Up @@ -267,7 +267,13 @@ export default class ImportCSV extends BaseCommand<typeof ImportCSV> {
if (!applyMigrations) {
process.exit(1);
}
await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits });

await xata.api.migrations.applyBranchSchemaEdit({
workspace,
region,
dbBranchName: `${database}:${branch}`,
edits
});
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export default class Init extends BaseCommand<typeof Init> {
if (this.projectConfig?.codegen?.output) {
const { schema: currentSchema } = await (
await this.getXataClient()
).api.branches.getBranchDetails({ workspace, database, region, branch });
).api.branch.getBranchDetails({ workspace, region, dbBranchName: `${database}:${branch}` });

const hasTables = currentSchema?.tables && currentSchema?.tables.length > 0;
const hasColumns = currentSchema?.tables.some((t) => t.columns.length > 0);
Expand Down Expand Up @@ -434,7 +434,7 @@ export default class Init extends BaseCommand<typeof Init> {
let retries = 0;
while (retries++ < maxRetries) {
try {
await xata.api.branches.getBranchList({ workspace, region, database });
await xata.api.branch.getBranchList({ workspace, region, dbName: database });
return;
} catch (err) {
if (err instanceof Error && err.message.includes('Invalid API key')) {
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/pull/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export default class Pull extends BaseCommand<typeof Pull> {
const { logs } = await xata.api.migrations.getBranchSchemaHistory({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
// TODO: Fix pagination in the API to start from last known migration and not from the beginning
// Also paginate until we get all migrations
page: { size: 200 }
Expand Down
10 changes: 7 additions & 3 deletions cli/src/commands/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export default class Push extends BaseCommand<typeof Push> {
const { logs } = await xata.api.migrations.getBranchSchemaHistory({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
// TODO: Fix pagination in the API to start from last known migration and not from the beginning
// Also paginate until we get all migrations
page: { size: 200 }
Expand Down Expand Up @@ -70,7 +69,12 @@ export default class Push extends BaseCommand<typeof Push> {
if (!confirm) return this.exit(1);

// TODO: Check for errors and print them
await xata.api.migrations.pushBranchMigrations({ workspace, region, database, branch, migrations: newMigrations });
await xata.api.migrations.pushBranchMigrations({
workspace,
region,
dbBranchName: `${database}:${branch}`,
migrations: newMigrations
});

this.log(`Pushed ${newMigrations.length} migrations to ${branch}`);
}
Expand Down
13 changes: 8 additions & 5 deletions cli/src/commands/random-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default class RandomData extends BaseCommand<typeof RandomData> {

const { workspace, region, database, branch } = await this.getParsedDatabaseURLWithBranch(flags.db, flags.branch);
const xata = await this.getXataClient();
const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch });
const branchDetails = await xata.api.branch.getBranchDetails({
workspace,
region,
dbBranchName: `${database}:${branch}`
});
if (!branchDetails) {
this.error('Could not resolve the current branch');
}
Expand All @@ -53,10 +57,9 @@ export default class RandomData extends BaseCommand<typeof RandomData> {
await xata.api.records.bulkInsertTableRecords({
workspace,
region,
database,
branch,
table: table.name,
records
dbBranchName: `${database}:${branch}`,
tableName: table.name,
records: records as any[]
});

this.info(
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/rebase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export default class Rebase extends BaseCommand<typeof Rebase> {
const { logs } = await xata.api.migrations.getBranchSchemaHistory({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
// TODO: Fix pagination in the API to start from last known migration and not from the beginning
// Also paginate until we get all migrations
page: { size: 200 }
Expand Down
6 changes: 5 additions & 1 deletion cli/src/commands/schema/dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export default class SchemaDump extends BaseCommand<typeof SchemaDump> {
const { workspace, region, database, branch } = await this.getParsedDatabaseURLWithBranch(flags.db, flags.branch);

const xata = await this.getXataClient();
const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch });
const branchDetails = await xata.api.branch.getBranchDetails({
workspace,
region,
dbBranchName: `${database}:${branch}`
});
if (!branchDetails) return this.error('Could not resolve the current branch');
if (!flags.file) {
ux.styledJSON(branchDetails.schema);
Expand Down
9 changes: 6 additions & 3 deletions cli/src/commands/schema/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ Beware that this can lead to ${chalk.bold(
this.branch = branch;

const xata = await this.getXataClient();
const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch });
const branchDetails = await xata.api.branch.getBranchDetails({
workspace,
region,
dbBranchName: `${database}:${branch}`
});
if (!branchDetails) this.error('Could not get the schema from the current branch');

if (flags.source) {
Expand Down Expand Up @@ -808,8 +812,7 @@ vectorDimension: \${vectorDimension}
await xata.api.migrations.applyBranchSchemaEdit({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
edits
});

Expand Down
16 changes: 12 additions & 4 deletions cli/src/commands/schema/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export default class UploadSchema extends BaseCommand<typeof UploadSchema> {
const xata = await this.getXataClient();

if (flags['create-only']) {
const { schema } = await xata.api.branches.getBranchDetails({ workspace, region, database, branch });
const { schema } = await xata.api.branch.getBranchDetails({
workspace,
region,
dbBranchName: `${database}:${branch}`
});
if (schema.tables.length > 0) {
this.info(
'Schema already exists. `xata schema upload --init` will only initialize the schema if it does not already exist.'
Expand All @@ -50,8 +54,7 @@ export default class UploadSchema extends BaseCommand<typeof UploadSchema> {
const { edits } = await xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
dbBranchName: `${database}:${branch}`,
schema
});

Expand All @@ -71,6 +74,11 @@ export default class UploadSchema extends BaseCommand<typeof UploadSchema> {
});
if (!confirm) return this.exit(1);

await xata.api.migrations.applyBranchSchemaEdit({ workspace, region, database, branch, edits });
await xata.api.migrations.applyBranchSchemaEdit({
workspace,
region,
dbBranchName: `${database}:${branch}`,
edits
});
}
}
6 changes: 5 additions & 1 deletion cli/src/commands/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export default class Shell extends BaseCommand<typeof Shell> {
const tempFile = path.join(__dirname, `xata-${Date.now()}.mjs`);
try {
const xata = await this.getXataClient();
const branchDetails = await xata.api.branches.getBranchDetails({ workspace, region, database, branch });
const branchDetails = await xata.api.branch.getBranchDetails({
workspace,
region,
dbBranchName: `${database}:${branch}`
});
const { schema } = branchDetails;

const { javascript } = await generate({ language: 'javascript', databaseURL, schema });
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/workspace/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class WorkspaceCreate extends BaseCommand<typeof WorkspaceCreate>

const xata = await this.getXataClient();

const result = await xata.api.workspaces.createWorkspace({ data: { name: workspace } });
const result = await xata.api.workspaces.createWorkspace({ name: workspace });

if (this.jsonEnabled()) return result;

Expand Down
Loading