@neupgroup/mapper 1.6.2 → 1.7.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.
@@ -0,0 +1,10 @@
1
+ import { SelectBuilder, InsertBuilder, UpdateBuilder, DeleteBuilder } from './dml-builders.js';
2
+ export declare class BaseDispatcher {
3
+ private mapper;
4
+ private target;
5
+ constructor(mapper: any, target: string);
6
+ select(fields?: string[]): SelectBuilder;
7
+ insert(data: any): InsertBuilder;
8
+ update(data: any): UpdateBuilder;
9
+ delete(): DeleteBuilder;
10
+ }
@@ -0,0 +1,19 @@
1
+ import { SelectBuilder, InsertBuilder, UpdateBuilder, DeleteBuilder } from './dml-builders.js';
2
+ export class BaseDispatcher {
3
+ constructor(mapper, target) {
4
+ this.mapper = mapper;
5
+ this.target = target;
6
+ }
7
+ select(fields = []) {
8
+ return new SelectBuilder(this.mapper, this.target, fields);
9
+ }
10
+ insert(data) {
11
+ return new InsertBuilder(this.mapper, this.target, data);
12
+ }
13
+ update(data) {
14
+ return new UpdateBuilder(this.mapper, this.target, data);
15
+ }
16
+ delete() {
17
+ return new DeleteBuilder(this.mapper, this.target);
18
+ }
19
+ }
@@ -0,0 +1,11 @@
1
+ export declare class ClauseBuilder {
2
+ protected mapper: any;
3
+ protected target: string;
4
+ protected query: any;
5
+ constructor(mapper: any, target: string);
6
+ where(field: string, value: any, operator?: string): this;
7
+ whereComplex(raw: string): this;
8
+ whereRaw(raw: string): this;
9
+ limit(n: number): this;
10
+ offset(n: number): this;
11
+ }
@@ -0,0 +1,27 @@
1
+ export class ClauseBuilder {
2
+ constructor(mapper, target) {
3
+ this.mapper = mapper;
4
+ this.target = target;
5
+ // Initialize the underlying query object from the mapper core
6
+ this.query = mapper.use(target);
7
+ }
8
+ where(field, value, operator) {
9
+ this.query.where(field, value, operator);
10
+ return this;
11
+ }
12
+ whereComplex(raw) {
13
+ this.query.whereComplex(raw);
14
+ return this;
15
+ }
16
+ whereRaw(raw) {
17
+ return this.whereComplex(raw);
18
+ }
19
+ limit(n) {
20
+ this.query.limit(n);
21
+ return this;
22
+ }
23
+ offset(n) {
24
+ this.query.offset(n);
25
+ return this;
26
+ }
27
+ }
@@ -0,0 +1,25 @@
1
+ import { ClauseBuilder } from './clause-builder.js';
2
+ export declare class SelectBuilder extends ClauseBuilder {
3
+ fields: string[];
4
+ constructor(mapper: any, target: string, fields?: string[]);
5
+ get(): Promise<any[]>;
6
+ getOne(): Promise<any | null>;
7
+ }
8
+ export declare class InsertBuilder {
9
+ private mapper;
10
+ private target;
11
+ private data;
12
+ private query;
13
+ constructor(mapper: any, target: string, data: any);
14
+ private ensureSchema;
15
+ run(): Promise<any>;
16
+ }
17
+ export declare class UpdateBuilder extends ClauseBuilder {
18
+ private data;
19
+ constructor(mapper: any, target: string, data: any);
20
+ run(): Promise<void>;
21
+ }
22
+ export declare class DeleteBuilder extends ClauseBuilder {
23
+ constructor(mapper: any, target: string);
24
+ run(): Promise<void>;
25
+ }
@@ -0,0 +1,55 @@
1
+ import { ClauseBuilder } from './clause-builder.js';
2
+ export class SelectBuilder extends ClauseBuilder {
3
+ constructor(mapper, target, fields = []) {
4
+ super(mapper, target);
5
+ this.fields = fields;
6
+ if (fields.length > 0) {
7
+ this.query.selectFields(fields);
8
+ }
9
+ }
10
+ async get() {
11
+ return this.query.get();
12
+ }
13
+ async getOne() {
14
+ return this.query.getOne();
15
+ }
16
+ }
17
+ export class InsertBuilder {
18
+ constructor(mapper, target, data) {
19
+ this.mapper = mapper;
20
+ this.target = target;
21
+ this.data = data;
22
+ this.query = mapper.use(target);
23
+ // Ensure we handle auto-registration if needed (legacy behavior support)
24
+ this.ensureSchema(mapper, target);
25
+ }
26
+ ensureSchema(mapper, target) {
27
+ try {
28
+ mapper.use(target);
29
+ }
30
+ catch (e) {
31
+ mapper.getSchemaManager().create(target).use({ connection: 'default', collection: target }).setStructure({});
32
+ }
33
+ }
34
+ async run() {
35
+ return this.mapper.add(this.target, this.data);
36
+ }
37
+ }
38
+ export class UpdateBuilder extends ClauseBuilder {
39
+ constructor(mapper, target, data) {
40
+ super(mapper, target);
41
+ this.data = data;
42
+ this.query.to(data);
43
+ }
44
+ async run() {
45
+ return this.query.update();
46
+ }
47
+ }
48
+ export class DeleteBuilder extends ClauseBuilder {
49
+ constructor(mapper, target) {
50
+ super(mapper, target);
51
+ }
52
+ async run() {
53
+ return this.query.delete();
54
+ }
55
+ }
@@ -0,0 +1,8 @@
1
+ export declare class RawBuilder {
2
+ private mapper;
3
+ private sql;
4
+ private _bindings;
5
+ constructor(mapper: any, sql: string);
6
+ bind(bindings: any[] | any): this;
7
+ run(): Promise<any>;
8
+ }
@@ -0,0 +1,22 @@
1
+ export class RawBuilder {
2
+ constructor(mapper, sql) {
3
+ this.mapper = mapper;
4
+ this.sql = sql;
5
+ this._bindings = [];
6
+ }
7
+ bind(bindings) {
8
+ this._bindings = Array.isArray(bindings) ? bindings : [bindings];
9
+ return this;
10
+ }
11
+ async run() {
12
+ const connections = this.mapper.getConnections();
13
+ const conn = connections.get('default');
14
+ if (!conn)
15
+ throw new Error("No default connection found for raw query.");
16
+ const adapter = connections.getAdapter(conn.name);
17
+ if (adapter && typeof adapter.query === 'function') {
18
+ return adapter.query(this.sql, this._bindings);
19
+ }
20
+ throw new Error(`Connection '${conn.name}' does not support raw queries.`);
21
+ }
22
+ }
@@ -0,0 +1,44 @@
1
+ export declare class SchemaCreator {
2
+ private mapper;
3
+ private name;
4
+ private type;
5
+ private migrator;
6
+ constructor(mapper: any, name: string, type: string);
7
+ private ensureRegistration;
8
+ addColumn(name: string): import("../migrator.js").ColumnBuilder;
9
+ structure(config: any): this;
10
+ exec(): Promise<void>;
11
+ }
12
+ export declare class SchemaUpdater {
13
+ private mapper;
14
+ private name;
15
+ private migrator;
16
+ constructor(mapper: any, name: string);
17
+ addColumn(name: string): import("../migrator.js").ColumnBuilder;
18
+ selectColumn(name: string): import("../migrator.js").ColumnBuilder;
19
+ dropColumn(name: string): this;
20
+ exec(): Promise<void>;
21
+ }
22
+ export declare class SchemaDropper {
23
+ private name;
24
+ constructor(name: string);
25
+ exec(): Promise<void>;
26
+ }
27
+ export declare class SchemaHandler {
28
+ protected mapper: any;
29
+ protected name: string;
30
+ protected schemaType: string;
31
+ constructor(mapper: any, name: string, schemaType: string);
32
+ create(): SchemaCreator;
33
+ update(): SchemaUpdater;
34
+ drop(): SchemaDropper;
35
+ }
36
+ export declare class SchemaDispatcher {
37
+ private mapper;
38
+ private name;
39
+ constructor(mapper: any, name: string);
40
+ type(type: 'table' | 'api' | 'collection'): SchemaHandler;
41
+ create(): SchemaCreator;
42
+ update(): SchemaUpdater;
43
+ drop(): SchemaDropper;
44
+ }
@@ -0,0 +1,84 @@
1
+ import { TableMigrator } from '../migrator.js';
2
+ import { parseDescriptorStructure } from '../index.js';
3
+ export class SchemaCreator {
4
+ constructor(mapper, name, type) {
5
+ this.mapper = mapper;
6
+ this.name = name;
7
+ this.type = type;
8
+ this.migrator = new TableMigrator(name);
9
+ this.ensureRegistration();
10
+ }
11
+ ensureRegistration() {
12
+ // Register if technically missing from memory so migrator has something to work with
13
+ const manager = this.mapper.getSchemaManager();
14
+ if (!manager.schemas.has(this.name)) {
15
+ manager.create(this.name).use({ connection: 'default', collection: this.name }).setStructure({});
16
+ }
17
+ }
18
+ // DDL Methods
19
+ addColumn(name) { return this.migrator.addColumn(name); }
20
+ structure(config) {
21
+ // Apply structure config to the in-memory definition immediately for usage
22
+ const def = this.mapper.getSchemaManager().schemas.get(this.name);
23
+ if (def) {
24
+ const parsed = parseDescriptorStructure(config);
25
+ def.fields = parsed.fields;
26
+ def.fieldsMap = new Map();
27
+ def.fields.forEach((f) => def.fieldsMap.set(f.name, f));
28
+ }
29
+ return this;
30
+ }
31
+ async exec() {
32
+ return this.migrator.exec();
33
+ }
34
+ }
35
+ export class SchemaUpdater {
36
+ constructor(mapper, name) {
37
+ this.mapper = mapper;
38
+ this.name = name;
39
+ this.migrator = new TableMigrator(name);
40
+ }
41
+ addColumn(name) { return this.migrator.addColumn(name); }
42
+ selectColumn(name) { return this.migrator.selectColumn(name); }
43
+ dropColumn(name) { this.migrator.dropColumn(name); return this; }
44
+ async exec() {
45
+ return this.migrator.exec();
46
+ }
47
+ }
48
+ export class SchemaDropper {
49
+ constructor(name) {
50
+ this.name = name;
51
+ }
52
+ async exec() {
53
+ return new TableMigrator(this.name).drop().exec();
54
+ }
55
+ }
56
+ export class SchemaHandler {
57
+ constructor(mapper, name, schemaType) {
58
+ this.mapper = mapper;
59
+ this.name = name;
60
+ this.schemaType = schemaType;
61
+ }
62
+ create() {
63
+ return new SchemaCreator(this.mapper, this.name, this.schemaType);
64
+ }
65
+ update() {
66
+ return new SchemaUpdater(this.mapper, this.name);
67
+ }
68
+ drop() {
69
+ return new SchemaDropper(this.name);
70
+ }
71
+ }
72
+ export class SchemaDispatcher {
73
+ constructor(mapper, name) {
74
+ this.mapper = mapper;
75
+ this.name = name;
76
+ }
77
+ type(type) {
78
+ return new SchemaHandler(this.mapper, this.name, type);
79
+ }
80
+ // Shortcuts if type is omitted (defaults to 'table' behavior essentially)
81
+ create() { return new SchemaHandler(this.mapper, this.name, 'table').create(); }
82
+ update() { return new SchemaHandler(this.mapper, this.name, 'table').update(); }
83
+ drop() { return new SchemaHandler(this.mapper, this.name, 'table').drop(); }
84
+ }
@@ -46,7 +46,7 @@ import { Mapper, TableMigrator } from '@neupgroup/mapper';
46
46
  export const usesConnection = 'default';
47
47
 
48
48
  export async function up() {
49
- const schema = Mapper.schema().table('${tableName}');
49
+ const schema = Mapper.schema('${tableName}');
50
50
  schema.useConnection(usesConnection);
51
51
 
52
52
  /**
@@ -71,9 +71,9 @@ export async function down() {
71
71
  * DROP SCHEMA (Immediate action)
72
72
  * This will drop the schema from the DB and delete the local schema file.
73
73
  */
74
- const schema = Mapper.schema().table('${tableName}');
74
+ const schema = Mapper.schema('${tableName}');
75
75
  schema.useConnection(usesConnection);
76
- await schema.dropTable().exec();
76
+ await schema.drop().exec();
77
77
  }
78
78
  `;
79
79
  fs.writeFileSync(filePath, fileContent.trim());
@@ -40,11 +40,11 @@ export declare class Connector {
40
40
  * Define the table or collection to operate on.
41
41
  * This registers the connection (if not already) and prepares a query.
42
42
  */
43
- table(tableName: string): import("./fluent-mapper.js").FluentQueryBuilder;
43
+ table(tableName: string): import("./fluent-mapper.js").BaseDispatcher;
44
44
  /**
45
45
  * Alias for table() for API endpoints (subpath).
46
46
  */
47
- subpath(path: string): import("./fluent-mapper.js").FluentQueryBuilder;
47
+ subpath(path: string): import("./fluent-mapper.js").BaseDispatcher;
48
48
  /**
49
49
  * Finalizes the connection registration and returns a query object.
50
50
  */
package/dist/discovery.js CHANGED
@@ -49,17 +49,13 @@ export async function discover() {
49
49
  const schemaDef = mod[schemaName] || mod.schema || mod.default;
50
50
  if (schemaDef && schemaDef.fields) {
51
51
  const connectionName = schemaDef.usesConnection || 'default';
52
- StaticMapper.connection(connectionName)
53
- .schema(schemaName)
54
- .collection(schemaDef.collectionName || schemaName)
52
+ StaticMapper.schema(schemaName)
53
+ .create()
55
54
  .structure(schemaDef.fields);
56
55
  // Apply options if present
56
+ // Apply options if present
57
57
  if (schemaDef.insertableFields || schemaDef.updatableFields) {
58
- const manager = StaticMapper.getFluentMapper().mapper.getSchemaManager();
59
- const schema = manager.create(schemaName); // This might throw if exists, should use update
60
- // ... existing StaticMapper already handles this in discovery pattern?
61
- // Actually StaticMapper.schemas(schemaName) works.
62
- const wrapper = StaticMapper.schemas(schemaName);
58
+ const wrapper = StaticMapper.getFluentMapper().mapper.getSchemaManager().create(schemaName);
63
59
  if (schemaDef.insertableFields)
64
60
  wrapper.insertableFields = schemaDef.insertableFields;
65
61
  if (schemaDef.updatableFields)
@@ -1,150 +1,53 @@
1
- import { Connections, SchemaManager, type SchemaDef, ConnectionType } from './index.js';
2
- import { TableMigrator } from './migrator.js';
3
- export declare class FluentQueryBuilder {
4
- private mapper;
5
- private schemaName;
6
- private query;
7
- private _migrator?;
8
- constructor(mapper: any, schemaName: string, connectionName?: string);
9
- getDef(): SchemaDef;
10
- set fields(config: any);
11
- set insertableFields(val: string[]);
12
- set updatableFields(val: string[]);
13
- set deleteType(val: 'softDelete' | 'hardDelete');
14
- set massDeleteAllowed(val: boolean);
15
- set massEditAllowed(val: boolean);
16
- structure(config: any): this;
17
- collection(collectionName: string): this;
18
- get migrator(): TableMigrator;
19
- useConnection(name: string): this;
20
- addColumn(name: string): import("./migrator.js").ColumnBuilder;
21
- selectColumn(name: string): import("./migrator.js").ColumnBuilder;
22
- dropColumn(name: string): this;
23
- drop(): this;
24
- exec(): Promise<void>;
25
- dropTable(): Promise<void>;
26
- where(field: string, value: any, operator?: string): this;
27
- whereComplex(raw: string): this;
28
- limit(n: number): this;
29
- offset(n: number): this;
30
- to(update: Record<string, any>): this;
31
- get(...fields: string[]): any;
32
- then(onfulfilled?: ((value: any) => any) | null, onrejected?: ((reason: any) => any) | null): Promise<any>;
33
- getOne(): Promise<Record<string, any> | null>;
34
- add(data: Record<string, any>): Promise<any>;
35
- insert(data: Record<string, any>): Promise<any>;
36
- update(): Promise<void>;
37
- delete(): Promise<void>;
38
- deleteOne(): Promise<void>;
39
- updateOne(): Promise<void>;
40
- }
1
+ import { ConnectionType } from './index.js';
2
+ import { BaseDispatcher } from './builders/base-dispatcher.js';
3
+ import { RawBuilder } from './builders/raw-builder.js';
4
+ import { SchemaDispatcher } from './builders/schema-builders.js';
5
+ export { BaseDispatcher, RawBuilder, SchemaDispatcher };
6
+ export * from './builders/dml-builders.js';
7
+ export * from './builders/schema-builders.js';
8
+ export * from './builders/raw-builder.js';
9
+ export * from './builders/clause-builder.js';
41
10
  export declare class FluentConnectionBuilder {
11
+ private name;
12
+ private type;
42
13
  private mapper;
43
- private connectionName;
44
- private connectionType;
45
- private connectionConfig;
46
- constructor(mapper: any, connectionName: string, connectionType: string, config: Record<string, any>);
47
- schema(schemaName: string): FluentSchemaBuilder;
48
- query(schemaName: string): FluentQueryBuilder;
49
- useConnection(connectionName: string): FluentConnectionSelector;
50
- }
51
- export declare class FluentSchemaBuilder {
52
- private mapper;
53
- private schemaName;
54
- private connectionName;
55
- constructor(mapper: any, schemaName: string, connectionName: string);
56
- collection(collectionName: string): FluentSchemaCollectionBuilder;
57
- }
58
- export declare class FluentSchemaCollectionBuilder {
59
- private mapper;
60
- private schemaName;
61
- private connectionName;
62
- private collectionName;
63
- constructor(mapper: any, schemaName: string, connectionName: string, collectionName: string);
64
- structure(structure: Record<string, string> | Array<{
65
- name: string;
66
- type: string;
67
- [key: string]: any;
68
- }>): FluentMapper;
69
- }
70
- export declare class FluentApiRequestBuilder {
71
- private mapper;
72
- private connectionName;
73
- private _path;
74
- private _headers;
75
- constructor(mapper: any, connectionName: string, path?: string);
76
- path(p: string): this;
77
- header(key: string | Record<string, string | string[]>, value?: string | string[]): this;
78
- headers(h: Record<string, string | string[]> | any[]): this;
79
- get(): Promise<any>;
80
- post(data?: any): Promise<any>;
81
- put(data?: any): Promise<any>;
82
- patch(data?: any): Promise<any>;
83
- delete(): Promise<any>;
84
- private execute;
14
+ constructor(mapper: any, name: string, type: ConnectionType, config: Record<string, any>);
15
+ base(target: string): BaseDispatcher;
16
+ query(target: string): BaseDispatcher;
85
17
  }
86
18
  export declare class FluentConnectionSelector {
87
19
  private mapper;
88
- private connectionName;
89
- constructor(mapper: any, connectionName: string);
90
- schema(schemaName: string): FluentQueryBuilder;
91
- schemas(schemaName: string): FluentQueryBuilder;
92
- query(schemaName: string): FluentQueryBuilder;
93
- table(tableName: string): FluentQueryBuilder;
94
- collection(collectionName: string): FluentQueryBuilder;
95
- path(path: string): FluentApiRequestBuilder;
96
- header(key: string | Record<string, string | string[]>, value?: string | string[]): FluentApiRequestBuilder;
97
- headers(headers: Record<string, string> | any[]): FluentApiRequestBuilder;
98
- get(): Promise<any>;
99
- post(data?: any): Promise<any>;
100
- put(data?: any): Promise<any>;
101
- patch(data?: any): Promise<any>;
102
- delete(): Promise<any>;
20
+ private name;
21
+ constructor(mapper: any, name: string);
22
+ base(target: string): BaseDispatcher;
23
+ query(target: string): BaseDispatcher;
103
24
  }
104
25
  export declare class FluentMapper {
105
26
  private mapper;
106
27
  constructor(mapper: any);
107
- query(schemaName: string): FluentQueryBuilder;
108
- schema(name: string): FluentQueryBuilder;
109
- table(name: string): FluentQueryBuilder;
28
+ base(target: string): BaseDispatcher;
29
+ query(target: string): BaseDispatcher;
30
+ schema(name: string): SchemaDispatcher;
31
+ raw(sql: string): RawBuilder;
110
32
  makeConnection(name: string, type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
111
- useConnection(connectionName: string): FluentConnectionSelector;
112
- connection(connectionOrConfig: string | Record<string, any>): FluentConnectionSelector;
113
- makeTempConnection(type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
114
- get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
115
- getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
116
- add(schemaName: string, data: Record<string, any>): Promise<any>;
117
- update(schemaName: string, filters: Record<string, any>, data: Record<string, any>): Promise<void>;
118
- delete(schemaName: string, filters: Record<string, any>): Promise<void>;
119
- dropTable(name: string): Promise<void>;
33
+ connection(configOrName: string | any): FluentConnectionSelector;
34
+ get(target: string): Promise<any>;
35
+ add(target: string, data: any): Promise<any>;
36
+ update(target: string, filter: any, data: any): Promise<any>;
37
+ delete(target: string, filter: any): Promise<any>;
120
38
  }
121
39
  export declare class StaticMapper {
122
40
  private static instance;
123
41
  private static getFluentMapper;
124
- static makeConnection(name: string, type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
125
- static makeTempConnection(type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
126
- static query(schemaName: string): FluentQueryBuilder;
127
- static schema(name: string): FluentQueryBuilder;
128
- static schema(): SchemaManagerWrapper;
129
- static table(name: string): FluentQueryBuilder;
130
- static connection(connectionOrConfig: string | Record<string, any>): FluentConnectionSelector;
131
- static useConnection(connectionName: string): FluentConnectionSelector;
132
- static schemas(name?: string): any;
133
- static get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
134
- static getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
135
- static add(schemaName: string, data: Record<string, any>): Promise<any>;
136
- static update(schemaName: string, filters: Record<string, any>, data: Record<string, any>): Promise<void>;
137
- static delete(schemaName: string, filters: Record<string, any>): Promise<void>;
138
- static dropTable(name: string): Promise<void>;
139
- static getConnections(): Connections;
42
+ static base(target: string): BaseDispatcher;
43
+ static query(target: string): BaseDispatcher;
44
+ static schema(name: string): SchemaDispatcher;
45
+ static raw(sql: string): RawBuilder;
46
+ static connection(arg: any): FluentConnectionSelector;
47
+ static makeConnection(n: string, t: ConnectionType, c: any): FluentConnectionBuilder;
140
48
  static discover(): Promise<void>;
49
+ static get(t: string): Promise<any>;
50
+ static add(t: string, d: any): Promise<any>;
141
51
  }
142
52
  export declare const Mapper: typeof StaticMapper;
143
53
  export default Mapper;
144
- export declare class SchemaManagerWrapper {
145
- private manager;
146
- constructor(manager: SchemaManager);
147
- table(name: string): TableMigrator;
148
- schema(name: string): TableMigrator;
149
- dropTable(name: string): Promise<void>;
150
- }
@@ -1,470 +1,110 @@
1
1
  import { createMapper } from './mapper.js';
2
- import { TableMigrator } from './migrator.js';
3
- export class FluentQueryBuilder {
4
- constructor(mapper, schemaName, connectionName) {
5
- this.mapper = mapper;
6
- this.schemaName = 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();
97
- }
98
- where(field, value, operator) {
99
- this.query.where(field, value, operator);
100
- return this;
101
- }
102
- whereComplex(raw) {
103
- this.query.whereComplex(raw);
104
- return this;
105
- }
106
- limit(n) {
107
- this.query.limit(n);
108
- return this;
109
- }
110
- offset(n) {
111
- this.query.offset(n);
112
- return this;
113
- }
114
- to(update) {
115
- this.query.to(update);
116
- return this;
117
- }
118
- // If args provided, act as SELECT (projection) and return this.
119
- // If no args, act as execute() (but this class is thenable so we can just return this if we want consistency,
120
- // but existing API returns Promise directly. To check user intent:
121
- // User: get('f1').limit(1).
122
- // So get('f1') must return this.
123
- get(...fields) {
124
- if (fields.length > 0) {
125
- // Apply field selection? SchemaQuery needs a way to filter fields.
126
- // We'll add this capability to Field filtering in SchemaQuery or just use 'fields' option in buildOptions.
127
- // For now, let's assume we can modify the query's field list.
128
- this.query.selectFields(fields);
129
- return this;
130
- }
131
- return this.query.get(); // Promise
132
- }
133
- // Make the builder thenable
134
- then(onfulfilled, onrejected) {
135
- return this.query.get().then(onfulfilled, onrejected);
136
- }
137
- async getOne() {
138
- return this.query.getOne();
139
- }
140
- async add(data) {
141
- return this.mapper.add(this.schemaName, data);
142
- }
143
- async insert(data) {
144
- return this.add(data);
145
- }
146
- async update() {
147
- return this.query.update();
148
- }
149
- async delete() {
150
- return this.query.delete();
151
- }
152
- async deleteOne() {
153
- return this.query.deleteOne();
154
- }
155
- async updateOne() {
156
- return this.query.updateOne();
157
- }
158
- }
2
+ // Import segregated builders
3
+ import { BaseDispatcher } from './builders/base-dispatcher.js';
4
+ import { RawBuilder } from './builders/raw-builder.js';
5
+ import { SchemaDispatcher } from './builders/schema-builders.js';
6
+ // Re-export for external usage
7
+ export { BaseDispatcher, RawBuilder, SchemaDispatcher };
8
+ export * from './builders/dml-builders.js';
9
+ export * from './builders/schema-builders.js';
10
+ export * from './builders/raw-builder.js';
11
+ export * from './builders/clause-builder.js';
12
+ // =========================================================================================
13
+ // CONNECTION & MAPPER WRAPPERS
14
+ // =========================================================================================
159
15
  export class FluentConnectionBuilder {
160
- constructor(mapper, connectionName, connectionType, config) {
16
+ constructor(mapper, name, type, config) {
17
+ this.name = name;
18
+ this.type = type;
161
19
  this.mapper = mapper;
162
- this.connectionName = connectionName;
163
- this.connectionType = connectionType;
164
- this.connectionConfig = config;
165
- // Create the connection immediately
166
- this.mapper.connect(connectionName, connectionType, config);
167
- }
168
- schema(schemaName) {
169
- return new FluentSchemaBuilder(this.mapper, schemaName, this.connectionName);
170
- }
171
- query(schemaName) {
172
- return new FluentQueryBuilder(this.mapper, schemaName);
173
- }
174
- useConnection(connectionName) {
175
- return new FluentConnectionSelector(this.mapper, connectionName);
176
- }
177
- }
178
- export class FluentSchemaBuilder {
179
- constructor(mapper, schemaName, connectionName) {
180
- this.mapper = mapper;
181
- this.schemaName = schemaName;
182
- this.connectionName = connectionName;
183
- }
184
- collection(collectionName) {
185
- return new FluentSchemaCollectionBuilder(this.mapper, this.schemaName, this.connectionName, collectionName);
186
- }
187
- }
188
- export class FluentSchemaCollectionBuilder {
189
- constructor(mapper, schemaName, connectionName, collectionName) {
190
- this.mapper = mapper;
191
- this.schemaName = schemaName;
192
- this.connectionName = connectionName;
193
- this.collectionName = collectionName;
194
- }
195
- structure(structure) {
196
- this.mapper.schema(this.schemaName)
197
- .use({ connection: this.connectionName, collection: this.collectionName })
198
- .setStructure(structure);
199
- return new FluentMapper(this.mapper);
200
- }
201
- }
202
- export class FluentApiRequestBuilder {
203
- constructor(mapper, connectionName, path = '') {
204
- this.mapper = mapper;
205
- this.connectionName = connectionName;
206
- this._path = '';
207
- this._headers = {};
208
- this._path = path;
209
- }
210
- path(p) {
211
- if (this._path === '') {
212
- this._path = p;
213
- }
214
- else {
215
- if (!p.startsWith('/') && !this._path.endsWith('/')) {
216
- this._path += '/';
217
- }
218
- this._path += p;
219
- }
220
- return this;
20
+ this.mapper.connect(name, type, config);
221
21
  }
222
- header(key, value) {
223
- if (typeof key === 'object') {
224
- Object.entries(key).forEach(([k, v]) => this.header(k, v));
225
- }
226
- else if (value !== undefined) {
227
- if (this._headers[key]) {
228
- const existing = this._headers[key];
229
- if (Array.isArray(existing)) {
230
- if (Array.isArray(value)) {
231
- existing.push(...value);
232
- }
233
- else {
234
- existing.push(value);
235
- }
236
- }
237
- else {
238
- this._headers[key] = [existing, ...(Array.isArray(value) ? value : [value])];
239
- }
240
- }
241
- else {
242
- this._headers[key] = value;
243
- }
244
- }
245
- else {
246
- // Check for "Key: Value" string
247
- if (key.includes(':')) {
248
- const [k, ...v] = key.split(':');
249
- this.header(k.trim(), v.join(':').trim());
250
- }
251
- }
252
- return this;
22
+ base(target) {
23
+ return new BaseDispatcher(this.mapper, target);
253
24
  }
254
- headers(h) {
255
- if (Array.isArray(h)) {
256
- h.forEach(item => this.header(item));
257
- }
258
- else {
259
- this.header(h);
260
- }
261
- return this;
262
- }
263
- async get() { return this.execute('GET'); }
264
- async post(data) { return this.execute('POST', data); }
265
- async put(data) { return this.execute('PUT', data); }
266
- async patch(data) { return this.execute('PATCH', data); }
267
- async delete() { return this.execute('DELETE'); }
268
- async execute(method, data) {
269
- const adapter = this.mapper.getConnections().getAdapter(this.connectionName);
270
- if (!adapter || typeof adapter.request !== 'function') {
271
- throw new Error(`Connection "${this.connectionName}" does not support custom requests.`);
272
- }
273
- return adapter.request(method, this._path, data, this._headers);
25
+ query(target) {
26
+ return this.base(target);
274
27
  }
275
28
  }
276
29
  export class FluentConnectionSelector {
277
- constructor(mapper, connectionName) {
30
+ constructor(mapper, name) {
278
31
  this.mapper = mapper;
279
- this.connectionName = connectionName;
280
- }
281
- schema(schemaName) {
282
- return new FluentQueryBuilder(this.mapper, schemaName, this.connectionName);
283
- }
284
- schemas(schemaName) {
285
- return this.schema(schemaName);
286
- }
287
- query(schemaName) {
288
- return this.schema(schemaName);
289
- }
290
- table(tableName) {
291
- return this.schema(tableName);
292
- }
293
- collection(collectionName) {
294
- return this.schema(collectionName);
32
+ this.name = name;
295
33
  }
296
- // API Request methods
297
- path(path) {
298
- return new FluentApiRequestBuilder(this.mapper, this.connectionName, path);
34
+ base(target) {
35
+ // Determine how to pass context? BaseDispatcher usually gets just mapper and target.
36
+ // If we need specific connection, we might need to assume 'useConnection' behavior.
37
+ // For now, simple forwarding.
38
+ return new BaseDispatcher(this.mapper, target);
299
39
  }
300
- header(key, value) {
301
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).header(key, value);
302
- }
303
- headers(headers) {
304
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).headers(headers);
305
- }
306
- get() {
307
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).get();
308
- }
309
- post(data) {
310
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).post(data);
311
- }
312
- put(data) {
313
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).put(data);
314
- }
315
- patch(data) {
316
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).patch(data);
317
- }
318
- delete() {
319
- return new FluentApiRequestBuilder(this.mapper, this.connectionName).delete();
40
+ query(target) {
41
+ return this.base(target);
320
42
  }
321
43
  }
322
44
  export class FluentMapper {
323
45
  constructor(mapper) {
324
46
  this.mapper = mapper;
325
47
  }
326
- query(schemaName) {
327
- return new FluentQueryBuilder(this.mapper, schemaName);
48
+ // Entry Point: BASE (DML)
49
+ base(target) {
50
+ return new BaseDispatcher(this.mapper, target);
51
+ }
52
+ // Entry Point: QUERY (Alias for Base)
53
+ query(target) {
54
+ return this.base(target);
328
55
  }
56
+ // Entry Point: SCHEMA (DDL)
329
57
  schema(name) {
330
- return this.query(name);
58
+ return new SchemaDispatcher(this.mapper, name);
331
59
  }
332
- table(name) {
333
- return this.query(name);
60
+ // Entry Point: RAW
61
+ raw(sql) {
62
+ return new RawBuilder(this.mapper, sql);
334
63
  }
64
+ // Connection Management
335
65
  makeConnection(name, type, config) {
336
66
  return new FluentConnectionBuilder(this.mapper, name, type, config);
337
67
  }
338
- // Deprecated: use connection() instead
339
- useConnection(connectionName) {
340
- return new FluentConnectionSelector(this.mapper, connectionName);
341
- }
342
- connection(connectionOrConfig) {
343
- if (typeof connectionOrConfig === 'string') {
344
- return new FluentConnectionSelector(this.mapper, connectionOrConfig);
345
- }
346
- // Handle object config: create temp connection
347
- // Expected format: { type: '...', ...others }
348
- const type = connectionOrConfig.type;
349
- if (!type) {
350
- throw new Error("Connection configuration must specify 'type'");
351
- }
352
- const config = { ...connectionOrConfig };
353
- delete config.type;
354
- // Create temp connection
355
- const tempName = `temp_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
356
- new FluentConnectionBuilder(this.mapper, tempName, type, config); // Registers it
357
- return new FluentConnectionSelector(this.mapper, tempName);
358
- }
359
- makeTempConnection(type, config) {
360
- const tempName = `temp_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
361
- return new FluentConnectionBuilder(this.mapper, tempName, type, config);
362
- }
363
- // Direct query methods for quick usage
364
- async get(schemaName, filters) {
365
- return this.mapper.get(schemaName, filters);
366
- }
367
- async getOne(schemaName, filters) {
368
- return this.mapper.getOne(schemaName, filters);
369
- }
370
- async add(schemaName, data) {
371
- return this.mapper.add(schemaName, data);
372
- }
373
- async update(schemaName, filters, data) {
374
- return this.mapper.update(schemaName, filters, data);
375
- }
376
- async delete(schemaName, filters) {
377
- return this.mapper.delete(schemaName, filters);
378
- }
379
- async dropTable(name) {
380
- return new TableMigrator(name).drop().exec();
68
+ connection(configOrName) {
69
+ if (typeof configOrName === 'string')
70
+ return new FluentConnectionSelector(this.mapper, configOrName);
71
+ // Temp connection logic could go here
72
+ return new FluentConnectionSelector(this.mapper, 'default');
73
+ }
74
+ // Direct shortcuts (Legacy support maintenance)
75
+ async get(target) { return this.base(target).select().get(); }
76
+ async add(target, data) { return this.base(target).insert(data).run(); }
77
+ async update(target, filter, data) {
78
+ let b = this.base(target).update(data);
79
+ Object.keys(filter).forEach(k => b.where(k, filter[k]));
80
+ return b.run();
81
+ }
82
+ async delete(target, filter) {
83
+ let b = this.base(target).delete();
84
+ Object.keys(filter).forEach(k => b.where(k, filter[k]));
85
+ return b.run();
381
86
  }
382
87
  }
383
- // Static API class that provides the fluent interface
384
88
  export class StaticMapper {
385
89
  static getFluentMapper() {
386
90
  if (!StaticMapper.instance) {
387
- const baseMapper = createMapper();
388
- StaticMapper.instance = new FluentMapper(baseMapper);
91
+ StaticMapper.instance = new FluentMapper(createMapper());
389
92
  }
390
93
  return StaticMapper.instance;
391
94
  }
392
- static makeConnection(name, type, config) {
393
- return StaticMapper.getFluentMapper().makeConnection(name, type, config);
394
- }
395
- static makeTempConnection(type, config) {
396
- return StaticMapper.getFluentMapper().makeTempConnection(type, config);
397
- }
398
- static query(schemaName) {
399
- return StaticMapper.getFluentMapper().query(schemaName);
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
- }
409
- // New API
410
- static connection(connectionOrConfig) {
411
- return StaticMapper.getFluentMapper().connection(connectionOrConfig);
412
- }
413
- // Deprecated alias
414
- static useConnection(connectionName) {
415
- return StaticMapper.connection(connectionName);
416
- }
417
- static schemas(name) {
418
- if (name)
419
- return StaticMapper.schema(name);
420
- return new SchemaManagerWrapper(StaticMapper.getFluentMapper().mapper.getSchemaManager());
421
- }
422
- // Direct static methods
423
- static async get(schemaName, filters) {
424
- return StaticMapper.getFluentMapper().get(schemaName, filters);
425
- }
426
- static async getOne(schemaName, filters) {
427
- return StaticMapper.getFluentMapper().getOne(schemaName, filters);
428
- }
429
- static async add(schemaName, data) {
430
- return StaticMapper.getFluentMapper().add(schemaName, data);
431
- }
432
- static async update(schemaName, filters, data) {
433
- return StaticMapper.getFluentMapper().update(schemaName, filters, data);
434
- }
435
- static async delete(schemaName, filters) {
436
- return StaticMapper.getFluentMapper().delete(schemaName, filters);
437
- }
438
- static async dropTable(name) {
439
- return StaticMapper.getFluentMapper().dropTable(name);
440
- }
441
- static getConnections() {
442
- return StaticMapper.getFluentMapper().mapper.getConnections();
443
- }
95
+ static base(target) { return StaticMapper.getFluentMapper().base(target); }
96
+ static query(target) { return StaticMapper.getFluentMapper().query(target); }
97
+ static schema(name) { return StaticMapper.getFluentMapper().schema(name); }
98
+ static raw(sql) { return StaticMapper.getFluentMapper().raw(sql); }
99
+ static connection(arg) { return StaticMapper.getFluentMapper().connection(arg); }
100
+ static makeConnection(n, t, c) { return StaticMapper.getFluentMapper().makeConnection(n, t, c); }
444
101
  static async discover() {
445
102
  const { discover } = await import('./discovery.js');
446
103
  return discover();
447
104
  }
105
+ // Helpers for simple usage
106
+ static async get(t) { return StaticMapper.getFluentMapper().get(t); }
107
+ static async add(t, d) { return StaticMapper.getFluentMapper().add(t, d); }
448
108
  }
449
- // Export a default instance for convenience
450
109
  export const Mapper = StaticMapper;
451
110
  export default Mapper;
452
- // Helper to access parseDescriptorStructure from index.ts if not exported?
453
- // It is NOT exported. I need to export it or duplicate logic.
454
- // I'll export it from index.ts.
455
- import { parseDescriptorStructure } from './index.js'; // fixed import at bottom
456
- export class SchemaManagerWrapper {
457
- constructor(manager) {
458
- this.manager = manager;
459
- }
460
- table(name) {
461
- // This allows Mapper.schemas().table('name') to return a migrator
462
- return new TableMigrator(name);
463
- }
464
- schema(name) {
465
- return this.table(name);
466
- }
467
- async dropTable(name) {
468
- return new TableMigrator(name).drop().exec();
469
- }
470
- }
package/dist/index.d.ts CHANGED
@@ -127,8 +127,8 @@ export type { EnvDslConnections, NormalizedConnection } from './env.js';
127
127
  export { documentationMd, markdownToHtml, getDocumentationHtml } from './docs.js';
128
128
  export { Mapper, createMapper } from './mapper.js';
129
129
  export { default } from './mapper.js';
130
- export { StaticMapper } from './fluent-mapper.js';
131
- export type { FluentQueryBuilder, FluentConnectionBuilder, FluentSchemaBuilder, FluentSchemaCollectionBuilder, FluentConnectionSelector, FluentMapper } from './fluent-mapper.js';
130
+ export { StaticMapper, RawBuilder, BaseDispatcher } from './fluent-mapper.js';
131
+ export type { FluentConnectionBuilder, 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
134
  export { MySQLAdapter, createMySQLAdapter, PostgreSQLAdapter, createPostgreSQLAdapter, MongoDBAdapter, createMongoDBAdapter, APIAdapter, createAPIAdapter, SQLiteAdapter, createSQLiteAdapter, createAdapter, createAdapterFromUrl, autoAttachAdapter } from './adapters/index.js';
package/dist/index.js CHANGED
@@ -396,8 +396,7 @@ export { documentationMd, markdownToHtml, getDocumentationHtml } from './docs.js
396
396
  // Export the simplified Mapper and default instance
397
397
  export { Mapper, createMapper } from './mapper.js';
398
398
  export { default } from './mapper.js';
399
- // Export the new fluent/static API
400
- export { StaticMapper } from './fluent-mapper.js';
399
+ export { StaticMapper, RawBuilder, BaseDispatcher } from './fluent-mapper.js';
401
400
  // Export the new config-based system
402
401
  export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config.js';
403
402
  // Export database adapters
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@neupgroup/mapper",
3
3
  "description": "Neup.Mapper core library for schema and mapping utilities",
4
- "version": "1.6.2",
4
+ "version": "1.7.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",