@vanit-co/sql-ts 0.0.2 → 0.2.0
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 +28 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -105,6 +105,34 @@ 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
|
+
### `as(column)`
|
|
109
|
+
|
|
110
|
+
Returns the aliased column name as a plain string — the same `prefix_name` string that `selectAs` writes into the SQL `AS` clause. Use this to read a column out of query results by its aliased key without repeating the string manually.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { schema, as, selectAs, all } from '@vanit-co/sql-ts'
|
|
114
|
+
|
|
115
|
+
const users = schema({ table: 'users', columns: ['id', 'email'] })
|
|
116
|
+
const u = schema({ table: 'users', columns: ['id', 'email'], alias: 'u' })
|
|
117
|
+
|
|
118
|
+
as(users.id) // 'users_id'
|
|
119
|
+
as(users.email) // 'users_email'
|
|
120
|
+
|
|
121
|
+
as(u.id) // 'u_id'
|
|
122
|
+
as(u.email) // 'u_email'
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
This is particularly useful when mapping over result rows:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const rows = await client.query(selectAs`SELECT ${all(users)} FROM ${users}`)
|
|
129
|
+
|
|
130
|
+
rows.map(row => ({
|
|
131
|
+
id: row[as(users.id)], // row['users_id']
|
|
132
|
+
email: row[as(users.email)], // row['users_email']
|
|
133
|
+
}))
|
|
134
|
+
```
|
|
135
|
+
|
|
108
136
|
---
|
|
109
137
|
|
|
110
138
|
### Result object
|
package/dist/index.d.ts
CHANGED
|
@@ -46,6 +46,7 @@ type Params<T extends string> = {
|
|
|
46
46
|
readonly alias?: string;
|
|
47
47
|
};
|
|
48
48
|
declare const schema: <T extends string>({ table, columns, alias }: Params<T>) => SchemaTable<T>;
|
|
49
|
+
declare const as: (col: { [K in symbol]: Column; }) => string;
|
|
49
50
|
|
|
50
51
|
declare const sql: (strings: TemplateStringsArray, ...binds: Array<any>) => Result;
|
|
51
52
|
declare const select: (strings: TemplateStringsArray, ...binds: Array<any>) => Result;
|
|
@@ -68,4 +69,4 @@ declare const pick: (...columns: Array<{
|
|
|
68
69
|
}>) => Value;
|
|
69
70
|
declare const raw: (content: string) => Value;
|
|
70
71
|
|
|
71
|
-
export { all, concat, empty, insert, join as j, join, pick, preparedStatementName, raw, select as s, selectAs as sa, schema, select, selectAs, sql, toMysql, toPostgres, update, where as w, where };
|
|
72
|
+
export { all, as, concat, empty, insert, join as j, join, pick, preparedStatementName, raw, select as s, selectAs as sa, schema, select, selectAs, sql, toMysql, toPostgres, update, where as w, where };
|
package/dist/index.js
CHANGED
|
@@ -79,6 +79,10 @@ const schema = ({ table, columns, alias }) => {
|
|
|
79
79
|
...makeColumns(resolvedAlias, columns)
|
|
80
80
|
};
|
|
81
81
|
};
|
|
82
|
+
const as = (col) => {
|
|
83
|
+
const c = col[SYM_COLUMN];
|
|
84
|
+
return c.prefix + '_' + c.name;
|
|
85
|
+
};
|
|
82
86
|
|
|
83
87
|
const identifier = (content) => ({ [SYM_IDENTIFIER]: true, content });
|
|
84
88
|
|
|
@@ -150,6 +154,7 @@ const raw = (content) => ({
|
|
|
150
154
|
});
|
|
151
155
|
|
|
152
156
|
exports.all = all;
|
|
157
|
+
exports.as = as;
|
|
153
158
|
exports.concat = concat;
|
|
154
159
|
exports.empty = empty;
|
|
155
160
|
exports.insert = insert;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vanit-co/sql-ts",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
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": "module",
|
|
6
6
|
"main": "./dist/index.js",
|