@neupgroup/mapper 1.6.1 → 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.
- package/dist/cli/create-migration.js +16 -16
- package/dist/errors.js +1 -1
- package/dist/fluent-mapper.d.ts +27 -23
- package/dist/fluent-mapper.js +112 -105
- package/package.json +1 -1
|
@@ -46,34 +46,34 @@ import { Mapper, TableMigrator } from '@neupgroup/mapper';
|
|
|
46
46
|
export const usesConnection = 'default';
|
|
47
47
|
|
|
48
48
|
export async function up() {
|
|
49
|
-
const
|
|
50
|
-
|
|
49
|
+
const schema = Mapper.schema().table('${tableName}');
|
|
50
|
+
schema.useConnection(usesConnection);
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
|
-
* CASE 1: CREATE
|
|
54
|
-
* Use this when defining a new
|
|
53
|
+
* CASE 1: CREATE SCHEMA (Requires .exec())
|
|
54
|
+
* Use this when defining a new schema. addColumn calls are batched.
|
|
55
55
|
*/
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
// await
|
|
56
|
+
// schema.addColumn('id').type('int').isPrimary().autoIncrement();
|
|
57
|
+
// schema.addColumn('name').type('string').notNull();
|
|
58
|
+
// await schema.exec();
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
* CASE 2: ALTER
|
|
61
|
+
* CASE 2: ALTER SCHEMA (Queued actions)
|
|
62
62
|
* These methods are queued and only execute when you call .exec()
|
|
63
63
|
*/
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
// await
|
|
64
|
+
// schema.dropColumn('old_field');
|
|
65
|
+
// schema.dropUnique('field_name');
|
|
66
|
+
// await schema.exec();
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
export async function down() {
|
|
70
70
|
/**
|
|
71
|
-
* DROP
|
|
72
|
-
* This will drop the
|
|
71
|
+
* DROP SCHEMA (Immediate action)
|
|
72
|
+
* This will drop the schema from the DB and delete the local schema file.
|
|
73
73
|
*/
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
await
|
|
74
|
+
const schema = Mapper.schema().table('${tableName}');
|
|
75
|
+
schema.useConnection(usesConnection);
|
|
76
|
+
await schema.dropTable().exec();
|
|
77
77
|
}
|
|
78
78
|
`;
|
|
79
79
|
fs.writeFileSync(filePath, fileContent.trim());
|
package/dist/errors.js
CHANGED
|
@@ -40,7 +40,7 @@ export class SchemaExistingError extends MapperError {
|
|
|
40
40
|
}
|
|
41
41
|
export class SchemaMissingError extends MapperError {
|
|
42
42
|
constructor(name) {
|
|
43
|
-
super(`Unknown schema '${name}'.`, 'SCHEMA_UNKNOWN', `Ensure you have
|
|
43
|
+
super(`Unknown schema '${name}'.`, 'SCHEMA_UNKNOWN', `The schema '${name}' is not registered. Ensure you have run migrations, called 'Mapper.discover()', or defined it manually using 'Mapper.schema().create("${name}")'.`);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
export class SchemaConfigurationError extends MapperError {
|
package/dist/fluent-mapper.d.ts
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
|
-
import { Connections, SchemaManager, ConnectionType } from './index.js';
|
|
1
|
+
import { Connections, SchemaManager, type SchemaDef, ConnectionType } from './index.js';
|
|
2
2
|
import { TableMigrator } from './migrator.js';
|
|
3
3
|
export declare class FluentQueryBuilder {
|
|
4
4
|
private mapper;
|
|
5
5
|
private schemaName;
|
|
6
6
|
private query;
|
|
7
|
-
|
|
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>;
|
|
8
26
|
where(field: string, value: any, operator?: string): this;
|
|
9
27
|
whereComplex(raw: string): this;
|
|
10
28
|
limit(n: number): this;
|
|
@@ -69,11 +87,11 @@ export declare class FluentConnectionSelector {
|
|
|
69
87
|
private mapper;
|
|
70
88
|
private connectionName;
|
|
71
89
|
constructor(mapper: any, connectionName: string);
|
|
72
|
-
schema(schemaName: string):
|
|
90
|
+
schema(schemaName: string): FluentQueryBuilder;
|
|
91
|
+
schemas(schemaName: string): FluentQueryBuilder;
|
|
73
92
|
query(schemaName: string): FluentQueryBuilder;
|
|
74
93
|
table(tableName: string): FluentQueryBuilder;
|
|
75
94
|
collection(collectionName: string): FluentQueryBuilder;
|
|
76
|
-
schemas(schemaName: string): FluentSchemaWrapper;
|
|
77
95
|
path(path: string): FluentApiRequestBuilder;
|
|
78
96
|
header(key: string | Record<string, string | string[]>, value?: string | string[]): FluentApiRequestBuilder;
|
|
79
97
|
headers(headers: Record<string, string> | any[]): FluentApiRequestBuilder;
|
|
@@ -87,6 +105,7 @@ export declare class FluentMapper {
|
|
|
87
105
|
private mapper;
|
|
88
106
|
constructor(mapper: any);
|
|
89
107
|
query(schemaName: string): FluentQueryBuilder;
|
|
108
|
+
schema(name: string): FluentQueryBuilder;
|
|
90
109
|
table(name: string): FluentQueryBuilder;
|
|
91
110
|
makeConnection(name: string, type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
|
|
92
111
|
useConnection(connectionName: string): FluentConnectionSelector;
|
|
@@ -105,10 +124,12 @@ export declare class StaticMapper {
|
|
|
105
124
|
static makeConnection(name: string, type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
|
|
106
125
|
static makeTempConnection(type: ConnectionType, config: Record<string, any>): FluentConnectionBuilder;
|
|
107
126
|
static query(schemaName: string): FluentQueryBuilder;
|
|
127
|
+
static schema(name: string): FluentQueryBuilder;
|
|
128
|
+
static schema(): SchemaManagerWrapper;
|
|
108
129
|
static table(name: string): FluentQueryBuilder;
|
|
109
130
|
static connection(connectionOrConfig: string | Record<string, any>): FluentConnectionSelector;
|
|
110
131
|
static useConnection(connectionName: string): FluentConnectionSelector;
|
|
111
|
-
static schemas(name?: string):
|
|
132
|
+
static schemas(name?: string): any;
|
|
112
133
|
static get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
|
|
113
134
|
static getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
|
|
114
135
|
static add(schemaName: string, data: Record<string, any>): Promise<any>;
|
|
@@ -120,27 +141,10 @@ export declare class StaticMapper {
|
|
|
120
141
|
}
|
|
121
142
|
export declare const Mapper: typeof StaticMapper;
|
|
122
143
|
export default Mapper;
|
|
123
|
-
export declare class FluentSchemaWrapper {
|
|
124
|
-
private manager;
|
|
125
|
-
private name;
|
|
126
|
-
private connectionName?;
|
|
127
|
-
constructor(manager: SchemaManager, name: string, connectionName?: string | undefined);
|
|
128
|
-
private getDef;
|
|
129
|
-
set fields(config: any);
|
|
130
|
-
set insertableFields(val: string[]);
|
|
131
|
-
set updatableFields(val: string[]);
|
|
132
|
-
set deleteType(val: 'softDelete' | 'hardDelete');
|
|
133
|
-
set massDeleteAllowed(val: boolean);
|
|
134
|
-
set massEditAllowed(val: boolean);
|
|
135
|
-
get(...fields: string[]): any;
|
|
136
|
-
limit(n: number): FluentQueryBuilder;
|
|
137
|
-
offset(n: number): FluentQueryBuilder;
|
|
138
|
-
insert(data: Record<string, any>): Promise<any>;
|
|
139
|
-
dropTable(): Promise<void>;
|
|
140
|
-
}
|
|
141
144
|
export declare class SchemaManagerWrapper {
|
|
142
145
|
private manager;
|
|
143
146
|
constructor(manager: SchemaManager);
|
|
144
147
|
table(name: string): TableMigrator;
|
|
148
|
+
schema(name: string): TableMigrator;
|
|
145
149
|
dropTable(name: string): Promise<void>;
|
|
146
150
|
}
|
package/dist/fluent-mapper.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
|
288
|
+
return this.schema(schemaName);
|
|
197
289
|
}
|
|
198
290
|
table(tableName) {
|
|
199
|
-
return this.
|
|
291
|
+
return this.schema(tableName);
|
|
200
292
|
}
|
|
201
293
|
collection(collectionName) {
|
|
202
|
-
return this.
|
|
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,9 @@ 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
|
+
}
|
|
240
332
|
table(name) {
|
|
241
333
|
return this.query(name);
|
|
242
334
|
}
|
|
@@ -306,8 +398,13 @@ export class StaticMapper {
|
|
|
306
398
|
static query(schemaName) {
|
|
307
399
|
return StaticMapper.getFluentMapper().query(schemaName);
|
|
308
400
|
}
|
|
401
|
+
static schema(name) {
|
|
402
|
+
if (name)
|
|
403
|
+
return StaticMapper.getFluentMapper().schema(name);
|
|
404
|
+
return StaticMapper.schemas();
|
|
405
|
+
}
|
|
309
406
|
static table(name) {
|
|
310
|
-
return StaticMapper.
|
|
407
|
+
return StaticMapper.schema(name);
|
|
311
408
|
}
|
|
312
409
|
// New API
|
|
313
410
|
static connection(connectionOrConfig) {
|
|
@@ -318,9 +415,8 @@ export class StaticMapper {
|
|
|
318
415
|
return StaticMapper.connection(connectionName);
|
|
319
416
|
}
|
|
320
417
|
static schemas(name) {
|
|
321
|
-
if (name)
|
|
322
|
-
return
|
|
323
|
-
}
|
|
418
|
+
if (name)
|
|
419
|
+
return StaticMapper.schema(name);
|
|
324
420
|
return new SchemaManagerWrapper(StaticMapper.getFluentMapper().mapper.getSchemaManager());
|
|
325
421
|
}
|
|
326
422
|
// Direct static methods
|
|
@@ -353,98 +449,6 @@ export class StaticMapper {
|
|
|
353
449
|
// Export a default instance for convenience
|
|
354
450
|
export const Mapper = StaticMapper;
|
|
355
451
|
export default Mapper;
|
|
356
|
-
export class FluentSchemaWrapper {
|
|
357
|
-
// builder unused
|
|
358
|
-
constructor(manager, name, connectionName) {
|
|
359
|
-
this.manager = manager;
|
|
360
|
-
this.name = name;
|
|
361
|
-
this.connectionName = connectionName;
|
|
362
|
-
// Ensure schema exists or create it?
|
|
363
|
-
// User pattern: Mapper.schemas('name').fields = ...
|
|
364
|
-
// So we likely need to create it if missing, or update it.
|
|
365
|
-
try {
|
|
366
|
-
this.manager.create(name).use({ connection: connectionName || 'default', collection: name }).setStructure({});
|
|
367
|
-
}
|
|
368
|
-
catch (e) {
|
|
369
|
-
// Ignore if exists, but maybe update connection if strictly provided?
|
|
370
|
-
// Use existing definition if available.
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
getDef() {
|
|
374
|
-
// Access private map from manager? Or expose a get method.
|
|
375
|
-
// Manager has .schemas map.
|
|
376
|
-
return this.manager.schemas.get(this.name);
|
|
377
|
-
}
|
|
378
|
-
set fields(config) {
|
|
379
|
-
// Update schema structure
|
|
380
|
-
const builder = new FluentSchemaBuilder(null, this.name, ''); // Dummy wrapper or use internal
|
|
381
|
-
// Easier: use Manager.create(name) returns SchemaBuilder which has setStructure.
|
|
382
|
-
// But if it exists, create() throws.
|
|
383
|
-
// We need 'update' or direct access.
|
|
384
|
-
// Let's hack: re-register or update def.
|
|
385
|
-
const def = this.getDef();
|
|
386
|
-
if (def) {
|
|
387
|
-
// Re-parse
|
|
388
|
-
const parsed = parseDescriptorStructure(config);
|
|
389
|
-
def.fields = parsed.fields;
|
|
390
|
-
def.fieldsMap = new Map();
|
|
391
|
-
def.fields.forEach((f) => def.fieldsMap.set(f.name, f));
|
|
392
|
-
def.allowUndefinedFields = parsed.allowUndefinedFields;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
set insertableFields(val) {
|
|
396
|
-
const def = this.getDef();
|
|
397
|
-
if (def)
|
|
398
|
-
def.insertableFields = val;
|
|
399
|
-
}
|
|
400
|
-
set updatableFields(val) {
|
|
401
|
-
const def = this.getDef();
|
|
402
|
-
if (def)
|
|
403
|
-
def.updatableFields = val;
|
|
404
|
-
}
|
|
405
|
-
set deleteType(val) {
|
|
406
|
-
const def = this.getDef();
|
|
407
|
-
if (def)
|
|
408
|
-
def.deleteType = val;
|
|
409
|
-
}
|
|
410
|
-
set massDeleteAllowed(val) {
|
|
411
|
-
const def = this.getDef();
|
|
412
|
-
if (def)
|
|
413
|
-
def.massDeleteAllowed = val;
|
|
414
|
-
}
|
|
415
|
-
set massEditAllowed(val) {
|
|
416
|
-
const def = this.getDef();
|
|
417
|
-
if (def)
|
|
418
|
-
def.massEditAllowed = val;
|
|
419
|
-
}
|
|
420
|
-
// Delegation to QueryBuilder
|
|
421
|
-
get(...fields) {
|
|
422
|
-
const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
|
|
423
|
-
return q.get(...fields);
|
|
424
|
-
}
|
|
425
|
-
limit(n) {
|
|
426
|
-
const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
|
|
427
|
-
return q.limit(n);
|
|
428
|
-
}
|
|
429
|
-
offset(n) {
|
|
430
|
-
const q = new FluentQueryBuilder({ use: (n) => this.manager.use(n) }, this.name);
|
|
431
|
-
return q.offset(n);
|
|
432
|
-
}
|
|
433
|
-
async insert(data) {
|
|
434
|
-
const q = new FluentQueryBuilder({
|
|
435
|
-
use: (n) => this.manager.use(n),
|
|
436
|
-
add: (n, d) => this.manager.use(n).add(d)
|
|
437
|
-
}, this.name);
|
|
438
|
-
return q.insert(data);
|
|
439
|
-
}
|
|
440
|
-
async dropTable() {
|
|
441
|
-
const migrator = new TableMigrator(this.name);
|
|
442
|
-
if (this.connectionName) {
|
|
443
|
-
migrator.useConnection(this.connectionName);
|
|
444
|
-
}
|
|
445
|
-
return migrator.drop().exec();
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
452
|
// Helper to access parseDescriptorStructure from index.ts if not exported?
|
|
449
453
|
// It is NOT exported. I need to export it or duplicate logic.
|
|
450
454
|
// I'll export it from index.ts.
|
|
@@ -457,6 +461,9 @@ export class SchemaManagerWrapper {
|
|
|
457
461
|
// This allows Mapper.schemas().table('name') to return a migrator
|
|
458
462
|
return new TableMigrator(name);
|
|
459
463
|
}
|
|
464
|
+
schema(name) {
|
|
465
|
+
return this.table(name);
|
|
466
|
+
}
|
|
460
467
|
async dropTable(name) {
|
|
461
468
|
return new TableMigrator(name).drop().exec();
|
|
462
469
|
}
|