@basictech/schema 0.6.0 → 0.7.0

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/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { ErrorObject as ErrorObject$1 } from 'ajv';
2
+
1
3
  declare function generateEmptySchema(project_id?: string, version?: number): {
2
4
  project_id: string;
3
5
  version: number;
@@ -19,6 +21,62 @@ type Schema = {
19
21
  version: number;
20
22
  tables: any;
21
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;
22
80
  /**
23
81
  * Compare two schemas and detect any differences between them
24
82
  * @param oldSchema - The original schema to compare against
@@ -39,7 +97,7 @@ declare function compareSchemas(oldSchema: any, newSchema: any): {
39
97
  */
40
98
  declare function validateSchema(schema: Schema): {
41
99
  valid: boolean;
42
- errors: ErrorObject[];
100
+ errors: ErrorObject$1[];
43
101
  };
44
102
  type ErrorObject = {
45
103
  keyword?: string;
@@ -79,7 +137,7 @@ type SchemaChange = {
79
137
  };
80
138
  declare function validateUpdateSchema(oldSchema: any, newSchema: any): {
81
139
  valid: boolean;
82
- errors: ErrorObject[];
140
+ errors: ErrorObject$1<string, Record<string, any>, unknown>[];
83
141
  message: string;
84
142
  changes?: undefined;
85
143
  } | {
@@ -95,6 +153,14 @@ declare function validateUpdateSchema(oldSchema: any, newSchema: any): {
95
153
  }[];
96
154
  message: string;
97
155
  changes?: undefined;
156
+ } | {
157
+ valid: boolean;
158
+ errors: {
159
+ change: SchemaChange;
160
+ message: string;
161
+ }[];
162
+ message: string;
163
+ changes?: undefined;
98
164
  } | {
99
165
  valid: boolean;
100
166
  changes: SchemaChange[];
@@ -210,4 +276,4 @@ declare function getJsonSchema(): {
210
276
  required: string[];
211
277
  };
212
278
 
213
- 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
@@ -1,3 +1,5 @@
1
+ import { ErrorObject as ErrorObject$1 } from 'ajv';
2
+
1
3
  declare function generateEmptySchema(project_id?: string, version?: number): {
2
4
  project_id: string;
3
5
  version: number;
@@ -19,6 +21,62 @@ type Schema = {
19
21
  version: number;
20
22
  tables: any;
21
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;
22
80
  /**
23
81
  * Compare two schemas and detect any differences between them
24
82
  * @param oldSchema - The original schema to compare against
@@ -39,7 +97,7 @@ declare function compareSchemas(oldSchema: any, newSchema: any): {
39
97
  */
40
98
  declare function validateSchema(schema: Schema): {
41
99
  valid: boolean;
42
- errors: ErrorObject[];
100
+ errors: ErrorObject$1[];
43
101
  };
44
102
  type ErrorObject = {
45
103
  keyword?: string;
@@ -79,7 +137,7 @@ type SchemaChange = {
79
137
  };
80
138
  declare function validateUpdateSchema(oldSchema: any, newSchema: any): {
81
139
  valid: boolean;
82
- errors: ErrorObject[];
140
+ errors: ErrorObject$1<string, Record<string, any>, unknown>[];
83
141
  message: string;
84
142
  changes?: undefined;
85
143
  } | {
@@ -95,6 +153,14 @@ declare function validateUpdateSchema(oldSchema: any, newSchema: any): {
95
153
  }[];
96
154
  message: string;
97
155
  changes?: undefined;
156
+ } | {
157
+ valid: boolean;
158
+ errors: {
159
+ change: SchemaChange;
160
+ message: string;
161
+ }[];
162
+ message: string;
163
+ changes?: undefined;
98
164
  } | {
99
165
  valid: boolean;
100
166
  changes: SchemaChange[];
@@ -210,4 +276,4 @@ declare function getJsonSchema(): {
210
276
  required: string[];
211
277
  };
212
278
 
213
- 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 };