@basictech/schema 0.7.0-beta.0 → 0.11.0-beta.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 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/dist/index.d.mts CHANGED
@@ -21,6 +21,62 @@ type Schema = {
21
21
  version: number;
22
22
  tables: any;
23
23
  };
24
+ type BasicFieldType = 'string' | 'boolean' | 'number' | 'json';
25
+ interface BasicFieldDef {
26
+ type: BasicFieldType;
27
+ indexed?: boolean;
28
+ required?: boolean;
29
+ }
30
+ interface BasicTableDef {
31
+ name?: string;
32
+ type?: 'collection';
33
+ origin?: {
34
+ type: 'reference';
35
+ project_id: string;
36
+ table: string;
37
+ version?: number;
38
+ };
39
+ fields: Record<string, BasicFieldDef>;
40
+ }
41
+ /** A full Basic schema document (`{ project_id, version, tables }`). */
42
+ interface BasicSchema {
43
+ project_id: string;
44
+ namespace?: string;
45
+ version: number;
46
+ tables: Record<string, BasicTableDef>;
47
+ }
48
+ /**
49
+ * Identity helper that preserves literal types so `InferRecord` can derive
50
+ * per-table record types:
51
+ *
52
+ * ```ts
53
+ * export const schema = defineSchema({
54
+ * project_id: '…',
55
+ * version: 1,
56
+ * tables: { todos: { fields: { title: { type: 'string', required: true } } } },
57
+ * })
58
+ * type Todo = InferRecord<typeof schema, 'todos'>
59
+ * // { id: string; title: string }
60
+ * ```
61
+ */
62
+ declare function defineSchema<const S extends BasicSchema>(schema: S): S;
63
+ type FieldTsType<F extends BasicFieldDef> = F['type'] extends 'string' ? string : F['type'] extends 'number' ? number : F['type'] extends 'boolean' ? boolean : F['type'] extends 'json' ? unknown : never;
64
+ type RequiredFieldKeys<T extends BasicTableDef> = {
65
+ [K in keyof T['fields']]: T['fields'][K] extends {
66
+ required: true;
67
+ } ? K : never;
68
+ }[keyof T['fields']];
69
+ type OptionalFieldKeys<T extends BasicTableDef> = Exclude<keyof T['fields'], RequiredFieldKeys<T>>;
70
+ /** The TypeScript record type for one table of a schema (plus its `id`). */
71
+ type InferRecord<S extends BasicSchema, T extends keyof S['tables']> = {
72
+ id: string;
73
+ } & {
74
+ [K in RequiredFieldKeys<S['tables'][T]>]: FieldTsType<S['tables'][T]['fields'][K]>;
75
+ } & {
76
+ [K in OptionalFieldKeys<S['tables'][T]>]?: FieldTsType<S['tables'][T]['fields'][K]>;
77
+ };
78
+ /** Table names of a schema. */
79
+ type TableNames<S extends BasicSchema> = keyof S['tables'] & string;
24
80
  /**
25
81
  * Compare two schemas and detect any differences between them
26
82
  * @param oldSchema - The original schema to compare against
@@ -220,4 +276,4 @@ declare function getJsonSchema(): {
220
276
  required: string[];
221
277
  };
222
278
 
223
- export { compareSchemas, generateEmptySchema, getJsonSchema, validateData, validateSchema, validateUpdateSchema };
279
+ export { type BasicFieldDef, type BasicFieldType, type BasicSchema, type BasicTableDef, type InferRecord, type TableNames, compareSchemas, defineSchema, generateEmptySchema, getJsonSchema, validateData, validateSchema, validateUpdateSchema };
package/dist/index.d.ts CHANGED
@@ -21,6 +21,62 @@ type Schema = {
21
21
  version: number;
22
22
  tables: any;
23
23
  };
24
+ type BasicFieldType = 'string' | 'boolean' | 'number' | 'json';
25
+ interface BasicFieldDef {
26
+ type: BasicFieldType;
27
+ indexed?: boolean;
28
+ required?: boolean;
29
+ }
30
+ interface BasicTableDef {
31
+ name?: string;
32
+ type?: 'collection';
33
+ origin?: {
34
+ type: 'reference';
35
+ project_id: string;
36
+ table: string;
37
+ version?: number;
38
+ };
39
+ fields: Record<string, BasicFieldDef>;
40
+ }
41
+ /** A full Basic schema document (`{ project_id, version, tables }`). */
42
+ interface BasicSchema {
43
+ project_id: string;
44
+ namespace?: string;
45
+ version: number;
46
+ tables: Record<string, BasicTableDef>;
47
+ }
48
+ /**
49
+ * Identity helper that preserves literal types so `InferRecord` can derive
50
+ * per-table record types:
51
+ *
52
+ * ```ts
53
+ * export const schema = defineSchema({
54
+ * project_id: '…',
55
+ * version: 1,
56
+ * tables: { todos: { fields: { title: { type: 'string', required: true } } } },
57
+ * })
58
+ * type Todo = InferRecord<typeof schema, 'todos'>
59
+ * // { id: string; title: string }
60
+ * ```
61
+ */
62
+ declare function defineSchema<const S extends BasicSchema>(schema: S): S;
63
+ type FieldTsType<F extends BasicFieldDef> = F['type'] extends 'string' ? string : F['type'] extends 'number' ? number : F['type'] extends 'boolean' ? boolean : F['type'] extends 'json' ? unknown : never;
64
+ type RequiredFieldKeys<T extends BasicTableDef> = {
65
+ [K in keyof T['fields']]: T['fields'][K] extends {
66
+ required: true;
67
+ } ? K : never;
68
+ }[keyof T['fields']];
69
+ type OptionalFieldKeys<T extends BasicTableDef> = Exclude<keyof T['fields'], RequiredFieldKeys<T>>;
70
+ /** The TypeScript record type for one table of a schema (plus its `id`). */
71
+ type InferRecord<S extends BasicSchema, T extends keyof S['tables']> = {
72
+ id: string;
73
+ } & {
74
+ [K in RequiredFieldKeys<S['tables'][T]>]: FieldTsType<S['tables'][T]['fields'][K]>;
75
+ } & {
76
+ [K in OptionalFieldKeys<S['tables'][T]>]?: FieldTsType<S['tables'][T]['fields'][K]>;
77
+ };
78
+ /** Table names of a schema. */
79
+ type TableNames<S extends BasicSchema> = keyof S['tables'] & string;
24
80
  /**
25
81
  * Compare two schemas and detect any differences between them
26
82
  * @param oldSchema - The original schema to compare against
@@ -220,4 +276,4 @@ declare function getJsonSchema(): {
220
276
  required: string[];
221
277
  };
222
278
 
223
- export { compareSchemas, generateEmptySchema, getJsonSchema, validateData, validateSchema, validateUpdateSchema };
279
+ export { type BasicFieldDef, type BasicFieldType, type BasicSchema, type BasicTableDef, type InferRecord, type TableNames, compareSchemas, defineSchema, generateEmptySchema, getJsonSchema, validateData, validateSchema, validateUpdateSchema };
package/dist/index.js CHANGED
@@ -18,16 +18,17 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // index.ts
21
- var schema_exports = {};
22
- __export(schema_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  compareSchemas: () => compareSchemas,
24
+ defineSchema: () => defineSchema,
24
25
  generateEmptySchema: () => generateEmptySchema,
25
26
  getJsonSchema: () => getJsonSchema,
26
27
  validateData: () => validateData,
27
28
  validateSchema: () => validateSchema,
28
29
  validateUpdateSchema: () => validateUpdateSchema
29
30
  });
30
- module.exports = __toCommonJS(schema_exports);
31
+ module.exports = __toCommonJS(index_exports);
31
32
 
32
33
  // generated-validator.js
33
34
  var validate = validate10;
@@ -749,6 +750,9 @@ function generateEmptySchema(project_id = "", version = 0) {
749
750
  }
750
751
  };
751
752
  }
753
+ function defineSchema(schema) {
754
+ return schema;
755
+ }
752
756
  function compareSchemas(oldSchema, newSchema) {
753
757
  const changes = _getSchemaChanges(oldSchema, newSchema);
754
758
  const valid = changes.length === 0 ? true : false;
@@ -765,8 +769,7 @@ function validateSchema(schema) {
765
769
  }
766
770
  function validateCaseInsensitiveNames(schema) {
767
771
  const errors = [];
768
- if (!schema.tables)
769
- return errors;
772
+ if (!schema.tables) return errors;
770
773
  const tableNames = /* @__PURE__ */ new Set();
771
774
  for (const tableName in schema.tables) {
772
775
  const lowerTableName = tableName.toLowerCase();
@@ -784,8 +787,7 @@ function validateCaseInsensitiveNames(schema) {
784
787
  }
785
788
  for (const tableName in schema.tables) {
786
789
  const table = schema.tables[tableName];
787
- if (!table.fields)
788
- continue;
790
+ if (!table.fields) continue;
789
791
  const fieldNames = /* @__PURE__ */ new Set();
790
792
  for (const fieldName in table.fields) {
791
793
  const lowerFieldName = fieldName.toLowerCase();
@@ -927,16 +929,13 @@ function _getSchemaChanges(oldSchema, newSchema) {
927
929
  for (const tableName in newSchema.tables) {
928
930
  const newTable = newSchema.tables[tableName];
929
931
  const oldTable = oldSchema.tables[tableName];
930
- if (!oldTable || !newTable.fields || !oldTable.fields)
931
- continue;
932
+ if (!oldTable || !newTable.fields || !oldTable.fields) continue;
932
933
  for (const fieldName in newTable.fields) {
933
934
  const newField = newTable.fields[fieldName];
934
935
  const oldField = oldTable.fields[fieldName];
935
- if (!oldField)
936
- continue;
936
+ if (!oldField) continue;
937
937
  for (const prop in newField) {
938
- if (prop === "type")
939
- continue;
938
+ if (prop === "type") continue;
940
939
  if (!(prop in oldField)) {
941
940
  changes.push({
942
941
  type: "field_property_added",
@@ -957,8 +956,7 @@ function _getSchemaChanges(oldSchema, newSchema) {
957
956
  }
958
957
  }
959
958
  for (const prop in oldField) {
960
- if (prop === "type")
961
- continue;
959
+ if (prop === "type") continue;
962
960
  if (!(prop in newField)) {
963
961
  changes.push({
964
962
  type: "field_property_removed",
@@ -1025,6 +1023,7 @@ function getJsonSchema() {
1025
1023
  // Annotate the CommonJS export names for ESM import in node:
1026
1024
  0 && (module.exports = {
1027
1025
  compareSchemas,
1026
+ defineSchema,
1028
1027
  generateEmptySchema,
1029
1028
  getJsonSchema,
1030
1029
  validateData,