The minimalistic document archiving platform.
refactor(database): extract SQL stringification helper (#1087)
Corentin Thomasset6e0abd834f8c
diff --git a/apps/papra-server/src/modules/app/database/database.models.test.ts b/apps/papra-server/src/modules/app/database/database.models.test.tsnew file mode 100644index 00000000..cb7b24a2--- /dev/null+++ b/apps/papra-server/src/modules/app/database/database.models.test.ts@@ -0,0 +1,23 @@+import { sql } from 'drizzle-orm';+import { describe, expect, test } from 'vitest';+import { stringifySqlQuery } from './database.models';++describe('database.models', () => {+ describe('stringifySqlQuery', async () => {+ test('works with raw SQL query builder', () => {+ expect(+ stringifySqlQuery(sql`select name from users where id = ${'user_1'}`),+ ).to.eql({+ query: 'select name from users where id = ?',+ params: ['user_1'],+ });+ });++ test('no SQL query builder provided means empty query and params', () => {+ expect(stringifySqlQuery()).to.eql({+ query: '',+ params: [],+ });+ });+ });+});diff --git a/apps/papra-server/src/modules/app/database/database.models.ts b/apps/papra-server/src/modules/app/database/database.models.tsnew file mode 100644index 00000000..033ebcca--- /dev/null+++ b/apps/papra-server/src/modules/app/database/database.models.ts@@ -0,0 +1,13 @@+import type { SQL } from 'drizzle-orm';+import { SQLiteSyncDialect } from 'drizzle-orm/sqlite-core';++const sqliteDialect = new SQLiteSyncDialect();++export function stringifySqlQuery(queryBuilder?: SQL) {+ if (!queryBuilder) {+ return { query: '', params: [] };+ }++ const { sql: query, params } = sqliteDialect.sqlToQuery(queryBuilder);+ return { query, params };+}diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.models.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.models.test.tsindex 6a050704..c7463b61 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.models.test.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.models.test.ts@@ -1,6 +1,5 @@-import type { SQL } from 'drizzle-orm';-import { SQLiteSyncDialect } from 'drizzle-orm/sqlite-core';import { describe, expect, test } from 'vitest';+import { stringifySqlQuery } from '../../../app/database/database.models';import { createInMemoryDatabase } from '../../../app/database/database.test-utils';import { makeSearchWhereClause } from './database-fts5.repository.models';@@ -8,22 +7,12 @@ describe('database-fts5 repository models', () => {describe('makeSearchWhereClause', async () => {const { db } = await createInMemoryDatabase();- const sqliteDialect = new SQLiteSyncDialect();- const getSqlString = (query?: SQL) => {- if (!query) {- return { sql: '', params: [] };- }-- const { sql, params } = sqliteDialect.sqlToQuery(query);- return { sql, params };- };-test('a simple text query', () => {const { issues, searchWhereClause } = makeSearchWhereClause({ organizationId: 'org_1', query: 'foo', db });expect(issues).to.eql([]);- expect(getSqlString(searchWhereClause)).to.eql({- sql: `(\"documents\".\"organization_id\" = ? and \"documents\".\"is_deleted\" = ? and \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?))`,+ expect(stringifySqlQuery(searchWhereClause)).to.eql({+ query: `(\"documents\".\"organization_id\" = ? and \"documents\".\"is_deleted\" = ? and \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?))`,params: ['org_1',0,@@ -40,8 +29,8 @@ describe('database-fts5 repository models', () => {});expect(issues).to.eql([]);- expect(getSqlString(searchWhereClause)).to.eql({- sql: '("documents"."organization_id" = ? and "documents"."is_deleted" = ? and (("documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."normalized_name" = ?)) or "documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."normalized_name" = ?))) and not "documents"."id" in (select distinct "document_id" from "documents_fts" where "documents_fts" = ?)))',+ expect(stringifySqlQuery(searchWhereClause)).to.eql({+ query: '("documents"."organization_id" = ? and "documents"."is_deleted" = ? and (("documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."normalized_name" = ?)) or "documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."normalized_name" = ?))) and not "documents"."id" in (select distinct "document_id" from "documents_fts" where "documents_fts" = ?)))',params: ['org_1',0,diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.custom-properties.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.custom-properties.test.tsindex 131b83a2..6e989399 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.custom-properties.test.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.custom-properties.test.ts@@ -1,25 +1,13 @@-import type { SQL } from 'drizzle-orm';import type { CustomPropertyDefinition } from './query-builder.custom-properties';-import { SQLiteSyncDialect } from 'drizzle-orm/sqlite-core';import { describe, expect, test } from 'vitest';+import { stringifySqlQuery } from '../../../../app/database/database.models';import { createInMemoryDatabase } from '../../../../app/database/database.test-utils';import { handleCustomPropertyFilter, handleHasCustomPropertyFilter } from './query-builder.custom-properties';describe('query-builder - custom properties', async () => {const { db } = await createInMemoryDatabase();- const sqliteDialect = new SQLiteSyncDialect();const now = new Date('2025-06-15');- const getSqlString = (query?: SQL) => {- if (!query) {- return { sql: '', params: [] };- }-- const { sql, params } = sqliteDialect.sqlToQuery(query);-- return { sql, params };- };-function makeDefinition(overrides: Partial<CustomPropertyDefinition> = {}): CustomPropertyDefinition {return {id: 'cpd_1',@@ -46,8 +34,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."boolean_value" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."boolean_value" = ?))`,params: ['cpd_1', 1],});});@@ -61,8 +49,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."boolean_value" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."boolean_value" = ?))`,params: ['cpd_1', 0],});});@@ -91,7 +79,7 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery).params[1]).to.equal(expected);+ expect(stringifySqlQuery(sqlQuery).params[1]).to.equal(expected);});test('warranty:maybe returns an INVALID_BOOLEAN_VALUE issue and a no-match query', () => {@@ -106,7 +94,7 @@ describe('query-builder - custom properties', async () => {message: 'Invalid boolean value "maybe" for warranty filter. Expected one of: true, yes, y, 1, on, enabled, false, no, n, 0, off, disabled',code: 'INVALID_BOOLEAN_VALUE',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});test('warranty:>true returns an UNSUPPORTED_FILTER_OPERATOR issue for non-equality operators', () => {@@ -121,7 +109,7 @@ describe('query-builder - custom properties', async () => {message: 'Unsupported operator ">" for warranty filter',code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -135,8 +123,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."text_value" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."text_value" = ?))`,params: ['cpd_2', 'invoice'],});});@@ -153,7 +141,7 @@ describe('query-builder - custom properties', async () => {message: 'Unsupported operator ">" for category filter',code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -167,8 +155,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" = ?))`,params: ['cpd_3', 100],});});@@ -182,8 +170,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" > ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" > ?))`,params: ['cpd_3', 100],});});@@ -197,8 +185,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" >= ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" >= ?))`,params: ['cpd_3', 100],});});@@ -212,8 +200,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" < ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" < ?))`,params: ['cpd_3', 100],});});@@ -227,8 +215,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" <= ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."number_value" <= ?))`,params: ['cpd_3', 100],});});@@ -245,7 +233,7 @@ describe('query-builder - custom properties', async () => {message: 'Invalid number value "notanumber" for amount filter. Expected a numeric value.',code: 'INVALID_NUMBER_VALUE',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -259,8 +247,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."date_value" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."date_value" = ?))`,params: ['cpd_4', 1735689600000],});});@@ -274,8 +262,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."date_value" > ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."date_value" > ?))`,params: ['cpd_4', 1735689600000],});});@@ -289,8 +277,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."date_value" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."date_value" = ?))`,params: ['cpd_4', now.getTime()],});});@@ -307,7 +295,7 @@ describe('query-builder - custom properties', async () => {message: 'Invalid date format for expiry filter: "not-a-date". Expected a valid date string.',code: 'INVALID_DATE_FORMAT',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -321,8 +309,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" inner join "custom_property_select_options" on "document_custom_property_values"."select_option_id" = "custom_property_select_options"."id" where ("document_custom_property_values"."property_definition_id" = ? and "custom_property_select_options"."key" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" inner join "custom_property_select_options" on "document_custom_property_values"."select_option_id" = "custom_property_select_options"."id" where ("document_custom_property_values"."property_definition_id" = ? and "custom_property_select_options"."key" = ?))`,params: ['cpd_5', 'approved'],});});@@ -339,7 +327,7 @@ describe('query-builder - custom properties', async () => {message: 'Unsupported operator ">" for status filter',code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -353,8 +341,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" inner join "custom_property_select_options" on "document_custom_property_values"."select_option_id" = "custom_property_select_options"."id" where ("document_custom_property_values"."property_definition_id" = ? and "custom_property_select_options"."key" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" inner join "custom_property_select_options" on "document_custom_property_values"."select_option_id" = "custom_property_select_options"."id" where ("document_custom_property_values"."property_definition_id" = ? and "custom_property_select_options"."key" = ?))`,params: ['cpd_6', 'urgent'],});});@@ -370,8 +358,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" left join "users" on "document_custom_property_values"."user_id" = "users"."id" where ("document_custom_property_values"."property_definition_id" = ? and ("document_custom_property_values"."user_id" = ? or "users"."email" = ? or "users"."name" = ?)))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" left join "users" on "document_custom_property_values"."user_id" = "users"."id" where ("document_custom_property_values"."property_definition_id" = ? and ("document_custom_property_values"."user_id" = ? or "users"."email" = ? or "users"."name" = ?)))`,params: ['cpd_7', 'usr_1', 'usr_1', 'usr_1'],});});@@ -385,8 +373,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" left join "users" on "document_custom_property_values"."user_id" = "users"."id" where ("document_custom_property_values"."property_definition_id" = ? and ("document_custom_property_values"."user_id" = ? or "users"."email" = ? or "users"."name" = ?)))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_custom_property_values"."document_id" from "document_custom_property_values" left join "users" on "document_custom_property_values"."user_id" = "users"."id" where ("document_custom_property_values"."property_definition_id" = ? and ("document_custom_property_values"."user_id" = ? or "users"."email" = ? or "users"."name" = ?)))`,});});@@ -403,7 +391,7 @@ describe('query-builder - custom properties', async () => {message: 'Unsupported operator ">" for assignee filter',code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -417,8 +405,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."related_document_id" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."related_document_id" = ?))`,params: ['cpd_8', 'doc_1'],});});@@ -435,7 +423,7 @@ describe('query-builder - custom properties', async () => {message: 'Unsupported operator ">" for related filter',code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: '0', params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: '0', params: [] });});});@@ -447,8 +435,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."boolean_value" is not null))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."boolean_value" is not null))`,params: ['cpd_1'],});});@@ -460,8 +448,8 @@ describe('query-builder - custom properties', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."text_value" is not null))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select "document_id" from "document_custom_property_values" where ("document_custom_property_values"."property_definition_id" = ? and "document_custom_property_values"."text_value" is not null))`,params: ['cpd_2'],});});diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.tsindex 7c89295f..097d7aa6 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.ts@@ -1,6 +1,5 @@-import type { SQL } from 'drizzle-orm';-import { SQLiteSyncDialect } from 'drizzle-orm/sqlite-core';import { describe, expect, test } from 'vitest';+import { stringifySqlQuery } from '../../../../app/database/database.models';import { createInMemoryDatabase } from '../../../../app/database/database.test-utils';import {handleAndExpression,@@ -20,15 +19,6 @@ import {describe('query-builder', async () => {const { db } = await createInMemoryDatabase();- const sqliteDialect = new SQLiteSyncDialect();- const getSqlString = (query?: SQL) => {- if (!query) {- return { sql: '', params: [] };- }-- const { sql, params } = sqliteDialect.sqlToQuery(query);- return { sql, params };- };describe('handleNameFilter', () => {test('builds a fts5 scoped on organization id and name column', () => {@@ -40,8 +30,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {name}:"important"*'],});});@@ -55,8 +45,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {name}:"important (draft)"*'],});});@@ -73,7 +63,7 @@ describe('query-builder', async () => {code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});});@@ -87,8 +77,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {content}:"confidential"*'],});});@@ -102,8 +92,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {content}:"confidential [v2]"*'],});});@@ -120,7 +110,7 @@ describe('query-builder', async () => {code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});});@@ -133,8 +123,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"created_at\" = ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"created_at\" = ?`,params: [1705276800000],});});@@ -148,8 +138,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"created_at\" > ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"created_at\" > ?`,params: [1705276800000],});});@@ -162,8 +152,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"created_at\" >= ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"created_at\" >= ?`,params: [1705276800000],});});@@ -176,8 +166,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"created_at\" < ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"created_at\" < ?`,params: [1705276800000],});});@@ -190,8 +180,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"created_at\" <= ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"created_at\" <= ?`,params: [1705276800000],});});@@ -207,7 +197,7 @@ describe('query-builder', async () => {code: 'INVALID_DATE_FORMAT',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});test('accepts various ISO date formats', () => {@@ -236,8 +226,8 @@ describe('query-builder', async () => {});expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"created_at\" = ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"created_at\" = ?`,params: [timestamp],});}@@ -255,8 +245,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" = ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" = ?`,params: [1705276800000],});});@@ -269,8 +259,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" > ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" > ?`,params: [1705276800000],});});@@ -283,8 +273,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" >= ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" >= ?`,params: [1705276800000],});});@@ -297,8 +287,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" < ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" < ?`,params: [1705276800000],});});@@ -311,8 +301,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" <= ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" <= ?`,params: [1705276800000],});});@@ -328,7 +318,7 @@ describe('query-builder', async () => {code: 'INVALID_DATE_FORMAT',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});test('supports "now" value to use the current date', () => {@@ -339,8 +329,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" <= ?`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" <= ?`,params: [now.getTime()],});});@@ -356,8 +346,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."document_date" is not null`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."document_date" is not null`,params: [],});});@@ -374,7 +364,7 @@ describe('query-builder', async () => {code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});});@@ -384,8 +374,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `1`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `1`,params: [],});});@@ -401,8 +391,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {name content}:"budget report"*'],});});@@ -416,8 +406,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {name content}:"Q3 (final)"*'],});});@@ -440,8 +430,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `(\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?) and \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `(\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?) and \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?))`,params: ['organization_id:"org_1" {name content}:"invoice"*','organization_id:"org_1" {name}:"Q3"*',@@ -474,8 +464,8 @@ describe('query-builder', async () => {},]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `(0 and 0)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `(0 and 0)`,params: [],});});@@ -498,8 +488,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `(\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?) or \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?))`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `(\"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?) or \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?))`,params: ['organization_id:"org_1" {name content}:"invoice"*','organization_id:"org_1" {name content}:"receipt"*',@@ -532,8 +522,8 @@ describe('query-builder', async () => {},]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `(0 or 0)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `(0 or 0)`,params: [],});});@@ -553,8 +543,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `not \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `not \"documents\".\"id\" in (select distinct \"document_id\" from \"documents_fts\" where \"documents_fts\" = ?)`,params: ['organization_id:"org_1" {name content}:"spam"*'],});});@@ -572,8 +562,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `not 1`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `not 1`,params: [],});});@@ -594,8 +584,8 @@ describe('query-builder', async () => {code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `not 0`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `not 0`,params: [],});});@@ -611,8 +601,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where "tags"."organization_id" = ?)`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where "tags"."organization_id" = ?)`,params: ['org_1'],});});@@ -629,7 +619,7 @@ describe('query-builder', async () => {code: 'UNSUPPORTED_FILTER_OPERATOR',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});});@@ -641,7 +631,7 @@ describe('query-builder', async () => {message: 'Unsupported expression type',code: 'UNSUPPORTED_EXPRESSION_TYPE',}]);- expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ expect(stringifySqlQuery(sqlQuery)).to.eql({ query: `0`, params: [] });});test('returns a no-op SQL query that always evaluates to false', () => {@@ -652,8 +642,8 @@ describe('query-builder', async () => {code: 'UNSUPPORTED_EXPRESSION_TYPE',}]);- expect(getSqlString(sqlQuery)).to.eql({- sql: `0`,+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: `0`,params: [],});});@@ -669,8 +659,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: '"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."id" = ?))',+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: '"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."id" = ?))',params: ['org_1', 'tag_123456789123456789123456'],});});@@ -684,8 +674,8 @@ describe('query-builder', async () => {expect(issues).to.eql([]);- expect(getSqlString(sqlQuery)).to.eql({- sql: '"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."normalized_name" = ?))',+ expect(stringifySqlQuery(sqlQuery)).to.eql({+ query: '"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where ("tags"."organization_id" = ? and "tags"."normalized_name" = ?))',params: ['org_1', 'important thing'],});});