@neupgroup/mapper 1.6.0 → 1.6.2

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.
@@ -1,10 +1,99 @@
1
1
  import { createMapper } from './mapper.js';
2
2
  import { TableMigrator } from './migrator.js';
3
3
  export class FluentQueryBuilder {
4
- constructor(mapper, schemaName) {
4
+ constructor(mapper, schemaName, connectionName) {
5
5
  this.mapper = mapper;
6
6
  this.schemaName = schemaName;
7
- this.query = mapper.use(schemaName);
7
+ try {
8
+ this.query = mapper.use(schemaName);
9
+ if (connectionName) {
10
+ const def = this.getDef();
11
+ if (def)
12
+ def.connectionName = connectionName;
13
+ }
14
+ }
15
+ catch (e) {
16
+ // Auto-register schema if missing (Automatically schemas!)
17
+ mapper.getSchemaManager().create(schemaName).use({ connection: connectionName || 'default', collection: schemaName }).setStructure({});
18
+ this.query = mapper.use(schemaName);
19
+ }
20
+ }
21
+ getDef() {
22
+ return this.mapper.getSchemaManager().schemas.get(this.schemaName);
23
+ }
24
+ set fields(config) {
25
+ const def = this.getDef();
26
+ if (def) {
27
+ const parsed = parseDescriptorStructure(config);
28
+ def.fields = parsed.fields;
29
+ def.fieldsMap = new Map();
30
+ def.fields.forEach((f) => def.fieldsMap.set(f.name, f));
31
+ def.allowUndefinedFields = parsed.allowUndefinedFields;
32
+ }
33
+ }
34
+ set insertableFields(val) {
35
+ const def = this.getDef();
36
+ if (def)
37
+ def.insertableFields = val;
38
+ }
39
+ set updatableFields(val) {
40
+ const def = this.getDef();
41
+ if (def)
42
+ def.updatableFields = val;
43
+ }
44
+ set deleteType(val) {
45
+ const def = this.getDef();
46
+ if (def)
47
+ def.deleteType = val;
48
+ }
49
+ set massDeleteAllowed(val) {
50
+ const def = this.getDef();
51
+ if (def)
52
+ def.massDeleteAllowed = val;
53
+ }
54
+ set massEditAllowed(val) {
55
+ const def = this.getDef();
56
+ if (def)
57
+ def.massEditAllowed = val;
58
+ }
59
+ structure(config) {
60
+ this.fields = config;
61
+ return this;
62
+ }
63
+ collection(collectionName) {
64
+ const def = this.getDef();
65
+ if (def)
66
+ def.collectionName = collectionName;
67
+ return this;
68
+ }
69
+ get migrator() {
70
+ if (!this._migrator) {
71
+ this._migrator = new TableMigrator(this.schemaName);
72
+ // Sync connection if already set on query
73
+ const def = this.getDef();
74
+ if (def === null || def === void 0 ? void 0 : def.connectionName) {
75
+ this._migrator.useConnection(def.connectionName);
76
+ }
77
+ }
78
+ return this._migrator;
79
+ }
80
+ // Migration/DDL Methods (Proxied to internal migrator)
81
+ useConnection(name) {
82
+ this.migrator.useConnection(name);
83
+ const def = this.getDef();
84
+ if (def)
85
+ def.connectionName = name;
86
+ return this;
87
+ }
88
+ addColumn(name) { return this.migrator.addColumn(name); }
89
+ selectColumn(name) { return this.migrator.selectColumn(name); }
90
+ dropColumn(name) { this.migrator.dropColumn(name); return this; }
91
+ drop() { this.migrator.drop(); return this; }
92
+ async exec() {
93
+ return this.migrator.exec();
94
+ }
95
+ async dropTable() {
96
+ return this.migrator.drop().exec();
8
97
  }
9
98
  where(field, value, operator) {
10
99
  this.query.where(field, value, operator);
@@ -190,19 +279,19 @@ export class FluentConnectionSelector {
190
279
  this.connectionName = connectionName;
191
280
  }
192
281
  schema(schemaName) {
193
- return new FluentSchemaBuilder(this.mapper, schemaName, this.connectionName);
282
+ return new FluentQueryBuilder(this.mapper, schemaName, this.connectionName);
283
+ }
284
+ schemas(schemaName) {
285
+ return this.schema(schemaName);
194
286
  }
195
287
  query(schemaName) {
196
- return new FluentQueryBuilder(this.mapper, schemaName);
288
+ return this.schema(schemaName);
197
289
  }
198
290
  table(tableName) {
199
- return this.query(tableName);
291
+ return this.schema(tableName);
200
292
  }
201
293
  collection(collectionName) {
202
- return this.query(collectionName);
203
- }
204
- schemas(schemaName) {
205
- return new FluentSchemaWrapper(this.mapper.getSchemaManager(), schemaName, this.connectionName);
294
+ return this.schema(collectionName);
206
295
  }
207
296
  // API Request methods
208
297
  path(path) {
@@ -237,6 +326,12 @@ export class FluentMapper {
237
326
  query(schemaName) {
238
327
  return new FluentQueryBuilder(this.mapper, schemaName);
239
328
  }
329
+ schema(name) {
330
+ return this.query(name);
331
+ }
332
+ table(name) {
333
+ return this.query(name);
334
+ }
240
335
  makeConnection(name, type, config) {
241
336
  return new FluentConnectionBuilder(this.mapper, name, type, config);
242
337
  }
@@ -281,6 +376,9 @@ export class FluentMapper {
281
376
  async delete(schemaName, filters) {
282
377
  return this.mapper.delete(schemaName, filters);
283
378
  }
379
+ async dropTable(name) {
380
+ return new TableMigrator(name).drop().exec();
381
+ }
284
382
  }
285
383
  // Static API class that provides the fluent interface
286
384
  export class StaticMapper {
@@ -300,6 +398,14 @@ export class StaticMapper {
300
398
  static query(schemaName) {
301
399
  return StaticMapper.getFluentMapper().query(schemaName);
302
400
  }
401
+ static schema(name) {
402
+ if (name)
403
+ return StaticMapper.getFluentMapper().schema(name);
404
+ return StaticMapper.schemas();
405
+ }
406
+ static table(name) {
407
+ return StaticMapper.schema(name);
408
+ }
303
409
  // New API
304
410
  static connection(connectionOrConfig) {
305
411
  return StaticMapper.getFluentMapper().connection(connectionOrConfig);
@@ -309,9 +415,8 @@ export class StaticMapper {
309
415
  return StaticMapper.connection(connectionName);
310
416
  }
311
417
  static schemas(name) {
312
- if (name) {
313
- return new FluentSchemaWrapper(StaticMapper.getFluentMapper().mapper.getSchemaManager(), name);
314
- }
418
+ if (name)
419
+ return StaticMapper.schema(name);
315
420
  return new SchemaManagerWrapper(StaticMapper.getFluentMapper().mapper.getSchemaManager());
316
421
  }
317
422
  // Direct static methods
@@ -330,95 +435,20 @@ export class StaticMapper {
330
435
  static async delete(schemaName, filters) {
331
436
  return StaticMapper.getFluentMapper().delete(schemaName, filters);
332
437
  }
333
- }
334
- // Export a default instance for convenience
335
- export const Mapper = StaticMapper;
336
- export default Mapper;
337
- export class FluentSchemaWrapper {
338
- // builder unused
339
- constructor(manager, name, connectionName) {
340
- this.manager = manager;
341
- this.name = name;
342
- this.connectionName = connectionName;
343
- // Ensure schema exists or create it?
344
- // User pattern: Mapper.schemas('name').fields = ...
345
- // So we likely need to create it if missing, or update it.
346
- try {
347
- this.manager.create(name).use({ connection: connectionName || 'default', collection: name }).setStructure({});
348
- }
349
- catch (e) {
350
- // Ignore if exists, but maybe update connection if strictly provided?
351
- // Use existing definition if available.
352
- }
438
+ static async dropTable(name) {
439
+ return StaticMapper.getFluentMapper().dropTable(name);
353
440
  }
354
- getDef() {
355
- // Access private map from manager? Or expose a get method.
356
- // Manager has .schemas map.
357
- return this.manager.schemas.get(this.name);
358
- }
359
- set fields(config) {
360
- // Update schema structure
361
- const builder = new FluentSchemaBuilder(null, this.name, ''); // Dummy wrapper or use internal
362
- // Easier: use Manager.create(name) returns SchemaBuilder which has setStructure.
363
- // But if it exists, create() throws.
364
- // We need 'update' or direct access.
365
- // Let's hack: re-register or update def.
366
- const def = this.getDef();
367
- if (def) {
368
- // Re-parse
369
- const parsed = parseDescriptorStructure(config);
370
- def.fields = parsed.fields;
371
- def.fieldsMap = new Map();
372
- def.fields.forEach((f) => def.fieldsMap.set(f.name, f));
373
- def.allowUndefinedFields = parsed.allowUndefinedFields;
374
- }
441
+ static getConnections() {
442
+ return StaticMapper.getFluentMapper().mapper.getConnections();
375
443
  }
376
- set insertableFields(val) {
377
- const def = this.getDef();
378
- if (def)
379
- def.insertableFields = val;
380
- }
381
- set updatableFields(val) {
382
- const def = this.getDef();
383
- if (def)
384
- def.updatableFields = val;
385
- }
386
- set deleteType(val) {
387
- const def = this.getDef();
388
- if (def)
389
- def.deleteType = val;
390
- }
391
- set massDeleteAllowed(val) {
392
- const def = this.getDef();
393
- if (def)
394
- def.massDeleteAllowed = val;
395
- }
396
- set massEditAllowed(val) {
397
- const def = this.getDef();
398
- if (def)
399
- def.massEditAllowed = val;
400
- }
401
- // Delegation to QueryBuilder
402
- get(...fields) {
403
- const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
404
- return q.get(...fields);
405
- }
406
- limit(n) {
407
- const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
408
- return q.limit(n);
409
- }
410
- offset(n) {
411
- const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
412
- return q.offset(n);
413
- }
414
- async insert(data) {
415
- const q = new FluentQueryBuilder({
416
- use: (n) => this.manager.use(n),
417
- add: (n, d) => this.manager.use(n).add(d)
418
- }, this.name);
419
- return q.insert(data);
444
+ static async discover() {
445
+ const { discover } = await import('./discovery.js');
446
+ return discover();
420
447
  }
421
448
  }
449
+ // Export a default instance for convenience
450
+ export const Mapper = StaticMapper;
451
+ export default Mapper;
422
452
  // Helper to access parseDescriptorStructure from index.ts if not exported?
423
453
  // It is NOT exported. I need to export it or duplicate logic.
424
454
  // I'll export it from index.ts.
@@ -431,4 +461,10 @@ export class SchemaManagerWrapper {
431
461
  // This allows Mapper.schemas().table('name') to return a migrator
432
462
  return new TableMigrator(name);
433
463
  }
464
+ schema(name) {
465
+ return this.table(name);
466
+ }
467
+ async dropTable(name) {
468
+ return new TableMigrator(name).drop().exec();
469
+ }
434
470
  }
package/dist/index.d.ts CHANGED
@@ -131,8 +131,8 @@ export { StaticMapper } from './fluent-mapper.js';
131
131
  export type { FluentQueryBuilder, FluentConnectionBuilder, FluentSchemaBuilder, FluentSchemaCollectionBuilder, FluentConnectionSelector, FluentMapper } from './fluent-mapper.js';
132
132
  export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config.js';
133
133
  export type { MapperConfig, ConnectionConfig, DatabaseConnectionConfig, ApiConnectionConfig, SqliteConnectionConfig, ConfigSchema } from './config.js';
134
- export { MySQLAdapter, createMySQLAdapter, PostgreSQLAdapter, createPostgreSQLAdapter, MongoDBAdapter, createMongoDBAdapter, APIAdapter, createAPIAdapter, createAdapter, createAdapterFromUrl, autoAttachAdapter } from './adapters/index.js';
135
- export type { MySQLConfig, PostgreSQLConfig, MongoDBConfig, APIAdapterConfig, AdapterConfig } from './adapters/index.js';
134
+ export { MySQLAdapter, createMySQLAdapter, PostgreSQLAdapter, createPostgreSQLAdapter, MongoDBAdapter, createMongoDBAdapter, APIAdapter, createAPIAdapter, SQLiteAdapter, createSQLiteAdapter, createAdapter, createAdapterFromUrl, autoAttachAdapter } from './adapters/index.js';
135
+ export type { MySQLConfig, PostgreSQLConfig, MongoDBConfig, APIAdapterConfig, SQLiteConfig, AdapterConfig } from './adapters/index.js';
136
136
  export { MapperError, AdapterMissingError, UpdatePayloadMissingError, DocumentMissingIdError, ConnectionExistingError, ConnectionUnknownError, SchemaExistingError, SchemaMissingError, SchemaConfigurationError, } from './errors.js';
137
137
  export { Connector, mapper } from './connector.js';
138
138
  export { TableMigrator, ColumnBuilder } from './migrator.js';
package/dist/index.js CHANGED
@@ -401,7 +401,7 @@ export { StaticMapper } from './fluent-mapper.js';
401
401
  // Export the new config-based system
402
402
  export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config.js';
403
403
  // Export database adapters
404
- export { MySQLAdapter, createMySQLAdapter, PostgreSQLAdapter, createPostgreSQLAdapter, MongoDBAdapter, createMongoDBAdapter, APIAdapter, createAPIAdapter, createAdapter, createAdapterFromUrl, autoAttachAdapter } from './adapters/index.js';
404
+ export { MySQLAdapter, createMySQLAdapter, PostgreSQLAdapter, createPostgreSQLAdapter, MongoDBAdapter, createMongoDBAdapter, APIAdapter, createAPIAdapter, SQLiteAdapter, createSQLiteAdapter, createAdapter, createAdapterFromUrl, autoAttachAdapter } from './adapters/index.js';
405
405
  export { MapperError, AdapterMissingError, UpdatePayloadMissingError, DocumentMissingIdError, ConnectionExistingError, ConnectionUnknownError, SchemaExistingError, SchemaMissingError, SchemaConfigurationError, } from './errors.js';
406
406
  export { Connector, mapper } from './connector.js';
407
407
  export { TableMigrator, ColumnBuilder } from './migrator.js';
@@ -1,8 +1,9 @@
1
1
  export type ColumnType = 'string' | 'number' | 'boolean' | 'date' | 'int';
2
2
  export declare class ColumnBuilder {
3
3
  private name;
4
+ private migrator?;
4
5
  private def;
5
- constructor(name: string);
6
+ constructor(name: string, migrator?: TableMigrator | undefined);
6
7
  type(t: ColumnType | string): this;
7
8
  isPrimary(): this;
8
9
  isUnique(): this;
@@ -11,22 +12,41 @@ export declare class ColumnBuilder {
11
12
  default(val: any): this;
12
13
  values(vals: any[]): this;
13
14
  foreignKey(table: string, column: string): this;
14
- exec(): Promise<void>;
15
+ /**
16
+ * Queues a unique constraint removal for this column
17
+ */
18
+ dropUnique(): this;
19
+ /**
20
+ * Queues a primary key removal for this column
21
+ */
22
+ dropPrimaryKey(): this;
23
+ /**
24
+ * Queues a column drop
25
+ */
26
+ drop(): this;
15
27
  getDefinition(): any;
16
28
  }
17
29
  export declare class TableMigrator {
18
30
  private name;
19
31
  private columns;
20
32
  private connectionName;
33
+ private actions;
21
34
  constructor(name: string);
22
35
  useConnection(name: string): this;
36
+ /**
37
+ * Register a new column for creation
38
+ */
23
39
  addColumn(name: string): ColumnBuilder;
24
- getColumns(): any[];
40
+ /**
41
+ * Select an existing column for modification or dropping
42
+ */
43
+ selectColumn(name: string): ColumnBuilder;
44
+ dropTable(): this;
45
+ drop(): this;
46
+ dropColumn(columnName: string): this;
47
+ dropUnique(columnName: string): this;
48
+ dropPrimaryKey(columnName: string): this;
25
49
  private getAdapter;
26
- private generateCreateSql;
50
+ private generateColumnSql;
27
51
  exec(): Promise<void>;
28
- drop(): Promise<void>;
29
- dropColumn(columnName: string): Promise<void>;
30
- dropUnique(columnName: string): Promise<void>;
31
- dropPrimaryKey(columnName: string): Promise<void>;
32
52
  }