Files
Foundry-VTT-Docker/resources/app/dist/database/fields-extensions.mjs

1 line
3.8 KiB
JavaScript
Raw Permalink Normal View History

2025-01-04 00:34:03 +01:00
import*as fields from"../../common/data/fields.mjs";import LevelDatabase from"./backend/level-database.mjs";import*as sanitization from"./sanitization.mjs";import EmbeddedDeltaMixin from"./backend/embedded-delta.mjs";import{randomID}from"../../common/utils/helpers.mjs";fields.HTMLField.prototype.sanitize=sanitization.sanitizeHTMLField,fields.FilePathField.prototype.sanitize=sanitization.sanitizeFilePathField,fields.EmbeddedCollectionField.prototype.sanitize=sanitization.sanitizeEmbeddedCollectionField,fields.EmbeddedDocumentField.prototype.sanitize=sanitization.sanitizeEmbeddedDocumentField,Object.defineProperty(fields.EmbeddedCollectionDeltaField.prototype,"model",{get(){return EmbeddedDeltaMixin(this.element.implementation)},configurable:!0}),fields.EmbeddedCollectionField.prototype.expandEmbedded=async function(e,t,i,d){const{logger:a}=global,o=LevelDatabase.formatKey(i,this.name),n=d.sublevels[o],l=e[this.name];if(!l)return[];if(!Array.isArray(l))return a.warn(`Did not retrieve ${this.name} collection from ${this.model.documentName} ${e._id} as it was not an Array.`),[];const r=l.map((e=>LevelDatabase.formatKey(t,e))),s=await n.getMany(r),m=await Promise.all(s.reduce(((e,i)=>(i?._id&&e.push(this.model.expandEmbedded(i,{ldb:d,idPrefix:t,sublevelName:o})),e)),[])),b=s.length-m.length;return b&&a.warn(`${b} embedded ${this.name} records in ${this.model.documentName} ${e._id} were undefined and not retrieved from the ${o} sublevel.`),m},fields.EmbeddedCollectionField.prototype._dbWrite=function(e,t,i,d,{writeEmbedded:a=!0,generateIds:o=!1}={}){const{logger:n}=global,l=LevelDatabase.formatKey(d,this.name),r=e[this.name];return r?Array.isArray(r)?r.reduce(((d,r)=>{if(!r._id){if(!o)return n.warn(`Did not write record in ${this.name} collection for ${this.model.documentName} ${e._id} as it had no ID.`),d;r._id=randomID()}return a&&this.model.batchWrite(r,t,{writeEmbedded:a,idPrefix:i,sublevelName:l}),d.push(r._id),d}),[]):(n.warn(`Did not write ${this.name} collection for ${this.model.documentName} ${e._id} as it was not an Array.`),[]):[]},fields.EmbeddedCollectionField.prototype._dbDelete=function(e,t,i,d){const a=LevelDatabase.formatKey(d,this.name),o=e[this.name];for(const e of o??[])this.model.batchDelete(e,t,{idPrefix:i,sublevelName:a})},fields.EmbeddedCollectionField.prototype._dbDeleteBranch=function(e,t,i){const d=e.getEmbeddedCollection(this.name);for(const e of d)e.batchDelete(t,i)},fields.EmbeddedCollectionDeltaField.prototype._dbDeleteBranch=function(e,t,i){const d=e.getEmbeddedCollection(this.name);for(const a of d._source)d.isTombstone(a._id)?d.documentClass.batchDelete(a,t,{idPrefix:e.dbKey,sublevelName:LevelDatabase.formatKey(e.sublevelName,this.name)}):d.get(a._id).batchDelete(t,i)},fields.EmbeddedDocumentField.prototype.expandEmbedded=async function(e,t,i,d){const{logger:a}=global,o=LevelDatabase.formatKey(i,this.name),n=d.sublevels[o];let l=e[this.name];if(!l)return null;l=LevelDatabase.formatKey(t,l);const r=await n.get(l);return r?(await this.model.expandEmbedded(r,{ldb:d,idPrefix:t,sublevelName:o}),r):(a.warn(`Singleton embedded ${this.name} ${l} in ${this.model.documentName} ${e._id} was undefined and not retrieved from the ${o} sublevel.`),null)},fields.EmbeddedDocumentField.prototype._dbWrite=function(e,t,i,d,{writeEmbedded:a=!0,generateIds:o=!1}={}){const{logger:n}=global,l=LevelDatabase.formatKey(d,this.name),r=e[this.name];if(!r)return null;if(!r._id){if(!o)return n.warn(`Did not write record in ${this.name} field for ${this.model.documentName} ${e._id} as it had no ID.`),null;r._id=randomID()}return a&&this.model.batchWrite(r,t,{writeEmbedded:a,idPrefix:i,sublevelName:l}),r._id},fields.EmbeddedDocumentField.prototype._dbDelete=function(e,t,i,d){const a=LevelDatabase.formatKey(d,this.name),o=e[this.name];o&&this.model.batchDelete(o,t,{idPrefix:i,sublevelName:a})};