UNPKG

@blueleader07/typeorm

Version:

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

87 lines (85 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamoSchemaBuilder = void 0; const SqlInMemory_1 = require("../driver/SqlInMemory"); const PlatformTools_1 = require("../platform/PlatformTools"); const DynamoGlobalSecondaryIndexHelper_1 = require("../driver/dynamo/helpers/DynamoGlobalSecondaryIndexHelper"); /** * Creates complete tables schemas in the database based on the entity metadatas. * * Steps how schema is being built: * 1. load list of all tables with complete column and keys information from the db * 2. drop all (old) foreign keys that exist in the table, but does not exist in the metadata * 3. create new tables that does not exist in the db, but exist in the metadata * 4. drop all columns exist (left old) in the db table, but does not exist in the metadata * 5. add columns from metadata which does not exist in the table * 6. update all exist columns which metadata has changed * 7. update primary keys - update old and create new primary key from changed columns * 8. create foreign keys which does not exist in the table yet * 9. create indices which are missing in db yet, and drops indices which exist in the db, but does not exist in the metadata anymore */ class DynamoSchemaBuilder { // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- constructor(connection) { this.connection = connection; } // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- /** * Creates complete schemas for the given entity metadatas. */ async build() { const AWS = PlatformTools_1.PlatformTools.load("aws-sdk"); const db = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); const driver = this.connection.driver; const metadatas = this.connection.entityMetadatas; for (let i = 0; i < metadatas.length; i += 1) { const metadata = metadatas[i]; const keySchema = []; for (let i = 0; i < metadata.primaryColumns.length; i += 1) { const primaryColumn = metadata.primaryColumns[i]; keySchema.push({ AttributeName: primaryColumn.propertyName, KeyType: "HASH", }); } const globalSecondaryIndexes = (0, DynamoGlobalSecondaryIndexHelper_1.buildGlobalSecondaryIndexes)(metadata) || []; const attributeDefinitions = (0, DynamoGlobalSecondaryIndexHelper_1.buildAttributeDefinitions)(metadata, driver); const schema = { AttributeDefinitions: attributeDefinitions, BillingMode: "PAY_PER_REQUEST", TableName: driver.buildTableName(metadata.tableName, metadata.schema, metadata.database), KeySchema: keySchema, GlobalSecondaryIndexes: globalSecondaryIndexes.length > 0 ? globalSecondaryIndexes : undefined, }; try { await db.createTable(schema).promise(); } catch (error) { const _error = error; if (_error && _error.code && _error.code === "ResourceInUseException") { PlatformTools_1.PlatformTools.logInfo("table already exists: ", metadata.tableName); await (0, DynamoGlobalSecondaryIndexHelper_1.updateGlobalSecondaryIndexes)(db, schema.TableName, attributeDefinitions, globalSecondaryIndexes); } else { PlatformTools_1.PlatformTools.logError("error creating table: ", error); } } } } /** * Returns query to be executed by schema builder. */ log() { return Promise.resolve(new SqlInMemory_1.SqlInMemory()); } } exports.DynamoSchemaBuilder = DynamoSchemaBuilder; //# sourceMappingURL=DynamoSchemaBuilder.js.map