UNPKG

@blueleader07/typeorm

Version:

Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.

546 lines (544 loc) 19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamoQueryRunner = void 0; const tslib_1 = require("tslib"); const PlatformTools_1 = require("../../platform/PlatformTools"); const Broadcaster_1 = require("../../subscriber/Broadcaster"); const error_1 = require("../../error"); const DynamoBatchHelper_1 = require("./helpers/DynamoBatchHelper"); const tiny_async_pool_1 = tslib_1.__importDefault(require("tiny-async-pool")); const DynamoClient_1 = require("./DynamoClient"); class DeleteManyOptions { } class PutManyOptions { } const batchDelete = async (tableName, batch) => { const RequestItems = {}; RequestItems[tableName] = batch.map((Key) => { return { DeleteRequest: { Key, }, }; }); return (0, DynamoClient_1.getDocumentClient)() .batchWrite({ RequestItems, }) .promise(); }; const batchWrite = async (tableName, batch) => { const RequestItems = {}; RequestItems[tableName] = batch.map((Item) => { return { PutRequest: { Item, }, }; }); return (0, DynamoClient_1.getDocumentClient)() .batchWrite({ RequestItems, }) .promise(); }; class DynamoQueryRunner { // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- constructor(connection, databaseConnection) { /** * Indicates if connection for this query runner is released. * Once its released, query runner cannot run queries anymore. * Always false for DynamoDB since DynamoDB has a single query executor instance. */ this.isReleased = false; /** * Indicates if transaction is active in this query executor. * Always false for DynamoDB since DynamoDB does not support transactions. */ this.isTransactionActive = false; /** * Stores temporarily user data. * Useful for sharing data with subscribers. */ this.data = {}; this.connection = connection; this.databaseConnection = databaseConnection; this.broadcaster = new Broadcaster_1.Broadcaster(this); } async clearDatabase(database) { const AWS = PlatformTools_1.PlatformTools.load("aws-sdk"); const db = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); const tables = await db.listTables(); for (let i = 0; i < tables.length; i++) { const table = tables[i]; const tableName = table.TableName; if (tableName.startsWith(database)) { await db.deleteTable({ TableName: table.TableName, }); } } } stream(query, parameters, onEnd, onError) { throw new Error("Method not implemented."); } getView(viewPath) { throw new Error("Method not implemented."); } getViews(viewPaths) { throw new Error("Method not implemented."); } // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- /** * Delete multiple documents on DynamoDB. */ async deleteMany(tableName, keys, options) { if (keys.length > 0) { const batchOptions = options || { maxConcurrency: 8 }; const batches = DynamoBatchHelper_1.dynamoBatchHelper.batch(keys); await (0, tiny_async_pool_1.default)(batchOptions.maxConcurrency, batches, (batch) => { return batchDelete(tableName, batch); }); } } /** * Delete a document on DynamoDB. */ async deleteOne(tableName, key) { const params = { TableName: tableName, Key: key, }; await (0, DynamoClient_1.getDocumentClient)().delete(params).promise(); } /** * Inserts an array of documents into DynamoDB. */ async putMany(tableName, docs, options) { if (docs.length > 0) { const batchOptions = options || { maxConcurrency: 8 }; const batches = DynamoBatchHelper_1.dynamoBatchHelper.batch(docs); await (0, tiny_async_pool_1.default)(batchOptions.maxConcurrency, batches, (batch) => { return batchWrite(tableName, batch); }); } } /** * Inserts a single document into DynamoDB. */ async putOne(tableName, doc) { const params = { TableName: tableName, Item: doc, }; await (0, DynamoClient_1.getDocumentClient)().put(params).promise(); return doc; } /** * For DynamoDB database we don't create connection, because its single connection already created by a driver. */ async connect() { } /** * For DynamoDB database we don't release connection, because its single connection. */ async release() { // releasing connection are not supported by DynamoDB driver, so simply don't do anything here } /** * Starts transaction. */ async startTransaction() { // transactions are not supported by DynamoDB driver, so simply don't do anything here } /** * Commits transaction. */ async commitTransaction() { // transactions are not supported by DynamoDB driver, so simply don't do anything here } /** * Rollbacks transaction. */ async rollbackTransaction() { // transactions are not supported by DynamoDB driver, so simply don't do anything here } /** * Executes a given SQL query. */ query(query, parameters) { throw new error_1.TypeORMError("Executing SQL query is not supported by DynamoDB driver."); } /** * Returns raw data stream. */ // stream (query: string, parameters?: any[], onEnd?: Function, onError?: Function): Promise<ReadStream> { // throw new TypeORMError('Stream is not supported yet. Use watch instead.') // } /** * Returns all available database names including system databases. */ async getDatabases() { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Returns all available schema names including system schemas. * If database parameter specified, returns schemas of that database. */ async getSchemas(database) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Loads given table's data from the database. */ async getTable(collectionName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Loads all tables (with given names) from the database and creates a Table from them. */ async getTables(collectionNames) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Checks if database with the given name exist. */ async hasDatabase(database) { throw new error_1.TypeORMError("Check database queries are not supported by DynamoDB driver."); } /** * Loads currently using database */ async getCurrentDatabase() { throw new error_1.TypeORMError("Check database queries are not supported by DynamoDB driver."); } /** * Checks if schema with the given name exist. */ async hasSchema(schema) { throw new error_1.TypeORMError("Check schema queries are not supported by DynamoDB driver."); } /** * Loads currently using database schema */ async getCurrentSchema() { throw new error_1.TypeORMError("Check schema queries are not supported by DynamoDB driver."); } /** * Checks if table with the given name exist in the database. */ async hasTable(collectionName) { throw new error_1.TypeORMError("Check schema queries are not supported by DynamoDB driver."); } /** * Checks if column with the given name exist in the given table. */ async hasColumn(tableOrName, columnName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a database if it's not created. */ async createDatabase(database) { throw new error_1.TypeORMError("Database create queries are not supported by DynamoDB driver."); } /** * Drops database. */ async dropDatabase(database, ifExist) { throw new error_1.TypeORMError("Database drop queries are not supported by DynamoDB driver."); } /** * Creates a new table schema. */ async createSchema(schemaPath, ifNotExist) { throw new error_1.TypeORMError("Schema create queries are not supported by DynamoDB driver."); } /** * Drops table schema. */ async dropSchema(schemaPath, ifExist) { throw new error_1.TypeORMError("Schema drop queries are not supported by DynamoDB driver."); } /** * Creates a new table from the given table and columns inside it. */ async createTable(table) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops the table. */ async dropTable(tableName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new view. */ async createView(view) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops the view. */ async dropView(target) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Renames the given table. */ async renameTable(oldTableOrName, newTableOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new column from the column in the table. */ async addColumn(tableOrName, column) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new columns from the column in the table. */ async addColumns(tableOrName, columns) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Renames column in the given table. */ async renameColumn(tableOrName, oldTableColumnOrName, newTableColumnOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Changes a column in the table. */ async changeColumn(tableOrName, oldTableColumnOrName, newColumn) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Changes a column in the table. */ async changeColumns(tableOrName, changedColumns) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops column in the table. */ async dropColumn(tableOrName, columnOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops the columns in the table. */ async dropColumns(tableOrName, columns) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new primary key. */ async createPrimaryKey(tableOrName, columnNames) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Updates composite primary keys. */ async updatePrimaryKeys(tableOrName, columns) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops a primary key. */ async dropPrimaryKey(tableOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new unique constraint. */ async createUniqueConstraint(tableOrName, uniqueConstraint) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new unique constraints. */ async createUniqueConstraints(tableOrName, uniqueConstraints) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops an unique constraint. */ async dropUniqueConstraint(tableOrName, uniqueOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops an unique constraints. */ async dropUniqueConstraints(tableOrName, uniqueConstraints) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new check constraint. */ async createCheckConstraint(tableOrName, checkConstraint) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new check constraints. */ async createCheckConstraints(tableOrName, checkConstraints) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops check constraint. */ async dropCheckConstraint(tableOrName, checkOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops check constraints. */ async dropCheckConstraints(tableOrName, checkConstraints) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new exclusion constraint. */ async createExclusionConstraint(tableOrName, exclusionConstraint) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new exclusion constraints. */ async createExclusionConstraints(tableOrName, exclusionConstraints) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops exclusion constraint. */ async dropExclusionConstraint(tableOrName, exclusionOrName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops exclusion constraints. */ async dropExclusionConstraints(tableOrName, exclusionConstraints) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new foreign key. */ async createForeignKey(tableOrName, foreignKey) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new foreign keys. */ async createForeignKeys(tableOrName, foreignKeys) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops a foreign key from the table. */ async dropForeignKey(tableOrName, foreignKey) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops a foreign keys from the table. */ async dropForeignKeys(tableOrName, foreignKeys) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new index. */ async createIndex(tableOrName, index) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Creates a new indices */ async createIndices(tableOrName, indices) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops an index from the table. */ async dropIndex(collectionName, indexName) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops an indices from the table. */ async dropIndices(tableOrName, indices) { throw new error_1.TypeORMError("Schema update queries are not supported by DynamoDB driver."); } /** * Drops collection. */ clearTable(tableName) { return (0, DynamoClient_1.getDocumentClient)() .deleteTable({ TableName: tableName, }) .promise(); } /** * Enables special query runner mode in which sql queries won't be executed, * instead they will be memorized into a special variable inside query runner. * You can get memorized sql using getMemorySql() method. */ enableSqlMemory() { throw new error_1.TypeORMError("This operation is not supported by DynamoDB driver."); } /** * Disables special query runner mode in which sql queries won't be executed * started by calling enableSqlMemory() method. * * Previously memorized sql will be flushed. */ disableSqlMemory() { throw new error_1.TypeORMError("This operation is not supported by DynamoDB driver."); } /** * Flushes all memorized sqls. */ clearSqlMemory() { throw new error_1.TypeORMError("This operation is not supported by DynamoDB driver."); } /** * Gets sql stored in the memory. Parameters in the sql are already replaced. */ getMemorySql() { throw new error_1.TypeORMError("This operation is not supported by DynamoDB driver."); } /** * Executes up sql queries. */ async executeMemoryUpSql() { throw new error_1.TypeORMError("This operation is not supported by DynamoDB driver."); } /** * Executes down sql queries. */ async executeMemoryDownSql() { throw new error_1.TypeORMError("This operation is not supported by DynamoDB driver."); } getReplicationMode() { return "master"; } /** * Called before migrations are run. */ beforeMigration() { return Promise.resolve(); } /** * Called after migrations are run. */ afterMigration() { return Promise.resolve(); } } exports.DynamoQueryRunner = DynamoQueryRunner; //# sourceMappingURL=DynamoQueryRunner.js.map