@cheetah.js/orm 0.1.0

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 (93) hide show
  1. package/README.md +228 -0
  2. package/build.ts +14 -0
  3. package/cheetah.config.ts +14 -0
  4. package/dist/SqlBuilder.d.ts +57 -0
  5. package/dist/SqlBuilder.js +436 -0
  6. package/dist/SqlBuilder.js.map +1 -0
  7. package/dist/bun/index.d.ts +13 -0
  8. package/dist/bun/index.js +224319 -0
  9. package/dist/bun/index.js.map +306 -0
  10. package/dist/cheetah.d.ts +1 -0
  11. package/dist/cheetah.js +24 -0
  12. package/dist/cheetah.js.map +1 -0
  13. package/dist/constants.d.ts +4 -0
  14. package/dist/constants.js +5 -0
  15. package/dist/constants.js.map +1 -0
  16. package/dist/decorators/entity.decorator.d.ts +3 -0
  17. package/dist/decorators/entity.decorator.js +10 -0
  18. package/dist/decorators/entity.decorator.js.map +1 -0
  19. package/dist/decorators/index.decorator.d.ts +3 -0
  20. package/dist/decorators/index.decorator.js +17 -0
  21. package/dist/decorators/index.decorator.js.map +1 -0
  22. package/dist/decorators/one-many.decorator.d.ts +3 -0
  23. package/dist/decorators/one-many.decorator.js +17 -0
  24. package/dist/decorators/one-many.decorator.js.map +1 -0
  25. package/dist/decorators/primary-key.decorator.d.ts +2 -0
  26. package/dist/decorators/primary-key.decorator.js +6 -0
  27. package/dist/decorators/primary-key.decorator.js.map +1 -0
  28. package/dist/decorators/property.decorator.d.ts +15 -0
  29. package/dist/decorators/property.decorator.js +25 -0
  30. package/dist/decorators/property.decorator.js.map +1 -0
  31. package/dist/domain/base-entity.d.ts +42 -0
  32. package/dist/domain/base-entity.js +106 -0
  33. package/dist/domain/base-entity.js.map +1 -0
  34. package/dist/domain/collection.d.ts +6 -0
  35. package/dist/domain/collection.js +11 -0
  36. package/dist/domain/collection.js.map +1 -0
  37. package/dist/domain/entities.d.ts +40 -0
  38. package/dist/domain/entities.js +137 -0
  39. package/dist/domain/entities.js.map +1 -0
  40. package/dist/domain/reference.d.ts +4 -0
  41. package/dist/domain/reference.js +7 -0
  42. package/dist/domain/reference.js.map +1 -0
  43. package/dist/driver/driver.interface.d.ts +270 -0
  44. package/dist/driver/driver.interface.js +2 -0
  45. package/dist/driver/driver.interface.js.map +1 -0
  46. package/dist/driver/pg-driver.d.ts +43 -0
  47. package/dist/driver/pg-driver.js +255 -0
  48. package/dist/driver/pg-driver.js.map +1 -0
  49. package/dist/index.d.ts +13 -0
  50. package/dist/index.js +16 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/migration/diff-calculator.d.ts +17 -0
  53. package/dist/migration/diff-calculator.js +230 -0
  54. package/dist/migration/diff-calculator.js.map +1 -0
  55. package/dist/migration/migrator.d.ts +18 -0
  56. package/dist/migration/migrator.js +233 -0
  57. package/dist/migration/migrator.js.map +1 -0
  58. package/dist/orm.d.ts +14 -0
  59. package/dist/orm.js +23 -0
  60. package/dist/orm.js.map +1 -0
  61. package/dist/orm.service.d.ts +8 -0
  62. package/dist/orm.service.js +115 -0
  63. package/dist/orm.service.js.map +1 -0
  64. package/dist/utils.d.ts +1 -0
  65. package/dist/utils.js +16 -0
  66. package/dist/utils.js.map +1 -0
  67. package/package.json +50 -0
  68. package/src/SqlBuilder.ts +542 -0
  69. package/src/cheetah.ts +28 -0
  70. package/src/constants.ts +4 -0
  71. package/src/decorators/entity.decorator.ts +10 -0
  72. package/src/decorators/index.decorator.ts +18 -0
  73. package/src/decorators/one-many.decorator.ts +19 -0
  74. package/src/decorators/primary-key.decorator.ts +6 -0
  75. package/src/decorators/property.decorator.ts +41 -0
  76. package/src/domain/base-entity.ts +149 -0
  77. package/src/domain/collection.ts +10 -0
  78. package/src/domain/entities.ts +159 -0
  79. package/src/domain/reference.ts +5 -0
  80. package/src/driver/driver.interface.ts +331 -0
  81. package/src/driver/pg-driver.ts +308 -0
  82. package/src/index.ts +17 -0
  83. package/src/migration/diff-calculator.ts +258 -0
  84. package/src/migration/migrator.ts +278 -0
  85. package/src/orm.service.ts +115 -0
  86. package/src/orm.ts +30 -0
  87. package/src/utils.ts +18 -0
  88. package/test/domain/base-entity.spec.ts +463 -0
  89. package/test/migration/.sql +5 -0
  90. package/test/migration/cheetah.config.ts +13 -0
  91. package/test/migration/migator.spec.ts +251 -0
  92. package/test/migration/test.sql +5 -0
  93. package/test/node-database.ts +32 -0
@@ -0,0 +1,115 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { Metadata, OnApplicationInit, Service } from '@cheetah.js/core';
11
+ import { EntityStorage } from './domain/entities';
12
+ import { ENTITIES, PROPERTIES_METADATA, PROPERTIES_RELATIONS } from './constants';
13
+ import { globbySync } from 'globby';
14
+ import { Project, SyntaxKind } from 'ts-morph';
15
+ let OrmService = class OrmService {
16
+ constructor(storage, entityFile = undefined) {
17
+ this.storage = storage;
18
+ this.allEntities = new Map();
19
+ console.log('Preparing entities...');
20
+ const files = new Project({ skipLoadingLibFiles: true }).addSourceFilesAtPaths(entityFile ?? this.getSourceFilePaths());
21
+ files.forEach(file => {
22
+ file.getClasses().forEach(classDeclaration => {
23
+ if (classDeclaration.getDecorator('Entity')) {
24
+ const properties = classDeclaration.getProperties();
25
+ const nullables = [];
26
+ const defaults = {};
27
+ properties.forEach(property => {
28
+ const propertyName = property.getName();
29
+ const isNullable = property.hasQuestionToken();
30
+ const initializer = property.getInitializer();
31
+ if (isNullable) {
32
+ nullables.push(propertyName);
33
+ }
34
+ if (initializer) {
35
+ const initializerKind = initializer.getKind();
36
+ switch (initializerKind) {
37
+ case SyntaxKind.StringLiteral:
38
+ defaults[propertyName] = initializer.getText();
39
+ break;
40
+ case SyntaxKind.NumericLiteral:
41
+ defaults[propertyName] = parseFloat(initializer.getText());
42
+ break;
43
+ default:
44
+ defaults[propertyName] = () => initializer.getText();
45
+ break;
46
+ }
47
+ }
48
+ this.allEntities.set(classDeclaration.getName(), { nullables, defaults });
49
+ });
50
+ }
51
+ });
52
+ });
53
+ }
54
+ async onInit() {
55
+ const configFile = globbySync('cheetah.config.ts', { absolute: true });
56
+ if (configFile.length === 0) {
57
+ console.log('No config file found!');
58
+ return;
59
+ }
60
+ const config = await import(configFile[0]);
61
+ if (typeof config.default.entities === 'string') {
62
+ const files = globbySync([config.default.entities, '!node_modules'], { gitignore: true, absolute: true });
63
+ for (const file of files) {
64
+ await import(file);
65
+ }
66
+ }
67
+ const entities = Metadata.get(ENTITIES, Reflect);
68
+ if (!entities) {
69
+ console.log('No entities found!');
70
+ return;
71
+ }
72
+ for (const entity of entities) {
73
+ const nullableDefaultEntity = this.allEntities.get(entity.target.name);
74
+ const properties = Metadata.get(PROPERTIES_METADATA, entity.target);
75
+ const relationship = Metadata.get(PROPERTIES_RELATIONS, entity.target);
76
+ for (const property in properties) {
77
+ if (nullableDefaultEntity?.nullables.includes(property)) {
78
+ properties[property].options.nullable = true;
79
+ }
80
+ if (nullableDefaultEntity?.defaults[property]) {
81
+ properties[property].options.default = nullableDefaultEntity?.defaults[property];
82
+ }
83
+ }
84
+ this.storage.add(entity, properties, relationship);
85
+ }
86
+ console.log('Entities prepared!');
87
+ }
88
+ getSourceFilePaths() {
89
+ const projectRoot = process.cwd(); // Ajuste conforme a estrutura do seu projeto
90
+ const getAllFiles = (dir) => {
91
+ const patterns = [`${dir}/**/*.(ts|js)`, "!**/node_modules/**"];
92
+ try {
93
+ return globbySync(patterns, { gitignore: true });
94
+ }
95
+ catch (error) {
96
+ console.error('Erro ao obter arquivos:', error);
97
+ return [];
98
+ }
99
+ };
100
+ // Filtra os arquivos pelo padrão de nomenclatura
101
+ return getAllFiles(projectRoot);
102
+ }
103
+ };
104
+ __decorate([
105
+ OnApplicationInit(),
106
+ __metadata("design:type", Function),
107
+ __metadata("design:paramtypes", []),
108
+ __metadata("design:returntype", Promise)
109
+ ], OrmService.prototype, "onInit", null);
110
+ OrmService = __decorate([
111
+ Service(),
112
+ __metadata("design:paramtypes", [EntityStorage, Object])
113
+ ], OrmService);
114
+ export { OrmService };
115
+ //# sourceMappingURL=orm.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orm.service.js","sourceRoot":"","sources":["../src/orm.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAY,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAKxC,IAAM,UAAU,GAAhB,MAAM,UAAU;IAGrB,YAAoB,OAAsB,EAAE,aAAiC,SAAS;QAAlE,YAAO,GAAP,OAAO,CAAe;QAFlC,gBAAW,GAAG,IAAI,GAAG,EAAqE,CAAC;QAGjG,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;QACpC,MAAM,KAAK,GAAG,IAAI,OAAO,CAAC,EAAC,mBAAmB,EAAE,IAAI,EAAC,CAAC,CAAC,qBAAqB,CAAC,UAAU,IAAI,IAAI,CAAC,kBAAkB,EAAE,CAAC,CAAA;QACrH,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC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(type === 'String') {
3
+ return 255;
4
+ }
5
+ if (type === 'Number') {
6
+ return 11;
7
+ }
8
+ if (type === 'Boolean') {
9
+ return 1;
10
+ }
11
+ if (type === 'Date') {
12
+ return 6;
13
+ }
14
+ return 255;
15
+ }
16
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,IAAI,KAAK,QAAQ,EAAE;QACrB,OAAO,GAAG,CAAC;KACZ;IAED,IAAI,IAAI,KAAK,QAAQ,EAAE;QACrB,OAAO,EAAE,CAAC;KACX;IAED,IAAI,IAAI,KAAK,SAAS,EAAE;QACtB,OAAO,CAAC,CAAC;KACV;IAED,IAAI,IAAI,KAAK,MAAM,EAAE;QACnB,OAAO,CAAC,CAAC;KACV;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@cheetah.js/orm",
3
+ "version": "0.1.0",
4
+ "description": "A simple ORM for Cheetah.js",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": "./dist/index.js"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "compile": "sudo rm -rf ./dist && tsc && bun run build.ts"
20
+ },
21
+ "dependencies": {
22
+ "uuid": "^9.0.1",
23
+ "reflect-metadata": "^0.1.13",
24
+ "ts-morph": "^20.0.0",
25
+ "commander": "^11.1.0"
26
+ },
27
+ "bin": {
28
+ "cheetah-orm": "./src/cheetah.ts"
29
+ },
30
+ "devDependencies": {
31
+ "@types/uuid": "^9.0.6",
32
+ "pg": "^8.11.3",
33
+ "@types/pg": "^8.10.9"
34
+ },
35
+ "peerDependencies": {
36
+ "@cheetah.js/core": "^0.1.0",
37
+ "pg": "^8.11.3"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "pg": {
41
+ "optional": true
42
+ }
43
+ },
44
+ "author": "",
45
+ "license": "MIT",
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "gitHead": "726879b8d699b9abf35e80aa64ada630c8ced14d"
50
+ }