@basictech/schema 0.7.0 → 0.11.0-beta.2

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Basic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Basic Schema Library
2
2
 
3
- A TypeScript library for validating and managing Basic schemas and their data.
3
+ A TypeScript library for validating Basic schemas and inferring SDK record types. The package root
4
+ contains runtime validation; the dependency-light `@basictech/schema/define` subpath contains the
5
+ typed helpers and is the preferred app-schema import.
4
6
 
5
7
  ## Installation
6
8
 
@@ -14,6 +16,24 @@ npm install @basictech/schema
14
16
  import { validateSchema, validateData, generateEmptySchema, validateUpdateSchema } from '@basictech/schema'
15
17
  ```
16
18
 
19
+ For a typed 0.11 client schema:
20
+
21
+ ```typescript
22
+ import { defineSchema, type InferRecord } from '@basictech/schema/define'
23
+
24
+ export const schema = defineSchema({
25
+ project_id: 'my-project',
26
+ version: 1,
27
+ tables: {
28
+ todos: { fields: { title: { type: 'string', required: true } } },
29
+ },
30
+ })
31
+
32
+ type Todo = InferRecord<typeof schema, 'todos'>
33
+ ```
34
+
35
+ `@basictech/core` re-exports these helpers and adds `InferValue` (the value without `id`).
36
+
17
37
  ## API Reference
18
38
 
19
39
  ### `validateSchema(schema: any)`
@@ -171,4 +191,4 @@ if (dataValidation.valid) {
171
191
 
172
192
  ## License
173
193
 
174
- MIT
194
+ MIT
@@ -0,0 +1,43 @@
1
+ type BasicFieldType = 'string' | 'boolean' | 'number' | 'json';
2
+ interface BasicFieldDef {
3
+ type: BasicFieldType;
4
+ indexed?: boolean;
5
+ required?: boolean;
6
+ }
7
+ interface BasicTableDef {
8
+ name?: string;
9
+ type?: 'collection';
10
+ origin?: {
11
+ type: 'reference';
12
+ project_id: string;
13
+ table: string;
14
+ version?: number;
15
+ };
16
+ fields: Record<string, BasicFieldDef>;
17
+ }
18
+ interface BasicSchema {
19
+ project_id: string;
20
+ namespace?: string;
21
+ version: number;
22
+ tables: Record<string, BasicTableDef>;
23
+ }
24
+ /** Preserve schema literals for table and record type inference. */
25
+ declare function defineSchema<const S extends BasicSchema>(schema: S): S;
26
+ type FieldValue<F extends BasicFieldDef> = F['type'] extends 'string' ? string : F['type'] extends 'number' ? number : F['type'] extends 'boolean' ? boolean : F['type'] extends 'json' ? unknown : never;
27
+ type RequiredFieldNames<T extends BasicTableDef> = {
28
+ [K in keyof T['fields']]: T['fields'][K] extends {
29
+ required: true;
30
+ } ? K : never;
31
+ }[keyof T['fields']];
32
+ type OptionalFieldNames<T extends BasicTableDef> = Exclude<keyof T['fields'], RequiredFieldNames<T>>;
33
+ /** The record shape for a schema table, including its Basic record id. */
34
+ type InferRecord<S extends BasicSchema, T extends keyof S['tables']> = {
35
+ id: string;
36
+ } & {
37
+ [K in RequiredFieldNames<S['tables'][T]>]: FieldValue<S['tables'][T]['fields'][K]>;
38
+ } & {
39
+ [K in OptionalFieldNames<S['tables'][T]>]?: FieldValue<S['tables'][T]['fields'][K]>;
40
+ };
41
+ type TableNames<S extends BasicSchema> = keyof S['tables'] & string;
42
+
43
+ export { type BasicFieldDef, type BasicFieldType, type BasicSchema, type BasicTableDef, type InferRecord, type TableNames, defineSchema };
@@ -0,0 +1,43 @@
1
+ type BasicFieldType = 'string' | 'boolean' | 'number' | 'json';
2
+ interface BasicFieldDef {
3
+ type: BasicFieldType;
4
+ indexed?: boolean;
5
+ required?: boolean;
6
+ }
7
+ interface BasicTableDef {
8
+ name?: string;
9
+ type?: 'collection';
10
+ origin?: {
11
+ type: 'reference';
12
+ project_id: string;
13
+ table: string;
14
+ version?: number;
15
+ };
16
+ fields: Record<string, BasicFieldDef>;
17
+ }
18
+ interface BasicSchema {
19
+ project_id: string;
20
+ namespace?: string;
21
+ version: number;
22
+ tables: Record<string, BasicTableDef>;
23
+ }
24
+ /** Preserve schema literals for table and record type inference. */
25
+ declare function defineSchema<const S extends BasicSchema>(schema: S): S;
26
+ type FieldValue<F extends BasicFieldDef> = F['type'] extends 'string' ? string : F['type'] extends 'number' ? number : F['type'] extends 'boolean' ? boolean : F['type'] extends 'json' ? unknown : never;
27
+ type RequiredFieldNames<T extends BasicTableDef> = {
28
+ [K in keyof T['fields']]: T['fields'][K] extends {
29
+ required: true;
30
+ } ? K : never;
31
+ }[keyof T['fields']];
32
+ type OptionalFieldNames<T extends BasicTableDef> = Exclude<keyof T['fields'], RequiredFieldNames<T>>;
33
+ /** The record shape for a schema table, including its Basic record id. */
34
+ type InferRecord<S extends BasicSchema, T extends keyof S['tables']> = {
35
+ id: string;
36
+ } & {
37
+ [K in RequiredFieldNames<S['tables'][T]>]: FieldValue<S['tables'][T]['fields'][K]>;
38
+ } & {
39
+ [K in OptionalFieldNames<S['tables'][T]>]?: FieldValue<S['tables'][T]['fields'][K]>;
40
+ };
41
+ type TableNames<S extends BasicSchema> = keyof S['tables'] & string;
42
+
43
+ export { type BasicFieldDef, type BasicFieldType, type BasicSchema, type BasicTableDef, type InferRecord, type TableNames, defineSchema };
package/dist/define.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // define.ts
21
+ var define_exports = {};
22
+ __export(define_exports, {
23
+ defineSchema: () => defineSchema
24
+ });
25
+ module.exports = __toCommonJS(define_exports);
26
+ function defineSchema(schema) {
27
+ return schema;
28
+ }
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ defineSchema
32
+ });
33
+ //# sourceMappingURL=define.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../define.ts"],"sourcesContent":["export type BasicFieldType = 'string' | 'boolean' | 'number' | 'json'\n\nexport interface BasicFieldDef {\n type: BasicFieldType\n indexed?: boolean\n required?: boolean\n}\n\nexport interface BasicTableDef {\n name?: string\n type?: 'collection'\n origin?: {\n type: 'reference'\n project_id: string\n table: string\n version?: number\n }\n fields: Record<string, BasicFieldDef>\n}\n\nexport interface BasicSchema {\n project_id: string\n namespace?: string\n version: number\n tables: Record<string, BasicTableDef>\n}\n\n/** Preserve schema literals for table and record type inference. */\nexport function defineSchema<const S extends BasicSchema>(schema: S): S {\n return schema\n}\n\ntype FieldValue<F extends BasicFieldDef> =\n F['type'] extends 'string' ? string\n : F['type'] extends 'number' ? number\n : F['type'] extends 'boolean' ? boolean\n : F['type'] extends 'json' ? unknown\n : never\n\ntype RequiredFieldNames<T extends BasicTableDef> = {\n [K in keyof T['fields']]: T['fields'][K] extends { required: true } ? K : never\n}[keyof T['fields']]\n\ntype OptionalFieldNames<T extends BasicTableDef> = Exclude<keyof T['fields'], RequiredFieldNames<T>>\n\n/** The record shape for a schema table, including its Basic record id. */\nexport type InferRecord<S extends BasicSchema, T extends keyof S['tables']> =\n { id: string }\n & { [K in RequiredFieldNames<S['tables'][T]>]: FieldValue<S['tables'][T]['fields'][K]> }\n & { [K in OptionalFieldNames<S['tables'][T]>]?: FieldValue<S['tables'][T]['fields'][K]> }\n\nexport type TableNames<S extends BasicSchema> = keyof S['tables'] & string\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BO,SAAS,aAA0C,QAAc;AACtE,SAAO;AACT;","names":[]}
@@ -0,0 +1,8 @@
1
+ // define.ts
2
+ function defineSchema(schema) {
3
+ return schema;
4
+ }
5
+ export {
6
+ defineSchema
7
+ };
8
+ //# sourceMappingURL=define.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../define.ts"],"sourcesContent":["export type BasicFieldType = 'string' | 'boolean' | 'number' | 'json'\n\nexport interface BasicFieldDef {\n type: BasicFieldType\n indexed?: boolean\n required?: boolean\n}\n\nexport interface BasicTableDef {\n name?: string\n type?: 'collection'\n origin?: {\n type: 'reference'\n project_id: string\n table: string\n version?: number\n }\n fields: Record<string, BasicFieldDef>\n}\n\nexport interface BasicSchema {\n project_id: string\n namespace?: string\n version: number\n tables: Record<string, BasicTableDef>\n}\n\n/** Preserve schema literals for table and record type inference. */\nexport function defineSchema<const S extends BasicSchema>(schema: S): S {\n return schema\n}\n\ntype FieldValue<F extends BasicFieldDef> =\n F['type'] extends 'string' ? string\n : F['type'] extends 'number' ? number\n : F['type'] extends 'boolean' ? boolean\n : F['type'] extends 'json' ? unknown\n : never\n\ntype RequiredFieldNames<T extends BasicTableDef> = {\n [K in keyof T['fields']]: T['fields'][K] extends { required: true } ? K : never\n}[keyof T['fields']]\n\ntype OptionalFieldNames<T extends BasicTableDef> = Exclude<keyof T['fields'], RequiredFieldNames<T>>\n\n/** The record shape for a schema table, including its Basic record id. */\nexport type InferRecord<S extends BasicSchema, T extends keyof S['tables']> =\n { id: string }\n & { [K in RequiredFieldNames<S['tables'][T]>]: FieldValue<S['tables'][T]['fields'][K]> }\n & { [K in OptionalFieldNames<S['tables'][T]>]?: FieldValue<S['tables'][T]['fields'][K]> }\n\nexport type TableNames<S extends BasicSchema> = keyof S['tables'] & string\n"],"mappings":";AA4BO,SAAS,aAA0C,QAAc;AACtE,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,13 @@
1
1
  {
2
2
  "name": "@basictech/schema",
3
- "version": "0.7.0",
3
+ "version": "0.11.0-beta.2",
4
+ "private": false,
4
5
  "description": "utils for Basic Schema",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "node": ">=24.0.0"
9
+ },
10
+ "type": "commonjs",
5
11
  "main": "dist/index.js",
6
12
  "module": "dist/index.mjs",
7
13
  "types": "dist/index.d.ts",
@@ -15,32 +21,40 @@
15
21
  "types": "./dist/index.d.ts",
16
22
  "default": "./dist/index.js"
17
23
  }
24
+ },
25
+ "./define": {
26
+ "import": {
27
+ "types": "./dist/define.d.mts",
28
+ "default": "./dist/define.mjs"
29
+ },
30
+ "require": {
31
+ "types": "./dist/define.d.ts",
32
+ "default": "./dist/define.js"
33
+ }
18
34
  }
19
35
  },
20
36
  "files": [
21
37
  "dist"
22
38
  ],
23
- "sideEffects": false,
24
- "private": false,
25
- "publishConfig": {
26
- "access": "public"
27
- },
28
39
  "scripts": {
29
40
  "prebuild": "node scripts/generate-validator.js",
30
41
  "build": "tsup",
31
42
  "dev": "tsup --watch",
43
+ "prepack": "npm run build",
32
44
  "test": "vitest run",
33
45
  "test:watch": "vitest",
34
46
  "generate-validator": "node scripts/generate-validator.js"
35
47
  },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "sideEffects": false,
36
52
  "author": "",
37
- "license": "ISC",
38
53
  "dependencies": {
39
54
  "ajv": "^8.17.1",
40
55
  "ajv-errors": "^3.0.0"
41
56
  },
42
57
  "devDependencies": {
43
- "@repo/typescript-config": "*",
44
58
  "tsup": "^8.0.0",
45
59
  "typescript": "^5.0.0",
46
60
  "vitest": "^1.6.0"