<commit_before>package pman.edb;

import tannus.io.*;
import tannus.ds.*;
import tannus.ds.promises.*;
import tannus.async.*;
import tannus.sys.*;
import tannus.sys.FileSystem as Fs;

import pman.Paths;
import pman.ds.OnceSignal as ReadySignal;
import pman.edb.Query;
import pman.edb.Modification;
import pman.edb.Query.*;

import nedb.DataStore;

import Slambda.fn;
import tannus.math.TMath.*;
import haxe.extern.EitherType;
import pman.Globals.*;

using StringTools;
using tannus.ds.StringUtils;
using Lambda;
using tannus.ds.ArrayTools;
using Slambda;
using tannus.async.Asyncs;
using tannus.async.VoidAsyncs;

class TableWrapper {
    /* Constructor Function */
    public function new(root:PManDatabase, store:DataStore):Void {
        this.root = root;
        this.store = store;
    }

/* === Instance Methods === */

    /**
      * initialize [this]
      */
    public function init(done : VoidCb):Void {
        store.loadDatabase( done );
    }

    /**
      * run a Query
      */
    public function query<T>(query : QueryDecl):ArrayPromise<T> {
        return find(query.toDynamic()).array();
    }
    public function _query<T>(query:QueryDecl, done:Cb<Array<T>>) {
        this.query(query).toAsync( done );
    }

    /**
      * run a Query
      */
    public function queryOne<T>(query : QueryDecl):Promise<T> {
        return findOne(query.toDynamic());
    }
    public function _queryOne<T>(query:QueryDecl, done:Cb<T>):Void {
        queryOne(query).toAsync( done );
    }

    /**
      * get the single item that matches [query]
      */
    public function findOne<T>(query:Dynamic):Promise<Maybe<T>> {
        return store.findOne.bind(query, _).toPromise();
    }
    public function _findOne<T>(q:Dynamic, f:Cb<Maybe<T>>):Void {
        findOne(q).toAsync( f );
    }

    /**
      * get all items taht match [query]
      */
    public function find<T>(query:Dynamic):Promise<Array<T>> {
        return untyped store.find.bind(query, _).toPromise();
    }
    public function _find<T>(q:Dynamic, f:Cb<T>):Void {
        find(q).toAsync(untyped f);
    }

    /**
      * get all items in [this] table
      */
    public function all<T>():ArrayPromise<T> {
        return find({}).array();
    }
    public function _all<T>(done : Cb<Array<T>>):Void {
        all().toAsync( done );
    }

    /**
      * get all by a single field
      */
    public function getAllBy<T>(index:String, value:Dynamic):ArrayPromise<T> {
        return find(_bo(function(o : Object) {
            o[index] = value;
        })).array();
    }
    public function _getAllBy<T>(index:String, value:Dynamic, done:Cb<Array<T>>):Void {
        getAllBy(index, value).toAsync( done );
    }

    /**
      * get a single row by a single field
      */
    public function getBy<T>(index:String, value:Dynamic):Promise<T> {
        return findOne(_bo(function(o : Object) {
            o[index] = value;
        }));
    }
    public function _getBy<T>(index:String, value:Dynamic, done:Cb<T>):Void {
        return getBy(index, value).toAsync( done );
    }

    /**
      * get an item by id
      */
    public function getById<T>(id : Dynamic):Promise<T> {
        return getBy('_id', id);
    }
    public function _getById<T>(id:Dynamic, done:Cb<T>):Void {
        getById(id).toAsync( done );
    }

    /**
      * update an item
      */
    public function update<T>(query:Dynamic, update:Dynamic, ?options:{?multi:Bool,?insert:Bool}):Promise<T> {
        return Promise.create({
            store.update(query, update, buildUpdateOptions(options), function(err:Null<Dynamic>, numAffected:Int, affectedDocuments:T, upsert:Bool) {
                if (err != null) {
                    throw err;
                }
                else {
                    return affectedDocuments;
                }
            });
        });
    }
    public function _update<T>(query:Dynamic, update:Dynamic, ?options:{?multi:Bool, ?insert:Bool}, done:Cb<T>):Void {
        update(query, update, options).toAsync( done );
    }

    /**
      * modify an item
      */
    public function mutate<T>(query:QueryDecl, mod:Mod, ?options:{?multi:Bool, ?insert:Bool}):Promise<T> {
        return Promise.create({
            var p = update(query.toObject(), mod.toObject(), options);
            p.then(function(o : Object) {
                @forward getById(o['_id']);
            });
            p.unless(function(error) {
                throw error;
            });
        });
    }
    public function _mutate<T>(query:QueryDecl, mod:Mod, ?options:{?multi:Bool,?insert:Bool}, done:Cb<T>):Void {
        mutate(query, mod, options).toAsync( done );
    }

    /**
      * put an item
      */
    public function put<T>(query:QueryDecl, update:Dynamic):Promise<T> {
        return update(query.toObject(), update, {
            multi: false,
            insert: true
        });
    }
    public function _put<T>(query:QueryDecl, update:Dynamic, done:Cb<T>):Void {
        put(query, update).toAsync( done );
    }

    /**
      * insert an item
      */
    public function insert<T>(doc : Dynamic):Promise<T> {
        return store.insert.bind(doc, _).toPromise();
    }
    public function _insert<T>(doc:Dynamic, done:Cb<T>) insert(doc).toAsync( done );

    /**
      * create a new Index
      */
    public function createIndex(fieldName:String, unique:Bool=false, sparse:Bool=false, ?done:VoidCb):Void {
        store.ensureIndex({
            fieldName: fieldName,
            unique: unique,
            sparse: sparse
        }, done);
    }<commit_msg>corrected some issues of name ambiguity in pman.edb.TableWrapper<commit_after>package pman.edb;

import tannus.io.*;
import tannus.ds.*;
import tannus.ds.promises.*;
import tannus.async.*;
import tannus.sys.*;
import tannus.sys.FileSystem as Fs;

import pman.Paths;
import pman.ds.OnceSignal as ReadySignal;
import pman.edb.Query;
import pman.edb.Modification;
import pman.edb.Query.*;

import nedb.DataStore;

import Slambda.fn;
import tannus.math.TMath.*;
import haxe.extern.EitherType;
import pman.Globals.*;

using StringTools;
using tannus.ds.StringUtils;
using Lambda;
using tannus.ds.ArrayTools;
using Slambda;
using tannus.async.Asyncs;
using tannus.async.VoidAsyncs;
using tannus.html.JSTools;

class TableWrapper {
    /* Constructor Function */
    public function new(root:PManDatabase, store:DataStore):Void {
        this.root = root;
        this.store = store;
    }

/* === Instance Methods === */

    /**
      * initialize [this]
      */
    public function init(done : VoidCb):Void {
        store.loadDatabase( done );
    }

    /**
      * run a Query
      */
    public function query<T>(query : QueryDecl):ArrayPromise<T> {
        return find(query.toDynamic()).array();
    }
    public function _query<T>(query:QueryDecl, done:Cb<Array<T>>) {
        this.query(query).toAsync( done );
    }

    /**
      * run a Query
      */
    public function queryOne<T>(query : QueryDecl):Promise<T> {
        return findOne(query.toDynamic());
    }
    public function _queryOne<T>(query:QueryDecl, done:Cb<T>):Void {
        queryOne(query).toAsync( done );
    }

    /**
      * get the single item that matches [query]
      */
    public function findOne<T>(query:Dynamic):Promise<Maybe<T>> {
        return store.findOne.bind(query, _).toPromise();
    }
    public function _findOne<T>(q:Dynamic, f:Cb<Maybe<T>>):Void {
        findOne(q).toAsync( f );
    }

    /**
      * get all items taht match [query]
      */
    public function find<T>(query:Dynamic):Promise<Array<T>> {
        return untyped store.find.bind(query, _).toPromise();
    }
    public function _find<T>(q:Dynamic, f:Cb<T>):Void {
        find(q).toAsync(untyped f);
    }

    /**
      * get all items in [this] table
      */
    public function all<T>():ArrayPromise<T> {
        return find({}).array();
    }
    public function _all<T>(done : Cb<Array<T>>):Void {
        all().toAsync( done );
    }

    /**
      * get all by a single field
      */
    public function getAllBy<T>(index:String, value:Dynamic):ArrayPromise<T> {
        return find(_bo(function(o : Object) {
            o[index] = value;
        })).array();
    }
    public function _getAllBy<T>(index:String, value:Dynamic, done:Cb<Array<T>>):Void {
        getAllBy(index, value).toAsync( done );
    }

    /**
      * get a single row by a single field
      */
    public function getBy<T>(index:String, value:Dynamic):Promise<T> {
        return findOne(_bo(function(o : Object) {
            o[index] = value;
        }));
    }
    public function _getBy<T>(index:String, value:Dynamic, done:Cb<T>):Void {
        return getBy(index, value).toAsync( done );
    }

    /**
      * get an item by id
      */
    public function getById<T>(id : Dynamic):Promise<T> {
        return getBy('_id', id);
    }
    public function _getById<T>(id:Dynamic, done:Cb<T>):Void {
        getById(id).toAsync( done );
    }

    /**
      * update an item
      */
    public function update<T>(query:Dynamic, update:Dynamic, ?options:{?multi:Bool,?insert:Bool}):Promise<T> {
        return Promise.create({
            store.update(query, update, buildUpdateOptions(options), function(err:Null<Dynamic>, numAffected:Int, affectedDocuments:T, upsert:Bool) {
                if (err != null) {
                    throw err;
                }
                else {
                    return affectedDocuments;
                }
            });
        });
    }
    public function _update<T>(query:Dynamic, update:Dynamic, ?options:{?multi:Bool, ?insert:Bool}, done:Cb<T>):Void {
        this.update(query, update, options).toAsync( done );
    }

    /**
      * modify an item
      */
    public function mutate<T>(query:QueryDecl, mod:Mod, ?options:{?multi:Bool, ?insert:Bool}):Promise<T> {
        return Promise.create({
            var p = update(query.toObject(), mod.toObject(), options);
            p.then(function(o : Object) {
                @forward getById(o['_id']);
            });
            p.unless(function(error) {
                throw error;
            });
        });
    }
    public function _mutate<T>(query:QueryDecl, mod:Mod, ?options:{?multi:Bool,?insert:Bool}, done:Cb<T>):Void {
        mutate(query, mod, options).toAsync( done );
    }

    /**
      * put an item
      */
    public function put<T>(query:QueryDecl, update:Dynamic):Promise<T> {
        if (update.nag('_id') == null) {
            update.nad('_id');
        }

        return this.update(query.toObject(), update, {
            multi: false,
            insert: true
        });
    }
    public function _put<T>(query:QueryDecl, update:Dynamic, done:Cb<T>):Void {
        put(query, update).toAsync( done );
    }

    /**
      * insert an item
      */
    public function insert<T>(doc : Dynamic):Promise<T> {
        if (doc.nag('_id') == null) {
            doc.nad('_id');
        }
        return store.insert.bind(doc, _).toPromise();
    }
    public function _insert<T>(doc:Dynamic, done:Cb<T>) insert(doc).toAsync( done );

    /**
      * create a new Index
      */
    public function createIndex(fieldName:String, unique:Bool=false, sparse:Bool=false, ?done:VoidCb):Void {
        store.ensureIndex({
            fieldName: fieldName,
            unique: unique,
            sparse: sparse
        }, done);
    }
{'fraction_non_alphanumeric': 0.1484505311210605, 'fraction_numerical': 0.0, 'mean_word_length': 2.3834273834273834, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6411120', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3611, 'n_words': 721}