From 1833f82c34013b73df8677f3501930a045600510 Mon Sep 17 00:00:00 2001 From: Alex Anderson <191496+alxndrsn@users.noreply.github.com> Date: Wed, 30 Oct 2024 08:09:59 +0300 Subject: [PATCH] lib/model/frame: use sql.*() instead of slonik-sql-tag-raw (#1239) --- lib/model/frame.js | 6 +++--- test/unit/model/frame.js | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/model/frame.js b/lib/model/frame.js index 5f4654e5d..cd1212def 100644 --- a/lib/model/frame.js +++ b/lib/model/frame.js @@ -7,7 +7,7 @@ // including this file, may be copied, modified, propagated, or distributed // except according to the terms contained in the LICENSE file. -const { raw } = require('slonik-sql-tag-raw'); +const { sql } = require('slonik'); const { pick, without, remove, indexOf } = require('ramda'); const uuid = require('uuid').v4; const { pickAll, noargs } = require('../util/util'); @@ -68,11 +68,11 @@ class Frame { { /* eslint-disable no-shadow */ // TODO: precomputing is good but this is sort of dirty :/ const Frame = class extends this { static get def() { return def; } }; - Frame.fieldlist = raw(def.fields.map((s) => `"${s}"`).join(',')); + Frame.fieldlist = sql.join(def.fields.map(f => sql.identifier([f])), sql`,`); Frame.insertfields = without([ 'id' ], def.fields); const indexOfId = indexOf('id', def.fields); Frame.insertFieldTypes = indexOfId > -1 ? remove(indexOf('id', def.fields), 1, def.fieldTypes ?? []) : def.fieldTypes; - Frame.insertlist = raw(Frame.insertfields.map((s) => `"${s}"`).join(',')); + Frame.insertlist = sql.join(Frame.insertfields.map(f => sql.identifier([f])), sql`,`); Frame.hasCreatedAt = def.fields.includes('createdAt'); Frame.hasUpdatedAt = def.fields.includes('updatedAt'); return Frame; diff --git a/test/unit/model/frame.js b/test/unit/model/frame.js index aa6cdc4bd..ca1c1dbef 100644 --- a/test/unit/model/frame.js +++ b/test/unit/model/frame.js @@ -7,7 +7,10 @@ const Option = require(appRoot + '/lib/util/option'); describe('Frame', () => { describe('definition', () => { it('should accept fields', () => { Frame.define('a', 'b').fields.should.eql([ 'a', 'b' ]); }); - it('should create a fieldlist', () => { Frame.define('a', 'b').fieldlist.should.eql(sql`"a","b"`); }); + it('should create a fieldlist', () => { + const { fieldlist } = Frame.define('a', 'b'); + sql`${fieldlist}`.should.eql(sql`"a","b"`); + }); it('should note readables', () => { Frame.define('a', writable, readable, 'b', 'c', readable).def.readable.should.eql([ 'a', 'c' ]); }); @@ -17,7 +20,7 @@ describe('Frame', () => { it('should note insert fields and list', () => { const Box = Frame.define('id', 'a', readable, writable, 'b', writable, 'c'); Box.insertfields.should.eql([ 'a', 'b', 'c' ]); - Box.insertlist.should.eql(sql`"a","b","c"`); + sql`${Box.insertlist}`.should.eql(sql`"a","b","c"`); }); it('should note hasCreatedAt and hasUpdatedAt', () => { const T = Frame.define('updatedAt');