@mantiq/database 0.5.6 → 0.5.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mantiq/database",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "description": "Query builder, ORM, migrations, seeders, factories — with SQLite, Postgres, MySQL and MongoDB support",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -3,37 +3,25 @@ import { Model } from './orm/Model.ts'
3
3
 
4
4
  export const DATABASE_MANAGER = Symbol('DatabaseManager')
5
5
 
6
- /**
7
- * Minimal service provider integration — provides a factory function
8
- * so @mantiq/database can be used without @mantiq/core if needed.
9
- *
10
- * When using with @mantiq/core, extend ServiceProvider and register
11
- * DatabaseManager as a singleton with your application's config.
12
- *
13
- * @example — with @mantiq/core:
14
- * ```ts
15
- * import { ServiceProvider } from '@mantiq/core'
16
- * import { DatabaseManager, Model } from '@mantiq/database'
17
- *
18
- * export class DatabaseServiceProvider extends ServiceProvider {
19
- * register(): void {
20
- * this.app.singleton(DatabaseManager, () => {
21
- * const config = this.app.make('config').get('database')
22
- * const manager = new DatabaseManager(config)
23
- * Model.setConnection(manager.connection())
24
- * return manager
25
- * })
26
- * }
27
- * }
28
- * ```
29
- */
6
+ /** Set the global DatabaseManager reference. */
7
+ export function setManager(manager: DatabaseManager): void {
8
+ ;(globalThis as any).__mantiq_db_manager = manager
9
+ }
10
+
11
+ /** Get the global DatabaseManager. */
12
+ export function getManager(): DatabaseManager | undefined {
13
+ return (globalThis as any).__mantiq_db_manager
14
+ }
15
+
16
+ /** Set the default connection on all Models. */
17
+ export function setupModels(manager: DatabaseManager, connectionName?: string): void {
18
+ Model.setConnection(manager.connection(connectionName))
19
+ }
20
+
21
+ /** Factory function for standalone use (without service provider). */
30
22
  export function createDatabaseManager(config: {
31
23
  default?: string
32
24
  connections: Record<string, any>
33
25
  }): DatabaseManager {
34
26
  return new DatabaseManager(config)
35
27
  }
36
-
37
- export function setupModels(manager: DatabaseManager, connectionName?: string): void {
38
- Model.setConnection(manager.connection(connectionName))
39
- }
package/src/orm/Model.ts CHANGED
@@ -96,7 +96,7 @@ export abstract class Model {
96
96
  const conn = this.connection
97
97
  if (!conn) throw new Error(`No connection set on model ${this.table}. Call Model.setConnection() first.`)
98
98
 
99
- const tableName = this.table || snakeCase(this.name)
99
+ const tableName = this.table || pluralize(snakeCase(this.name))
100
100
  const builder = new ModelQueryBuilder<T>(
101
101
  conn,
102
102
  tableName,
@@ -443,7 +443,7 @@ export abstract class Model {
443
443
  // saving (cancellable)
444
444
  if (await this.fireModelEvent('saving') === false) return this
445
445
 
446
- const table = ctor.table || snakeCase(ctor.name)
446
+ const table = ctor.table || pluralize(snakeCase(ctor.name))
447
447
  const now = new Date()
448
448
 
449
449
  if (this._exists) {
@@ -497,7 +497,7 @@ export abstract class Model {
497
497
  // deleting (cancellable)
498
498
  if (await this.fireModelEvent('deleting') === false) return false
499
499
 
500
- const table = ctor.table || snakeCase(ctor.name)
500
+ const table = ctor.table || pluralize(snakeCase(ctor.name))
501
501
 
502
502
  if (ctor.softDelete) {
503
503
  await ctor.connection.table(table)
@@ -522,7 +522,7 @@ export abstract class Model {
522
522
  // forceDeleting (cancellable)
523
523
  if (await this.fireModelEvent('forceDeleting') === false) return false
524
524
 
525
- const table = ctor.table || snakeCase(ctor.name)
525
+ const table = ctor.table || pluralize(snakeCase(ctor.name))
526
526
  await ctor.connection.table(table).where(ctor.primaryKey, this.getKey()).delete()
527
527
  this._exists = false
528
528
 
@@ -537,7 +537,7 @@ export abstract class Model {
537
537
  // restoring (cancellable)
538
538
  if (await this.fireModelEvent('restoring') === false) return false
539
539
 
540
- const table = ctor.table || snakeCase(ctor.name)
540
+ const table = ctor.table || pluralize(snakeCase(ctor.name))
541
541
  await ctor.connection.table(table)
542
542
  .where(ctor.primaryKey, this.getKey())
543
543
  .update({ [ctor.softDeleteColumn]: null })
@@ -786,3 +786,13 @@ function snakeCase(name: string): string {
786
786
  .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
787
787
  .toLowerCase()
788
788
  }
789
+
790
+ /** Simple English pluralization for table name derivation. */
791
+ function pluralize(word: string): string {
792
+ if (word.endsWith('ss') || word.endsWith('sh') || word.endsWith('ch') || word.endsWith('x') || word.endsWith('z')) return word + 'es'
793
+ if (word.endsWith('y') && !/[aeiou]y$/i.test(word)) return word.slice(0, -1) + 'ies'
794
+ if (word.endsWith('fe')) return word.slice(0, -2) + 'ves'
795
+ if (word.endsWith('f')) return word.slice(0, -1) + 'ves'
796
+ if (word.endsWith('s')) return word
797
+ return word + 's'
798
+ }