typeorm 0.3.7-dev.97e1dc1 → 0.3.7-dev.ec05f1d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,5 +48,5 @@ export declare class DbQueryResultCache implements QueryResultCache {
48
48
  /**
49
49
  * Gets a query runner to work with.
50
50
  */
51
- protected getQueryRunner(queryRunner: QueryRunner | undefined): QueryRunner;
51
+ protected getQueryRunner(queryRunner?: QueryRunner): QueryRunner;
52
52
  }
@@ -226,8 +226,9 @@ export class DbQueryResultCache {
226
226
  * Removes all cached results by given identifiers from cache.
227
227
  */
228
228
  async remove(identifiers, queryRunner) {
229
+ let _queryRunner = queryRunner || this.getQueryRunner();
229
230
  await Promise.all(identifiers.map((identifier) => {
230
- const qb = this.getQueryRunner(queryRunner).manager.createQueryBuilder();
231
+ const qb = _queryRunner.manager.createQueryBuilder();
231
232
  return qb
232
233
  .delete()
233
234
  .from(this.queryResultCacheTable)
@@ -236,6 +237,9 @@ export class DbQueryResultCache {
236
237
  })
237
238
  .execute();
238
239
  }));
240
+ if (!queryRunner) {
241
+ await _queryRunner.release();
242
+ }
239
243
  }
240
244
  // -------------------------------------------------------------------------
241
245
  // Protected Methods
@@ -1 +1 @@
1
- {"version":3,"sources":["../browser/src/cache/DbQueryResultCache.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAA;AAEnE,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAA;AAGrD,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAA;AAEnC;;GAEG;AACH,MAAM,OAAO,kBAAkB;IAW3B,4EAA4E;IAC5E,cAAc;IACd,4EAA4E;IAE5E,YAAsB,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;QACxC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,OAAc,CAAA;QACxD,MAAM,QAA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IACpB,4EAA4E;IAE5E;;OAEG;IACO,cAAc,CACpB,WAAoC;QAEpC,IAAI,WAAW;YAAE,OAAO,WAAW,CAAA;QAEnC,OAAO,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAA;IAC9C,CAAC;CACJ","file":"DbQueryResultCache.js","sourcesContent":["import { ObjectLiteral } from \"../common/ObjectLiteral\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { MssqlParameter } from \"../driver/sqlserver/MssqlParameter\"\nimport { QueryRunner } from \"../query-runner/QueryRunner\"\nimport { Table } from \"../schema-builder/table/Table\"\nimport { QueryResultCache } from \"./QueryResultCache\"\nimport { QueryResultCacheOptions } from \"./QueryResultCacheOptions\"\nimport { v4 as uuidv4 } from \"uuid\"\n\n/**\n * Caches query result into current database, into separate table called \"query-result-cache\".\n */\nexport class DbQueryResultCache implements QueryResultCache {\n // -------------------------------------------------------------------------\n // Private properties\n // -------------------------------------------------------------------------\n\n private queryResultCacheTable: string\n\n private queryResultCacheDatabase?: string\n\n private queryResultCacheSchema?: string\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(protected connection: DataSource) {\n const { schema } = this.connection.driver.options as any\n const database = this.connection.driver.database\n const cacheOptions =\n typeof this.connection.options.cache === \"object\"\n ? this.connection.options.cache\n : {}\n const cacheTableName = cacheOptions.tableName || \"query-result-cache\"\n\n this.queryResultCacheDatabase = database\n this.queryResultCacheSchema = schema\n this.queryResultCacheTable = this.connection.driver.buildTableName(\n cacheTableName,\n schema,\n database,\n )\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates a connection with given cache provider.\n */\n async connect(): Promise<void> {}\n\n /**\n * Disconnects with given cache provider.\n */\n async disconnect(): Promise<void> {}\n\n /**\n * Creates table for storing cache if it does not exist yet.\n */\n async synchronize(queryRunner?: QueryRunner): Promise<void> {\n queryRunner = this.getQueryRunner(queryRunner)\n const driver = this.connection.driver\n const tableExist = await queryRunner.hasTable(\n this.queryResultCacheTable,\n ) // todo: table name should be configurable\n if (tableExist) return\n\n await queryRunner.createTable(\n new Table({\n database: this.queryResultCacheDatabase,\n schema: this.queryResultCacheSchema,\n name: this.queryResultCacheTable,\n columns: [\n {\n name: \"id\",\n isPrimary: true,\n isNullable: false,\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheId,\n }),\n generationStrategy:\n driver.options.type === \"spanner\"\n ? \"uuid\"\n : \"increment\",\n isGenerated: true,\n },\n {\n name: \"identifier\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheIdentifier,\n }),\n isNullable: true,\n },\n {\n name: \"time\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheTime,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"duration\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheDuration,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"query\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheQuery,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"result\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheResult,\n }),\n isNullable: false,\n },\n ],\n }),\n )\n }\n\n /**\n * Caches given query result.\n * Returns cache result if found.\n * Returns undefined if result is not cached.\n */\n getFromCache(\n options: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<QueryResultCacheOptions | undefined> {\n queryRunner = this.getQueryRunner(queryRunner)\n const qb = this.connection\n .createQueryBuilder(queryRunner)\n .select()\n .from(this.queryResultCacheTable, \"cache\")\n\n if (options.identifier) {\n return qb\n .where(\n `${qb.escape(\"cache\")}.${qb.escape(\n \"identifier\",\n )} = :identifier`,\n )\n .setParameters({\n identifier:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.identifier, \"nvarchar\")\n : options.identifier,\n })\n .getRawOne()\n } else if (options.query) {\n if (this.connection.driver.options.type === \"oracle\") {\n return qb\n .where(\n `dbms_lob.compare(${qb.escape(\"cache\")}.${qb.escape(\n \"query\",\n )}, :query) = 0`,\n { query: options.query },\n )\n .getRawOne()\n }\n\n return qb\n .where(`${qb.escape(\"cache\")}.${qb.escape(\"query\")} = :query`)\n .setParameters({\n query:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.query, \"nvarchar\")\n : options.query,\n })\n .getRawOne()\n }\n\n return Promise.resolve(undefined)\n }\n\n /**\n * Checks if cache is expired or not.\n */\n isExpired(savedCache: QueryResultCacheOptions): boolean {\n const duration =\n typeof savedCache.duration === \"string\"\n ? parseInt(savedCache.duration)\n : savedCache.duration\n return (\n (typeof savedCache.time === \"string\"\n ? parseInt(savedCache.time as any)\n : savedCache.time)! +\n duration <\n new Date().getTime()\n )\n }\n\n /**\n * Stores given query result in the cache.\n */\n async storeInCache(\n options: QueryResultCacheOptions,\n savedCache: QueryResultCacheOptions | undefined,\n queryRunner?: QueryRunner,\n ): Promise<void> {\n const shouldCreateQueryRunner =\n queryRunner === undefined ||\n queryRunner?.getReplicationMode() === \"slave\"\n\n if (queryRunner === undefined || shouldCreateQueryRunner) {\n queryRunner = this.connection.createQueryRunner(\"master\")\n }\n\n let insertedValues: ObjectLiteral = options\n if (this.connection.driver.options.type === \"mssql\") {\n // todo: bad abstraction, re-implement this part, probably better if we create an entity metadata for cache table\n insertedValues = {\n identifier: new MssqlParameter(options.identifier, \"nvarchar\"),\n time: new MssqlParameter(options.time, \"bigint\"),\n duration: new MssqlParameter(options.duration, \"int\"),\n query: new MssqlParameter(options.query, \"nvarchar\"),\n result: new MssqlParameter(options.result, \"nvarchar\"),\n }\n }\n\n if (savedCache && savedCache.identifier) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n qb.where(`${qb.escape(\"identifier\")} = :condition`, {\n condition: insertedValues.identifier,\n })\n await qb.execute()\n } else if (savedCache && savedCache.query) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n if (this.connection.driver.options.type === \"oracle\") {\n qb.where(`dbms_lob.compare(\"query\", :condition) = 0`, {\n condition: insertedValues.query,\n })\n } else {\n qb.where(`${qb.escape(\"query\")} = :condition`, {\n condition: insertedValues.query,\n })\n }\n\n await qb.execute()\n } else {\n // Spanner does not support auto-generated columns\n if (\n this.connection.driver.options.type === \"spanner\" &&\n !insertedValues.id\n ) {\n insertedValues.id = uuidv4()\n }\n\n // otherwise insert\n await queryRunner.manager\n .createQueryBuilder()\n .insert()\n .into(this.queryResultCacheTable)\n .values(insertedValues)\n .execute()\n }\n\n if (shouldCreateQueryRunner) {\n await queryRunner.release()\n }\n }\n\n /**\n * Clears everything stored in the cache.\n */\n async clear(queryRunner: QueryRunner): Promise<void> {\n return this.getQueryRunner(queryRunner).clearTable(\n this.queryResultCacheTable,\n )\n }\n\n /**\n * Removes all cached results by given identifiers from cache.\n */\n async remove(\n identifiers: string[],\n queryRunner?: QueryRunner,\n ): Promise<void> {\n await Promise.all(\n identifiers.map((identifier) => {\n const qb =\n this.getQueryRunner(\n queryRunner,\n ).manager.createQueryBuilder()\n return qb\n .delete()\n .from(this.queryResultCacheTable)\n .where(`${qb.escape(\"identifier\")} = :identifier`, {\n identifier,\n })\n .execute()\n }),\n )\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets a query runner to work with.\n */\n protected getQueryRunner(\n queryRunner: QueryRunner | undefined,\n ): QueryRunner {\n if (queryRunner) return queryRunner\n\n return this.connection.createQueryRunner()\n }\n}\n"],"sourceRoot":".."}
1
+ {"version":3,"sources":["../browser/src/cache/DbQueryResultCache.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAA;AAEnE,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAA;AAGrD,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MA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B,EAAE,CAAA;YACpD,OAAO,EAAE;iBACJ,MAAM,EAAE;iBACR,IAAI,CAAC,IAAI,CAAC,qBAAqB,CAAC;iBAChC,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,gBAAgB,EAAE;gBAC/C,UAAU;aACb,CAAC;iBACD,OAAO,EAAE,CAAA;QAClB,CAAC,CAAC,CACL,CAAA;QAED,IAAI,CAAC,WAAW,EAAE;YACd,MAAM,YAAY,CAAC,OAAO,EAAE,CAAA;SAC/B;IACL,CAAC;IAED,4EAA4E;IAC5E,oBAAoB;IACpB,4EAA4E;IAE5E;;OAEG;IACO,cAAc,CAAC,WAAyB;QAC9C,IAAI,WAAW;YAAE,OAAO,WAAW,CAAA;QAEnC,OAAO,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAA;IAC9C,CAAC;CACJ","file":"DbQueryResultCache.js","sourcesContent":["import { ObjectLiteral } from \"../common/ObjectLiteral\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { MssqlParameter } from \"../driver/sqlserver/MssqlParameter\"\nimport { QueryRunner } from \"../query-runner/QueryRunner\"\nimport { Table } from \"../schema-builder/table/Table\"\nimport { QueryResultCache } from \"./QueryResultCache\"\nimport { QueryResultCacheOptions } from \"./QueryResultCacheOptions\"\nimport { v4 as uuidv4 } from \"uuid\"\n\n/**\n * Caches query result into current database, into separate table called \"query-result-cache\".\n */\nexport class DbQueryResultCache implements QueryResultCache {\n // -------------------------------------------------------------------------\n // Private properties\n // -------------------------------------------------------------------------\n\n private queryResultCacheTable: string\n\n private queryResultCacheDatabase?: string\n\n private queryResultCacheSchema?: string\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(protected connection: DataSource) {\n const { schema } = this.connection.driver.options as any\n const database = this.connection.driver.database\n const cacheOptions =\n typeof this.connection.options.cache === \"object\"\n ? this.connection.options.cache\n : {}\n const cacheTableName = cacheOptions.tableName || \"query-result-cache\"\n\n this.queryResultCacheDatabase = database\n this.queryResultCacheSchema = schema\n this.queryResultCacheTable = this.connection.driver.buildTableName(\n cacheTableName,\n schema,\n database,\n )\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates a connection with given cache provider.\n */\n async connect(): Promise<void> {}\n\n /**\n * Disconnects with given cache provider.\n */\n async disconnect(): Promise<void> {}\n\n /**\n * Creates table for storing cache if it does not exist yet.\n */\n async synchronize(queryRunner?: QueryRunner): Promise<void> {\n queryRunner = this.getQueryRunner(queryRunner)\n const driver = this.connection.driver\n const tableExist = await queryRunner.hasTable(\n this.queryResultCacheTable,\n ) // todo: table name should be configurable\n if (tableExist) return\n\n await queryRunner.createTable(\n new Table({\n database: this.queryResultCacheDatabase,\n schema: this.queryResultCacheSchema,\n name: this.queryResultCacheTable,\n columns: [\n {\n name: \"id\",\n isPrimary: true,\n isNullable: false,\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheId,\n }),\n generationStrategy:\n driver.options.type === \"spanner\"\n ? \"uuid\"\n : \"increment\",\n isGenerated: true,\n },\n {\n name: \"identifier\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheIdentifier,\n }),\n isNullable: true,\n },\n {\n name: \"time\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheTime,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"duration\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheDuration,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"query\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheQuery,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"result\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheResult,\n }),\n isNullable: false,\n },\n ],\n }),\n )\n }\n\n /**\n * Caches given query result.\n * Returns cache result if found.\n * Returns undefined if result is not cached.\n */\n getFromCache(\n options: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<QueryResultCacheOptions | undefined> {\n queryRunner = this.getQueryRunner(queryRunner)\n const qb = this.connection\n .createQueryBuilder(queryRunner)\n .select()\n .from(this.queryResultCacheTable, \"cache\")\n\n if (options.identifier) {\n return qb\n .where(\n `${qb.escape(\"cache\")}.${qb.escape(\n \"identifier\",\n )} = :identifier`,\n )\n .setParameters({\n identifier:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.identifier, \"nvarchar\")\n : options.identifier,\n })\n .getRawOne()\n } else if (options.query) {\n if (this.connection.driver.options.type === \"oracle\") {\n return qb\n .where(\n `dbms_lob.compare(${qb.escape(\"cache\")}.${qb.escape(\n \"query\",\n )}, :query) = 0`,\n { query: options.query },\n )\n .getRawOne()\n }\n\n return qb\n .where(`${qb.escape(\"cache\")}.${qb.escape(\"query\")} = :query`)\n .setParameters({\n query:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.query, \"nvarchar\")\n : options.query,\n })\n .getRawOne()\n }\n\n return Promise.resolve(undefined)\n }\n\n /**\n * Checks if cache is expired or not.\n */\n isExpired(savedCache: QueryResultCacheOptions): boolean {\n const duration =\n typeof savedCache.duration === \"string\"\n ? parseInt(savedCache.duration)\n : savedCache.duration\n return (\n (typeof savedCache.time === \"string\"\n ? parseInt(savedCache.time as any)\n : savedCache.time)! +\n duration <\n new Date().getTime()\n )\n }\n\n /**\n * Stores given query result in the cache.\n */\n async storeInCache(\n options: QueryResultCacheOptions,\n savedCache: QueryResultCacheOptions | undefined,\n queryRunner?: QueryRunner,\n ): Promise<void> {\n const shouldCreateQueryRunner =\n queryRunner === undefined ||\n queryRunner?.getReplicationMode() === \"slave\"\n\n if (queryRunner === undefined || shouldCreateQueryRunner) {\n queryRunner = this.connection.createQueryRunner(\"master\")\n }\n\n let insertedValues: ObjectLiteral = options\n if (this.connection.driver.options.type === \"mssql\") {\n // todo: bad abstraction, re-implement this part, probably better if we create an entity metadata for cache table\n insertedValues = {\n identifier: new MssqlParameter(options.identifier, \"nvarchar\"),\n time: new MssqlParameter(options.time, \"bigint\"),\n duration: new MssqlParameter(options.duration, \"int\"),\n query: new MssqlParameter(options.query, \"nvarchar\"),\n result: new MssqlParameter(options.result, \"nvarchar\"),\n }\n }\n\n if (savedCache && savedCache.identifier) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n qb.where(`${qb.escape(\"identifier\")} = :condition`, {\n condition: insertedValues.identifier,\n })\n await qb.execute()\n } else if (savedCache && savedCache.query) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n if (this.connection.driver.options.type === \"oracle\") {\n qb.where(`dbms_lob.compare(\"query\", :condition) = 0`, {\n condition: insertedValues.query,\n })\n } else {\n qb.where(`${qb.escape(\"query\")} = :condition`, {\n condition: insertedValues.query,\n })\n }\n\n await qb.execute()\n } else {\n // Spanner does not support auto-generated columns\n if (\n this.connection.driver.options.type === \"spanner\" &&\n !insertedValues.id\n ) {\n insertedValues.id = uuidv4()\n }\n\n // otherwise insert\n await queryRunner.manager\n .createQueryBuilder()\n .insert()\n .into(this.queryResultCacheTable)\n .values(insertedValues)\n .execute()\n }\n\n if (shouldCreateQueryRunner) {\n await queryRunner.release()\n }\n }\n\n /**\n * Clears everything stored in the cache.\n */\n async clear(queryRunner: QueryRunner): Promise<void> {\n return this.getQueryRunner(queryRunner).clearTable(\n this.queryResultCacheTable,\n )\n }\n\n /**\n * Removes all cached results by given identifiers from cache.\n */\n async remove(\n identifiers: string[],\n queryRunner?: QueryRunner,\n ): Promise<void> {\n let _queryRunner: QueryRunner = queryRunner || this.getQueryRunner()\n await Promise.all(\n identifiers.map((identifier) => {\n const qb = _queryRunner.manager.createQueryBuilder()\n return qb\n .delete()\n .from(this.queryResultCacheTable)\n .where(`${qb.escape(\"identifier\")} = :identifier`, {\n identifier,\n })\n .execute()\n }),\n )\n\n if (!queryRunner) {\n await _queryRunner.release()\n }\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets a query runner to work with.\n */\n protected getQueryRunner(queryRunner?: QueryRunner): QueryRunner {\n if (queryRunner) return queryRunner\n\n return this.connection.createQueryRunner()\n }\n}\n"],"sourceRoot":".."}
@@ -48,5 +48,5 @@ export declare class DbQueryResultCache implements QueryResultCache {
48
48
  /**
49
49
  * Gets a query runner to work with.
50
50
  */
51
- protected getQueryRunner(queryRunner: QueryRunner | undefined): QueryRunner;
51
+ protected getQueryRunner(queryRunner?: QueryRunner): QueryRunner;
52
52
  }
@@ -229,8 +229,9 @@ class DbQueryResultCache {
229
229
  * Removes all cached results by given identifiers from cache.
230
230
  */
231
231
  async remove(identifiers, queryRunner) {
232
+ let _queryRunner = queryRunner || this.getQueryRunner();
232
233
  await Promise.all(identifiers.map((identifier) => {
233
- const qb = this.getQueryRunner(queryRunner).manager.createQueryBuilder();
234
+ const qb = _queryRunner.manager.createQueryBuilder();
234
235
  return qb
235
236
  .delete()
236
237
  .from(this.queryResultCacheTable)
@@ -239,6 +240,9 @@ class DbQueryResultCache {
239
240
  })
240
241
  .execute();
241
242
  }));
243
+ if (!queryRunner) {
244
+ await _queryRunner.release();
245
+ }
242
246
  }
243
247
  // -------------------------------------------------------------------------
244
248
  // Protected Methods
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/cache/DbQueryResultCache.ts"],"names":[],"mappings":";;;AAEA,uEAAmE;AAEnE,yDAAqD;AAGrD,+BAAmC;AAEnC;;GAEG;AACH,MAAa,kBAAkB;IAW3B,4EAA4E;IAC5E,cAAc;IACd,4EAA4E;IAE5E,YAAsB,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;QACxC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,OAAc,CAAA;QACxD,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,QAAQ,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,CACpB,WAAoC;QAEpC,IAAI,WAAW;YAAE,OAAO,WAAW,CAAA;QAEnC,OAAO,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAA;IAC9C,CAAC;CACJ;AA/TD,gDA+TC","file":"DbQueryResultCache.js","sourcesContent":["import { ObjectLiteral } from \"../common/ObjectLiteral\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { MssqlParameter } from \"../driver/sqlserver/MssqlParameter\"\nimport { QueryRunner } from \"../query-runner/QueryRunner\"\nimport { Table } from \"../schema-builder/table/Table\"\nimport { QueryResultCache } from \"./QueryResultCache\"\nimport { QueryResultCacheOptions } from \"./QueryResultCacheOptions\"\nimport { v4 as uuidv4 } from \"uuid\"\n\n/**\n * Caches query result into current database, into separate table called \"query-result-cache\".\n */\nexport class DbQueryResultCache implements QueryResultCache {\n // -------------------------------------------------------------------------\n // Private properties\n // -------------------------------------------------------------------------\n\n private queryResultCacheTable: string\n\n private queryResultCacheDatabase?: string\n\n private queryResultCacheSchema?: string\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(protected connection: DataSource) {\n const { schema } = this.connection.driver.options as any\n const database = this.connection.driver.database\n const cacheOptions =\n typeof this.connection.options.cache === \"object\"\n ? this.connection.options.cache\n : {}\n const cacheTableName = cacheOptions.tableName || \"query-result-cache\"\n\n this.queryResultCacheDatabase = database\n this.queryResultCacheSchema = schema\n this.queryResultCacheTable = this.connection.driver.buildTableName(\n cacheTableName,\n schema,\n database,\n )\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates a connection with given cache provider.\n */\n async connect(): Promise<void> {}\n\n /**\n * Disconnects with given cache provider.\n */\n async disconnect(): Promise<void> {}\n\n /**\n * Creates table for storing cache if it does not exist yet.\n */\n async synchronize(queryRunner?: QueryRunner): Promise<void> {\n queryRunner = this.getQueryRunner(queryRunner)\n const driver = this.connection.driver\n const tableExist = await queryRunner.hasTable(\n this.queryResultCacheTable,\n ) // todo: table name should be configurable\n if (tableExist) return\n\n await queryRunner.createTable(\n new Table({\n database: this.queryResultCacheDatabase,\n schema: this.queryResultCacheSchema,\n name: this.queryResultCacheTable,\n columns: [\n {\n name: \"id\",\n isPrimary: true,\n isNullable: false,\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheId,\n }),\n generationStrategy:\n driver.options.type === \"spanner\"\n ? \"uuid\"\n : \"increment\",\n isGenerated: true,\n },\n {\n name: \"identifier\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheIdentifier,\n }),\n isNullable: true,\n },\n {\n name: \"time\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheTime,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"duration\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheDuration,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"query\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheQuery,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"result\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheResult,\n }),\n isNullable: false,\n },\n ],\n }),\n )\n }\n\n /**\n * Caches given query result.\n * Returns cache result if found.\n * Returns undefined if result is not cached.\n */\n getFromCache(\n options: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<QueryResultCacheOptions | undefined> {\n queryRunner = this.getQueryRunner(queryRunner)\n const qb = this.connection\n .createQueryBuilder(queryRunner)\n .select()\n .from(this.queryResultCacheTable, \"cache\")\n\n if (options.identifier) {\n return qb\n .where(\n `${qb.escape(\"cache\")}.${qb.escape(\n \"identifier\",\n )} = :identifier`,\n )\n .setParameters({\n identifier:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.identifier, \"nvarchar\")\n : options.identifier,\n })\n .getRawOne()\n } else if (options.query) {\n if (this.connection.driver.options.type === \"oracle\") {\n return qb\n .where(\n `dbms_lob.compare(${qb.escape(\"cache\")}.${qb.escape(\n \"query\",\n )}, :query) = 0`,\n { query: options.query },\n )\n .getRawOne()\n }\n\n return qb\n .where(`${qb.escape(\"cache\")}.${qb.escape(\"query\")} = :query`)\n .setParameters({\n query:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.query, \"nvarchar\")\n : options.query,\n })\n .getRawOne()\n }\n\n return Promise.resolve(undefined)\n }\n\n /**\n * Checks if cache is expired or not.\n */\n isExpired(savedCache: QueryResultCacheOptions): boolean {\n const duration =\n typeof savedCache.duration === \"string\"\n ? parseInt(savedCache.duration)\n : savedCache.duration\n return (\n (typeof savedCache.time === \"string\"\n ? parseInt(savedCache.time as any)\n : savedCache.time)! +\n duration <\n new Date().getTime()\n )\n }\n\n /**\n * Stores given query result in the cache.\n */\n async storeInCache(\n options: QueryResultCacheOptions,\n savedCache: QueryResultCacheOptions | undefined,\n queryRunner?: QueryRunner,\n ): Promise<void> {\n const shouldCreateQueryRunner =\n queryRunner === undefined ||\n queryRunner?.getReplicationMode() === \"slave\"\n\n if (queryRunner === undefined || shouldCreateQueryRunner) {\n queryRunner = this.connection.createQueryRunner(\"master\")\n }\n\n let insertedValues: ObjectLiteral = options\n if (this.connection.driver.options.type === \"mssql\") {\n // todo: bad abstraction, re-implement this part, probably better if we create an entity metadata for cache table\n insertedValues = {\n identifier: new MssqlParameter(options.identifier, \"nvarchar\"),\n time: new MssqlParameter(options.time, \"bigint\"),\n duration: new MssqlParameter(options.duration, \"int\"),\n query: new MssqlParameter(options.query, \"nvarchar\"),\n result: new MssqlParameter(options.result, \"nvarchar\"),\n }\n }\n\n if (savedCache && savedCache.identifier) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n qb.where(`${qb.escape(\"identifier\")} = :condition`, {\n condition: insertedValues.identifier,\n })\n await qb.execute()\n } else if (savedCache && savedCache.query) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n if (this.connection.driver.options.type === \"oracle\") {\n qb.where(`dbms_lob.compare(\"query\", :condition) = 0`, {\n condition: insertedValues.query,\n })\n } else {\n qb.where(`${qb.escape(\"query\")} = :condition`, {\n condition: insertedValues.query,\n })\n }\n\n await qb.execute()\n } else {\n // Spanner does not support auto-generated columns\n if (\n this.connection.driver.options.type === \"spanner\" &&\n !insertedValues.id\n ) {\n insertedValues.id = uuidv4()\n }\n\n // otherwise insert\n await queryRunner.manager\n .createQueryBuilder()\n .insert()\n .into(this.queryResultCacheTable)\n .values(insertedValues)\n .execute()\n }\n\n if (shouldCreateQueryRunner) {\n await queryRunner.release()\n }\n }\n\n /**\n * Clears everything stored in the cache.\n */\n async clear(queryRunner: QueryRunner): Promise<void> {\n return this.getQueryRunner(queryRunner).clearTable(\n this.queryResultCacheTable,\n )\n }\n\n /**\n * Removes all cached results by given identifiers from cache.\n */\n async remove(\n identifiers: string[],\n queryRunner?: QueryRunner,\n ): Promise<void> {\n await Promise.all(\n identifiers.map((identifier) => {\n const qb =\n this.getQueryRunner(\n queryRunner,\n ).manager.createQueryBuilder()\n return qb\n .delete()\n .from(this.queryResultCacheTable)\n .where(`${qb.escape(\"identifier\")} = :identifier`, {\n identifier,\n })\n .execute()\n }),\n )\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets a query runner to work with.\n */\n protected getQueryRunner(\n queryRunner: QueryRunner | undefined,\n ): QueryRunner {\n if (queryRunner) return queryRunner\n\n return this.connection.createQueryRunner()\n }\n}\n"],"sourceRoot":".."}
1
+ {"version":3,"sources":["../../src/cache/DbQueryResultCache.ts"],"names":[],"mappings":";;;AAEA,uEAAmE;AAEnE,yDAAqD;AAGrD,+BAAmC;AAEnC;;GAEG;AACH,MAAa,kBAAkB;IAW3B,4EAA4E;IAC5E,cAAc;IACd,4EAA4E;IAE5E,YAAsB,UAAsB;QAAtB,eAAU,GAAV,UAAU,CA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,MAAM,CAAC,YAAY,CAAC,gBAAgB,EAAE;gBAC/C,UAAU;aACb,CAAC;iBACD,OAAO,EAAE,CAAA;QAClB,CAAC,CAAC,CACL,CAAA;QAED,IAAI,CAAC,WAAW,EAAE;YACd,MAAM,YAAY,CAAC,OAAO,EAAE,CAAA;SAC/B;IACL,CAAC;IAED,4EAA4E;IAC5E,oBAAoB;IACpB,4EAA4E;IAE5E;;OAEG;IACO,cAAc,CAAC,WAAyB;QAC9C,IAAI,WAAW;YAAE,OAAO,WAAW,CAAA;QAEnC,OAAO,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAA;IAC9C,CAAC;CACJ;AA/TD,gDA+TC","file":"DbQueryResultCache.js","sourcesContent":["import { ObjectLiteral } from \"../common/ObjectLiteral\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { MssqlParameter } from \"../driver/sqlserver/MssqlParameter\"\nimport { QueryRunner } from \"../query-runner/QueryRunner\"\nimport { Table } from \"../schema-builder/table/Table\"\nimport { QueryResultCache } from \"./QueryResultCache\"\nimport { QueryResultCacheOptions } from \"./QueryResultCacheOptions\"\nimport { v4 as uuidv4 } from \"uuid\"\n\n/**\n * Caches query result into current database, into separate table called \"query-result-cache\".\n */\nexport class DbQueryResultCache implements QueryResultCache {\n // -------------------------------------------------------------------------\n // Private properties\n // -------------------------------------------------------------------------\n\n private queryResultCacheTable: string\n\n private queryResultCacheDatabase?: string\n\n private queryResultCacheSchema?: string\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(protected connection: DataSource) {\n const { schema } = this.connection.driver.options as any\n const database = this.connection.driver.database\n const cacheOptions =\n typeof this.connection.options.cache === \"object\"\n ? this.connection.options.cache\n : {}\n const cacheTableName = cacheOptions.tableName || \"query-result-cache\"\n\n this.queryResultCacheDatabase = database\n this.queryResultCacheSchema = schema\n this.queryResultCacheTable = this.connection.driver.buildTableName(\n cacheTableName,\n schema,\n database,\n )\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates a connection with given cache provider.\n */\n async connect(): Promise<void> {}\n\n /**\n * Disconnects with given cache provider.\n */\n async disconnect(): Promise<void> {}\n\n /**\n * Creates table for storing cache if it does not exist yet.\n */\n async synchronize(queryRunner?: QueryRunner): Promise<void> {\n queryRunner = this.getQueryRunner(queryRunner)\n const driver = this.connection.driver\n const tableExist = await queryRunner.hasTable(\n this.queryResultCacheTable,\n ) // todo: table name should be configurable\n if (tableExist) return\n\n await queryRunner.createTable(\n new Table({\n database: this.queryResultCacheDatabase,\n schema: this.queryResultCacheSchema,\n name: this.queryResultCacheTable,\n columns: [\n {\n name: \"id\",\n isPrimary: true,\n isNullable: false,\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheId,\n }),\n generationStrategy:\n driver.options.type === \"spanner\"\n ? \"uuid\"\n : \"increment\",\n isGenerated: true,\n },\n {\n name: \"identifier\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheIdentifier,\n }),\n isNullable: true,\n },\n {\n name: \"time\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheTime,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"duration\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheDuration,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"query\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheQuery,\n }),\n isPrimary: false,\n isNullable: false,\n },\n {\n name: \"result\",\n type: driver.normalizeType({\n type: driver.mappedDataTypes.cacheResult,\n }),\n isNullable: false,\n },\n ],\n }),\n )\n }\n\n /**\n * Caches given query result.\n * Returns cache result if found.\n * Returns undefined if result is not cached.\n */\n getFromCache(\n options: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<QueryResultCacheOptions | undefined> {\n queryRunner = this.getQueryRunner(queryRunner)\n const qb = this.connection\n .createQueryBuilder(queryRunner)\n .select()\n .from(this.queryResultCacheTable, \"cache\")\n\n if (options.identifier) {\n return qb\n .where(\n `${qb.escape(\"cache\")}.${qb.escape(\n \"identifier\",\n )} = :identifier`,\n )\n .setParameters({\n identifier:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.identifier, \"nvarchar\")\n : options.identifier,\n })\n .getRawOne()\n } else if (options.query) {\n if (this.connection.driver.options.type === \"oracle\") {\n return qb\n .where(\n `dbms_lob.compare(${qb.escape(\"cache\")}.${qb.escape(\n \"query\",\n )}, :query) = 0`,\n { query: options.query },\n )\n .getRawOne()\n }\n\n return qb\n .where(`${qb.escape(\"cache\")}.${qb.escape(\"query\")} = :query`)\n .setParameters({\n query:\n this.connection.driver.options.type === \"mssql\"\n ? new MssqlParameter(options.query, \"nvarchar\")\n : options.query,\n })\n .getRawOne()\n }\n\n return Promise.resolve(undefined)\n }\n\n /**\n * Checks if cache is expired or not.\n */\n isExpired(savedCache: QueryResultCacheOptions): boolean {\n const duration =\n typeof savedCache.duration === \"string\"\n ? parseInt(savedCache.duration)\n : savedCache.duration\n return (\n (typeof savedCache.time === \"string\"\n ? parseInt(savedCache.time as any)\n : savedCache.time)! +\n duration <\n new Date().getTime()\n )\n }\n\n /**\n * Stores given query result in the cache.\n */\n async storeInCache(\n options: QueryResultCacheOptions,\n savedCache: QueryResultCacheOptions | undefined,\n queryRunner?: QueryRunner,\n ): Promise<void> {\n const shouldCreateQueryRunner =\n queryRunner === undefined ||\n queryRunner?.getReplicationMode() === \"slave\"\n\n if (queryRunner === undefined || shouldCreateQueryRunner) {\n queryRunner = this.connection.createQueryRunner(\"master\")\n }\n\n let insertedValues: ObjectLiteral = options\n if (this.connection.driver.options.type === \"mssql\") {\n // todo: bad abstraction, re-implement this part, probably better if we create an entity metadata for cache table\n insertedValues = {\n identifier: new MssqlParameter(options.identifier, \"nvarchar\"),\n time: new MssqlParameter(options.time, \"bigint\"),\n duration: new MssqlParameter(options.duration, \"int\"),\n query: new MssqlParameter(options.query, \"nvarchar\"),\n result: new MssqlParameter(options.result, \"nvarchar\"),\n }\n }\n\n if (savedCache && savedCache.identifier) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n qb.where(`${qb.escape(\"identifier\")} = :condition`, {\n condition: insertedValues.identifier,\n })\n await qb.execute()\n } else if (savedCache && savedCache.query) {\n // if exist then update\n const qb = queryRunner.manager\n .createQueryBuilder()\n .update(this.queryResultCacheTable)\n .set(insertedValues)\n\n if (this.connection.driver.options.type === \"oracle\") {\n qb.where(`dbms_lob.compare(\"query\", :condition) = 0`, {\n condition: insertedValues.query,\n })\n } else {\n qb.where(`${qb.escape(\"query\")} = :condition`, {\n condition: insertedValues.query,\n })\n }\n\n await qb.execute()\n } else {\n // Spanner does not support auto-generated columns\n if (\n this.connection.driver.options.type === \"spanner\" &&\n !insertedValues.id\n ) {\n insertedValues.id = uuidv4()\n }\n\n // otherwise insert\n await queryRunner.manager\n .createQueryBuilder()\n .insert()\n .into(this.queryResultCacheTable)\n .values(insertedValues)\n .execute()\n }\n\n if (shouldCreateQueryRunner) {\n await queryRunner.release()\n }\n }\n\n /**\n * Clears everything stored in the cache.\n */\n async clear(queryRunner: QueryRunner): Promise<void> {\n return this.getQueryRunner(queryRunner).clearTable(\n this.queryResultCacheTable,\n )\n }\n\n /**\n * Removes all cached results by given identifiers from cache.\n */\n async remove(\n identifiers: string[],\n queryRunner?: QueryRunner,\n ): Promise<void> {\n let _queryRunner: QueryRunner = queryRunner || this.getQueryRunner()\n await Promise.all(\n identifiers.map((identifier) => {\n const qb = _queryRunner.manager.createQueryBuilder()\n return qb\n .delete()\n .from(this.queryResultCacheTable)\n .where(`${qb.escape(\"identifier\")} = :identifier`, {\n identifier,\n })\n .execute()\n }),\n )\n\n if (!queryRunner) {\n await _queryRunner.release()\n }\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets a query runner to work with.\n */\n protected getQueryRunner(queryRunner?: QueryRunner): QueryRunner {\n if (queryRunner) return queryRunner\n\n return this.connection.createQueryRunner()\n }\n}\n"],"sourceRoot":".."}
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "typeorm", "private": false, "version": "0.3.7-dev.97e1dc1", "description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.", "license": "MIT", "readmeFilename": "README.md", "author": { "name": "Umed Khudoiberdiev", "email": "pleerock.me@gmail.com" }, "engines": { "node": ">= 12.9.0" }, "exports": { ".": { "types": "./index.d.ts", "node": { "import": "./index.mjs", "require": "./index.js", "types": "./index.d.ts" }, "browser": { "require": "./index.js", "import": "./browser/index.js", "default": "./index.js" } }, "./browser": { "types": "./index.d.ts", "default": "./browser/index.js" }, "./*.js": "./*.js", "./*": { "require": "./*.js", "import": "./*" } }, "main": "./index.js", "module": "./index.mjs", "types": "./index.d.ts", "browser": { "./browser/connection/ConnectionOptionsReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsXmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsYmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/driver/aurora-data-api/AuroraDataApiDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/better-sqlite3/BetterSqlite3Driver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/cockroachdb/CockroachDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoQueryRunner.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mysql/MysqlDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/oracle/OracleDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/postgres/PostgresDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sap/SapDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlite/SqliteDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlserver/SqlServerDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/entity-manager/MongoEntityManager.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/logger/FileLogger.js": "./browser/platform/BrowserFileLoggerDummy.js", "./browser/platform/PlatformTools.js": "./browser/platform/BrowserPlatformTools.js", "./browser/repository/MongoRepository.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/util/DirectoryExportedClassesLoader.js": "./browser/platform/BrowserDirectoryExportedClassesLoader.js", "./index.js": "./browser/index.js", "./index.mjs": "./browser/index.js" }, "repository": { "type": "git", "url": "https://github.com/typeorm/typeorm.git" }, "bugs": { "url": "https://github.com/typeorm/typeorm/issues" }, "homepage": "https://typeorm.io", "tags": [ "orm", "typescript", "typescript-orm", "mysql", "mysql-orm", "postgresql", "postgresql-orm", "mariadb", "mariadb-orm", "spanner", "sqlite", "sqlite-orm", "sql-server", "sql-server-orm", "oracle", "oracle-orm", "cloud-spanner", "cloud-spanner-orm" ], "devDependencies": { "@types/app-root-path": "^1.2.4", "@types/chai": "^4.2.15", "@types/chai-as-promised": "^7.1.3", "@types/debug": "^4.1.5", "@types/js-yaml": "^4.0.0", "@types/mkdirp": "^1.0.1", "@types/mocha": "^8.2.1", "@types/node": "^14.14.31", "@types/rimraf": "^3.0.0", "@types/sha.js": "^2.4.0", "@types/sinon": "^9.0.10", "@types/source-map-support": "^0.5.3", "@types/uuid": "^8.3.4", "@types/xml2js": "^0.4.8", "@types/yargs": "^17.0.2", "better-sqlite3": "^7.1.2", "chai": "^4.3.0", "chai-as-promised": "^7.1.1", "class-transformer": "^0.4.0", "conventional-changelog-angular": "^5.0.12", "conventional-changelog-cli": "^2.1.1", "del": "^6.0.0", "gulp": "^4.0.2", "gulp-istanbul": "^1.1.3", "gulp-mocha": "^8.0.0", "gulp-rename": "^2.0.0", "gulp-replace": "^1.0.0", "gulp-shell": "^0.8.0", "gulp-sourcemaps": "^3.0.0", "gulp-typescript": "^6.0.0-alpha.1", "gulpclass": "^0.2.0", "husky": "^5.1.1", "mocha": "^8.3.0", "mongodb": "^3.6.4", "mssql": "^7.3.0", "mysql": "^2.18.1", "mysql2": "^2.2.5", "pg": "^8.5.1", "pg-query-stream": "^4.0.0", "prettier": "^2.5.1", "redis": "^3.1.1", "remap-istanbul": "^0.13.0", "rimraf": "^3.0.2", "sinon": "^9.2.4", "sinon-chai": "^3.5.0", "source-map-support": "^0.5.19", "sql.js": "^1.4.0", "sqlite3": "^5.0.3", "ts-node": "^10.7.0", "typeorm-aurora-data-api-driver": "^2.0.0", "typescript": "^4.6.2" }, "peerDependencies": { "@google-cloud/spanner": "^5.18.0", "@sap/hana-client": "^2.11.14", "better-sqlite3": "^7.1.2", "hdb-pool": "^0.1.6", "ioredis": "^5.0.4", "mongodb": "^3.6.0", "mssql": "^7.3.0", "mysql2": "^2.2.5", "oracledb": "^5.1.0", "pg": "^8.5.1", "pg-native": "^3.0.0", "pg-query-stream": "^4.0.0", "redis": "^3.1.1 || ^4.0.0", "sql.js": "^1.4.0", "sqlite3": "^5.0.3", "ts-node": "^10.7.0", "typeorm-aurora-data-api-driver": "^2.0.0" }, "peerDependenciesMeta": { "@google-cloud/spanner": { "optional": true }, "@sap/hana-client": { "optional": true }, "better-sqlite3": { "optional": true }, "hdb-pool": { "optional": true }, "ioredis": { "optional": true }, "mongodb": { "optional": true }, "mssql": { "optional": true }, "mysql2": { "optional": true }, "oracledb": { "optional": true }, "pg": { "optional": true }, "pg-native": { "optional": true }, "pg-query-stream": { "optional": true }, "redis": { "optional": true }, "sql.js": { "optional": true }, "sqlite3": { "optional": true }, "ts-node": { "optional": true }, "typeorm-aurora-data-api-driver": { "optional": true } }, "dependencies": { "@sqltools/formatter": "^1.2.2", "app-root-path": "^3.0.0", "buffer": "^6.0.3", "chalk": "^4.1.0", "cli-highlight": "^2.1.11", "date-fns": "^2.28.0", "debug": "^4.3.3", "dotenv": "^16.0.0", "glob": "^7.2.0", "js-yaml": "^4.1.0", "mkdirp": "^1.0.4", "reflect-metadata": "^0.1.13", "sha.js": "^2.4.11", "tslib": "^2.3.1", "uuid": "^8.3.2", "xml2js": "^0.4.23", "yargs": "^17.3.1" }, "scripts": { "test": "rimraf ./build && tsc && mocha --file ./build/compiled/test/utils/test-setup.js --bail --recursive --timeout 60000 ./build/compiled/test", "test-fast": "mocha --file ./build/compiled/test/utils/test-setup.js --bail --recursive --timeout 60000 ./build/compiled/test", "compile": "rimraf ./build && tsc", "watch": "./node_modules/.bin/tsc -w", "package": "gulp package", "pack": "gulp pack", "lint": "prettier --check \"./src/**/*.ts\" \"./test/**/*.ts\" \"./sample/**/*.ts\"", "format": "prettier --write \"./src/**/*.ts\" \"./test/**/*.ts\" \"./sample/**/*.ts\"", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 2" }, "bin": { "typeorm": "./cli.js", "typeorm-ts-node-commonjs": "./cli-ts-node-commonjs.js", "typeorm-ts-node-esm": "./cli-ts-node-esm.js" }, "funding": "https://opencollective.com/typeorm", "collective": { "type": "opencollective", "url": "https://opencollective.com/typeorm", "logo": "https://opencollective.com/opencollective/logo.txt" }, "nyc": { "all": true, "cache": false, "exclude": [ "**/*.d.ts" ], "extension": [ ".ts" ], "include": [ "build/compiled/src/**", "src/**" ], "reporter": "json" } }
1
+ { "name": "typeorm", "private": false, "version": "0.3.7-dev.ec05f1d", "description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.", "license": "MIT", "readmeFilename": "README.md", "author": { "name": "Umed Khudoiberdiev", "email": "pleerock.me@gmail.com" }, "engines": { "node": ">= 12.9.0" }, "exports": { ".": { "types": "./index.d.ts", "node": { "import": "./index.mjs", "require": "./index.js", "types": "./index.d.ts" }, "browser": { "require": "./index.js", "import": "./browser/index.js", "default": "./index.js" } }, "./browser": { "types": "./index.d.ts", "default": "./browser/index.js" }, "./*.js": "./*.js", "./*": { "require": "./*.js", "import": "./*" } }, "main": "./index.js", "module": "./index.mjs", "types": "./index.d.ts", "browser": { "./browser/connection/ConnectionOptionsReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsXmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsYmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/driver/aurora-data-api/AuroraDataApiDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/better-sqlite3/BetterSqlite3Driver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/cockroachdb/CockroachDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoQueryRunner.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mysql/MysqlDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/oracle/OracleDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/postgres/PostgresDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sap/SapDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlite/SqliteDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlserver/SqlServerDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/entity-manager/MongoEntityManager.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/logger/FileLogger.js": "./browser/platform/BrowserFileLoggerDummy.js", "./browser/platform/PlatformTools.js": "./browser/platform/BrowserPlatformTools.js", "./browser/repository/MongoRepository.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/util/DirectoryExportedClassesLoader.js": "./browser/platform/BrowserDirectoryExportedClassesLoader.js", "./index.js": "./browser/index.js", "./index.mjs": "./browser/index.js" }, "repository": { "type": "git", "url": "https://github.com/typeorm/typeorm.git" }, "bugs": { "url": "https://github.com/typeorm/typeorm/issues" }, "homepage": "https://typeorm.io", "tags": [ "orm", "typescript", "typescript-orm", "mysql", "mysql-orm", "postgresql", "postgresql-orm", "mariadb", "mariadb-orm", "spanner", "sqlite", "sqlite-orm", "sql-server", "sql-server-orm", "oracle", "oracle-orm", "cloud-spanner", "cloud-spanner-orm" ], "devDependencies": { "@types/app-root-path": "^1.2.4", "@types/chai": "^4.2.15", "@types/chai-as-promised": "^7.1.3", "@types/debug": "^4.1.5", "@types/js-yaml": "^4.0.0", "@types/mkdirp": "^1.0.1", "@types/mocha": "^8.2.1", "@types/node": "^14.14.31", "@types/rimraf": "^3.0.0", "@types/sha.js": "^2.4.0", "@types/sinon": "^9.0.10", "@types/source-map-support": "^0.5.3", "@types/uuid": "^8.3.4", "@types/xml2js": "^0.4.8", "@types/yargs": "^17.0.2", "better-sqlite3": "^7.1.2", "chai": "^4.3.0", "chai-as-promised": "^7.1.1", "class-transformer": "^0.4.0", "conventional-changelog-angular": "^5.0.12", "conventional-changelog-cli": "^2.1.1", "del": "^6.0.0", "gulp": "^4.0.2", "gulp-istanbul": "^1.1.3", "gulp-mocha": "^8.0.0", "gulp-rename": "^2.0.0", "gulp-replace": "^1.0.0", "gulp-shell": "^0.8.0", "gulp-sourcemaps": "^3.0.0", "gulp-typescript": "^6.0.0-alpha.1", "gulpclass": "^0.2.0", "husky": "^5.1.1", "mocha": "^8.3.0", "mongodb": "^3.6.4", "mssql": "^7.3.0", "mysql": "^2.18.1", "mysql2": "^2.2.5", "pg": "^8.5.1", "pg-query-stream": "^4.0.0", "prettier": "^2.5.1", "redis": "^3.1.1", "remap-istanbul": "^0.13.0", "rimraf": "^3.0.2", "sinon": "^9.2.4", "sinon-chai": "^3.5.0", "source-map-support": "^0.5.19", "sql.js": "^1.4.0", "sqlite3": "^5.0.3", "ts-node": "^10.7.0", "typeorm-aurora-data-api-driver": "^2.0.0", "typescript": "^4.6.2" }, "peerDependencies": { "@google-cloud/spanner": "^5.18.0", "@sap/hana-client": "^2.11.14", "better-sqlite3": "^7.1.2", "hdb-pool": "^0.1.6", "ioredis": "^5.0.4", "mongodb": "^3.6.0", "mssql": "^7.3.0", "mysql2": "^2.2.5", "oracledb": "^5.1.0", "pg": "^8.5.1", "pg-native": "^3.0.0", "pg-query-stream": "^4.0.0", "redis": "^3.1.1 || ^4.0.0", "sql.js": "^1.4.0", "sqlite3": "^5.0.3", "ts-node": "^10.7.0", "typeorm-aurora-data-api-driver": "^2.0.0" }, "peerDependenciesMeta": { "@google-cloud/spanner": { "optional": true }, "@sap/hana-client": { "optional": true }, "better-sqlite3": { "optional": true }, "hdb-pool": { "optional": true }, "ioredis": { "optional": true }, "mongodb": { "optional": true }, "mssql": { "optional": true }, "mysql2": { "optional": true }, "oracledb": { "optional": true }, "pg": { "optional": true }, "pg-native": { "optional": true }, "pg-query-stream": { "optional": true }, "redis": { "optional": true }, "sql.js": { "optional": true }, "sqlite3": { "optional": true }, "ts-node": { "optional": true }, "typeorm-aurora-data-api-driver": { "optional": true } }, "dependencies": { "@sqltools/formatter": "^1.2.2", "app-root-path": "^3.0.0", "buffer": "^6.0.3", "chalk": "^4.1.0", "cli-highlight": "^2.1.11", "date-fns": "^2.28.0", "debug": "^4.3.3", "dotenv": "^16.0.0", "glob": "^7.2.0", "js-yaml": "^4.1.0", "mkdirp": "^1.0.4", "reflect-metadata": "^0.1.13", "sha.js": "^2.4.11", "tslib": "^2.3.1", "uuid": "^8.3.2", "xml2js": "^0.4.23", "yargs": "^17.3.1" }, "scripts": { "test": "rimraf ./build && tsc && mocha --file ./build/compiled/test/utils/test-setup.js --bail --recursive --timeout 60000 ./build/compiled/test", "test-fast": "mocha --file ./build/compiled/test/utils/test-setup.js --bail --recursive --timeout 60000 ./build/compiled/test", "compile": "rimraf ./build && tsc", "watch": "./node_modules/.bin/tsc -w", "package": "gulp package", "pack": "gulp pack", "lint": "prettier --check \"./src/**/*.ts\" \"./test/**/*.ts\" \"./sample/**/*.ts\"", "format": "prettier --write \"./src/**/*.ts\" \"./test/**/*.ts\" \"./sample/**/*.ts\"", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 2" }, "bin": { "typeorm": "./cli.js", "typeorm-ts-node-commonjs": "./cli-ts-node-commonjs.js", "typeorm-ts-node-esm": "./cli-ts-node-esm.js" }, "funding": "https://opencollective.com/typeorm", "collective": { "type": "opencollective", "url": "https://opencollective.com/typeorm", "logo": "https://opencollective.com/opencollective/logo.txt" }, "nyc": { "all": true, "cache": false, "exclude": [ "**/*.d.ts" ], "extension": [ ".ts" ], "include": [ "build/compiled/src/**", "src/**" ], "reporter": "json" } }