@hedystia/db 2.0.0 → 2.0.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/_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 +416 -0
- package/dist/core/repository.cjs.map +1 -0
- package/dist/core/repository.d.cts +110 -0
- package/dist/core/repository.d.mts +110 -0
- package/dist/core/repository.mjs +416 -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 +69 -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 +161 -0
- package/dist/schema/column.cjs.map +1 -0
- package/dist/schema/column.d.cts +120 -0
- package/dist/schema/column.d.mts +120 -0
- package/dist/schema/column.mjs +161 -0
- package/dist/schema/column.mjs.map +1 -0
- package/dist/schema/columns/index.cjs +202 -0
- package/dist/schema/columns/index.cjs.map +1 -0
- package/dist/schema/columns/index.d.cts +141 -0
- package/dist/schema/columns/index.d.mts +141 -0
- package/dist/schema/columns/index.mjs +182 -0
- package/dist/schema/columns/index.mjs.map +1 -0
- package/dist/schema/registry.cjs +125 -0
- package/dist/schema/registry.cjs.map +1 -0
- package/dist/schema/registry.d.cts +66 -0
- package/dist/schema/registry.d.mts +66 -0
- package/dist/schema/registry.mjs +125 -0
- package/dist/schema/registry.mjs.map +1 -0
- package/dist/schema/table.cjs +39 -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 +39 -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 +229 -0
- package/dist/types.d.mts +229 -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 +87 -105
- package/index.d.ts +0 -65
- package/index.js +0 -1
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
const require_errors = require("../errors.cjs");
|
|
2
|
+
const require_file = require("../drivers/file.cjs");
|
|
3
|
+
const require_sql_compiler = require("../drivers/sql-compiler.cjs");
|
|
4
|
+
//#region src/core/repository.ts
|
|
5
|
+
/**
|
|
6
|
+
* Repository implementation that provides CRUD operations for a table
|
|
7
|
+
* @template T - The row type for this table
|
|
8
|
+
*/
|
|
9
|
+
var TableRepository = class {
|
|
10
|
+
tableName;
|
|
11
|
+
driver;
|
|
12
|
+
cache;
|
|
13
|
+
registry;
|
|
14
|
+
meta;
|
|
15
|
+
columnMap;
|
|
16
|
+
reverseColumnMap;
|
|
17
|
+
hasAliases;
|
|
18
|
+
constructor(tableName, driver, cache, registry) {
|
|
19
|
+
this.tableName = tableName;
|
|
20
|
+
this.driver = driver;
|
|
21
|
+
this.cache = cache;
|
|
22
|
+
this.registry = registry;
|
|
23
|
+
const meta = registry.getTable(tableName);
|
|
24
|
+
if (!meta) throw new require_errors.QueryError(`Table "${tableName}" is not registered`);
|
|
25
|
+
this.meta = meta;
|
|
26
|
+
this.columnMap = registry.getColumnMap(tableName);
|
|
27
|
+
this.reverseColumnMap = registry.getReverseColumnMap(tableName);
|
|
28
|
+
this.hasAliases = Object.entries(this.columnMap).some(([codeKey, dbName]) => codeKey !== dbName);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Find all rows matching the query options
|
|
32
|
+
* @param {QueryOptions<T>} [options] - Query options
|
|
33
|
+
* @returns {Promise<T[]>} Array of matching rows
|
|
34
|
+
*/
|
|
35
|
+
async find(options) {
|
|
36
|
+
return this.cache.getOrSet(this.tableName, "find", options, async () => {
|
|
37
|
+
const dbOptions = this.mapOptionsToDb(options);
|
|
38
|
+
let rows;
|
|
39
|
+
if (this.driver instanceof require_file.FileDriver) rows = this.mapRows(this.findFile(dbOptions));
|
|
40
|
+
else rows = this.mapRows(await this.findSQL(dbOptions));
|
|
41
|
+
if (options?.with) rows = await this.loadRelations(rows, options.with);
|
|
42
|
+
this.cacheEntities(rows);
|
|
43
|
+
return rows;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Find all rows matching the query options (alias for find)
|
|
48
|
+
* @param {QueryOptions<T>} [options] - Query options
|
|
49
|
+
* @returns {Promise<T[]>} Array of matching rows
|
|
50
|
+
*/
|
|
51
|
+
async findMany(options) {
|
|
52
|
+
return this.find(options);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Find the first row matching the query options
|
|
56
|
+
* @param {QueryOptions<T>} [options] - Query options
|
|
57
|
+
* @returns {Promise<T | null>} The first matching row or null
|
|
58
|
+
*/
|
|
59
|
+
async findFirst(options) {
|
|
60
|
+
return this.cache.getOrSet(this.tableName, "findFirst", options, async () => {
|
|
61
|
+
const opts = {
|
|
62
|
+
...this.mapOptionsToDb(options),
|
|
63
|
+
take: 1
|
|
64
|
+
};
|
|
65
|
+
let rows;
|
|
66
|
+
if (this.driver instanceof require_file.FileDriver) rows = this.mapRows(this.findFile(opts));
|
|
67
|
+
else rows = this.mapRows(await this.findSQL(opts));
|
|
68
|
+
if (rows.length === 0) return null;
|
|
69
|
+
if (options?.with) rows = await this.loadRelations(rows, options.with);
|
|
70
|
+
const row = rows[0] ?? null;
|
|
71
|
+
if (row) this.cacheEntity(row);
|
|
72
|
+
return row;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Insert one or more rows
|
|
77
|
+
* @param {Partial<T> | Partial<T>[]} data - Data to insert
|
|
78
|
+
* @returns {Promise<T>} The inserted row
|
|
79
|
+
*/
|
|
80
|
+
async insert(data) {
|
|
81
|
+
const single = Array.isArray(data) ? data[0] : data;
|
|
82
|
+
if (!single) throw new require_errors.QueryError("Insert data cannot be empty");
|
|
83
|
+
this.cache.invalidateTable(this.tableName);
|
|
84
|
+
const cleaned = this.toDbKeys(this.cleanData(single));
|
|
85
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
86
|
+
const id = this.driver.insertRow(this.tableName, cleaned);
|
|
87
|
+
const pk = this.registry.getPrimaryKey(this.tableName);
|
|
88
|
+
if (pk) cleaned[pk] = id;
|
|
89
|
+
const result = this.toCodeKeys(cleaned);
|
|
90
|
+
this.cacheEntity(result);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
const params = [];
|
|
94
|
+
const sql = require_sql_compiler.compileInsert(this.tableName, cleaned, params);
|
|
95
|
+
const result = await this.driver.execute(sql, params);
|
|
96
|
+
const pk = this.registry.getPrimaryKey(this.tableName);
|
|
97
|
+
if (pk && result.insertId) cleaned[pk] = result.insertId;
|
|
98
|
+
const mapped = this.toCodeKeys(cleaned);
|
|
99
|
+
this.cacheEntity(mapped);
|
|
100
|
+
return mapped;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Insert multiple rows
|
|
104
|
+
* @param {Partial<T>[]} data - Array of data to insert
|
|
105
|
+
* @returns {Promise<T[]>} The inserted rows
|
|
106
|
+
*/
|
|
107
|
+
async insertMany(data) {
|
|
108
|
+
if (data.length === 0) return [];
|
|
109
|
+
this.cache.invalidateTable(this.tableName);
|
|
110
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
111
|
+
const results = [];
|
|
112
|
+
for (const item of data) results.push(await this.insert(item));
|
|
113
|
+
return results;
|
|
114
|
+
}
|
|
115
|
+
const cleanedData = data.map((item) => this.toDbKeys(this.cleanData(item)));
|
|
116
|
+
const params = [];
|
|
117
|
+
const sql = require_sql_compiler.compileBulkInsert(this.tableName, cleanedData, params);
|
|
118
|
+
const result = await this.driver.execute(sql, params);
|
|
119
|
+
const pk = this.registry.getPrimaryKey(this.tableName);
|
|
120
|
+
if (pk && result.insertId) for (let i = 0; i < cleanedData.length; i++) {
|
|
121
|
+
const row = cleanedData[i];
|
|
122
|
+
if (row) row[pk] = result.insertId + i;
|
|
123
|
+
}
|
|
124
|
+
const finalRows = this.mapRows(cleanedData);
|
|
125
|
+
this.cacheEntities(finalRows);
|
|
126
|
+
return finalRows;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Update rows matching the where clause
|
|
130
|
+
* @param {UpdateOptions<T>} options - Update options with where and data
|
|
131
|
+
* @returns {Promise<T[]>} The updated rows
|
|
132
|
+
*/
|
|
133
|
+
async update(options) {
|
|
134
|
+
if (!options.where || Object.keys(options.where).length === 0) throw new require_errors.QueryError("Update requires a where clause");
|
|
135
|
+
this.cache.invalidateTable(this.tableName);
|
|
136
|
+
const cleaned = this.toDbKeys(this.cleanData(options.data));
|
|
137
|
+
const dbWhere = this.mapWhereToDb(options.where);
|
|
138
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
139
|
+
const filter = this.buildFileFilter(dbWhere);
|
|
140
|
+
this.driver.updateRows(this.tableName, filter, cleaned);
|
|
141
|
+
return this.find({ where: options.where });
|
|
142
|
+
}
|
|
143
|
+
const params = [];
|
|
144
|
+
const sql = require_sql_compiler.compileUpdate(this.tableName, cleaned, dbWhere, params);
|
|
145
|
+
await this.driver.execute(sql, params);
|
|
146
|
+
return this.find({ where: options.where });
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Delete rows matching the where clause
|
|
150
|
+
* @param {DeleteOptions<T>} options - Delete options with where clause
|
|
151
|
+
* @returns {Promise<number>} Number of deleted rows
|
|
152
|
+
*/
|
|
153
|
+
async delete(options) {
|
|
154
|
+
if (!options.where || Object.keys(options.where).length === 0) throw new require_errors.QueryError("Delete requires a where clause");
|
|
155
|
+
this.cache.invalidateTable(this.tableName);
|
|
156
|
+
const dbWhere = this.mapWhereToDb(options.where);
|
|
157
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
158
|
+
const filter = this.buildFileFilter(dbWhere);
|
|
159
|
+
return this.driver.deleteRows(this.tableName, filter);
|
|
160
|
+
}
|
|
161
|
+
const params = [];
|
|
162
|
+
const sql = require_sql_compiler.compileDelete(this.tableName, dbWhere, params);
|
|
163
|
+
return (await this.driver.execute(sql, params)).affectedRows;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Count rows matching the where clause
|
|
167
|
+
* @param {Pick<QueryOptions<T>, "where">} [options] - Count options
|
|
168
|
+
* @returns {Promise<number>} Row count
|
|
169
|
+
*/
|
|
170
|
+
async count(options) {
|
|
171
|
+
return this.cache.getOrSet(this.tableName, "count", options, async () => {
|
|
172
|
+
const dbWhere = options?.where ? this.mapWhereToDb(options.where) : void 0;
|
|
173
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
174
|
+
const filter = dbWhere ? this.buildFileFilter(dbWhere) : void 0;
|
|
175
|
+
return this.driver.countRows(this.tableName, filter);
|
|
176
|
+
}
|
|
177
|
+
const params = [];
|
|
178
|
+
let sql = `SELECT COUNT(*) as count FROM \`${this.tableName}\``;
|
|
179
|
+
if (dbWhere && Object.keys(dbWhere).length > 0) sql += ` WHERE ${require_sql_compiler.compileWhere(dbWhere, params)}`;
|
|
180
|
+
return (await this.driver.query(sql, params))[0]?.count ?? 0;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Check if any row exists matching the where clause
|
|
185
|
+
* @param {Pick<QueryOptions<T>, "where">} options - Exists options
|
|
186
|
+
* @returns {Promise<boolean>} Whether a matching row exists
|
|
187
|
+
*/
|
|
188
|
+
async exists(options) {
|
|
189
|
+
return await this.count(options) > 0;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Insert or update a row based on the where clause
|
|
193
|
+
* @param {object} options - Upsert options
|
|
194
|
+
* @param {WhereClause<T>} options.where - Condition to check
|
|
195
|
+
* @param {Partial<T>} options.create - Data to insert if not found
|
|
196
|
+
* @param {Partial<T>} options.update - Data to update if found
|
|
197
|
+
* @returns {Promise<T>} The upserted row
|
|
198
|
+
*/
|
|
199
|
+
async upsert(options) {
|
|
200
|
+
const existing = await this.findFirst({ where: options.where });
|
|
201
|
+
if (existing) return (await this.update({
|
|
202
|
+
where: options.where,
|
|
203
|
+
data: options.update
|
|
204
|
+
}))[0] ?? existing;
|
|
205
|
+
return this.insert(options.create);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Remove all rows from the table
|
|
209
|
+
*/
|
|
210
|
+
async truncate() {
|
|
211
|
+
this.cache.invalidateTable(this.tableName);
|
|
212
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
213
|
+
this.driver.truncateTable(this.tableName);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
await this.driver.execute(`DELETE FROM \`${this.tableName}\``);
|
|
217
|
+
}
|
|
218
|
+
async findSQL(options) {
|
|
219
|
+
const params = [];
|
|
220
|
+
const sql = require_sql_compiler.compileSelect(this.tableName, {
|
|
221
|
+
select: options?.select,
|
|
222
|
+
where: options?.where,
|
|
223
|
+
orderBy: options?.orderBy,
|
|
224
|
+
take: options?.take,
|
|
225
|
+
skip: options?.skip
|
|
226
|
+
}, params);
|
|
227
|
+
return this.driver.query(sql, params);
|
|
228
|
+
}
|
|
229
|
+
findFile(options) {
|
|
230
|
+
const fd = this.driver;
|
|
231
|
+
const filter = options?.where ? this.buildFileFilter(options.where) : void 0;
|
|
232
|
+
let rows = fd.findRows(this.tableName, filter);
|
|
233
|
+
if (options?.select) {
|
|
234
|
+
const selectSet = new Set(options.select);
|
|
235
|
+
rows = rows.map((row) => {
|
|
236
|
+
const filtered = {};
|
|
237
|
+
for (const key of selectSet) filtered[key] = row[key];
|
|
238
|
+
return filtered;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
if (options?.orderBy) {
|
|
242
|
+
const entries = Object.entries(options.orderBy);
|
|
243
|
+
rows.sort((a, b) => {
|
|
244
|
+
for (const [col, dir] of entries) {
|
|
245
|
+
const av = a[col];
|
|
246
|
+
const bv = b[col];
|
|
247
|
+
if (av < bv) return dir === "asc" ? -1 : 1;
|
|
248
|
+
if (av > bv) return dir === "asc" ? 1 : -1;
|
|
249
|
+
}
|
|
250
|
+
return 0;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
if (options?.skip) rows = rows.slice(options.skip);
|
|
254
|
+
if (options?.take) rows = rows.slice(0, options.take);
|
|
255
|
+
return rows;
|
|
256
|
+
}
|
|
257
|
+
buildFileFilter(where) {
|
|
258
|
+
return (row) => this.matchWhere(row, where);
|
|
259
|
+
}
|
|
260
|
+
matchWhere(row, where) {
|
|
261
|
+
for (const [key, value] of Object.entries(where)) {
|
|
262
|
+
if (key === "OR" && Array.isArray(value)) {
|
|
263
|
+
if (!value.some((sub) => this.matchWhere(row, sub))) return false;
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (key === "AND" && Array.isArray(value)) {
|
|
267
|
+
if (!value.every((sub) => this.matchWhere(row, sub))) return false;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
271
|
+
const cond = value;
|
|
272
|
+
const rowVal = row[key];
|
|
273
|
+
if (cond.eq !== void 0 && rowVal !== cond.eq) return false;
|
|
274
|
+
if (cond.neq !== void 0 && rowVal === cond.neq) return false;
|
|
275
|
+
if (cond.gt !== void 0 && !(rowVal > cond.gt)) return false;
|
|
276
|
+
if (cond.gte !== void 0 && !(rowVal >= cond.gte)) return false;
|
|
277
|
+
if (cond.lt !== void 0 && !(rowVal < cond.lt)) return false;
|
|
278
|
+
if (cond.lte !== void 0 && !(rowVal <= cond.lte)) return false;
|
|
279
|
+
if (cond.like !== void 0 && !String(rowVal).match(new RegExp(cond.like.replace(/%/g, ".*"), "i"))) return false;
|
|
280
|
+
if (cond.notLike !== void 0 && String(rowVal).match(new RegExp(cond.notLike.replace(/%/g, ".*"), "i"))) return false;
|
|
281
|
+
if (cond.in !== void 0 && !cond.in.includes(rowVal)) return false;
|
|
282
|
+
if (cond.notIn?.includes(rowVal)) return false;
|
|
283
|
+
if (cond.isNull === true && rowVal !== null && rowVal !== void 0) return false;
|
|
284
|
+
if (cond.isNull === false && (rowVal === null || rowVal === void 0)) return false;
|
|
285
|
+
if (cond.between !== void 0) {
|
|
286
|
+
if (rowVal < cond.between[0] || rowVal > cond.between[1]) return false;
|
|
287
|
+
}
|
|
288
|
+
} else if (row[key] !== value) return false;
|
|
289
|
+
}
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
async loadRelations(rows, withOpts) {
|
|
293
|
+
if (rows.length === 0) return rows;
|
|
294
|
+
const relations = this.registry.getRelations(this.tableName);
|
|
295
|
+
for (const [relationName, opts] of Object.entries(withOpts)) {
|
|
296
|
+
if (!opts) continue;
|
|
297
|
+
const relation = relations.find((r) => r.relationName === relationName);
|
|
298
|
+
if (!relation) continue;
|
|
299
|
+
if (relation.from.table === this.tableName) {
|
|
300
|
+
const ids = rows.map((r) => r[relation.from.column]).filter((v) => v != null);
|
|
301
|
+
if (ids.length === 0) continue;
|
|
302
|
+
const uniqueIds = [...new Set(ids)];
|
|
303
|
+
const relatedOpts = typeof opts === "object" ? opts : {};
|
|
304
|
+
const related = await this.findRelated(relation.to.table, relation.to.column, uniqueIds, relatedOpts);
|
|
305
|
+
const relatedMap = /* @__PURE__ */ new Map();
|
|
306
|
+
for (const r of related) {
|
|
307
|
+
const key = r[relation.to.column];
|
|
308
|
+
if (!relatedMap.has(key)) relatedMap.set(key, []);
|
|
309
|
+
relatedMap.get(key).push(r);
|
|
310
|
+
}
|
|
311
|
+
for (const row of rows) {
|
|
312
|
+
const key = row[relation.from.column];
|
|
313
|
+
row[relationName] = relatedMap.get(key) ?? [];
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
const pk = this.registry.getPrimaryKey(this.tableName);
|
|
317
|
+
if (!pk) continue;
|
|
318
|
+
const ids = rows.map((r) => r[pk]).filter((v) => v != null);
|
|
319
|
+
if (ids.length === 0) continue;
|
|
320
|
+
const uniqueIds = [...new Set(ids)];
|
|
321
|
+
const relatedOpts = typeof opts === "object" ? opts : {};
|
|
322
|
+
const related = await this.findRelated(relation.to.table, relation.to.column, uniqueIds, relatedOpts);
|
|
323
|
+
const relatedMap = /* @__PURE__ */ new Map();
|
|
324
|
+
for (const r of related) {
|
|
325
|
+
const key = r[relation.to.column];
|
|
326
|
+
if (!relatedMap.has(key)) relatedMap.set(key, []);
|
|
327
|
+
relatedMap.get(key).push(r);
|
|
328
|
+
}
|
|
329
|
+
for (const row of rows) {
|
|
330
|
+
const key = row[pk];
|
|
331
|
+
row[relationName] = relatedMap.get(key) ?? [];
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return rows;
|
|
336
|
+
}
|
|
337
|
+
async findRelated(tableName, column, ids, options) {
|
|
338
|
+
if (this.driver instanceof require_file.FileDriver) {
|
|
339
|
+
const fd = this.driver;
|
|
340
|
+
const filter = (row) => ids.includes(row[column]);
|
|
341
|
+
return fd.findRows(tableName, filter);
|
|
342
|
+
}
|
|
343
|
+
const params = [];
|
|
344
|
+
const placeholders = ids.map(() => "?").join(", ");
|
|
345
|
+
params.push(...ids);
|
|
346
|
+
let sql = `SELECT * FROM \`${tableName}\` WHERE \`${column}\` IN (${placeholders})`;
|
|
347
|
+
if (options.orderBy) {
|
|
348
|
+
const orderParts = Object.entries(options.orderBy).map(([col, dir]) => `\`${col}\` ${dir.toUpperCase()}`);
|
|
349
|
+
if (orderParts.length > 0) sql += ` ORDER BY ${orderParts.join(", ")}`;
|
|
350
|
+
}
|
|
351
|
+
if (options.take) sql += ` LIMIT ${options.take}`;
|
|
352
|
+
return this.driver.query(sql, params);
|
|
353
|
+
}
|
|
354
|
+
cleanData(data) {
|
|
355
|
+
const cleaned = {};
|
|
356
|
+
const dbColumnNames = new Set(this.meta.columns.map((c) => c.name));
|
|
357
|
+
const codeKeys = new Set(Object.keys(this.columnMap));
|
|
358
|
+
for (const [key, value] of Object.entries(data)) if (codeKeys.has(key) || dbColumnNames.has(key)) cleaned[key] = value;
|
|
359
|
+
return cleaned;
|
|
360
|
+
}
|
|
361
|
+
cacheEntities(rows) {
|
|
362
|
+
const pk = this.registry.getPrimaryKey(this.tableName);
|
|
363
|
+
if (!pk) return;
|
|
364
|
+
for (const row of rows) if (row[pk] != null) this.cache.setEntity(this.tableName, row[pk], row);
|
|
365
|
+
}
|
|
366
|
+
cacheEntity(row) {
|
|
367
|
+
const pk = this.registry.getPrimaryKey(this.tableName);
|
|
368
|
+
if (!pk || row[pk] == null) return;
|
|
369
|
+
this.cache.setEntity(this.tableName, row[pk], row);
|
|
370
|
+
}
|
|
371
|
+
toDbKeys(data) {
|
|
372
|
+
if (!this.hasAliases) return data;
|
|
373
|
+
const result = {};
|
|
374
|
+
for (const [key, value] of Object.entries(data)) result[this.columnMap[key] ?? key] = value;
|
|
375
|
+
return result;
|
|
376
|
+
}
|
|
377
|
+
toCodeKeys(row) {
|
|
378
|
+
if (!this.hasAliases) return row;
|
|
379
|
+
const result = {};
|
|
380
|
+
for (const [key, value] of Object.entries(row)) result[this.reverseColumnMap[key] ?? key] = value;
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
mapWhereToDb(where) {
|
|
384
|
+
if (!this.hasAliases) return where;
|
|
385
|
+
const result = {};
|
|
386
|
+
for (const [key, value] of Object.entries(where)) if (key === "OR" || key === "AND") result[key] = value.map((sub) => this.mapWhereToDb(sub));
|
|
387
|
+
else result[this.columnMap[key] ?? key] = value;
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
mapSelectToDb(select) {
|
|
391
|
+
if (!this.hasAliases) return select;
|
|
392
|
+
return select.map((key) => this.columnMap[key] ?? key);
|
|
393
|
+
}
|
|
394
|
+
mapOrderByToDb(orderBy) {
|
|
395
|
+
if (!this.hasAliases) return orderBy;
|
|
396
|
+
const result = {};
|
|
397
|
+
for (const [key, value] of Object.entries(orderBy)) result[this.columnMap[key] ?? key] = value;
|
|
398
|
+
return result;
|
|
399
|
+
}
|
|
400
|
+
mapOptionsToDb(options) {
|
|
401
|
+
if (!options || !this.hasAliases) return options;
|
|
402
|
+
const mapped = { ...options };
|
|
403
|
+
if (options.where) mapped.where = this.mapWhereToDb(options.where);
|
|
404
|
+
if (options.select) mapped.select = this.mapSelectToDb(options.select);
|
|
405
|
+
if (options.orderBy) mapped.orderBy = this.mapOrderByToDb(options.orderBy);
|
|
406
|
+
return mapped;
|
|
407
|
+
}
|
|
408
|
+
mapRows(rows) {
|
|
409
|
+
if (!this.hasAliases) return rows;
|
|
410
|
+
return rows.map((row) => this.toCodeKeys(row));
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
//#endregion
|
|
414
|
+
exports.TableRepository = TableRepository;
|
|
415
|
+
|
|
416
|
+
//# sourceMappingURL=repository.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.cjs","names":["QueryError","FileDriver","compileInsert","compileBulkInsert","compileUpdate","compileDelete","compileWhere","compileSelect"],"sources":["../../src/core/repository.ts"],"sourcesContent":["import type { CacheManager } from \"../cache\";\nimport { FileDriver } from \"../drivers/file\";\nimport {\n compileBulkInsert,\n compileDelete,\n compileInsert,\n compileSelect,\n compileUpdate,\n compileWhere,\n} from \"../drivers/sql-compiler\";\nimport { QueryError } from \"../errors\";\nimport type { SchemaRegistry } from \"../schema\";\nimport type {\n DatabaseDriver,\n DeleteOptions,\n QueryOptions,\n Repository,\n TableMetadata,\n UpdateOptions,\n WhereClause,\n} from \"../types\";\n\n/**\n * Repository implementation that provides CRUD operations for a table\n * @template T - The row type for this table\n */\nexport class TableRepository<T extends Record<string, any>> implements Repository<T> {\n private tableName: string;\n private driver: DatabaseDriver;\n private cache: CacheManager;\n private registry: SchemaRegistry;\n private meta: TableMetadata;\n private columnMap: Record<string, string>;\n private reverseColumnMap: Record<string, string>;\n private hasAliases: boolean;\n\n constructor(\n tableName: string,\n driver: DatabaseDriver,\n cache: CacheManager,\n registry: SchemaRegistry,\n ) {\n this.tableName = tableName;\n this.driver = driver;\n this.cache = cache;\n this.registry = registry;\n const meta = registry.getTable(tableName);\n if (!meta) {\n throw new QueryError(`Table \"${tableName}\" is not registered`);\n }\n this.meta = meta;\n this.columnMap = registry.getColumnMap(tableName);\n this.reverseColumnMap = registry.getReverseColumnMap(tableName);\n this.hasAliases = Object.entries(this.columnMap).some(\n ([codeKey, dbName]) => codeKey !== dbName,\n );\n }\n\n /**\n * Find all rows matching the query options\n * @param {QueryOptions<T>} [options] - Query options\n * @returns {Promise<T[]>} Array of matching rows\n */\n async find(options?: QueryOptions<T>): Promise<T[]> {\n return this.cache.getOrSet(this.tableName, \"find\", options, async () => {\n const dbOptions = this.mapOptionsToDb(options);\n let rows: T[];\n if (this.driver instanceof FileDriver) {\n rows = this.mapRows(this.findFile(dbOptions as QueryOptions<T>));\n } else {\n rows = this.mapRows(await this.findSQL(dbOptions as QueryOptions<T>));\n }\n if (options?.with) {\n rows = await this.loadRelations(rows, options.with);\n }\n this.cacheEntities(rows);\n return rows;\n });\n }\n\n /**\n * Find all rows matching the query options (alias for find)\n * @param {QueryOptions<T>} [options] - Query options\n * @returns {Promise<T[]>} Array of matching rows\n */\n async findMany(options?: QueryOptions<T>): Promise<T[]> {\n return this.find(options);\n }\n\n /**\n * Find the first row matching the query options\n * @param {QueryOptions<T>} [options] - Query options\n * @returns {Promise<T | null>} The first matching row or null\n */\n async findFirst(options?: QueryOptions<T>): Promise<T | null> {\n return this.cache.getOrSet(this.tableName, \"findFirst\", options, async () => {\n const dbOptions = this.mapOptionsToDb(options);\n const opts = { ...dbOptions, take: 1 };\n let rows: T[];\n if (this.driver instanceof FileDriver) {\n rows = this.mapRows(this.findFile(opts as QueryOptions<T>));\n } else {\n rows = this.mapRows(await this.findSQL(opts as QueryOptions<T>));\n }\n if (rows.length === 0) {\n return null;\n }\n if (options?.with) {\n rows = await this.loadRelations(rows, options.with);\n }\n const row = rows[0] ?? null;\n if (row) {\n this.cacheEntity(row);\n }\n return row;\n });\n }\n\n /**\n * Insert one or more rows\n * @param {Partial<T> | Partial<T>[]} data - Data to insert\n * @returns {Promise<T>} The inserted row\n */\n async insert(data: Partial<T> | Partial<T>[]): Promise<T> {\n const single = Array.isArray(data) ? data[0] : data;\n if (!single) {\n throw new QueryError(\"Insert data cannot be empty\");\n }\n\n this.cache.invalidateTable(this.tableName);\n const cleaned = this.toDbKeys(this.cleanData(single));\n\n if (this.driver instanceof FileDriver) {\n const id = (this.driver as FileDriver).insertRow(this.tableName, cleaned);\n const pk = this.registry.getPrimaryKey(this.tableName);\n if (pk) {\n cleaned[pk] = id;\n }\n const result = this.toCodeKeys(cleaned) as T;\n this.cacheEntity(result);\n return result;\n }\n\n const params: unknown[] = [];\n const sql = compileInsert(this.tableName, cleaned, params);\n const result = await this.driver.execute(sql, params);\n\n const pk = this.registry.getPrimaryKey(this.tableName);\n if (pk && result.insertId) {\n cleaned[pk] = result.insertId;\n }\n\n const mapped = this.toCodeKeys(cleaned) as T;\n this.cacheEntity(mapped);\n return mapped;\n }\n\n /**\n * Insert multiple rows\n * @param {Partial<T>[]} data - Array of data to insert\n * @returns {Promise<T[]>} The inserted rows\n */\n async insertMany(data: Partial<T>[]): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n this.cache.invalidateTable(this.tableName);\n\n if (this.driver instanceof FileDriver) {\n const results: T[] = [];\n for (const item of data) {\n results.push(await this.insert(item));\n }\n return results;\n }\n\n const cleanedData = data.map((item) => this.toDbKeys(this.cleanData(item)));\n const params: unknown[] = [];\n const sql = compileBulkInsert(this.tableName, cleanedData, params);\n const result = await this.driver.execute(sql, params);\n\n const pk = this.registry.getPrimaryKey(this.tableName);\n if (pk && result.insertId) {\n for (let i = 0; i < cleanedData.length; i++) {\n const row = cleanedData[i];\n if (row) {\n row[pk] = result.insertId + i;\n }\n }\n }\n\n const finalRows = this.mapRows(cleanedData);\n this.cacheEntities(finalRows);\n return finalRows;\n }\n\n /**\n * Update rows matching the where clause\n * @param {UpdateOptions<T>} options - Update options with where and data\n * @returns {Promise<T[]>} The updated rows\n */\n async update(options: UpdateOptions<T>): Promise<T[]> {\n if (!options.where || Object.keys(options.where).length === 0) {\n throw new QueryError(\"Update requires a where clause\");\n }\n\n this.cache.invalidateTable(this.tableName);\n const cleaned = this.toDbKeys(this.cleanData(options.data));\n const dbWhere = this.mapWhereToDb(options.where as WhereClause);\n\n if (this.driver instanceof FileDriver) {\n const filter = this.buildFileFilter(dbWhere);\n (this.driver as FileDriver).updateRows(this.tableName, filter, cleaned);\n return this.find({ where: options.where } as QueryOptions<T>);\n }\n\n const params: unknown[] = [];\n const sql = compileUpdate(this.tableName, cleaned, dbWhere, params);\n await this.driver.execute(sql, params);\n\n return this.find({ where: options.where } as QueryOptions<T>);\n }\n\n /**\n * Delete rows matching the where clause\n * @param {DeleteOptions<T>} options - Delete options with where clause\n * @returns {Promise<number>} Number of deleted rows\n */\n async delete(options: DeleteOptions<T>): Promise<number> {\n if (!options.where || Object.keys(options.where).length === 0) {\n throw new QueryError(\"Delete requires a where clause\");\n }\n\n this.cache.invalidateTable(this.tableName);\n const dbWhere = this.mapWhereToDb(options.where as WhereClause);\n\n if (this.driver instanceof FileDriver) {\n const filter = this.buildFileFilter(dbWhere);\n return (this.driver as FileDriver).deleteRows(this.tableName, filter);\n }\n\n const params: unknown[] = [];\n const sql = compileDelete(this.tableName, dbWhere, params);\n const result = await this.driver.execute(sql, params);\n return result.affectedRows;\n }\n\n /**\n * Count rows matching the where clause\n * @param {Pick<QueryOptions<T>, \"where\">} [options] - Count options\n * @returns {Promise<number>} Row count\n */\n async count(options?: Pick<QueryOptions<T>, \"where\">): Promise<number> {\n return this.cache.getOrSet(this.tableName, \"count\", options, async () => {\n const dbWhere = options?.where ? this.mapWhereToDb(options.where as WhereClause) : undefined;\n\n if (this.driver instanceof FileDriver) {\n const filter = dbWhere ? this.buildFileFilter(dbWhere) : undefined;\n return (this.driver as FileDriver).countRows(this.tableName, filter);\n }\n\n const params: unknown[] = [];\n let sql = `SELECT COUNT(*) as count FROM \\`${this.tableName}\\``;\n if (dbWhere && Object.keys(dbWhere).length > 0) {\n sql += ` WHERE ${compileWhere(dbWhere, params)}`;\n }\n const rows = await this.driver.query(sql, params);\n return rows[0]?.count ?? 0;\n });\n }\n\n /**\n * Check if any row exists matching the where clause\n * @param {Pick<QueryOptions<T>, \"where\">} options - Exists options\n * @returns {Promise<boolean>} Whether a matching row exists\n */\n async exists(options: Pick<QueryOptions<T>, \"where\">): Promise<boolean> {\n const c = await this.count(options);\n return c > 0;\n }\n\n /**\n * Insert or update a row based on the where clause\n * @param {object} options - Upsert options\n * @param {WhereClause<T>} options.where - Condition to check\n * @param {Partial<T>} options.create - Data to insert if not found\n * @param {Partial<T>} options.update - Data to update if found\n * @returns {Promise<T>} The upserted row\n */\n async upsert(options: {\n where: WhereClause<T>;\n create: Partial<T>;\n update: Partial<T>;\n }): Promise<T> {\n const existing = await this.findFirst({ where: options.where } as QueryOptions<T>);\n if (existing) {\n const updated = await this.update({ where: options.where, data: options.update });\n return updated[0] ?? existing;\n }\n return this.insert(options.create);\n }\n\n /**\n * Remove all rows from the table\n */\n async truncate(): Promise<void> {\n this.cache.invalidateTable(this.tableName);\n if (this.driver instanceof FileDriver) {\n (this.driver as FileDriver).truncateTable(this.tableName);\n return;\n }\n await this.driver.execute(`DELETE FROM \\`${this.tableName}\\``);\n }\n\n private async findSQL(options?: QueryOptions<T>): Promise<T[]> {\n const params: unknown[] = [];\n const sql = compileSelect(\n this.tableName,\n {\n select: options?.select as string[] | undefined,\n where: options?.where as WhereClause | undefined,\n orderBy: options?.orderBy as Record<string, \"asc\" | \"desc\"> | undefined,\n take: options?.take,\n skip: options?.skip,\n },\n params,\n );\n return this.driver.query(sql, params);\n }\n\n private findFile(options?: QueryOptions<T>): T[] {\n const fd = this.driver as FileDriver;\n const filter = options?.where ? this.buildFileFilter(options.where as WhereClause) : undefined;\n let rows = fd.findRows(this.tableName, filter) as T[];\n\n if (options?.select) {\n const selectSet = new Set(options.select as string[]);\n rows = rows.map((row) => {\n const filtered: Record<string, any> = {};\n for (const key of selectSet) {\n filtered[key as string] = row[key as string];\n }\n return filtered as T;\n });\n }\n\n if (options?.orderBy) {\n const entries = Object.entries(options.orderBy);\n rows.sort((a, b) => {\n for (const [col, dir] of entries) {\n const av = a[col];\n const bv = b[col];\n if (av < bv) {\n return dir === \"asc\" ? -1 : 1;\n }\n if (av > bv) {\n return dir === \"asc\" ? 1 : -1;\n }\n }\n return 0;\n });\n }\n\n if (options?.skip) {\n rows = rows.slice(options.skip);\n }\n if (options?.take) {\n rows = rows.slice(0, options.take);\n }\n\n return rows;\n }\n\n private buildFileFilter(where: WhereClause): (row: Record<string, unknown>) => boolean {\n return (row) => this.matchWhere(row, where);\n }\n\n private matchWhere(row: Record<string, unknown>, where: WhereClause): boolean {\n for (const [key, value] of Object.entries(where)) {\n if (key === \"OR\" && Array.isArray(value)) {\n const any = (value as WhereClause[]).some((sub) => this.matchWhere(row, sub));\n if (!any) {\n return false;\n }\n continue;\n }\n if (key === \"AND\" && Array.isArray(value)) {\n const all = (value as WhereClause[]).every((sub) => this.matchWhere(row, sub));\n if (!all) {\n return false;\n }\n continue;\n }\n\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n const cond = value as any;\n const rowVal = row[key];\n if (cond.eq !== undefined && rowVal !== cond.eq) {\n return false;\n }\n if (cond.neq !== undefined && rowVal === cond.neq) {\n return false;\n }\n if (cond.gt !== undefined && !((rowVal as any) > cond.gt)) {\n return false;\n }\n if (cond.gte !== undefined && !((rowVal as any) >= cond.gte)) {\n return false;\n }\n if (cond.lt !== undefined && !((rowVal as any) < cond.lt)) {\n return false;\n }\n if (cond.lte !== undefined && !((rowVal as any) <= cond.lte)) {\n return false;\n }\n if (\n cond.like !== undefined &&\n !String(rowVal).match(new RegExp(cond.like.replace(/%/g, \".*\"), \"i\"))\n ) {\n return false;\n }\n if (\n cond.notLike !== undefined &&\n String(rowVal).match(new RegExp(cond.notLike.replace(/%/g, \".*\"), \"i\"))\n ) {\n return false;\n }\n if (cond.in !== undefined && !cond.in.includes(rowVal)) {\n return false;\n }\n if (cond.notIn?.includes(rowVal)) {\n return false;\n }\n if (cond.isNull === true && rowVal !== null && rowVal !== undefined) {\n return false;\n }\n if (cond.isNull === false && (rowVal === null || rowVal === undefined)) {\n return false;\n }\n if (cond.between !== undefined) {\n if ((rowVal as any) < cond.between[0] || (rowVal as any) > cond.between[1]) {\n return false;\n }\n }\n } else {\n if (row[key] !== value) {\n return false;\n }\n }\n }\n return true;\n }\n\n private async loadRelations(\n rows: T[],\n withOpts: Record<string, boolean | QueryOptions>,\n ): Promise<T[]> {\n if (rows.length === 0) {\n return rows;\n }\n const relations = this.registry.getRelations(this.tableName);\n\n for (const [relationName, opts] of Object.entries(withOpts)) {\n if (!opts) {\n continue;\n }\n const relation = relations.find((r) => r.relationName === relationName);\n if (!relation) {\n continue;\n }\n\n const isParent = relation.from.table === this.tableName;\n if (isParent) {\n const ids = rows.map((r) => r[relation.from.column]).filter((v) => v != null);\n if (ids.length === 0) {\n continue;\n }\n const uniqueIds = [...new Set(ids)];\n const relatedOpts: QueryOptions = typeof opts === \"object\" ? opts : {};\n const related = await this.findRelated(\n relation.to.table,\n relation.to.column,\n uniqueIds,\n relatedOpts,\n );\n const relatedMap = new Map<unknown, unknown[]>();\n for (const r of related) {\n const key = (r as any)[relation.to.column];\n if (!relatedMap.has(key)) {\n relatedMap.set(key, []);\n }\n relatedMap.get(key)!.push(r);\n }\n for (const row of rows) {\n const key = row[relation.from.column];\n const relRows = relatedMap.get(key);\n (row as any)[relationName] = relRows ?? [];\n }\n } else {\n const pk = this.registry.getPrimaryKey(this.tableName);\n if (!pk) {\n continue;\n }\n const ids = rows.map((r) => r[pk]).filter((v) => v != null);\n if (ids.length === 0) {\n continue;\n }\n const uniqueIds = [...new Set(ids)];\n const relatedOpts: QueryOptions = typeof opts === \"object\" ? opts : {};\n const related = await this.findRelated(\n relation.to.table,\n relation.to.column,\n uniqueIds,\n relatedOpts,\n );\n const relatedMap = new Map<unknown, unknown[]>();\n for (const r of related) {\n const key = (r as any)[relation.to.column];\n if (!relatedMap.has(key)) {\n relatedMap.set(key, []);\n }\n relatedMap.get(key)!.push(r);\n }\n for (const row of rows) {\n const key = row[pk];\n (row as any)[relationName] = relatedMap.get(key) ?? [];\n }\n }\n }\n\n return rows;\n }\n\n private async findRelated(\n tableName: string,\n column: string,\n ids: unknown[],\n options: QueryOptions,\n ): Promise<any[]> {\n if (this.driver instanceof FileDriver) {\n const fd = this.driver as FileDriver;\n const filter = (row: Record<string, unknown>) => ids.includes(row[column]);\n return fd.findRows(tableName, filter);\n }\n\n const params: unknown[] = [];\n const placeholders = ids.map(() => \"?\").join(\", \");\n params.push(...ids);\n\n let sql = `SELECT * FROM \\`${tableName}\\` WHERE \\`${column}\\` IN (${placeholders})`;\n if (options.orderBy) {\n const orderParts = Object.entries(options.orderBy).map(\n ([col, dir]) => `\\`${col}\\` ${(dir as string).toUpperCase()}`,\n );\n if (orderParts.length > 0) {\n sql += ` ORDER BY ${orderParts.join(\", \")}`;\n }\n }\n if (options.take) {\n sql += ` LIMIT ${options.take}`;\n }\n\n return this.driver.query(sql, params);\n }\n\n private cleanData(data: Partial<T>): Record<string, unknown> {\n const cleaned: Record<string, unknown> = {};\n const dbColumnNames = new Set(this.meta.columns.map((c) => c.name));\n const codeKeys = new Set(Object.keys(this.columnMap));\n for (const [key, value] of Object.entries(data as Record<string, unknown>)) {\n if (codeKeys.has(key) || dbColumnNames.has(key)) {\n cleaned[key] = value;\n }\n }\n return cleaned;\n }\n\n private cacheEntities(rows: T[]): void {\n const pk = this.registry.getPrimaryKey(this.tableName);\n if (!pk) {\n return;\n }\n for (const row of rows) {\n if (row[pk] != null) {\n this.cache.setEntity(this.tableName, row[pk], row);\n }\n }\n }\n\n private cacheEntity(row: T): void {\n const pk = this.registry.getPrimaryKey(this.tableName);\n if (!pk || row[pk] == null) {\n return;\n }\n this.cache.setEntity(this.tableName, row[pk], row);\n }\n\n private toDbKeys(data: Record<string, unknown>): Record<string, unknown> {\n if (!this.hasAliases) {\n return data;\n }\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(data)) {\n result[this.columnMap[key] ?? key] = value;\n }\n return result;\n }\n\n private toCodeKeys(row: Record<string, unknown>): Record<string, unknown> {\n if (!this.hasAliases) {\n return row;\n }\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(row)) {\n result[this.reverseColumnMap[key] ?? key] = value;\n }\n return result;\n }\n\n private mapWhereToDb(where: WhereClause): WhereClause {\n if (!this.hasAliases) {\n return where;\n }\n const result: WhereClause = {};\n for (const [key, value] of Object.entries(where)) {\n if (key === \"OR\" || key === \"AND\") {\n (result as any)[key] = (value as WhereClause[]).map((sub) => this.mapWhereToDb(sub));\n } else {\n result[this.columnMap[key] ?? key] = value;\n }\n }\n return result;\n }\n\n private mapSelectToDb(select: string[]): string[] {\n if (!this.hasAliases) {\n return select;\n }\n return select.map((key) => this.columnMap[key] ?? key);\n }\n\n private mapOrderByToDb(orderBy: Record<string, \"asc\" | \"desc\">): Record<string, \"asc\" | \"desc\"> {\n if (!this.hasAliases) {\n return orderBy;\n }\n const result: Record<string, \"asc\" | \"desc\"> = {};\n for (const [key, value] of Object.entries(orderBy)) {\n result[this.columnMap[key] ?? key] = value;\n }\n return result;\n }\n\n private mapOptionsToDb(options?: QueryOptions<T>): QueryOptions | undefined {\n if (!options || !this.hasAliases) {\n return options as QueryOptions | undefined;\n }\n const mapped: QueryOptions = { ...options } as any;\n if (options.where) {\n mapped.where = this.mapWhereToDb(options.where as WhereClause);\n }\n if (options.select) {\n mapped.select = this.mapSelectToDb(options.select);\n }\n if (options.orderBy) {\n mapped.orderBy = this.mapOrderByToDb(options.orderBy as Record<string, \"asc\" | \"desc\">);\n }\n return mapped;\n }\n\n private mapRows(rows: Record<string, unknown>[]): T[] {\n if (!this.hasAliases) {\n return rows as T[];\n }\n return rows.map((row) => this.toCodeKeys(row) as T);\n }\n}\n"],"mappings":";;;;;;;;AA0BA,IAAa,kBAAb,MAAqF;CACnF;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA,YACE,WACA,QACA,OACA,UACA;AACA,OAAK,YAAY;AACjB,OAAK,SAAS;AACd,OAAK,QAAQ;AACb,OAAK,WAAW;EAChB,MAAM,OAAO,SAAS,SAAS,UAAU;AACzC,MAAI,CAAC,KACH,OAAM,IAAIA,eAAAA,WAAW,UAAU,UAAU,qBAAqB;AAEhE,OAAK,OAAO;AACZ,OAAK,YAAY,SAAS,aAAa,UAAU;AACjD,OAAK,mBAAmB,SAAS,oBAAoB,UAAU;AAC/D,OAAK,aAAa,OAAO,QAAQ,KAAK,UAAU,CAAC,MAC9C,CAAC,SAAS,YAAY,YAAY,OACpC;;;;;;;CAQH,MAAM,KAAK,SAAyC;AAClD,SAAO,KAAK,MAAM,SAAS,KAAK,WAAW,QAAQ,SAAS,YAAY;GACtE,MAAM,YAAY,KAAK,eAAe,QAAQ;GAC9C,IAAI;AACJ,OAAI,KAAK,kBAAkBC,aAAAA,WACzB,QAAO,KAAK,QAAQ,KAAK,SAAS,UAA6B,CAAC;OAEhE,QAAO,KAAK,QAAQ,MAAM,KAAK,QAAQ,UAA6B,CAAC;AAEvE,OAAI,SAAS,KACX,QAAO,MAAM,KAAK,cAAc,MAAM,QAAQ,KAAK;AAErD,QAAK,cAAc,KAAK;AACxB,UAAO;IACP;;;;;;;CAQJ,MAAM,SAAS,SAAyC;AACtD,SAAO,KAAK,KAAK,QAAQ;;;;;;;CAQ3B,MAAM,UAAU,SAA8C;AAC5D,SAAO,KAAK,MAAM,SAAS,KAAK,WAAW,aAAa,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Q,OACV,QAAO,SAAS,KAAK,cAAc,QAAQ,OAAO;AAEpD,MAAI,QAAQ,QACV,QAAO,UAAU,KAAK,eAAe,QAAQ,QAA0C;AAEzF,SAAO;;CAGT,QAAgB,MAAsC;AACpD,MAAI,CAAC,KAAK,WACR,QAAO;AAET,SAAO,KAAK,KAAK,QAAQ,KAAK,WAAW,IAAI,CAAM"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { DatabaseDriver, DeleteOptions, QueryOptions, Repository, UpdateOptions, WhereClause } from "../types.cjs";
|
|
2
|
+
import { CacheManager } from "../cache/manager.cjs";
|
|
3
|
+
import { SchemaRegistry } from "../schema/registry.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/core/repository.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Repository implementation that provides CRUD operations for a table
|
|
8
|
+
* @template T - The row type for this table
|
|
9
|
+
*/
|
|
10
|
+
declare class TableRepository<T extends Record<string, any>> implements Repository<T> {
|
|
11
|
+
private tableName;
|
|
12
|
+
private driver;
|
|
13
|
+
private cache;
|
|
14
|
+
private registry;
|
|
15
|
+
private meta;
|
|
16
|
+
private columnMap;
|
|
17
|
+
private reverseColumnMap;
|
|
18
|
+
private hasAliases;
|
|
19
|
+
constructor(tableName: string, driver: DatabaseDriver, cache: CacheManager, registry: SchemaRegistry);
|
|
20
|
+
/**
|
|
21
|
+
* Find all rows matching the query options
|
|
22
|
+
* @param {QueryOptions<T>} [options] - Query options
|
|
23
|
+
* @returns {Promise<T[]>} Array of matching rows
|
|
24
|
+
*/
|
|
25
|
+
find(options?: QueryOptions<T>): Promise<T[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Find all rows matching the query options (alias for find)
|
|
28
|
+
* @param {QueryOptions<T>} [options] - Query options
|
|
29
|
+
* @returns {Promise<T[]>} Array of matching rows
|
|
30
|
+
*/
|
|
31
|
+
findMany(options?: QueryOptions<T>): Promise<T[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Find the first row matching the query options
|
|
34
|
+
* @param {QueryOptions<T>} [options] - Query options
|
|
35
|
+
* @returns {Promise<T | null>} The first matching row or null
|
|
36
|
+
*/
|
|
37
|
+
findFirst(options?: QueryOptions<T>): Promise<T | null>;
|
|
38
|
+
/**
|
|
39
|
+
* Insert one or more rows
|
|
40
|
+
* @param {Partial<T> | Partial<T>[]} data - Data to insert
|
|
41
|
+
* @returns {Promise<T>} The inserted row
|
|
42
|
+
*/
|
|
43
|
+
insert(data: Partial<T> | Partial<T>[]): Promise<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Insert multiple rows
|
|
46
|
+
* @param {Partial<T>[]} data - Array of data to insert
|
|
47
|
+
* @returns {Promise<T[]>} The inserted rows
|
|
48
|
+
*/
|
|
49
|
+
insertMany(data: Partial<T>[]): Promise<T[]>;
|
|
50
|
+
/**
|
|
51
|
+
* Update rows matching the where clause
|
|
52
|
+
* @param {UpdateOptions<T>} options - Update options with where and data
|
|
53
|
+
* @returns {Promise<T[]>} The updated rows
|
|
54
|
+
*/
|
|
55
|
+
update(options: UpdateOptions<T>): Promise<T[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Delete rows matching the where clause
|
|
58
|
+
* @param {DeleteOptions<T>} options - Delete options with where clause
|
|
59
|
+
* @returns {Promise<number>} Number of deleted rows
|
|
60
|
+
*/
|
|
61
|
+
delete(options: DeleteOptions<T>): Promise<number>;
|
|
62
|
+
/**
|
|
63
|
+
* Count rows matching the where clause
|
|
64
|
+
* @param {Pick<QueryOptions<T>, "where">} [options] - Count options
|
|
65
|
+
* @returns {Promise<number>} Row count
|
|
66
|
+
*/
|
|
67
|
+
count(options?: Pick<QueryOptions<T>, "where">): Promise<number>;
|
|
68
|
+
/**
|
|
69
|
+
* Check if any row exists matching the where clause
|
|
70
|
+
* @param {Pick<QueryOptions<T>, "where">} options - Exists options
|
|
71
|
+
* @returns {Promise<boolean>} Whether a matching row exists
|
|
72
|
+
*/
|
|
73
|
+
exists(options: Pick<QueryOptions<T>, "where">): Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Insert or update a row based on the where clause
|
|
76
|
+
* @param {object} options - Upsert options
|
|
77
|
+
* @param {WhereClause<T>} options.where - Condition to check
|
|
78
|
+
* @param {Partial<T>} options.create - Data to insert if not found
|
|
79
|
+
* @param {Partial<T>} options.update - Data to update if found
|
|
80
|
+
* @returns {Promise<T>} The upserted row
|
|
81
|
+
*/
|
|
82
|
+
upsert(options: {
|
|
83
|
+
where: WhereClause<T>;
|
|
84
|
+
create: Partial<T>;
|
|
85
|
+
update: Partial<T>;
|
|
86
|
+
}): Promise<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Remove all rows from the table
|
|
89
|
+
*/
|
|
90
|
+
truncate(): Promise<void>;
|
|
91
|
+
private findSQL;
|
|
92
|
+
private findFile;
|
|
93
|
+
private buildFileFilter;
|
|
94
|
+
private matchWhere;
|
|
95
|
+
private loadRelations;
|
|
96
|
+
private findRelated;
|
|
97
|
+
private cleanData;
|
|
98
|
+
private cacheEntities;
|
|
99
|
+
private cacheEntity;
|
|
100
|
+
private toDbKeys;
|
|
101
|
+
private toCodeKeys;
|
|
102
|
+
private mapWhereToDb;
|
|
103
|
+
private mapSelectToDb;
|
|
104
|
+
private mapOrderByToDb;
|
|
105
|
+
private mapOptionsToDb;
|
|
106
|
+
private mapRows;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
export { TableRepository };
|
|
110
|
+
//# sourceMappingURL=repository.d.cts.map
|