Skip to content

JSON schema #257

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

Merged
merged 7 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [["@powersync/service-core", "@powersync/service-image"]],
"fixed": [["@powersync/service-core", "@powersync/service-image", "@powersync/service-schema"]],
"linked": [],
"access": "restricted",
"baseBranch": "origin/main",
Expand Down
7 changes: 7 additions & 0 deletions .changeset/lemon-hotels-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@powersync/service-types': minor
'@powersync/service-core': minor
'@powersync/service-schema': minor
---

Added JSON schema export for base PowerSyncConfig
1 change: 1 addition & 0 deletions packages/schema/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json-schema/
1 change: 1 addition & 0 deletions packages/schema/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @powersync/service-schema
67 changes: 67 additions & 0 deletions packages/schema/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Functional Source License, Version 1.1, Apache 2.0 Future License

## Abbreviation

FSL-1.1-Apache-2.0

## Notice

Copyright 2023-2024 Journey Mobile, Inc.

## Terms and Conditions

### Licensor ("We")

The party offering the Software under these Terms and Conditions.

### The Software

The "Software" is each version of the software that we make available under these Terms and Conditions, as indicated by our inclusion of these Terms and Conditions with the Software.

### License Grant

Subject to your compliance with this License Grant and the Patents, Redistribution and Trademark clauses below, we hereby grant you the right to use, copy, modify, create derivative works, publicly perform, publicly display and redistribute the Software for any Permitted Purpose identified below.

### Permitted Purpose

A Permitted Purpose is any purpose other than a Competing Use. A Competing Use means making the Software available to others in a commercial product or service that:

1. substitutes for the Software;
2. substitutes for any other product or service we offer using the Software that exists as of the date we make the Software available; or
3. offers the same or substantially similar functionality as the Software.

Permitted Purposes specifically include using the Software:

1. for your internal use and access;
2. for non-commercial education;
3. for non-commercial research; and
4. in connection with professional services that you provide to a licensee using the Software in accordance with these Terms and Conditions.

### Patents

To the extent your use for a Permitted Purpose would necessarily infringe our patents, the license grant above includes a license under our patents. If you make a claim against any party that the Software infringes or contributes to the infringement of any patent, then your patent license to the Software ends immediately.

### Redistribution

The Terms and Conditions apply to all copies, modifications and derivatives of the Software.
If you redistribute any copies, modifications or derivatives of the Software, you must include a copy of or a link to these Terms and Conditions and not remove any copyright notices provided in or with the Software.

### Disclaimer

THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.

### Trademarks

Except for displaying the License Details and identifying us as the origin of the Software, you have no right under these Terms and Conditions to use our trademarks, trade names, service marks or product names.

## Grant of Future License

We hereby irrevocably grant you an additional license to use the Software under the Apache License, Version 2.0 that is effective on the second anniversary of the date we make the Software available. On or after that date, you may use the Software under the Apache License, Version 2.0, in which case the following will apply:

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
9 changes: 9 additions & 0 deletions packages/schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PowerSync Service Schema

This package includes a JSON Schema for the PowerSync service configuration file.

This can be used to provide validations and suggestions for configuration files

```yaml
# yaml-language-server: $schema=https://unpkg.com/@powersync/service-schema@latest/json-schema/powersync-config.json
```
31 changes: 31 additions & 0 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@powersync/service-schema",
"version": "1.11.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "FSL-1.1-Apache-2.0",
"type": "module",
"publishConfig": {
"access": "public"
},
"files": [
"dist/**/*",
"json-schema/*"
],
"repository": "https://github.com/powersync-ja/powersync-service",
"scripts": {
"clean": "rm -r ./dist && tsc -b --clean",
"build:ts": "tsc -b",
"build": "pnpm build:ts && node ./dist/scripts/compile-json-schema.js"
},
"dependencies": {},
"devDependencies": {
"@powersync/service-module-postgres": "workspace:*",
"@powersync/service-module-postgres-storage": "workspace:*",
"@powersync/service-module-mongodb": "workspace:*",
"@powersync/service-module-mongodb-storage": "workspace:*",
"@powersync/service-module-mysql": "workspace:*",
"@powersync/service-types": "workspace:*",
"ts-codec": "^1.3.0"
}
}
1 change: 1 addition & 0 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This project does not yet include TypeScript source, but it does use TypeScript to depend on the build of @powersync/service-types.
43 changes: 43 additions & 0 deletions packages/schema/src/scripts/compile-json-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MongoStorageConfig } from '@powersync/service-module-mongodb-storage/types';
import { MongoConnectionConfig } from '@powersync/service-module-mongodb/types';
import { MySQLConnectionConfig } from '@powersync/service-module-mysql/types';
import { PostgresStorageConfig } from '@powersync/service-module-postgres-storage/types';
import { PostgresConnectionConfig } from '@powersync/service-module-postgres/types';
import { configFile } from '@powersync/service-types';
import fs from 'fs';
import path from 'path';
import * as t from 'ts-codec';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const schemaDir = path.join(__dirname, '../../json-schema');

fs.mkdirSync(schemaDir, { recursive: true });

// Merge configs for modules
const baseShape = configFile.powerSyncConfig.props.shape;

const mergedDataSourceConfig = configFile.genericDataSourceConfig
.or(PostgresConnectionConfig)
.or(MongoConnectionConfig)
.or(MySQLConnectionConfig);

const mergedStorageConfig = configFile.GenericStorageConfig.or(PostgresStorageConfig).or(MongoStorageConfig);

const mergedConfig = t.object({
...baseShape,
replication: t
.object({
...baseShape.replication.props.shape,
connections: t.array(mergedDataSourceConfig).optional()
})
.optional(),
storage: mergedStorageConfig.optional()
});

const mergedConfigSchema = t.generateJSONSchema(mergedConfig, {
allowAdditional: true,
parsers: [configFile.portParser]
});

fs.writeFileSync(path.join(schemaDir, 'powersync-config.json'), JSON.stringify(mergedConfigSchema, null, '\t'));
30 changes: 30 additions & 0 deletions packages/schema/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"declarationDir": "dist",
"rootDir": "src",
"skipLibCheck": true
},
"include": ["src"],
"references": [
{
"path": "../types"
},
{
"path": "../../modules/module-postgres"
},
{
"path": "../../modules/module-postgres-storage"
},
{
"path": "../../modules/module-mongodb"
},
{
"path": "../../modules/module-mongodb-storage"
},
{
"path": "../../modules/module-mysql"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as t from 'ts-codec';
import * as yaml from 'yaml';

import { configFile } from '@powersync/service-types';
import { schema } from '@powersync/lib-services-framework';
import { configFile } from '@powersync/service-types';

import { RunnerConfig } from '../types.js';

Expand All @@ -24,11 +23,7 @@ export enum ConfigFileFormat {
const YAML_ENV_PREFIX = 'PS_';

// ts-codec itself doesn't give great validation errors, so we use json schema for that
const configSchemaValidator = schema
.parseJSONSchema(
t.generateJSONSchema(configFile.powerSyncConfig, { allowAdditional: true, parsers: [configFile.portParser] })
)
.validator();
const configSchemaValidator = schema.parseJSONSchema(configFile.PowerSyncConfigJSONSchema).validator();

export abstract class ConfigCollector {
abstract get name(): string;
Expand Down
1 change: 1 addition & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build": "tsc -b"
},
"dependencies": {
"dedent": "^1.6.0",
"ts-codec": "^1.3.0",
"uri-js": "^4.4.1"
}
Expand Down
Loading