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
Merged
5 changes: 5 additions & 0 deletions .changeset/light-cycles-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xata.io/client": major
---

Make XataApiClient to use ES Proxies
36 changes: 23 additions & 13 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({ body: { name } });
return workspace.id;
} else if (workspaces.workspaces.length === 1) {
const workspace = workspaces.workspaces[0].id;
Expand Down Expand Up @@ -309,7 +309,9 @@ 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({
pathParams: { workspaceId: workspace }
});

if (dbs.length > 0) {
const choices = dbs.map((db) => ({
Expand Down Expand Up @@ -355,7 +357,9 @@ 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({
pathParams: { workspace, region, dbName: database }
});

const EMPTY_CHOICE = '$empty';
const CREATE_CHOICE = '$create';
Expand Down Expand Up @@ -421,7 +425,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({ pathParams: { workspaceId: workspace } });
const { region } = await this.prompt(
{
type: 'select',
Expand All @@ -434,7 +438,10 @@ 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({
pathParams: { workspaceId: workspace, dbName: name },
body: { region }
});

return { name: result.databaseName, region };
}
Expand All @@ -455,9 +462,12 @@ 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({ pathParams: { workspace, region, dbBranchName: `${database}:${name}` } });
} else {
await xata.api.branches.createBranch({ workspace, region, database, branch: name, from });
await xata.api.branch.createBranch({
pathParams: { workspace, region, dbBranchName: `${database}:${name}` },
body: { from }
});
}

return name;
Expand Down Expand Up @@ -566,11 +576,8 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
async deploySchema(workspace: string, region: string, database: string, branch: string, schema: Schemas.Schema) {
const xata = await this.getXataClient();
const compare = await xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
schema
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { schema }
});

if (compare.edits.operations.length === 0) {
Expand All @@ -587,7 +594,10 @@ 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({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { edits: compare.edits }
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion cli/src/commands/branch/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ 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({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { 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({ pathParams: { 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({ pathParams: { workspace, region, dbName: database } });

if (this.jsonEnabled()) return branches;

Expand Down
4 changes: 3 additions & 1 deletion cli/src/commands/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ 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({
pathParams: { 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({ pathParams: { workspaceId: workspace, dbName: database } });

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

Expand Down
4 changes: 3 additions & 1 deletion cli/src/commands/dbs/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ 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({
pathParams: { workspaceId: workspace }
});

if (this.jsonEnabled()) return dbs;

Expand Down
5 changes: 4 additions & 1 deletion cli/src/commands/dbs/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ 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({
pathParams: { workspaceId: workspace, dbName: database },
body: { newName }
});

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

Expand Down
15 changes: 4 additions & 11 deletions cli/src/commands/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,12 @@ export default class Diff extends BaseCommand<typeof Diff> {
const apiRequest =
args.branch && args.base
? xata.api.migrations.compareBranchSchemas({
workspace,
region,
database,
branch: args.branch,
compare: args.base
pathParams: { workspace, region, dbBranchName: `${database}:${args.branch}`, branchName: args.base },
body: {}
})
: xata.api.migrations.compareBranchWithUserSchema({
workspace,
region,
database,
branch,
schema: { tables: [] },
schemaOperations
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { schema: { tables: [] }, schemaOperations }
});

const {
Expand Down
22 changes: 11 additions & 11 deletions cli/src/commands/import/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,20 @@ 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({
workspace,
region,
database,
branch
const { schema: existingSchema } = await xata.api.branch.getBranchDetails({
pathParams: { workspace, region, 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',
schema: newSchema
pathParams: { workspace, region, dbBranchName: `${database}:main` },
body: { schema: newSchema }
});
if (edits.operations.length > 0) {
const destructiveOperations = edits.operations
Expand Down Expand Up @@ -267,7 +263,11 @@ 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({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { 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({ pathParams: { 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({ pathParams: { workspace, region, dbName: database } });
return;
} catch (err) {
if (err instanceof Error && err.message.includes('Invalid API key')) {
Expand Down
13 changes: 6 additions & 7 deletions cli/src/commands/pull/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ export default class Pull extends BaseCommand<typeof Pull> {
);

const { logs } = await xata.api.migrations.getBranchSchemaHistory({
workspace,
region,
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 }
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: {
// 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 }
}
});

if (flags.force) {
Expand Down
18 changes: 10 additions & 8 deletions cli/src/commands/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ export default class Push extends BaseCommand<typeof Push> {
);

const { logs } = await xata.api.migrations.getBranchSchemaHistory({
workspace,
region,
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 }
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: {
// 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 }
}
});

const localMigrationFiles = await getLocalMigrationFiles();
Expand Down Expand Up @@ -70,7 +69,10 @@ 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({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: { migrations: newMigrations }
});

this.log(`Pushed ${newMigrations.length} migrations to ${branch}`);
}
Expand Down
12 changes: 5 additions & 7 deletions cli/src/commands/random-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ 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({
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` }
});
if (!branchDetails) {
this.error('Could not resolve the current branch');
}
Expand All @@ -51,12 +53,8 @@ export default class RandomData extends BaseCommand<typeof RandomData> {
const records = generateRandomData(table, totalRecords);

await xata.api.records.bulkInsertTableRecords({
workspace,
region,
database,
branch,
table: table.name,
records
pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table.name },
body: { records: records as any[] }
});

this.info(
Expand Down
13 changes: 6 additions & 7 deletions cli/src/commands/rebase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ export default class Rebase extends BaseCommand<typeof Rebase> {
this.info(`Rebase command is experimental, use with caution`);

const { logs } = await xata.api.migrations.getBranchSchemaHistory({
workspace,
region,
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 }
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
body: {
// 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 }
}
});

const remoteMigrationFiles = commitToMigrationFile(logs);
Expand Down
4 changes: 3 additions & 1 deletion cli/src/commands/schema/dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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({
pathParams: { 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
11 changes: 5 additions & 6 deletions cli/src/commands/schema/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ 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({
pathParams: { 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 @@ -806,11 +808,8 @@ vectorDimension: \${vectorDimension}
}

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

this.success('Migration completed!');
Expand Down
Loading
Loading