sequencework / sql Goto Github PK
View Code? Open in Web Editor NEWJavascript tag to format SQL template literals
Javascript tag to format SQL template literals
Have you seen pg-sql2
? Just wondering how it compares to this lib?
const sql = require('@sequencework/sql')
console.log(sql`
0 ${sql.raw(`
1 ${sql.raw('2')}
`)}
`.text)
Output :
\n 0 \n 1 [object Object]\n \n
Expected :
\n 0 \n 1 2\n \n
Actually, it's not really a bug, it's more a discussion.
I have a library which constructs SQL filters and I want to consume it in my main project.
In library
:
export const filter = sql`WHERE year = 2018`;
In the main project:
const {filter} = require('library');
sql`SELECT * FROM books ${filterThisYear ? filter : undefined}`
I expect nested sql
tags to merge and append the filter.
Due to the object weirdness in JavaScript, class instances are compared by their prototype chain which depends on the way the class was imported. In this case, instanceof returns false
. The SqlContainer
class is the same, but imported from a different place. Finally this cause sql
not to detect nested tag and treats is as an object parameter instead.
I created a fork which adds a tag method to allow comparison across modules. This is not the cleanest code ever, but is successfully used in Moment.js for instance.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.