Home Reference Source

src/table.js

const Util = require('./util/util');
/**
 * Represents a table of data
 */
class Table {
    /**
     * Creates a new table instance
     * @param {TableParams} options Options for the table
     */
    constructor(options = {}) {
        /**
         * The name of the table
         * @type {string}
         */
        this.name = options.name;

        /**
         * The Core that initialized this table
         * @type {Core}
         * @private
         */
        this._core = options.core;

        /**
         * The raw table data returns from better-sqlite3
         * @type {RawTableData}
         */
        this.raw = this._core.prepared.get('getTable').get(this.name);

        /**
         * An array of {@link KeyOptions} used to make this table
         * @type {TableOptions}
         * @private
         */
        this.options = [];

        /**
         * An array of the column names in the table
         * @type {string[]}
         */
        this.columns = [];
    }

    /**
     * The database
     * @type {external:Database}
     * @readonly
     */
    get db() {
        return this._core.db;
    }

    /**
     * Initializes a table and parses the SQL string used to make it
     * @returns {Table}
     * @private
     */
    _init() {
        this.options = Util._parseSql(this.raw.sql, this.name);
        return this;
    }

    valueOf() {
        return this.name;
    }
}

module.exports = Table;

/**
 * @typedef {Object} RawData
 * @property {string} type The type of object the data is
 * @property {string} name The name of the object
 * @property {string} tbl_name The name of the table that holds the object
 * @property {number} rootpage The root page of the item
 * @property {Statement} sql The raw sql used to retrieve the data
 */

/**
 * @typedef {RawData} RawTableData
 * @property {string} tbl_name The name of the retrieved table
 */