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.
- package/dist/browser.cjs +382 -143
- package/dist/browser.d.cts +127 -5
- package/dist/browser.d.ts +127 -5
- package/dist/browser.js +380 -141
- package/dist/client.cjs +16 -11
- package/dist/client.d.cts +3 -1
- package/dist/client.d.ts +3 -1
- package/dist/client.js +16 -11
- package/dist/cloudflare-do.cjs +937 -286
- package/dist/cloudflare-do.d.cts +517 -15
- package/dist/cloudflare-do.d.ts +517 -15
- package/dist/cloudflare-do.js +928 -286
- package/dist/cloudflare.cjs +573 -18
- package/dist/cloudflare.d.cts +147 -2
- package/dist/cloudflare.d.ts +147 -2
- package/dist/cloudflare.js +573 -18
- package/dist/codegen.cjs +7 -15
- package/dist/index.cjs +32 -154
- package/dist/index.d.cts +2 -5
- package/dist/index.d.ts +2 -5
- package/dist/index.js +32 -152
- package/dist/node.cjs +404 -151
- package/dist/node.d.cts +132 -5
- package/dist/node.d.ts +132 -5
- package/dist/node.js +390 -145
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1855,7 +1855,7 @@ var init_BaseModel = __esm({
|
|
|
1855
1855
|
getDocumentId() {
|
|
1856
1856
|
return this._metaDocId;
|
|
1857
1857
|
}
|
|
1858
|
-
// id is
|
|
1858
|
+
// id is a plain property. Subclasses define it via defineModelSchema.
|
|
1859
1859
|
id;
|
|
1860
1860
|
type;
|
|
1861
1861
|
// This should be the modelName from ModelOptions
|
|
@@ -2402,7 +2402,7 @@ var init_BaseModel = __esm({
|
|
|
2402
2402
|
const verboseEnabled = Logger.getLogLevel() >= 5 /* VERBOSE */;
|
|
2403
2403
|
if (!schema || !schema.options || !schema.options.name || !schema.resolvedUniqueConstraints) {
|
|
2404
2404
|
throw new Error(
|
|
2405
|
-
`[${this.name}] Model schema is not registered, missing options, or missing resolvedUniqueConstraints. Did you forget to
|
|
2405
|
+
`[${this.name}] Model schema is not registered, missing options, or missing resolvedUniqueConstraints. Did you forget to call attachAndRegisterModel (or autoRegisterModel) for this model?`
|
|
2406
2406
|
);
|
|
2407
2407
|
}
|
|
2408
2408
|
const modelName = schema.options.name;
|
|
@@ -5110,11 +5110,11 @@ var init_ModelRegistry = __esm({
|
|
|
5110
5110
|
init_relationshipManager();
|
|
5111
5111
|
ModelRegistry = class _ModelRegistry {
|
|
5112
5112
|
static instance;
|
|
5113
|
-
// Stores globally registered model classes by their name
|
|
5113
|
+
// Stores globally registered model classes by their name
|
|
5114
5114
|
models = /* @__PURE__ */ new Map();
|
|
5115
|
-
// Stores options for globally registered models
|
|
5115
|
+
// Stores options for globally registered models
|
|
5116
5116
|
modelOptions = /* @__PURE__ */ new Map();
|
|
5117
|
-
// Stores fields for globally registered models (
|
|
5117
|
+
// Stores fields for globally registered models (or a static getter on class)
|
|
5118
5118
|
// For simplicity, let's assume fields can be derived or are less critical for this registry part
|
|
5119
5119
|
// private modelFields: Map<string, Map<string, FieldOptions>> = new Map();
|
|
5120
5120
|
// Holds the subset of models explicitly set for the current initialization session
|
|
@@ -5132,7 +5132,7 @@ var init_ModelRegistry = __esm({
|
|
|
5132
5132
|
registerModel(modelClass, options, fields) {
|
|
5133
5133
|
if (!options.name) {
|
|
5134
5134
|
throw new Error(
|
|
5135
|
-
`[ModelRegistry] Model class is missing a name in its
|
|
5135
|
+
`[ModelRegistry] Model class is missing a name in its options. Ensure the schema passed to defineModelSchema/createModelClass/attachAndRegisterModel includes a 'name' property.`
|
|
5136
5136
|
);
|
|
5137
5137
|
}
|
|
5138
5138
|
if (this.models.has(options.name)) {
|
|
@@ -5176,7 +5176,7 @@ var init_ModelRegistry = __esm({
|
|
|
5176
5176
|
}
|
|
5177
5177
|
return infos;
|
|
5178
5178
|
}
|
|
5179
|
-
// Helper to get model name from class (
|
|
5179
|
+
// Helper to get model name from class (set by attachAndRegisterModel/createModelClass)
|
|
5180
5180
|
getModelNameFromClass(modelClass) {
|
|
5181
5181
|
return modelClass.modelName;
|
|
5182
5182
|
}
|
|
@@ -5187,13 +5187,13 @@ var init_ModelRegistry = __esm({
|
|
|
5187
5187
|
const modelName = this.getModelNameFromClass(modelClass);
|
|
5188
5188
|
if (!modelName) {
|
|
5189
5189
|
console.warn(
|
|
5190
|
-
`[ModelRegistry] A model class provided to setExplicitModelsForSession does not have a static 'modelName' property or it's undefined. It will be ignored. Ensure
|
|
5190
|
+
`[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.`
|
|
5191
5191
|
);
|
|
5192
5192
|
return;
|
|
5193
5193
|
}
|
|
5194
5194
|
if (!this.models.has(modelName) || this.models.get(modelName) !== modelClass) {
|
|
5195
5195
|
console.warn(
|
|
5196
|
-
`[ModelRegistry] Model class with name ${modelName} provided to setExplicitModelsForSession was not found in the global
|
|
5196
|
+
`[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.`
|
|
5197
5197
|
);
|
|
5198
5198
|
return;
|
|
5199
5199
|
}
|
|
@@ -5223,7 +5223,7 @@ var init_ModelRegistry = __esm({
|
|
|
5223
5223
|
} else {
|
|
5224
5224
|
this.activeSessionModels = null;
|
|
5225
5225
|
console.log(
|
|
5226
|
-
"[ModelRegistry] No explicit models specified for session (undefined), will use all
|
|
5226
|
+
"[ModelRegistry] No explicit models specified for session (undefined), will use all globally registered models."
|
|
5227
5227
|
);
|
|
5228
5228
|
}
|
|
5229
5229
|
this.validateSessionModels();
|
|
@@ -5234,7 +5234,7 @@ var init_ModelRegistry = __esm({
|
|
|
5234
5234
|
const modelsToInitialize = this.activeSessionModels || this.models;
|
|
5235
5235
|
if (modelsToInitialize.size === 0) {
|
|
5236
5236
|
console.warn(
|
|
5237
|
-
"[ModelRegistry] No models to initialize (either no explicit models set for session or no models globally registered via
|
|
5237
|
+
"[ModelRegistry] No models to initialize (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
5238
5238
|
);
|
|
5239
5239
|
return;
|
|
5240
5240
|
}
|
|
@@ -5265,7 +5265,7 @@ var init_ModelRegistry = __esm({
|
|
|
5265
5265
|
const modelsToInitialize = this.activeSessionModels || this.models;
|
|
5266
5266
|
if (modelsToInitialize.size === 0) {
|
|
5267
5267
|
Logger.warn(
|
|
5268
|
-
"[ModelRegistry] No models to initialize for document (either no explicit models set for session or no models globally registered via
|
|
5268
|
+
"[ModelRegistry] No models to initialize for document (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
5269
5269
|
);
|
|
5270
5270
|
return;
|
|
5271
5271
|
}
|
|
@@ -5303,7 +5303,7 @@ var init_ModelRegistry = __esm({
|
|
|
5303
5303
|
const modelsToCleanup = this.activeSessionModels || this.models;
|
|
5304
5304
|
if (modelsToCleanup.size === 0) {
|
|
5305
5305
|
console.warn(
|
|
5306
|
-
"[ModelRegistry] No models to cleanup for document (either no explicit models set for session or no models globally registered via
|
|
5306
|
+
"[ModelRegistry] No models to cleanup for document (either no explicit models set for session or no models globally registered via attachAndRegisterModel)."
|
|
5307
5307
|
);
|
|
5308
5308
|
return;
|
|
5309
5309
|
}
|
|
@@ -6834,133 +6834,6 @@ async function resetJsBao() {
|
|
|
6834
6834
|
// src/index.ts
|
|
6835
6835
|
init_documentTypes();
|
|
6836
6836
|
init_BaseModel();
|
|
6837
|
-
|
|
6838
|
-
// src/models/decorators.ts
|
|
6839
|
-
init_ModelRegistry();
|
|
6840
|
-
init_BaseModel();
|
|
6841
|
-
init_sql();
|
|
6842
|
-
var SCHEMA_FIELDS_PROPERTY = "_jsbaoSchemaFields";
|
|
6843
|
-
function Field(options) {
|
|
6844
|
-
return function(target, propertyKey) {
|
|
6845
|
-
const fieldName = String(propertyKey);
|
|
6846
|
-
const modelConstructor = target.constructor || target;
|
|
6847
|
-
assertValidIdentifier(
|
|
6848
|
-
fieldName,
|
|
6849
|
-
`[Field Decorator] Invalid field name on ${modelConstructor?.name || "unknown"}`
|
|
6850
|
-
);
|
|
6851
|
-
if (!modelConstructor) {
|
|
6852
|
-
console.error(
|
|
6853
|
-
"[Field Decorator] Could not determine model constructor for field:",
|
|
6854
|
-
fieldName,
|
|
6855
|
-
"Target:",
|
|
6856
|
-
target
|
|
6857
|
-
);
|
|
6858
|
-
throw new Error(
|
|
6859
|
-
`Failed to resolve model constructor for field '${fieldName}'.`
|
|
6860
|
-
);
|
|
6861
|
-
}
|
|
6862
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
6863
|
-
modelConstructor,
|
|
6864
|
-
SCHEMA_FIELDS_PROPERTY
|
|
6865
|
-
)) {
|
|
6866
|
-
modelConstructor[SCHEMA_FIELDS_PROPERTY] = /* @__PURE__ */ new Map();
|
|
6867
|
-
}
|
|
6868
|
-
modelConstructor[SCHEMA_FIELDS_PROPERTY].set(
|
|
6869
|
-
fieldName,
|
|
6870
|
-
options
|
|
6871
|
-
);
|
|
6872
|
-
Logger.debug(
|
|
6873
|
-
`[Field Decorator] Staged field "${fieldName}" for class ${modelConstructor.name}:`,
|
|
6874
|
-
options
|
|
6875
|
-
);
|
|
6876
|
-
};
|
|
6877
|
-
}
|
|
6878
|
-
function Model(options) {
|
|
6879
|
-
return function(targetClass) {
|
|
6880
|
-
Logger.debug(
|
|
6881
|
-
`[Model Decorator] Registering model:`,
|
|
6882
|
-
targetClass.name,
|
|
6883
|
-
options
|
|
6884
|
-
);
|
|
6885
|
-
assertValidIdentifier(
|
|
6886
|
-
options.name,
|
|
6887
|
-
`[Model Decorator] Invalid model name for ${targetClass.name}`
|
|
6888
|
-
);
|
|
6889
|
-
const registry = ModelRegistry.getInstance();
|
|
6890
|
-
targetClass.modelName = options.name;
|
|
6891
|
-
targetClass.getSchema = () => {
|
|
6892
|
-
const combinedFields = /* @__PURE__ */ new Map();
|
|
6893
|
-
const hierarchyStack = [];
|
|
6894
|
-
let currentEvalClass = targetClass;
|
|
6895
|
-
while (currentEvalClass && currentEvalClass.prototype) {
|
|
6896
|
-
hierarchyStack.push(currentEvalClass);
|
|
6897
|
-
const parentPrototype = Object.getPrototypeOf(
|
|
6898
|
-
currentEvalClass.prototype
|
|
6899
|
-
);
|
|
6900
|
-
currentEvalClass = parentPrototype ? parentPrototype.constructor : null;
|
|
6901
|
-
if (currentEvalClass === Object) break;
|
|
6902
|
-
}
|
|
6903
|
-
for (let i = hierarchyStack.length - 1; i >= 0; i--) {
|
|
6904
|
-
const cls = hierarchyStack[i];
|
|
6905
|
-
if (Object.prototype.hasOwnProperty.call(cls, SCHEMA_FIELDS_PROPERTY)) {
|
|
6906
|
-
const fieldsForClass = cls[SCHEMA_FIELDS_PROPERTY];
|
|
6907
|
-
fieldsForClass.forEach((fieldOpts, fieldName) => {
|
|
6908
|
-
combinedFields.set(fieldName, fieldOpts);
|
|
6909
|
-
});
|
|
6910
|
-
}
|
|
6911
|
-
}
|
|
6912
|
-
const resolvedUniqueConstraints = [];
|
|
6913
|
-
combinedFields.forEach((fieldOpts, fieldName) => {
|
|
6914
|
-
if (fieldOpts.unique) {
|
|
6915
|
-
const constraintName = `${options.name}_${fieldName}_unique`;
|
|
6916
|
-
resolvedUniqueConstraints.push({
|
|
6917
|
-
name: constraintName,
|
|
6918
|
-
fields: [fieldName]
|
|
6919
|
-
});
|
|
6920
|
-
}
|
|
6921
|
-
});
|
|
6922
|
-
if (options.uniqueConstraints) {
|
|
6923
|
-
options.uniqueConstraints.forEach(
|
|
6924
|
-
(constraintConfig) => {
|
|
6925
|
-
const allFieldsExist = constraintConfig.fields.every(
|
|
6926
|
-
(f) => combinedFields.has(f)
|
|
6927
|
-
);
|
|
6928
|
-
if (!allFieldsExist) {
|
|
6929
|
-
console.warn(
|
|
6930
|
-
`[Model Decorator] Unique constraint "${constraintConfig.name}" for model "${options.name}" specifies non-existent fields. Skipping.`
|
|
6931
|
-
);
|
|
6932
|
-
return;
|
|
6933
|
-
}
|
|
6934
|
-
if (resolvedUniqueConstraints.some(
|
|
6935
|
-
(rc) => rc.name === constraintConfig.name
|
|
6936
|
-
)) {
|
|
6937
|
-
console.warn(
|
|
6938
|
-
`[Model Decorator] Duplicate unique constraint name "${constraintConfig.name}" in model "${options.name}".`
|
|
6939
|
-
);
|
|
6940
|
-
}
|
|
6941
|
-
resolvedUniqueConstraints.push({
|
|
6942
|
-
name: constraintConfig.name,
|
|
6943
|
-
fields: constraintConfig.fields
|
|
6944
|
-
});
|
|
6945
|
-
}
|
|
6946
|
-
);
|
|
6947
|
-
}
|
|
6948
|
-
return {
|
|
6949
|
-
class: targetClass,
|
|
6950
|
-
options,
|
|
6951
|
-
fields: combinedFields,
|
|
6952
|
-
resolvedUniqueConstraints
|
|
6953
|
-
};
|
|
6954
|
-
};
|
|
6955
|
-
const directFieldsForThisClass = Object.prototype.hasOwnProperty.call(
|
|
6956
|
-
targetClass,
|
|
6957
|
-
SCHEMA_FIELDS_PROPERTY
|
|
6958
|
-
) ? targetClass[SCHEMA_FIELDS_PROPERTY] : /* @__PURE__ */ new Map();
|
|
6959
|
-
registry.registerModel(targetClass, options, directFieldsForThisClass);
|
|
6960
|
-
};
|
|
6961
|
-
}
|
|
6962
|
-
|
|
6963
|
-
// src/index.ts
|
|
6964
6837
|
init_StringSet();
|
|
6965
6838
|
|
|
6966
6839
|
// src/models/schema.ts
|
|
@@ -7278,32 +7151,41 @@ import * as Y3 from "yjs";
|
|
|
7278
7151
|
function discoverSchema(yDoc) {
|
|
7279
7152
|
const models = {};
|
|
7280
7153
|
const metaNames = /* @__PURE__ */ new Set();
|
|
7281
|
-
for (const
|
|
7154
|
+
for (const key of yDoc.share.keys()) {
|
|
7282
7155
|
if (!key.startsWith("_meta_")) continue;
|
|
7283
|
-
|
|
7156
|
+
const map = materializeMap(yDoc, key);
|
|
7157
|
+
if (!map) continue;
|
|
7284
7158
|
const modelName = key.slice("_meta_".length);
|
|
7285
7159
|
metaNames.add(modelName);
|
|
7286
|
-
models[modelName] = readModelMeta(
|
|
7160
|
+
models[modelName] = readModelMeta(map);
|
|
7287
7161
|
}
|
|
7288
|
-
for (const
|
|
7162
|
+
for (const key of yDoc.share.keys()) {
|
|
7289
7163
|
if (key.startsWith("_")) continue;
|
|
7290
|
-
if (!(type instanceof Y3.Map)) continue;
|
|
7291
7164
|
if (metaNames.has(key)) continue;
|
|
7292
|
-
const
|
|
7293
|
-
if (
|
|
7294
|
-
const inferred = inferModelFromData(
|
|
7165
|
+
const map = materializeMap(yDoc, key);
|
|
7166
|
+
if (!map || map.size === 0) continue;
|
|
7167
|
+
const inferred = inferModelFromData(map);
|
|
7295
7168
|
if (inferred) models[key] = inferred;
|
|
7296
7169
|
}
|
|
7297
7170
|
return { models };
|
|
7298
7171
|
}
|
|
7299
7172
|
function discoverModelNames(yDoc) {
|
|
7300
7173
|
const names = [];
|
|
7301
|
-
for (const
|
|
7174
|
+
for (const key of yDoc.share.keys()) {
|
|
7302
7175
|
if (key.startsWith("_")) continue;
|
|
7303
|
-
|
|
7176
|
+
const map = materializeMap(yDoc, key);
|
|
7177
|
+
if (map) names.push(key);
|
|
7304
7178
|
}
|
|
7305
7179
|
return names.sort();
|
|
7306
7180
|
}
|
|
7181
|
+
function materializeMap(yDoc, key) {
|
|
7182
|
+
try {
|
|
7183
|
+
const map = yDoc.getMap(key);
|
|
7184
|
+
return map instanceof Y3.Map ? map : null;
|
|
7185
|
+
} catch {
|
|
7186
|
+
return null;
|
|
7187
|
+
}
|
|
7188
|
+
}
|
|
7307
7189
|
function readModelMeta(metaMap) {
|
|
7308
7190
|
const fields = {};
|
|
7309
7191
|
let constraints;
|
|
@@ -7523,10 +7405,8 @@ export {
|
|
|
7523
7405
|
DatabaseEngine,
|
|
7524
7406
|
DocumentClosedError,
|
|
7525
7407
|
DocumentResolutionError,
|
|
7526
|
-
Field,
|
|
7527
7408
|
LogLevel,
|
|
7528
7409
|
Logger,
|
|
7529
|
-
Model,
|
|
7530
7410
|
ModelRegistry,
|
|
7531
7411
|
RecordNotFoundError,
|
|
7532
7412
|
StringSet,
|