js-bao 0.3.0 → 0.3.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/dist/node.d.cts CHANGED
@@ -904,6 +904,136 @@ type HasManyThroughMethod<T> = (options?: PaginationOptions) => Promise<Paginate
904
904
  type AddRelationMethod<T> = (target: T | string) => Promise<void>;
905
905
  type RemoveRelationMethod<T> = (target: T | string) => Promise<void>;
906
906
 
907
+ /**
908
+ * YDoc Schema Discovery
909
+ *
910
+ * Reads _meta_* YMaps from a YDoc and returns a plain JSON description
911
+ * of the schema. Works with any YDoc — no BaseModel or database needed.
912
+ *
913
+ * Usage:
914
+ * import { discoverSchema } from "js-bao";
915
+ * const schema = discoverSchema(yDoc);
916
+ */
917
+
918
+ interface DiscoveredField {
919
+ type: string;
920
+ indexed?: boolean;
921
+ unique?: boolean;
922
+ required?: boolean;
923
+ default?: string | number | boolean;
924
+ autoAssign?: boolean;
925
+ maxLength?: number;
926
+ maxCount?: number;
927
+ }
928
+ interface DiscoveredConstraint {
929
+ type: string;
930
+ fields: string[];
931
+ }
932
+ interface DiscoveredRelationship {
933
+ type: string;
934
+ model: string;
935
+ [key: string]: any;
936
+ }
937
+ interface DiscoveredModel {
938
+ fields: Record<string, DiscoveredField>;
939
+ constraints?: Record<string, DiscoveredConstraint>;
940
+ relationships?: Record<string, DiscoveredRelationship>;
941
+ }
942
+ interface DiscoveredSchema {
943
+ models: Record<string, DiscoveredModel>;
944
+ }
945
+ /**
946
+ * Discover the schema embedded in a YDoc by reading its `_meta_*` maps.
947
+ *
948
+ * Returns a plain JSON object describing every model that has metadata.
949
+ * Does not require BaseModel, a database, or any schema registration —
950
+ * just a Y.Doc.
951
+ */
952
+ declare function discoverSchema(yDoc: Y.Doc): DiscoveredSchema;
953
+ /**
954
+ * List model names that have data in the YDoc (non-`_` prefixed top-level maps).
955
+ */
956
+ declare function discoverModelNames(yDoc: Y.Doc): string[];
957
+ /**
958
+ * Convert a DiscoveredSchema to a TOML string matching the js-bao
959
+ * schema format.
960
+ *
961
+ * Usage:
962
+ * const schema = discoverSchema(yDoc);
963
+ * const toml = schemaToToml(schema);
964
+ */
965
+ declare function schemaToToml(schema: DiscoveredSchema): string;
966
+
967
+ /**
968
+ * TOML Schema Loader
969
+ *
970
+ * Parses a TOML schema file and returns an array of DefinedModelSchema
971
+ * objects. Can be used in place of manual `defineModelSchema()` calls.
972
+ *
973
+ * TOML conventions:
974
+ * - Property names are snake_case (auto_assign, max_count, …)
975
+ * - Model names and field names are as-is (user-chosen)
976
+ * - Relationships live under [models.*.relationships.*]
977
+ * - Compound unique constraints are [[models.*.unique_constraints]]
978
+ */
979
+
980
+ /**
981
+ * Parse a TOML string and return an array of DefinedModelSchema objects.
982
+ */
983
+ declare function loadSchemaFromTomlString(tomlString: string): DefinedModelSchema[];
984
+ /**
985
+ * Read a TOML file from disk and return an array of DefinedModelSchema objects.
986
+ * Only available in Node.js environments.
987
+ */
988
+ declare function loadSchemaFromToml(filePath: string): Promise<DefinedModelSchema[]>;
989
+
990
+ /**
991
+ * Meta Sync — writes _meta_* YMaps into a YDoc.
992
+ *
993
+ * Each model with data in a YDoc gets a `_meta_{modelName}` YMap that
994
+ * describes the model's fields, constraints, and relationships. This
995
+ * lets any language that can read a YDoc discover the schema without
996
+ * external configuration.
997
+ *
998
+ * Two modes:
999
+ * 1. **Schema-aware** — full schema known (from defineModelSchema / TOML).
1000
+ * Writes field metadata, constraints, and relationships.
1001
+ * 2. **Infer-on-write** — no schema. Infers types from record values.
1002
+ */
1003
+
1004
+ /**
1005
+ * Infer a _meta_ type string from a JS runtime value.
1006
+ */
1007
+ declare function inferFieldType(value: unknown): "string" | "number" | "boolean" | "stringset" | null;
1008
+ /**
1009
+ * Register a named function default so metaSync can encode it.
1010
+ * Called once per known default generator.
1011
+ */
1012
+ declare function registerFunctionDefault(fn: Function, name: string): void;
1013
+ /**
1014
+ * Clear the sync cache for a specific doc (or all docs).
1015
+ * Useful for testing or when schema changes at runtime.
1016
+ */
1017
+ declare function clearMetaSyncCache(yDoc?: Y.Doc): void;
1018
+ /**
1019
+ * Sync full schema metadata into `_meta_{modelName}` inside a YDoc.
1020
+ *
1021
+ * Call this inside an existing `yDoc.transact()`. Y.Map `.set()` on
1022
+ * identical values is a CRDT no-op so there is no overhead after the
1023
+ * first write.
1024
+ *
1025
+ * Skips the sync if this (yDoc, modelName) pair has already been synced
1026
+ * in the current session.
1027
+ */
1028
+ declare function syncModelMeta(yDoc: Y.Doc, modelName: string, schema: ModelSchemaRuntimeShape): void;
1029
+ /**
1030
+ * Infer-on-write: when no schema is available, infer field types from
1031
+ * the record data and write minimal _meta_.
1032
+ *
1033
+ * Call inside an existing `yDoc.transact()`.
1034
+ */
1035
+ declare function syncInferredMeta(yDoc: Y.Doc, modelName: string, recordData: Record<string, any>): void;
1036
+
907
1037
  interface DumpOptions {
908
1038
  includeIndexes?: boolean;
909
1039
  }
@@ -947,4 +1077,4 @@ declare function summarizePlainYDoc(dump: PlainYDoc): DumpSummary;
947
1077
 
948
1078
  declare function getRecommendedNodeEngine(): Promise<string>;
949
1079
 
950
- export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineOptions, type DatabaseEngineType, NodeDatabaseFactory as DatabaseFactory, type DumpOptions, type DumpSummary, type DumpSummaryEntry, Field, type FieldOptions, type FieldType, type GroupByField, type HasManyMethod, type HasManyThroughMethod, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, Model, type ModelConstructor, type ModelOptions, ModelRegistry, type NodeSqliteEngineOptions, type PaginatedResult, type PaginationOptions, type PlainYDoc, RecordNotFoundError, type RefersToMethod, type RegisteredModelInfo, type RemoveRelationMethod, type SQLJSEngineOptions, type Schema, SqljsEngine, type SqljsEngineOptions, StringSet, type StringSetMembership, type UniqueConstraintConfig, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, constants, createModelClass, defineModelSchema, detectEnvironment, dumpYDocToPlain, features, generateULID, getRecommendedNodeEngine, initJsBao, isBrowser, isNode, isNodeModuleAvailable, resetJsBao, summarizePlainYDoc };
1080
+ export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineOptions, type DatabaseEngineType, NodeDatabaseFactory as DatabaseFactory, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, type DumpOptions, type DumpSummary, type DumpSummaryEntry, Field, type FieldOptions, type FieldType, type GroupByField, type HasManyMethod, type HasManyThroughMethod, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, Model, type ModelConstructor, type ModelOptions, ModelRegistry, type NodeSqliteEngineOptions, type PaginatedResult, type PaginationOptions, type PlainYDoc, RecordNotFoundError, type RefersToMethod, type RegisteredModelInfo, type RemoveRelationMethod, type SQLJSEngineOptions, type Schema, SqljsEngine, type SqljsEngineOptions, StringSet, type StringSetMembership, type UniqueConstraintConfig, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, clearMetaSyncCache, constants, createModelClass, defineModelSchema, detectEnvironment, discoverModelNames, discoverSchema, dumpYDocToPlain, features, generateULID, getRecommendedNodeEngine, inferFieldType, initJsBao, isBrowser, isNode, isNodeModuleAvailable, loadSchemaFromToml, loadSchemaFromTomlString, registerFunctionDefault, resetJsBao, schemaToToml, summarizePlainYDoc, syncInferredMeta, syncModelMeta };
package/dist/node.d.ts CHANGED
@@ -904,6 +904,136 @@ type HasManyThroughMethod<T> = (options?: PaginationOptions) => Promise<Paginate
904
904
  type AddRelationMethod<T> = (target: T | string) => Promise<void>;
905
905
  type RemoveRelationMethod<T> = (target: T | string) => Promise<void>;
906
906
 
907
+ /**
908
+ * YDoc Schema Discovery
909
+ *
910
+ * Reads _meta_* YMaps from a YDoc and returns a plain JSON description
911
+ * of the schema. Works with any YDoc — no BaseModel or database needed.
912
+ *
913
+ * Usage:
914
+ * import { discoverSchema } from "js-bao";
915
+ * const schema = discoverSchema(yDoc);
916
+ */
917
+
918
+ interface DiscoveredField {
919
+ type: string;
920
+ indexed?: boolean;
921
+ unique?: boolean;
922
+ required?: boolean;
923
+ default?: string | number | boolean;
924
+ autoAssign?: boolean;
925
+ maxLength?: number;
926
+ maxCount?: number;
927
+ }
928
+ interface DiscoveredConstraint {
929
+ type: string;
930
+ fields: string[];
931
+ }
932
+ interface DiscoveredRelationship {
933
+ type: string;
934
+ model: string;
935
+ [key: string]: any;
936
+ }
937
+ interface DiscoveredModel {
938
+ fields: Record<string, DiscoveredField>;
939
+ constraints?: Record<string, DiscoveredConstraint>;
940
+ relationships?: Record<string, DiscoveredRelationship>;
941
+ }
942
+ interface DiscoveredSchema {
943
+ models: Record<string, DiscoveredModel>;
944
+ }
945
+ /**
946
+ * Discover the schema embedded in a YDoc by reading its `_meta_*` maps.
947
+ *
948
+ * Returns a plain JSON object describing every model that has metadata.
949
+ * Does not require BaseModel, a database, or any schema registration —
950
+ * just a Y.Doc.
951
+ */
952
+ declare function discoverSchema(yDoc: Y.Doc): DiscoveredSchema;
953
+ /**
954
+ * List model names that have data in the YDoc (non-`_` prefixed top-level maps).
955
+ */
956
+ declare function discoverModelNames(yDoc: Y.Doc): string[];
957
+ /**
958
+ * Convert a DiscoveredSchema to a TOML string matching the js-bao
959
+ * schema format.
960
+ *
961
+ * Usage:
962
+ * const schema = discoverSchema(yDoc);
963
+ * const toml = schemaToToml(schema);
964
+ */
965
+ declare function schemaToToml(schema: DiscoveredSchema): string;
966
+
967
+ /**
968
+ * TOML Schema Loader
969
+ *
970
+ * Parses a TOML schema file and returns an array of DefinedModelSchema
971
+ * objects. Can be used in place of manual `defineModelSchema()` calls.
972
+ *
973
+ * TOML conventions:
974
+ * - Property names are snake_case (auto_assign, max_count, …)
975
+ * - Model names and field names are as-is (user-chosen)
976
+ * - Relationships live under [models.*.relationships.*]
977
+ * - Compound unique constraints are [[models.*.unique_constraints]]
978
+ */
979
+
980
+ /**
981
+ * Parse a TOML string and return an array of DefinedModelSchema objects.
982
+ */
983
+ declare function loadSchemaFromTomlString(tomlString: string): DefinedModelSchema[];
984
+ /**
985
+ * Read a TOML file from disk and return an array of DefinedModelSchema objects.
986
+ * Only available in Node.js environments.
987
+ */
988
+ declare function loadSchemaFromToml(filePath: string): Promise<DefinedModelSchema[]>;
989
+
990
+ /**
991
+ * Meta Sync — writes _meta_* YMaps into a YDoc.
992
+ *
993
+ * Each model with data in a YDoc gets a `_meta_{modelName}` YMap that
994
+ * describes the model's fields, constraints, and relationships. This
995
+ * lets any language that can read a YDoc discover the schema without
996
+ * external configuration.
997
+ *
998
+ * Two modes:
999
+ * 1. **Schema-aware** — full schema known (from defineModelSchema / TOML).
1000
+ * Writes field metadata, constraints, and relationships.
1001
+ * 2. **Infer-on-write** — no schema. Infers types from record values.
1002
+ */
1003
+
1004
+ /**
1005
+ * Infer a _meta_ type string from a JS runtime value.
1006
+ */
1007
+ declare function inferFieldType(value: unknown): "string" | "number" | "boolean" | "stringset" | null;
1008
+ /**
1009
+ * Register a named function default so metaSync can encode it.
1010
+ * Called once per known default generator.
1011
+ */
1012
+ declare function registerFunctionDefault(fn: Function, name: string): void;
1013
+ /**
1014
+ * Clear the sync cache for a specific doc (or all docs).
1015
+ * Useful for testing or when schema changes at runtime.
1016
+ */
1017
+ declare function clearMetaSyncCache(yDoc?: Y.Doc): void;
1018
+ /**
1019
+ * Sync full schema metadata into `_meta_{modelName}` inside a YDoc.
1020
+ *
1021
+ * Call this inside an existing `yDoc.transact()`. Y.Map `.set()` on
1022
+ * identical values is a CRDT no-op so there is no overhead after the
1023
+ * first write.
1024
+ *
1025
+ * Skips the sync if this (yDoc, modelName) pair has already been synced
1026
+ * in the current session.
1027
+ */
1028
+ declare function syncModelMeta(yDoc: Y.Doc, modelName: string, schema: ModelSchemaRuntimeShape): void;
1029
+ /**
1030
+ * Infer-on-write: when no schema is available, infer field types from
1031
+ * the record data and write minimal _meta_.
1032
+ *
1033
+ * Call inside an existing `yDoc.transact()`.
1034
+ */
1035
+ declare function syncInferredMeta(yDoc: Y.Doc, modelName: string, recordData: Record<string, any>): void;
1036
+
907
1037
  interface DumpOptions {
908
1038
  includeIndexes?: boolean;
909
1039
  }
@@ -947,4 +1077,4 @@ declare function summarizePlainYDoc(dump: PlainYDoc): DumpSummary;
947
1077
 
948
1078
  declare function getRecommendedNodeEngine(): Promise<string>;
949
1079
 
950
- export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineOptions, type DatabaseEngineType, NodeDatabaseFactory as DatabaseFactory, type DumpOptions, type DumpSummary, type DumpSummaryEntry, Field, type FieldOptions, type FieldType, type GroupByField, type HasManyMethod, type HasManyThroughMethod, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, Model, type ModelConstructor, type ModelOptions, ModelRegistry, type NodeSqliteEngineOptions, type PaginatedResult, type PaginationOptions, type PlainYDoc, RecordNotFoundError, type RefersToMethod, type RegisteredModelInfo, type RemoveRelationMethod, type SQLJSEngineOptions, type Schema, SqljsEngine, type SqljsEngineOptions, StringSet, type StringSetMembership, type UniqueConstraintConfig, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, constants, createModelClass, defineModelSchema, detectEnvironment, dumpYDocToPlain, features, generateULID, getRecommendedNodeEngine, initJsBao, isBrowser, isNode, isNodeModuleAvailable, resetJsBao, summarizePlainYDoc };
1080
+ export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineOptions, type DatabaseEngineType, NodeDatabaseFactory as DatabaseFactory, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, type DumpOptions, type DumpSummary, type DumpSummaryEntry, Field, type FieldOptions, type FieldType, type GroupByField, type HasManyMethod, type HasManyThroughMethod, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, Model, type ModelConstructor, type ModelOptions, ModelRegistry, type NodeSqliteEngineOptions, type PaginatedResult, type PaginationOptions, type PlainYDoc, RecordNotFoundError, type RefersToMethod, type RegisteredModelInfo, type RemoveRelationMethod, type SQLJSEngineOptions, type Schema, SqljsEngine, type SqljsEngineOptions, StringSet, type StringSetMembership, type UniqueConstraintConfig, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, clearMetaSyncCache, constants, createModelClass, defineModelSchema, detectEnvironment, discoverModelNames, discoverSchema, dumpYDocToPlain, features, generateULID, getRecommendedNodeEngine, inferFieldType, initJsBao, isBrowser, isNode, isNodeModuleAvailable, loadSchemaFromToml, loadSchemaFromTomlString, registerFunctionDefault, resetJsBao, schemaToToml, summarizePlainYDoc, syncInferredMeta, syncModelMeta };