@vanit-co/sql-ts 1.0.2 → 1.0.3

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
@@ -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
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.3",
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",