stabilize-orm 1.3.8 → 2.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 (75) hide show
  1. package/README.md +1097 -546
  2. package/dist/auto-migrate.d.ts +34 -0
  3. package/dist/auto-migrate.d.ts.map +1 -0
  4. package/dist/auto-migrate.js +3003 -0
  5. package/dist/auto-migrate.js.map +163 -0
  6. package/dist/cache.d.ts +90 -0
  7. package/dist/cache.d.ts.map +1 -0
  8. package/dist/cache.js +166 -0
  9. package/dist/cache.js.map +64 -0
  10. package/dist/client.d.ts +73 -0
  11. package/dist/client.d.ts.map +1 -0
  12. package/dist/client.js +2997 -0
  13. package/dist/client.js.map +162 -0
  14. package/dist/hooks.d.ts +31 -0
  15. package/dist/hooks.d.ts.map +1 -0
  16. package/dist/hooks.js +4 -0
  17. package/dist/hooks.js.map +11 -0
  18. package/dist/index.d.ts +101 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +3183 -0
  21. package/dist/index.js.map +222 -0
  22. package/dist/logger.d.ts +40 -0
  23. package/dist/logger.d.ts.map +1 -0
  24. package/dist/logger.js +8 -0
  25. package/dist/logger.js.map +11 -0
  26. package/dist/migrations.d.ts +31 -0
  27. package/dist/migrations.d.ts.map +1 -0
  28. package/dist/migrations.js +3009 -0
  29. package/dist/migrations.js.map +164 -0
  30. package/{model.ts → dist/model.d.ts} +124 -189
  31. package/dist/model.d.ts.map +1 -0
  32. package/dist/model.js +4 -0
  33. package/dist/model.js.map +10 -0
  34. package/dist/query-builder.d.ts +91 -0
  35. package/dist/query-builder.d.ts.map +1 -0
  36. package/dist/query-builder.js +14 -0
  37. package/dist/query-builder.js.map +12 -0
  38. package/dist/repository.d.ts +165 -0
  39. package/dist/repository.d.ts.map +1 -0
  40. package/dist/repository.js +176 -0
  41. package/dist/repository.js.map +69 -0
  42. package/dist/tsconfig.tsbuildinfo +1 -0
  43. package/dist/types.d.ts +110 -0
  44. package/dist/types.d.ts.map +1 -0
  45. package/dist/types.js +4 -0
  46. package/dist/types.js.map +10 -0
  47. package/dist/utils/encryption.d.ts +13 -0
  48. package/dist/utils/encryption.d.ts.map +1 -0
  49. package/dist/utils/encryption.js +4 -0
  50. package/dist/utils/encryption.js.map +10 -0
  51. package/package.json +104 -25
  52. package/.eslintrc.json +0 -10
  53. package/.github/ISSUE_TEMPLATE/PULL_REQUEST_TEMPLATE.md +0 -23
  54. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -25
  55. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
  56. package/.github/workflows/ci-cd.yml +0 -22
  57. package/CHANGELOG.md +0 -75
  58. package/CODE_OF_CONDUCT.md +0 -87
  59. package/CONTRIBUTING.md +0 -48
  60. package/FUNDING.md +0 -14
  61. package/SECURITY.md +0 -35
  62. package/SUPPORT.md +0 -18
  63. package/bun.lock +0 -667
  64. package/cache.ts +0 -181
  65. package/client.ts +0 -249
  66. package/docker-compose.yml +0 -22
  67. package/hooks.ts +0 -76
  68. package/index.ts +0 -158
  69. package/logger.ts +0 -127
  70. package/migrations.ts +0 -318
  71. package/query-builder.ts +0 -209
  72. package/repository.ts +0 -1096
  73. package/tests/migrations.test.ts +0 -141
  74. package/tsconfig.json +0 -32
  75. package/types.ts +0 -106
@@ -1,141 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import { generateMigration, runMigrations } from '../migrations';
3
- import { DBType } from '../types';
4
- // We must mock the imports that provide the metadata keys and the client implementation
5
- import { ModelKey, ColumnKey, ValidatorKey, SoftDeleteKey } from '../decorators';
6
- import { DBClient } from '../client';
7
-
8
- // --- MOCKING SETUP ---
9
-
10
- // Mocking Reflect.getMetadata behavior to simulate data set by the @Model, @Column, etc. decorators
11
- const mockMetadata = new Map();
12
-
13
- // Helper to set mock data for tests
14
- const setMockMetadata = (tableName: string, columns: any, validators: any, softDeleteField?: string) => {
15
- mockMetadata.set(ModelKey, tableName);
16
- mockMetadata.set(ColumnKey, columns);
17
- mockMetadata.set(ValidatorKey, validators);
18
- if (softDeleteField) {
19
- mockMetadata.set(SoftDeleteKey, softDeleteField);
20
- } else {
21
- mockMetadata.delete(SoftDeleteKey);
22
- }
23
- };
24
-
25
- // Spy on the global Reflect.getMetadata used by the functions to return our mock data
26
- const getMetadataSpy = vi.spyOn(Reflect, 'getMetadata');
27
- getMetadataSpy.mockImplementation((key, target) => mockMetadata.get(key));
28
-
29
-
30
- // Mock Model Placeholder
31
- class MockModel {}
32
-
33
- // Mock DBClient for runMigrations test: this prevents hitting a real database
34
- vi.mock('./client', () => {
35
- const mockQuery = vi.fn(async (query: string) => {
36
- // Simulate no existing migration found when selecting from 'migrations'
37
- if (query.includes('SELECT id FROM migrations')) {
38
- return [];
39
- }
40
- return [];
41
- });
42
- return {
43
- DBClient: vi.fn(() => ({
44
- query: mockQuery,
45
- close: vi.fn(async () => {}),
46
- // Provide a mock config for runMigrations to safely access DBType
47
- config: {
48
- type: DBType.SQLite
49
- }
50
- }))
51
- };
52
- });
53
-
54
- // --- TESTS START HERE ---
55
-
56
- describe('generateMigration', () => {
57
- const commonColumns = {
58
- id: { name: 'id', type: 'INTEGER' },
59
- username: { name: 'user_name', type: 'TEXT' },
60
- createdAt: { name: 'created_at', type: 'TEXT' }
61
- };
62
- const commonValidators = {
63
- username: ['required', 'unique']
64
- };
65
-
66
- it('should generate SQLite-specific primary key (AUTOINCREMENT)', async () => {
67
- setMockMetadata('users', commonColumns, commonValidators);
68
-
69
- const migration = await generateMigration(MockModel, DBType.SQLite);
70
-
71
- expect(migration.up[0]).toBe(
72
- 'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT NOT NULL UNIQUE, created_at TEXT)'
73
- );
74
- });
75
-
76
- it('should generate PostgreSQL-specific primary key (SERIAL)', async () => {
77
- setMockMetadata('products', commonColumns, commonValidators);
78
-
79
- const migration = await generateMigration(MockModel, DBType.Postgres);
80
-
81
- expect(migration.up[0]).toBe(
82
- 'CREATE TABLE IF NOT EXISTS products (id SERIAL PRIMARY KEY, user_name TEXT NOT NULL UNIQUE, created_at TEXT)'
83
- );
84
- });
85
-
86
- it('should generate MySQL-specific primary key (AUTO_INCREMENT)', async () => {
87
- setMockMetadata('posts', commonColumns, commonValidators);
88
-
89
- const migration = await generateMigration(MockModel, DBType.MySQL);
90
-
91
- expect(migration.up[0]).toBe(
92
- 'CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY AUTO_INCREMENT, user_name TEXT NOT NULL UNIQUE, created_at TEXT)'
93
- );
94
- });
95
-
96
- it('should include soft delete field if present on the model', async () => {
97
- const softDeleteColumns = {
98
- ...commonColumns,
99
- deletedAt: { name: 'deleted_at', type: 'TEXT' }
100
- };
101
- setMockMetadata('orders', softDeleteColumns, commonValidators, 'deletedAt');
102
-
103
- const migration = await generateMigration(MockModel, DBType.SQLite);
104
-
105
- expect(migration.up[0]).toContain(', deleted_at TEXT)');
106
- });
107
-
108
- it('should throw an error if the model is missing the @Model decorator', async () => {
109
- mockMetadata.delete(ModelKey);
110
- await expect(generateMigration(MockModel, DBType.SQLite)).rejects.toThrow('Model not decorated with @Model');
111
- });
112
- });
113
-
114
- describe('runMigrations', () => {
115
- it('should create the migrations table and run the UP script for new migrations', async () => {
116
- const mockMigrations = [
117
- { up: ['CREATE TABLE test_table (id INT)'], down: ['DROP TABLE test_table'] }
118
- ];
119
-
120
- // Run migrations using the mocked DBClient (defaulted to SQLite type)
121
- await runMigrations({ type: DBType.SQLite,connectionString:"" }, mockMigrations);
122
-
123
- // Access the mock instance
124
- const mockClient = (DBClient as any).mock.results[0].value;
125
-
126
- // 1. Verify CREATE TABLE IF NOT EXISTS migrations was called
127
- expect(mockClient.query).toHaveBeenCalledWith(expect.stringContaining('CREATE TABLE IF NOT EXISTS migrations ('));
128
-
129
- // 2. Verify the actual UP query was executed
130
- expect(mockClient.query).toHaveBeenCalledWith('CREATE TABLE test_table (id INT)', []);
131
-
132
- // 3. Verify the migration log record was inserted
133
- expect(mockClient.query).toHaveBeenCalledWith(
134
- expect.stringContaining('INSERT INTO migrations (name, applied_at) VALUES (?, ?)'),
135
- expect.arrayContaining([expect.stringContaining('migration_0_'), expect.any(String)])
136
- );
137
-
138
- // 4. Verify the client was closed in the finally block
139
- expect(mockClient.close).toHaveBeenCalled();
140
- });
141
- });
package/tsconfig.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Environment setup & latest features
4
- "lib": ["ESNext"],
5
- "target": "ESNext",
6
- "module": "Preserve",
7
- "moduleDetection": "force",
8
- "jsx": "react-jsx",
9
- "allowJs": true,
10
-
11
- // Bundler mode
12
- "moduleResolution": "bundler",
13
- "allowImportingTsExtensions": true,
14
- "verbatimModuleSyntax": true,
15
- "noEmit": true,
16
-
17
- // Best practices
18
- "strict": true,
19
- "skipLibCheck": true,
20
- "noFallthroughCasesInSwitch": true,
21
- "noUncheckedIndexedAccess": true,
22
- "noImplicitOverride": true,
23
-
24
- // Some stricter flags (disabled by default)
25
- "noUnusedLocals": false,
26
- "noUnusedParameters": false,
27
- "noPropertyAccessFromIndexSignature": false,
28
-
29
- "experimentalDecorators": true,
30
- "emitDecoratorMetadata": true
31
- }
32
- }
package/types.ts DELETED
@@ -1,106 +0,0 @@
1
- /**
2
- * @file types.ts
3
- * @description Contains all shared type definitions and enums for the Stabilize ORM.
4
- * @author ElectronSz
5
- */
6
-
7
- export enum DBType {
8
- Postgres = "postgres",
9
- MySQL = "mysql",
10
- SQLite = "sqlite",
11
- }
12
-
13
- export enum LogLevel {
14
- Debug,
15
- Info,
16
- Warn,
17
- Error,
18
- }
19
-
20
- export enum RelationType {
21
- OneToOne,
22
- OneToMany,
23
- ManyToOne,
24
- ManyToMany,
25
- }
26
-
27
- /**
28
- * An enumeration of abstract data types that are mapped to database-specific types.
29
- * This allows models to be defined in a database-agnostic way.
30
- */
31
- export enum DataTypes {
32
- STRING, // Maps to VARCHAR or TEXT
33
- TEXT, // Maps to TEXT
34
- INTEGER, // Maps to INTEGER or INT
35
- BIGINT, // Maps to BIGINT
36
- FLOAT, // Maps to REAL or FLOAT
37
- DOUBLE, // Maps to DOUBLE PRECISION
38
- DECIMAL, // Maps to DECIMAL or NUMERIC
39
- BOOLEAN, // Maps to BOOLEAN or TINYINT/INTEGER
40
- DATE, // Maps to DATE or TEXT
41
- DATETIME, // Maps to TIMESTAMP, DATETIME, or TEXT
42
- JSON, // Maps to JSON, JSONB, or TEXT
43
- UUID, // Maps to UUID or VARCHAR(36)
44
- BLOB, // Maps to BYTEA or BLOB
45
-
46
- }
47
-
48
- export interface DBConfig {
49
- type: DBType;
50
- connectionString: string;
51
- retryAttempts?: number;
52
- retryDelay?: number;
53
- maxJitter?: number;
54
- }
55
-
56
- export interface CacheConfig {
57
- enabled: boolean;
58
- ttl: number;
59
- redisUrl?: string;
60
- cachePrefix?: string;
61
- strategy?: "cache-aside" | "write-through";
62
- }
63
-
64
- /**
65
- * Configuration for the logger.
66
- */
67
- export interface LoggerConfig {
68
- level?: LogLevel;
69
- filePath?: string;
70
- maxFileSize?: number;
71
- maxFiles?: number;
72
- }
73
-
74
- export interface PoolMetrics {
75
- activeConnections: number;
76
- idleConnections: number;
77
- totalConnections: number;
78
- }
79
-
80
- export interface QueryHint {
81
- type: string;
82
- value: string;
83
- }
84
-
85
- export interface CacheStats {
86
- hits: number;
87
- misses: number;
88
- keys: number;
89
- }
90
-
91
- export interface Migration {
92
- name: string;
93
- up: string[];
94
- down: string[];
95
- }
96
-
97
- export class StabilizeError extends Error {
98
- constructor(
99
- message: string,
100
- public code: string,
101
- public originalError?: Error,
102
- ) {
103
- super(message);
104
- this.name = "StabilizeError";
105
- }
106
- }