generalised-datastore
Version:
Generalised Datastore (GDS) — a decentralized data engine and RPC layer built for Web 4.0 applications, enabling distributed joins, caching, and remote program execution across nodes.
95 lines (68 loc) • 2.08 kB
JavaScript
import Config from "./Config.js";
import Queries from "./Queries.js";
import Response from "./Response.js";
class Folder extends Queries {
constructor(address, ds, options) {
super();
this.address = address;
this.ds = ds;
this.options = options || {};
this.account = this.options.account;
this.manager = ds.manager;
}
get_config = async () => {
return this.config.config;
};
write = async (data, options = {}) => {
!options.allow_index_write && this.check_readonly();
let res = await this.write_data(data, options);
return res;
};
update = async (query, update_query, options) => {
this.check_readonly();
let res = await this.update_data(query, update_query, options);
return res;
};
read = async (query, options) => {
let res = await this.read_data(query, { ...options });
return res;
};
readone = async (query, options) => {
let res = await this.read_data(query, { ...options, one: true });
return res;
};
check_readonly = () => {
if (this.readinterface_only) throw new Error("index folders are readonly");
};
remove = async (query, options) => {
this.check_readonly();
let res = await this.remove_data(query, { ...options });
return res;
};
drop = async () => {
this.check_readonly();
this.config.config.dropped = true;
await this.config.persist(!this.is_new);
};
sync = async () => {
this.path = this.address;
this.indexes = this.options.indexes || [];
this.joins = this.options.joins || {}
this.config = new Config(this);
await this.config.sync();
return this;
};
folder = async (name, options) => {
let sub_folder = await this.ds.folder(`${this.address}/${name}`, options);
return sub_folder;
};
index = async (value) => {
let index_folder = await this.ds.folder(
`${this.address}/index-${await this.manager.hash(value)}`,
{ account: this.account }
);
index_folder.readinterface_only = true;
return index_folder;
};
}
export default Folder;