question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In order to address a variety of small issues, I think sql-bricks needs a significant refactor. I want to reduce the line count, but more importantly, I want to reduce the complexity. SqlBricks is no longer as “easy to understand & debug” as it claims to be.

  • Make SqlBricks namespace a function or class, so you have to call SqlBricks().select instead of SqlBricks.select (or var select = require('sql-bricks')().select;). This way you can set the default options, like SqlBricks({placeholder: '?%d'}) (#91) and that way extensions won’t need to pollute the global namespace to change things like sql.conversions (#62).
  • replace the _extension/subclass/inherits/applyNew stuff with basic prototypical inheritance & restrict the way SQLBricks can be called (throw an error if it’s called with or without new, depending on whether it’s required)
  • remove the defineClause/mini-templating language #100
  • remove the placeholder-replacing code
  • reduce the variety of ways things can be called (values as an array, object vs separate args, insert() vs insert.into() vs split_keys_vals_mode). Have one right way, thus reducing the API surface area & making the lib shorter, simpler & more maintainable
  • (possibly) support named parameters (#91)

While we’re at it, we might as well fix the outstanding bugs & issues, but I don’t want those to necessarily slow down the refactoring. It might be better to split the refactoring into pieces and do them separately over time, but I’d like to release it as one big 3.x rather than multiple backwards-incompatible releases.

Oct 2017 Update:

Looking through the capabilities and the code, I think there must be a cleaner, more generic way to go about this. I suspect that the important parts of this library can be implemented in 100 or 200 lines of code instead of ~1k. Drop the “mini” templating language and the subclassing/inherits. Probably be a little more rigid in what kinds of input the methods accept. Use JSON to define all the clauses (and their order) and what kinds of input they accept. It may make sense to template strings, but if so it should be a one-line regex, not a mini-templating language. Allow extension in a simpler way: by allowing consumers/libraries to directly swap out methods or metadata, like Backbone.ajax/sync. The toParams() parameter substitution code is quite hairy (auto-numbering, etc); we probably need to tighten up the scope and disallow certain things there.

Mar 2022 Update:

I renamed this Issue from “3.0 Refactor” to “Major Refactors”, since I decided to go ahead and ship v3.0 with only one of these (remove the defineClause/mini-templating language #100), along with the removal of the Underscore.js dependency. I would still like to tackle some of these, especially the Oct 2017 idea of simplifying the library dramatically, but I don’t have much motivation to do so, since I don’t use this library often (in most cases I think it’s better to write the SQL directly).

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
prustcommented, Oct 23, 2019

@paleo: FYI, I added a reference to “A Layer Above Database Connectors” to the readme in 9663defe67 – feel free to issue a pull request if you’d like to update it in any way.

1reaction
paleocommented, May 5, 2018

Could you consider switching to TypeScript and to a modern JS instead of ES5 / Underscore?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code refactoring - Wikipedia
In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing ...
Read more >
The Big Refactor - Medium
The team decided that the biggest value to be gained was refactoring a module of common api classes, interfaces and Java beans. Moving...
Read more >
What is Refactoring (Code Refactoring)? - TechTarget
Refactor, patch and update regularly. Refactoring generates the most return on investment when it can address a significant issue without taking too much...
Read more >
The Complete Engineer's Guide to Code Refactoring - Stepsize
In this guide, you'll learn all about refactoring source code: the benefits, challenges, tools, and best practices, and what is the ...
Read more >
Code Refactoring Best Practices: When (and When Not) to ...
How to perform code refactoring: the main techniques. Red-Green-Refactor; Refactoring by Abstraction; Composing Method; Simplifying Methods ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found