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qljsConnectionOptions\"\nimport { SqljsQueryRunner } from \"./SqljsQueryRunner\"\nimport { QueryRunner } from \"../../query-runner/QueryRunner\"\nimport { DataSource } from \"../../data-source/DataSource\"\nimport { DriverPackageNotInstalledError } from \"../../error/DriverPackageNotInstalledError\"\nimport { DriverOptionNotSetError } from \"../../error/DriverOptionNotSetError\"\nimport { PlatformTools } from \"../../platform/PlatformTools\"\nimport { EntityMetadata } from \"../../metadata/EntityMetadata\"\nimport { OrmUtils } from \"../../util/OrmUtils\"\nimport { ObjectLiteral } from \"../../common/ObjectLiteral\"\nimport { ReplicationMode } from \"../types/ReplicationMode\"\nimport { TypeORMError } from \"../../error\"\n\n// This is needed to satisfy the typescript compiler.\ninterface Window {\n SQL: any\n localforage: any\n}\ndeclare let window: Window\n\nexport class SqljsDriver extends AbstractSqliteDriver {\n // The driver specific options.\n options: SqljsConnectionOptions\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(connection: DataSource) {\n super(connection)\n\n // If autoSave is enabled by user, location or autoSaveCallback have to be set\n // because either autoSave saves to location or calls autoSaveCallback.\n if (\n this.options.autoSave &&\n !this.options.location &&\n !this.options.autoSaveCallback\n ) {\n throw new DriverOptionNotSetError(`location or autoSaveCallback`)\n }\n\n // load sql.js package\n this.loadDependencies()\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Performs connection to the database.\n */\n async connect(): Promise<void> {\n this.databaseConnection = await this.createDatabaseConnection()\n }\n\n /**\n * Closes connection with database.\n */\n async disconnect(): Promise<void> {\n this.queryRunner = undefined\n this.databaseConnection.close()\n }\n\n /**\n * Creates a query runner used to execute database queries.\n */\n createQueryRunner(mode: ReplicationMode): QueryRunner {\n if (!this.queryRunner) this.queryRunner = new SqljsQueryRunner(this)\n\n return this.queryRunner\n }\n\n /**\n * Loads a database from a given file (Node.js), local storage key (browser) or array.\n * This will delete the current database!\n */\n async load(\n fileNameOrLocalStorageOrData: string | Uint8Array,\n checkIfFileOrLocalStorageExists: boolean = true,\n ): Promise<any> {\n if (typeof fileNameOrLocalStorageOrData === \"string\") {\n // content has to be loaded\n if (PlatformTools.type === \"node\") {\n // Node.js\n // fileNameOrLocalStorageOrData should be a path to the file\n if (PlatformTools.fileExist(fileNameOrLocalStorageOrData)) {\n const database = PlatformTools.readFileSync(\n fileNameOrLocalStorageOrData,\n )\n return this.createDatabaseConnectionWithImport(database)\n } else if (checkIfFileOrLocalStorageExists) {\n throw new TypeORMError(\n `File ${fileNameOrLocalStorageOrData} does not exist`,\n )\n } else {\n // File doesn't exist and checkIfFileOrLocalStorageExists is set to false.\n // Therefore open a database without importing an existing file.\n // File will be written on first write operation.\n return this.createDatabaseConnectionWithImport()\n }\n } else {\n // browser\n // fileNameOrLocalStorageOrData should be a local storage / indexedDB key\n let localStorageContent = null\n if (this.options.useLocalForage) {\n if (window.localforage) {\n localStorageContent = await window.localforage.getItem(\n fileNameOrLocalStorageOrData,\n )\n } else {\n throw new TypeORMError(\n `localforage is not defined - please import localforage.js into your site`,\n )\n }\n } else {\n localStorageContent =\n PlatformTools.getGlobalVariable().localStorage.getItem(\n fileNameOrLocalStorageOrData,\n )\n }\n\n if (localStorageContent != null) {\n // localStorage value exists.\n return this.createDatabaseConnectionWithImport(\n JSON.parse(localStorageContent),\n )\n } else if (checkIfFileOrLocalStorageExists) {\n throw new TypeORMError(\n `File ${fileNameOrLocalStorageOrData} does not exist`,\n )\n } else {\n // localStorage value doesn't exist and checkIfFileOrLocalStorageExists is set to false.\n // Therefore open a database without importing anything.\n // localStorage value will be written on first write operation.\n return this.createDatabaseConnectionWithImport()\n }\n }\n } else {\n return this.createDatabaseConnectionWithImport(\n fileNameOrLocalStorageOrData,\n )\n }\n }\n\n /**\n * Saved the current database to the given file (Node.js), local storage key (browser) or\n * indexedDB key (browser with enabled useLocalForage option).\n * If no location path is given, the location path in the options (if specified) will be used.\n */\n async save(location?: string) {\n if (!location && !this.options.location) {\n throw new TypeORMError(\n `No location is set, specify a location parameter or add the location option to your configuration`,\n )\n }\n\n let path = \"\"\n if (location) {\n path = location\n } else if (this.options.location) {\n path = this.options.location\n }\n\n if (PlatformTools.type === \"node\") {\n try {\n const content = Buffer.from(this.databaseConnection.export())\n await PlatformTools.writeFile(path, content)\n } catch (e) {\n throw new TypeORMError(`Could not save database, error: ${e}`)\n }\n } else {\n const database: Uint8Array = this.databaseConnection.export()\n // convert Uint8Array to number array to improve local-storage storage\n const databaseArray = [].slice.call(database)\n if (this.options.useLocalForage) {\n if (window.localforage) {\n await window.localforage.setItem(\n path,\n JSON.stringify(databaseArray),\n )\n } else {\n throw new TypeORMError(\n `localforage is not defined - please import localforage.js into your site`,\n )\n }\n } else {\n PlatformTools.getGlobalVariable().localStorage.setItem(\n path,\n JSON.stringify(databaseArray),\n )\n }\n }\n }\n\n /**\n * This gets called by the QueryRunner when a change to the database is made.\n * If a custom autoSaveCallback is specified, it get's called with the database as Uint8Array,\n * otherwise the save method is called which saves it to file (Node.js), local storage (browser)\n * or indexedDB (browser with enabled useLocalForage option).\n * Don't auto-save when in transaction as the call to export will end the current transaction\n */\n async autoSave() {\n if (this.options.autoSave && !this.queryRunner?.isTransactionActive) {\n if (this.options.autoSaveCallback) {\n await this.options.autoSaveCallback(this.export())\n } else {\n await this.save()\n }\n }\n }\n\n /**\n * Returns the current database as Uint8Array.\n */\n export(): Uint8Array {\n return this.databaseConnection.export()\n }\n\n /**\n * Creates generated map of values generated or returned by database after INSERT query.\n */\n createGeneratedMap(metadata: EntityMetadata, insertResult: any) {\n const generatedMap = metadata.generatedColumns.reduce(\n (map, generatedColumn) => {\n // seems to be the only way to get the inserted id, see https://github.com/kripken/sql.js/issues/77\n if (\n generatedColumn.isPrimary &&\n generatedColumn.generationStrategy === \"increment\"\n ) {\n const query = \"SELECT last_insert_rowid()\"\n try {\n let result = this.databaseConnection.exec(query)\n this.connection.logger.logQuery(query)\n return OrmUtils.mergeDeep(\n map,\n generatedColumn.createValueMap(\n result[0].values[0][0],\n ),\n )\n } catch (e) {\n this.connection.logger.logQueryError(e, query, [])\n }\n }\n\n return map\n },\n {} as ObjectLiteral,\n )\n\n return Object.keys(generatedMap).length > 0 ? generatedMap : undefined\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates connection with the database.\n * If the location option is set, the database is loaded first.\n */\n protected createDatabaseConnection(): Promise<any> {\n if (this.options.location) {\n return this.load(this.options.location, false)\n }\n\n return this.createDatabaseConnectionWithImport(this.options.database)\n }\n\n /**\n * Creates connection with an optional database.\n * If database is specified it is loaded, otherwise a new empty database is created.\n */\n protected async createDatabaseConnectionWithImport(\n database?: Uint8Array,\n ): Promise<any> {\n // sql.js < 1.0 exposes an object with a `Database` method.\n const isLegacyVersion = typeof this.sqlite.Database === \"function\"\n const sqlite = isLegacyVersion\n ? this.sqlite\n : await this.sqlite(this.options.sqlJsConfig)\n if (database && database.length > 0) {\n this.databaseConnection = new sqlite.Database(database)\n } else {\n this.databaseConnection = new sqlite.Database()\n }\n\n this.databaseConnection.exec(`PRAGMA foreign_keys = ON`)\n\n return this.databaseConnection\n }\n\n /**\n * If driver dependency is not given explicitly, then try to load it via \"require\".\n */\n protected loadDependencies(): void {\n if (PlatformTools.type === \"browser\") {\n const sqlite = this.options.driver || window.SQL\n this.sqlite = sqlite\n } else {\n try {\n const sqlite =\n this.options.driver || PlatformTools.load(\"sql.js\")\n this.sqlite = sqlite\n } catch (e) {\n throw new DriverPackageNotInstalledError(\"sql.js\", \"sql.js\")\n }\n }\n }\n}\n"],"sourceRoot":"../.."}