js-bao / BaseModel
Class: BaseModel
Implements
StringSetChangeTracker
Constructors
Constructor
new BaseModel(
data):BaseModel
Parameters
data
Partial<any> = {}
Returns
BaseModel
Properties
id
id:
string
type
type:
string
connectedDocuments
protectedstaticconnectedDocuments:Map<string, {permissionHint:DocumentPermissionHint;yDoc:Doc; }>
dbInstance
protectedstaticdbInstance:DatabaseEngine|null=null
documentYMaps
protectedstaticdocumentYMaps:Map<string,YMap<any>>
modelName?
staticoptionalmodelName:string
Accessors
hasUnsavedChanges
Get Signature
get hasUnsavedChanges():
boolean
Returns
boolean
isDirty
Get Signature
get isDirty():
boolean
Returns
boolean
Methods
delete()
delete():
Promise<void>
Returns
Promise<void>
discardChanges()
discardChanges():
void
Returns
void
getChangedFields()
getChangedFields():
string[]
Returns
string[]
getCurrentJSState()
protectedgetCurrentJSState():Record<string,any>
Returns
Record<string, any>
getCurrentValue()
getCurrentValue(
fieldKey):any
Parameters
fieldKey
string
Returns
any
getDocumentId()
getDocumentId():
string|null
Returns the document id this instance is associated with, or null if not resolved yet.
Returns
string | null
getOriginalValue()
getOriginalValue(
fieldKey):any
Parameters
fieldKey
string
Returns
any
hasFieldChanged()
hasFieldChanged(
fieldKey):boolean
Parameters
fieldKey
string
Returns
boolean
markStringSetChange()
markStringSetChange(
fieldName,operation,value?):void
Parameters
fieldName
string
operation
"add" | "remove" | "clear"
value?
string
Returns
void
Implementation of
StringSetChangeTracker.markStringSetChange
save()
save(
options?):Promise<void>
Parameters
options?
Returns
Promise<void>
toJSON()
protectedtoJSON():Record<string,any>
Returns
Record<string, any>
validateBeforeSave()
protectedvalidateBeforeSave():void
Returns
void
validateFieldValue()
protectedvalidateFieldValue(fieldKey,value):void
Parameters
fieldKey
string
value
any
Returns
void
aggregate()
staticaggregate<T>(this,options):Promise<AggregationResult>
Main aggregation API - performs grouping, faceting, and statistical operations
Type Parameters
T
T extends BaseModel
Parameters
this
(...args) => T
options
AggregationOptions
Aggregation configuration with groupBy, operations, filter, limit, and sort
Returns
Promise<AggregationResult>
Nested object structure with aggregation results
Examples
// Simple facet count
const tagCounts = await Model.aggregate({
groupBy: ['tags'],
operations: [{ type: 'count' }]
});
// Result: { red: 15, blue: 8, green: 12 }// Multi-dimensional grouping with multiple operations
const categoryStats = await Model.aggregate({
groupBy: ['category', 'status'],
operations: [
{ type: 'count' },
{ type: 'sum', field: 'amount' },
{ type: 'avg', field: 'score' }
],
filter: { active: true },
sort: { field: 'count', direction: 'desc' },
limit: 10
});// StringSet membership grouping
const urgentCounts = await Model.aggregate({
groupBy: [{ field: 'tags', contains: 'urgent' }],
operations: [{ type: 'count' }]
});
// Result: { true: 5, false: 23 }attachFieldAccessors()
staticattachFieldAccessors(modelClass,fields):void
Parameters
modelClass
typeof BaseModel
fields
Map<string, FieldOptions>
Returns
void
buildAggregationQuery()
protectedstaticbuildAggregationQuery(options,schema,modelName):AggregationQueryPlan
Build SQL query for structured aggregation
Parameters
options
AggregationOptions
schema
any
modelName
string
Returns
AggregationQueryPlan
buildRegularAggregationQuery()
protectedstaticbuildRegularAggregationQuery(regularGroupBy,stringSetMemberships,options,translator,modelName):AggregationQueryPlan
Build query for regular field aggregation
Parameters
regularGroupBy
string[]
stringSetMemberships
StringSetMembership[]
options
AggregationOptions
translator
DocumentQueryTranslator
modelName
string
Returns
AggregationQueryPlan
buildStringSetFacetQuery()
protectedstaticbuildStringSetFacetQuery(stringSetFields,options,translator,modelName):AggregationQueryPlan
Build query for StringSet facet counts
Parameters
stringSetFields
string[]
options
AggregationOptions
translator
DocumentQueryTranslator
modelName
string
Returns
AggregationQueryPlan
cleanupDocumentData()
staticcleanupDocumentData(docId):Promise<void>
Parameters
docId
string
Returns
Promise<void>
clearGlobalDefaultDocumentId()
staticclearGlobalDefaultDocumentId():void
Returns
void
clearModelDefaultDocumentIds()
staticclearModelDefaultDocumentIds():void
Returns
void
count()
staticcount(this,filter,options?):Promise<number>
Document-style count API
Parameters
this
(...args) => any
filter
DocumentFilter = {}
options?
Pick<QueryOptions, "documents">
Returns
Promise<number>
find()
staticfind<T>(this,id):Promise<T|null>
Type Parameters
T
T extends BaseModel
Parameters
this
(...args) => T
id
string
Returns
Promise<T | null>
findAll()
staticfindAll<T>(this):Promise<T[]>
Type Parameters
T
T extends BaseModel
Parameters
this
(...args) => T
Returns
Promise<T[]>
findByUnique()
staticfindByUnique<T>(this,constraintName,value):Promise<T|null>
Type Parameters
T
T extends BaseModel
Parameters
this
typeof BaseModel & (...args) => T
constraintName
string
value
any
Returns
Promise<T | null>
getDatabaseJunctionTableName()
protectedstaticgetDatabaseJunctionTableName(modelName,fieldName):string
Get the proper database junction table name for StringSet fields
Parameters
modelName
string
fieldName
string
Returns
string
getDatabaseTableName()
protectedstaticgetDatabaseTableName(modelName):string
Get the proper database table name (should match database engine naming)
Parameters
modelName
string
Returns
string
getDocumentIdForModel()
staticgetDocumentIdForModel(modelName):string|undefined
Parameters
modelName
string
Returns
string | undefined
getGlobalDefaultDocumentId()
staticgetGlobalDefaultDocumentId():string|undefined
Returns
string | undefined
getLogLevel()
staticgetLogLevel():LogLevel
Returns
getModelDefaultDocumentMapping()
staticgetModelDefaultDocumentMapping():Record<string,string>
Returns
Record<string, string>
initialize()
staticinitialize(_yDoc,_db):Promise<void>
Parameters
_yDoc
Doc
_db
Returns
Promise<void>
initializeForDocument()
staticinitializeForDocument(yDoc,db,docId,permissionHint):Promise<void>
Parameters
yDoc
Doc
db
docId
string
permissionHint
Returns
Promise<void>
migrateToNestedYMaps()
staticmigrateToNestedYMaps():Promise<void>
Legacy migration method - no longer needed in the new multidoc architecture. Data migration is now handled during document initialization.
Returns
Promise<void>
notifyListeners()
protectedstaticnotifyListeners():void
Returns
void
onDefaultDocChanged()
staticonDefaultDocChanged(listener): () =>void
Parameters
listener
(payload) => void
Returns
():
void
Returns
void
onModelDocMappingChanged()
staticonModelDocMappingChanged(listener): () =>void
Parameters
listener
(payload) => void
Returns
():
void
Returns
void
processAggregationResults()
protectedstaticprocessAggregationResults(results,options,aliasMetadata?):AggregationResult
Process aggregation results into nested structure
Parameters
results
any[]
options
AggregationOptions
aliasMetadata?
AggregationAliasDetail[]
Returns
AggregationResult
query()
staticquery<T,P>(this,filter,options?):Promise<PaginatedResult<QueryResult<T,P>>>
Document-style query API - returns paginated results
Type Parameters
T
T extends BaseModel
P
P extends ProjectionSpec | undefined = undefined
Parameters
this
(...args) => T
filter
DocumentFilter = {}
options?
QueryOptions & object
Returns
Promise<PaginatedResult<QueryResult<T, P>>>
queryOne()
staticqueryOne<T,P>(this,filter,options?):Promise<QueryResult<T,P> |null>
Document-style query API - returns single result or null
Type Parameters
T
T extends BaseModel
P
P extends ProjectionSpec | undefined = undefined
Parameters
this
(...args) => T
filter
DocumentFilter = {}
options?
Omit<QueryOptions, "limit" | "uniqueStartKey" | "direction"> & object
Returns
Promise<QueryResult<T, P> | null>
removeModelDefaultDocumentId()
staticremoveModelDefaultDocumentId(modelName):void
Parameters
modelName
string
Returns
void
setGlobalDefaultDocumentId()
staticsetGlobalDefaultDocumentId(docId):void
Parameters
docId
string
Returns
void
setLogLevel()
staticsetLogLevel(level):void
Parameters
level
Returns
void
setModelDefaultDocumentId()
staticsetModelDefaultDocumentId(modelName,docId):void
Parameters
modelName
string
docId
string
Returns
void
setupNestedYMapObserver()
protectedstaticsetupNestedYMapObserver(recordId,recordYMap):void
Sets up deep observation on a nested YMap to sync field-level changes to the database
Parameters
recordId
string
recordYMap
YMap<any>
Returns
void
setupNestedYMapObserverForDocument()
protectedstaticsetupNestedYMapObserverForDocument(recordId,recordYMap,docId,permissionHint):void
Sets up deep observation on a nested YMap for a specific document to sync field-level changes to the database
Parameters
recordId
string
recordYMap
YMap<any>
docId
string
permissionHint
Returns
void
subscribe()
staticsubscribe(callback): () =>void
Parameters
callback
() => void
Returns
():
void
Returns
void
upsertByUnique()
staticupsertByUnique<T>(this,constraintName,uniqueLookupValue,dataToUpsert,options?):Promise<T>
Type Parameters
T
T extends BaseModel
Parameters
this
typeof BaseModel & (...args) => T
constraintName
string
uniqueLookupValue
any
dataToUpsert
Partial<Omit<T, keyof BaseModel | "toJSON">> & object
options?
objectMustExist?
boolean
objectMustNotExist?
boolean
targetDocument?
string
Returns
Promise<T>
withTransaction()
staticwithTransaction<T>(callback):Promise<T>
Execute a callback with automatic transaction handling for all modified models
Type Parameters
T
T
Parameters
callback
() => T | Promise<T>
Returns
Promise<T>