UNPKG

acebase-core

Version:

Shared AceBase core components, no need to install manually

199 lines (195 loc) 7.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AceBaseBase = exports.AceBaseBaseSettings = void 0; /** ________________________________________________________________________________ ___ ______ / _ \ | ___ \ / /_\ \ ___ ___| |_/ / __ _ ___ ___ | _ |/ __/ _ \ ___ \/ _` / __|/ _ \ | | | | (_| __/ |_/ / (_| \__ \ __/ \_| |_/\___\___\____/ \__,_|___/\___| realtime database Copyright 2018-2022 by Ewout Stortenbeker (me@appy.one) Published under MIT license See docs at https://github.com/appy-one/acebase ________________________________________________________________________________ */ const simple_event_emitter_1 = require("./simple-event-emitter"); const data_reference_1 = require("./data-reference"); const type_mappings_1 = require("./type-mappings"); const optional_observable_1 = require("./optional-observable"); const debug_1 = require("./debug"); const simple_colors_1 = require("./simple-colors"); class AceBaseBaseSettings { constructor(options) { /** * What level to use for console logging. * @default 'log' */ this.logLevel = 'log'; /** * Whether to use colors in the console logs output * @default true */ this.logColors = true; /** * @internal (for internal use) */ this.info = 'realtime database'; /** * You can turn this on if you are a sponsor. See https://github.com/appy-one/acebase/discussions/100 for more info */ this.sponsor = false; if (typeof options !== 'object') { options = {}; } if (typeof options.logger === 'object') { this.logger = options.logger; } if (typeof options.logLevel === 'string') { this.logLevel = options.logLevel; } if (typeof options.logColors === 'boolean') { this.logColors = options.logColors; } if (typeof options.info === 'string') { this.info = options.info; } if (typeof options.sponsor === 'boolean') { this.sponsor = options.sponsor; } } } exports.AceBaseBaseSettings = AceBaseBaseSettings; class AceBaseBase extends simple_event_emitter_1.SimpleEventEmitter { /** * @param dbname Name of the database to open or create */ constructor(dbname, options = {}) { var _a; super(); this._ready = false; options = new AceBaseBaseSettings(options); this.name = dbname; // Setup console logging const legacyLogger = new debug_1.DebugLogger(options.logLevel, `[${dbname}]`); this.debug = legacyLogger; // For backward compatibility this.logger = (_a = options.logger) !== null && _a !== void 0 ? _a : legacyLogger; // Enable/disable logging with colors (0, simple_colors_1.SetColorsEnabled)(options.logColors); // ASCI art: http://patorjk.com/software/taag/#p=display&f=Doom&t=AceBase const logoStyle = [simple_colors_1.ColorStyle.magenta, simple_colors_1.ColorStyle.bold]; const logo = ' ___ ______ ' + '\n' + ' / _ \\ | ___ \\ ' + '\n' + ' / /_\\ \\ ___ ___| |_/ / __ _ ___ ___ ' + '\n' + ' | _ |/ __/ _ \\ ___ \\/ _` / __|/ _ \\' + '\n' + ' | | | | (_| __/ |_/ / (_| \\__ \\ __/' + '\n' + ' \\_| |_/\\___\\___\\____/ \\__,_|___/\\___|'; const info = (options.info ? ''.padStart(40 - options.info.length, ' ') + options.info + '\n' : ''); if (!options.sponsor) { // if you are a sponsor, you can switch off the "AceBase banner ad" legacyLogger.write(logo.colorize(logoStyle)); info && legacyLogger.write(info.colorize(simple_colors_1.ColorStyle.magenta)); } // Setup type mapping functionality this.types = new type_mappings_1.TypeMappings(this); this.once('ready', () => { // console.log(`database "${dbname}" (${this.constructor.name}) is ready to use`); this._ready = true; }); } /** * Waits for the database to be ready before running your callback. * @param callback (optional) callback function that is called when the database is ready to be used. You can also use the returned promise. * @returns returns a promise that resolves when ready */ async ready(callback) { if (!this._ready) { // Wait for ready event await new Promise(resolve => this.on('ready', resolve)); } callback === null || callback === void 0 ? void 0 : callback(); } get isReady() { return this._ready; } /** * Allow specific observable implementation to be used * @param ObservableImpl Implementation to use */ setObservable(ObservableImpl) { (0, optional_observable_1.setObservable)(ObservableImpl); } /** * Creates a reference to a node * @param path * @returns reference to the requested node */ ref(path) { return new data_reference_1.DataReference(this, path); } /** * Get a reference to the root database node * @returns reference to root node */ get root() { return this.ref(''); } /** * Creates a query on the requested node * @param path * @returns query for the requested node */ query(path) { const ref = new data_reference_1.DataReference(this, path); return new data_reference_1.DataReferenceQuery(ref); } get indexes() { return { /** * Gets all indexes */ get: () => { return this.api.getIndexes(); }, /** * Creates an index on "key" for all child nodes at "path". If the index already exists, nothing happens. * Example: creating an index on all "name" keys of child objects of path "system/users", * will index "system/users/user1/name", "system/users/user2/name" etc. * You can also use wildcard paths to enable indexing and quering of fragmented data. * Example: path "users/*\/posts", key "title": will index all "title" keys in all posts of all users. * @param path path to the container node * @param key name of the key to index every container child node * @param options any additional options */ create: (path, key, options) => { return this.api.createIndex(path, key, options); }, /** * Deletes an existing index from the database */ delete: async (filePath) => { return this.api.deleteIndex(filePath); }, }; } get schema() { return { get: (path) => { return this.api.getSchema(path); }, set: (path, schema, warnOnly = false) => { return this.api.setSchema(path, schema, warnOnly); }, all: () => { return this.api.getSchemas(); }, check: (path, value, isUpdate) => { return this.api.validateSchema(path, value, isUpdate); }, }; } } exports.AceBaseBase = AceBaseBase; //# sourceMappingURL=acebase-base.js.map