From 9c61e83166ab6e172ac2efc4e7d281c9a8ac6856 Mon Sep 17 00:00:00 2001 From: Tben Date: Tue, 29 Aug 2023 08:02:21 +0100 Subject: [PATCH] Updates --- engine/query/utils/addDbEntry.js | 16 ++++++++++++++++ engine/query/utils/updateDbEntry.js | 8 ++++++++ package.json | 2 +- types/database-schema.td.js | 2 ++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/engine/query/utils/addDbEntry.js b/engine/query/utils/addDbEntry.js index 6feefc3..4add668 100644 --- a/engine/query/utils/addDbEntry.js +++ b/engine/query/utils/addDbEntry.js @@ -89,6 +89,22 @@ async function addDbEntry({ dbFullName, tableName, data, tableSchema, duplicateC console.log("DSQL: Encrypted value =>", value); } + if (targetFieldSchema?.pattern) { + const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || ""); + if (!pattern.test(value)) { + console.log("DSQL: Pattern not matched =>", value); + value = ""; + } + } + + if (typeof value === "string" && !value.match(/./i)) { + value = { + toSqlString: function () { + return "NULL"; + }, + }; + } + insertKeysArray.push("`" + dataKey + "`"); if (typeof value === "object") { diff --git a/engine/query/utils/updateDbEntry.js b/engine/query/utils/updateDbEntry.js index 74eca6d..2531b5b 100644 --- a/engine/query/utils/updateDbEntry.js +++ b/engine/query/utils/updateDbEntry.js @@ -81,6 +81,14 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, }; } + if (targetFieldSchema?.pattern) { + const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || ""); + if (!pattern.test(value)) { + console.log("DSQL: Pattern not matched =>", value); + value = ""; + } + } + if (typeof value === "string" && !value.match(/./i)) { value = { toSqlString: function () { diff --git a/package.json b/package.json index eb00c03..b6b1ca8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.8.8", + "version": "1.8.9", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { diff --git a/types/database-schema.td.js b/types/database-schema.td.js index b4ceb9f..bf4a285 100644 --- a/types/database-schema.td.js +++ b/types/database-schema.td.js @@ -60,6 +60,8 @@ * @property {string} [defaultValueLiteral] - SQL key word which generates value automatically => "CURRENT_TIMESTAMP" * @property {DSQL_ForeignKeyType} [foreignKey] - Field foreign key reference object * @property {boolean} [richText] - Rich text field + * @property {string | RegExp} [pattern] - Field pattern for validation. Can be a string or a regular expression. Example: "^[a-zA-Z0-9_]*$" + * @property {string} [patternFlags] - Field pattern flags for validation. Example: "i" */ /**