masterrecord 0.3.68 → 1.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/.claude/settings.local.json +3 -1
- package/CHANGES.md +31 -2
- package/Cache/QueryCache.js +10 -4
- package/Cache/RedisQueryCache.js +3 -2
- package/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +2 -2
- package/Entity/entityTrackerModel.js +3 -3
- package/Entity/fieldTransformer.js +1 -1
- package/FOREIGN_KEY_STRING_FIX.md +2 -2
- package/MasterRecord.js +6 -43
- package/Migrations/cli.js +44 -113
- package/Migrations/dependencyGraph.js +1 -1
- package/Migrations/migrationMySQLQuery.js +1 -1
- package/Migrations/migrationPostgresQuery.js +1 -1
- package/Migrations/migrationSQLiteQuery.js +1 -1
- package/Migrations/migrationTemplate.js +8 -39
- package/Migrations/migrations.js +16 -14
- package/Migrations/pathUtils.js +2 -5
- package/Migrations/pathUtils.test.js +33 -49
- package/Migrations/schema.js +52 -89
- package/QueryLanguage/queryMethods.js +4 -4
- package/QueryLanguage/queryParameters.js +1 -1
- package/QueryLanguage/queryScript.js +4 -3
- package/SQLLiteEngine.js +30 -8
- package/Tools.js +10 -8
- package/context.js +43 -42
- package/deleteManager.js +2 -2
- package/docs/MIGRATIONS_GUIDE.md +237 -532
- package/docs/POSTGRESQL_SETUP.md +4 -4
- package/docs/QUERY_CACHING_GUIDE.md +8 -7
- package/eslint.config.js +40 -0
- package/insertManager.js +28 -20
- package/mySQLConnect.js +2 -2
- package/mySQLEngine.js +161 -16
- package/package.json +34 -5
- package/postgresEngine.js +74 -26
- package/postgresSyncConnect.js +5 -3
- package/prettier.config.js +22 -0
- package/readme.md +35 -27
- package/test/context-errors.test.js +43 -0
- package/test/count-chained-where.test.js +136 -0
- package/test/esm-imports.test.js +110 -0
- package/test/fixtures/count-chained-where/config/environments/env.countchained.json +1 -0
- package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/query-builder-parity/config/environments/env.parity.json +1 -0
- package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/migration-template.test.js +47 -0
- package/test/new-setter-large-string.test.js +191 -0
- package/test/pool-key.test.js +43 -0
- package/test/query-builder-parity.test.js +260 -0
- package/test/sqlite-lifecycle.test.js +76 -0
- package/.eslintrc.js +0 -290
- package/.prettierrc.js +0 -109
- package/test/anyCommaStringTest.js +0 -237
- package/test/anyMethodTest.js +0 -176
- package/test/attachDetached.test.js +0 -303
- package/test/bulk-operations-test.js +0 -235
- package/test/cache-toObject-test.js +0 -105
- package/test/cacheIntegration.test.js +0 -319
- package/test/config-glob-pattern-test.js +0 -232
- package/test/double-where-bug-test.js +0 -71
- package/test/ensure-ready-test.js +0 -251
- package/test/entity-deduplication-test.js +0 -174
- package/test/entity-methods-test.js +0 -269
- package/test/findByIdTest.js +0 -227
- package/test/foreign-key-string-value-test.js +0 -406
- package/test/global-model-registry-test.js +0 -538
- package/test/id-setting-validation.js +0 -202
- package/test/includesFeatureTest.js +0 -183
- package/test/includesTransformTest.js +0 -110
- package/test/index-bug-fix-test.js +0 -188
- package/test/insert-return-test.js +0 -39
- package/test/lifecycle-hooks-test.js +0 -258
- package/test/multiContextCache.test.js +0 -230
- package/test/new-entity-migration-test.js +0 -162
- package/test/newMethodTest.js +0 -330
- package/test/newMethodUnitTest.js +0 -320
- package/test/optInCache.test.js +0 -221
- package/test/parameterizedPlaceholderTest.js +0 -159
- package/test/pool-registry-test.js +0 -303
- package/test/post-insert-tracking-test.js +0 -258
- package/test/postgresEngineTest.js +0 -463
- package/test/postgresIntegrationTest.js +0 -381
- package/test/promise-detection-test.js +0 -290
- package/test/qa-context-pattern-test.js +0 -319
- package/test/query-helpers-test.js +0 -258
- package/test/query-isolation-test.js +0 -59
- package/test/queryCache.test.js +0 -148
- package/test/securityTest.js +0 -268
- package/test/seed-data-test.js +0 -212
- package/test/seed-deduplication-test.js +0 -313
- package/test/seed-features-integration-test.js +0 -418
- package/test/seed-migration-template-test.js +0 -220
- package/test/singleDollarPlaceholderTest.js +0 -238
- package/test/tablePrefixTest.js +0 -100
- package/test/transformerTest.js +0 -287
- package/test/v0.3.34-bug-fixes-test.js +0 -428
- package/test/validation-test.js +0 -302
- package/test/whereChainingTest.js +0 -88
package/docs/POSTGRESQL_SETUP.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install pg@^8.16.3
|
|
|
13
13
|
### 1. Initialize PostgreSQL Connection
|
|
14
14
|
|
|
15
15
|
```javascript
|
|
16
|
-
|
|
16
|
+
import context from 'masterrecord/context';
|
|
17
17
|
|
|
18
18
|
// Create a new context
|
|
19
19
|
const db = new context();
|
|
@@ -105,7 +105,7 @@ await db.saveChanges();
|
|
|
105
105
|
### Transactions
|
|
106
106
|
|
|
107
107
|
```javascript
|
|
108
|
-
|
|
108
|
+
import PostgresSyncConnect from 'masterrecord/postgresSyncConnect';
|
|
109
109
|
|
|
110
110
|
const connection = new PostgresSyncConnect();
|
|
111
111
|
await connection.connect(config);
|
|
@@ -347,12 +347,12 @@ node test/postgresIntegrationTest.js
|
|
|
347
347
|
- **MasterRecord**: 0.3.0+
|
|
348
348
|
- **pg (node-postgres)**: 8.16.3+
|
|
349
349
|
- **PostgreSQL Server**: 9.6+ (tested with 12+, 13+, 14+)
|
|
350
|
-
- **Node.js**:
|
|
350
|
+
- **Node.js**: 20+ (ESM-only package)
|
|
351
351
|
|
|
352
352
|
## Complete Example
|
|
353
353
|
|
|
354
354
|
```javascript
|
|
355
|
-
|
|
355
|
+
import context from 'masterrecord/context';
|
|
356
356
|
|
|
357
357
|
async function main() {
|
|
358
358
|
// Initialize
|
|
@@ -12,8 +12,8 @@ MasterRecord's query caching works like **Active Record** in Rails:
|
|
|
12
12
|
### 1. Express Middleware (Recommended)
|
|
13
13
|
|
|
14
14
|
```javascript
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
import express from 'express';
|
|
16
|
+
import AppContext from './models/AppContext.js';
|
|
17
17
|
|
|
18
18
|
const app = express();
|
|
19
19
|
|
|
@@ -30,9 +30,9 @@ app.use((req, res, next) => {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
// Routes
|
|
33
|
-
app.get('/api/categories', (req, res) => {
|
|
33
|
+
app.get('/api/categories', async (req, res) => {
|
|
34
34
|
// Opt-in caching with .cache()
|
|
35
|
-
const categories = req.db.Categories.cache().toList();
|
|
35
|
+
const categories = await req.db.Categories.cache().toList();
|
|
36
36
|
res.json(categories);
|
|
37
37
|
// Cache auto-cleared after response
|
|
38
38
|
});
|
|
@@ -130,8 +130,8 @@ const transactions = db.Transactions
|
|
|
130
130
|
### Example 1: E-Commerce API
|
|
131
131
|
|
|
132
132
|
```javascript
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
import express from 'express';
|
|
134
|
+
import AppContext from './models/AppContext.js';
|
|
135
135
|
|
|
136
136
|
const app = express();
|
|
137
137
|
|
|
@@ -322,7 +322,8 @@ const cats = db.Categories.cache().toList();
|
|
|
322
322
|
### Unit Tests
|
|
323
323
|
|
|
324
324
|
```javascript
|
|
325
|
-
|
|
325
|
+
import { describe, test, beforeEach, afterEach } from 'node:test';
|
|
326
|
+
import AppContext from '../models/AppContext.js';
|
|
326
327
|
|
|
327
328
|
describe('Category API', () => {
|
|
328
329
|
let db;
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import js from '@eslint/js';
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
js.configs.recommended,
|
|
5
|
+
{
|
|
6
|
+
languageOptions: {
|
|
7
|
+
ecmaVersion: 2023,
|
|
8
|
+
sourceType: 'module',
|
|
9
|
+
globals: {
|
|
10
|
+
console: 'readonly',
|
|
11
|
+
process: 'readonly',
|
|
12
|
+
Buffer: 'readonly',
|
|
13
|
+
global: 'readonly',
|
|
14
|
+
setTimeout: 'readonly',
|
|
15
|
+
clearTimeout: 'readonly',
|
|
16
|
+
setInterval: 'readonly',
|
|
17
|
+
clearInterval: 'readonly',
|
|
18
|
+
URL: 'readonly',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
rules: {
|
|
22
|
+
'no-var': 'error',
|
|
23
|
+
'prefer-const': 'error',
|
|
24
|
+
'no-unused-vars': ['error', {
|
|
25
|
+
vars: 'all',
|
|
26
|
+
args: 'after-used',
|
|
27
|
+
ignoreRestSiblings: true,
|
|
28
|
+
argsIgnorePattern: '^_',
|
|
29
|
+
varsIgnorePattern: '^_',
|
|
30
|
+
}],
|
|
31
|
+
'no-console': 'off',
|
|
32
|
+
eqeqeq: ['error', 'always'],
|
|
33
|
+
'no-eval': 'error',
|
|
34
|
+
'no-implied-eval': 'error',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
ignores: ['node_modules/**', '_migrations/**', 'test/**'],
|
|
39
|
+
},
|
|
40
|
+
];
|
package/insertManager.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
// version 1.0.0 - FAANG-level refactor
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import tools from './Tools.js';
|
|
3
|
+
import queryScript from './QueryLanguage/queryScript.js';
|
|
4
|
+
import FieldTransformer from './Entity/fieldTransformer.js';
|
|
6
5
|
|
|
7
6
|
// Constants
|
|
8
7
|
const TIMESTAMP_FIELDS = {
|
|
@@ -352,27 +351,36 @@ class InsertManager {
|
|
|
352
351
|
if (!isRelationship) {
|
|
353
352
|
const val = currentRealModel[entity];
|
|
354
353
|
if (val != null && typeof val === 'object') {
|
|
355
|
-
// Always reject Promises —
|
|
354
|
+
// Always reject Promises — no transform/set can meaningfully handle them
|
|
356
355
|
if (typeof val.then === 'function') {
|
|
357
356
|
const entityName = entityModel.__name || 'unknown';
|
|
358
357
|
this._errorModel.isValid = false;
|
|
359
358
|
this._errorModel.errors.push(
|
|
360
359
|
`Property '${entity}' on entity '${entityName}' contains a Promise. Did you forget to await an async call?`
|
|
361
360
|
);
|
|
362
|
-
} else
|
|
363
|
-
//
|
|
364
|
-
//
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
361
|
+
} else {
|
|
362
|
+
// Allow Array/Object if the field has any serializer that can
|
|
363
|
+
// turn it into a scalar before the SQL insert runs:
|
|
364
|
+
// - legacy set() transform (currentEntity.set)
|
|
365
|
+
// - field transformer (currentEntity.transform.toDatabase)
|
|
366
|
+
const hasLegacySet = typeof currentEntity.set === 'function';
|
|
367
|
+
const hasTransformer = FieldTransformer.hasTransformer(currentEntity)
|
|
368
|
+
&& typeof currentEntity.transform.toDatabase === 'function';
|
|
369
|
+
const canSerialize = hasLegacySet || hasTransformer;
|
|
370
|
+
|
|
371
|
+
if (!canSerialize && !(val instanceof Date)) {
|
|
372
|
+
const entityName = entityModel.__name || 'unknown';
|
|
373
|
+
if (Array.isArray(val)) {
|
|
374
|
+
this._errorModel.isValid = false;
|
|
375
|
+
this._errorModel.errors.push(
|
|
376
|
+
`Property '${entity}' on entity '${entityName}' contains an Array, expected a scalar value`
|
|
377
|
+
);
|
|
378
|
+
} else {
|
|
379
|
+
this._errorModel.isValid = false;
|
|
380
|
+
this._errorModel.errors.push(
|
|
381
|
+
`Property '${entity}' on entity '${entityName}' contains an Object, expected a scalar value`
|
|
382
|
+
);
|
|
383
|
+
}
|
|
376
384
|
}
|
|
377
385
|
}
|
|
378
386
|
}
|
|
@@ -441,4 +449,4 @@ class InsertManager {
|
|
|
441
449
|
}
|
|
442
450
|
}
|
|
443
451
|
|
|
444
|
-
|
|
452
|
+
export default InsertManager;
|
package/mySQLConnect.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Version 1.0.0 - MySQL async connection manager using mysql2/promise
|
|
2
|
-
|
|
2
|
+
import mysql from 'mysql2/promise';
|
|
3
3
|
|
|
4
4
|
class MySQLAsyncClient {
|
|
5
5
|
constructor(config) {
|
|
@@ -48,4 +48,4 @@ class MySQLAsyncClient {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
export default MySQLAsyncClient;
|
package/mySQLEngine.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Version 1.0.0 - Complete MySQL implementation with mysql2/promise
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import tools from './Tools.js';
|
|
3
|
+
import FieldTransformer from './Entity/fieldTransformer.js';
|
|
4
4
|
|
|
5
5
|
class MySQLEngine {
|
|
6
6
|
|
|
@@ -156,26 +156,32 @@ class MySQLEngine {
|
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
/**
|
|
159
|
-
* SELECT COUNT
|
|
159
|
+
* SELECT COUNT. Builds the SQL inline (like SQLite/Postgres do) instead
|
|
160
|
+
* of trying to regex-wrap the full SELECT from buildQuery — the old code
|
|
161
|
+
* called `queryObject.count(sqlString)` which is a queryScript builder
|
|
162
|
+
* method (expects a lambda), not a SQL wrapper; the return value was the
|
|
163
|
+
* queryScript object and running it as SQL produced "[object Object]".
|
|
160
164
|
*/
|
|
161
165
|
async getCount(queryObject, entity, context) {
|
|
162
166
|
const query = queryObject.script;
|
|
163
167
|
try {
|
|
164
|
-
let
|
|
168
|
+
let sql;
|
|
165
169
|
if (query.raw) {
|
|
166
|
-
|
|
170
|
+
sql = query.raw;
|
|
167
171
|
} else {
|
|
168
|
-
|
|
172
|
+
if (query.count === undefined) {
|
|
173
|
+
query.count = "none";
|
|
174
|
+
}
|
|
175
|
+
sql = `SELECT ${this.buildCount(query, entity)} ${this.buildFrom(query, entity)} ${this.buildWhere(query, entity)} ${this.buildAnd(query, entity)}`;
|
|
169
176
|
}
|
|
170
177
|
|
|
171
|
-
if (
|
|
172
|
-
const queryCount = queryObject.count(queryString.query);
|
|
178
|
+
if (sql) {
|
|
173
179
|
const params = query.parameters ? query.parameters.getParams() : [];
|
|
174
180
|
if (process.env.LOG_SQL === 'true' || process.env.NODE_ENV !== 'production') {
|
|
175
|
-
console.debug("[SQL]",
|
|
181
|
+
console.debug("[SQL]", sql);
|
|
176
182
|
console.debug("[Params]", params);
|
|
177
183
|
}
|
|
178
|
-
const result = await this._runWithParams(
|
|
184
|
+
const result = await this._runWithParams(sql, params);
|
|
179
185
|
return result[0] || null;
|
|
180
186
|
}
|
|
181
187
|
return null;
|
|
@@ -185,6 +191,20 @@ class MySQLEngine {
|
|
|
185
191
|
}
|
|
186
192
|
}
|
|
187
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Build COUNT(...) fragment for SELECT ... COUNT(...) FROM ...
|
|
196
|
+
* Matches the SQLite/Postgres engine contract.
|
|
197
|
+
*/
|
|
198
|
+
buildCount(query, entity) {
|
|
199
|
+
const alias = this.getEntity(query.parentName || entity.__name, query.entityMap);
|
|
200
|
+
if (!query.count) return "";
|
|
201
|
+
if (query.count === "none") return `COUNT(*)`;
|
|
202
|
+
// query.count is a cachedExpr with selectFields when set via `.count(l => l.field)`.
|
|
203
|
+
const field = query.count.selectFields && query.count.selectFields[0];
|
|
204
|
+
if (!field) return `COUNT(*)`;
|
|
205
|
+
return `COUNT(${alias}.\`${field}\`)`;
|
|
206
|
+
}
|
|
207
|
+
|
|
188
208
|
/**
|
|
189
209
|
* SELECT multiple records
|
|
190
210
|
*/
|
|
@@ -260,16 +280,121 @@ class MySQLEngine {
|
|
|
260
280
|
select: this.buildSelect(query, entity),
|
|
261
281
|
from: this.buildFrom(query, entity),
|
|
262
282
|
include: this.buildInclude(query, entity, context, {}),
|
|
263
|
-
where: this.buildWhere(query, entity)
|
|
283
|
+
where: this.buildWhere(query, entity),
|
|
284
|
+
and: this.buildAnd(query, entity),
|
|
285
|
+
orderBy: this.buildOrderBy(query, entity),
|
|
286
|
+
take: this.buildTake(query),
|
|
287
|
+
skip: this.buildSkip(query)
|
|
264
288
|
};
|
|
265
289
|
|
|
266
|
-
const queryString = `${queryObject.select} ${queryObject.from} ${queryObject.include} ${queryObject.where}`;
|
|
290
|
+
const queryString = `${queryObject.select} ${queryObject.from} ${queryObject.include} ${queryObject.where} ${queryObject.and} ${queryObject.orderBy} ${queryObject.take} ${queryObject.skip}`;
|
|
267
291
|
return {
|
|
268
292
|
query: queryString,
|
|
269
293
|
entity: this.getEntity(entity.__name, query.entityMap)
|
|
270
294
|
};
|
|
271
295
|
}
|
|
272
296
|
|
|
297
|
+
/**
|
|
298
|
+
* Build ORDER BY clause from query.orderBy or query.orderByDesc.
|
|
299
|
+
* Both are objects shaped like `{ selectFields: ['col1', 'col2'], ... }`
|
|
300
|
+
* (see QueryLanguage/queryScript.js buildScript()).
|
|
301
|
+
*/
|
|
302
|
+
buildOrderBy(query, entity) {
|
|
303
|
+
let orderByType = "ASC";
|
|
304
|
+
let orderByEntity = query.orderBy;
|
|
305
|
+
if (orderByEntity === false || orderByEntity === undefined) {
|
|
306
|
+
orderByType = "DESC";
|
|
307
|
+
orderByEntity = query.orderByDesc;
|
|
308
|
+
}
|
|
309
|
+
if (!orderByEntity) return "";
|
|
310
|
+
|
|
311
|
+
// Security: Validate field exists in entity before interpolating into SQL.
|
|
312
|
+
if (entity && orderByEntity.selectFields) {
|
|
313
|
+
for (const item in orderByEntity.selectFields) {
|
|
314
|
+
const field = orderByEntity.selectFields[item];
|
|
315
|
+
if (!entity[field]) {
|
|
316
|
+
throw new Error(`Invalid ORDER BY field: ${field} not found in ${entity.__name || 'entity'}`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const entityAlias = this.getEntity(query.parentName, query.entityMap);
|
|
322
|
+
const fieldList = [];
|
|
323
|
+
for (const item in orderByEntity.selectFields) {
|
|
324
|
+
fieldList.push(`${entityAlias}.\`${orderByEntity.selectFields[item]}\``);
|
|
325
|
+
}
|
|
326
|
+
if (fieldList.length === 0) return "";
|
|
327
|
+
return `ORDER BY ${fieldList.join(', ')} ${orderByType}`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
buildTake(query) {
|
|
331
|
+
if (query.take) {
|
|
332
|
+
return `LIMIT ${Number(query.take)}`;
|
|
333
|
+
}
|
|
334
|
+
return "";
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
buildSkip(query) {
|
|
338
|
+
if (query.skip) {
|
|
339
|
+
return `OFFSET ${Number(query.skip)}`;
|
|
340
|
+
}
|
|
341
|
+
return "";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Build chained AND clause from query.and (array of cachedExpr objects).
|
|
346
|
+
* Produces "AND <expr> AND <expr>" that appends to the WHERE clause.
|
|
347
|
+
*/
|
|
348
|
+
buildAnd(query, mainQuery) {
|
|
349
|
+
const andEntity = query.and;
|
|
350
|
+
const $that = this;
|
|
351
|
+
if (!andEntity) return "";
|
|
352
|
+
|
|
353
|
+
let entity = this.getEntity(query.parentName, query.entityMap);
|
|
354
|
+
const andList = [];
|
|
355
|
+
|
|
356
|
+
for (const entityPart in andEntity) {
|
|
357
|
+
const itemEntity = andEntity[entityPart];
|
|
358
|
+
for (const table in itemEntity[query.parentName]) {
|
|
359
|
+
const item = itemEntity[query.parentName][table];
|
|
360
|
+
const expressions = [];
|
|
361
|
+
for (const exp in item.expressions) {
|
|
362
|
+
let field = tools.capitalizeFirstLetter(item.expressions[exp].field);
|
|
363
|
+
if (mainQuery[field] && mainQuery[field].isNavigational) {
|
|
364
|
+
entity = $that.getEntity(field, query.entityMap);
|
|
365
|
+
field = item.fields[1];
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let func = item.expressions[exp].func;
|
|
369
|
+
const arg = item.expressions[exp].arg;
|
|
370
|
+
|
|
371
|
+
if (arg === "null") {
|
|
372
|
+
if (func === "=") func = "IS";
|
|
373
|
+
if (func === "!=") func = "IS NOT";
|
|
374
|
+
expressions.push(`${entity}.\`${field}\` ${func} ${arg}`);
|
|
375
|
+
} else {
|
|
376
|
+
// arg may be a parameterized placeholder (?, $1, (?, ?, ?))
|
|
377
|
+
// or a literal value quoted with single quotes.
|
|
378
|
+
const isPlaceholder = (arg === '?' || /^\$\d+$/.test(arg) || /^\(.*\)$/.test(arg));
|
|
379
|
+
if (isPlaceholder || func === "IN") {
|
|
380
|
+
expressions.push(`${entity}.\`${field}\` ${func} ${arg}`);
|
|
381
|
+
} else {
|
|
382
|
+
expressions.push(`${entity}.\`${field}\` ${func} '${arg}'`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (expressions.length > 0) {
|
|
387
|
+
andList.push(expressions.join(" AND "));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (andList.length > 0) {
|
|
393
|
+
return `AND ${andList.join(" AND ")}`;
|
|
394
|
+
}
|
|
395
|
+
return "";
|
|
396
|
+
}
|
|
397
|
+
|
|
273
398
|
buildSelect(query, entity) {
|
|
274
399
|
const select = "SELECT";
|
|
275
400
|
const arr = [];
|
|
@@ -627,9 +752,23 @@ class MySQLEngine {
|
|
|
627
752
|
params.push(model["_" + dirtyFields[column]]);
|
|
628
753
|
break;
|
|
629
754
|
|
|
630
|
-
default:
|
|
755
|
+
default: {
|
|
756
|
+
// Covers `text` and any other column type without a dedicated
|
|
757
|
+
// case above. Run the toDatabase transformer here too so that
|
|
758
|
+
// fields with a serializer (e.g. JSON text columns) get their
|
|
759
|
+
// object values turned into scalars before they reach the driver.
|
|
760
|
+
let rawValue = model["_" + dirtyFields[column]];
|
|
761
|
+
if (rawValue === undefined) {
|
|
762
|
+
rawValue = model[dirtyFields[column]];
|
|
763
|
+
}
|
|
764
|
+
try {
|
|
765
|
+
rawValue = FieldTransformer.toDatabase(rawValue, model.__entity[dirtyFields[column]], model.__entity.__name, dirtyFields[column]);
|
|
766
|
+
} catch (transformError) {
|
|
767
|
+
throw new Error(`UPDATE failed: ${transformError.message}`);
|
|
768
|
+
}
|
|
631
769
|
sqlParts.push(`\`${dirtyFields[column]}\` = ?`);
|
|
632
|
-
params.push(
|
|
770
|
+
params.push(rawValue);
|
|
771
|
+
}
|
|
633
772
|
}
|
|
634
773
|
}
|
|
635
774
|
|
|
@@ -656,13 +795,19 @@ class MySQLEngine {
|
|
|
656
795
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
657
796
|
}
|
|
658
797
|
|
|
659
|
-
if (
|
|
798
|
+
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
799
|
+
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
800
|
+
// objects into scalars (e.g. JSON.stringify) so running them
|
|
801
|
+
// before the type check is essential for fields that use a
|
|
802
|
+
// custom serializer on top of a text/json column.
|
|
660
803
|
try {
|
|
661
804
|
fieldColumn = FieldTransformer.toDatabase(fieldColumn, modelEntity[column], modelEntity.__name, column);
|
|
662
805
|
} catch (transformError) {
|
|
663
806
|
throw new Error(`INSERT failed: ${transformError.message}`);
|
|
664
807
|
}
|
|
808
|
+
}
|
|
665
809
|
|
|
810
|
+
if ((fieldColumn !== undefined && fieldColumn !== null) && typeof(fieldColumn) !== "object") {
|
|
666
811
|
try {
|
|
667
812
|
fieldColumn = $that._validateAndCoerceFieldType(fieldColumn, modelEntity[column], modelEntity.__name, column);
|
|
668
813
|
} catch (typeError) {
|
|
@@ -854,4 +999,4 @@ class MySQLEngine {
|
|
|
854
999
|
}
|
|
855
1000
|
}
|
|
856
1001
|
|
|
857
|
-
|
|
1002
|
+
export default MySQLEngine;
|
package/package.json
CHANGED
|
@@ -1,14 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.001",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./MasterRecord.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./MasterRecord.js",
|
|
9
|
+
"./context": "./context.js",
|
|
10
|
+
"./Tools": "./Tools.js",
|
|
11
|
+
"./SQLLiteEngine": "./SQLLiteEngine.js",
|
|
12
|
+
"./mySQLEngine": "./mySQLEngine.js",
|
|
13
|
+
"./postgresEngine": "./postgresEngine.js",
|
|
14
|
+
"./mySQLConnect": "./mySQLConnect.js",
|
|
15
|
+
"./postgresSyncConnect": "./postgresSyncConnect.js",
|
|
16
|
+
"./insertManager": "./insertManager.js",
|
|
17
|
+
"./deleteManager": "./deleteManager.js",
|
|
18
|
+
"./Cache/QueryCache": "./Cache/QueryCache.js",
|
|
19
|
+
"./Cache/RedisQueryCache": "./Cache/RedisQueryCache.js",
|
|
20
|
+
"./Entity/entityModel": "./Entity/entityModel.js",
|
|
21
|
+
"./Entity/entityModelBuilder": "./Entity/entityModelBuilder.js",
|
|
22
|
+
"./Entity/entityTrackerModel": "./Entity/entityTrackerModel.js",
|
|
23
|
+
"./Entity/fieldTransformer": "./Entity/fieldTransformer.js",
|
|
24
|
+
"./QueryLanguage/queryMethods": "./QueryLanguage/queryMethods.js",
|
|
25
|
+
"./QueryLanguage/queryParameters": "./QueryLanguage/queryParameters.js",
|
|
26
|
+
"./QueryLanguage/queryScript": "./QueryLanguage/queryScript.js",
|
|
27
|
+
"./Migrations/schema": "./Migrations/schema.js",
|
|
28
|
+
"./Migrations/migrations": "./Migrations/migrations.js",
|
|
29
|
+
"./Migrations/migrationTemplate": "./Migrations/migrationTemplate.js",
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
6
32
|
"bin": {
|
|
7
|
-
"masterrecord": "Migrations/cli.js"
|
|
33
|
+
"masterrecord": "./Migrations/cli.js"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=20.0.0"
|
|
8
37
|
},
|
|
9
38
|
"scripts": {
|
|
10
|
-
"test": "
|
|
11
|
-
"preinstall": "node -e \"try { require.resolve('masterrecord'); } catch(e) { if (!process.env.npm_config_global) { console.log('\\n\\u001b[36m📦 MasterRecord Installation\\u001b[0m'); console.log('\\u001b[33mℹ For best experience, install globally first:\\u001b[0m'); console.log(' npm install -g masterrecord\\n'); } }\""
|
|
39
|
+
"test": "node --test test/ Migrations/pathUtils.test.js"
|
|
12
40
|
},
|
|
13
41
|
"author": "Alexandr Rich",
|
|
14
42
|
"license": "MIT",
|
|
@@ -24,6 +52,7 @@
|
|
|
24
52
|
"mysql",
|
|
25
53
|
"sqlite",
|
|
26
54
|
"postgres",
|
|
55
|
+
"esm",
|
|
27
56
|
"masterrecord"
|
|
28
57
|
],
|
|
29
58
|
"dependencies": {
|