powersync 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +67 -0
- package/README.md +1376 -173
- package/bin/dev.cmd +3 -3
- package/bin/dev.js +20 -7
- package/bin/run.cmd +3 -3
- package/bin/run.js +9 -5
- package/dist/api/cloud/create-cloud-instance.d.ts +15 -0
- package/dist/api/cloud/create-cloud-instance.js +19 -0
- package/dist/api/cloud/fetch-cloud-config.d.ts +12 -0
- package/dist/api/cloud/fetch-cloud-config.js +18 -0
- package/dist/api/cloud/fetch-cloud-sync-rules-content.d.ts +7 -0
- package/dist/api/cloud/fetch-cloud-sync-rules-content.js +26 -0
- package/dist/api/cloud/test-connection.d.ts +16 -0
- package/dist/api/cloud/test-connection.js +36 -0
- package/dist/api/cloud/validate-cloud-link-config.d.ts +17 -0
- package/dist/api/cloud/validate-cloud-link-config.js +47 -0
- package/dist/api/cloud/wait-for-operation.d.ts +16 -0
- package/dist/api/cloud/wait-for-operation.js +33 -0
- package/dist/api/cloud/write-cloud-link.d.ts +10 -0
- package/dist/api/cloud/write-cloud-link.js +21 -0
- package/dist/api/display-status.d.ts +4 -0
- package/dist/api/display-status.js +80 -0
- package/dist/api/ide/configure-vscode-ide.d.ts +14 -0
- package/dist/api/ide/configure-vscode-ide.js +62 -0
- package/dist/api/login-server.d.ts +26 -0
- package/dist/api/login-server.js +137 -0
- package/dist/api/run-validation-tests.d.ts +63 -0
- package/dist/api/run-validation-tests.js +202 -0
- package/dist/api/self-hosted/fetch-self-hosted-sync-rules-content.d.ts +7 -0
- package/dist/api/self-hosted/fetch-self-hosted-sync-rules-content.js +26 -0
- package/dist/commands/configure/ide.d.ts +8 -0
- package/dist/commands/configure/ide.js +58 -0
- package/dist/commands/configure/index.d.ts +8 -0
- package/dist/commands/configure/index.js +11 -0
- package/dist/commands/deploy/index.d.ts +48 -0
- package/dist/commands/deploy/index.js +333 -0
- package/dist/commands/deploy/service-config.d.ts +14 -0
- package/dist/commands/deploy/service-config.js +30 -0
- package/dist/commands/deploy/sync-config.d.ts +24 -0
- package/dist/commands/deploy/sync-config.js +103 -0
- package/dist/commands/destroy.d.ts +14 -0
- package/dist/commands/destroy.js +62 -0
- package/dist/commands/fetch/config.d.ts +14 -0
- package/dist/commands/fetch/config.js +33 -0
- package/dist/commands/fetch/index.d.ts +13 -0
- package/dist/commands/fetch/index.js +16 -0
- package/dist/commands/fetch/instances.d.ts +47 -0
- package/dist/commands/fetch/instances.js +177 -0
- package/dist/commands/fetch/status.d.ts +18 -0
- package/dist/commands/fetch/status.js +60 -0
- package/dist/commands/{instance/schema.d.ts → generate/index.d.ts} +3 -1
- package/dist/commands/generate/index.js +11 -0
- package/dist/commands/generate/schema.d.ts +19 -0
- package/dist/commands/generate/schema.js +72 -0
- package/dist/commands/generate/token.d.ts +25 -0
- package/dist/commands/generate/token.js +124 -0
- package/dist/commands/init/cloud.d.ts +11 -0
- package/dist/commands/init/cloud.js +64 -0
- package/dist/commands/{init.d.ts → init/index.d.ts} +2 -1
- package/dist/commands/init/index.js +11 -0
- package/dist/commands/init/self-hosted.d.ts +11 -0
- package/dist/commands/init/self-hosted.js +60 -0
- package/dist/commands/link/cloud.d.ts +15 -0
- package/dist/commands/link/cloud.js +111 -0
- package/dist/commands/{instance/config.d.ts → link/index.d.ts} +3 -1
- package/dist/commands/link/index.js +11 -0
- package/dist/commands/link/self-hosted.d.ts +12 -0
- package/dist/commands/link/self-hosted.js +54 -0
- package/dist/commands/login.d.ts +8 -0
- package/dist/commands/login.js +107 -0
- package/dist/commands/logout.d.ts +8 -0
- package/dist/commands/logout.js +19 -0
- package/dist/commands/migrate/index.d.ts +7 -0
- package/dist/commands/migrate/index.js +10 -0
- package/dist/commands/migrate/sync-rules.d.ts +15 -0
- package/dist/commands/migrate/sync-rules.js +51 -0
- package/dist/commands/{instances.d.ts → pull/index.d.ts} +3 -1
- package/dist/commands/pull/index.js +11 -0
- package/dist/commands/pull/instance.d.ts +14 -0
- package/dist/commands/pull/instance.js +108 -0
- package/dist/commands/status.d.ts +1 -0
- package/dist/commands/status.js +2 -0
- package/dist/commands/stop.d.ts +14 -0
- package/dist/commands/stop.js +62 -0
- package/dist/commands/validate.d.ts +16 -0
- package/dist/commands/validate.js +165 -0
- package/dist/help.d.ts +31 -0
- package/dist/help.js +123 -0
- package/oclif.manifest.json +1259 -330
- package/package.json +104 -75
- package/templates/cloud/powersync/cli.yaml +3 -0
- package/templates/cloud/powersync/service.yaml +174 -0
- package/templates/cloud/powersync/sync-config.yaml +22 -0
- package/templates/self-hosted/base/powersync/cli.yaml +3 -0
- package/templates/self-hosted/base/powersync/service.yaml +243 -0
- package/templates/self-hosted/base/powersync/sync-config.yaml +22 -0
- package/templates/vscode/settings.json +3 -0
- package/dist/commands/init.js +0 -66
- package/dist/commands/instance/config.js +0 -23
- package/dist/commands/instance/create.d.ts +0 -36
- package/dist/commands/instance/create.js +0 -137
- package/dist/commands/instance/deploy.d.ts +0 -21
- package/dist/commands/instance/deploy.js +0 -91
- package/dist/commands/instance/destroy.d.ts +0 -9
- package/dist/commands/instance/destroy.js +0 -46
- package/dist/commands/instance/generate-dev-token.d.ts +0 -10
- package/dist/commands/instance/generate-dev-token.js +0 -54
- package/dist/commands/instance/schema.js +0 -20
- package/dist/commands/instance/set.d.ts +0 -9
- package/dist/commands/instance/set.js +0 -31
- package/dist/commands/instance/status.d.ts +0 -12
- package/dist/commands/instance/status.js +0 -50
- package/dist/commands/instance/stop.d.ts +0 -9
- package/dist/commands/instance/stop.js +0 -42
- package/dist/commands/instance/sync-rules/deploy.d.ts +0 -10
- package/dist/commands/instance/sync-rules/deploy.js +0 -49
- package/dist/commands/instance/sync-rules/generate-schema.d.ts +0 -10
- package/dist/commands/instance/sync-rules/generate-schema.js +0 -56
- package/dist/commands/instance/sync-rules/validate.d.ts +0 -9
- package/dist/commands/instance/sync-rules/validate.js +0 -29
- package/dist/commands/instances.js +0 -29
- package/dist/commands.utils.d.ts +0 -2
- package/dist/commands.utils.js +0 -131
- package/dist/connection.utils.d.ts +0 -56
- package/dist/connection.utils.js +0 -76
- package/dist/constants.d.ts +0 -8
- package/dist/constants.js +0 -10
- package/dist/instance-command-flags.d.ts +0 -13
- package/dist/instance-command-flags.js +0 -22
- package/dist/service/accounts.client.js +0 -11307
- package/dist/service/powersync.client.js +0 -75510
- package/dist/service/types.js +0 -1
- package/dist/service/utils.js +0 -153
- package/dist/utils.d.ts +0 -14
- package/dist/utils.js +0 -68
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
/**
|
|
3
|
+
* This cloud only implementation is exported as index.ts
|
|
4
|
+
* in order to allow plugin commands to extend the command name.
|
|
5
|
+
* For example a plugin could add a `fetch instances self-hosted` command.
|
|
6
|
+
*/
|
|
7
|
+
export default class Fetch extends Command {
|
|
8
|
+
static description = 'Subcommands: list Cloud instances in org/project (fetch instances), print instance config as YAML/JSON (fetch config), or show instance diagnostics (fetch status).';
|
|
9
|
+
static examples = ['<%= config.bin %> <%= command.id %>'];
|
|
10
|
+
static hidden = true;
|
|
11
|
+
static summary = 'List instances, fetch config, or fetch instance diagnostics.';
|
|
12
|
+
async run() {
|
|
13
|
+
await this.parse(Fetch);
|
|
14
|
+
this.log('Use a subcommand: fetch instances | fetch config | fetch status');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
import { CommandHelpGroup } from '@powersync/cli-core';
|
|
3
|
+
import { CLIConfig } from '@powersync/cli-schemas';
|
|
4
|
+
type Instance = {
|
|
5
|
+
deployable: boolean;
|
|
6
|
+
has_config: boolean;
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
export type Project = {
|
|
11
|
+
id: string;
|
|
12
|
+
instances: Instance[];
|
|
13
|
+
name: string;
|
|
14
|
+
};
|
|
15
|
+
type ProjectMap = {
|
|
16
|
+
[project_id: string]: Project;
|
|
17
|
+
};
|
|
18
|
+
type Organization = {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
projects: ProjectMap;
|
|
22
|
+
};
|
|
23
|
+
type OrganizationMap = {
|
|
24
|
+
[org_id: string]: Organization;
|
|
25
|
+
};
|
|
26
|
+
export default class FetchInstances extends Command {
|
|
27
|
+
static commandHelpGroup: CommandHelpGroup;
|
|
28
|
+
static description: string;
|
|
29
|
+
static examples: string[];
|
|
30
|
+
static flags: {
|
|
31
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
32
|
+
output: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
33
|
+
'output-file': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
34
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
35
|
+
};
|
|
36
|
+
static summary: string;
|
|
37
|
+
protected fetchCloudInstances(params: {
|
|
38
|
+
org_id?: string;
|
|
39
|
+
project_id?: string;
|
|
40
|
+
}): Promise<OrganizationMap>;
|
|
41
|
+
protected fetchLinkedInstances(): Promise<{
|
|
42
|
+
config: CLIConfig;
|
|
43
|
+
subDirectory: string;
|
|
44
|
+
}[]>;
|
|
45
|
+
run(): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Command, Flags, ux } from '@oclif/core';
|
|
2
|
+
import { CLI_FILENAME, CommandHelpGroup, createAccountsHubClient, createCloudClient, parseYamlFile } from '@powersync/cli-core';
|
|
3
|
+
import { CLIConfig } from '@powersync/cli-schemas';
|
|
4
|
+
import sortBy from 'lodash/sortBy.js';
|
|
5
|
+
import fs, { readdir } from 'node:fs/promises';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import ora from 'ora';
|
|
8
|
+
export default class FetchInstances extends Command {
|
|
9
|
+
static commandHelpGroup = CommandHelpGroup.CLOUD;
|
|
10
|
+
static description = 'List PowerSync Cloud and linked instances, Cloud instances are grouped by organization and project.';
|
|
11
|
+
static examples = [
|
|
12
|
+
'<%= config.bin %> <%= command.id %>',
|
|
13
|
+
'<%= config.bin %> <%= command.id %> --project-id=<id> --output=json'
|
|
14
|
+
];
|
|
15
|
+
static flags = {
|
|
16
|
+
'org-id': Flags.string({
|
|
17
|
+
description: 'Optional Organization ID. Defaults to all organizations.',
|
|
18
|
+
required: false
|
|
19
|
+
}),
|
|
20
|
+
output: Flags.string({
|
|
21
|
+
default: 'human',
|
|
22
|
+
description: 'Output format: human or json.',
|
|
23
|
+
options: ['human', 'json']
|
|
24
|
+
}),
|
|
25
|
+
'output-file': Flags.string({
|
|
26
|
+
description: 'Optionally write instance information to a file.',
|
|
27
|
+
required: false
|
|
28
|
+
}),
|
|
29
|
+
'project-id': Flags.string({
|
|
30
|
+
description: 'Optional Project ID. Defaults to all projects in the org.',
|
|
31
|
+
required: false
|
|
32
|
+
})
|
|
33
|
+
};
|
|
34
|
+
static summary = 'List Cloud and linked instances.';
|
|
35
|
+
async fetchCloudInstances(params) {
|
|
36
|
+
const { org_id, project_id } = params;
|
|
37
|
+
const accountsClient = createAccountsHubClient();
|
|
38
|
+
const managementClient = createCloudClient();
|
|
39
|
+
const instanceMap = {};
|
|
40
|
+
let totalOrgs;
|
|
41
|
+
let processedOrgs = 0;
|
|
42
|
+
let spinnerStarted = false;
|
|
43
|
+
const spinner = ora({
|
|
44
|
+
discardStdin: false,
|
|
45
|
+
stream: process.stdout,
|
|
46
|
+
text: 'Fetching cloud instances...'
|
|
47
|
+
});
|
|
48
|
+
try {
|
|
49
|
+
for await (const page of accountsClient.listOrganizations.paginate({ id: org_id })) {
|
|
50
|
+
const { objects: organizations, total } = page;
|
|
51
|
+
if (totalOrgs === undefined) {
|
|
52
|
+
totalOrgs = total;
|
|
53
|
+
if (total > 0) {
|
|
54
|
+
spinner.start();
|
|
55
|
+
spinnerStarted = true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const organization of organizations) {
|
|
59
|
+
spinner.text = `Fetching org ${processedOrgs + 1} of ${totalOrgs}...`;
|
|
60
|
+
const orgMap = (instanceMap[organization.id] = {
|
|
61
|
+
id: organization.id,
|
|
62
|
+
name: organization.label,
|
|
63
|
+
projects: {}
|
|
64
|
+
});
|
|
65
|
+
let totalProjects;
|
|
66
|
+
let processedProjects = 0;
|
|
67
|
+
for await (const projectPage of accountsClient.listProjects.paginate({
|
|
68
|
+
id: project_id,
|
|
69
|
+
org_id: organization.id
|
|
70
|
+
})) {
|
|
71
|
+
const { objects: projects, total } = projectPage;
|
|
72
|
+
if (totalProjects === undefined) {
|
|
73
|
+
totalProjects = total;
|
|
74
|
+
}
|
|
75
|
+
for (const project of projects) {
|
|
76
|
+
spinner.text = `Fetching org ${processedOrgs + 1} of ${totalOrgs}, project ${processedProjects + 1} of ${totalProjects}...`;
|
|
77
|
+
const projectMap = (orgMap.projects[project.id] = {
|
|
78
|
+
id: project.id,
|
|
79
|
+
instances: [],
|
|
80
|
+
name: project.name
|
|
81
|
+
});
|
|
82
|
+
const instances = await managementClient.listInstances({
|
|
83
|
+
app_id: project.id,
|
|
84
|
+
org_id: organization.id
|
|
85
|
+
});
|
|
86
|
+
projectMap.instances.push(...instances.instances);
|
|
87
|
+
processedProjects++;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
processedOrgs++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return instanceMap;
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
if (spinnerStarted) {
|
|
97
|
+
spinner.stop();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async fetchLinkedInstances() {
|
|
102
|
+
const subDirectories = await readdir(process.cwd());
|
|
103
|
+
const linkedProjects = [];
|
|
104
|
+
for (const subDirectory of subDirectories) {
|
|
105
|
+
const linkPath = path.join(process.cwd(), subDirectory, CLI_FILENAME);
|
|
106
|
+
const exists = await fs
|
|
107
|
+
.stat(linkPath)
|
|
108
|
+
.then(() => true)
|
|
109
|
+
.catch(() => false);
|
|
110
|
+
if (!exists) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const valid = CLIConfig.decode(parseYamlFile(linkPath).contents?.toJSON());
|
|
115
|
+
linkedProjects.push({
|
|
116
|
+
config: valid,
|
|
117
|
+
subDirectory
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
catch { }
|
|
121
|
+
}
|
|
122
|
+
return linkedProjects;
|
|
123
|
+
}
|
|
124
|
+
async run() {
|
|
125
|
+
const { flags } = await this.parse(FetchInstances);
|
|
126
|
+
this.log(''); // Add spacing
|
|
127
|
+
const cloudInstanceMap = await this.fetchCloudInstances({
|
|
128
|
+
org_id: flags['org-id'],
|
|
129
|
+
project_id: flags['project-id']
|
|
130
|
+
}).catch((error) => {
|
|
131
|
+
this.warn(`Failed to fetch cloud instances: ${error instanceof Error ? error.message : String(error)}`);
|
|
132
|
+
return {};
|
|
133
|
+
});
|
|
134
|
+
const linkedInstances = await this.fetchLinkedInstances().catch((error) => {
|
|
135
|
+
this.warn(`Failed to fetch linked instances: ${error instanceof Error ? error.message : String(error)}`);
|
|
136
|
+
return [];
|
|
137
|
+
});
|
|
138
|
+
if (flags.output === 'human') {
|
|
139
|
+
// Log in human readable format
|
|
140
|
+
for (const org of sortBy(Object.values(cloudInstanceMap), 'name')) {
|
|
141
|
+
this.log(`${ux.colorize('blue', 'Organization: ')} ${org.name} ${ux.colorize('gray', `id: ${org.id}`)}`);
|
|
142
|
+
for (const project of sortBy(Object.values(org.projects), 'name')) {
|
|
143
|
+
this.log(`\t${ux.colorize('blue', 'Project: ')} ${project.name} ${ux.colorize('gray', `id: ${project.id}`)}`);
|
|
144
|
+
for (const instance of sortBy(project.instances, 'name')) {
|
|
145
|
+
this.log(`\t\t${ux.colorize('blue', 'Instance: ')} ${instance.name} ${ux.colorize('gray', `id: ${instance.id}`)} ${ux.colorize('gray', `has_config: ${instance.has_config}`)} ${ux.colorize('gray', `is_provisioned: ${instance.deployable}`)}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
this.log('');
|
|
149
|
+
}
|
|
150
|
+
for (const linked of linkedInstances) {
|
|
151
|
+
this.log(`Locally linked in ./${linked.subDirectory}/`);
|
|
152
|
+
this.log(`\t${ux.colorize('blue', 'Project type: ')} ${linked.config.type}`);
|
|
153
|
+
if (linked.config.type === 'cloud') {
|
|
154
|
+
this.log(`\t${ux.colorize('blue', 'Project ID: ')} ${linked.config.project_id}`);
|
|
155
|
+
this.log(`\t${ux.colorize('blue', 'Instance ID: ')} ${linked.config.instance_id}`);
|
|
156
|
+
}
|
|
157
|
+
else if (linked.config.type === 'self-hosted') {
|
|
158
|
+
this.log(`\t${ux.colorize('blue', 'API URL: ')} ${linked.config.api_url}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
this.log('');
|
|
162
|
+
}
|
|
163
|
+
const outputObject = {
|
|
164
|
+
cloudInstances: cloudInstanceMap,
|
|
165
|
+
linkedInstances
|
|
166
|
+
};
|
|
167
|
+
if (flags.output === 'json' || flags['output-file']) {
|
|
168
|
+
const content = ux.colorizeJson(outputObject);
|
|
169
|
+
if (flags.output === 'json') {
|
|
170
|
+
this.log(content);
|
|
171
|
+
}
|
|
172
|
+
if (flags['output-file']) {
|
|
173
|
+
await fs.writeFile(flags['output-file'], content);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CloudProject, SelfHostedProject, SharedInstanceCommand } from '@powersync/cli-core';
|
|
2
|
+
import { DiagnosticsResponse } from '../../api/display-status.js';
|
|
3
|
+
export default class FetchStatus extends SharedInstanceCommand {
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
output: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
};
|
|
14
|
+
static summary: string;
|
|
15
|
+
getCloudStatus(project: CloudProject): Promise<DiagnosticsResponse>;
|
|
16
|
+
getSelfHostedStatus(project: SelfHostedProject): Promise<DiagnosticsResponse>;
|
|
17
|
+
run(): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { createSelfHostedClient, SharedInstanceCommand } from '@powersync/cli-core';
|
|
3
|
+
import { Document } from 'yaml';
|
|
4
|
+
import { formatDiagnosticsHuman } from '../../api/display-status.js';
|
|
5
|
+
export default class FetchStatus extends SharedInstanceCommand {
|
|
6
|
+
static description = 'Fetch instance diagnostics: connection status, active and deploying sync config, replication state. Output as human-readable, JSON, or YAML. Cloud and self-hosted.';
|
|
7
|
+
static examples = [
|
|
8
|
+
'<%= config.bin %> <%= command.id %>',
|
|
9
|
+
'<%= config.bin %> <%= command.id %> --output=json',
|
|
10
|
+
'<%= config.bin %> <%= command.id %> --instance-id=<id> --project-id=<id>'
|
|
11
|
+
];
|
|
12
|
+
static flags = {
|
|
13
|
+
output: Flags.string({
|
|
14
|
+
default: 'human',
|
|
15
|
+
description: 'Output format: human-readable, json, or yaml.',
|
|
16
|
+
options: ['human', 'json', 'yaml']
|
|
17
|
+
}),
|
|
18
|
+
...SharedInstanceCommand.flags
|
|
19
|
+
};
|
|
20
|
+
static summary = 'Show instance diagnostics (connections, sync config, replication).';
|
|
21
|
+
async getCloudStatus(project) {
|
|
22
|
+
const { linked } = project;
|
|
23
|
+
const client = this.cloudClient;
|
|
24
|
+
await this.ensureCloudProvisioned(project);
|
|
25
|
+
return client.getInstanceDiagnostics({
|
|
26
|
+
app_id: linked.project_id,
|
|
27
|
+
id: linked.instance_id,
|
|
28
|
+
org_id: linked.org_id
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async getSelfHostedStatus(project) {
|
|
32
|
+
const { linked } = project;
|
|
33
|
+
const client = createSelfHostedClient({
|
|
34
|
+
apiKey: linked.api_key,
|
|
35
|
+
apiUrl: linked.api_url
|
|
36
|
+
});
|
|
37
|
+
return client.diagnostics({});
|
|
38
|
+
}
|
|
39
|
+
async run() {
|
|
40
|
+
const { flags } = await this.parse(FetchStatus);
|
|
41
|
+
const project = await this.loadProject(flags);
|
|
42
|
+
try {
|
|
43
|
+
const diagnostics = await (project.linked.type === 'cloud'
|
|
44
|
+
? this.getCloudStatus(project)
|
|
45
|
+
: this.getSelfHostedStatus(project));
|
|
46
|
+
if (flags.output === 'json') {
|
|
47
|
+
this.log(JSON.stringify(diagnostics, null, 2));
|
|
48
|
+
}
|
|
49
|
+
else if (flags.output === 'yaml') {
|
|
50
|
+
this.log(new Document(diagnostics).toString());
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.log(formatDiagnosticsHuman(diagnostics));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
this.styledError({ error, message: 'Failed to fetch instance diagnostics' });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Command } from '@oclif/core';
|
|
2
|
-
export default class
|
|
2
|
+
export default class Generate extends Command {
|
|
3
3
|
static description: string;
|
|
4
4
|
static examples: string[];
|
|
5
|
+
static hidden: boolean;
|
|
6
|
+
static summary: string;
|
|
5
7
|
run(): Promise<void>;
|
|
6
8
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
export default class Generate extends Command {
|
|
3
|
+
static description = 'Generate client artifacts: schema (from instance schema + sync config) or a development token for connecting clients. Cloud and self-hosted where supported.';
|
|
4
|
+
static examples = ['<%= config.bin %> <%= command.id %>'];
|
|
5
|
+
static hidden = true;
|
|
6
|
+
static summary = 'Generate client schema or development token.';
|
|
7
|
+
async run() {
|
|
8
|
+
await this.parse(Generate);
|
|
9
|
+
this.log('Use a subcommand: generate schema | generate token');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CloudProject, SelfHostedProject, SharedInstanceCommand } from '@powersync/cli-core';
|
|
2
|
+
import { routes } from '@powersync/management-types';
|
|
3
|
+
export default class GenerateSchema extends SharedInstanceCommand {
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
output: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
'output-path': import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
};
|
|
15
|
+
static summary: string;
|
|
16
|
+
getCloudSchema(project: CloudProject): Promise<routes.GetSchemaResponse>;
|
|
17
|
+
getSelfHostedSchema(project: SelfHostedProject): Promise<routes.GetSchemaResponse>;
|
|
18
|
+
run(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Flags, ux } from '@oclif/core';
|
|
2
|
+
import { createCloudClient, createSelfHostedClient, SharedInstanceCommand } from '@powersync/cli-core';
|
|
3
|
+
import { schemaGenerators, SqlSyncRules, StaticSchema } from '@powersync/service-sync-rules';
|
|
4
|
+
import { writeFileSync } from 'node:fs';
|
|
5
|
+
import { fetchCloudSyncRulesContent } from '../../api/cloud/fetch-cloud-sync-rules-content.js';
|
|
6
|
+
import { fetchSelfHostedSyncRulesContent } from '../../api/self-hosted/fetch-self-hosted-sync-rules-content.js';
|
|
7
|
+
export default class GenerateSchema extends SharedInstanceCommand {
|
|
8
|
+
static description = 'Generate a client-side schema file from the instance database schema and sync config. Supports multiple output types (e.g. type, dart). Requires a linked instance. Cloud and self-hosted.';
|
|
9
|
+
static examples = [
|
|
10
|
+
'<%= config.bin %> <%= command.id %> --output=ts --output-path=schema.ts',
|
|
11
|
+
'<%= config.bin %> <%= command.id %> --output=dart --output-path=lib/schema.dart --instance-id=<id> --project-id=<id>'
|
|
12
|
+
];
|
|
13
|
+
static flags = {
|
|
14
|
+
output: Flags.string({
|
|
15
|
+
default: 'type',
|
|
16
|
+
description: 'Output type: ' + Object.keys(schemaGenerators).join(', '),
|
|
17
|
+
options: Object.keys(schemaGenerators),
|
|
18
|
+
required: true
|
|
19
|
+
}),
|
|
20
|
+
'output-path': Flags.string({
|
|
21
|
+
description: 'Path to output the schema file.',
|
|
22
|
+
required: true
|
|
23
|
+
}),
|
|
24
|
+
...SharedInstanceCommand.flags
|
|
25
|
+
};
|
|
26
|
+
static summary = 'Generate client schema file from instance schema and sync config.';
|
|
27
|
+
async getCloudSchema(project) {
|
|
28
|
+
const { linked } = project;
|
|
29
|
+
const client = await createCloudClient();
|
|
30
|
+
return client.getInstanceSchema({
|
|
31
|
+
app_id: linked.project_id,
|
|
32
|
+
id: linked.instance_id,
|
|
33
|
+
org_id: linked.org_id
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async getSelfHostedSchema(project) {
|
|
37
|
+
const { linked } = project;
|
|
38
|
+
const client = createSelfHostedClient({
|
|
39
|
+
apiKey: linked.api_key,
|
|
40
|
+
apiUrl: linked.api_url
|
|
41
|
+
});
|
|
42
|
+
return client.getSchema({});
|
|
43
|
+
}
|
|
44
|
+
async run() {
|
|
45
|
+
const { flags } = await this.parse(GenerateSchema);
|
|
46
|
+
const project = await this.loadProject(flags);
|
|
47
|
+
const schemaGenerator = schemaGenerators[flags.output];
|
|
48
|
+
if (!schemaGenerator) {
|
|
49
|
+
this.styledError({
|
|
50
|
+
message: `Invalid output type: ${flags.output}. Supported types: ${Object.keys(schemaGenerators).join(', ')}`
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const databaseSchema = await (project.linked.type === 'cloud'
|
|
55
|
+
? this.getCloudSchema(project)
|
|
56
|
+
: this.getSelfHostedSchema(project));
|
|
57
|
+
const syncRulesContent = await (project.linked.type === 'cloud'
|
|
58
|
+
? fetchCloudSyncRulesContent(project)
|
|
59
|
+
: fetchSelfHostedSyncRulesContent(project));
|
|
60
|
+
const staticSchema = new StaticSchema(databaseSchema.connections);
|
|
61
|
+
const schema = schemaGenerator.generate(SqlSyncRules.fromYaml(syncRulesContent, {
|
|
62
|
+
defaultSchema: databaseSchema.defaultSchema ?? 'public',
|
|
63
|
+
schema: staticSchema
|
|
64
|
+
}).config, staticSchema);
|
|
65
|
+
writeFileSync(flags['output-path'], schema, 'utf8');
|
|
66
|
+
this.log(ux.colorize('green', `Generated schema written to ${flags['output-path']}`));
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
this.styledError({ error, message: 'Failed to generate schema' });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { CloudProject, SelfHostedProject, SharedInstanceCommand } from '@powersync/cli-core';
|
|
2
|
+
type TokenConfig = {
|
|
3
|
+
expiresInSeconds: number;
|
|
4
|
+
kid?: string;
|
|
5
|
+
subject: string;
|
|
6
|
+
};
|
|
7
|
+
export default class GenerateToken extends SharedInstanceCommand {
|
|
8
|
+
static description: string;
|
|
9
|
+
static examples: string[];
|
|
10
|
+
static flags: {
|
|
11
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
15
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
16
|
+
'expires-in-seconds': import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
|
|
17
|
+
kid: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
18
|
+
subject: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
19
|
+
};
|
|
20
|
+
static summary: string;
|
|
21
|
+
protected generateCloudToken(project: CloudProject, config: TokenConfig): Promise<string>;
|
|
22
|
+
protected generateSelfHostedToken(project: SelfHostedProject, config: TokenConfig): Promise<string>;
|
|
23
|
+
run(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { createCloudClient, SERVICE_FILENAME, SharedInstanceCommand } from '@powersync/cli-core';
|
|
3
|
+
import * as jose from 'jose';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
export default class GenerateToken extends SharedInstanceCommand {
|
|
6
|
+
static description = 'Generate a JWT for development clients to connect to PowerSync. Cloud: uses instance dev-token API (allow_temporary_tokens must be enabled). Self-hosted: signs with shared secret from config. Requires --subject; optional --expires-in-seconds.';
|
|
7
|
+
static examples = [
|
|
8
|
+
'<%= config.bin %> <%= command.id %> --subject=user-123',
|
|
9
|
+
'<%= config.bin %> <%= command.id %> --subject=user-123 --expires-in-seconds=3600'
|
|
10
|
+
];
|
|
11
|
+
static flags = {
|
|
12
|
+
'expires-in-seconds': Flags.integer({
|
|
13
|
+
default: 43_200,
|
|
14
|
+
description: 'Expiration time in seconds. Default is 43,200 (12 hours).',
|
|
15
|
+
required: false
|
|
16
|
+
}),
|
|
17
|
+
kid: Flags.string({
|
|
18
|
+
description: '[Self-hosted only] Key ID of the key to use for signing the token. If not provided, the first key will be used.',
|
|
19
|
+
required: false
|
|
20
|
+
}),
|
|
21
|
+
subject: Flags.string({
|
|
22
|
+
description: 'Subject of the token.',
|
|
23
|
+
required: true
|
|
24
|
+
}),
|
|
25
|
+
...SharedInstanceCommand.flags
|
|
26
|
+
};
|
|
27
|
+
static summary = 'Generate a development JWT for client connections.';
|
|
28
|
+
async generateCloudToken(project, config) {
|
|
29
|
+
const { linked } = project;
|
|
30
|
+
const client = createCloudClient();
|
|
31
|
+
// Get the config in order to check if development tokens are enabled.
|
|
32
|
+
const cloudInstanceConfig = await client
|
|
33
|
+
.getInstanceConfig({
|
|
34
|
+
app_id: linked.project_id,
|
|
35
|
+
id: linked.instance_id,
|
|
36
|
+
org_id: linked.org_id
|
|
37
|
+
})
|
|
38
|
+
.catch((error) => {
|
|
39
|
+
this.styledError({
|
|
40
|
+
error,
|
|
41
|
+
message: `Failed to get config for instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}`
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
if (!cloudInstanceConfig?.config?.client_auth?.allow_temporary_tokens) {
|
|
45
|
+
this.styledError({
|
|
46
|
+
message: 'Development tokens are not enabled for this instance. Set the following config in the instance config to enable development tokens:\n client_auth:\n allow_temporary_tokens: true\nThen deploy an update to enable development tokens first.'
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
const response = await client.generateDevToken({
|
|
50
|
+
app_id: linked.project_id,
|
|
51
|
+
expiresInSeconds: config.expiresInSeconds,
|
|
52
|
+
id: linked.instance_id,
|
|
53
|
+
org_id: linked.org_id,
|
|
54
|
+
subject: config.subject
|
|
55
|
+
});
|
|
56
|
+
return response.token;
|
|
57
|
+
}
|
|
58
|
+
async generateSelfHostedToken(project, config) {
|
|
59
|
+
// For self hosted we can check if there is a shared secret in the config file and then manually create a token with JOSE
|
|
60
|
+
let instanceConfig;
|
|
61
|
+
try {
|
|
62
|
+
instanceConfig = this.parseSelfHostedConfig(project.projectDirectory);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
this.styledError({
|
|
66
|
+
error,
|
|
67
|
+
message: 'Generating a token for self hosted instances requires the configuration to be locally present.',
|
|
68
|
+
suggestions: [`Ensure that ${join(project.projectDirectory, SERVICE_FILENAME)} exists`]
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const usableKeys = instanceConfig.client_auth?.jwks?.keys?.filter((key) => key.alg === 'HS256') ?? [];
|
|
72
|
+
if (usableKeys.length === 0) {
|
|
73
|
+
this.styledError({
|
|
74
|
+
message: [
|
|
75
|
+
`No usable keys found in the config file.`,
|
|
76
|
+
`Please add a shared secret to the config file.`,
|
|
77
|
+
`Secrets should be added to the client_auth->jwks->keys array in the config file, for example:`,
|
|
78
|
+
`client_auth:`,
|
|
79
|
+
` jwks:`,
|
|
80
|
+
` keys:`,
|
|
81
|
+
` - kty: oct`,
|
|
82
|
+
` alg: HS256`,
|
|
83
|
+
` kid: my-key-id`,
|
|
84
|
+
` k: base64-encoded-secret-here`
|
|
85
|
+
].join('\n')
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const specificKey = usableKeys.find((key) => key.kid === config.kid);
|
|
89
|
+
if (config.kid && !specificKey) {
|
|
90
|
+
this.styledError({ message: 'No key found with the given kid.' });
|
|
91
|
+
}
|
|
92
|
+
const key = (config.kid ? specificKey : usableKeys[0]);
|
|
93
|
+
const endpoint = project.linked.api_url;
|
|
94
|
+
const audiences = [endpoint];
|
|
95
|
+
for (const aud of instanceConfig.client_auth?.audience ?? []) {
|
|
96
|
+
audiences.push(aud);
|
|
97
|
+
}
|
|
98
|
+
const token = await new jose.SignJWT({})
|
|
99
|
+
.setProtectedHeader({ alg: key.alg, kid: key.kid })
|
|
100
|
+
.setSubject(config.subject)
|
|
101
|
+
.setAudience(audiences)
|
|
102
|
+
.setIssuer('powersync-cli')
|
|
103
|
+
.setIssuedAt()
|
|
104
|
+
.setExpirationTime(`${config.expiresInSeconds}s`)
|
|
105
|
+
.sign(key);
|
|
106
|
+
return token;
|
|
107
|
+
}
|
|
108
|
+
async run() {
|
|
109
|
+
const { flags } = await this.parse(GenerateToken);
|
|
110
|
+
const project = await this.loadProject(flags);
|
|
111
|
+
const token = await (project.linked.type === 'cloud'
|
|
112
|
+
? this.generateCloudToken(project, {
|
|
113
|
+
expiresInSeconds: flags['expires-in-seconds'],
|
|
114
|
+
subject: flags.subject
|
|
115
|
+
})
|
|
116
|
+
: this.generateSelfHostedToken(project, {
|
|
117
|
+
expiresInSeconds: flags['expires-in-seconds'],
|
|
118
|
+
kid: flags.kid,
|
|
119
|
+
subject: flags.subject
|
|
120
|
+
}));
|
|
121
|
+
// This is purposefully simple in order for the output to be easily used in shell scripts.
|
|
122
|
+
this.log(token);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommandHelpGroup, InstanceCommand } from '@powersync/cli-core';
|
|
2
|
+
export default class InitCloud extends InstanceCommand {
|
|
3
|
+
static commandHelpGroup: CommandHelpGroup;
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
};
|
|
9
|
+
static summary: string;
|
|
10
|
+
run(): Promise<void>;
|
|
11
|
+
}
|