orchid-orm 1.17.18 → 1.17.20
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/dist/index.d.ts +42 -441
- package/dist/index.js +3 -91
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -91
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -14,7 +14,9 @@ const createBaseTable = ({
|
|
|
14
14
|
schemaProvider: schemaProviderArg
|
|
15
15
|
} = {}) => {
|
|
16
16
|
var _a;
|
|
17
|
-
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(
|
|
17
|
+
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(
|
|
18
|
+
pqb.columnTypes
|
|
19
|
+
) : columnTypesArg || pqb.columnTypes;
|
|
18
20
|
const filePathOrStack = filePathArg || orchidCore.getStackTrace();
|
|
19
21
|
let filePath;
|
|
20
22
|
function schemaProvider() {
|
|
@@ -73,96 +75,6 @@ const createBaseTable = ({
|
|
|
73
75
|
}
|
|
74
76
|
return this.constructor.prototype.columns = shape;
|
|
75
77
|
}
|
|
76
|
-
/**
|
|
77
|
-
* You can add a generated column in the migration (see [generated](/guide/migration-column-methods.html#generated-column)),
|
|
78
|
-
* such column will persist in the database, it can be indexed.
|
|
79
|
-
*
|
|
80
|
-
* Or you can add a computed column on the ORM level, without adding it to the database, in such a way:
|
|
81
|
-
*
|
|
82
|
-
* ```ts
|
|
83
|
-
* import { BaseTable } from './baseTable';
|
|
84
|
-
*
|
|
85
|
-
* export class UserTable extends BaseTable {
|
|
86
|
-
* readonly table = 'user';
|
|
87
|
-
* columns = this.setColumns((t) => ({
|
|
88
|
-
* id: t.identity().primaryKey(),
|
|
89
|
-
* firstName: t.string(),
|
|
90
|
-
* lastName: t.string(),
|
|
91
|
-
* }));
|
|
92
|
-
*
|
|
93
|
-
* computed = this.setComputed({
|
|
94
|
-
* fullName: (q) =>
|
|
95
|
-
* q.sql`${q.column('firstName')} || ' ' || ${q.column('lastName')}`.type(
|
|
96
|
-
* (t) => t.string(),
|
|
97
|
-
* ),
|
|
98
|
-
* });
|
|
99
|
-
* }
|
|
100
|
-
* ```
|
|
101
|
-
*
|
|
102
|
-
* `setComputed` takes an object where keys are computed column names, and values are functions returning raw SQL.
|
|
103
|
-
*
|
|
104
|
-
* Use `q.column` as shown above to reference a table column, it will be prefixed with a correct table name even if the table is joined under a different name.
|
|
105
|
-
*
|
|
106
|
-
* Computed columns are not selected by default, only on demand:
|
|
107
|
-
*
|
|
108
|
-
* ```ts
|
|
109
|
-
* const a = await db.user.take();
|
|
110
|
-
* a.fullName; // not selected
|
|
111
|
-
*
|
|
112
|
-
* const b = await db.user.select('*', 'fullName');
|
|
113
|
-
* b.fullName; // selected
|
|
114
|
-
*
|
|
115
|
-
* // Table post belongs to user as an author.
|
|
116
|
-
* // it's possible to select joined computed column:
|
|
117
|
-
* const posts = await db.post
|
|
118
|
-
* .join('author')
|
|
119
|
-
* .select('post.title', 'author.fullName');
|
|
120
|
-
* ```
|
|
121
|
-
*
|
|
122
|
-
* SQL query can be generated dynamically based on the current request context.
|
|
123
|
-
*
|
|
124
|
-
* Imagine we are using [AsyncLocalStorage](https://nodejs.org/api/async_context.html#asynchronous-context-tracking)
|
|
125
|
-
* to keep track of current user's language.
|
|
126
|
-
*
|
|
127
|
-
* And we have articles translated to different languages, each article has `title_en`, `title_uk`, `title_be` and so on.
|
|
128
|
-
*
|
|
129
|
-
* We can define a computed `title` by passing a function into `sql` method:
|
|
130
|
-
*
|
|
131
|
-
* ```ts
|
|
132
|
-
* type Locale = 'en' | 'uk' | 'be';
|
|
133
|
-
* const asyncLanguageStorage = new AsyncLocalStorage<Locale>();
|
|
134
|
-
* const defaultLocale: Locale = 'en';
|
|
135
|
-
*
|
|
136
|
-
* export class ArticleTable extends BaseTable {
|
|
137
|
-
* readonly table = 'article';
|
|
138
|
-
* columns = this.setColumns((t) => ({
|
|
139
|
-
* id: t.identity().primaryKey(),
|
|
140
|
-
* title_en: t.text(),
|
|
141
|
-
* title_uk: t.text().nullable(),
|
|
142
|
-
* title_be: t.text().nullable(),
|
|
143
|
-
* }));
|
|
144
|
-
*
|
|
145
|
-
* computed = this.setComputed({
|
|
146
|
-
* title: (q) =>
|
|
147
|
-
* q
|
|
148
|
-
* // .sql can take a function that accepts `sql` argument and must return SQL
|
|
149
|
-
* .sql((sql) => {
|
|
150
|
-
* // get locale dynamically based on current storage value
|
|
151
|
-
* const locale = asyncLanguageStorage.getStore() || defaultLocale;
|
|
152
|
-
*
|
|
153
|
-
* // use COALESCE in case when localized title is NULL, use title_en
|
|
154
|
-
* return sql`COALESCE(
|
|
155
|
-
* ${q.column(`title_${locale}`)},
|
|
156
|
-
* ${q.column(`title_${defaultLocale}`)}
|
|
157
|
-
* )`;
|
|
158
|
-
* })
|
|
159
|
-
* .type((t) => t.text()),
|
|
160
|
-
* });
|
|
161
|
-
* }
|
|
162
|
-
* ```
|
|
163
|
-
*
|
|
164
|
-
* @param computed - object where keys are column names and values are functions returning raw SQL
|
|
165
|
-
*/
|
|
166
78
|
setComputed(computed) {
|
|
167
79
|
return computed;
|
|
168
80
|
}
|