@ainias42/typeorm-helper 0.0.4 → 0.0.5

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.
Files changed (37) hide show
  1. package/dist/BaseModel.d.ts +1 -1
  2. package/dist/BaseModel.js +14 -7
  3. package/dist/BaseModel.js.map +1 -1
  4. package/dist/DbNamingStrategy.js +6 -2
  5. package/dist/DbNamingStrategy.js.map +1 -1
  6. package/dist/{ServerSubscriber.d.ts → DefaultSubscriber.d.ts} +1 -1
  7. package/dist/{ServerSubscriber.js → DefaultSubscriber.js} +18 -13
  8. package/dist/DefaultSubscriber.js.map +1 -0
  9. package/dist/dataSource/dataSource.js +8 -4
  10. package/dist/dataSource/dataSource.js.map +1 -1
  11. package/dist/dataSource/getRepository.js +6 -3
  12. package/dist/dataSource/getRepository.js.map +1 -1
  13. package/dist/decorators/FileColumn/FileColumn.js +6 -3
  14. package/dist/decorators/FileColumn/FileColumn.js.map +1 -1
  15. package/dist/decorators/FileColumn/FileTransformer.js +2 -1
  16. package/dist/decorators/FileColumn/FileType.js +2 -1
  17. package/dist/decorators/FileColumn/FileWriter.js +17 -11
  18. package/dist/decorators/FileColumn/FileWriter.js.map +1 -1
  19. package/dist/index.d.ts +2 -1
  20. package/dist/index.js +27 -10
  21. package/dist/index.js.map +1 -1
  22. package/dist/migration/getCreateTableColumns.d.ts +5 -2
  23. package/dist/migration/getCreateTableColumns.js +9 -3
  24. package/dist/migration/getCreateTableColumns.js.map +1 -1
  25. package/dist/migration/getCreateTableColumnsV1.d.ts +24 -0
  26. package/dist/migration/getCreateTableColumnsV1.js +31 -0
  27. package/dist/migration/getCreateTableColumnsV1.js.map +1 -0
  28. package/dist/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +1 -2
  30. package/src/BaseModel.ts +2 -8
  31. package/src/{ServerSubscriber.ts → DefaultSubscriber.ts} +12 -7
  32. package/src/decorators/FileColumn/FileWriter.ts +4 -4
  33. package/src/index.ts +2 -1
  34. package/src/migration/getCreateTableColumns.ts +5 -2
  35. package/src/migration/getCreateTableColumnsV1.ts +27 -0
  36. package/tsconfig.json +2 -1
  37. package/dist/ServerSubscriber.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ainias42/typeorm-helper",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,7 +17,6 @@
17
17
  "lint:fix": "npm run lint -- --fix",
18
18
  "typecheck": "tsc --noEmit"
19
19
  },
20
- "type": "module",
21
20
  "author": "",
22
21
  "repository": {
23
22
  "type": "git",
package/src/BaseModel.ts CHANGED
@@ -1,14 +1,8 @@
1
- import {
2
- CreateDateColumn,
3
- DeleteDateColumn,
4
- PrimaryGeneratedColumn,
5
- UpdateDateColumn,
6
- VersionColumn
7
- } from "typeorm";
1
+ import { CreateDateColumn, DeleteDateColumn, PrimaryGeneratedColumn, UpdateDateColumn, VersionColumn } from "typeorm";
8
2
 
9
3
  export class BaseModel {
10
4
  @PrimaryGeneratedColumn()
11
- id?: number;
5
+ id: number = -1;
12
6
 
13
7
  @CreateDateColumn()
14
8
  createdAt?: Date;
@@ -2,15 +2,18 @@
2
2
  import { ColumnMetadata } from "typeorm/metadata/ColumnMetadata";
3
3
  import {
4
4
  EntitySubscriberInterface,
5
- EventSubscriber, InsertEvent,
6
- ObjectLiteral, OptimisticLockVersionMismatchError, UpdateEvent
5
+ EventSubscriber,
6
+ InsertEvent,
7
+ ObjectLiteral,
8
+ OptimisticLockVersionMismatchError,
9
+ UpdateEvent
7
10
  } from "typeorm";
8
11
  import { FileTransformer } from "./decorators/FileColumn/FileTransformer";
9
12
  import { FileType } from "./decorators/FileColumn/FileType";
10
13
  import { FileWriter } from "./decorators/FileColumn/FileWriter";
11
14
 
12
15
  @EventSubscriber()
13
- export class ServerSubscriber implements EntitySubscriberInterface {
16
+ export class DefaultSubscriber implements EntitySubscriberInterface {
14
17
 
15
18
  async saveFiles(entity: ObjectLiteral, columns: ColumnMetadata[]) {
16
19
  const promises = [];
@@ -44,9 +47,11 @@ export class ServerSubscriber implements EntitySubscriberInterface {
44
47
  */
45
48
  async beforeInsert({entity, metadata: {columns}}: InsertEvent<any>) {
46
49
  if (entity) {
47
- // TODO check if this is necessary
48
- // Reflect.set(entity, "updatedAt", new Date());
49
- // Reflect.set(entity, "createdAt", new Date());
50
+ Reflect.set(entity, "updatedAt", new Date());
51
+ Reflect.set(entity, "createdAt", new Date());
52
+ if (Reflect.get(entity, "id") <= 0) {
53
+ Reflect.set(entity, "id", undefined);
54
+ }
50
55
 
51
56
  await this.saveFiles(entity, columns);
52
57
  }
@@ -90,6 +95,6 @@ export class ServerSubscriber implements EntitySubscriberInterface {
90
95
  }
91
96
 
92
97
  // beforeRemove({metadata, entity, ...other}: RemoveEvent<any>): Promise<any> | void {
93
- // TODO Remove files from server (?)
98
+ // TODO Remove files from server (?)
94
99
  // }
95
100
  }
@@ -1,6 +1,6 @@
1
1
  import { PassThrough, Readable } from 'stream';
2
- import { createHash, randomBytes } from 'crypto';
3
2
  import { createWriteStream, existsSync, mkdirSync } from 'fs';
3
+ import crypto from 'crypto';
4
4
 
5
5
 
6
6
  export const FileWriter = {
@@ -18,17 +18,17 @@ export const FileWriter = {
18
18
  const fileEnding = src.slice(indexSlash + 1, indexBase64SearchText);
19
19
  const data = src.slice(Math.max(0, indexBase64SearchText + base64SearchText.length));
20
20
 
21
- const seed = randomBytes(20);
21
+ const seed = crypto.randomBytes(20);
22
22
  const now = new Date();
23
23
 
24
24
  // Month is 0-based. Add 1 to get 1-12
25
- const name = `${now.getUTCFullYear()}-${now.getUTCMonth()+1}-${now.getUTCDate()}-${fileType}-${createHash('sha1').update(seed).digest('hex')}.${fileEnding}`;
25
+ const name = `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()}-${fileType}-${crypto.createHash('sha1').update(seed).digest('hex')}.${fileEnding}`;
26
26
 
27
27
  const dataBuffer = Buffer.from(data, 'base64');
28
28
  const inputStream = new Readable();
29
29
  const dataStream = new PassThrough();
30
30
 
31
- if(!existsSync(saveDirectory)) {
31
+ if (!existsSync(saveDirectory)) {
32
32
  mkdirSync(saveDirectory, {recursive: true});
33
33
  }
34
34
 
package/src/index.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  export * from './BaseModel';
2
2
  export * from './DbNamingStrategy';
3
- export * from './ServerSubscriber';
3
+ export * from './DefaultSubscriber';
4
4
  export * from './dataSource/dataSource';
5
5
  export * from './dataSource/getRepository';
6
6
  export * from './migration/getCreateTableColumns';
7
+ export * from './migration/getCreateTableColumnsV1';
7
8
  export * from './decorators/FileColumn/FileColumn';
8
9
  export * from './decorators/FileColumn/FileTransformer';
9
10
  export * from './decorators/FileColumn/FileType';
@@ -9,12 +9,15 @@ export function getCreateTableColumns() {
9
9
  isNullable: false,
10
10
  }, {
11
11
  name: 'createdAt',
12
- type: 'datetime',
12
+ type: 'datetime(6)',
13
13
  isNullable: false,
14
+ default: "CURRENT_TIMESTAMP(6)",
14
15
  }, {
15
16
  name: 'updatedAt',
16
- type: 'datetime',
17
+ type: 'datetime(6)',
17
18
  isNullable: false,
19
+ default: "CURRENT_TIMESTAMP(6)",
20
+ onUpdate: "CURRENT_TIMESTAMP(6)",
18
21
  }, {
19
22
  name: 'deletedAt',
20
23
  type: 'datetime(6)',
@@ -0,0 +1,27 @@
1
+ // Do not change the columns here. It is needed for migration, aka creation of the database
2
+ export function getCreateTableColumnsV1() {
3
+ return [{
4
+ name: 'id',
5
+ type: 'int',
6
+ isPrimary: true,
7
+ isGenerated: true,
8
+ generationStrategy: 'increment',
9
+ isNullable: false,
10
+ }, {
11
+ name: 'createdAt',
12
+ type: 'datetime',
13
+ isNullable: false,
14
+ }, {
15
+ name: 'updatedAt',
16
+ type: 'datetime',
17
+ isNullable: false,
18
+ }, {
19
+ name: 'deletedAt',
20
+ type: 'datetime(6)',
21
+ isNullable: true,
22
+ }, {
23
+ name: 'version',
24
+ type: 'int',
25
+ isNullable: false,
26
+ }] as const;
27
+ }
package/tsconfig.json CHANGED
@@ -2,6 +2,7 @@
2
2
  "extends": "@ainias42/typescript-config",
3
3
  "compilerOptions": {
4
4
  "jsx": "react",
5
+ "module": "commonjs",
5
6
  "outDir": "./dist",
6
7
  "declaration": true,
7
8
  "baseUrl": "./",
@@ -21,7 +22,7 @@
21
22
  ],
22
23
  "include": [
23
24
  "src/**/*.tsx",
24
- "src/**/*.ts",
25
+ "src/**/*.ts"
25
26
  ],
26
27
  "tsc-alias": {
27
28
  "resolveFullPaths": true,
@@ -1 +0,0 @@
1
- {"version":3,"file":"ServerSubscriber.js","sourceRoot":"","sources":["../src/ServerSubscriber.ts"],"names":[],"mappings":";;;;;;AAEA,OAAO,EAEH,eAAe,EACA,kCAAkC,EACpD,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAC;AAGzD,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAEzB,KAAK,CAAC,SAAS,CAAC,MAAqB,EAAE,OAAyB;QAC5D,MAAM,QAAQ,GAAG,EAAE,CAAC;QACpB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC3B,MAAM,WAAW,GAAG,MAAM,CAAC,WAA0C,CAAC;YACtE,IAAI,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,MAAM,EAAE,CAAC;gBACtB,IAAI,MAAM,GAAsC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC;gBACzF,IAAI,MAAM,EAAE,CAAC;oBACT,IAAI,MAAM,GAAG,KAAK,CAAC;oBACnB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;wBACzB,MAAM,GAAG,CAAC,MAAM,CAAC,CAAC;wBAClB,MAAM,GAAG,IAAI,CAAC;oBAClB,CAAC;oBACD,QAAQ,CAAC,IAAI,CAAC,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