@hedystia/db 2.0.0 → 2.0.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.
- package/dist/_virtual/_rolldown/runtime.cjs +13 -0
- package/dist/_virtual/_rolldown/runtime.mjs +17 -0
- package/dist/cache/manager.cjs +137 -0
- package/dist/cache/manager.cjs.map +1 -0
- package/dist/cache/manager.d.cts +72 -0
- package/dist/cache/manager.d.mts +72 -0
- package/dist/cache/manager.mjs +140 -0
- package/dist/cache/manager.mjs.map +1 -0
- package/dist/cache/memory-store.cjs +122 -0
- package/dist/cache/memory-store.cjs.map +1 -0
- package/dist/cache/memory-store.mjs +122 -0
- package/dist/cache/memory-store.mjs.map +1 -0
- package/dist/cli/commands/migration.cjs +22 -0
- package/dist/cli/commands/migration.cjs.map +1 -0
- package/dist/cli/commands/migration.mjs +32 -0
- package/dist/cli/commands/migration.mjs.map +1 -0
- package/dist/cli/commands/schema.cjs +21 -0
- package/dist/cli/commands/schema.cjs.map +1 -0
- package/dist/cli/commands/schema.mjs +30 -0
- package/dist/cli/commands/schema.mjs.map +1 -0
- package/dist/cli.cjs +50 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +57 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/constants.cjs +16 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.mjs +16 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/core/database.cjs +158 -0
- package/dist/core/database.cjs.map +1 -0
- package/dist/core/database.d.cts +74 -0
- package/dist/core/database.d.mts +74 -0
- package/dist/core/database.mjs +159 -0
- package/dist/core/database.mjs.map +1 -0
- package/dist/core/repository.cjs +362 -0
- package/dist/core/repository.cjs.map +1 -0
- package/dist/core/repository.d.cts +100 -0
- package/dist/core/repository.d.mts +100 -0
- package/dist/core/repository.mjs +362 -0
- package/dist/core/repository.mjs.map +1 -0
- package/dist/drivers/driver.cjs +11 -0
- package/dist/drivers/driver.cjs.map +1 -0
- package/dist/drivers/driver.mjs +11 -0
- package/dist/drivers/driver.mjs.map +1 -0
- package/dist/drivers/file.cjs +336 -0
- package/dist/drivers/file.cjs.map +1 -0
- package/dist/drivers/file.mjs +337 -0
- package/dist/drivers/file.mjs.map +1 -0
- package/dist/drivers/index.cjs +28 -0
- package/dist/drivers/index.cjs.map +1 -0
- package/dist/drivers/index.d.cts +14 -0
- package/dist/drivers/index.d.mts +14 -0
- package/dist/drivers/index.mjs +28 -0
- package/dist/drivers/index.mjs.map +1 -0
- package/dist/drivers/mysql.cjs +272 -0
- package/dist/drivers/mysql.cjs.map +1 -0
- package/dist/drivers/mysql.mjs +272 -0
- package/dist/drivers/mysql.mjs.map +1 -0
- package/dist/drivers/sql-compiler.cjs +284 -0
- package/dist/drivers/sql-compiler.cjs.map +1 -0
- package/dist/drivers/sql-compiler.d.cts +66 -0
- package/dist/drivers/sql-compiler.d.mts +66 -0
- package/dist/drivers/sql-compiler.mjs +276 -0
- package/dist/drivers/sql-compiler.mjs.map +1 -0
- package/dist/drivers/sqlite.cjs +262 -0
- package/dist/drivers/sqlite.cjs.map +1 -0
- package/dist/drivers/sqlite.mjs +262 -0
- package/dist/drivers/sqlite.mjs.map +1 -0
- package/dist/errors.cjs +74 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +46 -0
- package/dist/errors.d.mts +46 -0
- package/dist/errors.mjs +68 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/index.cjs +55 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +21 -0
- package/dist/index.mjs.map +1 -0
- package/dist/migrations/definition.cjs +20 -0
- package/dist/migrations/definition.cjs.map +1 -0
- package/dist/migrations/definition.d.cts +18 -0
- package/dist/migrations/definition.d.mts +18 -0
- package/dist/migrations/definition.mjs +23 -0
- package/dist/migrations/definition.mjs.map +1 -0
- package/dist/migrations/index.mjs +12 -0
- package/dist/migrations/index.mjs.map +1 -0
- package/dist/migrations/templates.cjs +39 -0
- package/dist/migrations/templates.cjs.map +1 -0
- package/dist/migrations/templates.d.cts +16 -0
- package/dist/migrations/templates.d.mts +16 -0
- package/dist/migrations/templates.mjs +41 -0
- package/dist/migrations/templates.mjs.map +1 -0
- package/dist/schema/column.cjs +129 -0
- package/dist/schema/column.cjs.map +1 -0
- package/dist/schema/column.d.cts +96 -0
- package/dist/schema/column.d.mts +96 -0
- package/dist/schema/column.mjs +129 -0
- package/dist/schema/column.mjs.map +1 -0
- package/dist/schema/columns/index.cjs +110 -0
- package/dist/schema/columns/index.cjs.map +1 -0
- package/dist/schema/columns/index.d.cts +91 -0
- package/dist/schema/columns/index.d.mts +91 -0
- package/dist/schema/columns/index.mjs +104 -0
- package/dist/schema/columns/index.mjs.map +1 -0
- package/dist/schema/registry.cjs +101 -0
- package/dist/schema/registry.cjs.map +1 -0
- package/dist/schema/registry.d.cts +52 -0
- package/dist/schema/registry.d.mts +52 -0
- package/dist/schema/registry.mjs +101 -0
- package/dist/schema/registry.mjs.map +1 -0
- package/dist/schema/table.cjs +36 -0
- package/dist/schema/table.cjs.map +1 -0
- package/dist/schema/table.d.cts +17 -0
- package/dist/schema/table.d.mts +17 -0
- package/dist/schema/table.mjs +36 -0
- package/dist/schema/table.mjs.map +1 -0
- package/dist/sync/synchronizer.cjs +43 -0
- package/dist/sync/synchronizer.cjs.map +1 -0
- package/dist/sync/synchronizer.d.cts +22 -0
- package/dist/sync/synchronizer.d.mts +22 -0
- package/dist/sync/synchronizer.mjs +43 -0
- package/dist/sync/synchronizer.mjs.map +1 -0
- package/dist/types.d.cts +227 -0
- package/dist/types.d.mts +227 -0
- package/dist/utils/fs.cjs +24 -0
- package/dist/utils/fs.cjs.map +1 -0
- package/dist/utils/fs.mjs +26 -0
- package/dist/utils/fs.mjs.map +1 -0
- package/dist/utils/index.mjs +14 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/naming.cjs +13 -0
- package/dist/utils/naming.cjs.map +1 -0
- package/dist/utils/naming.mjs +16 -0
- package/dist/utils/naming.mjs.map +1 -0
- package/dist/utils/stable-stringify.cjs +19 -0
- package/dist/utils/stable-stringify.cjs.map +1 -0
- package/dist/utils/stable-stringify.mjs +22 -0
- package/dist/utils/stable-stringify.mjs.map +1 -0
- package/package.json +64 -27
- package/readme.md +86 -106
- package/index.d.ts +0 -65
- package/index.js +0 -1
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
//#region src/drivers/sql-compiler.ts
|
|
2
|
+
/**
|
|
3
|
+
* Compile column type to SQL string for a specific database dialect
|
|
4
|
+
* @param {ColumnMetadata} col - Column metadata
|
|
5
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
6
|
+
* @returns {string} SQL column type string
|
|
7
|
+
*/
|
|
8
|
+
function compileColumnType(col, dialect) {
|
|
9
|
+
const name = typeof dialect === "string" ? dialect : dialect.name;
|
|
10
|
+
return {
|
|
11
|
+
integer: {
|
|
12
|
+
mysql: "INT",
|
|
13
|
+
mariadb: "INT",
|
|
14
|
+
sqlite: "INTEGER",
|
|
15
|
+
file: "INTEGER"
|
|
16
|
+
},
|
|
17
|
+
bigint: {
|
|
18
|
+
mysql: "BIGINT",
|
|
19
|
+
mariadb: "BIGINT",
|
|
20
|
+
sqlite: "INTEGER",
|
|
21
|
+
file: "BIGINT"
|
|
22
|
+
},
|
|
23
|
+
varchar: {
|
|
24
|
+
mysql: `VARCHAR(${col.length ?? 255})`,
|
|
25
|
+
mariadb: `VARCHAR(${col.length ?? 255})`,
|
|
26
|
+
sqlite: "TEXT",
|
|
27
|
+
file: "VARCHAR"
|
|
28
|
+
},
|
|
29
|
+
char: {
|
|
30
|
+
mysql: `CHAR(${col.length ?? 1})`,
|
|
31
|
+
mariadb: `CHAR(${col.length ?? 1})`,
|
|
32
|
+
sqlite: "TEXT",
|
|
33
|
+
file: "CHAR"
|
|
34
|
+
},
|
|
35
|
+
text: {
|
|
36
|
+
mysql: "TEXT",
|
|
37
|
+
mariadb: "TEXT",
|
|
38
|
+
sqlite: "TEXT",
|
|
39
|
+
file: "TEXT"
|
|
40
|
+
},
|
|
41
|
+
boolean: {
|
|
42
|
+
mysql: "TINYINT(1)",
|
|
43
|
+
mariadb: "TINYINT(1)",
|
|
44
|
+
sqlite: "INTEGER",
|
|
45
|
+
file: "BOOLEAN"
|
|
46
|
+
},
|
|
47
|
+
json: {
|
|
48
|
+
mysql: "JSON",
|
|
49
|
+
mariadb: "JSON",
|
|
50
|
+
sqlite: "TEXT",
|
|
51
|
+
file: "JSON"
|
|
52
|
+
},
|
|
53
|
+
datetime: {
|
|
54
|
+
mysql: "DATETIME",
|
|
55
|
+
mariadb: "DATETIME",
|
|
56
|
+
sqlite: "TEXT",
|
|
57
|
+
file: "DATETIME"
|
|
58
|
+
},
|
|
59
|
+
timestamp: {
|
|
60
|
+
mysql: "TIMESTAMP",
|
|
61
|
+
mariadb: "TIMESTAMP",
|
|
62
|
+
sqlite: "TEXT",
|
|
63
|
+
file: "TIMESTAMP"
|
|
64
|
+
},
|
|
65
|
+
decimal: {
|
|
66
|
+
mysql: `DECIMAL(${col.precision ?? 10},${col.scale ?? 2})`,
|
|
67
|
+
mariadb: `DECIMAL(${col.precision ?? 10},${col.scale ?? 2})`,
|
|
68
|
+
sqlite: "REAL",
|
|
69
|
+
file: "DECIMAL"
|
|
70
|
+
},
|
|
71
|
+
float: {
|
|
72
|
+
mysql: "FLOAT",
|
|
73
|
+
mariadb: "FLOAT",
|
|
74
|
+
sqlite: "REAL",
|
|
75
|
+
file: "FLOAT"
|
|
76
|
+
},
|
|
77
|
+
blob: {
|
|
78
|
+
mysql: "BLOB",
|
|
79
|
+
mariadb: "BLOB",
|
|
80
|
+
sqlite: "BLOB",
|
|
81
|
+
file: "BLOB"
|
|
82
|
+
}
|
|
83
|
+
}[col.type]?.[name] ?? "TEXT";
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Compile a column definition to a SQL fragment
|
|
87
|
+
* @param {ColumnMetadata} col - Column metadata
|
|
88
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
89
|
+
* @returns {string} SQL column definition
|
|
90
|
+
*/
|
|
91
|
+
function compileColumnDef(col, dialect) {
|
|
92
|
+
const parts = [`\`${col.name}\``, compileColumnType(col, dialect)];
|
|
93
|
+
if (col.primaryKey) parts.push("PRIMARY KEY");
|
|
94
|
+
if (col.autoIncrement) parts.push(dialect === "mysql" ? "AUTO_INCREMENT" : "AUTOINCREMENT");
|
|
95
|
+
if (col.notNull && !col.primaryKey) parts.push("NOT NULL");
|
|
96
|
+
if (col.unique && !col.primaryKey) parts.push("UNIQUE");
|
|
97
|
+
if (col.defaultValue !== void 0) if (typeof col.defaultValue === "string") parts.push(`DEFAULT '${col.defaultValue}'`);
|
|
98
|
+
else if (col.defaultValue === null) parts.push("DEFAULT NULL");
|
|
99
|
+
else parts.push(`DEFAULT ${col.defaultValue}`);
|
|
100
|
+
return parts.join(" ");
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Compile a CREATE TABLE statement from table metadata
|
|
104
|
+
* @param {TableMetadata} table - Table metadata
|
|
105
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
106
|
+
* @returns {string} CREATE TABLE SQL statement
|
|
107
|
+
*/
|
|
108
|
+
function compileCreateTable(table, dialect) {
|
|
109
|
+
const columnDefs = [];
|
|
110
|
+
const constraints = [];
|
|
111
|
+
for (const col of table.columns) {
|
|
112
|
+
columnDefs.push(compileColumnDef(col, dialect));
|
|
113
|
+
if (col.references) {
|
|
114
|
+
const onDelete = col.references.onDelete ? ` ON DELETE ${col.references.onDelete}` : "";
|
|
115
|
+
const onUpdate = col.references.onUpdate ? ` ON UPDATE ${col.references.onUpdate}` : "";
|
|
116
|
+
constraints.push(`FOREIGN KEY (\`${col.name}\`) REFERENCES \`${col.references.table}\`(\`${col.references.column}\`)${onDelete}${onUpdate}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const allDefs = [...columnDefs, ...constraints].join(", ");
|
|
120
|
+
return `CREATE TABLE IF NOT EXISTS \`${table.name}\` (${allDefs})`;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Compile a WHERE clause from a condition object
|
|
124
|
+
* @param {WhereClause} where - Where clause object
|
|
125
|
+
* @param {unknown[]} params - Parameter array to push values into
|
|
126
|
+
* @returns {string} SQL WHERE fragment
|
|
127
|
+
*/
|
|
128
|
+
function compileWhere(where, params) {
|
|
129
|
+
const conditions = [];
|
|
130
|
+
for (const [key, value] of Object.entries(where)) {
|
|
131
|
+
if (key === "OR" && Array.isArray(value)) {
|
|
132
|
+
const orParts = value.map((sub) => compileWhere(sub, params));
|
|
133
|
+
if (orParts.length > 0) conditions.push(`(${orParts.join(" OR ")})`);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (key === "AND" && Array.isArray(value)) {
|
|
137
|
+
const andParts = value.map((sub) => compileWhere(sub, params));
|
|
138
|
+
if (andParts.length > 0) conditions.push(`(${andParts.join(" AND ")})`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
142
|
+
const cond = value;
|
|
143
|
+
if (cond.eq !== void 0) {
|
|
144
|
+
params.push(cond.eq);
|
|
145
|
+
conditions.push(`\`${key}\` = ?`);
|
|
146
|
+
}
|
|
147
|
+
if (cond.neq !== void 0) {
|
|
148
|
+
params.push(cond.neq);
|
|
149
|
+
conditions.push(`\`${key}\` != ?`);
|
|
150
|
+
}
|
|
151
|
+
if (cond.gt !== void 0) {
|
|
152
|
+
params.push(cond.gt);
|
|
153
|
+
conditions.push(`\`${key}\` > ?`);
|
|
154
|
+
}
|
|
155
|
+
if (cond.gte !== void 0) {
|
|
156
|
+
params.push(cond.gte);
|
|
157
|
+
conditions.push(`\`${key}\` >= ?`);
|
|
158
|
+
}
|
|
159
|
+
if (cond.lt !== void 0) {
|
|
160
|
+
params.push(cond.lt);
|
|
161
|
+
conditions.push(`\`${key}\` < ?`);
|
|
162
|
+
}
|
|
163
|
+
if (cond.lte !== void 0) {
|
|
164
|
+
params.push(cond.lte);
|
|
165
|
+
conditions.push(`\`${key}\` <= ?`);
|
|
166
|
+
}
|
|
167
|
+
if (cond.like !== void 0) {
|
|
168
|
+
params.push(cond.like);
|
|
169
|
+
conditions.push(`\`${key}\` LIKE ?`);
|
|
170
|
+
}
|
|
171
|
+
if (cond.notLike !== void 0) {
|
|
172
|
+
params.push(cond.notLike);
|
|
173
|
+
conditions.push(`\`${key}\` NOT LIKE ?`);
|
|
174
|
+
}
|
|
175
|
+
if (cond.in !== void 0 && Array.isArray(cond.in)) {
|
|
176
|
+
const placeholders = cond.in.map(() => "?").join(", ");
|
|
177
|
+
params.push(...cond.in);
|
|
178
|
+
conditions.push(`\`${key}\` IN (${placeholders})`);
|
|
179
|
+
}
|
|
180
|
+
if (cond.notIn !== void 0 && Array.isArray(cond.notIn)) {
|
|
181
|
+
const placeholders = cond.notIn.map(() => "?").join(", ");
|
|
182
|
+
params.push(...cond.notIn);
|
|
183
|
+
conditions.push(`\`${key}\` NOT IN (${placeholders})`);
|
|
184
|
+
}
|
|
185
|
+
if (cond.isNull === true) conditions.push(`\`${key}\` IS NULL`);
|
|
186
|
+
if (cond.isNull === false) conditions.push(`\`${key}\` IS NOT NULL`);
|
|
187
|
+
if (cond.between !== void 0 && Array.isArray(cond.between)) {
|
|
188
|
+
params.push(cond.between[0], cond.between[1]);
|
|
189
|
+
conditions.push(`\`${key}\` BETWEEN ? AND ?`);
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
params.push(value);
|
|
193
|
+
conditions.push(`\`${key}\` = ?`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return conditions.length > 0 ? conditions.join(" AND ") : "1=1";
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Compile a SELECT query from options
|
|
200
|
+
* @param {string} tableName - Table name
|
|
201
|
+
* @param {object} options - Query options
|
|
202
|
+
* @param {unknown[]} params - Parameter array
|
|
203
|
+
* @returns {string} SQL SELECT statement
|
|
204
|
+
*/
|
|
205
|
+
function compileSelect(tableName, options, params) {
|
|
206
|
+
let sql = `SELECT ${options.select?.map((c) => `\`${String(c)}\``).join(", ") ?? "*"} FROM \`${tableName}\``;
|
|
207
|
+
if (options.where && Object.keys(options.where).length > 0) sql += ` WHERE ${compileWhere(options.where, params)}`;
|
|
208
|
+
if (options.orderBy) {
|
|
209
|
+
const orderParts = Object.entries(options.orderBy).map(([col, dir]) => `\`${col}\` ${dir.toUpperCase()}`);
|
|
210
|
+
if (orderParts.length > 0) sql += ` ORDER BY ${orderParts.join(", ")}`;
|
|
211
|
+
}
|
|
212
|
+
if (options.take !== void 0) sql += ` LIMIT ${options.take}`;
|
|
213
|
+
if (options.skip !== void 0) sql += ` OFFSET ${options.skip}`;
|
|
214
|
+
return sql;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Compile an INSERT statement
|
|
218
|
+
* @param {string} tableName - Table name
|
|
219
|
+
* @param {Record<string, unknown>} data - Data to insert
|
|
220
|
+
* @param {unknown[]} params - Parameter array
|
|
221
|
+
* @returns {string} SQL INSERT statement
|
|
222
|
+
*/
|
|
223
|
+
function compileInsert(tableName, data, params) {
|
|
224
|
+
const keys = Object.keys(data);
|
|
225
|
+
const cols = keys.map((k) => `\`${k}\``).join(", ");
|
|
226
|
+
const placeholders = keys.map(() => "?").join(", ");
|
|
227
|
+
params.push(...keys.map((k) => data[k]));
|
|
228
|
+
return `INSERT INTO \`${tableName}\` (${cols}) VALUES (${placeholders})`;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Compile a bulk INSERT statement for multiple rows
|
|
232
|
+
* @param {string} tableName - Table name
|
|
233
|
+
* @param {Record<string, unknown>[]} data - Array of data to insert
|
|
234
|
+
* @param {unknown[]} params - Parameter array
|
|
235
|
+
* @returns {string} SQL bulk INSERT statement
|
|
236
|
+
*/
|
|
237
|
+
function compileBulkInsert(tableName, data, params) {
|
|
238
|
+
if (data.length === 0) return "";
|
|
239
|
+
const keys = Object.keys(data[0]);
|
|
240
|
+
const cols = keys.map((k) => `\`${k}\``).join(", ");
|
|
241
|
+
const valuePlaceholders = data.map(() => `(${keys.map(() => "?").join(", ")})`).join(", ");
|
|
242
|
+
for (const row of data) for (const key of keys) params.push(row[key]);
|
|
243
|
+
return `INSERT INTO \`${tableName}\` (${cols}) VALUES ${valuePlaceholders}`;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Compile an UPDATE statement
|
|
247
|
+
* @param {string} tableName - Table name
|
|
248
|
+
* @param {Record<string, unknown>} data - Data to update
|
|
249
|
+
* @param {WhereClause} where - Where clause
|
|
250
|
+
* @param {unknown[]} params - Parameter array
|
|
251
|
+
* @returns {string} SQL UPDATE statement
|
|
252
|
+
*/
|
|
253
|
+
function compileUpdate(tableName, data, where, params) {
|
|
254
|
+
let sql = `UPDATE \`${tableName}\` SET ${Object.keys(data).map((k) => {
|
|
255
|
+
params.push(data[k]);
|
|
256
|
+
return `\`${k}\` = ?`;
|
|
257
|
+
}).join(", ")}`;
|
|
258
|
+
if (Object.keys(where).length > 0) sql += ` WHERE ${compileWhere(where, params)}`;
|
|
259
|
+
return sql;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Compile a DELETE statement
|
|
263
|
+
* @param {string} tableName - Table name
|
|
264
|
+
* @param {WhereClause} where - Where clause
|
|
265
|
+
* @param {unknown[]} params - Parameter array
|
|
266
|
+
* @returns {string} SQL DELETE statement
|
|
267
|
+
*/
|
|
268
|
+
function compileDelete(tableName, where, params) {
|
|
269
|
+
let sql = `DELETE FROM \`${tableName}\``;
|
|
270
|
+
if (Object.keys(where).length > 0) sql += ` WHERE ${compileWhere(where, params)}`;
|
|
271
|
+
return sql;
|
|
272
|
+
}
|
|
273
|
+
//#endregion
|
|
274
|
+
exports.compileBulkInsert = compileBulkInsert;
|
|
275
|
+
exports.compileColumnDef = compileColumnDef;
|
|
276
|
+
exports.compileColumnType = compileColumnType;
|
|
277
|
+
exports.compileCreateTable = compileCreateTable;
|
|
278
|
+
exports.compileDelete = compileDelete;
|
|
279
|
+
exports.compileInsert = compileInsert;
|
|
280
|
+
exports.compileSelect = compileSelect;
|
|
281
|
+
exports.compileUpdate = compileUpdate;
|
|
282
|
+
exports.compileWhere = compileWhere;
|
|
283
|
+
|
|
284
|
+
//# sourceMappingURL=sql-compiler.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-compiler.cjs","names":[],"sources":["../../src/drivers/sql-compiler.ts"],"sourcesContent":["import type {\n ColumnDataType,\n ColumnMetadata,\n DatabaseType,\n TableMetadata,\n WhereClause,\n WhereCondition,\n} from \"../types\";\n\n/**\n * Compile column type to SQL string for a specific database dialect\n * @param {ColumnMetadata} col - Column metadata\n * @param {DatabaseType} dialect - Database dialect\n * @returns {string} SQL column type string\n */\nexport function compileColumnType(col: ColumnMetadata, dialect: DatabaseType): string {\n const name = typeof dialect === \"string\" ? dialect : dialect.name;\n\n const typeMap: Record<ColumnDataType, Record<string, string>> = {\n integer: { mysql: \"INT\", mariadb: \"INT\", sqlite: \"INTEGER\", file: \"INTEGER\" },\n bigint: { mysql: \"BIGINT\", mariadb: \"BIGINT\", sqlite: \"INTEGER\", file: \"BIGINT\" },\n varchar: {\n mysql: `VARCHAR(${col.length ?? 255})`,\n mariadb: `VARCHAR(${col.length ?? 255})`,\n sqlite: \"TEXT\",\n file: \"VARCHAR\",\n },\n char: {\n mysql: `CHAR(${col.length ?? 1})`,\n mariadb: `CHAR(${col.length ?? 1})`,\n sqlite: \"TEXT\",\n file: \"CHAR\",\n },\n text: { mysql: \"TEXT\", mariadb: \"TEXT\", sqlite: \"TEXT\", file: \"TEXT\" },\n boolean: { mysql: \"TINYINT(1)\", mariadb: \"TINYINT(1)\", sqlite: \"INTEGER\", file: \"BOOLEAN\" },\n json: { mysql: \"JSON\", mariadb: \"JSON\", sqlite: \"TEXT\", file: \"JSON\" },\n datetime: { mysql: \"DATETIME\", mariadb: \"DATETIME\", sqlite: \"TEXT\", file: \"DATETIME\" },\n timestamp: { mysql: \"TIMESTAMP\", mariadb: \"TIMESTAMP\", sqlite: \"TEXT\", file: \"TIMESTAMP\" },\n decimal: {\n mysql: `DECIMAL(${col.precision ?? 10},${col.scale ?? 2})`,\n mariadb: `DECIMAL(${col.precision ?? 10},${col.scale ?? 2})`,\n sqlite: \"REAL\",\n file: \"DECIMAL\",\n },\n float: { mysql: \"FLOAT\", mariadb: \"FLOAT\", sqlite: \"REAL\", file: \"FLOAT\" },\n blob: { mysql: \"BLOB\", mariadb: \"BLOB\", sqlite: \"BLOB\", file: \"BLOB\" },\n };\n\n return typeMap[col.type]?.[name] ?? \"TEXT\";\n}\n\n/**\n * Compile a column definition to a SQL fragment\n * @param {ColumnMetadata} col - Column metadata\n * @param {DatabaseType} dialect - Database dialect\n * @returns {string} SQL column definition\n */\nexport function compileColumnDef(col: ColumnMetadata, dialect: DatabaseType): string {\n const parts: string[] = [`\\`${col.name}\\``, compileColumnType(col, dialect)];\n\n if (col.primaryKey) {\n parts.push(\"PRIMARY KEY\");\n }\n if (col.autoIncrement) {\n parts.push(dialect === \"mysql\" ? \"AUTO_INCREMENT\" : \"AUTOINCREMENT\");\n }\n if (col.notNull && !col.primaryKey) {\n parts.push(\"NOT NULL\");\n }\n if (col.unique && !col.primaryKey) {\n parts.push(\"UNIQUE\");\n }\n if (col.defaultValue !== undefined) {\n if (typeof col.defaultValue === \"string\") {\n parts.push(`DEFAULT '${col.defaultValue}'`);\n } else if (col.defaultValue === null) {\n parts.push(\"DEFAULT NULL\");\n } else {\n parts.push(`DEFAULT ${col.defaultValue}`);\n }\n }\n\n return parts.join(\" \");\n}\n\n/**\n * Compile a CREATE TABLE statement from table metadata\n * @param {TableMetadata} table - Table metadata\n * @param {DatabaseType} dialect - Database dialect\n * @returns {string} CREATE TABLE SQL statement\n */\nexport function compileCreateTable(table: TableMetadata, dialect: DatabaseType): string {\n const columnDefs: string[] = [];\n const constraints: string[] = [];\n\n for (const col of table.columns) {\n columnDefs.push(compileColumnDef(col, dialect));\n\n if (col.references) {\n const onDelete = col.references.onDelete ? ` ON DELETE ${col.references.onDelete}` : \"\";\n const onUpdate = col.references.onUpdate ? ` ON UPDATE ${col.references.onUpdate}` : \"\";\n constraints.push(\n `FOREIGN KEY (\\`${col.name}\\`) REFERENCES \\`${col.references.table}\\`(\\`${col.references.column}\\`)${onDelete}${onUpdate}`,\n );\n }\n }\n\n const allDefs = [...columnDefs, ...constraints].join(\", \");\n return `CREATE TABLE IF NOT EXISTS \\`${table.name}\\` (${allDefs})`;\n}\n\n/**\n * Compile a WHERE clause from a condition object\n * @param {WhereClause} where - Where clause object\n * @param {unknown[]} params - Parameter array to push values into\n * @returns {string} SQL WHERE fragment\n */\nexport function compileWhere(where: WhereClause, params: unknown[]): string {\n const conditions: string[] = [];\n\n for (const [key, value] of Object.entries(where)) {\n if (key === \"OR\" && Array.isArray(value)) {\n const orParts = (value as WhereClause[]).map((sub) => compileWhere(sub, params));\n if (orParts.length > 0) {\n conditions.push(`(${orParts.join(\" OR \")})`);\n }\n continue;\n }\n if (key === \"AND\" && Array.isArray(value)) {\n const andParts = (value as WhereClause[]).map((sub) => compileWhere(sub, params));\n if (andParts.length > 0) {\n conditions.push(`(${andParts.join(\" AND \")})`);\n }\n continue;\n }\n\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n const cond = value as WhereCondition;\n if (cond.eq !== undefined) {\n params.push(cond.eq);\n conditions.push(`\\`${key}\\` = ?`);\n }\n if (cond.neq !== undefined) {\n params.push(cond.neq);\n conditions.push(`\\`${key}\\` != ?`);\n }\n if (cond.gt !== undefined) {\n params.push(cond.gt);\n conditions.push(`\\`${key}\\` > ?`);\n }\n if (cond.gte !== undefined) {\n params.push(cond.gte);\n conditions.push(`\\`${key}\\` >= ?`);\n }\n if (cond.lt !== undefined) {\n params.push(cond.lt);\n conditions.push(`\\`${key}\\` < ?`);\n }\n if (cond.lte !== undefined) {\n params.push(cond.lte);\n conditions.push(`\\`${key}\\` <= ?`);\n }\n if (cond.like !== undefined) {\n params.push(cond.like);\n conditions.push(`\\`${key}\\` LIKE ?`);\n }\n if (cond.notLike !== undefined) {\n params.push(cond.notLike);\n conditions.push(`\\`${key}\\` NOT LIKE ?`);\n }\n if (cond.in !== undefined && Array.isArray(cond.in)) {\n const placeholders = cond.in.map(() => \"?\").join(\", \");\n params.push(...cond.in);\n conditions.push(`\\`${key}\\` IN (${placeholders})`);\n }\n if (cond.notIn !== undefined && Array.isArray(cond.notIn)) {\n const placeholders = cond.notIn.map(() => \"?\").join(\", \");\n params.push(...cond.notIn);\n conditions.push(`\\`${key}\\` NOT IN (${placeholders})`);\n }\n if (cond.isNull === true) {\n conditions.push(`\\`${key}\\` IS NULL`);\n }\n if (cond.isNull === false) {\n conditions.push(`\\`${key}\\` IS NOT NULL`);\n }\n if (cond.between !== undefined && Array.isArray(cond.between)) {\n params.push(cond.between[0], cond.between[1]);\n conditions.push(`\\`${key}\\` BETWEEN ? AND ?`);\n }\n } else {\n params.push(value);\n conditions.push(`\\`${key}\\` = ?`);\n }\n }\n\n return conditions.length > 0 ? conditions.join(\" AND \") : \"1=1\";\n}\n\n/**\n * Compile a SELECT query from options\n * @param {string} tableName - Table name\n * @param {object} options - Query options\n * @param {unknown[]} params - Parameter array\n * @returns {string} SQL SELECT statement\n */\nexport function compileSelect(\n tableName: string,\n options: {\n select?: string[];\n where?: WhereClause;\n orderBy?: Record<string, \"asc\" | \"desc\">;\n take?: number;\n skip?: number;\n },\n params: unknown[],\n): string {\n const cols = options.select?.map((c) => `\\`${String(c)}\\``).join(\", \") ?? \"*\";\n let sql = `SELECT ${cols} FROM \\`${tableName}\\``;\n\n if (options.where && Object.keys(options.where).length > 0) {\n sql += ` WHERE ${compileWhere(options.where, params)}`;\n }\n if (options.orderBy) {\n const orderParts = Object.entries(options.orderBy).map(\n ([col, dir]) => `\\`${col}\\` ${dir.toUpperCase()}`,\n );\n if (orderParts.length > 0) {\n sql += ` ORDER BY ${orderParts.join(\", \")}`;\n }\n }\n if (options.take !== undefined) {\n sql += ` LIMIT ${options.take}`;\n }\n if (options.skip !== undefined) {\n sql += ` OFFSET ${options.skip}`;\n }\n\n return sql;\n}\n\n/**\n * Compile an INSERT statement\n * @param {string} tableName - Table name\n * @param {Record<string, unknown>} data - Data to insert\n * @param {unknown[]} params - Parameter array\n * @returns {string} SQL INSERT statement\n */\nexport function compileInsert(\n tableName: string,\n data: Record<string, unknown>,\n params: unknown[],\n): string {\n const keys = Object.keys(data);\n const cols = keys.map((k) => `\\`${k}\\``).join(\", \");\n const placeholders = keys.map(() => \"?\").join(\", \");\n params.push(...keys.map((k) => data[k]));\n return `INSERT INTO \\`${tableName}\\` (${cols}) VALUES (${placeholders})`;\n}\n\n/**\n * Compile a bulk INSERT statement for multiple rows\n * @param {string} tableName - Table name\n * @param {Record<string, unknown>[]} data - Array of data to insert\n * @param {unknown[]} params - Parameter array\n * @returns {string} SQL bulk INSERT statement\n */\nexport function compileBulkInsert(\n tableName: string,\n data: Record<string, unknown>[],\n params: unknown[],\n): string {\n if (data.length === 0) {\n return \"\";\n }\n const keys = Object.keys(data[0]!);\n const cols = keys.map((k) => `\\`${k}\\``).join(\", \");\n const valuePlaceholders = data.map(() => `(${keys.map(() => \"?\").join(\", \")})`).join(\", \");\n\n for (const row of data) {\n for (const key of keys) {\n params.push(row[key]);\n }\n }\n\n return `INSERT INTO \\`${tableName}\\` (${cols}) VALUES ${valuePlaceholders}`;\n}\n\n/**\n * Compile an UPDATE statement\n * @param {string} tableName - Table name\n * @param {Record<string, unknown>} data - Data to update\n * @param {WhereClause} where - Where clause\n * @param {unknown[]} params - Parameter array\n * @returns {string} SQL UPDATE statement\n */\nexport function compileUpdate(\n tableName: string,\n data: Record<string, unknown>,\n where: WhereClause,\n params: unknown[],\n): string {\n const setParts = Object.keys(data).map((k) => {\n params.push(data[k]);\n return `\\`${k}\\` = ?`;\n });\n let sql = `UPDATE \\`${tableName}\\` SET ${setParts.join(\", \")}`;\n if (Object.keys(where).length > 0) {\n sql += ` WHERE ${compileWhere(where, params)}`;\n }\n return sql;\n}\n\n/**\n * Compile a DELETE statement\n * @param {string} tableName - Table name\n * @param {WhereClause} where - Where clause\n * @param {unknown[]} params - Parameter array\n * @returns {string} SQL DELETE statement\n */\nexport function compileDelete(tableName: string, where: WhereClause, params: unknown[]): string {\n let sql = `DELETE FROM \\`${tableName}\\``;\n if (Object.keys(where).length > 0) {\n sql += ` WHERE ${compileWhere(where, params)}`;\n }\n return sql;\n}\n"],"mappings":";;;;;;;AAeA,SAAgB,kBAAkB,KAAqB,SAA+B;CACpF,MAAM,OAAO,OAAO,YAAY,WAAW,UAAU,QAAQ;AAgC7D,QA9BgE;EAC9D,SAAS;GAAE,OAAO;GAAO,SAAS;GAAO,QAAQ;GAAW,MAAM;GAAW;EAC7E,QAAQ;GAAE,OAAO;GAAU,SAAS;GAAU,QAAQ;GAAW,MAAM;GAAU;EACjF,SAAS;GACP,OAAO,WAAW,IAAI,UAAU,IAAI;GACpC,SAAS,WAAW,IAAI,UAAU,IAAI;GACtC,QAAQ;GACR,MAAM;GACP;EACD,MAAM;GACJ,OAAO,QAAQ,IAAI,UAAU,EAAE;GAC/B,SAAS,QAAQ,IAAI,UAAU,EAAE;GACjC,QAAQ;GACR,MAAM;GACP;EACD,MAAM;GAAE,OAAO;GAAQ,SAAS;GAAQ,QAAQ;GAAQ,MAAM;GAAQ;EACtE,SAAS;GAAE,OAAO;GAAc,SAAS;GAAc,QAAQ;GAAW,MAAM;GAAW;EAC3F,MAAM;GAAE,OAAO;GAAQ,SAAS;GAAQ,QAAQ;GAAQ,MAAM;GAAQ;EACtE,UAAU;GAAE,OAAO;GAAY,SAAS;GAAY,QAAQ;GAAQ,MAAM;GAAY;EACtF,WAAW;GAAE,OAAO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AK,KAAK;CACnD,MAAM,oBAAoB,KAAK,UAAU,IAAI,KAAK,UAAU,IAAI,CAAC,KAAK,KAAK,CAAC,GAAG,CAAC,KAAK,KAAK;AAE1F,MAAK,MAAM,OAAO,KAChB,MAAK,MAAM,OAAO,KAChB,QAAO,KAAK,IAAI,KAAK;AAIzB,QAAO,iBAAiB,UAAU,MAAM,KAAK,WAAW;;;;;;;;;;AAW1D,SAAgB,cACd,WACA,MACA,OACA,QACQ;CAKR,IAAI,MAAM,YAAY,UAAU,SAJf,OAAO,KAAK,KAAK,CAAC,KAAK,MAAM;AAC5C,SAAO,KAAK,KAAK,GAAG;AACpB,SAAO,KAAK,EAAE;GACd,CACgD,KAAK,KAAK;AAC5D,KAAI,OAAO,KAAK,MAAM,CAAC,SAAS,EAC9B,QAAO,UAAU,aAAa,OAAO,OAAO;AAE9C,QAAO;;;;;;;;;AAUT,SAAgB,cAAc,WAAmB,OAAoB,QAA2B;CAC9F,IAAI,MAAM,iBAAiB,UAAU;AACrC,KAAI,OAAO,KAAK,MAAM,CAAC,SAAS,EAC9B,QAAO,UAAU,aAAa,OAAO,OAAO;AAE9C,QAAO"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ColumnMetadata, DatabaseType, TableMetadata, WhereClause } from "../types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/drivers/sql-compiler.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Compile a column definition to a SQL fragment
|
|
6
|
+
* @param {ColumnMetadata} col - Column metadata
|
|
7
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
8
|
+
* @returns {string} SQL column definition
|
|
9
|
+
*/
|
|
10
|
+
declare function compileColumnDef(col: ColumnMetadata, dialect: DatabaseType): string;
|
|
11
|
+
/**
|
|
12
|
+
* Compile a CREATE TABLE statement from table metadata
|
|
13
|
+
* @param {TableMetadata} table - Table metadata
|
|
14
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
15
|
+
* @returns {string} CREATE TABLE SQL statement
|
|
16
|
+
*/
|
|
17
|
+
declare function compileCreateTable(table: TableMetadata, dialect: DatabaseType): string;
|
|
18
|
+
/**
|
|
19
|
+
* Compile a WHERE clause from a condition object
|
|
20
|
+
* @param {WhereClause} where - Where clause object
|
|
21
|
+
* @param {unknown[]} params - Parameter array to push values into
|
|
22
|
+
* @returns {string} SQL WHERE fragment
|
|
23
|
+
*/
|
|
24
|
+
declare function compileWhere(where: WhereClause, params: unknown[]): string;
|
|
25
|
+
/**
|
|
26
|
+
* Compile a SELECT query from options
|
|
27
|
+
* @param {string} tableName - Table name
|
|
28
|
+
* @param {object} options - Query options
|
|
29
|
+
* @param {unknown[]} params - Parameter array
|
|
30
|
+
* @returns {string} SQL SELECT statement
|
|
31
|
+
*/
|
|
32
|
+
declare function compileSelect(tableName: string, options: {
|
|
33
|
+
select?: string[];
|
|
34
|
+
where?: WhereClause;
|
|
35
|
+
orderBy?: Record<string, "asc" | "desc">;
|
|
36
|
+
take?: number;
|
|
37
|
+
skip?: number;
|
|
38
|
+
}, params: unknown[]): string;
|
|
39
|
+
/**
|
|
40
|
+
* Compile an INSERT statement
|
|
41
|
+
* @param {string} tableName - Table name
|
|
42
|
+
* @param {Record<string, unknown>} data - Data to insert
|
|
43
|
+
* @param {unknown[]} params - Parameter array
|
|
44
|
+
* @returns {string} SQL INSERT statement
|
|
45
|
+
*/
|
|
46
|
+
declare function compileInsert(tableName: string, data: Record<string, unknown>, params: unknown[]): string;
|
|
47
|
+
/**
|
|
48
|
+
* Compile an UPDATE statement
|
|
49
|
+
* @param {string} tableName - Table name
|
|
50
|
+
* @param {Record<string, unknown>} data - Data to update
|
|
51
|
+
* @param {WhereClause} where - Where clause
|
|
52
|
+
* @param {unknown[]} params - Parameter array
|
|
53
|
+
* @returns {string} SQL UPDATE statement
|
|
54
|
+
*/
|
|
55
|
+
declare function compileUpdate(tableName: string, data: Record<string, unknown>, where: WhereClause, params: unknown[]): string;
|
|
56
|
+
/**
|
|
57
|
+
* Compile a DELETE statement
|
|
58
|
+
* @param {string} tableName - Table name
|
|
59
|
+
* @param {WhereClause} where - Where clause
|
|
60
|
+
* @param {unknown[]} params - Parameter array
|
|
61
|
+
* @returns {string} SQL DELETE statement
|
|
62
|
+
*/
|
|
63
|
+
declare function compileDelete(tableName: string, where: WhereClause, params: unknown[]): string;
|
|
64
|
+
//#endregion
|
|
65
|
+
export { compileColumnDef, compileCreateTable, compileDelete, compileInsert, compileSelect, compileUpdate, compileWhere };
|
|
66
|
+
//# sourceMappingURL=sql-compiler.d.cts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ColumnMetadata, DatabaseType, TableMetadata, WhereClause } from "../types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/drivers/sql-compiler.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Compile a column definition to a SQL fragment
|
|
6
|
+
* @param {ColumnMetadata} col - Column metadata
|
|
7
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
8
|
+
* @returns {string} SQL column definition
|
|
9
|
+
*/
|
|
10
|
+
declare function compileColumnDef(col: ColumnMetadata, dialect: DatabaseType): string;
|
|
11
|
+
/**
|
|
12
|
+
* Compile a CREATE TABLE statement from table metadata
|
|
13
|
+
* @param {TableMetadata} table - Table metadata
|
|
14
|
+
* @param {DatabaseType} dialect - Database dialect
|
|
15
|
+
* @returns {string} CREATE TABLE SQL statement
|
|
16
|
+
*/
|
|
17
|
+
declare function compileCreateTable(table: TableMetadata, dialect: DatabaseType): string;
|
|
18
|
+
/**
|
|
19
|
+
* Compile a WHERE clause from a condition object
|
|
20
|
+
* @param {WhereClause} where - Where clause object
|
|
21
|
+
* @param {unknown[]} params - Parameter array to push values into
|
|
22
|
+
* @returns {string} SQL WHERE fragment
|
|
23
|
+
*/
|
|
24
|
+
declare function compileWhere(where: WhereClause, params: unknown[]): string;
|
|
25
|
+
/**
|
|
26
|
+
* Compile a SELECT query from options
|
|
27
|
+
* @param {string} tableName - Table name
|
|
28
|
+
* @param {object} options - Query options
|
|
29
|
+
* @param {unknown[]} params - Parameter array
|
|
30
|
+
* @returns {string} SQL SELECT statement
|
|
31
|
+
*/
|
|
32
|
+
declare function compileSelect(tableName: string, options: {
|
|
33
|
+
select?: string[];
|
|
34
|
+
where?: WhereClause;
|
|
35
|
+
orderBy?: Record<string, "asc" | "desc">;
|
|
36
|
+
take?: number;
|
|
37
|
+
skip?: number;
|
|
38
|
+
}, params: unknown[]): string;
|
|
39
|
+
/**
|
|
40
|
+
* Compile an INSERT statement
|
|
41
|
+
* @param {string} tableName - Table name
|
|
42
|
+
* @param {Record<string, unknown>} data - Data to insert
|
|
43
|
+
* @param {unknown[]} params - Parameter array
|
|
44
|
+
* @returns {string} SQL INSERT statement
|
|
45
|
+
*/
|
|
46
|
+
declare function compileInsert(tableName: string, data: Record<string, unknown>, params: unknown[]): string;
|
|
47
|
+
/**
|
|
48
|
+
* Compile an UPDATE statement
|
|
49
|
+
* @param {string} tableName - Table name
|
|
50
|
+
* @param {Record<string, unknown>} data - Data to update
|
|
51
|
+
* @param {WhereClause} where - Where clause
|
|
52
|
+
* @param {unknown[]} params - Parameter array
|
|
53
|
+
* @returns {string} SQL UPDATE statement
|
|
54
|
+
*/
|
|
55
|
+
declare function compileUpdate(tableName: string, data: Record<string, unknown>, where: WhereClause, params: unknown[]): string;
|
|
56
|
+
/**
|
|
57
|
+
* Compile a DELETE statement
|
|
58
|
+
* @param {string} tableName - Table name
|
|
59
|
+
* @param {WhereClause} where - Where clause
|
|
60
|
+
* @param {unknown[]} params - Parameter array
|
|
61
|
+
* @returns {string} SQL DELETE statement
|
|
62
|
+
*/
|
|
63
|
+
declare function compileDelete(tableName: string, where: WhereClause, params: unknown[]): string;
|
|
64
|
+
//#endregion
|
|
65
|
+
export { compileColumnDef, compileCreateTable, compileDelete, compileInsert, compileSelect, compileUpdate, compileWhere };
|
|
66
|
+
//# sourceMappingURL=sql-compiler.d.mts.map
|