js-bao 0.3.0 → 0.4.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.
@@ -938,9 +938,10 @@ declare class DOClientEngine extends DatabaseEngine {
938
938
  /**
939
939
  * Save a record to the DO.
940
940
  */
941
- saveModel(modelName: string, id: string, data: Record<string, any>, stringSets?: Record<string, string[]>, options?: {
941
+ saveModel(modelName: string, id: string | undefined, data: Record<string, any>, stringSets?: Record<string, string[]>, options?: {
942
942
  ifNotExists?: boolean;
943
943
  condition?: DocumentFilter;
944
+ upsertOn?: string;
944
945
  }): Promise<string>;
945
946
  /**
946
947
  * Patch (partial update) a record in the DO.
@@ -1297,6 +1298,7 @@ interface SaveOptions {
1297
1298
  stringSets?: Record<string, string[]>;
1298
1299
  ifNotExists?: boolean;
1299
1300
  condition?: DocumentFilter;
1301
+ upsertOn?: string;
1300
1302
  }
1301
1303
  interface PatchOptions {
1302
1304
  stringSets?: Record<string, string[]>;
@@ -1383,4 +1385,147 @@ interface DoDb {
1383
1385
  */
1384
1386
  declare function connectDoDb(options: ConnectDoDbOptions): DoDb;
1385
1387
 
1386
- export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, connectDoDb, getActiveDOEngine, initJsBaoDO, resetJsBaoDO };
1388
+ /**
1389
+ * YDoc Schema Discovery
1390
+ *
1391
+ * Reads _meta_* YMaps from a YDoc and returns a plain JSON description
1392
+ * of the schema. Works with any YDoc — no BaseModel or database needed.
1393
+ *
1394
+ * Usage:
1395
+ * import { discoverSchema } from "js-bao";
1396
+ * const schema = discoverSchema(yDoc);
1397
+ */
1398
+
1399
+ interface DiscoveredField {
1400
+ type: string;
1401
+ indexed?: boolean;
1402
+ unique?: boolean;
1403
+ required?: boolean;
1404
+ default?: string | number | boolean;
1405
+ autoAssign?: boolean;
1406
+ maxLength?: number;
1407
+ maxCount?: number;
1408
+ }
1409
+ interface DiscoveredConstraint {
1410
+ type: string;
1411
+ fields: string[];
1412
+ }
1413
+ interface DiscoveredRelationship {
1414
+ type: string;
1415
+ model: string;
1416
+ [key: string]: any;
1417
+ }
1418
+ interface DiscoveredModel {
1419
+ fields: Record<string, DiscoveredField>;
1420
+ constraints?: Record<string, DiscoveredConstraint>;
1421
+ relationships?: Record<string, DiscoveredRelationship>;
1422
+ }
1423
+ interface DiscoveredSchema {
1424
+ models: Record<string, DiscoveredModel>;
1425
+ }
1426
+ /**
1427
+ * Discover the schema embedded in a YDoc by reading its `_meta_*` maps.
1428
+ *
1429
+ * Returns a plain JSON object describing every model that has metadata.
1430
+ * Does not require BaseModel, a database, or any schema registration —
1431
+ * just a Y.Doc.
1432
+ */
1433
+ declare function discoverSchema(yDoc: Y.Doc): DiscoveredSchema;
1434
+ /**
1435
+ * List model names that have data in the YDoc (non-`_` prefixed top-level maps).
1436
+ */
1437
+ declare function discoverModelNames(yDoc: Y.Doc): string[];
1438
+ /**
1439
+ * Convert a DiscoveredSchema to a TOML string matching the js-bao
1440
+ * schema format.
1441
+ *
1442
+ * Usage:
1443
+ * const schema = discoverSchema(yDoc);
1444
+ * const toml = schemaToToml(schema);
1445
+ */
1446
+ declare function schemaToToml(schema: DiscoveredSchema): string;
1447
+
1448
+ interface ResolvedUniqueConstraint {
1449
+ name: string;
1450
+ fields: string[];
1451
+ }
1452
+ interface ModelSchemaRuntimeShape {
1453
+ class: typeof BaseModel;
1454
+ options: ModelOptions;
1455
+ fields: Map<string, FieldOptions>;
1456
+ resolvedUniqueConstraints: ResolvedUniqueConstraint[];
1457
+ }
1458
+ interface DefinedModelSchema<TFields extends Record<string, FieldOptions> = Record<string, FieldOptions>> {
1459
+ name: string;
1460
+ fields: TFields;
1461
+ options: ModelOptions;
1462
+ runtimeShape?: ModelSchemaRuntimeShape;
1463
+ buildRuntimeShape(modelClass: typeof BaseModel): ModelSchemaRuntimeShape;
1464
+ }
1465
+
1466
+ /**
1467
+ * TOML Schema Loader
1468
+ *
1469
+ * Parses a TOML schema file and returns an array of DefinedModelSchema
1470
+ * objects. Can be used in place of manual `defineModelSchema()` calls.
1471
+ *
1472
+ * TOML conventions:
1473
+ * - Property names are snake_case (auto_assign, max_count, …)
1474
+ * - Model names and field names are as-is (user-chosen)
1475
+ * - Relationships live under [models.*.relationships.*]
1476
+ * - Compound unique constraints are [[models.*.unique_constraints]]
1477
+ */
1478
+
1479
+ /**
1480
+ * Parse a TOML string and return an array of DefinedModelSchema objects.
1481
+ */
1482
+ declare function loadSchemaFromTomlString(tomlString: string): DefinedModelSchema[];
1483
+
1484
+ /**
1485
+ * Meta Sync — writes _meta_* YMaps into a YDoc.
1486
+ *
1487
+ * Each model with data in a YDoc gets a `_meta_{modelName}` YMap that
1488
+ * describes the model's fields, constraints, and relationships. This
1489
+ * lets any language that can read a YDoc discover the schema without
1490
+ * external configuration.
1491
+ *
1492
+ * Two modes:
1493
+ * 1. **Schema-aware** — full schema known (from defineModelSchema / TOML).
1494
+ * Writes field metadata, constraints, and relationships.
1495
+ * 2. **Infer-on-write** — no schema. Infers types from record values.
1496
+ */
1497
+
1498
+ /**
1499
+ * Infer a _meta_ type string from a JS runtime value.
1500
+ */
1501
+ declare function inferFieldType(value: unknown): "string" | "number" | "boolean" | "stringset" | null;
1502
+ /**
1503
+ * Register a named function default so metaSync can encode it.
1504
+ * Called once per known default generator.
1505
+ */
1506
+ declare function registerFunctionDefault(fn: Function, name: string): void;
1507
+ /**
1508
+ * Clear the sync cache for a specific doc (or all docs).
1509
+ * Useful for testing or when schema changes at runtime.
1510
+ */
1511
+ declare function clearMetaSyncCache(yDoc?: Y.Doc): void;
1512
+ /**
1513
+ * Sync full schema metadata into `_meta_{modelName}` inside a YDoc.
1514
+ *
1515
+ * Call this inside an existing `yDoc.transact()`. Y.Map `.set()` on
1516
+ * identical values is a CRDT no-op so there is no overhead after the
1517
+ * first write.
1518
+ *
1519
+ * Skips the sync if this (yDoc, modelName) pair has already been synced
1520
+ * in the current session.
1521
+ */
1522
+ declare function syncModelMeta(yDoc: Y.Doc, modelName: string, schema: ModelSchemaRuntimeShape): void;
1523
+ /**
1524
+ * Infer-on-write: when no schema is available, infer field types from
1525
+ * the record data and write minimal _meta_.
1526
+ *
1527
+ * Call inside an existing `yDoc.transact()`.
1528
+ */
1529
+ declare function syncInferredMeta(yDoc: Y.Doc, modelName: string, recordData: Record<string, any>): void;
1530
+
1531
+ export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, clearMetaSyncCache, connectDoDb, discoverModelNames, discoverSchema, getActiveDOEngine, inferFieldType, initJsBaoDO, loadSchemaFromTomlString, registerFunctionDefault, resetJsBaoDO, schemaToToml, syncInferredMeta, syncModelMeta };
@@ -938,9 +938,10 @@ declare class DOClientEngine extends DatabaseEngine {
938
938
  /**
939
939
  * Save a record to the DO.
940
940
  */
941
- saveModel(modelName: string, id: string, data: Record<string, any>, stringSets?: Record<string, string[]>, options?: {
941
+ saveModel(modelName: string, id: string | undefined, data: Record<string, any>, stringSets?: Record<string, string[]>, options?: {
942
942
  ifNotExists?: boolean;
943
943
  condition?: DocumentFilter;
944
+ upsertOn?: string;
944
945
  }): Promise<string>;
945
946
  /**
946
947
  * Patch (partial update) a record in the DO.
@@ -1297,6 +1298,7 @@ interface SaveOptions {
1297
1298
  stringSets?: Record<string, string[]>;
1298
1299
  ifNotExists?: boolean;
1299
1300
  condition?: DocumentFilter;
1301
+ upsertOn?: string;
1300
1302
  }
1301
1303
  interface PatchOptions {
1302
1304
  stringSets?: Record<string, string[]>;
@@ -1383,4 +1385,147 @@ interface DoDb {
1383
1385
  */
1384
1386
  declare function connectDoDb(options: ConnectDoDbOptions): DoDb;
1385
1387
 
1386
- export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, connectDoDb, getActiveDOEngine, initJsBaoDO, resetJsBaoDO };
1388
+ /**
1389
+ * YDoc Schema Discovery
1390
+ *
1391
+ * Reads _meta_* YMaps from a YDoc and returns a plain JSON description
1392
+ * of the schema. Works with any YDoc — no BaseModel or database needed.
1393
+ *
1394
+ * Usage:
1395
+ * import { discoverSchema } from "js-bao";
1396
+ * const schema = discoverSchema(yDoc);
1397
+ */
1398
+
1399
+ interface DiscoveredField {
1400
+ type: string;
1401
+ indexed?: boolean;
1402
+ unique?: boolean;
1403
+ required?: boolean;
1404
+ default?: string | number | boolean;
1405
+ autoAssign?: boolean;
1406
+ maxLength?: number;
1407
+ maxCount?: number;
1408
+ }
1409
+ interface DiscoveredConstraint {
1410
+ type: string;
1411
+ fields: string[];
1412
+ }
1413
+ interface DiscoveredRelationship {
1414
+ type: string;
1415
+ model: string;
1416
+ [key: string]: any;
1417
+ }
1418
+ interface DiscoveredModel {
1419
+ fields: Record<string, DiscoveredField>;
1420
+ constraints?: Record<string, DiscoveredConstraint>;
1421
+ relationships?: Record<string, DiscoveredRelationship>;
1422
+ }
1423
+ interface DiscoveredSchema {
1424
+ models: Record<string, DiscoveredModel>;
1425
+ }
1426
+ /**
1427
+ * Discover the schema embedded in a YDoc by reading its `_meta_*` maps.
1428
+ *
1429
+ * Returns a plain JSON object describing every model that has metadata.
1430
+ * Does not require BaseModel, a database, or any schema registration —
1431
+ * just a Y.Doc.
1432
+ */
1433
+ declare function discoverSchema(yDoc: Y.Doc): DiscoveredSchema;
1434
+ /**
1435
+ * List model names that have data in the YDoc (non-`_` prefixed top-level maps).
1436
+ */
1437
+ declare function discoverModelNames(yDoc: Y.Doc): string[];
1438
+ /**
1439
+ * Convert a DiscoveredSchema to a TOML string matching the js-bao
1440
+ * schema format.
1441
+ *
1442
+ * Usage:
1443
+ * const schema = discoverSchema(yDoc);
1444
+ * const toml = schemaToToml(schema);
1445
+ */
1446
+ declare function schemaToToml(schema: DiscoveredSchema): string;
1447
+
1448
+ interface ResolvedUniqueConstraint {
1449
+ name: string;
1450
+ fields: string[];
1451
+ }
1452
+ interface ModelSchemaRuntimeShape {
1453
+ class: typeof BaseModel;
1454
+ options: ModelOptions;
1455
+ fields: Map<string, FieldOptions>;
1456
+ resolvedUniqueConstraints: ResolvedUniqueConstraint[];
1457
+ }
1458
+ interface DefinedModelSchema<TFields extends Record<string, FieldOptions> = Record<string, FieldOptions>> {
1459
+ name: string;
1460
+ fields: TFields;
1461
+ options: ModelOptions;
1462
+ runtimeShape?: ModelSchemaRuntimeShape;
1463
+ buildRuntimeShape(modelClass: typeof BaseModel): ModelSchemaRuntimeShape;
1464
+ }
1465
+
1466
+ /**
1467
+ * TOML Schema Loader
1468
+ *
1469
+ * Parses a TOML schema file and returns an array of DefinedModelSchema
1470
+ * objects. Can be used in place of manual `defineModelSchema()` calls.
1471
+ *
1472
+ * TOML conventions:
1473
+ * - Property names are snake_case (auto_assign, max_count, …)
1474
+ * - Model names and field names are as-is (user-chosen)
1475
+ * - Relationships live under [models.*.relationships.*]
1476
+ * - Compound unique constraints are [[models.*.unique_constraints]]
1477
+ */
1478
+
1479
+ /**
1480
+ * Parse a TOML string and return an array of DefinedModelSchema objects.
1481
+ */
1482
+ declare function loadSchemaFromTomlString(tomlString: string): DefinedModelSchema[];
1483
+
1484
+ /**
1485
+ * Meta Sync — writes _meta_* YMaps into a YDoc.
1486
+ *
1487
+ * Each model with data in a YDoc gets a `_meta_{modelName}` YMap that
1488
+ * describes the model's fields, constraints, and relationships. This
1489
+ * lets any language that can read a YDoc discover the schema without
1490
+ * external configuration.
1491
+ *
1492
+ * Two modes:
1493
+ * 1. **Schema-aware** — full schema known (from defineModelSchema / TOML).
1494
+ * Writes field metadata, constraints, and relationships.
1495
+ * 2. **Infer-on-write** — no schema. Infers types from record values.
1496
+ */
1497
+
1498
+ /**
1499
+ * Infer a _meta_ type string from a JS runtime value.
1500
+ */
1501
+ declare function inferFieldType(value: unknown): "string" | "number" | "boolean" | "stringset" | null;
1502
+ /**
1503
+ * Register a named function default so metaSync can encode it.
1504
+ * Called once per known default generator.
1505
+ */
1506
+ declare function registerFunctionDefault(fn: Function, name: string): void;
1507
+ /**
1508
+ * Clear the sync cache for a specific doc (or all docs).
1509
+ * Useful for testing or when schema changes at runtime.
1510
+ */
1511
+ declare function clearMetaSyncCache(yDoc?: Y.Doc): void;
1512
+ /**
1513
+ * Sync full schema metadata into `_meta_{modelName}` inside a YDoc.
1514
+ *
1515
+ * Call this inside an existing `yDoc.transact()`. Y.Map `.set()` on
1516
+ * identical values is a CRDT no-op so there is no overhead after the
1517
+ * first write.
1518
+ *
1519
+ * Skips the sync if this (yDoc, modelName) pair has already been synced
1520
+ * in the current session.
1521
+ */
1522
+ declare function syncModelMeta(yDoc: Y.Doc, modelName: string, schema: ModelSchemaRuntimeShape): void;
1523
+ /**
1524
+ * Infer-on-write: when no schema is available, infer field types from
1525
+ * the record data and write minimal _meta_.
1526
+ *
1527
+ * Call inside an existing `yDoc.transact()`.
1528
+ */
1529
+ declare function syncInferredMeta(yDoc: Y.Doc, modelName: string, recordData: Record<string, any>): void;
1530
+
1531
+ export { type AfterQueryContext, type AfterQueryResult, type AggregateRequest, type AggregateResponse, type AggregationOperation, type AggregationOptions, type AggregationResult, type BatchOperation, type BatchOperationResult, type BatchRequest, type BatchResponse, type BeforeDeleteContext, type BeforeQueryContext, type BeforeQueryResult, type BeforeSaveContext, type ConnectDoDbOptions, type CountRequest, type CountResponse, DOClientEngine, type DOClientEngineConfig, type DatabaseDOHooks, type DeleteRequest, type DeleteResponse, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, type DoDb, type DocumentFilter, type DropIndexRequest, type DropIndexResponse, type DropUniqueConstraintRequest, type DropUniqueConstraintResponse, type ErrorResponse, type GroupByField, type HookContext, type HookResult, type IncrementRequest, type IncrementResponse, type IndexEntry, type IndexListResponse, type InitJsBaoDOOptions, type InitJsBaoDOResult, type ModelAccessor, type PaginatedResult, type PatchOptions, type PatchRequest, type PatchResponse, type ProjectionSpec, type QueryOptions, type QueryRequest, type QueryResponse, type RegisterIndexRequest, type RegisterIndexResponse, type RegisterUniqueConstraintRequest, type RegisterUniqueConstraintResponse, type SaveOptions, type SaveRequest, type SaveResponse, type SortSpec, type StringSetMembership, type StringSetUpdateRequest, type StringSetUpdateResponse, type SyncIndexesRequest, type SyncIndexesResponse, type UniqueConstraintEntry, type UniqueConstraintListResponse, type WriteCondition, clearMetaSyncCache, connectDoDb, discoverModelNames, discoverSchema, getActiveDOEngine, inferFieldType, initJsBaoDO, loadSchemaFromTomlString, registerFunctionDefault, resetJsBaoDO, schemaToToml, syncInferredMeta, syncModelMeta };