@neupgroup/mapper 1.2.3 → 1.2.4

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,3 +1,4 @@
1
+ import { SchemaManager } from './index.js';
1
2
  export declare class FluentQueryBuilder {
2
3
  private mapper;
3
4
  private schemaName;
@@ -5,10 +6,14 @@ export declare class FluentQueryBuilder {
5
6
  constructor(mapper: any, schemaName: string);
6
7
  where(field: string, value: any, operator?: string): this;
7
8
  whereComplex(raw: string): this;
9
+ limit(n: number): this;
10
+ offset(n: number): this;
8
11
  to(update: Record<string, any>): this;
9
- get(): Promise<Record<string, any>[]>;
12
+ get(...fields: string[]): any;
13
+ then(onfulfilled?: ((value: any) => any) | null, onrejected?: ((reason: any) => any) | null): Promise<any>;
10
14
  getOne(): Promise<Record<string, any> | null>;
11
15
  add(data: Record<string, any>): Promise<any>;
16
+ insert(data: Record<string, any>): Promise<any>;
12
17
  update(): Promise<void>;
13
18
  delete(): Promise<void>;
14
19
  deleteOne(): Promise<void>;
@@ -49,6 +54,9 @@ export declare class FluentConnectionSelector {
49
54
  constructor(mapper: any, connectionName: string);
50
55
  schema(schemaName: string): FluentSchemaBuilder;
51
56
  query(schemaName: string): FluentQueryBuilder;
57
+ table(tableName: string): FluentQueryBuilder;
58
+ collection(collectionName: string): FluentQueryBuilder;
59
+ schemas(schemaName: string): FluentSchemaWrapper;
52
60
  }
53
61
  export declare class FluentMapper {
54
62
  private mapper;
@@ -56,6 +64,7 @@ export declare class FluentMapper {
56
64
  query(schemaName: string): FluentQueryBuilder;
57
65
  makeConnection(name: string, type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
58
66
  useConnection(connectionName: string): FluentConnectionSelector;
67
+ connection(connectionOrConfig: string | Record<string, any>): FluentConnectionSelector;
59
68
  makeTempConnection(type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
60
69
  get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
61
70
  getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
@@ -67,9 +76,11 @@ export declare class StaticMapper {
67
76
  private static instance;
68
77
  private static getFluentMapper;
69
78
  static makeConnection(name: string, type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
70
- static useConnection(connectionName: string): FluentConnectionSelector;
71
79
  static makeTempConnection(type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
72
80
  static query(schemaName: string): FluentQueryBuilder;
81
+ static connection(connectionOrConfig: string | Record<string, any>): FluentConnectionSelector;
82
+ static useConnection(connectionName: string): FluentConnectionSelector;
83
+ static schemas(name: string): FluentSchemaWrapper;
73
84
  static get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
74
85
  static getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
75
86
  static add(schemaName: string, data: Record<string, any>): Promise<any>;
@@ -78,3 +89,20 @@ export declare class StaticMapper {
78
89
  }
79
90
  export declare const Mapper: typeof StaticMapper;
80
91
  export default Mapper;
92
+ export declare class FluentSchemaWrapper {
93
+ private manager;
94
+ private name;
95
+ private connectionName?;
96
+ constructor(manager: SchemaManager, name: string, connectionName?: string | undefined);
97
+ private getDef;
98
+ set fields(config: any);
99
+ set insertableFields(val: string[]);
100
+ set updatableFields(val: string[]);
101
+ set deleteType(val: 'softDelete' | 'hardDelete');
102
+ set massDeleteAllowed(val: boolean);
103
+ set massEditAllowed(val: boolean);
104
+ get(...fields: string[]): any;
105
+ limit(n: number): FluentQueryBuilder;
106
+ offset(n: number): FluentQueryBuilder;
107
+ insert(data: Record<string, any>): Promise<any>;
108
+ }
@@ -13,12 +13,36 @@ export class FluentQueryBuilder {
13
13
  this.query.whereComplex(raw);
14
14
  return this;
15
15
  }
16
+ limit(n) {
17
+ this.query.limit(n);
18
+ return this;
19
+ }
20
+ offset(n) {
21
+ this.query.offset(n);
22
+ return this;
23
+ }
16
24
  to(update) {
17
25
  this.query.to(update);
18
26
  return this;
19
27
  }
20
- async get() {
21
- return this.query.get();
28
+ // If args provided, act as SELECT (projection) and return this.
29
+ // If no args, act as execute() (but this class is thenable so we can just return this if we want consistency,
30
+ // but existing API returns Promise directly. To check user intent:
31
+ // User: get('f1').limit(1).
32
+ // So get('f1') must return this.
33
+ get(...fields) {
34
+ if (fields.length > 0) {
35
+ // Apply field selection? SchemaQuery needs a way to filter fields.
36
+ // We'll add this capability to Field filtering in SchemaQuery or just use 'fields' option in buildOptions.
37
+ // For now, let's assume we can modify the query's field list.
38
+ this.query.selectFields(fields);
39
+ return this;
40
+ }
41
+ return this.query.get(); // Promise
42
+ }
43
+ // Make the builder thenable
44
+ then(onfulfilled, onrejected) {
45
+ return this.query.get().then(onfulfilled, onrejected);
22
46
  }
23
47
  async getOne() {
24
48
  return this.query.getOne();
@@ -26,6 +50,9 @@ export class FluentQueryBuilder {
26
50
  async add(data) {
27
51
  return this.mapper.add(this.schemaName, data);
28
52
  }
53
+ async insert(data) {
54
+ return this.add(data);
55
+ }
29
56
  async update() {
30
57
  return this.query.update();
31
58
  }
@@ -93,6 +120,15 @@ export class FluentConnectionSelector {
93
120
  query(schemaName) {
94
121
  return new FluentQueryBuilder(this.mapper, schemaName);
95
122
  }
123
+ table(tableName) {
124
+ return this.query(tableName);
125
+ }
126
+ collection(collectionName) {
127
+ return this.query(collectionName);
128
+ }
129
+ schemas(schemaName) {
130
+ return new FluentSchemaWrapper(this.mapper.getSchemaManager(), schemaName, this.connectionName);
131
+ }
96
132
  }
97
133
  export class FluentMapper {
98
134
  constructor(mapper) {
@@ -104,9 +140,27 @@ export class FluentMapper {
104
140
  makeConnection(name, type, config) {
105
141
  return new FluentConnectionBuilder(this.mapper, name, type, config);
106
142
  }
143
+ // Deprecated: use connection() instead
107
144
  useConnection(connectionName) {
108
145
  return new FluentConnectionSelector(this.mapper, connectionName);
109
146
  }
147
+ connection(connectionOrConfig) {
148
+ if (typeof connectionOrConfig === 'string') {
149
+ return new FluentConnectionSelector(this.mapper, connectionOrConfig);
150
+ }
151
+ // Handle object config: create temp connection
152
+ // Expected format: { type: '...', ...others }
153
+ const type = connectionOrConfig.type;
154
+ if (!type) {
155
+ throw new Error("Connection configuration must specify 'type'");
156
+ }
157
+ const config = { ...connectionOrConfig };
158
+ delete config.type;
159
+ // Create temp connection
160
+ const tempName = `temp_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
161
+ new FluentConnectionBuilder(this.mapper, tempName, type, config); // Registers it
162
+ return new FluentConnectionSelector(this.mapper, tempName);
163
+ }
110
164
  makeTempConnection(type, config) {
111
165
  const tempName = `temp_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
112
166
  return new FluentConnectionBuilder(this.mapper, tempName, type, config);
@@ -140,15 +194,24 @@ export class StaticMapper {
140
194
  static makeConnection(name, type, config) {
141
195
  return StaticMapper.getFluentMapper().makeConnection(name, type, config);
142
196
  }
143
- static useConnection(connectionName) {
144
- return StaticMapper.getFluentMapper().useConnection(connectionName);
145
- }
146
197
  static makeTempConnection(type, config) {
147
198
  return StaticMapper.getFluentMapper().makeTempConnection(type, config);
148
199
  }
149
200
  static query(schemaName) {
150
201
  return StaticMapper.getFluentMapper().query(schemaName);
151
202
  }
203
+ // New API
204
+ static connection(connectionOrConfig) {
205
+ return StaticMapper.getFluentMapper().connection(connectionOrConfig);
206
+ }
207
+ // Deprecated alias
208
+ static useConnection(connectionName) {
209
+ return StaticMapper.connection(connectionName);
210
+ }
211
+ static schemas(name) {
212
+ return new FluentSchemaWrapper(StaticMapper.getFluentMapper().mapper.getSchemaManager(), // Access underlying manager
213
+ name);
214
+ }
152
215
  // Direct static methods
153
216
  static async get(schemaName, filters) {
154
217
  return StaticMapper.getFluentMapper().get(schemaName, filters);
@@ -169,3 +232,92 @@ export class StaticMapper {
169
232
  // Export a default instance for convenience
170
233
  export const Mapper = StaticMapper;
171
234
  export default Mapper;
235
+ export class FluentSchemaWrapper {
236
+ // builder unused
237
+ constructor(manager, name, connectionName) {
238
+ this.manager = manager;
239
+ this.name = name;
240
+ this.connectionName = connectionName;
241
+ // Ensure schema exists or create it?
242
+ // User pattern: Mapper.schemas('name').fields = ...
243
+ // So we likely need to create it if missing, or update it.
244
+ try {
245
+ this.manager.create(name).use({ connection: connectionName || 'default', collection: name }).setStructure({});
246
+ }
247
+ catch (e) {
248
+ // Ignore if exists, but maybe update connection if strictly provided?
249
+ // Use existing definition if available.
250
+ }
251
+ }
252
+ getDef() {
253
+ // Access private map from manager? Or expose a get method.
254
+ // Manager has .schemas map.
255
+ return this.manager.schemas.get(this.name);
256
+ }
257
+ set fields(config) {
258
+ // Update schema structure
259
+ const builder = new FluentSchemaBuilder(null, this.name, ''); // Dummy wrapper or use internal
260
+ // Easier: use Manager.create(name) returns SchemaBuilder which has setStructure.
261
+ // But if it exists, create() throws.
262
+ // We need 'update' or direct access.
263
+ // Let's hack: re-register or update def.
264
+ const def = this.getDef();
265
+ if (def) {
266
+ // Re-parse
267
+ const parsed = parseDescriptorStructure(config);
268
+ def.fields = parsed.fields;
269
+ def.fieldsMap = new Map();
270
+ def.fields.forEach((f) => def.fieldsMap.set(f.name, f));
271
+ def.allowUndefinedFields = parsed.allowUndefinedFields;
272
+ }
273
+ }
274
+ set insertableFields(val) {
275
+ const def = this.getDef();
276
+ if (def)
277
+ def.insertableFields = val;
278
+ }
279
+ set updatableFields(val) {
280
+ const def = this.getDef();
281
+ if (def)
282
+ def.updatableFields = val;
283
+ }
284
+ set deleteType(val) {
285
+ const def = this.getDef();
286
+ if (def)
287
+ def.deleteType = val;
288
+ }
289
+ set massDeleteAllowed(val) {
290
+ const def = this.getDef();
291
+ if (def)
292
+ def.massDeleteAllowed = val;
293
+ }
294
+ set massEditAllowed(val) {
295
+ const def = this.getDef();
296
+ if (def)
297
+ def.massEditAllowed = val;
298
+ }
299
+ // Delegation to QueryBuilder
300
+ get(...fields) {
301
+ const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
302
+ return q.get(...fields);
303
+ }
304
+ limit(n) {
305
+ const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
306
+ return q.limit(n);
307
+ }
308
+ offset(n) {
309
+ const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
310
+ return q.offset(n);
311
+ }
312
+ async insert(data) {
313
+ const q = new FluentQueryBuilder({
314
+ use: (n) => this.manager.use(n),
315
+ add: (n, d) => this.manager.use(n).add(d)
316
+ }, this.name);
317
+ return q.insert(data);
318
+ }
319
+ }
320
+ // Helper to access parseDescriptorStructure from index.ts if not exported?
321
+ // It is NOT exported. I need to export it or duplicate logic.
322
+ // I'll export it from index.ts.
323
+ import { parseDescriptorStructure } from './index.js'; // fixed import at bottom
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { DbAdapter, QueryOptions } from './orm';
2
- import Mapper from './mapper';
1
+ import type { DbAdapter, QueryOptions } from './orm/index.js';
3
2
  export type ColumnType = 'string' | 'number' | 'boolean' | 'date' | 'int';
4
3
  export type ConnectionType = 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api';
5
4
  export interface Field {
@@ -8,14 +7,25 @@ export interface Field {
8
7
  editable?: boolean;
9
8
  autoIncrement?: boolean;
10
9
  nullable?: boolean;
11
- defaultValue?: unknown;
10
+ defaultValue?: any;
11
+ isUnique?: boolean;
12
+ isForeignKey?: boolean;
13
+ foreignRef?: string;
14
+ enumValues?: any[];
15
+ config?: any[];
12
16
  }
13
17
  export interface SchemaDef {
14
18
  name: string;
15
19
  connectionName: string;
16
20
  collectionName: string;
17
21
  fields: Field[];
22
+ fieldsMap: Map<string, Field>;
18
23
  allowUndefinedFields?: boolean;
24
+ insertableFields?: string[];
25
+ updatableFields?: string[];
26
+ deleteType: 'softDelete' | 'hardDelete';
27
+ massDeleteAllowed: boolean;
28
+ massEditAllowed: boolean;
19
29
  }
20
30
  interface ConnectionConfig {
21
31
  name: string;
@@ -39,6 +49,10 @@ export declare class Connections {
39
49
  getAdapter(name: string): DbAdapter | undefined;
40
50
  list(): ConnectionConfig[];
41
51
  }
52
+ export declare function parseDescriptorStructure(struct: Record<string, string | any[]>): {
53
+ fields: Field[];
54
+ allowUndefinedFields: boolean;
55
+ };
42
56
  declare class SchemaBuilder {
43
57
  private manager;
44
58
  private name;
@@ -46,12 +60,24 @@ declare class SchemaBuilder {
46
60
  private collectionName?;
47
61
  private fields;
48
62
  private allowUndefinedFields;
63
+ private insertableFields?;
64
+ private updatableFields?;
65
+ private deleteType;
66
+ private massDeleteAllowed;
67
+ private massEditAllowed;
49
68
  constructor(manager: SchemaManager, name: string);
50
69
  use(options: {
51
70
  connection: string;
52
71
  collection: string;
53
72
  }): this;
54
- setStructure(structure: Record<string, string> | Field[]): SchemaManager;
73
+ setOptions(options: {
74
+ insertableFields?: string[];
75
+ updatableFields?: string[];
76
+ deleteType?: 'softDelete' | 'hardDelete';
77
+ massDeleteAllowed?: boolean;
78
+ massEditAllowed?: boolean;
79
+ }): this;
80
+ setStructure(structure: Record<string, any> | Field[]): SchemaManager;
55
81
  }
56
82
  declare class SchemaQuery {
57
83
  private manager;
@@ -59,10 +85,19 @@ declare class SchemaQuery {
59
85
  private filters;
60
86
  private rawWhere;
61
87
  private pendingUpdate;
88
+ private cachedAdapter;
89
+ private cachedFieldNames;
90
+ private allowedFields;
91
+ private _limit;
92
+ private _offset;
62
93
  constructor(manager: SchemaManager, def: SchemaDef);
94
+ limit(n: number): this;
95
+ offset(n: number): this;
96
+ selectFields(fields: string[]): this;
63
97
  where(fieldOrPair: string | [string, any], value?: any, operator?: string): this;
64
98
  whereComplex(raw: string): this;
65
99
  private buildOptions;
100
+ private getAdapter;
66
101
  to(update: Record<string, any>): this;
67
102
  get(): Promise<Record<string, any>[]>;
68
103
  getOne(): Promise<Record<string, any> | null>;
@@ -85,14 +120,18 @@ export declare class SchemaManager {
85
120
  export declare function connection(): Connections;
86
121
  export declare function schema(conns?: Connections): SchemaManager;
87
122
  export declare const schemas: SchemaManager;
88
- export { createOrm } from './orm';
123
+ export { createOrm } from './orm/index.js';
89
124
  export type { DbAdapter, QueryOptions };
90
- export { parseConnectionsDsl, toNormalizedConnections } from './env';
91
- export type { EnvDslConnections, NormalizedConnection } from './env';
92
- export { documentationMd, markdownToHtml, getDocumentationHtml } from './docs';
93
- export { Mapper, createMapper } from './mapper';
94
- export default Mapper;
95
- export { StaticMapper } from './fluent-mapper';
96
- export type { FluentQueryBuilder, FluentConnectionBuilder, FluentSchemaBuilder, FluentSchemaCollectionBuilder, FluentConnectionSelector, FluentMapper } from './fluent-mapper';
97
- export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config';
98
- export type { MapperConfig, ConnectionConfig, DatabaseConnectionConfig, ApiConnectionConfig, ConfigSchema } from './config';
125
+ export { parseConnectionsDsl, toNormalizedConnections } from './env.js';
126
+ export type { EnvDslConnections, NormalizedConnection } from './env.js';
127
+ export { documentationMd, markdownToHtml, getDocumentationHtml } from './docs.js';
128
+ export { Mapper, createMapper } from './mapper.js';
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';
132
+ export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config.js';
133
+ export type { MapperConfig, ConnectionConfig, DatabaseConnectionConfig, ApiConnectionConfig, 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';
136
+ export { MapperError, AdapterMissingError, UpdatePayloadMissingError, DocumentMissingIdError, ConnectionExistingError, ConnectionUnknownError, SchemaExistingError, SchemaMissingError, SchemaConfigurationError, } from './errors.js';
137
+ export { Connector, mapper } from './connector.js';