@vanit-co/sql-ts 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A TypeScript SQL query builder using tagged template literals. Write plain SQL with safe, automatic parameter binding and properly quoted identifiers. No DSL to learn, no magic, no ORM.
4
4
 
5
- Supports **MySQL** (`?` placeholders, `` ` `` backtick identifiers) and **PostgreSQL** (`$n` placeholders, `"` double-quote identifiers) out of the box.
5
+ Supports **MySQL** (`?` placeholders, `` ` `` backtick identifiers), **PostgreSQL** (`$n` placeholders, `"` double-quote identifiers) and **SQLite** (through the MySQL format, see [Result object](#result-object)) out of the box.
6
6
 
7
7
  ## Why this library?
8
8
 
@@ -105,6 +105,29 @@ const u = schema({ table: 'users', columns: ['id', 'email'], alias: 'u' })
105
105
 
106
106
  After calling `schema`, the returned object has a typed property for each column (`users.id`, `users.email`, etc.).
107
107
 
108
+ #### Multiple aliases for the same table: `alias`
109
+
110
+ `alias(table, newAlias)` returns a copy of a schema under a different alias, keeping the same table and typed columns. The original schema is left untouched, so it is handy in local scope for self-joins or joining the same table more than once.
111
+
112
+ ```ts
113
+ import { schema, alias, selectAs, join, pick } from '@vanit-co/sql-ts'
114
+
115
+ const users = schema({ table: 'users', columns: ['id', 'email'] })
116
+ const posts = schema({ table: 'posts', columns: ['author_id', 'reviewer_id'], alias: 'p' })
117
+
118
+ const author = alias(users, 'author')
119
+ const reviewer = alias(users, 'reviewer')
120
+
121
+ const q = selectAs`SELECT ${pick(author.email, reviewer.email)} FROM ${posts}`
122
+ .append(join` JOIN ${author} ON ${posts.author_id} = ${author.id}`)
123
+ .append(join` JOIN ${reviewer} ON ${posts.reviewer_id} = ${reviewer.id}`)
124
+
125
+ q.text
126
+ // SELECT "author"."email" as "author_email" ,"reviewer"."email" as "reviewer_email" FROM "posts" "p"
127
+ // JOIN "users" "author" ON "p"."author_id" = "author"."id"
128
+ // JOIN "users" "reviewer" ON "p"."reviewer_id" = "reviewer"."id"
129
+ ```
130
+
108
131
  ---
109
132
 
110
133
  ### Result object
@@ -113,7 +136,7 @@ Every template tag and statement builder returns a `Result` object with three pr
113
136
 
114
137
  | property | description |
115
138
  |----------|-------------|
116
- | `.sql` | Query string in MySQL format (`?` placeholders, backtick-quoted identifiers) |
139
+ | `.sql` | Query string in MySQL format (`?` placeholders, backtick-quoted identifiers), also used for SQLite |
117
140
  | `.text` | Query string in PostgreSQL format (`$n` placeholders, double-quote identifiers) |
118
141
  | `.values` | Array of bind values in order, ready to pass to your database driver |
119
142
 
@@ -125,8 +148,13 @@ await client.query(query)
125
148
 
126
149
  // mysql2
127
150
  await connection.query(query)
151
+
152
+ // better-sqlite3 / node:sqlite
153
+ db.prepare(query.sql).all(...query.values)
128
154
  ```
129
155
 
156
+ SQLite accepts backtick-quoted identifiers and `?` placeholders, so use `.sql` with it. Do not use `.text`: SQLite reads `$1` as a named parameter, so positional values won't bind.
157
+
130
158
  ---
131
159
 
132
160
  ## Template tag functions
@@ -328,7 +356,7 @@ q3.text // insert into "users" ("id" ,"email") values (nextval('users_id_seq'
328
356
  q3.values // ['alice@example.com']
329
357
  ```
330
358
 
331
- Column objects used as values are resolved to quoted identifiers rather than bound. They render unprefixed, since the statement has no alias in scope.
359
+ Column objects used as values are resolved to quoted identifiers rather than bound. They render unprefixed, since the statement has no alias in scope. Referencing a column inside `values (...)` is only accepted by MySQL; PostgreSQL and SQLite reject it.
332
360
 
333
361
  ### `update(table, colsVals)`
334
362
 
package/dist/index.d.ts CHANGED
@@ -47,6 +47,7 @@ type Params<T extends string> = {
47
47
  };
48
48
  declare const schema: <T extends string>({ table, columns, alias }: Params<T>) => SchemaTable<T>;
49
49
  declare const as: (col: { [K in symbol]: Column; }) => string;
50
+ declare const alias: <T extends string>(t: SchemaTable<T>, newAlias: string) => SchemaTable<T>;
50
51
 
51
52
  declare const sql: (strings: TemplateStringsArray, ...binds: Array<any>) => Result;
52
53
  declare const select: (strings: TemplateStringsArray, ...binds: Array<any>) => Result;
@@ -67,4 +68,4 @@ declare const pick: (...columns: Array<{
67
68
  }>) => Value;
68
69
  declare const raw: (content: string) => Value;
69
70
 
70
- export { all, as, concat, empty, select as g, select as groupBy, select as h, select as having, insert, select as j, select as join, select as o, select as orderBy, pick, preparedStatementName, raw, select as s, selectAs as sa, schema, select, selectAs, sql, toMysql, toPostgres, update, select as w, select as where };
71
+ export { alias, all, as, concat, empty, select as g, select as groupBy, select as h, select as having, insert, select as j, select as join, select as o, select as orderBy, pick, preparedStatementName, raw, select as s, selectAs as sa, schema, select, selectAs, sql, toMysql, toPostgres, update, select as w, select as where };
package/dist/index.js CHANGED
@@ -83,6 +83,11 @@ const as = (col) => {
83
83
  const c = col[SYM_COLUMN];
84
84
  return c.prefix + '_' + c.name;
85
85
  };
86
+ const alias$1 = (t, newAlias) => schema({
87
+ table: t[SYM_TABLE].name,
88
+ columns: Object.keys(t),
89
+ alias: newAlias
90
+ });
86
91
 
87
92
  const identifier = (content) => ({ [SYM_IDENTIFIER]: true, content });
88
93
 
@@ -172,6 +177,7 @@ const raw = (content) => ({
172
177
  content
173
178
  });
174
179
 
180
+ exports.alias = alias$1;
175
181
  exports.all = all;
176
182
  exports.as = as;
177
183
  exports.concat = concat;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanit-co/sql-ts",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A TypeScript SQL query builder using tagged template literals. Write plain SQL with safe, automatic parameter binding and properly quoted identifiers. No DSL to learn, no magic, no ORM.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -37,7 +37,9 @@
37
37
  "postgres",
38
38
  "pg",
39
39
  "mysql",
40
- "mysql2"
40
+ "mysql2",
41
+ "sqlite",
42
+ "better-sqlite3"
41
43
  ],
42
44
  "author": "Geovanny Silva",
43
45
  "license": "MIT",