@neupgroup/mapper 1.2.0 → 1.2.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/README.md +705 -36
- package/dist/config-wrapper.d.ts +6 -0
- package/dist/config-wrapper.js +7 -0
- package/dist/config.d.ts +68 -0
- package/dist/config.js +234 -0
- package/dist/docs.d.ts +3 -0
- package/dist/docs.js +351 -0
- package/dist/env.d.ts +25 -0
- package/dist/env.js +88 -0
- package/dist/fluent-mapper.d.ts +80 -0
- package/dist/fluent-mapper.js +171 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/mapper.d.ts +27 -0
- package/dist/mapper.js +144 -0
- package/dist/orm/index.d.ts +2 -2
- package/dist/orm/types.d.ts +1 -1
- package/package.json +5 -2
- package/dist/actions/ai-operation-suggestion.d.ts +0 -27
- package/dist/actions/ai-operation-suggestion.js +0 -46
- package/dist/actions/ai-schema-suggestion.d.ts +0 -24
- package/dist/actions/ai-schema-suggestion.js +0 -46
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type EnvDslConnections = Record<string, Record<string, string>>;
|
|
2
|
+
/**
|
|
3
|
+
* Parse a simple DSL of the form:
|
|
4
|
+
* connections = [
|
|
5
|
+
* connectionName = [
|
|
6
|
+
* key: value,
|
|
7
|
+
* key2: "value2",
|
|
8
|
+
* ],
|
|
9
|
+
* other = [
|
|
10
|
+
* type: SQL,
|
|
11
|
+
* host: localhost,
|
|
12
|
+
* ]
|
|
13
|
+
* ]
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseConnectionsDsl(text: string): EnvDslConnections;
|
|
16
|
+
export type NormalizedConnection = {
|
|
17
|
+
name: string;
|
|
18
|
+
type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api';
|
|
19
|
+
key: Record<string, any>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Convert DSL map to normalized connections compatible with library Connections.
|
|
23
|
+
* If no explicit type is provided, defaults to 'api'.
|
|
24
|
+
*/
|
|
25
|
+
export declare function toNormalizedConnections(map: EnvDslConnections): NormalizedConnection[];
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
function stripComments(input) {
|
|
2
|
+
return input
|
|
3
|
+
.split(/\r?\n/)
|
|
4
|
+
.map(line => line.replace(/#.*$/, ''))
|
|
5
|
+
.join('\n');
|
|
6
|
+
}
|
|
7
|
+
function unquote(val) {
|
|
8
|
+
const trimmed = val.trim();
|
|
9
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
10
|
+
return trimmed.slice(1, -1);
|
|
11
|
+
}
|
|
12
|
+
return trimmed;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parse a simple DSL of the form:
|
|
16
|
+
* connections = [
|
|
17
|
+
* connectionName = [
|
|
18
|
+
* key: value,
|
|
19
|
+
* key2: "value2",
|
|
20
|
+
* ],
|
|
21
|
+
* other = [
|
|
22
|
+
* type: SQL,
|
|
23
|
+
* host: localhost,
|
|
24
|
+
* ]
|
|
25
|
+
* ]
|
|
26
|
+
*/
|
|
27
|
+
export function parseConnectionsDsl(text) {
|
|
28
|
+
const cleaned = stripComments(text);
|
|
29
|
+
const result = {};
|
|
30
|
+
// Find the connections block
|
|
31
|
+
const match = cleaned.match(/connections\s*=\s*\[/i);
|
|
32
|
+
if (!match)
|
|
33
|
+
return result;
|
|
34
|
+
// Extract everything after 'connections = [' and before the final ']'
|
|
35
|
+
const start = match.index + match[0].length;
|
|
36
|
+
const after = cleaned.slice(start);
|
|
37
|
+
// naive bracket balance to find end of top-level list
|
|
38
|
+
let depth = 1;
|
|
39
|
+
let endIndex = -1;
|
|
40
|
+
for (let i = 0; i < after.length; i++) {
|
|
41
|
+
const ch = after[i];
|
|
42
|
+
if (ch === '[')
|
|
43
|
+
depth++;
|
|
44
|
+
else if (ch === ']') {
|
|
45
|
+
depth--;
|
|
46
|
+
if (depth === 0) {
|
|
47
|
+
endIndex = i;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const inner = endIndex >= 0 ? after.slice(0, endIndex) : after;
|
|
53
|
+
// Split by connection assignments at top level: name = [ ... ]
|
|
54
|
+
const connBlocks = inner.split(/\],?/).map(s => s.trim()).filter(Boolean);
|
|
55
|
+
for (const block of connBlocks) {
|
|
56
|
+
const assign = block.match(/^(\w[\w-]*)\s*=\s*\[/);
|
|
57
|
+
if (!assign)
|
|
58
|
+
continue;
|
|
59
|
+
const name = assign[1];
|
|
60
|
+
const content = block.slice(assign[0].length); // inside the [ ...
|
|
61
|
+
const lines = content.split(/\r?\n|,/).map(l => l.trim()).filter(l => l.length);
|
|
62
|
+
const map = {};
|
|
63
|
+
for (const line of lines) {
|
|
64
|
+
const kv = line.match(/^(\w[\w-]*)\s*:\s*(.+)$/);
|
|
65
|
+
if (!kv)
|
|
66
|
+
continue;
|
|
67
|
+
const key = kv[1];
|
|
68
|
+
const value = unquote(kv[2].replace(/,\s*$/, ''));
|
|
69
|
+
map[key] = value;
|
|
70
|
+
}
|
|
71
|
+
result[name] = map;
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Convert DSL map to normalized connections compatible with library Connections.
|
|
77
|
+
* If no explicit type is provided, defaults to 'api'.
|
|
78
|
+
*/
|
|
79
|
+
export function toNormalizedConnections(map) {
|
|
80
|
+
const conns = [];
|
|
81
|
+
for (const [name, kv] of Object.entries(map)) {
|
|
82
|
+
const rawType = (kv['type'] || kv['dbType'] || '').toLowerCase();
|
|
83
|
+
const type = ['mysql', 'sql', 'firestore', 'mongodb', 'api'].includes(rawType) ? rawType : 'api';
|
|
84
|
+
const { type: _omitType, dbType: _omitDbType, ...rest } = kv;
|
|
85
|
+
conns.push({ name, type, key: rest });
|
|
86
|
+
}
|
|
87
|
+
return conns;
|
|
88
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export declare class FluentQueryBuilder {
|
|
2
|
+
private mapper;
|
|
3
|
+
private schemaName;
|
|
4
|
+
private query;
|
|
5
|
+
constructor(mapper: any, schemaName: string);
|
|
6
|
+
where(field: string, value: any, operator?: string): this;
|
|
7
|
+
whereComplex(raw: string): this;
|
|
8
|
+
to(update: Record<string, any>): this;
|
|
9
|
+
get(): Promise<Record<string, any>[]>;
|
|
10
|
+
getOne(): Promise<Record<string, any> | null>;
|
|
11
|
+
add(data: Record<string, any>): Promise<any>;
|
|
12
|
+
update(): Promise<void>;
|
|
13
|
+
delete(): Promise<void>;
|
|
14
|
+
deleteOne(): Promise<void>;
|
|
15
|
+
updateOne(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export declare class FluentConnectionBuilder {
|
|
18
|
+
private mapper;
|
|
19
|
+
private connectionName;
|
|
20
|
+
private connectionType;
|
|
21
|
+
private connectionConfig;
|
|
22
|
+
constructor(mapper: any, connectionName: string, connectionType: string, config: Record<string, any>);
|
|
23
|
+
schema(schemaName: string): FluentSchemaBuilder;
|
|
24
|
+
query(schemaName: string): FluentQueryBuilder;
|
|
25
|
+
useConnection(connectionName: string): FluentConnectionSelector;
|
|
26
|
+
}
|
|
27
|
+
export declare class FluentSchemaBuilder {
|
|
28
|
+
private mapper;
|
|
29
|
+
private schemaName;
|
|
30
|
+
private connectionName;
|
|
31
|
+
constructor(mapper: any, schemaName: string, connectionName: string);
|
|
32
|
+
collection(collectionName: string): FluentSchemaCollectionBuilder;
|
|
33
|
+
}
|
|
34
|
+
export declare class FluentSchemaCollectionBuilder {
|
|
35
|
+
private mapper;
|
|
36
|
+
private schemaName;
|
|
37
|
+
private connectionName;
|
|
38
|
+
private collectionName;
|
|
39
|
+
constructor(mapper: any, schemaName: string, connectionName: string, collectionName: string);
|
|
40
|
+
structure(structure: Record<string, string> | Array<{
|
|
41
|
+
name: string;
|
|
42
|
+
type: string;
|
|
43
|
+
[key: string]: any;
|
|
44
|
+
}>): FluentMapper;
|
|
45
|
+
}
|
|
46
|
+
export declare class FluentConnectionSelector {
|
|
47
|
+
private mapper;
|
|
48
|
+
private connectionName;
|
|
49
|
+
constructor(mapper: any, connectionName: string);
|
|
50
|
+
schema(schemaName: string): FluentSchemaBuilder;
|
|
51
|
+
query(schemaName: string): FluentQueryBuilder;
|
|
52
|
+
}
|
|
53
|
+
export declare class FluentMapper {
|
|
54
|
+
private mapper;
|
|
55
|
+
constructor(mapper: any);
|
|
56
|
+
query(schemaName: string): FluentQueryBuilder;
|
|
57
|
+
makeConnection(name: string, type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
|
|
58
|
+
useConnection(connectionName: string): FluentConnectionSelector;
|
|
59
|
+
makeTempConnection(type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
|
|
60
|
+
get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
|
|
61
|
+
getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
|
|
62
|
+
add(schemaName: string, data: Record<string, any>): Promise<any>;
|
|
63
|
+
update(schemaName: string, filters: Record<string, any>, data: Record<string, any>): Promise<void>;
|
|
64
|
+
delete(schemaName: string, filters: Record<string, any>): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
export declare class StaticMapper {
|
|
67
|
+
private static instance;
|
|
68
|
+
private static getFluentMapper;
|
|
69
|
+
static makeConnection(name: string, type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
|
|
70
|
+
static useConnection(connectionName: string): FluentConnectionSelector;
|
|
71
|
+
static makeTempConnection(type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): FluentConnectionBuilder;
|
|
72
|
+
static query(schemaName: string): FluentQueryBuilder;
|
|
73
|
+
static get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
|
|
74
|
+
static getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
|
|
75
|
+
static add(schemaName: string, data: Record<string, any>): Promise<any>;
|
|
76
|
+
static update(schemaName: string, filters: Record<string, any>, data: Record<string, any>): Promise<void>;
|
|
77
|
+
static delete(schemaName: string, filters: Record<string, any>): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
export declare const Mapper: typeof StaticMapper;
|
|
80
|
+
export default Mapper;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { createMapper } from './mapper.js';
|
|
2
|
+
export class FluentQueryBuilder {
|
|
3
|
+
constructor(mapper, schemaName) {
|
|
4
|
+
this.mapper = mapper;
|
|
5
|
+
this.schemaName = schemaName;
|
|
6
|
+
this.query = mapper.use(schemaName);
|
|
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
|
+
to(update) {
|
|
17
|
+
this.query.to(update);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
async get() {
|
|
21
|
+
return this.query.get();
|
|
22
|
+
}
|
|
23
|
+
async getOne() {
|
|
24
|
+
return this.query.getOne();
|
|
25
|
+
}
|
|
26
|
+
async add(data) {
|
|
27
|
+
return this.mapper.add(this.schemaName, data);
|
|
28
|
+
}
|
|
29
|
+
async update() {
|
|
30
|
+
return this.query.update();
|
|
31
|
+
}
|
|
32
|
+
async delete() {
|
|
33
|
+
return this.query.delete();
|
|
34
|
+
}
|
|
35
|
+
async deleteOne() {
|
|
36
|
+
return this.query.deleteOne();
|
|
37
|
+
}
|
|
38
|
+
async updateOne() {
|
|
39
|
+
return this.query.updateOne();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class FluentConnectionBuilder {
|
|
43
|
+
constructor(mapper, connectionName, connectionType, config) {
|
|
44
|
+
this.mapper = mapper;
|
|
45
|
+
this.connectionName = connectionName;
|
|
46
|
+
this.connectionType = connectionType;
|
|
47
|
+
this.connectionConfig = config;
|
|
48
|
+
// Create the connection immediately
|
|
49
|
+
this.mapper.connect(connectionName, connectionType, config);
|
|
50
|
+
}
|
|
51
|
+
schema(schemaName) {
|
|
52
|
+
return new FluentSchemaBuilder(this.mapper, schemaName, this.connectionName);
|
|
53
|
+
}
|
|
54
|
+
query(schemaName) {
|
|
55
|
+
return new FluentQueryBuilder(this.mapper, schemaName);
|
|
56
|
+
}
|
|
57
|
+
useConnection(connectionName) {
|
|
58
|
+
return new FluentConnectionSelector(this.mapper, connectionName);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class FluentSchemaBuilder {
|
|
62
|
+
constructor(mapper, schemaName, connectionName) {
|
|
63
|
+
this.mapper = mapper;
|
|
64
|
+
this.schemaName = schemaName;
|
|
65
|
+
this.connectionName = connectionName;
|
|
66
|
+
}
|
|
67
|
+
collection(collectionName) {
|
|
68
|
+
return new FluentSchemaCollectionBuilder(this.mapper, this.schemaName, this.connectionName, collectionName);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export class FluentSchemaCollectionBuilder {
|
|
72
|
+
constructor(mapper, schemaName, connectionName, collectionName) {
|
|
73
|
+
this.mapper = mapper;
|
|
74
|
+
this.schemaName = schemaName;
|
|
75
|
+
this.connectionName = connectionName;
|
|
76
|
+
this.collectionName = collectionName;
|
|
77
|
+
}
|
|
78
|
+
structure(structure) {
|
|
79
|
+
this.mapper.schema(this.schemaName)
|
|
80
|
+
.use({ connection: this.connectionName, collection: this.collectionName })
|
|
81
|
+
.setStructure(structure);
|
|
82
|
+
return new FluentMapper(this.mapper);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export class FluentConnectionSelector {
|
|
86
|
+
constructor(mapper, connectionName) {
|
|
87
|
+
this.mapper = mapper;
|
|
88
|
+
this.connectionName = connectionName;
|
|
89
|
+
}
|
|
90
|
+
schema(schemaName) {
|
|
91
|
+
return new FluentSchemaBuilder(this.mapper, schemaName, this.connectionName);
|
|
92
|
+
}
|
|
93
|
+
query(schemaName) {
|
|
94
|
+
return new FluentQueryBuilder(this.mapper, schemaName);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export class FluentMapper {
|
|
98
|
+
constructor(mapper) {
|
|
99
|
+
this.mapper = mapper;
|
|
100
|
+
}
|
|
101
|
+
query(schemaName) {
|
|
102
|
+
return new FluentQueryBuilder(this.mapper, schemaName);
|
|
103
|
+
}
|
|
104
|
+
makeConnection(name, type, config) {
|
|
105
|
+
return new FluentConnectionBuilder(this.mapper, name, type, config);
|
|
106
|
+
}
|
|
107
|
+
useConnection(connectionName) {
|
|
108
|
+
return new FluentConnectionSelector(this.mapper, connectionName);
|
|
109
|
+
}
|
|
110
|
+
makeTempConnection(type, config) {
|
|
111
|
+
const tempName = `temp_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
112
|
+
return new FluentConnectionBuilder(this.mapper, tempName, type, config);
|
|
113
|
+
}
|
|
114
|
+
// Direct query methods for quick usage
|
|
115
|
+
async get(schemaName, filters) {
|
|
116
|
+
return this.mapper.get(schemaName, filters);
|
|
117
|
+
}
|
|
118
|
+
async getOne(schemaName, filters) {
|
|
119
|
+
return this.mapper.getOne(schemaName, filters);
|
|
120
|
+
}
|
|
121
|
+
async add(schemaName, data) {
|
|
122
|
+
return this.mapper.add(schemaName, data);
|
|
123
|
+
}
|
|
124
|
+
async update(schemaName, filters, data) {
|
|
125
|
+
return this.mapper.update(schemaName, filters, data);
|
|
126
|
+
}
|
|
127
|
+
async delete(schemaName, filters) {
|
|
128
|
+
return this.mapper.delete(schemaName, filters);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Static API class that provides the fluent interface
|
|
132
|
+
export class StaticMapper {
|
|
133
|
+
static getFluentMapper() {
|
|
134
|
+
if (!StaticMapper.instance) {
|
|
135
|
+
const baseMapper = createMapper();
|
|
136
|
+
StaticMapper.instance = new FluentMapper(baseMapper);
|
|
137
|
+
}
|
|
138
|
+
return StaticMapper.instance;
|
|
139
|
+
}
|
|
140
|
+
static makeConnection(name, type, config) {
|
|
141
|
+
return StaticMapper.getFluentMapper().makeConnection(name, type, config);
|
|
142
|
+
}
|
|
143
|
+
static useConnection(connectionName) {
|
|
144
|
+
return StaticMapper.getFluentMapper().useConnection(connectionName);
|
|
145
|
+
}
|
|
146
|
+
static makeTempConnection(type, config) {
|
|
147
|
+
return StaticMapper.getFluentMapper().makeTempConnection(type, config);
|
|
148
|
+
}
|
|
149
|
+
static query(schemaName) {
|
|
150
|
+
return StaticMapper.getFluentMapper().query(schemaName);
|
|
151
|
+
}
|
|
152
|
+
// Direct static methods
|
|
153
|
+
static async get(schemaName, filters) {
|
|
154
|
+
return StaticMapper.getFluentMapper().get(schemaName, filters);
|
|
155
|
+
}
|
|
156
|
+
static async getOne(schemaName, filters) {
|
|
157
|
+
return StaticMapper.getFluentMapper().getOne(schemaName, filters);
|
|
158
|
+
}
|
|
159
|
+
static async add(schemaName, data) {
|
|
160
|
+
return StaticMapper.getFluentMapper().add(schemaName, data);
|
|
161
|
+
}
|
|
162
|
+
static async update(schemaName, filters, data) {
|
|
163
|
+
return StaticMapper.getFluentMapper().update(schemaName, filters, data);
|
|
164
|
+
}
|
|
165
|
+
static async delete(schemaName, filters) {
|
|
166
|
+
return StaticMapper.getFluentMapper().delete(schemaName, filters);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Export a default instance for convenience
|
|
170
|
+
export const Mapper = StaticMapper;
|
|
171
|
+
export default Mapper;
|
package/dist/index.d.ts
CHANGED
|
@@ -86,3 +86,12 @@ export declare function schema(conns?: Connections): SchemaManager;
|
|
|
86
86
|
export declare const schemas: SchemaManager;
|
|
87
87
|
export { createOrm } from './orm';
|
|
88
88
|
export type { DbAdapter, QueryOptions };
|
|
89
|
+
export { parseConnectionsDsl, toNormalizedConnections } from './env';
|
|
90
|
+
export type { EnvDslConnections, NormalizedConnection } from './env';
|
|
91
|
+
export { documentationMd, markdownToHtml, getDocumentationHtml } from './docs';
|
|
92
|
+
export { Mapper, createMapper } from './mapper';
|
|
93
|
+
export { default } from './mapper';
|
|
94
|
+
export { StaticMapper } from './fluent-mapper';
|
|
95
|
+
export type { FluentQueryBuilder, FluentConnectionBuilder, FluentSchemaBuilder, FluentSchemaCollectionBuilder, FluentConnectionSelector, FluentMapper } from './fluent-mapper';
|
|
96
|
+
export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config';
|
|
97
|
+
export type { MapperConfig, ConnectionConfig, DatabaseConnectionConfig, ApiConnectionConfig, ConfigSchema } from './config';
|
package/dist/index.js
CHANGED
|
@@ -276,3 +276,12 @@ export const schemas = (() => {
|
|
|
276
276
|
return new SchemaManager(conns);
|
|
277
277
|
})();
|
|
278
278
|
export { createOrm } from './orm';
|
|
279
|
+
export { parseConnectionsDsl, toNormalizedConnections } from './env';
|
|
280
|
+
export { documentationMd, markdownToHtml, getDocumentationHtml } from './docs';
|
|
281
|
+
// Export the simplified Mapper as default
|
|
282
|
+
export { Mapper, createMapper } from './mapper';
|
|
283
|
+
export { default } from './mapper';
|
|
284
|
+
// Export the new fluent/static API
|
|
285
|
+
export { StaticMapper } from './fluent-mapper';
|
|
286
|
+
// Export the new config-based system
|
|
287
|
+
export { ConfigBasedMapper, ConfigLoader, createConfigMapper, getConfigMapper, createDefaultMapper } from './config';
|
package/dist/mapper.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Connections, SchemaManager } from './index';
|
|
2
|
+
export declare class Mapper {
|
|
3
|
+
private connections;
|
|
4
|
+
private schemaManager;
|
|
5
|
+
private static instance;
|
|
6
|
+
constructor();
|
|
7
|
+
static getInstance(): Mapper;
|
|
8
|
+
autoConfigure(): this;
|
|
9
|
+
private detectEnvironmentConfig;
|
|
10
|
+
private inferConnectionType;
|
|
11
|
+
private applyConfig;
|
|
12
|
+
private applyDefaultConfig;
|
|
13
|
+
private createSchema;
|
|
14
|
+
connect(name: string, type: 'mysql' | 'sql' | 'firestore' | 'mongodb' | 'api', config: Record<string, any>): this;
|
|
15
|
+
schema(name: string): ReturnType<SchemaManager['create']>;
|
|
16
|
+
use(schemaName: string): ReturnType<SchemaManager['use']>;
|
|
17
|
+
get(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any>[]>;
|
|
18
|
+
getOne(schemaName: string, filters?: Record<string, any>): Promise<Record<string, any> | null>;
|
|
19
|
+
add(schemaName: string, data: Record<string, any>): Promise<any>;
|
|
20
|
+
update(schemaName: string, filters: Record<string, any>, data: Record<string, any>): Promise<void>;
|
|
21
|
+
delete(schemaName: string, filters: Record<string, any>): Promise<void>;
|
|
22
|
+
getConnections(): Connections;
|
|
23
|
+
getSchemaManager(): SchemaManager;
|
|
24
|
+
}
|
|
25
|
+
export declare const createMapper: () => Mapper;
|
|
26
|
+
declare const _default: Mapper;
|
|
27
|
+
export default _default;
|
package/dist/mapper.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Connections, SchemaManager } from './index';
|
|
2
|
+
export class Mapper {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.connections = new Connections();
|
|
5
|
+
this.schemaManager = new SchemaManager(this.connections);
|
|
6
|
+
}
|
|
7
|
+
static getInstance() {
|
|
8
|
+
if (!Mapper.instance) {
|
|
9
|
+
Mapper.instance = new Mapper();
|
|
10
|
+
}
|
|
11
|
+
return Mapper.instance;
|
|
12
|
+
}
|
|
13
|
+
// Auto-configuration based on environment or defaults
|
|
14
|
+
autoConfigure() {
|
|
15
|
+
// Check for environment variables or config files
|
|
16
|
+
const envConfig = this.detectEnvironmentConfig();
|
|
17
|
+
if (envConfig) {
|
|
18
|
+
this.applyConfig(envConfig);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
// Use sensible defaults
|
|
22
|
+
this.applyDefaultConfig();
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
detectEnvironmentConfig() {
|
|
27
|
+
// Check for common environment variables or config files
|
|
28
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
29
|
+
// Node.js environment
|
|
30
|
+
const dbUrl = process.env.DATABASE_URL;
|
|
31
|
+
if (dbUrl) {
|
|
32
|
+
return {
|
|
33
|
+
connection: {
|
|
34
|
+
name: 'default',
|
|
35
|
+
type: this.inferConnectionType(dbUrl),
|
|
36
|
+
key: { url: dbUrl }
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Check for browser environment with global config
|
|
42
|
+
if (typeof window !== 'undefined' && window.__MAPPER_CONFIG__) {
|
|
43
|
+
return window.__MAPPER_CONFIG__;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
inferConnectionType(url) {
|
|
48
|
+
if (url.includes('mysql'))
|
|
49
|
+
return 'mysql';
|
|
50
|
+
if (url.includes('postgres') || url.includes('postgresql'))
|
|
51
|
+
return 'sql';
|
|
52
|
+
if (url.includes('mongodb'))
|
|
53
|
+
return 'mongodb';
|
|
54
|
+
if (url.includes('firestore'))
|
|
55
|
+
return 'firestore';
|
|
56
|
+
return 'api';
|
|
57
|
+
}
|
|
58
|
+
applyConfig(config) {
|
|
59
|
+
if (config.connection) {
|
|
60
|
+
const conn = config.connection;
|
|
61
|
+
this.connections.create(conn.name, conn.type).key(conn.key);
|
|
62
|
+
}
|
|
63
|
+
if (config.schemas) {
|
|
64
|
+
for (const schemaConfig of config.schemas) {
|
|
65
|
+
this.createSchema(schemaConfig);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
applyDefaultConfig() {
|
|
70
|
+
// Create a default in-memory connection for quick start
|
|
71
|
+
this.connections.create('default', 'api').key({ endpoint: 'memory' });
|
|
72
|
+
}
|
|
73
|
+
createSchema(config) {
|
|
74
|
+
const builder = this.schemaManager.create(config.name);
|
|
75
|
+
if (config.connection && config.collection) {
|
|
76
|
+
builder.use({ connection: config.connection, collection: config.collection });
|
|
77
|
+
}
|
|
78
|
+
if (config.structure) {
|
|
79
|
+
builder.setStructure(config.structure);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Simplified API methods
|
|
83
|
+
connect(name, type, config) {
|
|
84
|
+
this.connections.create(name, type).key(config);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
schema(name) {
|
|
88
|
+
return this.schemaManager.create(name);
|
|
89
|
+
}
|
|
90
|
+
use(schemaName) {
|
|
91
|
+
return this.schemaManager.use(schemaName);
|
|
92
|
+
}
|
|
93
|
+
// Quick query methods
|
|
94
|
+
async get(schemaName, filters) {
|
|
95
|
+
const query = this.use(schemaName);
|
|
96
|
+
if (filters) {
|
|
97
|
+
Object.entries(filters).forEach(([field, value]) => {
|
|
98
|
+
query.where(field, value);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return query.get();
|
|
102
|
+
}
|
|
103
|
+
async getOne(schemaName, filters) {
|
|
104
|
+
const query = this.use(schemaName);
|
|
105
|
+
if (filters) {
|
|
106
|
+
Object.entries(filters).forEach(([field, value]) => {
|
|
107
|
+
query.where(field, value);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return query.getOne();
|
|
111
|
+
}
|
|
112
|
+
async add(schemaName, data) {
|
|
113
|
+
return this.use(schemaName).add(data);
|
|
114
|
+
}
|
|
115
|
+
async update(schemaName, filters, data) {
|
|
116
|
+
const query = this.use(schemaName);
|
|
117
|
+
Object.entries(filters).forEach(([field, value]) => {
|
|
118
|
+
query.where(field, value);
|
|
119
|
+
});
|
|
120
|
+
await query.to(data).update();
|
|
121
|
+
}
|
|
122
|
+
async delete(schemaName, filters) {
|
|
123
|
+
const query = this.use(schemaName);
|
|
124
|
+
Object.entries(filters).forEach(([field, value]) => {
|
|
125
|
+
query.where(field, value);
|
|
126
|
+
});
|
|
127
|
+
await query.delete();
|
|
128
|
+
}
|
|
129
|
+
// Get the underlying managers for advanced usage
|
|
130
|
+
getConnections() {
|
|
131
|
+
return this.connections;
|
|
132
|
+
}
|
|
133
|
+
getSchemaManager() {
|
|
134
|
+
return this.schemaManager;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Create and configure the default instance
|
|
138
|
+
export const createMapper = () => {
|
|
139
|
+
const mapper = Mapper.getInstance();
|
|
140
|
+
mapper.autoConfigure();
|
|
141
|
+
return mapper;
|
|
142
|
+
};
|
|
143
|
+
// Export a ready-to-use default instance
|
|
144
|
+
export default createMapper();
|
package/dist/orm/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { DbAdapter, QueryOptions } from './types';
|
|
2
2
|
export declare function createOrm(adapter: DbAdapter): {
|
|
3
|
-
get(options: QueryOptions): Promise<import("
|
|
3
|
+
get(options: QueryOptions): Promise<import("./types").DocumentData[]>;
|
|
4
4
|
getOne(options: QueryOptions): Promise<any>;
|
|
5
|
-
getDocuments(options: QueryOptions): Promise<import("
|
|
5
|
+
getDocuments(options: QueryOptions): Promise<import("./types").DocumentData[]>;
|
|
6
6
|
addDocument(collectionName: string, data: Record<string, any>): Promise<string>;
|
|
7
7
|
updateDocument(collectionName: string, docId: string, data: Record<string, any>): Promise<void>;
|
|
8
8
|
deleteDocument(collectionName: string, docId: string): Promise<void>;
|
package/dist/orm/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neupgroup/mapper",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Neup.Mapper core library for schema and mapping utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
},
|
|
16
16
|
"publishConfig": {
|
|
17
17
|
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^24.10.1",
|
|
21
|
+
"typescript": "^5.9.3"
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
|
-
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { z } from 'genkit';
|
|
2
|
-
export declare const AIOperationSuggestionInputSchema: z.ZodObject<{
|
|
3
|
-
operationDescription: z.ZodString;
|
|
4
|
-
databaseType: z.ZodString;
|
|
5
|
-
schema: z.ZodString;
|
|
6
|
-
}, "strip", z.ZodTypeAny, {
|
|
7
|
-
databaseType: string;
|
|
8
|
-
operationDescription: string;
|
|
9
|
-
schema: string;
|
|
10
|
-
}, {
|
|
11
|
-
databaseType: string;
|
|
12
|
-
operationDescription: string;
|
|
13
|
-
schema: string;
|
|
14
|
-
}>;
|
|
15
|
-
export type AIOperationSuggestionInput = z.infer<typeof AIOperationSuggestionInputSchema>;
|
|
16
|
-
export declare const AIOperationSuggestionOutputSchema: z.ZodObject<{
|
|
17
|
-
suggestedCode: z.ZodString;
|
|
18
|
-
rationale: z.ZodString;
|
|
19
|
-
}, "strip", z.ZodTypeAny, {
|
|
20
|
-
rationale: string;
|
|
21
|
-
suggestedCode: string;
|
|
22
|
-
}, {
|
|
23
|
-
rationale: string;
|
|
24
|
-
suggestedCode: string;
|
|
25
|
-
}>;
|
|
26
|
-
export type AIOperationSuggestionOutput = z.infer<typeof AIOperationSuggestionOutputSchema>;
|
|
27
|
-
export declare function createSuggestOperation(ai: any): (input: AIOperationSuggestionInput) => Promise<any>;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { z } from 'genkit';
|
|
2
|
-
export const AIOperationSuggestionInputSchema = z.object({
|
|
3
|
-
operationDescription: z
|
|
4
|
-
.string()
|
|
5
|
-
.describe('A detailed description of the operation to be performed.'),
|
|
6
|
-
databaseType: z
|
|
7
|
-
.string()
|
|
8
|
-
.describe('The type of database (e.g., MongoDB, Firestore, SQL).'),
|
|
9
|
-
schema: z.string().describe('The JSON schema of the data structure.'),
|
|
10
|
-
});
|
|
11
|
-
export const AIOperationSuggestionOutputSchema = z.object({
|
|
12
|
-
suggestedCode: z
|
|
13
|
-
.string()
|
|
14
|
-
.describe('The suggested code for the operation, as a string.'),
|
|
15
|
-
rationale: z
|
|
16
|
-
.string()
|
|
17
|
-
.describe('Explanation of why the suggested code is optimal.'),
|
|
18
|
-
});
|
|
19
|
-
export function createSuggestOperation(ai) {
|
|
20
|
-
const aiOperationSuggestionPrompt = ai.definePrompt({
|
|
21
|
-
name: 'aiOperationSuggestionPrompt',
|
|
22
|
-
input: { schema: AIOperationSuggestionInputSchema },
|
|
23
|
-
output: { schema: AIOperationSuggestionOutputSchema },
|
|
24
|
-
prompt: `You are an expert database engineer. Given a description of a database operation, a database type, and a data schema, you will generate the code to perform that operation. The operation can be for inserting, fetching, or updating data.
|
|
25
|
-
|
|
26
|
-
Operation Description: {{{operationDescription}}}
|
|
27
|
-
Database Type: {{{databaseType}}}
|
|
28
|
-
Schema: {{{schema}}}
|
|
29
|
-
|
|
30
|
-
Generate the code for the specified database type. Provide a rationale for the generated code.
|
|
31
|
-
|
|
32
|
-
Return the suggested code as a string, and include the rationale.
|
|
33
|
-
`,
|
|
34
|
-
});
|
|
35
|
-
const aiOperationSuggestionFlow = ai.defineFlow({
|
|
36
|
-
name: 'aiOperationSuggestionFlow',
|
|
37
|
-
inputSchema: AIOperationSuggestionInputSchema,
|
|
38
|
-
outputSchema: AIOperationSuggestionOutputSchema,
|
|
39
|
-
}, async (input) => {
|
|
40
|
-
const { output } = await aiOperationSuggestionPrompt(input);
|
|
41
|
-
return output;
|
|
42
|
-
});
|
|
43
|
-
return async function suggestOperation(input) {
|
|
44
|
-
return aiOperationSuggestionFlow(input);
|
|
45
|
-
};
|
|
46
|
-
}
|