Skip to content

Commit

Permalink
fixup dev uris and add origin override in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
wernst committed Aug 17, 2023
1 parent eede6ef commit 3800a82
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
3 changes: 3 additions & 0 deletions packages/client/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ yargs(hideBin(process.argv))
'Get current migration status',
(yargs) => {
yargs.option('token', tokenOption);
yargs.option('origin', { type: 'string', hidden: true });
return yargs;
},
statusCommand
Expand All @@ -51,6 +52,7 @@ yargs(hideBin(process.argv))
describe: 'The version to migrate up to',
});
yargs.option('token', tokenOption);
yargs.option('origin', { type: 'string', hidden: true });
},
upCommand
);
Expand All @@ -64,6 +66,7 @@ yargs(hideBin(process.argv))
describe: 'The version to migrate down to',
});
yargs.option('token', tokenOption);
yargs.option('origin', { type: 'string', hidden: true });
},
downCommand
);
Expand Down
48 changes: 30 additions & 18 deletions packages/client/cli/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import { Migration } from '@triplit/db';
import { codegen } from './codegen';
import { readMigrations } from './migrations';
import { readRemoteMigrationStatus } from './status';
import { RemoteAccessOptions } from './utils/remote';
import { request } from './utils/request';
import { parseJWT } from './utils/token';

async function applyMigration(
migration: Migration,
direction: 'up' | 'down',
token: string
remoteOptions: RemoteAccessOptions
) {
const { token, originOverride } = remoteOptions;
const payload = parseJWT(token);
const projectId = payload?.['x-triplit-project-id'];
if (!projectId) {
throw new Error('Could not find project ID in token');
}
const res = await request(
`http://${projectId}.localhost:8787/migration/apply`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${token}`,
},
body: JSON.stringify({ migration, direction }),
}
);
const origin = originOverride || `https://${projectId}.triplit.io`;
const res = await request(`${origin}/migration/apply`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${token}`,
},
body: JSON.stringify({ migration, direction }),
});
if (!res.ok) {
throw new Error(
`Error applying ${direction} migration ${migration.version}`
Expand All @@ -36,12 +36,17 @@ async function applyMigration(
export type UpCommandArgs = {
version?: number;
token: string;
origin?: string;
};

export async function upCommand(args: UpCommandArgs) {
const remoteOptions = {
token: args.token,
originOverride: args.origin,
};
// Get migration version from remote
const { data, error } = await readRemoteMigrationStatusWithCmdMessages(
args.token
remoteOptions
);
if (error) {
console.error(error);
Expand Down Expand Up @@ -70,7 +75,7 @@ export async function upCommand(args: UpCommandArgs) {
} else {
for (const migration of migrations) {
console.log('applying up migration with version', migration.version);
await applyMigration(migration, 'up', args.token);
await applyMigration(migration, 'up', remoteOptions);
currentVersion = migration.version;
}
}
Expand All @@ -82,12 +87,17 @@ export async function upCommand(args: UpCommandArgs) {
export type DownCommandArgs = {
version: number;
token: string;
origin?: string;
};

export async function downCommand(args: DownCommandArgs) {
const remoteOptions = {
token: args.token,
originOverride: args.origin,
};
// Get migration version from remote
const { data, error } = await readRemoteMigrationStatusWithCmdMessages(
args.token
remoteOptions
);
if (error) {
console.error(error);
Expand All @@ -114,7 +124,7 @@ export async function downCommand(args: DownCommandArgs) {
} else {
for (const migration of migrations) {
console.log('applying down migration with version', migration.version);
await applyMigration(migration, 'down', args.token);
await applyMigration(migration, 'down', remoteOptions);
currentVersion = migration.parent;
}
}
Expand All @@ -123,9 +133,11 @@ export async function downCommand(args: DownCommandArgs) {
}
}

async function readRemoteMigrationStatusWithCmdMessages(token: string) {
async function readRemoteMigrationStatusWithCmdMessages(
options: RemoteAccessOptions
) {
try {
const { data, error } = await readRemoteMigrationStatus(token);
const { data, error } = await readRemoteMigrationStatus(options);
if (error) {
return { data: undefined, error };
}
Expand Down
28 changes: 17 additions & 11 deletions packages/client/cli/status.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RemoteAccessOptions } from './utils/remote';
import { request } from './utils/request';
import { parseJWT } from './utils/token';

Expand All @@ -8,11 +9,15 @@ interface MigrationStatus {

export type StatusCommandArgs = {
token: string;
origin?: string;
};

export async function statusCommand(args: StatusCommandArgs) {
console.info('Getting migration status from remote...');
const { data: status, error } = await readRemoteMigrationStatus(args.token);
const { data: status, error } = await readRemoteMigrationStatus({
token: args.token,
originOverride: args.origin,
});
if (error) {
console.error(error);
return;
Expand All @@ -24,11 +29,14 @@ export async function statusCommand(args: StatusCommandArgs) {
console.log(status);
}

export async function readRemoteMigrationStatus(token: string): Promise<{
export async function readRemoteMigrationStatus(
options: RemoteAccessOptions
): Promise<{
data?: MigrationStatus;
error?: any;
}> {
try {
const { token, originOverride } = options;
const payload = parseJWT(token);
const projectId = payload?.['x-triplit-project-id'];
if (!projectId) {
Expand All @@ -37,15 +45,13 @@ export async function readRemoteMigrationStatus(token: string): Promise<{
error: 'Could not find project ID in token',
};
}
const res = await request(
`http://${projectId}.localhost:8787/migration/status`,
{
method: 'GET',
headers: {
authorization: `Bearer ${token}`,
},
}
);
const origin = originOverride || `https://${projectId}.triplit.io`;
const res = await request(`${origin}/migration/status`, {
method: 'GET',
headers: {
authorization: `Bearer ${token}`,
},
});
if (!res.ok) {
return {
data: undefined,
Expand Down
4 changes: 4 additions & 0 deletions packages/client/cli/utils/remote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface RemoteAccessOptions {
token: string;
originOverride?: string;
}

0 comments on commit 3800a82

Please sign in to comment.