UNPKG

godprotocol

Version:

A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.

103 lines (70 loc) 2.71 kB
import store from "godprotocol/callables/functions/store.js"; import Storage from "godprotocol/Datatypes/functions/storage.js"; class Fold extends Storage{ constructor(config, account) { super(account) this.config = config this.type = 'folder' } child = async(name, opts)=>{ return await this.folder(name, {...opts, account: this.account.physical_address}) } _folder_ = async()=>{ let folder = await this.account.manager.ds.folder(this.config.value, {account: this.account.physical_address}) return folder; } get_config = async()=>{ let folder = await this._folder_() return folder.config.config } total_entries = async()=>{ let folder = await this._folder_() return folder.config.config.files } read = async(query, args, {call_config, chain})=>{ let ds = this.account.manager.ds; let folder = await ds.folder(this.config.value, {account: this.account.physical_address}) let options = args[1] && await args[1].literal() let res = await folder.read(query && await query.literal(), {...options}) for (let i=0; i< res.length; i++){ let item = res[i] let fold = await ds.folder(item.address, {account: this.account.physical_address}) let value = await fold.readone({_id: item.data_id || item._id}) let obj = await this.account.vm.retrieve(value) res[i] = obj; } return await this.account.vm.instantiate({ type: 'array', location: call_config.location, value: res, }, {chain}) } readone = async(query, args)=>{ } persist_repo = async(buffer)=>{ } write = async(data, args, {thread})=>{ let _id = await data.get('_id', {raw: true}) let options = (args[1] && await args[1].literal()) || {} if (_id && _id.type === 'address'){ _id = await (await this.account.vm.cloth(_id)).literal() if (_id) _id = await this.account.vm.hash(_id.toString()) } else _id = null let folder = await this.account.manager.ds.folder(this.config.value, {account: this.account.physical_address}) if (_id && options.replace === false){ if (await folder.readone({_id})) return _id } data = await store({object: data}, {chain: this.account.chain, vm: this.account.vm, thread }) let res = await folder.write({address: data.location, data_id: data._id, _id: _id || data._id, }, {replace: true, ...options}) return res._id; } update = async(query, args)=>{ } remove = async(query, args)=>{ } literal = async()=>{ let folder = await this.account.manager.gds.folder(this.config.value) return `[Folder ${folder.name}]` } } export default Fold