Skip to content

Commit

Permalink
lib/model/frame: use sql.*() instead of slonik-sql-tag-raw (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Oct 30, 2024
1 parent 42a50ad commit 1833f82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/model/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions test/unit/model/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]);
});
Expand All @@ -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');
Expand Down

0 comments on commit 1833f82

Please sign in to comment.