nanodb-orm 0.0.1

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 (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +288 -0
  3. package/dist/constants/index.d.ts +59 -0
  4. package/dist/constants/index.d.ts.map +1 -0
  5. package/dist/constants/index.js +67 -0
  6. package/dist/constants/index.js.map +1 -0
  7. package/dist/core/config.d.ts +17 -0
  8. package/dist/core/config.d.ts.map +1 -0
  9. package/dist/core/config.js +40 -0
  10. package/dist/core/config.js.map +1 -0
  11. package/dist/core/connection.d.ts +29 -0
  12. package/dist/core/connection.d.ts.map +1 -0
  13. package/dist/core/connection.js +77 -0
  14. package/dist/core/connection.js.map +1 -0
  15. package/dist/core/index.d.ts +7 -0
  16. package/dist/core/index.d.ts.map +1 -0
  17. package/dist/core/index.js +26 -0
  18. package/dist/core/index.js.map +1 -0
  19. package/dist/example.d.ts +67 -0
  20. package/dist/example.d.ts.map +1 -0
  21. package/dist/example.js +86 -0
  22. package/dist/example.js.map +1 -0
  23. package/dist/index.d.ts +14 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +41 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/init.d.ts +28 -0
  28. package/dist/init.d.ts.map +1 -0
  29. package/dist/init.js +63 -0
  30. package/dist/init.js.map +1 -0
  31. package/dist/types/errors.d.ts +23 -0
  32. package/dist/types/errors.d.ts.map +1 -0
  33. package/dist/types/errors.js +46 -0
  34. package/dist/types/errors.js.map +1 -0
  35. package/dist/types/index.d.ts +7 -0
  36. package/dist/types/index.d.ts.map +1 -0
  37. package/dist/types/index.js +23 -0
  38. package/dist/types/index.js.map +1 -0
  39. package/dist/types/types.d.ts +30 -0
  40. package/dist/types/types.d.ts.map +1 -0
  41. package/dist/types/types.js +6 -0
  42. package/dist/types/types.js.map +1 -0
  43. package/dist/utils/index.d.ts +10 -0
  44. package/dist/utils/index.d.ts.map +1 -0
  45. package/dist/utils/index.js +18 -0
  46. package/dist/utils/index.js.map +1 -0
  47. package/dist/utils/logger.d.ts +26 -0
  48. package/dist/utils/logger.d.ts.map +1 -0
  49. package/dist/utils/logger.js +41 -0
  50. package/dist/utils/logger.js.map +1 -0
  51. package/dist/utils/migrations.d.ts +94 -0
  52. package/dist/utils/migrations.d.ts.map +1 -0
  53. package/dist/utils/migrations.js +440 -0
  54. package/dist/utils/migrations.js.map +1 -0
  55. package/dist/utils/schema-introspection.d.ts +183 -0
  56. package/dist/utils/schema-introspection.d.ts.map +1 -0
  57. package/dist/utils/schema-introspection.js +501 -0
  58. package/dist/utils/schema-introspection.js.map +1 -0
  59. package/dist/utils/seeds.d.ts +51 -0
  60. package/dist/utils/seeds.d.ts.map +1 -0
  61. package/dist/utils/seeds.js +209 -0
  62. package/dist/utils/seeds.js.map +1 -0
  63. package/dist/utils/sync.d.ts +57 -0
  64. package/dist/utils/sync.d.ts.map +1 -0
  65. package/dist/utils/sync.js +221 -0
  66. package/dist/utils/sync.js.map +1 -0
  67. package/package.json +53 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Damilola Alao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,288 @@
1
+ # nanodb-orm
2
+
3
+ A generic, flexible database package built on top of Drizzle ORM with automatic migrations, schema introspection, and support for both local SQLite and remote Turso databases.
4
+
5
+ ## Features
6
+
7
+ - 🚀 **Auto-Migrations**: Automatically creates and updates database schemas from Drizzle table definitions
8
+ - 🔍 **Schema Introspection**: Comprehensive schema analysis and validation
9
+ - 🌐 **Multi-Database Support**: Works with local SQLite and remote Turso databases
10
+ - 📦 **NPM Package Ready**: Designed to be used as a standalone npm package
11
+ - 🛡️ **Type Safe**: Full TypeScript support with proper type inference
12
+ - ⚙️ **Configurable**: Flexible migration and seeding options
13
+ - 🧪 **Test Ready**: Built-in testing utilities and isolation
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install nanodb-orm
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import { initializeDatabase, DatabaseSync } from 'nanodb-orm';
25
+ import { tables } from './models'; // Your Drizzle table definitions
26
+
27
+ // Initialize the database package
28
+ initializeDatabase({
29
+ tables,
30
+ seedData: {
31
+ users: [
32
+ { name: 'John Doe', age: 30, email: 'john@example.com' }
33
+ ]
34
+ }
35
+ });
36
+
37
+ // Setup database (creates tables, runs migrations, seeds data)
38
+ await DatabaseSync.setup();
39
+ ```
40
+
41
+ ## API Reference
42
+
43
+ ### `initializeDatabase(schemaData: SchemaData)`
44
+
45
+ Initializes the database package with your schema data.
46
+
47
+ ```typescript
48
+ interface SchemaData {
49
+ tables: Record<string, any>; // Your Drizzle table definitions
50
+ seedData?: Record<string, any[]>; // Optional seed data
51
+ migrationConfig?: MigrationConfig; // Optional migration configuration
52
+ }
53
+
54
+ interface MigrationConfig {
55
+ preserveData?: boolean; // Preserve existing data (default: true)
56
+ autoMigrate?: boolean; // Enable auto-migrations (default: true)
57
+ dropTables?: boolean; // Allow dropping tables (default: false)
58
+ }
59
+ ```
60
+
61
+ ### `DatabaseSync`
62
+
63
+ Main database synchronization class.
64
+
65
+ ```typescript
66
+ // Setup database (create tables, migrate, seed)
67
+ await DatabaseSync.setup();
68
+
69
+ // Reset database (drop all tables and recreate)
70
+ await DatabaseSync.reset();
71
+
72
+ // Check if database is ready
73
+ const isReady = await DatabaseSync.isReady();
74
+ ```
75
+
76
+ ### `SchemaIntrospection`
77
+
78
+ Comprehensive schema analysis utilities.
79
+
80
+ ```typescript
81
+ // Get all table names
82
+ const tableNames = SchemaIntrospection.getAllTableNames();
83
+
84
+ // Get table information
85
+ const tableInfo = SchemaIntrospection.getTableInfo('users');
86
+
87
+ // Get schema statistics
88
+ const stats = SchemaIntrospection.getSchemaStats();
89
+
90
+ // Validate schema
91
+ const validation = await SchemaIntrospection.validateSchema();
92
+ ```
93
+
94
+ ### `DatabaseMigrations`
95
+
96
+ Migration management utilities.
97
+
98
+ ```typescript
99
+ // Initialize schema
100
+ await DatabaseMigrations.initializeSchema();
101
+
102
+ // Check table existence
103
+ const existence = await DatabaseMigrations.checkTableExistence();
104
+
105
+ // Validate schema
106
+ const validation = await DatabaseMigrations.validateSchema();
107
+ ```
108
+
109
+ ### `DatabaseSeeds`
110
+
111
+ Database seeding utilities.
112
+
113
+ ```typescript
114
+ // Seed database
115
+ await DatabaseSeeds.seedDatabase();
116
+
117
+ // Check if database has data
118
+ const hasData = await DatabaseSeeds.hasData();
119
+
120
+ // Clear all data
121
+ await DatabaseSeeds.clearAllData();
122
+ ```
123
+
124
+ ## Configuration
125
+
126
+ ### Environment Variables
127
+
128
+ ```bash
129
+ # Turso Database (optional)
130
+ TURSO_CONNECTION_URL=libsql://your-database.turso.io
131
+ TURSO_AUTH_TOKEN=your-auth-token
132
+
133
+ # Force local database (for testing)
134
+ FORCE_LOCAL_DB=true
135
+ NODE_ENV=test
136
+ ```
137
+
138
+ ### Migration Configuration
139
+
140
+ ```typescript
141
+ const migrationConfig = {
142
+ preserveData: true, // Always try to preserve existing data
143
+ autoMigrate: true, // Automatically handle schema changes
144
+ dropTables: false // Don't drop tables by default
145
+ };
146
+
147
+ initializeDatabase({
148
+ tables,
149
+ migrationConfig
150
+ });
151
+ ```
152
+
153
+ ## Usage Examples
154
+
155
+ ### Basic Setup
156
+
157
+ ```typescript
158
+ import { initializeDatabase, DatabaseSync } from 'nanodb-orm';
159
+ import { usersTable, postsTable } from './models';
160
+
161
+ const tables = {
162
+ users: usersTable,
163
+ posts: postsTable
164
+ };
165
+
166
+ initializeDatabase({ tables });
167
+ await DatabaseSync.setup();
168
+ ```
169
+
170
+ ### With Seed Data
171
+
172
+ ```typescript
173
+ const seedData = {
174
+ users: [
175
+ { name: 'Alice', age: 25, email: 'alice@example.com' },
176
+ { name: 'Bob', age: 30, email: 'bob@example.com' }
177
+ ],
178
+ posts: [
179
+ { title: 'Hello World', content: 'My first post', userId: 1 },
180
+ { title: 'Second Post', content: 'Another post', userId: 2 }
181
+ ]
182
+ };
183
+
184
+ initializeDatabase({ tables, seedData });
185
+ await DatabaseSync.setup();
186
+ ```
187
+
188
+ ### Custom Migration Config
189
+
190
+ ```typescript
191
+ const migrationConfig = {
192
+ preserveData: false, // Allow data loss for development
193
+ autoMigrate: true, // Enable auto-migrations
194
+ dropTables: true // Allow dropping tables
195
+ };
196
+
197
+ initializeDatabase({
198
+ tables,
199
+ migrationConfig
200
+ });
201
+ ```
202
+
203
+ ### Schema Introspection
204
+
205
+ ```typescript
206
+ import { SchemaIntrospection } from 'nanodb-orm';
207
+
208
+ // Get comprehensive schema information
209
+ const schemaInfo = SchemaIntrospection.getSchemaStats();
210
+ console.log('Total tables:', schemaInfo.totalTables);
211
+ console.log('Table details:', schemaInfo.tableDetails);
212
+
213
+ // Validate schema integrity
214
+ const validation = await SchemaIntrospection.validateSchema();
215
+ if (!validation.isValid) {
216
+ console.log('Schema issues:', validation.errors);
217
+ }
218
+ ```
219
+
220
+ ## Testing
221
+
222
+ The package includes built-in testing utilities:
223
+
224
+ ```typescript
225
+ import { DatabaseSync } from 'nanodb-orm';
226
+
227
+ describe('My Tests', () => {
228
+ beforeEach(async () => {
229
+ // Reset database for each test
230
+ await DatabaseSync.reset();
231
+ });
232
+
233
+ test('should work with clean database', async () => {
234
+ // Your test code here
235
+ });
236
+ });
237
+ ```
238
+
239
+ ## Database Support
240
+
241
+ ### Local SQLite
242
+ - Automatically used when Turso credentials are not available
243
+ - Perfect for development and testing
244
+ - File-based storage
245
+
246
+ ### Remote Turso
247
+ - Cloud-hosted SQLite database
248
+ - Requires `TURSO_CONNECTION_URL` and `TURSO_AUTH_TOKEN`
249
+ - Production-ready with global replication
250
+
251
+ ## Error Handling
252
+
253
+ The package provides comprehensive error handling:
254
+
255
+ ```typescript
256
+ import { DatabaseError } from 'nanodb-orm';
257
+
258
+ try {
259
+ await DatabaseSync.setup();
260
+ } catch (error) {
261
+ if (error instanceof DatabaseError) {
262
+ console.log('Database error:', error.message);
263
+ console.log('Operation:', error.operation);
264
+ }
265
+ }
266
+ ```
267
+
268
+ ## Contributing
269
+
270
+ 1. Fork the repository
271
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
272
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
273
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
274
+ 5. Open a Pull Request
275
+
276
+ ## License
277
+
278
+ MIT © Damilola Alao
279
+
280
+ ## Changelog
281
+
282
+ ### 1.0.0
283
+ - Initial release
284
+ - Auto-migration system
285
+ - Schema introspection
286
+ - Multi-database support
287
+ - TypeScript support
288
+ - Testing utilities
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Database constants and configuration values
3
+ */
4
+ export declare const TABLE_NAMES: {
5
+ readonly MIGRATIONS: "migrations";
6
+ };
7
+ export declare const ENV_VARS: {
8
+ readonly TURSO_CONNECTION_URL: "TURSO_CONNECTION_URL";
9
+ readonly TURSO_AUTH_TOKEN: "TURSO_AUTH_TOKEN";
10
+ readonly NODE_ENV: "NODE_ENV";
11
+ };
12
+ export declare const DEFAULT_CONFIG: {
13
+ readonly SYNC_INTERVAL: 60;
14
+ readonly LOG_LEVEL: "INFO";
15
+ readonly MAX_RETRIES: 3;
16
+ readonly TIMEOUT: 5000;
17
+ };
18
+ export declare const MIGRATION_VERSIONS: {
19
+ readonly INITIAL: "001";
20
+ };
21
+ export declare const HEALTH_THRESHOLDS: {
22
+ readonly MAX_CONNECTION_TIME: 1000;
23
+ readonly MAX_QUERY_TIME: 1000;
24
+ readonly MAX_TOTAL_TIME: 5000;
25
+ };
26
+ export declare const ERROR_CODES: {
27
+ readonly CONNECTION_FAILED: "CONNECTION_FAILED";
28
+ readonly SYNC_NOT_SUPPORTED: "SYNC_NOT_SUPPORTED";
29
+ readonly SCHEMA_VALIDATION_FAILED: "SCHEMA_VALIDATION_FAILED";
30
+ readonly MIGRATION_FAILED: "MIGRATION_FAILED";
31
+ readonly SEED_FAILED: "SEED_FAILED";
32
+ };
33
+ export declare const LOG_LEVELS: {
34
+ readonly DEBUG: 0;
35
+ readonly INFO: 1;
36
+ readonly WARN: 2;
37
+ readonly ERROR: 3;
38
+ };
39
+ export declare const OPERATION_TYPES: {
40
+ readonly CONNECTION: "connection";
41
+ readonly SYNC: "sync";
42
+ readonly MIGRATION: "migration";
43
+ readonly SEED: "seed";
44
+ readonly HEALTH: "health";
45
+ readonly SCHEMA: "schema";
46
+ readonly QUERY: "query";
47
+ };
48
+ export declare const FILE_PATHS: {
49
+ readonly DEFAULT_LOCAL_DB: "./local.db";
50
+ readonly DEFAULT_REPLICA_DB: "./replica.db";
51
+ readonly ENV_FILE: ".env";
52
+ };
53
+ export type TableName = typeof TABLE_NAMES[keyof typeof TABLE_NAMES];
54
+ export type EnvVar = typeof ENV_VARS[keyof typeof ENV_VARS];
55
+ export type MigrationVersion = typeof MIGRATION_VERSIONS[keyof typeof MIGRATION_VERSIONS];
56
+ export type ErrorCode = typeof ERROR_CODES[keyof typeof ERROR_CODES];
57
+ export type LogLevel = typeof LOG_LEVELS[keyof typeof LOG_LEVELS];
58
+ export type OperationType = typeof OPERATION_TYPES[keyof typeof OPERATION_TYPES];
59
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../constants/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,eAAO,MAAM,WAAW;;CAEd,CAAC;AAMX,eAAO,MAAM,QAAQ;;;;CAIX,CAAC;AAGX,eAAO,MAAM,cAAc;;;;;CAKjB,CAAC;AAGX,eAAO,MAAM,kBAAkB;;CAErB,CAAC;AAGX,eAAO,MAAM,iBAAiB;;;;CAIpB,CAAC;AAGX,eAAO,MAAM,WAAW;;;;;;CAMd,CAAC;AAGX,eAAO,MAAM,UAAU;;;;;CAKb,CAAC;AAGX,eAAO,MAAM,eAAe;;;;;;;;CAQlB,CAAC;AAGX,eAAO,MAAM,UAAU;;;;CAIb,CAAC;AAGX,MAAM,MAAM,SAAS,GAAG,OAAO,WAAW,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;AACrE,MAAM,MAAM,MAAM,GAAG,OAAO,QAAQ,CAAC,MAAM,OAAO,QAAQ,CAAC,CAAC;AAC5D,MAAM,MAAM,gBAAgB,GAAG,OAAO,kBAAkB,CAAC,MAAM,OAAO,kBAAkB,CAAC,CAAC;AAC1F,MAAM,MAAM,SAAS,GAAG,OAAO,WAAW,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;AACrE,MAAM,MAAM,QAAQ,GAAG,OAAO,UAAU,CAAC,MAAM,OAAO,UAAU,CAAC,CAAC;AAClE,MAAM,MAAM,aAAa,GAAG,OAAO,eAAe,CAAC,MAAM,OAAO,eAAe,CAAC,CAAC"}
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ /**
3
+ * Database constants and configuration values
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FILE_PATHS = exports.OPERATION_TYPES = exports.LOG_LEVELS = exports.ERROR_CODES = exports.HEALTH_THRESHOLDS = exports.MIGRATION_VERSIONS = exports.DEFAULT_CONFIG = exports.ENV_VARS = exports.TABLE_NAMES = void 0;
7
+ // Database table names (now dynamic from schema.ts)
8
+ exports.TABLE_NAMES = {
9
+ MIGRATIONS: 'migrations', // System table
10
+ };
11
+ // Note: Column names are now dynamic from schema.ts
12
+ // No hardcoded column names needed since the system is generic
13
+ // Environment variable names
14
+ exports.ENV_VARS = {
15
+ TURSO_CONNECTION_URL: 'TURSO_CONNECTION_URL',
16
+ TURSO_AUTH_TOKEN: 'TURSO_AUTH_TOKEN',
17
+ NODE_ENV: 'NODE_ENV',
18
+ };
19
+ // Default configuration values
20
+ exports.DEFAULT_CONFIG = {
21
+ SYNC_INTERVAL: 60, // seconds
22
+ LOG_LEVEL: 'INFO',
23
+ MAX_RETRIES: 3,
24
+ TIMEOUT: 5000, // milliseconds
25
+ };
26
+ // Migration versions (now dynamic from schema.ts)
27
+ exports.MIGRATION_VERSIONS = {
28
+ INITIAL: '001',
29
+ };
30
+ // Health check thresholds
31
+ exports.HEALTH_THRESHOLDS = {
32
+ MAX_CONNECTION_TIME: 1000, // milliseconds
33
+ MAX_QUERY_TIME: 1000, // milliseconds
34
+ MAX_TOTAL_TIME: 5000, // milliseconds
35
+ };
36
+ // Error codes
37
+ exports.ERROR_CODES = {
38
+ CONNECTION_FAILED: 'CONNECTION_FAILED',
39
+ SYNC_NOT_SUPPORTED: 'SYNC_NOT_SUPPORTED',
40
+ SCHEMA_VALIDATION_FAILED: 'SCHEMA_VALIDATION_FAILED',
41
+ MIGRATION_FAILED: 'MIGRATION_FAILED',
42
+ SEED_FAILED: 'SEED_FAILED',
43
+ };
44
+ // Log levels
45
+ exports.LOG_LEVELS = {
46
+ DEBUG: 0,
47
+ INFO: 1,
48
+ WARN: 2,
49
+ ERROR: 3,
50
+ };
51
+ // Database operation types
52
+ exports.OPERATION_TYPES = {
53
+ CONNECTION: 'connection',
54
+ SYNC: 'sync',
55
+ MIGRATION: 'migration',
56
+ SEED: 'seed',
57
+ HEALTH: 'health',
58
+ SCHEMA: 'schema',
59
+ QUERY: 'query',
60
+ };
61
+ // File paths
62
+ exports.FILE_PATHS = {
63
+ DEFAULT_LOCAL_DB: './local.db',
64
+ DEFAULT_REPLICA_DB: './replica.db',
65
+ ENV_FILE: '.env',
66
+ };
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../constants/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oDAAoD;AACvC,QAAA,WAAW,GAAG;IACzB,UAAU,EAAE,YAAY,EAAE,eAAe;CACjC,CAAC;AAEX,oDAAoD;AACpD,+DAA+D;AAE/D,6BAA6B;AAChB,QAAA,QAAQ,GAAG;IACtB,oBAAoB,EAAE,sBAAsB;IAC5C,gBAAgB,EAAE,kBAAkB;IACpC,QAAQ,EAAE,UAAU;CACZ,CAAC;AAEX,+BAA+B;AAClB,QAAA,cAAc,GAAG;IAC5B,aAAa,EAAE,EAAE,EAAE,UAAU;IAC7B,SAAS,EAAE,MAAM;IACjB,WAAW,EAAE,CAAC;IACd,OAAO,EAAE,IAAI,EAAE,eAAe;CACtB,CAAC;AAEX,kDAAkD;AACrC,QAAA,kBAAkB,GAAG;IAChC,OAAO,EAAE,KAAK;CACN,CAAC;AAEX,0BAA0B;AACb,QAAA,iBAAiB,GAAG;IAC/B,mBAAmB,EAAE,IAAI,EAAE,eAAe;IAC1C,cAAc,EAAE,IAAI,EAAE,eAAe;IACrC,cAAc,EAAE,IAAI,EAAE,eAAe;CAC7B,CAAC;AAEX,cAAc;AACD,QAAA,WAAW,GAAG;IACzB,iBAAiB,EAAE,mBAAmB;IACtC,kBAAkB,EAAE,oBAAoB;IACxC,wBAAwB,EAAE,0BAA0B;IACpD,gBAAgB,EAAE,kBAAkB;IACpC,WAAW,EAAE,aAAa;CAClB,CAAC;AAEX,aAAa;AACA,QAAA,UAAU,GAAG;IACxB,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;CACA,CAAC;AAEX,2BAA2B;AACd,QAAA,eAAe,GAAG;IAC7B,UAAU,EAAE,YAAY;IACxB,IAAI,EAAE,MAAM;IACZ,SAAS,EAAE,WAAW;IACtB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,KAAK,EAAE,OAAO;CACN,CAAC;AAEX,aAAa;AACA,QAAA,UAAU,GAAG;IACxB,gBAAgB,EAAE,YAAY;IAC9B,kBAAkB,EAAE,cAAc;IAClC,QAAQ,EAAE,MAAM;CACR,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Database configuration interface
3
+ */
4
+ export interface DatabaseConfig {
5
+ connectionUrl: string;
6
+ authToken: string;
7
+ syncInterval: number;
8
+ }
9
+ /**
10
+ * Get database configuration from environment variables
11
+ */
12
+ export declare function getDatabaseConfig(): DatabaseConfig;
13
+ /**
14
+ * Validate database configuration
15
+ */
16
+ export declare function validateDatabaseConfig(): boolean;
17
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../core/config.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,CAiBlD;AAED;;GAEG;AACH,wBAAgB,sBAAsB,IAAI,OAAO,CAQhD"}
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDatabaseConfig = getDatabaseConfig;
4
+ exports.validateDatabaseConfig = validateDatabaseConfig;
5
+ const dotenv_1 = require("dotenv");
6
+ const errors_1 = require("../types/errors");
7
+ // Load environment variables
8
+ (0, dotenv_1.config)({ path: '.env' });
9
+ /**
10
+ * Get database configuration from environment variables
11
+ */
12
+ function getDatabaseConfig() {
13
+ const connectionUrl = process.env.TURSO_CONNECTION_URL;
14
+ const authToken = process.env.TURSO_AUTH_TOKEN;
15
+ if (!connectionUrl) {
16
+ throw new errors_1.DatabaseError('TURSO_CONNECTION_URL environment variable is required', 'config');
17
+ }
18
+ if (!authToken) {
19
+ throw new errors_1.DatabaseError('TURSO_AUTH_TOKEN environment variable is required', 'config');
20
+ }
21
+ return {
22
+ connectionUrl,
23
+ authToken,
24
+ syncInterval: 60, // Default sync interval in seconds
25
+ };
26
+ }
27
+ /**
28
+ * Validate database configuration
29
+ */
30
+ function validateDatabaseConfig() {
31
+ try {
32
+ getDatabaseConfig();
33
+ return true;
34
+ }
35
+ catch (error) {
36
+ console.error('Database configuration validation failed:', error);
37
+ return false;
38
+ }
39
+ }
40
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../core/config.ts"],"names":[],"mappings":";;AAkBA,8CAiBC;AAKD,wDAQC;AAhDD,mCAAgC;AAChC,4CAAgD;AAEhD,6BAA6B;AAC7B,IAAA,eAAM,EAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AAWzB;;GAEG;AACH,SAAgB,iBAAiB;IAC/B,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC;IACvD,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;IAE/C,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,sBAAa,CAAC,uDAAuD,EAAE,QAAQ,CAAC,CAAC;IAC7F,CAAC;IAED,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,sBAAa,CAAC,mDAAmD,EAAE,QAAQ,CAAC,CAAC;IACzF,CAAC;IAED,OAAO;QACL,aAAa;QACb,SAAS;QACT,YAAY,EAAE,EAAE,EAAE,mCAAmC;KACtD,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAgB,sBAAsB;IACpC,IAAI,CAAC;QACH,iBAAiB,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,KAAK,CAAC,CAAC;QAClE,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Database connection management
3
+ * Handles the creation and configuration of database connections
4
+ */
5
+ export declare class DatabaseConnection {
6
+ private static instance;
7
+ /**
8
+ * Get or create the database connection instance
9
+ */
10
+ static getInstance(): import("drizzle-orm/libsql").LibSQLDatabase<Record<string, unknown>> & {
11
+ $client: import("@libsql/client").Client;
12
+ };
13
+ /**
14
+ * Create a new database connection
15
+ */
16
+ private static createConnection;
17
+ /**
18
+ * Reset the connection instance (useful for testing)
19
+ */
20
+ static reset(): void;
21
+ /**
22
+ * Check if connection is established
23
+ */
24
+ static isConnected(): boolean;
25
+ }
26
+ export declare const db: import("drizzle-orm/libsql").LibSQLDatabase<Record<string, unknown>> & {
27
+ $client: import("@libsql/client").Client;
28
+ };
29
+ //# sourceMappingURL=connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../core/connection.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA2C;IAElE;;OAEG;IACH,MAAM,CAAC,WAAW;;;IAOlB;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAmC/B;;OAEG;IACH,MAAM,CAAC,KAAK;IAIZ;;OAEG;IACH,MAAM,CAAC,WAAW,IAAI,OAAO;CAG9B;AAGD,eAAO,MAAM,EAAE;;CAAmC,CAAC"}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.db = exports.DatabaseConnection = void 0;
4
+ const libsql_1 = require("drizzle-orm/libsql");
5
+ const client_1 = require("@libsql/client");
6
+ const config_1 = require("./config");
7
+ const errors_1 = require("../types/errors");
8
+ /**
9
+ * Database connection management
10
+ * Handles the creation and configuration of database connections
11
+ */
12
+ class DatabaseConnection {
13
+ /**
14
+ * Get or create the database connection instance
15
+ */
16
+ static getInstance() {
17
+ if (!this.instance) {
18
+ this.instance = this.createConnection();
19
+ }
20
+ return this.instance;
21
+ }
22
+ /**
23
+ * Create a new database connection
24
+ */
25
+ static createConnection() {
26
+ try {
27
+ // For testing, force local SQLite database to avoid remote schema conflicts
28
+ if (process.env.NODE_ENV === 'test' || process.env.FORCE_LOCAL_DB === 'true') {
29
+ // Use unique database file for each test to avoid locking issues
30
+ const testDbName = process.env.JEST_WORKER_ID ? `test-${process.env.JEST_WORKER_ID}.db` : 'test.db';
31
+ console.log(`Using local SQLite database for testing: ${testDbName}`);
32
+ const client = (0, client_1.createClient)({
33
+ url: `file:${testDbName}`,
34
+ });
35
+ return (0, libsql_1.drizzle)(client);
36
+ }
37
+ // Try to get Turso config first, fallback to local SQLite if not available
38
+ try {
39
+ const config = (0, config_1.getDatabaseConfig)();
40
+ const client = (0, client_1.createClient)({
41
+ url: config.connectionUrl,
42
+ authToken: config.authToken,
43
+ });
44
+ console.log('Connected to Turso database');
45
+ return (0, libsql_1.drizzle)(client);
46
+ }
47
+ catch (configError) {
48
+ // Fallback to local SQLite database
49
+ console.log('Turso config not found, using local SQLite database');
50
+ const client = (0, client_1.createClient)({
51
+ url: 'file:database.db',
52
+ });
53
+ return (0, libsql_1.drizzle)(client);
54
+ }
55
+ }
56
+ catch (error) {
57
+ throw new errors_1.DatabaseError(`Failed to create database connection: ${error.message}`, 'connection', error);
58
+ }
59
+ }
60
+ /**
61
+ * Reset the connection instance (useful for testing)
62
+ */
63
+ static reset() {
64
+ this.instance = null;
65
+ }
66
+ /**
67
+ * Check if connection is established
68
+ */
69
+ static isConnected() {
70
+ return this.instance !== null;
71
+ }
72
+ }
73
+ exports.DatabaseConnection = DatabaseConnection;
74
+ DatabaseConnection.instance = null;
75
+ // Export the database instance for convenience
76
+ exports.db = DatabaseConnection.getInstance();
77
+ //# sourceMappingURL=connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.js","sourceRoot":"","sources":["../../core/connection.ts"],"names":[],"mappings":";;;AAAA,+CAA6C;AAC7C,2CAA8C;AAC9C,qCAA6C;AAC7C,4CAAgD;AAEhD;;;GAGG;AACH,MAAa,kBAAkB;IAG7B;;OAEG;IACH,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAC1C,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;OAEG;IACK,MAAM,CAAC,gBAAgB;QAC7B,IAAI,CAAC;YACH,4EAA4E;YAC5E,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,KAAK,MAAM,EAAE,CAAC;gBAC7E,iEAAiE;gBACjE,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,QAAQ,OAAO,CAAC,GAAG,CAAC,cAAc,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;gBACpG,OAAO,CAAC,GAAG,CAAC,4CAA4C,UAAU,EAAE,CAAC,CAAC;gBACtE,MAAM,MAAM,GAAG,IAAA,qBAAY,EAAC;oBAC1B,GAAG,EAAE,QAAQ,UAAU,EAAE;iBAC1B,CAAC,CAAC;gBACH,OAAO,IAAA,gBAAO,EAAC,MAAM,CAAC,CAAC;YACzB,CAAC;YAED,2EAA2E;YAC3E,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,IAAA,0BAAiB,GAAE,CAAC;gBACnC,MAAM,MAAM,GAAG,IAAA,qBAAY,EAAC;oBAC1B,GAAG,EAAE,MAAM,CAAC,aAAa;oBACzB,SAAS,EAAE,MAAM,CAAC,SAAS;iBAC5B,CAAC,CAAC;gBACH,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;gBAC3C,OAAO,IAAA,gBAAO,EAAC,MAAM,CAAC,CAAC;YACzB,CAAC;YAAC,OAAO,WAAW,EAAE,CAAC;gBACrB,oCAAoC;gBACpC,OAAO,CAAC,GAAG,CAAC,qDAAqD,CAAC,CAAC;gBACnE,MAAM,MAAM,GAAG,IAAA,qBAAY,EAAC;oBAC1B,GAAG,EAAE,kBAAkB;iBACxB,CAAC,CAAC;gBACH,OAAO,IAAA,gBAAO,EAAC,MAAM,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,sBAAa,CAAC,yCAA0C,KAAe,CAAC,OAAO,EAAE,EAAE,YAAY,EAAE,KAAc,CAAC,CAAC;QAC7H,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAK;QACV,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,WAAW;QAChB,OAAO,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC;IAChC,CAAC;;AA/DH,gDAgEC;AA/DgB,2BAAQ,GAAsC,IAAI,CAAC;AAiEpE,+CAA+C;AAClC,QAAA,EAAE,GAAG,kBAAkB,CAAC,WAAW,EAAE,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Core database functionality
3
+ * Essential database components: connection, schema, and configuration
4
+ */
5
+ export { db, DatabaseConnection } from './connection';
6
+ export * from './config';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../core/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,EAAE,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACtD,cAAc,UAAU,CAAC"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * Core database functionality
4
+ * Essential database components: connection, schema, and configuration
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.DatabaseConnection = exports.db = void 0;
22
+ var connection_1 = require("./connection");
23
+ Object.defineProperty(exports, "db", { enumerable: true, get: function () { return connection_1.db; } });
24
+ Object.defineProperty(exports, "DatabaseConnection", { enumerable: true, get: function () { return connection_1.DatabaseConnection; } });
25
+ __exportStar(require("./config"), exports);
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../core/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;AAEH,2CAAsD;AAA7C,gGAAA,EAAE,OAAA;AAAE,gHAAA,kBAAkB,OAAA;AAC/B,2CAAyB"}