@gustavo-valsechi/node 1.0.14 → 1.0.16

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.
package/dist/index.d.mts CHANGED
@@ -2,6 +2,7 @@ export { databaseInit } from './src/database/index.mjs';
2
2
  export { apiRouter, mainRouter } from './src/router/index.mjs';
3
3
  export { app, serverInit } from './src/server/index.mjs';
4
4
  export { OPERATORS, converter, existsOperator, moduler, paginate, refactorWhere, responseWrapper } from './src/tools/index.mjs';
5
+ export { IMiddlewareContract, TenantService } from './src/services/index.mjs';
5
6
  import 'typeorm';
6
7
  import 'fastify';
7
8
  import 'http';
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export { databaseInit } from './src/database/index.js';
2
2
  export { apiRouter, mainRouter } from './src/router/index.js';
3
3
  export { app, serverInit } from './src/server/index.js';
4
4
  export { OPERATORS, converter, existsOperator, moduler, paginate, refactorWhere, responseWrapper } from './src/tools/index.js';
5
+ export { IMiddlewareContract, TenantService } from './src/services/index.js';
5
6
  import 'typeorm';
6
7
  import 'fastify';
7
8
  import 'http';
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  OPERATORS: () => OPERATORS,
34
+ TenantService: () => TenantService,
34
35
  apiRouter: () => apiRouter,
35
36
  app: () => app,
36
37
  converter: () => converter,
@@ -283,9 +284,51 @@ var mainRouter = (router, options, done) => {
283
284
  router.register(healthRouter, { prefix: "health" });
284
285
  done();
285
286
  };
287
+
288
+ // src/services/tenant.ts
289
+ var TenantService = class {
290
+ constructor(db, migrations, entity, idTenant) {
291
+ this.db = db;
292
+ this.migrations = migrations;
293
+ this.entity = entity;
294
+ this.idTenant = idTenant;
295
+ }
296
+ async getTenantQueryRunner() {
297
+ var _a;
298
+ if (this.runner) return this.runner;
299
+ const runner = this.db.createQueryRunner();
300
+ await runner.connect();
301
+ const result = await runner.query(`
302
+ SELECT EXISTS (
303
+ SELECT 1
304
+ FROM information_schema.schemata
305
+ WHERE schema_name = '${this.idTenant}'
306
+ ) AS "exists"
307
+ `);
308
+ await runner.query(`CREATE SCHEMA IF NOT EXISTS "${this.idTenant}"`);
309
+ await runner.query(`SET search_path TO "${this.idTenant}"`);
310
+ if (!((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.exists)) await runner.query(this.migrations);
311
+ this.runner = runner;
312
+ return runner;
313
+ }
314
+ async getRepository() {
315
+ var _a;
316
+ await this.getTenantQueryRunner();
317
+ const repository = (_a = this.runner) == null ? void 0 : _a.manager.getRepository(this.entity);
318
+ this.repository = repository;
319
+ return repository;
320
+ }
321
+ async closeConnection() {
322
+ var _a, _b;
323
+ await this.getTenantQueryRunner();
324
+ await ((_a = this.runner) == null ? void 0 : _a.query("SET search_path TO public"));
325
+ await ((_b = this.runner) == null ? void 0 : _b.release());
326
+ }
327
+ };
286
328
  // Annotate the CommonJS export names for ESM import in node:
287
329
  0 && (module.exports = {
288
330
  OPERATORS,
331
+ TenantService,
289
332
  apiRouter,
290
333
  app,
291
334
  converter,
package/dist/index.mjs CHANGED
@@ -254,8 +254,50 @@ var mainRouter = (router, options, done) => {
254
254
  router.register(healthRouter, { prefix: "health" });
255
255
  done();
256
256
  };
257
+
258
+ // src/services/tenant.ts
259
+ var TenantService = class {
260
+ constructor(db, migrations, entity, idTenant) {
261
+ this.db = db;
262
+ this.migrations = migrations;
263
+ this.entity = entity;
264
+ this.idTenant = idTenant;
265
+ }
266
+ async getTenantQueryRunner() {
267
+ var _a;
268
+ if (this.runner) return this.runner;
269
+ const runner = this.db.createQueryRunner();
270
+ await runner.connect();
271
+ const result = await runner.query(`
272
+ SELECT EXISTS (
273
+ SELECT 1
274
+ FROM information_schema.schemata
275
+ WHERE schema_name = '${this.idTenant}'
276
+ ) AS "exists"
277
+ `);
278
+ await runner.query(`CREATE SCHEMA IF NOT EXISTS "${this.idTenant}"`);
279
+ await runner.query(`SET search_path TO "${this.idTenant}"`);
280
+ if (!((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.exists)) await runner.query(this.migrations);
281
+ this.runner = runner;
282
+ return runner;
283
+ }
284
+ async getRepository() {
285
+ var _a;
286
+ await this.getTenantQueryRunner();
287
+ const repository = (_a = this.runner) == null ? void 0 : _a.manager.getRepository(this.entity);
288
+ this.repository = repository;
289
+ return repository;
290
+ }
291
+ async closeConnection() {
292
+ var _a, _b;
293
+ await this.getTenantQueryRunner();
294
+ await ((_a = this.runner) == null ? void 0 : _a.query("SET search_path TO public"));
295
+ await ((_b = this.runner) == null ? void 0 : _b.release());
296
+ }
297
+ };
257
298
  export {
258
299
  OPERATORS,
300
+ TenantService,
259
301
  apiRouter,
260
302
  app,
261
303
  converter,
@@ -0,0 +1,21 @@
1
+ import { QueryRunner, Repository, ObjectLiteral, DataSource } from 'typeorm';
2
+ import { FastifyRequest, FastifyReply } from 'fastify';
3
+
4
+ declare class TenantService {
5
+ private readonly db;
6
+ private readonly migrations;
7
+ private readonly entity;
8
+ private readonly idTenant;
9
+ runner: QueryRunner | undefined;
10
+ repository: Repository<ObjectLiteral> | undefined;
11
+ constructor(db: DataSource, migrations: string, entity: any, idTenant: string);
12
+ getTenantQueryRunner(): Promise<QueryRunner>;
13
+ getRepository(): Promise<Repository<ObjectLiteral> | undefined>;
14
+ closeConnection(): Promise<void>;
15
+ }
16
+
17
+ interface IMiddlewareContract {
18
+ execute(request: FastifyRequest, response: FastifyReply): Promise<void>;
19
+ }
20
+
21
+ export { type IMiddlewareContract, TenantService };
@@ -0,0 +1,21 @@
1
+ import { QueryRunner, Repository, ObjectLiteral, DataSource } from 'typeorm';
2
+ import { FastifyRequest, FastifyReply } from 'fastify';
3
+
4
+ declare class TenantService {
5
+ private readonly db;
6
+ private readonly migrations;
7
+ private readonly entity;
8
+ private readonly idTenant;
9
+ runner: QueryRunner | undefined;
10
+ repository: Repository<ObjectLiteral> | undefined;
11
+ constructor(db: DataSource, migrations: string, entity: any, idTenant: string);
12
+ getTenantQueryRunner(): Promise<QueryRunner>;
13
+ getRepository(): Promise<Repository<ObjectLiteral> | undefined>;
14
+ closeConnection(): Promise<void>;
15
+ }
16
+
17
+ interface IMiddlewareContract {
18
+ execute(request: FastifyRequest, response: FastifyReply): Promise<void>;
19
+ }
20
+
21
+ export { type IMiddlewareContract, TenantService };
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/services/index.ts
21
+ var services_exports = {};
22
+ __export(services_exports, {
23
+ TenantService: () => TenantService
24
+ });
25
+ module.exports = __toCommonJS(services_exports);
26
+
27
+ // src/services/tenant.ts
28
+ var TenantService = class {
29
+ constructor(db, migrations, entity, idTenant) {
30
+ this.db = db;
31
+ this.migrations = migrations;
32
+ this.entity = entity;
33
+ this.idTenant = idTenant;
34
+ }
35
+ async getTenantQueryRunner() {
36
+ var _a;
37
+ if (this.runner) return this.runner;
38
+ const runner = this.db.createQueryRunner();
39
+ await runner.connect();
40
+ const result = await runner.query(`
41
+ SELECT EXISTS (
42
+ SELECT 1
43
+ FROM information_schema.schemata
44
+ WHERE schema_name = '${this.idTenant}'
45
+ ) AS "exists"
46
+ `);
47
+ await runner.query(`CREATE SCHEMA IF NOT EXISTS "${this.idTenant}"`);
48
+ await runner.query(`SET search_path TO "${this.idTenant}"`);
49
+ if (!((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.exists)) await runner.query(this.migrations);
50
+ this.runner = runner;
51
+ return runner;
52
+ }
53
+ async getRepository() {
54
+ var _a;
55
+ await this.getTenantQueryRunner();
56
+ const repository = (_a = this.runner) == null ? void 0 : _a.manager.getRepository(this.entity);
57
+ this.repository = repository;
58
+ return repository;
59
+ }
60
+ async closeConnection() {
61
+ var _a, _b;
62
+ await this.getTenantQueryRunner();
63
+ await ((_a = this.runner) == null ? void 0 : _a.query("SET search_path TO public"));
64
+ await ((_b = this.runner) == null ? void 0 : _b.release());
65
+ }
66
+ };
67
+ // Annotate the CommonJS export names for ESM import in node:
68
+ 0 && (module.exports = {
69
+ TenantService
70
+ });
@@ -0,0 +1,43 @@
1
+ // src/services/tenant.ts
2
+ var TenantService = class {
3
+ constructor(db, migrations, entity, idTenant) {
4
+ this.db = db;
5
+ this.migrations = migrations;
6
+ this.entity = entity;
7
+ this.idTenant = idTenant;
8
+ }
9
+ async getTenantQueryRunner() {
10
+ var _a;
11
+ if (this.runner) return this.runner;
12
+ const runner = this.db.createQueryRunner();
13
+ await runner.connect();
14
+ const result = await runner.query(`
15
+ SELECT EXISTS (
16
+ SELECT 1
17
+ FROM information_schema.schemata
18
+ WHERE schema_name = '${this.idTenant}'
19
+ ) AS "exists"
20
+ `);
21
+ await runner.query(`CREATE SCHEMA IF NOT EXISTS "${this.idTenant}"`);
22
+ await runner.query(`SET search_path TO "${this.idTenant}"`);
23
+ if (!((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.exists)) await runner.query(this.migrations);
24
+ this.runner = runner;
25
+ return runner;
26
+ }
27
+ async getRepository() {
28
+ var _a;
29
+ await this.getTenantQueryRunner();
30
+ const repository = (_a = this.runner) == null ? void 0 : _a.manager.getRepository(this.entity);
31
+ this.repository = repository;
32
+ return repository;
33
+ }
34
+ async closeConnection() {
35
+ var _a, _b;
36
+ await this.getTenantQueryRunner();
37
+ await ((_a = this.runner) == null ? void 0 : _a.query("SET search_path TO public"));
38
+ await ((_b = this.runner) == null ? void 0 : _b.release());
39
+ }
40
+ };
41
+ export {
42
+ TenantService
43
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gustavo-valsechi/node",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -71,6 +71,11 @@
71
71
  "types": "./dist/src/tools/index.d.ts",
72
72
  "require": "./dist/src/tools/index.js",
73
73
  "import": "./dist/src/tools/index.js"
74
+ },
75
+ "./services": {
76
+ "types": "./dist/src/services/index.d.ts",
77
+ "require": "./dist/src/services/index.js",
78
+ "import": "./dist/src/services/index.js"
74
79
  }
75
80
  }
76
81
  }