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;;;;gBACpE,IAAI,QAAQ,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;oBACjC,IAAI,CAAC,kBAAkB,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;iBAChE;qBACI;oBACD,IAAI,CAAC,kBAAkB,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;iBACxD;gBAED,mCAAmC;gBACnC,sBAAO,IAAI,OAAO,CAAM,UAAC,EAAE,EAAE,IAAI;wBAC7B,IAAI;4BACA,KAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;4BAC1D,EAAE,CAAC,KAAI,CAAC,kBAAkB,CAAC,CAAC;yBAC/B;wBACD,OAAO,CAAC,EAAE;4BACN,IAAI,CAAC,CAAC,CAAC,CAAC;yBACX;oBACL,CAAC,CAAC,EAAC;;;KACN;IAED;;OAEG;IACO,sCAAgB,GAA1B;QACI,IAAI,aAAa,CAAC,IAAI,KAAK,SAAS,EAAE;YAClC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC;SAC5B;aACI;YACD,IAAI;gBACA,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;aAE9C;YAAC,OAAO,CAAC,EAAE;gBACR,MAAM,IAAI,8BAA8B,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;aAChE;SACJ;IACL,CAAC;IACL,kBAAC;AAAD,CAxQA,AAwQC,CAxQgC,oBAAoB,GAwQpD","file":"SqljsDriver.js","sourcesContent":["import {AbstractSqliteDriver} from \"../sqlite-abstract/AbstractSqliteDriver\";\nimport {SqljsConnectionOptions} from \"./SqljsConnectionOptions\";\nimport {SqljsQueryRunner} from \"./SqljsQueryRunner\";\nimport {QueryRunner} from \"../../query-runner/QueryRunner\";\nimport {Connection} from \"../../connection/Connection\";\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\";\n\n// This is needed to satisfy the typescript compiler.\ninterface Window {\n    SQL: any;\n    localforage: any;\n}\ndeclare var 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: Connection) {\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 (this.options.autoSave && !this.options.location && !this.options.autoSaveCallback) {\n            throw new DriverOptionNotSetError(`location or autoSaveCallback`);\n        }\n\n        // load sql.js package\n        this.loadDependencies();\n    }\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        return new Promise<void>((ok, fail) => {\n            try {\n                this.queryRunner = undefined;\n                this.databaseConnection.close();\n                ok();\n            }\n            catch (e)  {\n                fail(e);\n            }\n        });\n    }\n\n    /**\n     * Creates a query runner used to execute database queries.\n     */\n    createQueryRunner(mode: \"master\" | \"slave\" = \"master\"): QueryRunner {\n        if (!this.queryRunner)\n            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(fileNameOrLocalStorageOrData: string | Uint8Array, checkIfFileOrLocalStorageExists: boolean = true): 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(fileNameOrLocalStorageOrData);\n                    return this.createDatabaseConnectionWithImport(database);\n                }\n                else if (checkIfFileOrLocalStorageExists) {\n                    throw new Error(`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            } \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(fileNameOrLocalStorageOrData);\n                    } else {\n                        throw new Error(`localforage is not defined - please import localforage.js into your site`);\n                    }\n                } else {\n                    localStorageContent = PlatformTools.getGlobalVariable().localStorage.getItem(fileNameOrLocalStorageOrData);\n                }\n                \n                if (localStorageContent != null) {\n                    // localStorage value exists.\n                    return this.createDatabaseConnectionWithImport(JSON.parse(localStorageContent));\n                }\n                else if (checkIfFileOrLocalStorageExists) {\n                    throw new Error(`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        }\n        else {\n            return this.createDatabaseConnectionWithImport(fileNameOrLocalStorageOrData);\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 Error(`No location is set, specify a location parameter or add the location option to your configuration`);\n        }\n        \n        let path = \"\";\n        if (location) {\n            path = location;\n        }\n        else if (this.options.location) {\n            path = this.options.location;\n        }\n\n        if (PlatformTools.type === \"node\") {\n            try {\n                const content = new Buffer(this.databaseConnection.export());\n                await PlatformTools.writeFile(path, content);\n            }\n            catch (e) {\n                throw new Error(`Could not save database, error: ${e}`);\n            }\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(path, JSON.stringify(databaseArray));\n                } else {\n                    throw new Error(`localforage is not defined - please import localforage.js into your site`);\n                }\n            } else {\n                PlatformTools.getGlobalVariable().localStorage.setItem(path, JSON.stringify(databaseArray));\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     */\n    async autoSave() {\n        if (this.options.autoSave) {\n            if (this.options.autoSaveCallback) {\n                await this.options.autoSaveCallback(this.export());\n            }\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((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 (generatedColumn.isPrimary && generatedColumn.generationStrategy === \"increment\") {\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(map, generatedColumn.createValueMap(result[0].values[0][0]));\n                }\n                catch (e) {\n                    this.connection.logger.logQueryError(e, query, []);\n                }\n            }\n\n            return map;\n        }, {} as ObjectLiteral);\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(database?: Uint8Array): Promise<any> {\n        if (database && database.length > 0) {\n            this.databaseConnection = new this.sqlite.Database(database);\n        }\n        else {\n            this.databaseConnection = new this.sqlite.Database();\n        }\n\n        // Enable foreign keys for database\n        return new Promise<any>((ok, fail) => {\n            try {\n                this.databaseConnection.exec(`PRAGMA foreign_keys = ON;`);\n                ok(this.databaseConnection);\n            }\n            catch (e) {\n                fail(e);\n            }\n        });\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            this.sqlite = window.SQL;\n        }\n        else {\n            try {\n                this.sqlite = PlatformTools.load(\"sql.js\");\n\n            } catch (e) {\n                throw new DriverPackageNotInstalledError(\"sql.js\", \"sql.js\");\n            }\n        }\n    }\n}"],"sourceRoot":"../.."}