js-bao 0.3.1 → 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.
- package/dist/browser.cjs +15 -142
- package/dist/browser.d.cts +2 -5
- package/dist/browser.d.ts +2 -5
- package/dist/browser.js +13 -140
- package/dist/codegen.cjs +2 -10
- package/dist/index.cjs +13 -144
- package/dist/index.d.cts +2 -5
- package/dist/index.d.ts +2 -5
- package/dist/index.js +13 -142
- package/dist/node.cjs +16 -145
- package/dist/node.d.cts +2 -5
- package/dist/node.d.ts +2 -5
- package/dist/node.js +13 -140
- package/package.json +1 -1
package/dist/browser.cjs
CHANGED
|
@@ -1634,11 +1634,11 @@ var init_ModelRegistry = __esm({
|
|
|
1634
1634
|
init_relationshipManager();
|
|
1635
1635
|
ModelRegistry = class _ModelRegistry {
|
|
1636
1636
|
static instance;
|
|
1637
|
-
// Stores globally registered model classes by their name
|
|
1637
|
+
// Stores globally registered model classes by their name
|
|
1638
1638
|
models = /* @__PURE__ */ new Map();
|
|
1639
|
-
// Stores options for globally registered models
|
|
1639
|
+
// Stores options for globally registered models
|
|
1640
1640
|
modelOptions = /* @__PURE__ */ new Map();
|
|
1641
|
-
// Stores fields for globally registered models (
|
|
1641
|
+
// Stores fields for globally registered models (or a static getter on class)
|
|
1642
1642
|
// For simplicity, let's assume fields can be derived or are less critical for this registry part
|
|
1643
1643
|
// private modelFields: Map<string, Map<string, FieldOptions>> = new Map();
|
|
1644
1644
|
// Holds the subset of models explicitly set for the current initialization session
|
|
@@ -1656,7 +1656,7 @@ var init_ModelRegistry = __esm({
|
|
|
1656
1656
|
registerModel(modelClass, options, fields) {
|
|
1657
1657
|
if (!options.name) {
|
|
1658
1658
|
throw new Error(
|
|
1659
|
-
`[ModelRegistry] Model class is missing a name in its
|
|
1659
|
+
`[ModelRegistry] Model class is missing a name in its options. Ensure the schema passed to defineModelSchema/createModelClass/attachAndRegisterModel includes a 'name' property.`
|
|
1660
1660
|
);
|
|
1661
1661
|
}
|
|
1662
1662
|
if (this.models.has(options.name)) {
|
|
@@ -1700,7 +1700,7 @@ var init_ModelRegistry = __esm({
|
|
|
1700
1700
|
}
|
|
1701
1701
|
return infos;
|
|
1702
1702
|
}
|
|
1703
|
-
// Helper to get model name from class (
|
|
1703
|
+
// Helper to get model name from class (set by attachAndRegisterModel/createModelClass)
|
|
1704
1704
|
getModelNameFromClass(modelClass) {
|
|
1705
1705
|
return modelClass.modelName;
|
|
1706
1706
|
}
|
|
@@ -1711,13 +1711,13 @@ var init_ModelRegistry = __esm({
|
|
|
1711
1711
|
const modelName = this.getModelNameFromClass(modelClass);
|
|
1712
1712
|
if (!modelName) {
|
|
1713
1713
|
console.warn(
|
|
1714
|
-
`[ModelRegistry] A model class provided to setExplicitModelsForSession does not have a static 'modelName' property or it's undefined. It will be ignored. Ensure
|
|
1714
|
+
`[ModelRegistry] A model class provided to setExplicitModelsForSession does not have a static 'modelName' property or it's undefined. It will be ignored. Ensure attachAndRegisterModel (or createModelClass) was called for this model.`
|
|
1715
1715
|
);
|
|
1716
1716
|
return;
|
|
1717
1717
|
}
|
|
1718
1718
|
if (!this.models.has(modelName) || this.models.get(modelName) !== modelClass) {
|
|
1719
1719
|
console.warn(
|
|
1720
|
-
`[ModelRegistry] Model class with name ${modelName} provided to setExplicitModelsForSession was not found in the global
|
|
1720
|
+
`[ModelRegistry] Model class with name ${modelName} provided to setExplicitModelsForSession was not found in the global registry or does not match. It will be ignored. Ensure the model file is imported and attachAndRegisterModel (or createModelClass) has run.`
|
|
1721
1721
|
);
|
|
1722
1722
|
return;
|
|
1723
1723
|
}
|
|
@@ -1747,7 +1747,7 @@ var init_ModelRegistry = __esm({
|
|
|
1747
1747
|
} else {
|
|
1748
1748
|
this.activeSessionModels = null;
|
|
1749
1749
|
console.log(
|
|
1750
|
-
"[ModelRegistry] No explicit models specified for session (undefined), will use all
|
|
1750
|
+
"[ModelRegistry] No explicit models specified for session (undefined), will use all globally registered models."
|
|
1751
1751
|
);
|
|
1752
1752
|
}
|
|
1753
1753
|
this.validateSessionModels();
|
|
@@ -1758,7 +1758,7 @@ var init_ModelRegistry = __esm({
|
|
|
1758
1758
|
const modelsToInitialize = this.activeSessionModels || this.models;
|
|
1759
1759
|
if (modelsToInitialize.size === 0) {
|
|
1760
1760
|
console.warn(
|
|
1761
|
-
"[ModelRegistry] No models to initialize (either no explicit models set for session or no models globally registered via
|
|
1761
|
+
"[ModelRegistry] No models to initialize (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
1762
1762
|
);
|
|
1763
1763
|
return;
|
|
1764
1764
|
}
|
|
@@ -1789,7 +1789,7 @@ var init_ModelRegistry = __esm({
|
|
|
1789
1789
|
const modelsToInitialize = this.activeSessionModels || this.models;
|
|
1790
1790
|
if (modelsToInitialize.size === 0) {
|
|
1791
1791
|
Logger.warn(
|
|
1792
|
-
"[ModelRegistry] No models to initialize for document (either no explicit models set for session or no models globally registered via
|
|
1792
|
+
"[ModelRegistry] No models to initialize for document (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
1793
1793
|
);
|
|
1794
1794
|
return;
|
|
1795
1795
|
}
|
|
@@ -1827,7 +1827,7 @@ var init_ModelRegistry = __esm({
|
|
|
1827
1827
|
const modelsToCleanup = this.activeSessionModels || this.models;
|
|
1828
1828
|
if (modelsToCleanup.size === 0) {
|
|
1829
1829
|
console.warn(
|
|
1830
|
-
"[ModelRegistry] No models to cleanup for document (either no explicit models set for session or no models globally registered via
|
|
1830
|
+
"[ModelRegistry] No models to cleanup for document (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
1831
1831
|
);
|
|
1832
1832
|
return;
|
|
1833
1833
|
}
|
|
@@ -2591,7 +2591,7 @@ var init_BaseModel = __esm({
|
|
|
2591
2591
|
getDocumentId() {
|
|
2592
2592
|
return this._metaDocId;
|
|
2593
2593
|
}
|
|
2594
|
-
// id is
|
|
2594
|
+
// id is a plain property. Subclasses define it via defineModelSchema.
|
|
2595
2595
|
id;
|
|
2596
2596
|
type;
|
|
2597
2597
|
// This should be the modelName from ModelOptions
|
|
@@ -3138,7 +3138,7 @@ var init_BaseModel = __esm({
|
|
|
3138
3138
|
const verboseEnabled = Logger.getLogLevel() >= 5 /* VERBOSE */;
|
|
3139
3139
|
if (!schema || !schema.options || !schema.options.name || !schema.resolvedUniqueConstraints) {
|
|
3140
3140
|
throw new Error(
|
|
3141
|
-
`[${this.name}] Model schema is not registered, missing options, or missing resolvedUniqueConstraints. Did you forget to
|
|
3141
|
+
`[${this.name}] Model schema is not registered, missing options, or missing resolvedUniqueConstraints. Did you forget to call attachAndRegisterModel (or autoRegisterModel) for this model?`
|
|
3142
3142
|
);
|
|
3143
3143
|
}
|
|
3144
3144
|
const modelName = schema.options.name;
|
|
@@ -5538,10 +5538,8 @@ __export(browser_exports, {
|
|
|
5538
5538
|
BaseModel: () => BaseModel2,
|
|
5539
5539
|
DatabaseEngine: () => DatabaseEngine,
|
|
5540
5540
|
DatabaseFactory: () => BrowserDatabaseFactory,
|
|
5541
|
-
Field: () => Field,
|
|
5542
5541
|
LogLevel: () => LogLevel,
|
|
5543
5542
|
Logger: () => Logger,
|
|
5544
|
-
Model: () => Model,
|
|
5545
5543
|
ModelRegistry: () => ModelRegistry,
|
|
5546
5544
|
RecordNotFoundError: () => RecordNotFoundError,
|
|
5547
5545
|
SqljsEngine: () => SqljsEngine,
|
|
@@ -5573,131 +5571,6 @@ module.exports = __toCommonJS(browser_exports);
|
|
|
5573
5571
|
init_BaseModel();
|
|
5574
5572
|
init_ModelRegistry();
|
|
5575
5573
|
|
|
5576
|
-
// src/models/decorators.ts
|
|
5577
|
-
init_ModelRegistry();
|
|
5578
|
-
init_BaseModel();
|
|
5579
|
-
init_sql();
|
|
5580
|
-
var SCHEMA_FIELDS_PROPERTY = "_jsbaoSchemaFields";
|
|
5581
|
-
function Field(options) {
|
|
5582
|
-
return function(target, propertyKey) {
|
|
5583
|
-
const fieldName = String(propertyKey);
|
|
5584
|
-
const modelConstructor = target.constructor || target;
|
|
5585
|
-
assertValidIdentifier(
|
|
5586
|
-
fieldName,
|
|
5587
|
-
`[Field Decorator] Invalid field name on ${modelConstructor?.name || "unknown"}`
|
|
5588
|
-
);
|
|
5589
|
-
if (!modelConstructor) {
|
|
5590
|
-
console.error(
|
|
5591
|
-
"[Field Decorator] Could not determine model constructor for field:",
|
|
5592
|
-
fieldName,
|
|
5593
|
-
"Target:",
|
|
5594
|
-
target
|
|
5595
|
-
);
|
|
5596
|
-
throw new Error(
|
|
5597
|
-
`Failed to resolve model constructor for field '${fieldName}'.`
|
|
5598
|
-
);
|
|
5599
|
-
}
|
|
5600
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
5601
|
-
modelConstructor,
|
|
5602
|
-
SCHEMA_FIELDS_PROPERTY
|
|
5603
|
-
)) {
|
|
5604
|
-
modelConstructor[SCHEMA_FIELDS_PROPERTY] = /* @__PURE__ */ new Map();
|
|
5605
|
-
}
|
|
5606
|
-
modelConstructor[SCHEMA_FIELDS_PROPERTY].set(
|
|
5607
|
-
fieldName,
|
|
5608
|
-
options
|
|
5609
|
-
);
|
|
5610
|
-
Logger.debug(
|
|
5611
|
-
`[Field Decorator] Staged field "${fieldName}" for class ${modelConstructor.name}:`,
|
|
5612
|
-
options
|
|
5613
|
-
);
|
|
5614
|
-
};
|
|
5615
|
-
}
|
|
5616
|
-
function Model(options) {
|
|
5617
|
-
return function(targetClass) {
|
|
5618
|
-
Logger.debug(
|
|
5619
|
-
`[Model Decorator] Registering model:`,
|
|
5620
|
-
targetClass.name,
|
|
5621
|
-
options
|
|
5622
|
-
);
|
|
5623
|
-
assertValidIdentifier(
|
|
5624
|
-
options.name,
|
|
5625
|
-
`[Model Decorator] Invalid model name for ${targetClass.name}`
|
|
5626
|
-
);
|
|
5627
|
-
const registry = ModelRegistry.getInstance();
|
|
5628
|
-
targetClass.modelName = options.name;
|
|
5629
|
-
targetClass.getSchema = () => {
|
|
5630
|
-
const combinedFields = /* @__PURE__ */ new Map();
|
|
5631
|
-
const hierarchyStack = [];
|
|
5632
|
-
let currentEvalClass = targetClass;
|
|
5633
|
-
while (currentEvalClass && currentEvalClass.prototype) {
|
|
5634
|
-
hierarchyStack.push(currentEvalClass);
|
|
5635
|
-
const parentPrototype = Object.getPrototypeOf(
|
|
5636
|
-
currentEvalClass.prototype
|
|
5637
|
-
);
|
|
5638
|
-
currentEvalClass = parentPrototype ? parentPrototype.constructor : null;
|
|
5639
|
-
if (currentEvalClass === Object) break;
|
|
5640
|
-
}
|
|
5641
|
-
for (let i = hierarchyStack.length - 1; i >= 0; i--) {
|
|
5642
|
-
const cls = hierarchyStack[i];
|
|
5643
|
-
if (Object.prototype.hasOwnProperty.call(cls, SCHEMA_FIELDS_PROPERTY)) {
|
|
5644
|
-
const fieldsForClass = cls[SCHEMA_FIELDS_PROPERTY];
|
|
5645
|
-
fieldsForClass.forEach((fieldOpts, fieldName) => {
|
|
5646
|
-
combinedFields.set(fieldName, fieldOpts);
|
|
5647
|
-
});
|
|
5648
|
-
}
|
|
5649
|
-
}
|
|
5650
|
-
const resolvedUniqueConstraints = [];
|
|
5651
|
-
combinedFields.forEach((fieldOpts, fieldName) => {
|
|
5652
|
-
if (fieldOpts.unique) {
|
|
5653
|
-
const constraintName = `${options.name}_${fieldName}_unique`;
|
|
5654
|
-
resolvedUniqueConstraints.push({
|
|
5655
|
-
name: constraintName,
|
|
5656
|
-
fields: [fieldName]
|
|
5657
|
-
});
|
|
5658
|
-
}
|
|
5659
|
-
});
|
|
5660
|
-
if (options.uniqueConstraints) {
|
|
5661
|
-
options.uniqueConstraints.forEach(
|
|
5662
|
-
(constraintConfig) => {
|
|
5663
|
-
const allFieldsExist = constraintConfig.fields.every(
|
|
5664
|
-
(f) => combinedFields.has(f)
|
|
5665
|
-
);
|
|
5666
|
-
if (!allFieldsExist) {
|
|
5667
|
-
console.warn(
|
|
5668
|
-
`[Model Decorator] Unique constraint "${constraintConfig.name}" for model "${options.name}" specifies non-existent fields. Skipping.`
|
|
5669
|
-
);
|
|
5670
|
-
return;
|
|
5671
|
-
}
|
|
5672
|
-
if (resolvedUniqueConstraints.some(
|
|
5673
|
-
(rc) => rc.name === constraintConfig.name
|
|
5674
|
-
)) {
|
|
5675
|
-
console.warn(
|
|
5676
|
-
`[Model Decorator] Duplicate unique constraint name "${constraintConfig.name}" in model "${options.name}".`
|
|
5677
|
-
);
|
|
5678
|
-
}
|
|
5679
|
-
resolvedUniqueConstraints.push({
|
|
5680
|
-
name: constraintConfig.name,
|
|
5681
|
-
fields: constraintConfig.fields
|
|
5682
|
-
});
|
|
5683
|
-
}
|
|
5684
|
-
);
|
|
5685
|
-
}
|
|
5686
|
-
return {
|
|
5687
|
-
class: targetClass,
|
|
5688
|
-
options,
|
|
5689
|
-
fields: combinedFields,
|
|
5690
|
-
resolvedUniqueConstraints
|
|
5691
|
-
};
|
|
5692
|
-
};
|
|
5693
|
-
const directFieldsForThisClass = Object.prototype.hasOwnProperty.call(
|
|
5694
|
-
targetClass,
|
|
5695
|
-
SCHEMA_FIELDS_PROPERTY
|
|
5696
|
-
) ? targetClass[SCHEMA_FIELDS_PROPERTY] : /* @__PURE__ */ new Map();
|
|
5697
|
-
registry.registerModel(targetClass, options, directFieldsForThisClass);
|
|
5698
|
-
};
|
|
5699
|
-
}
|
|
5700
|
-
|
|
5701
5574
|
// src/engines/DatabaseEngine.ts
|
|
5702
5575
|
var DatabaseEngine = class {
|
|
5703
5576
|
createTable(_modelName, _schema, _options) {
|
|
@@ -5737,7 +5610,7 @@ var DatabaseEngine = class {
|
|
|
5737
5610
|
};
|
|
5738
5611
|
|
|
5739
5612
|
// src/engines/SqljsEngine.ts
|
|
5740
|
-
var
|
|
5613
|
+
var import_sql4 = __toESM(require("sql.js"), 1);
|
|
5741
5614
|
init_BaseModel();
|
|
5742
5615
|
var import_async_mutex = require("async-mutex");
|
|
5743
5616
|
init_sql();
|
|
@@ -5814,7 +5687,7 @@ var SqljsEngine = class _SqljsEngine {
|
|
|
5814
5687
|
"[SqljsEngine] Attempting to load SQL.js WASM from:",
|
|
5815
5688
|
locateFileConfig("sql-wasm.wasm")
|
|
5816
5689
|
);
|
|
5817
|
-
_SqljsEngine.SQL = await (0,
|
|
5690
|
+
_SqljsEngine.SQL = await (0, import_sql4.default)({
|
|
5818
5691
|
locateFile: locateFileConfig
|
|
5819
5692
|
});
|
|
5820
5693
|
Logger.info("[SqljsEngine] SQL.js WASM loaded successfully.");
|
package/dist/browser.d.cts
CHANGED
|
@@ -663,9 +663,6 @@ declare class ModelRegistry {
|
|
|
663
663
|
private validateSessionModels;
|
|
664
664
|
}
|
|
665
665
|
|
|
666
|
-
declare function Field(options: FieldOptions): (target: any, propertyKey: string | symbol) => void;
|
|
667
|
-
declare function Model(options: ModelOptions): (targetClass: Function) => void;
|
|
668
|
-
|
|
669
666
|
interface SQLJSEngineOptions {
|
|
670
667
|
wasmURL?: string;
|
|
671
668
|
locateFile?: (file: string) => string;
|
|
@@ -751,7 +748,7 @@ interface InitJsBaoOptions {
|
|
|
751
748
|
/**
|
|
752
749
|
* Optional array of model classes to initialize.
|
|
753
750
|
* If not provided, the ORM will rely on models being registered
|
|
754
|
-
* via
|
|
751
|
+
* via attachAndRegisterModel (assuming they have been imported by the application).
|
|
755
752
|
*/
|
|
756
753
|
models?: (typeof BaseModel)[];
|
|
757
754
|
}
|
|
@@ -1000,4 +997,4 @@ declare function syncModelMeta(yDoc: Y.Doc, modelName: string, schema: ModelSche
|
|
|
1000
997
|
*/
|
|
1001
998
|
declare function syncInferredMeta(yDoc: Y.Doc, modelName: string, recordData: Record<string, any>): void;
|
|
1002
999
|
|
|
1003
|
-
export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineType, BrowserDatabaseFactory as DatabaseFactory, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema,
|
|
1000
|
+
export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineType, BrowserDatabaseFactory as DatabaseFactory, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, type FieldOptions, type FieldType, type GroupByField, type HasManyMethod, type HasManyThroughMethod, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, type ModelConstructor, type ModelOptions, ModelRegistry, type PaginatedResult, type PaginationOptions, RecordNotFoundError, type RefersToMethod, type RegisteredModelInfo, type RemoveRelationMethod, type SQLJSEngineOptions, type Schema, SqljsEngine, type SqljsEngineOptions, StringSet, type StringSetMembership, type UniqueConstraintConfig, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, clearMetaSyncCache, createModelClass, defineModelSchema, detectEnvironment, discoverModelNames, discoverSchema, features, generateULID, inferFieldType, initJsBao, isBrowser, isNode, loadSchemaFromTomlString, registerFunctionDefault, resetJsBao, schemaToToml, syncInferredMeta, syncModelMeta };
|
package/dist/browser.d.ts
CHANGED
|
@@ -663,9 +663,6 @@ declare class ModelRegistry {
|
|
|
663
663
|
private validateSessionModels;
|
|
664
664
|
}
|
|
665
665
|
|
|
666
|
-
declare function Field(options: FieldOptions): (target: any, propertyKey: string | symbol) => void;
|
|
667
|
-
declare function Model(options: ModelOptions): (targetClass: Function) => void;
|
|
668
|
-
|
|
669
666
|
interface SQLJSEngineOptions {
|
|
670
667
|
wasmURL?: string;
|
|
671
668
|
locateFile?: (file: string) => string;
|
|
@@ -751,7 +748,7 @@ interface InitJsBaoOptions {
|
|
|
751
748
|
/**
|
|
752
749
|
* Optional array of model classes to initialize.
|
|
753
750
|
* If not provided, the ORM will rely on models being registered
|
|
754
|
-
* via
|
|
751
|
+
* via attachAndRegisterModel (assuming they have been imported by the application).
|
|
755
752
|
*/
|
|
756
753
|
models?: (typeof BaseModel)[];
|
|
757
754
|
}
|
|
@@ -1000,4 +997,4 @@ declare function syncModelMeta(yDoc: Y.Doc, modelName: string, schema: ModelSche
|
|
|
1000
997
|
*/
|
|
1001
998
|
declare function syncInferredMeta(yDoc: Y.Doc, modelName: string, recordData: Record<string, any>): void;
|
|
1002
999
|
|
|
1003
|
-
export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineType, BrowserDatabaseFactory as DatabaseFactory, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema,
|
|
1000
|
+
export { type AddRelationMethod, type AggregationAliasDebugInfo, type AggregationOperation, type AggregationOptions, type AggregationResult, BaseModel, type DatabaseConfig, DatabaseEngine, type DatabaseEngineType, BrowserDatabaseFactory as DatabaseFactory, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, type FieldOptions, type FieldType, type GroupByField, type HasManyMethod, type HasManyThroughMethod, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, type ModelConstructor, type ModelOptions, ModelRegistry, type PaginatedResult, type PaginationOptions, RecordNotFoundError, type RefersToMethod, type RegisteredModelInfo, type RemoveRelationMethod, type SQLJSEngineOptions, type Schema, SqljsEngine, type SqljsEngineOptions, StringSet, type StringSetMembership, type UniqueConstraintConfig, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, clearMetaSyncCache, createModelClass, defineModelSchema, detectEnvironment, discoverModelNames, discoverSchema, features, generateULID, inferFieldType, initJsBao, isBrowser, isNode, loadSchemaFromTomlString, registerFunctionDefault, resetJsBao, schemaToToml, syncInferredMeta, syncModelMeta };
|
package/dist/browser.js
CHANGED
|
@@ -1612,11 +1612,11 @@ var init_ModelRegistry = __esm({
|
|
|
1612
1612
|
init_relationshipManager();
|
|
1613
1613
|
ModelRegistry = class _ModelRegistry {
|
|
1614
1614
|
static instance;
|
|
1615
|
-
// Stores globally registered model classes by their name
|
|
1615
|
+
// Stores globally registered model classes by their name
|
|
1616
1616
|
models = /* @__PURE__ */ new Map();
|
|
1617
|
-
// Stores options for globally registered models
|
|
1617
|
+
// Stores options for globally registered models
|
|
1618
1618
|
modelOptions = /* @__PURE__ */ new Map();
|
|
1619
|
-
// Stores fields for globally registered models (
|
|
1619
|
+
// Stores fields for globally registered models (or a static getter on class)
|
|
1620
1620
|
// For simplicity, let's assume fields can be derived or are less critical for this registry part
|
|
1621
1621
|
// private modelFields: Map<string, Map<string, FieldOptions>> = new Map();
|
|
1622
1622
|
// Holds the subset of models explicitly set for the current initialization session
|
|
@@ -1634,7 +1634,7 @@ var init_ModelRegistry = __esm({
|
|
|
1634
1634
|
registerModel(modelClass, options, fields) {
|
|
1635
1635
|
if (!options.name) {
|
|
1636
1636
|
throw new Error(
|
|
1637
|
-
`[ModelRegistry] Model class is missing a name in its
|
|
1637
|
+
`[ModelRegistry] Model class is missing a name in its options. Ensure the schema passed to defineModelSchema/createModelClass/attachAndRegisterModel includes a 'name' property.`
|
|
1638
1638
|
);
|
|
1639
1639
|
}
|
|
1640
1640
|
if (this.models.has(options.name)) {
|
|
@@ -1678,7 +1678,7 @@ var init_ModelRegistry = __esm({
|
|
|
1678
1678
|
}
|
|
1679
1679
|
return infos;
|
|
1680
1680
|
}
|
|
1681
|
-
// Helper to get model name from class (
|
|
1681
|
+
// Helper to get model name from class (set by attachAndRegisterModel/createModelClass)
|
|
1682
1682
|
getModelNameFromClass(modelClass) {
|
|
1683
1683
|
return modelClass.modelName;
|
|
1684
1684
|
}
|
|
@@ -1689,13 +1689,13 @@ var init_ModelRegistry = __esm({
|
|
|
1689
1689
|
const modelName = this.getModelNameFromClass(modelClass);
|
|
1690
1690
|
if (!modelName) {
|
|
1691
1691
|
console.warn(
|
|
1692
|
-
`[ModelRegistry] A model class provided to setExplicitModelsForSession does not have a static 'modelName' property or it's undefined. It will be ignored. Ensure
|
|
1692
|
+
`[ModelRegistry] A model class provided to setExplicitModelsForSession does not have a static 'modelName' property or it's undefined. It will be ignored. Ensure attachAndRegisterModel (or createModelClass) was called for this model.`
|
|
1693
1693
|
);
|
|
1694
1694
|
return;
|
|
1695
1695
|
}
|
|
1696
1696
|
if (!this.models.has(modelName) || this.models.get(modelName) !== modelClass) {
|
|
1697
1697
|
console.warn(
|
|
1698
|
-
`[ModelRegistry] Model class with name ${modelName} provided to setExplicitModelsForSession was not found in the global
|
|
1698
|
+
`[ModelRegistry] Model class with name ${modelName} provided to setExplicitModelsForSession was not found in the global registry or does not match. It will be ignored. Ensure the model file is imported and attachAndRegisterModel (or createModelClass) has run.`
|
|
1699
1699
|
);
|
|
1700
1700
|
return;
|
|
1701
1701
|
}
|
|
@@ -1725,7 +1725,7 @@ var init_ModelRegistry = __esm({
|
|
|
1725
1725
|
} else {
|
|
1726
1726
|
this.activeSessionModels = null;
|
|
1727
1727
|
console.log(
|
|
1728
|
-
"[ModelRegistry] No explicit models specified for session (undefined), will use all
|
|
1728
|
+
"[ModelRegistry] No explicit models specified for session (undefined), will use all globally registered models."
|
|
1729
1729
|
);
|
|
1730
1730
|
}
|
|
1731
1731
|
this.validateSessionModels();
|
|
@@ -1736,7 +1736,7 @@ var init_ModelRegistry = __esm({
|
|
|
1736
1736
|
const modelsToInitialize = this.activeSessionModels || this.models;
|
|
1737
1737
|
if (modelsToInitialize.size === 0) {
|
|
1738
1738
|
console.warn(
|
|
1739
|
-
"[ModelRegistry] No models to initialize (either no explicit models set for session or no models globally registered via
|
|
1739
|
+
"[ModelRegistry] No models to initialize (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
1740
1740
|
);
|
|
1741
1741
|
return;
|
|
1742
1742
|
}
|
|
@@ -1767,7 +1767,7 @@ var init_ModelRegistry = __esm({
|
|
|
1767
1767
|
const modelsToInitialize = this.activeSessionModels || this.models;
|
|
1768
1768
|
if (modelsToInitialize.size === 0) {
|
|
1769
1769
|
Logger.warn(
|
|
1770
|
-
"[ModelRegistry] No models to initialize for document (either no explicit models set for session or no models globally registered via
|
|
1770
|
+
"[ModelRegistry] No models to initialize for document (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
1771
1771
|
);
|
|
1772
1772
|
return;
|
|
1773
1773
|
}
|
|
@@ -1805,7 +1805,7 @@ var init_ModelRegistry = __esm({
|
|
|
1805
1805
|
const modelsToCleanup = this.activeSessionModels || this.models;
|
|
1806
1806
|
if (modelsToCleanup.size === 0) {
|
|
1807
1807
|
console.warn(
|
|
1808
|
-
"[ModelRegistry] No models to cleanup for document (either no explicit models set for session or no models globally registered via
|
|
1808
|
+
"[ModelRegistry] No models to cleanup for document (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
1809
1809
|
);
|
|
1810
1810
|
return;
|
|
1811
1811
|
}
|
|
@@ -2569,7 +2569,7 @@ var init_BaseModel = __esm({
|
|
|
2569
2569
|
getDocumentId() {
|
|
2570
2570
|
return this._metaDocId;
|
|
2571
2571
|
}
|
|
2572
|
-
// id is
|
|
2572
|
+
// id is a plain property. Subclasses define it via defineModelSchema.
|
|
2573
2573
|
id;
|
|
2574
2574
|
type;
|
|
2575
2575
|
// This should be the modelName from ModelOptions
|
|
@@ -3116,7 +3116,7 @@ var init_BaseModel = __esm({
|
|
|
3116
3116
|
const verboseEnabled = Logger.getLogLevel() >= 5 /* VERBOSE */;
|
|
3117
3117
|
if (!schema || !schema.options || !schema.options.name || !schema.resolvedUniqueConstraints) {
|
|
3118
3118
|
throw new Error(
|
|
3119
|
-
`[${this.name}] Model schema is not registered, missing options, or missing resolvedUniqueConstraints. Did you forget to
|
|
3119
|
+
`[${this.name}] Model schema is not registered, missing options, or missing resolvedUniqueConstraints. Did you forget to call attachAndRegisterModel (or autoRegisterModel) for this model?`
|
|
3120
3120
|
);
|
|
3121
3121
|
}
|
|
3122
3122
|
const modelName = schema.options.name;
|
|
@@ -5514,131 +5514,6 @@ var init_BaseModel = __esm({
|
|
|
5514
5514
|
init_BaseModel();
|
|
5515
5515
|
init_ModelRegistry();
|
|
5516
5516
|
|
|
5517
|
-
// src/models/decorators.ts
|
|
5518
|
-
init_ModelRegistry();
|
|
5519
|
-
init_BaseModel();
|
|
5520
|
-
init_sql();
|
|
5521
|
-
var SCHEMA_FIELDS_PROPERTY = "_jsbaoSchemaFields";
|
|
5522
|
-
function Field(options) {
|
|
5523
|
-
return function(target, propertyKey) {
|
|
5524
|
-
const fieldName = String(propertyKey);
|
|
5525
|
-
const modelConstructor = target.constructor || target;
|
|
5526
|
-
assertValidIdentifier(
|
|
5527
|
-
fieldName,
|
|
5528
|
-
`[Field Decorator] Invalid field name on ${modelConstructor?.name || "unknown"}`
|
|
5529
|
-
);
|
|
5530
|
-
if (!modelConstructor) {
|
|
5531
|
-
console.error(
|
|
5532
|
-
"[Field Decorator] Could not determine model constructor for field:",
|
|
5533
|
-
fieldName,
|
|
5534
|
-
"Target:",
|
|
5535
|
-
target
|
|
5536
|
-
);
|
|
5537
|
-
throw new Error(
|
|
5538
|
-
`Failed to resolve model constructor for field '${fieldName}'.`
|
|
5539
|
-
);
|
|
5540
|
-
}
|
|
5541
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
5542
|
-
modelConstructor,
|
|
5543
|
-
SCHEMA_FIELDS_PROPERTY
|
|
5544
|
-
)) {
|
|
5545
|
-
modelConstructor[SCHEMA_FIELDS_PROPERTY] = /* @__PURE__ */ new Map();
|
|
5546
|
-
}
|
|
5547
|
-
modelConstructor[SCHEMA_FIELDS_PROPERTY].set(
|
|
5548
|
-
fieldName,
|
|
5549
|
-
options
|
|
5550
|
-
);
|
|
5551
|
-
Logger.debug(
|
|
5552
|
-
`[Field Decorator] Staged field "${fieldName}" for class ${modelConstructor.name}:`,
|
|
5553
|
-
options
|
|
5554
|
-
);
|
|
5555
|
-
};
|
|
5556
|
-
}
|
|
5557
|
-
function Model(options) {
|
|
5558
|
-
return function(targetClass) {
|
|
5559
|
-
Logger.debug(
|
|
5560
|
-
`[Model Decorator] Registering model:`,
|
|
5561
|
-
targetClass.name,
|
|
5562
|
-
options
|
|
5563
|
-
);
|
|
5564
|
-
assertValidIdentifier(
|
|
5565
|
-
options.name,
|
|
5566
|
-
`[Model Decorator] Invalid model name for ${targetClass.name}`
|
|
5567
|
-
);
|
|
5568
|
-
const registry = ModelRegistry.getInstance();
|
|
5569
|
-
targetClass.modelName = options.name;
|
|
5570
|
-
targetClass.getSchema = () => {
|
|
5571
|
-
const combinedFields = /* @__PURE__ */ new Map();
|
|
5572
|
-
const hierarchyStack = [];
|
|
5573
|
-
let currentEvalClass = targetClass;
|
|
5574
|
-
while (currentEvalClass && currentEvalClass.prototype) {
|
|
5575
|
-
hierarchyStack.push(currentEvalClass);
|
|
5576
|
-
const parentPrototype = Object.getPrototypeOf(
|
|
5577
|
-
currentEvalClass.prototype
|
|
5578
|
-
);
|
|
5579
|
-
currentEvalClass = parentPrototype ? parentPrototype.constructor : null;
|
|
5580
|
-
if (currentEvalClass === Object) break;
|
|
5581
|
-
}
|
|
5582
|
-
for (let i = hierarchyStack.length - 1; i >= 0; i--) {
|
|
5583
|
-
const cls = hierarchyStack[i];
|
|
5584
|
-
if (Object.prototype.hasOwnProperty.call(cls, SCHEMA_FIELDS_PROPERTY)) {
|
|
5585
|
-
const fieldsForClass = cls[SCHEMA_FIELDS_PROPERTY];
|
|
5586
|
-
fieldsForClass.forEach((fieldOpts, fieldName) => {
|
|
5587
|
-
combinedFields.set(fieldName, fieldOpts);
|
|
5588
|
-
});
|
|
5589
|
-
}
|
|
5590
|
-
}
|
|
5591
|
-
const resolvedUniqueConstraints = [];
|
|
5592
|
-
combinedFields.forEach((fieldOpts, fieldName) => {
|
|
5593
|
-
if (fieldOpts.unique) {
|
|
5594
|
-
const constraintName = `${options.name}_${fieldName}_unique`;
|
|
5595
|
-
resolvedUniqueConstraints.push({
|
|
5596
|
-
name: constraintName,
|
|
5597
|
-
fields: [fieldName]
|
|
5598
|
-
});
|
|
5599
|
-
}
|
|
5600
|
-
});
|
|
5601
|
-
if (options.uniqueConstraints) {
|
|
5602
|
-
options.uniqueConstraints.forEach(
|
|
5603
|
-
(constraintConfig) => {
|
|
5604
|
-
const allFieldsExist = constraintConfig.fields.every(
|
|
5605
|
-
(f) => combinedFields.has(f)
|
|
5606
|
-
);
|
|
5607
|
-
if (!allFieldsExist) {
|
|
5608
|
-
console.warn(
|
|
5609
|
-
`[Model Decorator] Unique constraint "${constraintConfig.name}" for model "${options.name}" specifies non-existent fields. Skipping.`
|
|
5610
|
-
);
|
|
5611
|
-
return;
|
|
5612
|
-
}
|
|
5613
|
-
if (resolvedUniqueConstraints.some(
|
|
5614
|
-
(rc) => rc.name === constraintConfig.name
|
|
5615
|
-
)) {
|
|
5616
|
-
console.warn(
|
|
5617
|
-
`[Model Decorator] Duplicate unique constraint name "${constraintConfig.name}" in model "${options.name}".`
|
|
5618
|
-
);
|
|
5619
|
-
}
|
|
5620
|
-
resolvedUniqueConstraints.push({
|
|
5621
|
-
name: constraintConfig.name,
|
|
5622
|
-
fields: constraintConfig.fields
|
|
5623
|
-
});
|
|
5624
|
-
}
|
|
5625
|
-
);
|
|
5626
|
-
}
|
|
5627
|
-
return {
|
|
5628
|
-
class: targetClass,
|
|
5629
|
-
options,
|
|
5630
|
-
fields: combinedFields,
|
|
5631
|
-
resolvedUniqueConstraints
|
|
5632
|
-
};
|
|
5633
|
-
};
|
|
5634
|
-
const directFieldsForThisClass = Object.prototype.hasOwnProperty.call(
|
|
5635
|
-
targetClass,
|
|
5636
|
-
SCHEMA_FIELDS_PROPERTY
|
|
5637
|
-
) ? targetClass[SCHEMA_FIELDS_PROPERTY] : /* @__PURE__ */ new Map();
|
|
5638
|
-
registry.registerModel(targetClass, options, directFieldsForThisClass);
|
|
5639
|
-
};
|
|
5640
|
-
}
|
|
5641
|
-
|
|
5642
5517
|
// src/engines/DatabaseEngine.ts
|
|
5643
5518
|
var DatabaseEngine = class {
|
|
5644
5519
|
createTable(_modelName, _schema, _options) {
|
|
@@ -6871,10 +6746,8 @@ export {
|
|
|
6871
6746
|
BaseModel2 as BaseModel,
|
|
6872
6747
|
DatabaseEngine,
|
|
6873
6748
|
BrowserDatabaseFactory as DatabaseFactory,
|
|
6874
|
-
Field,
|
|
6875
6749
|
LogLevel,
|
|
6876
6750
|
Logger,
|
|
6877
|
-
Model,
|
|
6878
6751
|
ModelRegistry,
|
|
6879
6752
|
RecordNotFoundError,
|
|
6880
6753
|
SqljsEngine,
|
package/dist/codegen.cjs
CHANGED
|
@@ -312,7 +312,7 @@ var ModelAnalyzer = class {
|
|
|
312
312
|
return {};
|
|
313
313
|
}
|
|
314
314
|
/**
|
|
315
|
-
* Parse relationships object from
|
|
315
|
+
* Parse relationships object from a model schema definition
|
|
316
316
|
*/
|
|
317
317
|
parseRelationships(obj) {
|
|
318
318
|
const relationships = {};
|
|
@@ -461,14 +461,6 @@ var ModelAnalyzer = class {
|
|
|
461
461
|
type: this.getTypeString(member.type),
|
|
462
462
|
isOptional: !!member.questionToken
|
|
463
463
|
};
|
|
464
|
-
const memberDecorators = ts.getDecorators?.(member) || member.decorators;
|
|
465
|
-
if (memberDecorators) {
|
|
466
|
-
for (const decorator of memberDecorators) {
|
|
467
|
-
if (ts.isCallExpression(decorator.expression) && ts.isIdentifier(decorator.expression.expression) && decorator.expression.expression.text === "Field") {
|
|
468
|
-
field.decoratorOptions = {};
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
464
|
fields[fieldName] = field;
|
|
473
465
|
}
|
|
474
466
|
}
|
|
@@ -1189,7 +1181,7 @@ var SchemaExtractor = class {
|
|
|
1189
1181
|
// package.json
|
|
1190
1182
|
var package_default = {
|
|
1191
1183
|
name: "js-bao",
|
|
1192
|
-
version: "0.
|
|
1184
|
+
version: "0.4.0",
|
|
1193
1185
|
description: "A library providing data modeling capabilities which support live updates and queries.",
|
|
1194
1186
|
types: "dist/index.d.ts",
|
|
1195
1187
|
type: "module",
|