{"version":3,"file":"hive.UwlFcf6B.mjs","sources":["../../../@hive/sdk/dist/database.mjs"],"sourcesContent":["import { ResourceNotInitializedError, Selector } from \"./_chunks/errors-CNH_pZDT.mjs\";\n\n//#region src/database.ts\nvar Database = class extends Selector {\n\t/**\n\t* `Storage` instance to use for persisting the Database.\n\t*\n\t* If not set, the Database must be initialized by passing it to either\n\t* `hive.create()` or `hive.get()`.\n\t*/\n\tstorage = null;\n\tconstructor(config) {\n\t\tsuper({\n\t\t\ttype: config?.type ?? \"database\",\n\t\t\tid: config?.id ?? crypto.randomUUID(),\n\t\t\tparent: config?.parent ?? null\n\t\t});\n\t\tthis.storage = config?.storage ?? null;\n\t}\n\t/**\n\t* Get the metadata stored for the Database.\n\t*\n\t* @param opts Options for getting the metadata stored for the Database.\n\t*\n\t* @returns Promise resolving with the metadata stored for the Database.\n\t*\n\t* @throws `ResourceNotFoundError` if the Database was not found.\n\t* @throws `ResourceNotInitializedError` if the Database is not initialized.\n\t* To initialize the Database, pass it to either `hive.create()` or `hive.get()`.\n\t*/\n\tgetMetadata = (opts) => {\n\t\tif (!this.storage) throw new ResourceNotInitializedError(this);\n\t\treturn this.storage.getMetadata(this, { trace: opts?.trace });\n\t};\n\t/**\n\t* Get the contents of the Database.\n\t*\n\t* @param opts Options for getting the contents of the Database.\n\t*\n\t* @returns Promise resolving with the contents of the Database.\n\t*\n\t* @throws `ResourceNotFoundError` if the Database was not found.\n\t* @throws `ResourceNotInitializedError` if the Database is not initialized.\n\t* To initialize the Database, pass it to either `hive.create()` or `hive.get()`.\n\t*/\n\tgetContents = (opts) => {\n\t\tif (!this.storage) throw new ResourceNotInitializedError(this);\n\t\treturn this.storage.getContents(this, { trace: opts?.trace });\n\t};\n\t/**\n\t* Update the contents of the Database.\n\t*\n\t* @param contents Contents to store.\n\t* @param opts Options for storing the contents of the Database.\n\t*\n\t* @returns Promise resolving when the contents of the Database have been\n\t* stored.\n\t*\n\t* @throws `ResourceNotFoundError` if the Database was not found.\n\t* @throws `ResourceNotInitializedError` if the Database is not initialized.\n\t* To initialize the Database, pass it to either `hive.create()` or `hive.get()`.\n\t*/\n\tsetContents = async (contents, opts) => {\n\t\tif (!this.storage) throw new ResourceNotInitializedError(this);\n\t\treturn this.storage.setContents(this, contents, { trace: opts?.trace }).then(() => this);\n\t};\n\t/**\n\t* Apply a Transaction to the Database.\n\t*\n\t* @param input Transaction to apply to the Database.\n\t* @param opts Options for applying the Transaction to the Database.\n\t*\n\t* @returns Promise resolving the results of the Transaction.\n\t*\n\t* @throws `ResourceNotFoundError` if the Resource was not found.\n\t* @throws `StatementPreparationError` if a Statement of the Transaction\n\t* could not be prepared.\n\t* @throws `StatementExecutionError` if a Statement of the Transaction\n\t* could not be executed.\n\t*\n\t* @emits `ResourceTransactionEvent` when the Transaction has been applied.\n\t*/\n\tquery = (input, opts) => {\n\t\tif (!this.storage) throw new ResourceNotInitializedError(this);\n\t\tconst statements = (Array.isArray(input) ? input : input.statements).map((statement) => ({\n\t\t\tsql: typeof statement === \"string\" ? statement : statement.sql,\n\t\t\tparams: typeof statement === \"string\" ? null : statement.params ?? null,\n\t\t\tmethod: typeof statement === \"string\" ? \"all\" : statement.method ?? \"all\"\n\t\t}));\n\t\tconst transaction = {\n\t\t\tstatements,\n\t\t\tmode: Array.isArray(input) ? \"DEFERRED\" : input.mode ?? \"DEFERRED\"\n\t\t};\n\t\treturn this.storage.query(this, transaction, { trace: opts?.trace });\n\t};\n\t/**\n\t* Free the Database.\n\t*\n\t* @param opts Options for freeing the Database.\n\t*\n\t* @returns Promise resolving when the Database has been freed.\n\t*\n\t* @throws `ResourceNotFoundError` if the Database was not found.\n\t* @throws `ResourceNotInitializedError` if the Database is not initialized.\n\t* To initialize the Database, pass it to either `hive.create()` or `hive.get()`.\n\t*/\n\tfree = async (opts) => {\n\t\tif (!this.storage) throw new ResourceNotInitializedError(this);\n\t\tawait this.storage.free(this, { trace: opts?.trace });\n\t\treturn this;\n\t};\n};\n\n//#endregion\nexport { Database };"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