@blueleader07/typeorm
Version:
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.
541 lines (539 loc) • 18.1 kB
JavaScript
import { PlatformTools } from "../../platform/PlatformTools";
import { Broadcaster } from "../../subscriber/Broadcaster";
import { TypeORMError } from "../../error";
import { dynamoBatchHelper } from "./helpers/DynamoBatchHelper";
import asyncPool from "tiny-async-pool";
import { getDocumentClient } from "./DynamoClient";
class DeleteManyOptions {
}
class PutManyOptions {
}
const batchDelete = async (tableName, batch) => {
const RequestItems = {};
RequestItems[tableName] = batch.map((Key) => {
return {
DeleteRequest: {
Key,
},
};
});
return getDocumentClient()
.batchWrite({
RequestItems,
})
.promise();
};
const batchWrite = async (tableName, batch) => {
const RequestItems = {};
RequestItems[tableName] = batch.map((Item) => {
return {
PutRequest: {
Item,
},
};
});
return getDocumentClient()
.batchWrite({
RequestItems,
})
.promise();
};
export 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(this);
}
async clearDatabase(database) {
const AWS = 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.batch(keys);
await asyncPool(batchOptions.maxConcurrency, batches, (batch) => {
return batchDelete(tableName, batch);
});
}
}
/**
* Delete a document on DynamoDB.
*/
async deleteOne(tableName, key) {
const params = {
TableName: tableName,
Key: key,
};
await 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.batch(docs);
await asyncPool(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 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 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 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 TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Loads given table's data from the database.
*/
async getTable(collectionName) {
throw new 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 TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Checks if database with the given name exist.
*/
async hasDatabase(database) {
throw new TypeORMError("Check database queries are not supported by DynamoDB driver.");
}
/**
* Loads currently using database
*/
async getCurrentDatabase() {
throw new TypeORMError("Check database queries are not supported by DynamoDB driver.");
}
/**
* Checks if schema with the given name exist.
*/
async hasSchema(schema) {
throw new TypeORMError("Check schema queries are not supported by DynamoDB driver.");
}
/**
* Loads currently using database schema
*/
async getCurrentSchema() {
throw new 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 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 TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a database if it's not created.
*/
async createDatabase(database) {
throw new TypeORMError("Database create queries are not supported by DynamoDB driver.");
}
/**
* Drops database.
*/
async dropDatabase(database, ifExist) {
throw new TypeORMError("Database drop queries are not supported by DynamoDB driver.");
}
/**
* Creates a new table schema.
*/
async createSchema(schemaPath, ifNotExist) {
throw new TypeORMError("Schema create queries are not supported by DynamoDB driver.");
}
/**
* Drops table schema.
*/
async dropSchema(schemaPath, ifExist) {
throw new 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 TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops the table.
*/
async dropTable(tableName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new view.
*/
async createView(view) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops the view.
*/
async dropView(target) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Renames the given table.
*/
async renameTable(oldTableOrName, newTableOrName) {
throw new 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 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 TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Renames column in the given table.
*/
async renameColumn(tableOrName, oldTableColumnOrName, newTableColumnOrName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Changes a column in the table.
*/
async changeColumn(tableOrName, oldTableColumnOrName, newColumn) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Changes a column in the table.
*/
async changeColumns(tableOrName, changedColumns) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops column in the table.
*/
async dropColumn(tableOrName, columnOrName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName, columns) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new primary key.
*/
async createPrimaryKey(tableOrName, columnNames) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Updates composite primary keys.
*/
async updatePrimaryKeys(tableOrName, columns) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops a primary key.
*/
async dropPrimaryKey(tableOrName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new unique constraint.
*/
async createUniqueConstraint(tableOrName, uniqueConstraint) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new unique constraints.
*/
async createUniqueConstraints(tableOrName, uniqueConstraints) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops an unique constraint.
*/
async dropUniqueConstraint(tableOrName, uniqueOrName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops an unique constraints.
*/
async dropUniqueConstraints(tableOrName, uniqueConstraints) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new check constraint.
*/
async createCheckConstraint(tableOrName, checkConstraint) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new check constraints.
*/
async createCheckConstraints(tableOrName, checkConstraints) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops check constraint.
*/
async dropCheckConstraint(tableOrName, checkOrName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops check constraints.
*/
async dropCheckConstraints(tableOrName, checkConstraints) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new exclusion constraint.
*/
async createExclusionConstraint(tableOrName, exclusionConstraint) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new exclusion constraints.
*/
async createExclusionConstraints(tableOrName, exclusionConstraints) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops exclusion constraint.
*/
async dropExclusionConstraint(tableOrName, exclusionOrName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops exclusion constraints.
*/
async dropExclusionConstraints(tableOrName, exclusionConstraints) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new foreign key.
*/
async createForeignKey(tableOrName, foreignKey) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new foreign keys.
*/
async createForeignKeys(tableOrName, foreignKeys) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops a foreign key from the table.
*/
async dropForeignKey(tableOrName, foreignKey) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops a foreign keys from the table.
*/
async dropForeignKeys(tableOrName, foreignKeys) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new index.
*/
async createIndex(tableOrName, index) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Creates a new indices
*/
async createIndices(tableOrName, indices) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops an index from the table.
*/
async dropIndex(collectionName, indexName) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops an indices from the table.
*/
async dropIndices(tableOrName, indices) {
throw new TypeORMError("Schema update queries are not supported by DynamoDB driver.");
}
/**
* Drops collection.
*/
clearTable(tableName) {
return 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 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 TypeORMError("This operation is not supported by DynamoDB driver.");
}
/**
* Flushes all memorized sqls.
*/
clearSqlMemory() {
throw new 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 TypeORMError("This operation is not supported by DynamoDB driver.");
}
/**
* Executes up sql queries.
*/
async executeMemoryUpSql() {
throw new TypeORMError("This operation is not supported by DynamoDB driver.");
}
/**
* Executes down sql queries.
*/
async executeMemoryDownSql() {
throw new 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();
}
}
//# sourceMappingURL=DynamoQueryRunner.js.map