peta-orm 0.2.6 → 0.4.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 +186 -210
- package/bin/peta +1 -1
- package/dist/collection-PFmrQHyM.mjs +173 -0
- package/dist/crud-BCWvg5MI.mjs +101 -0
- package/dist/errors-sfFJolfu.mjs +69 -0
- package/dist/factory-BBvIMQuc.mjs +173 -0
- package/dist/hooks-BD0xy7uw.mjs +77 -0
- package/dist/index-BdJnSMYi.d.mts +480 -0
- package/dist/index.d.mts +215 -0
- package/dist/index.mjs +2604 -0
- package/dist/migrations/cli.d.mts +4 -0
- package/dist/migrations/cli.mjs +74 -0
- package/dist/migrations/index.d.mts +53 -0
- package/dist/migrations/index.mjs +2 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/dist/runner-DQ7uT6LC.mjs +180 -0
- package/dist/save-D5UKXvqC.mjs +331 -0
- package/dist/state-LtlHp6XV.mjs +56 -0
- package/package.json +27 -16
- package/dist/builder/delete-builder.d.ts +0 -9
- package/dist/builder/delete-builder.d.ts.map +0 -1
- package/dist/builder/eager-loader.d.ts +0 -13
- package/dist/builder/eager-loader.d.ts.map +0 -1
- package/dist/builder/index.d.ts +0 -6
- package/dist/builder/index.d.ts.map +0 -1
- package/dist/builder/query-builder.d.ts +0 -57
- package/dist/builder/query-builder.d.ts.map +0 -1
- package/dist/builder/update-builder.d.ts +0 -9
- package/dist/builder/update-builder.d.ts.map +0 -1
- package/dist/collection/collection.d.ts +0 -48
- package/dist/collection/collection.d.ts.map +0 -1
- package/dist/collection-wwtv7qmv.js +0 -8
- package/dist/columns/arktype-config.d.ts +0 -8
- package/dist/columns/arktype-config.d.ts.map +0 -1
- package/dist/columns/column-types.d.ts +0 -27
- package/dist/columns/column-types.d.ts.map +0 -1
- package/dist/columns/column.d.ts +0 -30
- package/dist/columns/column.d.ts.map +0 -1
- package/dist/columns/schema-config.d.ts +0 -10
- package/dist/columns/schema-config.d.ts.map +0 -1
- package/dist/errors/errors.d.ts +0 -26
- package/dist/errors/errors.d.ts.map +0 -1
- package/dist/hooks/lifecycle.d.ts +0 -11
- package/dist/hooks/lifecycle.d.ts.map +0 -1
- package/dist/index-4xnrys72.js +0 -56
- package/dist/index-ddxdqxz6.js +0 -636
- package/dist/index-k18nf2r7.js +0 -18
- package/dist/index-qb301480.js +0 -2424
- package/dist/index-sm1xx8gs.js +0 -7828
- package/dist/index.d.ts +0 -24
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -9229
- package/dist/integrations/elysia.d.ts +0 -12
- package/dist/integrations/elysia.d.ts.map +0 -1
- package/dist/integrations/hono.d.ts +0 -7
- package/dist/integrations/hono.d.ts.map +0 -1
- package/dist/migrations/cli.d.ts +0 -2
- package/dist/migrations/cli.d.ts.map +0 -1
- package/dist/migrations/cli.js +0 -4079
- package/dist/migrations/config.d.ts +0 -5
- package/dist/migrations/config.d.ts.map +0 -1
- package/dist/migrations/generator.d.ts +0 -9
- package/dist/migrations/generator.d.ts.map +0 -1
- package/dist/migrations/index.d.ts +0 -5
- package/dist/migrations/index.d.ts.map +0 -1
- package/dist/migrations/index.js +0 -17
- package/dist/migrations/runner.d.ts +0 -12
- package/dist/migrations/runner.d.ts.map +0 -1
- package/dist/migrations/types.d.ts +0 -26
- package/dist/migrations/types.d.ts.map +0 -1
- package/dist/model/model-delete.d.ts +0 -7
- package/dist/model/model-delete.d.ts.map +0 -1
- package/dist/model/model-hooks.d.ts +0 -10
- package/dist/model/model-hooks.d.ts.map +0 -1
- package/dist/model/model-relation.d.ts +0 -7
- package/dist/model/model-relation.d.ts.map +0 -1
- package/dist/model/model-save.d.ts +0 -6
- package/dist/model/model-save.d.ts.map +0 -1
- package/dist/model/model-scope.d.ts +0 -6
- package/dist/model/model-scope.d.ts.map +0 -1
- package/dist/model/model-serialize.d.ts +0 -3
- package/dist/model/model-serialize.d.ts.map +0 -1
- package/dist/model/model-state.d.ts +0 -27
- package/dist/model/model-state.d.ts.map +0 -1
- package/dist/model/model.d.ts +0 -88
- package/dist/model/model.d.ts.map +0 -1
- package/dist/pagination/paginator.d.ts +0 -30
- package/dist/pagination/paginator.d.ts.map +0 -1
- package/dist/paginator-tmp4hxj5.js +0 -9
- package/dist/peta.d.ts +0 -20
- package/dist/peta.d.ts.map +0 -1
- package/dist/relations/morph.d.ts +0 -44
- package/dist/relations/morph.d.ts.map +0 -1
- package/dist/relations/relation.d.ts +0 -85
- package/dist/relations/relation.d.ts.map +0 -1
- package/dist/types.d.ts +0 -18
- package/dist/types.d.ts.map +0 -1
package/dist/index-qb301480.js
DELETED
|
@@ -1,2424 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
import {
|
|
3
|
-
AliasNode,
|
|
4
|
-
CreateTableNode,
|
|
5
|
-
CreateViewNode,
|
|
6
|
-
IdentifierNode,
|
|
7
|
-
InsertQueryNode,
|
|
8
|
-
ManyToMany,
|
|
9
|
-
MergeQueryNode,
|
|
10
|
-
NOOP_QUERY_EXECUTOR,
|
|
11
|
-
OperatorNode,
|
|
12
|
-
ParensNode,
|
|
13
|
-
Peta,
|
|
14
|
-
QueryNode,
|
|
15
|
-
RawNode,
|
|
16
|
-
SetOperationNode,
|
|
17
|
-
ValueNode,
|
|
18
|
-
WithSchemaPlugin,
|
|
19
|
-
createQueryId,
|
|
20
|
-
freeze,
|
|
21
|
-
getLast,
|
|
22
|
-
isBigInt,
|
|
23
|
-
isBoolean,
|
|
24
|
-
isDate,
|
|
25
|
-
isModelClass,
|
|
26
|
-
isNull,
|
|
27
|
-
isNumber,
|
|
28
|
-
isOperationNodeSource,
|
|
29
|
-
isString,
|
|
30
|
-
parseStringReference,
|
|
31
|
-
parseTable,
|
|
32
|
-
parseValueExpression,
|
|
33
|
-
preventAwait
|
|
34
|
-
} from "./index-sm1xx8gs.js";
|
|
35
|
-
import {
|
|
36
|
-
__require
|
|
37
|
-
} from "./index-k18nf2r7.js";
|
|
38
|
-
|
|
39
|
-
// node_modules/kysely/dist/esm/raw-builder/raw-builder.js
|
|
40
|
-
class RawBuilderImpl {
|
|
41
|
-
#props;
|
|
42
|
-
constructor(props) {
|
|
43
|
-
this.#props = freeze(props);
|
|
44
|
-
}
|
|
45
|
-
get expressionType() {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
get isRawBuilder() {
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
as(alias) {
|
|
52
|
-
return new AliasedRawBuilderImpl(this, alias);
|
|
53
|
-
}
|
|
54
|
-
$castTo() {
|
|
55
|
-
return new RawBuilderImpl({ ...this.#props });
|
|
56
|
-
}
|
|
57
|
-
$notNull() {
|
|
58
|
-
return new RawBuilderImpl(this.#props);
|
|
59
|
-
}
|
|
60
|
-
withPlugin(plugin) {
|
|
61
|
-
return new RawBuilderImpl({
|
|
62
|
-
...this.#props,
|
|
63
|
-
plugins: this.#props.plugins !== undefined ? freeze([...this.#props.plugins, plugin]) : freeze([plugin])
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
toOperationNode() {
|
|
67
|
-
return this.#toOperationNode(this.#getExecutor());
|
|
68
|
-
}
|
|
69
|
-
compile(executorProvider) {
|
|
70
|
-
return this.#compile(this.#getExecutor(executorProvider));
|
|
71
|
-
}
|
|
72
|
-
async execute(executorProvider) {
|
|
73
|
-
const executor = this.#getExecutor(executorProvider);
|
|
74
|
-
return executor.executeQuery(this.#compile(executor), this.#props.queryId);
|
|
75
|
-
}
|
|
76
|
-
#getExecutor(executorProvider) {
|
|
77
|
-
const executor = executorProvider !== undefined ? executorProvider.getExecutor() : NOOP_QUERY_EXECUTOR;
|
|
78
|
-
return this.#props.plugins !== undefined ? executor.withPlugins(this.#props.plugins) : executor;
|
|
79
|
-
}
|
|
80
|
-
#toOperationNode(executor) {
|
|
81
|
-
return executor.transformQuery(this.#props.rawNode, this.#props.queryId);
|
|
82
|
-
}
|
|
83
|
-
#compile(executor) {
|
|
84
|
-
return executor.compileQuery(this.#toOperationNode(executor), this.#props.queryId);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function createRawBuilder(props) {
|
|
88
|
-
return new RawBuilderImpl(props);
|
|
89
|
-
}
|
|
90
|
-
preventAwait(RawBuilderImpl, "don't await RawBuilder instances directly. To execute the query you need to call `execute`");
|
|
91
|
-
|
|
92
|
-
class AliasedRawBuilderImpl {
|
|
93
|
-
#rawBuilder;
|
|
94
|
-
#alias;
|
|
95
|
-
constructor(rawBuilder, alias) {
|
|
96
|
-
this.#rawBuilder = rawBuilder;
|
|
97
|
-
this.#alias = alias;
|
|
98
|
-
}
|
|
99
|
-
get expression() {
|
|
100
|
-
return this.#rawBuilder;
|
|
101
|
-
}
|
|
102
|
-
get alias() {
|
|
103
|
-
return this.#alias;
|
|
104
|
-
}
|
|
105
|
-
get rawBuilder() {
|
|
106
|
-
return this.#rawBuilder;
|
|
107
|
-
}
|
|
108
|
-
toOperationNode() {
|
|
109
|
-
return AliasNode.create(this.#rawBuilder.toOperationNode(), isOperationNodeSource(this.#alias) ? this.#alias.toOperationNode() : IdentifierNode.create(this.#alias));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
preventAwait(AliasedRawBuilderImpl, "don't await AliasedRawBuilder instances directly. AliasedRawBuilder should never be executed directly since it's always a part of another query.");
|
|
113
|
-
|
|
114
|
-
// node_modules/kysely/dist/esm/raw-builder/sql.js
|
|
115
|
-
var sql = Object.assign((sqlFragments, ...parameters) => {
|
|
116
|
-
return createRawBuilder({
|
|
117
|
-
queryId: createQueryId(),
|
|
118
|
-
rawNode: RawNode.create(sqlFragments, parameters?.map(parseParameter) ?? [])
|
|
119
|
-
});
|
|
120
|
-
}, {
|
|
121
|
-
ref(columnReference) {
|
|
122
|
-
return createRawBuilder({
|
|
123
|
-
queryId: createQueryId(),
|
|
124
|
-
rawNode: RawNode.createWithChild(parseStringReference(columnReference))
|
|
125
|
-
});
|
|
126
|
-
},
|
|
127
|
-
val(value) {
|
|
128
|
-
return createRawBuilder({
|
|
129
|
-
queryId: createQueryId(),
|
|
130
|
-
rawNode: RawNode.createWithChild(parseValueExpression(value))
|
|
131
|
-
});
|
|
132
|
-
},
|
|
133
|
-
value(value) {
|
|
134
|
-
return this.val(value);
|
|
135
|
-
},
|
|
136
|
-
table(tableReference) {
|
|
137
|
-
return createRawBuilder({
|
|
138
|
-
queryId: createQueryId(),
|
|
139
|
-
rawNode: RawNode.createWithChild(parseTable(tableReference))
|
|
140
|
-
});
|
|
141
|
-
},
|
|
142
|
-
id(...ids) {
|
|
143
|
-
const fragments = new Array(ids.length + 1).fill(".");
|
|
144
|
-
fragments[0] = "";
|
|
145
|
-
fragments[fragments.length - 1] = "";
|
|
146
|
-
return createRawBuilder({
|
|
147
|
-
queryId: createQueryId(),
|
|
148
|
-
rawNode: RawNode.create(fragments, ids.map(IdentifierNode.create))
|
|
149
|
-
});
|
|
150
|
-
},
|
|
151
|
-
lit(value) {
|
|
152
|
-
return createRawBuilder({
|
|
153
|
-
queryId: createQueryId(),
|
|
154
|
-
rawNode: RawNode.createWithChild(ValueNode.createImmediate(value))
|
|
155
|
-
});
|
|
156
|
-
},
|
|
157
|
-
literal(value) {
|
|
158
|
-
return this.lit(value);
|
|
159
|
-
},
|
|
160
|
-
raw(sql2) {
|
|
161
|
-
return createRawBuilder({
|
|
162
|
-
queryId: createQueryId(),
|
|
163
|
-
rawNode: RawNode.createWithSql(sql2)
|
|
164
|
-
});
|
|
165
|
-
},
|
|
166
|
-
join(array, separator = sql`, `) {
|
|
167
|
-
const nodes = new Array(2 * array.length - 1);
|
|
168
|
-
const sep = separator.toOperationNode();
|
|
169
|
-
for (let i = 0;i < array.length; ++i) {
|
|
170
|
-
nodes[2 * i] = parseParameter(array[i]);
|
|
171
|
-
if (i !== array.length - 1) {
|
|
172
|
-
nodes[2 * i + 1] = sep;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return createRawBuilder({
|
|
176
|
-
queryId: createQueryId(),
|
|
177
|
-
rawNode: RawNode.createWithChildren(nodes)
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
function parseParameter(param) {
|
|
182
|
-
if (isOperationNodeSource(param)) {
|
|
183
|
-
return param.toOperationNode();
|
|
184
|
-
}
|
|
185
|
-
return parseValueExpression(param);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// node_modules/kysely/dist/esm/operation-node/operation-node-visitor.js
|
|
189
|
-
class OperationNodeVisitor {
|
|
190
|
-
nodeStack = [];
|
|
191
|
-
get parentNode() {
|
|
192
|
-
return this.nodeStack[this.nodeStack.length - 2];
|
|
193
|
-
}
|
|
194
|
-
#visitors = freeze({
|
|
195
|
-
AliasNode: this.visitAlias.bind(this),
|
|
196
|
-
ColumnNode: this.visitColumn.bind(this),
|
|
197
|
-
IdentifierNode: this.visitIdentifier.bind(this),
|
|
198
|
-
SchemableIdentifierNode: this.visitSchemableIdentifier.bind(this),
|
|
199
|
-
RawNode: this.visitRaw.bind(this),
|
|
200
|
-
ReferenceNode: this.visitReference.bind(this),
|
|
201
|
-
SelectQueryNode: this.visitSelectQuery.bind(this),
|
|
202
|
-
SelectionNode: this.visitSelection.bind(this),
|
|
203
|
-
TableNode: this.visitTable.bind(this),
|
|
204
|
-
FromNode: this.visitFrom.bind(this),
|
|
205
|
-
SelectAllNode: this.visitSelectAll.bind(this),
|
|
206
|
-
AndNode: this.visitAnd.bind(this),
|
|
207
|
-
OrNode: this.visitOr.bind(this),
|
|
208
|
-
ValueNode: this.visitValue.bind(this),
|
|
209
|
-
ValueListNode: this.visitValueList.bind(this),
|
|
210
|
-
PrimitiveValueListNode: this.visitPrimitiveValueList.bind(this),
|
|
211
|
-
ParensNode: this.visitParens.bind(this),
|
|
212
|
-
JoinNode: this.visitJoin.bind(this),
|
|
213
|
-
OperatorNode: this.visitOperator.bind(this),
|
|
214
|
-
WhereNode: this.visitWhere.bind(this),
|
|
215
|
-
InsertQueryNode: this.visitInsertQuery.bind(this),
|
|
216
|
-
DeleteQueryNode: this.visitDeleteQuery.bind(this),
|
|
217
|
-
ReturningNode: this.visitReturning.bind(this),
|
|
218
|
-
CreateTableNode: this.visitCreateTable.bind(this),
|
|
219
|
-
AddColumnNode: this.visitAddColumn.bind(this),
|
|
220
|
-
ColumnDefinitionNode: this.visitColumnDefinition.bind(this),
|
|
221
|
-
DropTableNode: this.visitDropTable.bind(this),
|
|
222
|
-
DataTypeNode: this.visitDataType.bind(this),
|
|
223
|
-
OrderByNode: this.visitOrderBy.bind(this),
|
|
224
|
-
OrderByItemNode: this.visitOrderByItem.bind(this),
|
|
225
|
-
GroupByNode: this.visitGroupBy.bind(this),
|
|
226
|
-
GroupByItemNode: this.visitGroupByItem.bind(this),
|
|
227
|
-
UpdateQueryNode: this.visitUpdateQuery.bind(this),
|
|
228
|
-
ColumnUpdateNode: this.visitColumnUpdate.bind(this),
|
|
229
|
-
LimitNode: this.visitLimit.bind(this),
|
|
230
|
-
OffsetNode: this.visitOffset.bind(this),
|
|
231
|
-
OnConflictNode: this.visitOnConflict.bind(this),
|
|
232
|
-
OnDuplicateKeyNode: this.visitOnDuplicateKey.bind(this),
|
|
233
|
-
CreateIndexNode: this.visitCreateIndex.bind(this),
|
|
234
|
-
DropIndexNode: this.visitDropIndex.bind(this),
|
|
235
|
-
ListNode: this.visitList.bind(this),
|
|
236
|
-
PrimaryKeyConstraintNode: this.visitPrimaryKeyConstraint.bind(this),
|
|
237
|
-
UniqueConstraintNode: this.visitUniqueConstraint.bind(this),
|
|
238
|
-
ReferencesNode: this.visitReferences.bind(this),
|
|
239
|
-
CheckConstraintNode: this.visitCheckConstraint.bind(this),
|
|
240
|
-
WithNode: this.visitWith.bind(this),
|
|
241
|
-
CommonTableExpressionNode: this.visitCommonTableExpression.bind(this),
|
|
242
|
-
CommonTableExpressionNameNode: this.visitCommonTableExpressionName.bind(this),
|
|
243
|
-
HavingNode: this.visitHaving.bind(this),
|
|
244
|
-
CreateSchemaNode: this.visitCreateSchema.bind(this),
|
|
245
|
-
DropSchemaNode: this.visitDropSchema.bind(this),
|
|
246
|
-
AlterTableNode: this.visitAlterTable.bind(this),
|
|
247
|
-
DropColumnNode: this.visitDropColumn.bind(this),
|
|
248
|
-
RenameColumnNode: this.visitRenameColumn.bind(this),
|
|
249
|
-
AlterColumnNode: this.visitAlterColumn.bind(this),
|
|
250
|
-
ModifyColumnNode: this.visitModifyColumn.bind(this),
|
|
251
|
-
AddConstraintNode: this.visitAddConstraint.bind(this),
|
|
252
|
-
DropConstraintNode: this.visitDropConstraint.bind(this),
|
|
253
|
-
ForeignKeyConstraintNode: this.visitForeignKeyConstraint.bind(this),
|
|
254
|
-
CreateViewNode: this.visitCreateView.bind(this),
|
|
255
|
-
DropViewNode: this.visitDropView.bind(this),
|
|
256
|
-
GeneratedNode: this.visitGenerated.bind(this),
|
|
257
|
-
DefaultValueNode: this.visitDefaultValue.bind(this),
|
|
258
|
-
OnNode: this.visitOn.bind(this),
|
|
259
|
-
ValuesNode: this.visitValues.bind(this),
|
|
260
|
-
SelectModifierNode: this.visitSelectModifier.bind(this),
|
|
261
|
-
CreateTypeNode: this.visitCreateType.bind(this),
|
|
262
|
-
DropTypeNode: this.visitDropType.bind(this),
|
|
263
|
-
ExplainNode: this.visitExplain.bind(this),
|
|
264
|
-
DefaultInsertValueNode: this.visitDefaultInsertValue.bind(this),
|
|
265
|
-
AggregateFunctionNode: this.visitAggregateFunction.bind(this),
|
|
266
|
-
OverNode: this.visitOver.bind(this),
|
|
267
|
-
PartitionByNode: this.visitPartitionBy.bind(this),
|
|
268
|
-
PartitionByItemNode: this.visitPartitionByItem.bind(this),
|
|
269
|
-
SetOperationNode: this.visitSetOperation.bind(this),
|
|
270
|
-
BinaryOperationNode: this.visitBinaryOperation.bind(this),
|
|
271
|
-
UnaryOperationNode: this.visitUnaryOperation.bind(this),
|
|
272
|
-
UsingNode: this.visitUsing.bind(this),
|
|
273
|
-
FunctionNode: this.visitFunction.bind(this),
|
|
274
|
-
CaseNode: this.visitCase.bind(this),
|
|
275
|
-
WhenNode: this.visitWhen.bind(this),
|
|
276
|
-
JSONReferenceNode: this.visitJSONReference.bind(this),
|
|
277
|
-
JSONPathNode: this.visitJSONPath.bind(this),
|
|
278
|
-
JSONPathLegNode: this.visitJSONPathLeg.bind(this),
|
|
279
|
-
JSONOperatorChainNode: this.visitJSONOperatorChain.bind(this),
|
|
280
|
-
TupleNode: this.visitTuple.bind(this),
|
|
281
|
-
MergeQueryNode: this.visitMergeQuery.bind(this),
|
|
282
|
-
MatchedNode: this.visitMatched.bind(this),
|
|
283
|
-
AddIndexNode: this.visitAddIndex.bind(this),
|
|
284
|
-
CastNode: this.visitCast.bind(this),
|
|
285
|
-
FetchNode: this.visitFetch.bind(this),
|
|
286
|
-
TopNode: this.visitTop.bind(this),
|
|
287
|
-
OutputNode: this.visitOutput.bind(this)
|
|
288
|
-
});
|
|
289
|
-
visitNode = (node) => {
|
|
290
|
-
this.nodeStack.push(node);
|
|
291
|
-
this.#visitors[node.kind](node);
|
|
292
|
-
this.nodeStack.pop();
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// node_modules/kysely/dist/esm/query-compiler/default-query-compiler.js
|
|
297
|
-
class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
298
|
-
#sql = "";
|
|
299
|
-
#parameters = [];
|
|
300
|
-
get numParameters() {
|
|
301
|
-
return this.#parameters.length;
|
|
302
|
-
}
|
|
303
|
-
compileQuery(node) {
|
|
304
|
-
this.#sql = "";
|
|
305
|
-
this.#parameters = [];
|
|
306
|
-
this.nodeStack.splice(0, this.nodeStack.length);
|
|
307
|
-
this.visitNode(node);
|
|
308
|
-
return freeze({
|
|
309
|
-
query: node,
|
|
310
|
-
sql: this.getSql(),
|
|
311
|
-
parameters: [...this.#parameters]
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
getSql() {
|
|
315
|
-
return this.#sql;
|
|
316
|
-
}
|
|
317
|
-
visitSelectQuery(node) {
|
|
318
|
-
const wrapInParens = this.parentNode !== undefined && !ParensNode.is(this.parentNode) && !InsertQueryNode.is(this.parentNode) && !CreateTableNode.is(this.parentNode) && !CreateViewNode.is(this.parentNode) && !SetOperationNode.is(this.parentNode);
|
|
319
|
-
if (this.parentNode === undefined && node.explain) {
|
|
320
|
-
this.visitNode(node.explain);
|
|
321
|
-
this.append(" ");
|
|
322
|
-
}
|
|
323
|
-
if (wrapInParens) {
|
|
324
|
-
this.append("(");
|
|
325
|
-
}
|
|
326
|
-
if (node.with) {
|
|
327
|
-
this.visitNode(node.with);
|
|
328
|
-
this.append(" ");
|
|
329
|
-
}
|
|
330
|
-
this.append("select");
|
|
331
|
-
if (node.distinctOn) {
|
|
332
|
-
this.append(" ");
|
|
333
|
-
this.compileDistinctOn(node.distinctOn);
|
|
334
|
-
}
|
|
335
|
-
if (node.frontModifiers?.length) {
|
|
336
|
-
this.append(" ");
|
|
337
|
-
this.compileList(node.frontModifiers, " ");
|
|
338
|
-
}
|
|
339
|
-
if (node.top) {
|
|
340
|
-
this.append(" ");
|
|
341
|
-
this.visitNode(node.top);
|
|
342
|
-
}
|
|
343
|
-
if (node.selections) {
|
|
344
|
-
this.append(" ");
|
|
345
|
-
this.compileList(node.selections);
|
|
346
|
-
}
|
|
347
|
-
if (node.from) {
|
|
348
|
-
this.append(" ");
|
|
349
|
-
this.visitNode(node.from);
|
|
350
|
-
}
|
|
351
|
-
if (node.joins) {
|
|
352
|
-
this.append(" ");
|
|
353
|
-
this.compileList(node.joins, " ");
|
|
354
|
-
}
|
|
355
|
-
if (node.where) {
|
|
356
|
-
this.append(" ");
|
|
357
|
-
this.visitNode(node.where);
|
|
358
|
-
}
|
|
359
|
-
if (node.groupBy) {
|
|
360
|
-
this.append(" ");
|
|
361
|
-
this.visitNode(node.groupBy);
|
|
362
|
-
}
|
|
363
|
-
if (node.having) {
|
|
364
|
-
this.append(" ");
|
|
365
|
-
this.visitNode(node.having);
|
|
366
|
-
}
|
|
367
|
-
if (node.setOperations) {
|
|
368
|
-
this.append(" ");
|
|
369
|
-
this.compileList(node.setOperations, " ");
|
|
370
|
-
}
|
|
371
|
-
if (node.orderBy) {
|
|
372
|
-
this.append(" ");
|
|
373
|
-
this.visitNode(node.orderBy);
|
|
374
|
-
}
|
|
375
|
-
if (node.limit) {
|
|
376
|
-
this.append(" ");
|
|
377
|
-
this.visitNode(node.limit);
|
|
378
|
-
}
|
|
379
|
-
if (node.offset) {
|
|
380
|
-
this.append(" ");
|
|
381
|
-
this.visitNode(node.offset);
|
|
382
|
-
}
|
|
383
|
-
if (node.fetch) {
|
|
384
|
-
this.append(" ");
|
|
385
|
-
this.visitNode(node.fetch);
|
|
386
|
-
}
|
|
387
|
-
if (node.endModifiers?.length) {
|
|
388
|
-
this.append(" ");
|
|
389
|
-
this.compileList(this.sortSelectModifiers([...node.endModifiers]), " ");
|
|
390
|
-
}
|
|
391
|
-
if (wrapInParens) {
|
|
392
|
-
this.append(")");
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
visitFrom(node) {
|
|
396
|
-
this.append("from ");
|
|
397
|
-
this.compileList(node.froms);
|
|
398
|
-
}
|
|
399
|
-
visitSelection(node) {
|
|
400
|
-
this.visitNode(node.selection);
|
|
401
|
-
}
|
|
402
|
-
visitColumn(node) {
|
|
403
|
-
this.visitNode(node.column);
|
|
404
|
-
}
|
|
405
|
-
compileDistinctOn(expressions) {
|
|
406
|
-
this.append("distinct on (");
|
|
407
|
-
this.compileList(expressions);
|
|
408
|
-
this.append(")");
|
|
409
|
-
}
|
|
410
|
-
compileList(nodes, separator = ", ") {
|
|
411
|
-
const lastIndex = nodes.length - 1;
|
|
412
|
-
for (let i = 0;i <= lastIndex; i++) {
|
|
413
|
-
this.visitNode(nodes[i]);
|
|
414
|
-
if (i < lastIndex) {
|
|
415
|
-
this.append(separator);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
visitWhere(node) {
|
|
420
|
-
this.append("where ");
|
|
421
|
-
this.visitNode(node.where);
|
|
422
|
-
}
|
|
423
|
-
visitHaving(node) {
|
|
424
|
-
this.append("having ");
|
|
425
|
-
this.visitNode(node.having);
|
|
426
|
-
}
|
|
427
|
-
visitInsertQuery(node) {
|
|
428
|
-
const rootQueryNode = this.nodeStack.find(QueryNode.is);
|
|
429
|
-
const isSubQuery = rootQueryNode !== node;
|
|
430
|
-
if (!isSubQuery && node.explain) {
|
|
431
|
-
this.visitNode(node.explain);
|
|
432
|
-
this.append(" ");
|
|
433
|
-
}
|
|
434
|
-
if (isSubQuery && !MergeQueryNode.is(rootQueryNode)) {
|
|
435
|
-
this.append("(");
|
|
436
|
-
}
|
|
437
|
-
if (node.with) {
|
|
438
|
-
this.visitNode(node.with);
|
|
439
|
-
this.append(" ");
|
|
440
|
-
}
|
|
441
|
-
this.append(node.replace ? "replace" : "insert");
|
|
442
|
-
if (node.ignore) {
|
|
443
|
-
this.append(" ignore");
|
|
444
|
-
}
|
|
445
|
-
if (node.top) {
|
|
446
|
-
this.append(" ");
|
|
447
|
-
this.visitNode(node.top);
|
|
448
|
-
}
|
|
449
|
-
if (node.into) {
|
|
450
|
-
this.append(" into ");
|
|
451
|
-
this.visitNode(node.into);
|
|
452
|
-
}
|
|
453
|
-
if (node.columns) {
|
|
454
|
-
this.append(" (");
|
|
455
|
-
this.compileList(node.columns);
|
|
456
|
-
this.append(")");
|
|
457
|
-
}
|
|
458
|
-
if (node.output) {
|
|
459
|
-
this.append(" ");
|
|
460
|
-
this.visitNode(node.output);
|
|
461
|
-
}
|
|
462
|
-
if (node.values) {
|
|
463
|
-
this.append(" ");
|
|
464
|
-
this.visitNode(node.values);
|
|
465
|
-
}
|
|
466
|
-
if (node.defaultValues) {
|
|
467
|
-
this.append(" ");
|
|
468
|
-
this.append("default values");
|
|
469
|
-
}
|
|
470
|
-
if (node.onConflict) {
|
|
471
|
-
this.append(" ");
|
|
472
|
-
this.visitNode(node.onConflict);
|
|
473
|
-
}
|
|
474
|
-
if (node.onDuplicateKey) {
|
|
475
|
-
this.append(" ");
|
|
476
|
-
this.visitNode(node.onDuplicateKey);
|
|
477
|
-
}
|
|
478
|
-
if (node.returning) {
|
|
479
|
-
this.append(" ");
|
|
480
|
-
this.visitNode(node.returning);
|
|
481
|
-
}
|
|
482
|
-
if (isSubQuery && !MergeQueryNode.is(rootQueryNode)) {
|
|
483
|
-
this.append(")");
|
|
484
|
-
}
|
|
485
|
-
if (node.endModifiers?.length) {
|
|
486
|
-
this.append(" ");
|
|
487
|
-
this.compileList(node.endModifiers, " ");
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
visitValues(node) {
|
|
491
|
-
this.append("values ");
|
|
492
|
-
this.compileList(node.values);
|
|
493
|
-
}
|
|
494
|
-
visitDeleteQuery(node) {
|
|
495
|
-
const isSubQuery = this.nodeStack.find(QueryNode.is) !== node;
|
|
496
|
-
if (!isSubQuery && node.explain) {
|
|
497
|
-
this.visitNode(node.explain);
|
|
498
|
-
this.append(" ");
|
|
499
|
-
}
|
|
500
|
-
if (isSubQuery) {
|
|
501
|
-
this.append("(");
|
|
502
|
-
}
|
|
503
|
-
if (node.with) {
|
|
504
|
-
this.visitNode(node.with);
|
|
505
|
-
this.append(" ");
|
|
506
|
-
}
|
|
507
|
-
this.append("delete ");
|
|
508
|
-
if (node.top) {
|
|
509
|
-
this.visitNode(node.top);
|
|
510
|
-
this.append(" ");
|
|
511
|
-
}
|
|
512
|
-
this.visitNode(node.from);
|
|
513
|
-
if (node.output) {
|
|
514
|
-
this.append(" ");
|
|
515
|
-
this.visitNode(node.output);
|
|
516
|
-
}
|
|
517
|
-
if (node.using) {
|
|
518
|
-
this.append(" ");
|
|
519
|
-
this.visitNode(node.using);
|
|
520
|
-
}
|
|
521
|
-
if (node.joins) {
|
|
522
|
-
this.append(" ");
|
|
523
|
-
this.compileList(node.joins, " ");
|
|
524
|
-
}
|
|
525
|
-
if (node.where) {
|
|
526
|
-
this.append(" ");
|
|
527
|
-
this.visitNode(node.where);
|
|
528
|
-
}
|
|
529
|
-
if (node.orderBy) {
|
|
530
|
-
this.append(" ");
|
|
531
|
-
this.visitNode(node.orderBy);
|
|
532
|
-
}
|
|
533
|
-
if (node.limit) {
|
|
534
|
-
this.append(" ");
|
|
535
|
-
this.visitNode(node.limit);
|
|
536
|
-
}
|
|
537
|
-
if (node.returning) {
|
|
538
|
-
this.append(" ");
|
|
539
|
-
this.visitNode(node.returning);
|
|
540
|
-
}
|
|
541
|
-
if (isSubQuery) {
|
|
542
|
-
this.append(")");
|
|
543
|
-
}
|
|
544
|
-
if (node.endModifiers?.length) {
|
|
545
|
-
this.append(" ");
|
|
546
|
-
this.compileList(node.endModifiers, " ");
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
visitReturning(node) {
|
|
550
|
-
this.append("returning ");
|
|
551
|
-
this.compileList(node.selections);
|
|
552
|
-
}
|
|
553
|
-
visitAlias(node) {
|
|
554
|
-
this.visitNode(node.node);
|
|
555
|
-
this.append(" as ");
|
|
556
|
-
this.visitNode(node.alias);
|
|
557
|
-
}
|
|
558
|
-
visitReference(node) {
|
|
559
|
-
if (node.table) {
|
|
560
|
-
this.visitNode(node.table);
|
|
561
|
-
this.append(".");
|
|
562
|
-
}
|
|
563
|
-
this.visitNode(node.column);
|
|
564
|
-
}
|
|
565
|
-
visitSelectAll(_) {
|
|
566
|
-
this.append("*");
|
|
567
|
-
}
|
|
568
|
-
visitIdentifier(node) {
|
|
569
|
-
this.append(this.getLeftIdentifierWrapper());
|
|
570
|
-
this.compileUnwrappedIdentifier(node);
|
|
571
|
-
this.append(this.getRightIdentifierWrapper());
|
|
572
|
-
}
|
|
573
|
-
compileUnwrappedIdentifier(node) {
|
|
574
|
-
if (!isString(node.name)) {
|
|
575
|
-
throw new Error("a non-string identifier was passed to compileUnwrappedIdentifier.");
|
|
576
|
-
}
|
|
577
|
-
this.append(this.sanitizeIdentifier(node.name));
|
|
578
|
-
}
|
|
579
|
-
visitAnd(node) {
|
|
580
|
-
this.visitNode(node.left);
|
|
581
|
-
this.append(" and ");
|
|
582
|
-
this.visitNode(node.right);
|
|
583
|
-
}
|
|
584
|
-
visitOr(node) {
|
|
585
|
-
this.visitNode(node.left);
|
|
586
|
-
this.append(" or ");
|
|
587
|
-
this.visitNode(node.right);
|
|
588
|
-
}
|
|
589
|
-
visitValue(node) {
|
|
590
|
-
if (node.immediate) {
|
|
591
|
-
this.appendImmediateValue(node.value);
|
|
592
|
-
} else {
|
|
593
|
-
this.appendValue(node.value);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
visitValueList(node) {
|
|
597
|
-
this.append("(");
|
|
598
|
-
this.compileList(node.values);
|
|
599
|
-
this.append(")");
|
|
600
|
-
}
|
|
601
|
-
visitTuple(node) {
|
|
602
|
-
this.append("(");
|
|
603
|
-
this.compileList(node.values);
|
|
604
|
-
this.append(")");
|
|
605
|
-
}
|
|
606
|
-
visitPrimitiveValueList(node) {
|
|
607
|
-
this.append("(");
|
|
608
|
-
const { values } = node;
|
|
609
|
-
for (let i = 0;i < values.length; ++i) {
|
|
610
|
-
this.appendValue(values[i]);
|
|
611
|
-
if (i !== values.length - 1) {
|
|
612
|
-
this.append(", ");
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
this.append(")");
|
|
616
|
-
}
|
|
617
|
-
visitParens(node) {
|
|
618
|
-
this.append("(");
|
|
619
|
-
this.visitNode(node.node);
|
|
620
|
-
this.append(")");
|
|
621
|
-
}
|
|
622
|
-
visitJoin(node) {
|
|
623
|
-
this.append(JOIN_TYPE_SQL[node.joinType]);
|
|
624
|
-
this.append(" ");
|
|
625
|
-
this.visitNode(node.table);
|
|
626
|
-
if (node.on) {
|
|
627
|
-
this.append(" ");
|
|
628
|
-
this.visitNode(node.on);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
visitOn(node) {
|
|
632
|
-
this.append("on ");
|
|
633
|
-
this.visitNode(node.on);
|
|
634
|
-
}
|
|
635
|
-
visitRaw(node) {
|
|
636
|
-
const { sqlFragments, parameters: params } = node;
|
|
637
|
-
for (let i = 0;i < sqlFragments.length; ++i) {
|
|
638
|
-
this.append(sqlFragments[i]);
|
|
639
|
-
if (params.length > i) {
|
|
640
|
-
this.visitNode(params[i]);
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
visitOperator(node) {
|
|
645
|
-
this.append(node.operator);
|
|
646
|
-
}
|
|
647
|
-
visitTable(node) {
|
|
648
|
-
this.visitNode(node.table);
|
|
649
|
-
}
|
|
650
|
-
visitSchemableIdentifier(node) {
|
|
651
|
-
if (node.schema) {
|
|
652
|
-
this.visitNode(node.schema);
|
|
653
|
-
this.append(".");
|
|
654
|
-
}
|
|
655
|
-
this.visitNode(node.identifier);
|
|
656
|
-
}
|
|
657
|
-
visitCreateTable(node) {
|
|
658
|
-
this.append("create ");
|
|
659
|
-
if (node.frontModifiers && node.frontModifiers.length > 0) {
|
|
660
|
-
this.compileList(node.frontModifiers, " ");
|
|
661
|
-
this.append(" ");
|
|
662
|
-
}
|
|
663
|
-
if (node.temporary) {
|
|
664
|
-
this.append("temporary ");
|
|
665
|
-
}
|
|
666
|
-
this.append("table ");
|
|
667
|
-
if (node.ifNotExists) {
|
|
668
|
-
this.append("if not exists ");
|
|
669
|
-
}
|
|
670
|
-
this.visitNode(node.table);
|
|
671
|
-
if (node.selectQuery) {
|
|
672
|
-
this.append(" as ");
|
|
673
|
-
this.visitNode(node.selectQuery);
|
|
674
|
-
} else {
|
|
675
|
-
this.append(" (");
|
|
676
|
-
this.compileList([...node.columns, ...node.constraints ?? []]);
|
|
677
|
-
this.append(")");
|
|
678
|
-
if (node.onCommit) {
|
|
679
|
-
this.append(" on commit ");
|
|
680
|
-
this.append(node.onCommit);
|
|
681
|
-
}
|
|
682
|
-
if (node.endModifiers && node.endModifiers.length > 0) {
|
|
683
|
-
this.append(" ");
|
|
684
|
-
this.compileList(node.endModifiers, " ");
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
visitColumnDefinition(node) {
|
|
689
|
-
if (node.ifNotExists) {
|
|
690
|
-
this.append("if not exists ");
|
|
691
|
-
}
|
|
692
|
-
this.visitNode(node.column);
|
|
693
|
-
this.append(" ");
|
|
694
|
-
this.visitNode(node.dataType);
|
|
695
|
-
if (node.unsigned) {
|
|
696
|
-
this.append(" unsigned");
|
|
697
|
-
}
|
|
698
|
-
if (node.frontModifiers && node.frontModifiers.length > 0) {
|
|
699
|
-
this.append(" ");
|
|
700
|
-
this.compileList(node.frontModifiers, " ");
|
|
701
|
-
}
|
|
702
|
-
if (node.generated) {
|
|
703
|
-
this.append(" ");
|
|
704
|
-
this.visitNode(node.generated);
|
|
705
|
-
}
|
|
706
|
-
if (node.identity) {
|
|
707
|
-
this.append(" identity");
|
|
708
|
-
}
|
|
709
|
-
if (node.defaultTo) {
|
|
710
|
-
this.append(" ");
|
|
711
|
-
this.visitNode(node.defaultTo);
|
|
712
|
-
}
|
|
713
|
-
if (node.notNull) {
|
|
714
|
-
this.append(" not null");
|
|
715
|
-
}
|
|
716
|
-
if (node.unique) {
|
|
717
|
-
this.append(" unique");
|
|
718
|
-
}
|
|
719
|
-
if (node.nullsNotDistinct) {
|
|
720
|
-
this.append(" nulls not distinct");
|
|
721
|
-
}
|
|
722
|
-
if (node.primaryKey) {
|
|
723
|
-
this.append(" primary key");
|
|
724
|
-
}
|
|
725
|
-
if (node.autoIncrement) {
|
|
726
|
-
this.append(" ");
|
|
727
|
-
this.append(this.getAutoIncrement());
|
|
728
|
-
}
|
|
729
|
-
if (node.references) {
|
|
730
|
-
this.append(" ");
|
|
731
|
-
this.visitNode(node.references);
|
|
732
|
-
}
|
|
733
|
-
if (node.check) {
|
|
734
|
-
this.append(" ");
|
|
735
|
-
this.visitNode(node.check);
|
|
736
|
-
}
|
|
737
|
-
if (node.endModifiers && node.endModifiers.length > 0) {
|
|
738
|
-
this.append(" ");
|
|
739
|
-
this.compileList(node.endModifiers, " ");
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
getAutoIncrement() {
|
|
743
|
-
return "auto_increment";
|
|
744
|
-
}
|
|
745
|
-
visitReferences(node) {
|
|
746
|
-
this.append("references ");
|
|
747
|
-
this.visitNode(node.table);
|
|
748
|
-
this.append(" (");
|
|
749
|
-
this.compileList(node.columns);
|
|
750
|
-
this.append(")");
|
|
751
|
-
if (node.onDelete) {
|
|
752
|
-
this.append(" on delete ");
|
|
753
|
-
this.append(node.onDelete);
|
|
754
|
-
}
|
|
755
|
-
if (node.onUpdate) {
|
|
756
|
-
this.append(" on update ");
|
|
757
|
-
this.append(node.onUpdate);
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
visitDropTable(node) {
|
|
761
|
-
this.append("drop table ");
|
|
762
|
-
if (node.ifExists) {
|
|
763
|
-
this.append("if exists ");
|
|
764
|
-
}
|
|
765
|
-
this.visitNode(node.table);
|
|
766
|
-
if (node.cascade) {
|
|
767
|
-
this.append(" cascade");
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
visitDataType(node) {
|
|
771
|
-
this.append(node.dataType);
|
|
772
|
-
}
|
|
773
|
-
visitOrderBy(node) {
|
|
774
|
-
this.append("order by ");
|
|
775
|
-
this.compileList(node.items);
|
|
776
|
-
}
|
|
777
|
-
visitOrderByItem(node) {
|
|
778
|
-
this.visitNode(node.orderBy);
|
|
779
|
-
if (node.direction) {
|
|
780
|
-
this.append(" ");
|
|
781
|
-
this.visitNode(node.direction);
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
visitGroupBy(node) {
|
|
785
|
-
this.append("group by ");
|
|
786
|
-
this.compileList(node.items);
|
|
787
|
-
}
|
|
788
|
-
visitGroupByItem(node) {
|
|
789
|
-
this.visitNode(node.groupBy);
|
|
790
|
-
}
|
|
791
|
-
visitUpdateQuery(node) {
|
|
792
|
-
const rootQueryNode = this.nodeStack.find(QueryNode.is);
|
|
793
|
-
const isSubQuery = rootQueryNode !== node;
|
|
794
|
-
if (!isSubQuery && node.explain) {
|
|
795
|
-
this.visitNode(node.explain);
|
|
796
|
-
this.append(" ");
|
|
797
|
-
}
|
|
798
|
-
if (isSubQuery && !MergeQueryNode.is(rootQueryNode)) {
|
|
799
|
-
this.append("(");
|
|
800
|
-
}
|
|
801
|
-
if (node.with) {
|
|
802
|
-
this.visitNode(node.with);
|
|
803
|
-
this.append(" ");
|
|
804
|
-
}
|
|
805
|
-
this.append("update ");
|
|
806
|
-
if (node.top) {
|
|
807
|
-
this.visitNode(node.top);
|
|
808
|
-
this.append(" ");
|
|
809
|
-
}
|
|
810
|
-
if (node.table) {
|
|
811
|
-
this.visitNode(node.table);
|
|
812
|
-
this.append(" ");
|
|
813
|
-
}
|
|
814
|
-
this.append("set ");
|
|
815
|
-
if (node.updates) {
|
|
816
|
-
this.compileList(node.updates);
|
|
817
|
-
}
|
|
818
|
-
if (node.output) {
|
|
819
|
-
this.append(" ");
|
|
820
|
-
this.visitNode(node.output);
|
|
821
|
-
}
|
|
822
|
-
if (node.from) {
|
|
823
|
-
this.append(" ");
|
|
824
|
-
this.visitNode(node.from);
|
|
825
|
-
}
|
|
826
|
-
if (node.joins) {
|
|
827
|
-
this.append(" ");
|
|
828
|
-
this.compileList(node.joins, " ");
|
|
829
|
-
}
|
|
830
|
-
if (node.where) {
|
|
831
|
-
this.append(" ");
|
|
832
|
-
this.visitNode(node.where);
|
|
833
|
-
}
|
|
834
|
-
if (node.limit) {
|
|
835
|
-
this.append(" ");
|
|
836
|
-
this.visitNode(node.limit);
|
|
837
|
-
}
|
|
838
|
-
if (node.returning) {
|
|
839
|
-
this.append(" ");
|
|
840
|
-
this.visitNode(node.returning);
|
|
841
|
-
}
|
|
842
|
-
if (isSubQuery && !MergeQueryNode.is(rootQueryNode)) {
|
|
843
|
-
this.append(")");
|
|
844
|
-
}
|
|
845
|
-
if (node.endModifiers?.length) {
|
|
846
|
-
this.append(" ");
|
|
847
|
-
this.compileList(node.endModifiers, " ");
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
visitColumnUpdate(node) {
|
|
851
|
-
this.visitNode(node.column);
|
|
852
|
-
this.append(" = ");
|
|
853
|
-
this.visitNode(node.value);
|
|
854
|
-
}
|
|
855
|
-
visitLimit(node) {
|
|
856
|
-
this.append("limit ");
|
|
857
|
-
this.visitNode(node.limit);
|
|
858
|
-
}
|
|
859
|
-
visitOffset(node) {
|
|
860
|
-
this.append("offset ");
|
|
861
|
-
this.visitNode(node.offset);
|
|
862
|
-
}
|
|
863
|
-
visitOnConflict(node) {
|
|
864
|
-
this.append("on conflict");
|
|
865
|
-
if (node.columns) {
|
|
866
|
-
this.append(" (");
|
|
867
|
-
this.compileList(node.columns);
|
|
868
|
-
this.append(")");
|
|
869
|
-
} else if (node.constraint) {
|
|
870
|
-
this.append(" on constraint ");
|
|
871
|
-
this.visitNode(node.constraint);
|
|
872
|
-
} else if (node.indexExpression) {
|
|
873
|
-
this.append(" (");
|
|
874
|
-
this.visitNode(node.indexExpression);
|
|
875
|
-
this.append(")");
|
|
876
|
-
}
|
|
877
|
-
if (node.indexWhere) {
|
|
878
|
-
this.append(" ");
|
|
879
|
-
this.visitNode(node.indexWhere);
|
|
880
|
-
}
|
|
881
|
-
if (node.doNothing === true) {
|
|
882
|
-
this.append(" do nothing");
|
|
883
|
-
} else if (node.updates) {
|
|
884
|
-
this.append(" do update set ");
|
|
885
|
-
this.compileList(node.updates);
|
|
886
|
-
if (node.updateWhere) {
|
|
887
|
-
this.append(" ");
|
|
888
|
-
this.visitNode(node.updateWhere);
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
visitOnDuplicateKey(node) {
|
|
893
|
-
this.append("on duplicate key update ");
|
|
894
|
-
this.compileList(node.updates);
|
|
895
|
-
}
|
|
896
|
-
visitCreateIndex(node) {
|
|
897
|
-
this.append("create ");
|
|
898
|
-
if (node.unique) {
|
|
899
|
-
this.append("unique ");
|
|
900
|
-
}
|
|
901
|
-
this.append("index ");
|
|
902
|
-
if (node.ifNotExists) {
|
|
903
|
-
this.append("if not exists ");
|
|
904
|
-
}
|
|
905
|
-
this.visitNode(node.name);
|
|
906
|
-
if (node.table) {
|
|
907
|
-
this.append(" on ");
|
|
908
|
-
this.visitNode(node.table);
|
|
909
|
-
}
|
|
910
|
-
if (node.using) {
|
|
911
|
-
this.append(" using ");
|
|
912
|
-
this.visitNode(node.using);
|
|
913
|
-
}
|
|
914
|
-
if (node.columns) {
|
|
915
|
-
this.append(" (");
|
|
916
|
-
this.compileList(node.columns);
|
|
917
|
-
this.append(")");
|
|
918
|
-
}
|
|
919
|
-
if (node.nullsNotDistinct) {
|
|
920
|
-
this.append(" nulls not distinct");
|
|
921
|
-
}
|
|
922
|
-
if (node.where) {
|
|
923
|
-
this.append(" ");
|
|
924
|
-
this.visitNode(node.where);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
visitDropIndex(node) {
|
|
928
|
-
this.append("drop index ");
|
|
929
|
-
if (node.ifExists) {
|
|
930
|
-
this.append("if exists ");
|
|
931
|
-
}
|
|
932
|
-
this.visitNode(node.name);
|
|
933
|
-
if (node.table) {
|
|
934
|
-
this.append(" on ");
|
|
935
|
-
this.visitNode(node.table);
|
|
936
|
-
}
|
|
937
|
-
if (node.cascade) {
|
|
938
|
-
this.append(" cascade");
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
visitCreateSchema(node) {
|
|
942
|
-
this.append("create schema ");
|
|
943
|
-
if (node.ifNotExists) {
|
|
944
|
-
this.append("if not exists ");
|
|
945
|
-
}
|
|
946
|
-
this.visitNode(node.schema);
|
|
947
|
-
}
|
|
948
|
-
visitDropSchema(node) {
|
|
949
|
-
this.append("drop schema ");
|
|
950
|
-
if (node.ifExists) {
|
|
951
|
-
this.append("if exists ");
|
|
952
|
-
}
|
|
953
|
-
this.visitNode(node.schema);
|
|
954
|
-
if (node.cascade) {
|
|
955
|
-
this.append(" cascade");
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
visitPrimaryKeyConstraint(node) {
|
|
959
|
-
if (node.name) {
|
|
960
|
-
this.append("constraint ");
|
|
961
|
-
this.visitNode(node.name);
|
|
962
|
-
this.append(" ");
|
|
963
|
-
}
|
|
964
|
-
this.append("primary key (");
|
|
965
|
-
this.compileList(node.columns);
|
|
966
|
-
this.append(")");
|
|
967
|
-
}
|
|
968
|
-
visitUniqueConstraint(node) {
|
|
969
|
-
if (node.name) {
|
|
970
|
-
this.append("constraint ");
|
|
971
|
-
this.visitNode(node.name);
|
|
972
|
-
this.append(" ");
|
|
973
|
-
}
|
|
974
|
-
this.append("unique");
|
|
975
|
-
if (node.nullsNotDistinct) {
|
|
976
|
-
this.append(" nulls not distinct");
|
|
977
|
-
}
|
|
978
|
-
this.append(" (");
|
|
979
|
-
this.compileList(node.columns);
|
|
980
|
-
this.append(")");
|
|
981
|
-
}
|
|
982
|
-
visitCheckConstraint(node) {
|
|
983
|
-
if (node.name) {
|
|
984
|
-
this.append("constraint ");
|
|
985
|
-
this.visitNode(node.name);
|
|
986
|
-
this.append(" ");
|
|
987
|
-
}
|
|
988
|
-
this.append("check (");
|
|
989
|
-
this.visitNode(node.expression);
|
|
990
|
-
this.append(")");
|
|
991
|
-
}
|
|
992
|
-
visitForeignKeyConstraint(node) {
|
|
993
|
-
if (node.name) {
|
|
994
|
-
this.append("constraint ");
|
|
995
|
-
this.visitNode(node.name);
|
|
996
|
-
this.append(" ");
|
|
997
|
-
}
|
|
998
|
-
this.append("foreign key (");
|
|
999
|
-
this.compileList(node.columns);
|
|
1000
|
-
this.append(") ");
|
|
1001
|
-
this.visitNode(node.references);
|
|
1002
|
-
if (node.onDelete) {
|
|
1003
|
-
this.append(" on delete ");
|
|
1004
|
-
this.append(node.onDelete);
|
|
1005
|
-
}
|
|
1006
|
-
if (node.onUpdate) {
|
|
1007
|
-
this.append(" on update ");
|
|
1008
|
-
this.append(node.onUpdate);
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
visitList(node) {
|
|
1012
|
-
this.compileList(node.items);
|
|
1013
|
-
}
|
|
1014
|
-
visitWith(node) {
|
|
1015
|
-
this.append("with ");
|
|
1016
|
-
if (node.recursive) {
|
|
1017
|
-
this.append("recursive ");
|
|
1018
|
-
}
|
|
1019
|
-
this.compileList(node.expressions);
|
|
1020
|
-
}
|
|
1021
|
-
visitCommonTableExpression(node) {
|
|
1022
|
-
this.visitNode(node.name);
|
|
1023
|
-
this.append(" as ");
|
|
1024
|
-
if (isBoolean(node.materialized)) {
|
|
1025
|
-
if (!node.materialized) {
|
|
1026
|
-
this.append("not ");
|
|
1027
|
-
}
|
|
1028
|
-
this.append("materialized ");
|
|
1029
|
-
}
|
|
1030
|
-
this.visitNode(node.expression);
|
|
1031
|
-
}
|
|
1032
|
-
visitCommonTableExpressionName(node) {
|
|
1033
|
-
this.visitNode(node.table);
|
|
1034
|
-
if (node.columns) {
|
|
1035
|
-
this.append("(");
|
|
1036
|
-
this.compileList(node.columns);
|
|
1037
|
-
this.append(")");
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
visitAlterTable(node) {
|
|
1041
|
-
this.append("alter table ");
|
|
1042
|
-
this.visitNode(node.table);
|
|
1043
|
-
this.append(" ");
|
|
1044
|
-
if (node.renameTo) {
|
|
1045
|
-
this.append("rename to ");
|
|
1046
|
-
this.visitNode(node.renameTo);
|
|
1047
|
-
}
|
|
1048
|
-
if (node.setSchema) {
|
|
1049
|
-
this.append("set schema ");
|
|
1050
|
-
this.visitNode(node.setSchema);
|
|
1051
|
-
}
|
|
1052
|
-
if (node.addConstraint) {
|
|
1053
|
-
this.visitNode(node.addConstraint);
|
|
1054
|
-
}
|
|
1055
|
-
if (node.dropConstraint) {
|
|
1056
|
-
this.visitNode(node.dropConstraint);
|
|
1057
|
-
}
|
|
1058
|
-
if (node.columnAlterations) {
|
|
1059
|
-
this.compileColumnAlterations(node.columnAlterations);
|
|
1060
|
-
}
|
|
1061
|
-
if (node.addIndex) {
|
|
1062
|
-
this.visitNode(node.addIndex);
|
|
1063
|
-
}
|
|
1064
|
-
if (node.dropIndex) {
|
|
1065
|
-
this.visitNode(node.dropIndex);
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
visitAddColumn(node) {
|
|
1069
|
-
this.append("add column ");
|
|
1070
|
-
this.visitNode(node.column);
|
|
1071
|
-
}
|
|
1072
|
-
visitRenameColumn(node) {
|
|
1073
|
-
this.append("rename column ");
|
|
1074
|
-
this.visitNode(node.column);
|
|
1075
|
-
this.append(" to ");
|
|
1076
|
-
this.visitNode(node.renameTo);
|
|
1077
|
-
}
|
|
1078
|
-
visitDropColumn(node) {
|
|
1079
|
-
this.append("drop column ");
|
|
1080
|
-
this.visitNode(node.column);
|
|
1081
|
-
}
|
|
1082
|
-
visitAlterColumn(node) {
|
|
1083
|
-
this.append("alter column ");
|
|
1084
|
-
this.visitNode(node.column);
|
|
1085
|
-
this.append(" ");
|
|
1086
|
-
if (node.dataType) {
|
|
1087
|
-
if (this.announcesNewColumnDataType()) {
|
|
1088
|
-
this.append("type ");
|
|
1089
|
-
}
|
|
1090
|
-
this.visitNode(node.dataType);
|
|
1091
|
-
if (node.dataTypeExpression) {
|
|
1092
|
-
this.append("using ");
|
|
1093
|
-
this.visitNode(node.dataTypeExpression);
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
if (node.setDefault) {
|
|
1097
|
-
this.append("set default ");
|
|
1098
|
-
this.visitNode(node.setDefault);
|
|
1099
|
-
}
|
|
1100
|
-
if (node.dropDefault) {
|
|
1101
|
-
this.append("drop default");
|
|
1102
|
-
}
|
|
1103
|
-
if (node.setNotNull) {
|
|
1104
|
-
this.append("set not null");
|
|
1105
|
-
}
|
|
1106
|
-
if (node.dropNotNull) {
|
|
1107
|
-
this.append("drop not null");
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
visitModifyColumn(node) {
|
|
1111
|
-
this.append("modify column ");
|
|
1112
|
-
this.visitNode(node.column);
|
|
1113
|
-
}
|
|
1114
|
-
visitAddConstraint(node) {
|
|
1115
|
-
this.append("add ");
|
|
1116
|
-
this.visitNode(node.constraint);
|
|
1117
|
-
}
|
|
1118
|
-
visitDropConstraint(node) {
|
|
1119
|
-
this.append("drop constraint ");
|
|
1120
|
-
if (node.ifExists) {
|
|
1121
|
-
this.append("if exists ");
|
|
1122
|
-
}
|
|
1123
|
-
this.visitNode(node.constraintName);
|
|
1124
|
-
if (node.modifier === "cascade") {
|
|
1125
|
-
this.append(" cascade");
|
|
1126
|
-
} else if (node.modifier === "restrict") {
|
|
1127
|
-
this.append(" restrict");
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
visitSetOperation(node) {
|
|
1131
|
-
this.append(node.operator);
|
|
1132
|
-
this.append(" ");
|
|
1133
|
-
if (node.all) {
|
|
1134
|
-
this.append("all ");
|
|
1135
|
-
}
|
|
1136
|
-
this.visitNode(node.expression);
|
|
1137
|
-
}
|
|
1138
|
-
visitCreateView(node) {
|
|
1139
|
-
this.append("create ");
|
|
1140
|
-
if (node.orReplace) {
|
|
1141
|
-
this.append("or replace ");
|
|
1142
|
-
}
|
|
1143
|
-
if (node.materialized) {
|
|
1144
|
-
this.append("materialized ");
|
|
1145
|
-
}
|
|
1146
|
-
if (node.temporary) {
|
|
1147
|
-
this.append("temporary ");
|
|
1148
|
-
}
|
|
1149
|
-
this.append("view ");
|
|
1150
|
-
if (node.ifNotExists) {
|
|
1151
|
-
this.append("if not exists ");
|
|
1152
|
-
}
|
|
1153
|
-
this.visitNode(node.name);
|
|
1154
|
-
this.append(" ");
|
|
1155
|
-
if (node.columns) {
|
|
1156
|
-
this.append("(");
|
|
1157
|
-
this.compileList(node.columns);
|
|
1158
|
-
this.append(") ");
|
|
1159
|
-
}
|
|
1160
|
-
if (node.as) {
|
|
1161
|
-
this.append("as ");
|
|
1162
|
-
this.visitNode(node.as);
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
visitDropView(node) {
|
|
1166
|
-
this.append("drop ");
|
|
1167
|
-
if (node.materialized) {
|
|
1168
|
-
this.append("materialized ");
|
|
1169
|
-
}
|
|
1170
|
-
this.append("view ");
|
|
1171
|
-
if (node.ifExists) {
|
|
1172
|
-
this.append("if exists ");
|
|
1173
|
-
}
|
|
1174
|
-
this.visitNode(node.name);
|
|
1175
|
-
if (node.cascade) {
|
|
1176
|
-
this.append(" cascade");
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
|
-
visitGenerated(node) {
|
|
1180
|
-
this.append("generated ");
|
|
1181
|
-
if (node.always) {
|
|
1182
|
-
this.append("always ");
|
|
1183
|
-
}
|
|
1184
|
-
if (node.byDefault) {
|
|
1185
|
-
this.append("by default ");
|
|
1186
|
-
}
|
|
1187
|
-
this.append("as ");
|
|
1188
|
-
if (node.identity) {
|
|
1189
|
-
this.append("identity");
|
|
1190
|
-
}
|
|
1191
|
-
if (node.expression) {
|
|
1192
|
-
this.append("(");
|
|
1193
|
-
this.visitNode(node.expression);
|
|
1194
|
-
this.append(")");
|
|
1195
|
-
}
|
|
1196
|
-
if (node.stored) {
|
|
1197
|
-
this.append(" stored");
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
visitDefaultValue(node) {
|
|
1201
|
-
this.append("default ");
|
|
1202
|
-
this.visitNode(node.defaultValue);
|
|
1203
|
-
}
|
|
1204
|
-
visitSelectModifier(node) {
|
|
1205
|
-
if (node.rawModifier) {
|
|
1206
|
-
this.visitNode(node.rawModifier);
|
|
1207
|
-
} else {
|
|
1208
|
-
this.append(SELECT_MODIFIER_SQL[node.modifier]);
|
|
1209
|
-
}
|
|
1210
|
-
if (node.of) {
|
|
1211
|
-
this.append(" of ");
|
|
1212
|
-
this.compileList(node.of, ", ");
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
visitCreateType(node) {
|
|
1216
|
-
this.append("create type ");
|
|
1217
|
-
this.visitNode(node.name);
|
|
1218
|
-
if (node.enum) {
|
|
1219
|
-
this.append(" as enum ");
|
|
1220
|
-
this.visitNode(node.enum);
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
visitDropType(node) {
|
|
1224
|
-
this.append("drop type ");
|
|
1225
|
-
if (node.ifExists) {
|
|
1226
|
-
this.append("if exists ");
|
|
1227
|
-
}
|
|
1228
|
-
this.visitNode(node.name);
|
|
1229
|
-
}
|
|
1230
|
-
visitExplain(node) {
|
|
1231
|
-
this.append("explain");
|
|
1232
|
-
if (node.options || node.format) {
|
|
1233
|
-
this.append(" ");
|
|
1234
|
-
this.append(this.getLeftExplainOptionsWrapper());
|
|
1235
|
-
if (node.options) {
|
|
1236
|
-
this.visitNode(node.options);
|
|
1237
|
-
if (node.format) {
|
|
1238
|
-
this.append(this.getExplainOptionsDelimiter());
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1241
|
-
if (node.format) {
|
|
1242
|
-
this.append("format");
|
|
1243
|
-
this.append(this.getExplainOptionAssignment());
|
|
1244
|
-
this.append(node.format);
|
|
1245
|
-
}
|
|
1246
|
-
this.append(this.getRightExplainOptionsWrapper());
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
visitDefaultInsertValue(_) {
|
|
1250
|
-
this.append("default");
|
|
1251
|
-
}
|
|
1252
|
-
visitAggregateFunction(node) {
|
|
1253
|
-
this.append(node.func);
|
|
1254
|
-
this.append("(");
|
|
1255
|
-
if (node.distinct) {
|
|
1256
|
-
this.append("distinct ");
|
|
1257
|
-
}
|
|
1258
|
-
this.compileList(node.aggregated);
|
|
1259
|
-
if (node.orderBy) {
|
|
1260
|
-
this.append(" ");
|
|
1261
|
-
this.visitNode(node.orderBy);
|
|
1262
|
-
}
|
|
1263
|
-
this.append(")");
|
|
1264
|
-
if (node.filter) {
|
|
1265
|
-
this.append(" filter(");
|
|
1266
|
-
this.visitNode(node.filter);
|
|
1267
|
-
this.append(")");
|
|
1268
|
-
}
|
|
1269
|
-
if (node.over) {
|
|
1270
|
-
this.append(" ");
|
|
1271
|
-
this.visitNode(node.over);
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
visitOver(node) {
|
|
1275
|
-
this.append("over(");
|
|
1276
|
-
if (node.partitionBy) {
|
|
1277
|
-
this.visitNode(node.partitionBy);
|
|
1278
|
-
if (node.orderBy) {
|
|
1279
|
-
this.append(" ");
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
if (node.orderBy) {
|
|
1283
|
-
this.visitNode(node.orderBy);
|
|
1284
|
-
}
|
|
1285
|
-
this.append(")");
|
|
1286
|
-
}
|
|
1287
|
-
visitPartitionBy(node) {
|
|
1288
|
-
this.append("partition by ");
|
|
1289
|
-
this.compileList(node.items);
|
|
1290
|
-
}
|
|
1291
|
-
visitPartitionByItem(node) {
|
|
1292
|
-
this.visitNode(node.partitionBy);
|
|
1293
|
-
}
|
|
1294
|
-
visitBinaryOperation(node) {
|
|
1295
|
-
this.visitNode(node.leftOperand);
|
|
1296
|
-
this.append(" ");
|
|
1297
|
-
this.visitNode(node.operator);
|
|
1298
|
-
this.append(" ");
|
|
1299
|
-
this.visitNode(node.rightOperand);
|
|
1300
|
-
}
|
|
1301
|
-
visitUnaryOperation(node) {
|
|
1302
|
-
this.visitNode(node.operator);
|
|
1303
|
-
if (!this.isMinusOperator(node.operator)) {
|
|
1304
|
-
this.append(" ");
|
|
1305
|
-
}
|
|
1306
|
-
this.visitNode(node.operand);
|
|
1307
|
-
}
|
|
1308
|
-
isMinusOperator(node) {
|
|
1309
|
-
return OperatorNode.is(node) && node.operator === "-";
|
|
1310
|
-
}
|
|
1311
|
-
visitUsing(node) {
|
|
1312
|
-
this.append("using ");
|
|
1313
|
-
this.compileList(node.tables);
|
|
1314
|
-
}
|
|
1315
|
-
visitFunction(node) {
|
|
1316
|
-
this.append(node.func);
|
|
1317
|
-
this.append("(");
|
|
1318
|
-
this.compileList(node.arguments);
|
|
1319
|
-
this.append(")");
|
|
1320
|
-
}
|
|
1321
|
-
visitCase(node) {
|
|
1322
|
-
this.append("case");
|
|
1323
|
-
if (node.value) {
|
|
1324
|
-
this.append(" ");
|
|
1325
|
-
this.visitNode(node.value);
|
|
1326
|
-
}
|
|
1327
|
-
if (node.when) {
|
|
1328
|
-
this.append(" ");
|
|
1329
|
-
this.compileList(node.when, " ");
|
|
1330
|
-
}
|
|
1331
|
-
if (node.else) {
|
|
1332
|
-
this.append(" else ");
|
|
1333
|
-
this.visitNode(node.else);
|
|
1334
|
-
}
|
|
1335
|
-
this.append(" end");
|
|
1336
|
-
if (node.isStatement) {
|
|
1337
|
-
this.append(" case");
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
visitWhen(node) {
|
|
1341
|
-
this.append("when ");
|
|
1342
|
-
this.visitNode(node.condition);
|
|
1343
|
-
if (node.result) {
|
|
1344
|
-
this.append(" then ");
|
|
1345
|
-
this.visitNode(node.result);
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
visitJSONReference(node) {
|
|
1349
|
-
this.visitNode(node.reference);
|
|
1350
|
-
this.visitNode(node.traversal);
|
|
1351
|
-
}
|
|
1352
|
-
visitJSONPath(node) {
|
|
1353
|
-
if (node.inOperator) {
|
|
1354
|
-
this.visitNode(node.inOperator);
|
|
1355
|
-
}
|
|
1356
|
-
this.append("'$");
|
|
1357
|
-
for (const pathLeg of node.pathLegs) {
|
|
1358
|
-
this.visitNode(pathLeg);
|
|
1359
|
-
}
|
|
1360
|
-
this.append("'");
|
|
1361
|
-
}
|
|
1362
|
-
visitJSONPathLeg(node) {
|
|
1363
|
-
const isArrayLocation = node.type === "ArrayLocation";
|
|
1364
|
-
this.append(isArrayLocation ? "[" : ".");
|
|
1365
|
-
this.append(String(node.value));
|
|
1366
|
-
if (isArrayLocation) {
|
|
1367
|
-
this.append("]");
|
|
1368
|
-
}
|
|
1369
|
-
}
|
|
1370
|
-
visitJSONOperatorChain(node) {
|
|
1371
|
-
for (let i = 0, len = node.values.length;i < len; i++) {
|
|
1372
|
-
if (i === len - 1) {
|
|
1373
|
-
this.visitNode(node.operator);
|
|
1374
|
-
} else {
|
|
1375
|
-
this.append("->");
|
|
1376
|
-
}
|
|
1377
|
-
this.visitNode(node.values[i]);
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
visitMergeQuery(node) {
|
|
1381
|
-
if (node.with) {
|
|
1382
|
-
this.visitNode(node.with);
|
|
1383
|
-
this.append(" ");
|
|
1384
|
-
}
|
|
1385
|
-
this.append("merge ");
|
|
1386
|
-
if (node.top) {
|
|
1387
|
-
this.visitNode(node.top);
|
|
1388
|
-
this.append(" ");
|
|
1389
|
-
}
|
|
1390
|
-
this.append("into ");
|
|
1391
|
-
this.visitNode(node.into);
|
|
1392
|
-
if (node.using) {
|
|
1393
|
-
this.append(" ");
|
|
1394
|
-
this.visitNode(node.using);
|
|
1395
|
-
}
|
|
1396
|
-
if (node.whens) {
|
|
1397
|
-
this.append(" ");
|
|
1398
|
-
this.compileList(node.whens, " ");
|
|
1399
|
-
}
|
|
1400
|
-
if (node.output) {
|
|
1401
|
-
this.append(" ");
|
|
1402
|
-
this.visitNode(node.output);
|
|
1403
|
-
}
|
|
1404
|
-
if (node.endModifiers?.length) {
|
|
1405
|
-
this.append(" ");
|
|
1406
|
-
this.compileList(node.endModifiers, " ");
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
visitMatched(node) {
|
|
1410
|
-
if (node.not) {
|
|
1411
|
-
this.append("not ");
|
|
1412
|
-
}
|
|
1413
|
-
this.append("matched");
|
|
1414
|
-
if (node.bySource) {
|
|
1415
|
-
this.append(" by source");
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
visitAddIndex(node) {
|
|
1419
|
-
this.append("add ");
|
|
1420
|
-
if (node.unique) {
|
|
1421
|
-
this.append("unique ");
|
|
1422
|
-
}
|
|
1423
|
-
this.append("index ");
|
|
1424
|
-
this.visitNode(node.name);
|
|
1425
|
-
if (node.columns) {
|
|
1426
|
-
this.append(" (");
|
|
1427
|
-
this.compileList(node.columns);
|
|
1428
|
-
this.append(")");
|
|
1429
|
-
}
|
|
1430
|
-
if (node.using) {
|
|
1431
|
-
this.append(" using ");
|
|
1432
|
-
this.visitNode(node.using);
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
visitCast(node) {
|
|
1436
|
-
this.append("cast(");
|
|
1437
|
-
this.visitNode(node.expression);
|
|
1438
|
-
this.append(" as ");
|
|
1439
|
-
this.visitNode(node.dataType);
|
|
1440
|
-
this.append(")");
|
|
1441
|
-
}
|
|
1442
|
-
visitFetch(node) {
|
|
1443
|
-
this.append("fetch next ");
|
|
1444
|
-
this.visitNode(node.rowCount);
|
|
1445
|
-
this.append(` rows ${node.modifier}`);
|
|
1446
|
-
}
|
|
1447
|
-
visitOutput(node) {
|
|
1448
|
-
this.append("output ");
|
|
1449
|
-
this.compileList(node.selections);
|
|
1450
|
-
}
|
|
1451
|
-
visitTop(node) {
|
|
1452
|
-
this.append(`top(${node.expression})`);
|
|
1453
|
-
if (node.modifiers) {
|
|
1454
|
-
this.append(` ${node.modifiers}`);
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
append(str) {
|
|
1458
|
-
this.#sql += str;
|
|
1459
|
-
}
|
|
1460
|
-
appendValue(parameter) {
|
|
1461
|
-
this.addParameter(parameter);
|
|
1462
|
-
this.append(this.getCurrentParameterPlaceholder());
|
|
1463
|
-
}
|
|
1464
|
-
getLeftIdentifierWrapper() {
|
|
1465
|
-
return '"';
|
|
1466
|
-
}
|
|
1467
|
-
getRightIdentifierWrapper() {
|
|
1468
|
-
return '"';
|
|
1469
|
-
}
|
|
1470
|
-
getCurrentParameterPlaceholder() {
|
|
1471
|
-
return "$" + this.numParameters;
|
|
1472
|
-
}
|
|
1473
|
-
getLeftExplainOptionsWrapper() {
|
|
1474
|
-
return "(";
|
|
1475
|
-
}
|
|
1476
|
-
getExplainOptionAssignment() {
|
|
1477
|
-
return " ";
|
|
1478
|
-
}
|
|
1479
|
-
getExplainOptionsDelimiter() {
|
|
1480
|
-
return ", ";
|
|
1481
|
-
}
|
|
1482
|
-
getRightExplainOptionsWrapper() {
|
|
1483
|
-
return ")";
|
|
1484
|
-
}
|
|
1485
|
-
sanitizeIdentifier(identifier) {
|
|
1486
|
-
const leftWrap = this.getLeftIdentifierWrapper();
|
|
1487
|
-
const rightWrap = this.getRightIdentifierWrapper();
|
|
1488
|
-
let sanitized = "";
|
|
1489
|
-
for (const c of identifier) {
|
|
1490
|
-
sanitized += c;
|
|
1491
|
-
if (c === leftWrap) {
|
|
1492
|
-
sanitized += leftWrap;
|
|
1493
|
-
} else if (c === rightWrap) {
|
|
1494
|
-
sanitized += rightWrap;
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
return sanitized;
|
|
1498
|
-
}
|
|
1499
|
-
addParameter(parameter) {
|
|
1500
|
-
this.#parameters.push(parameter);
|
|
1501
|
-
}
|
|
1502
|
-
appendImmediateValue(value) {
|
|
1503
|
-
if (isString(value)) {
|
|
1504
|
-
this.append(`'${value}'`);
|
|
1505
|
-
} else if (isNumber(value) || isBoolean(value)) {
|
|
1506
|
-
this.append(value.toString());
|
|
1507
|
-
} else if (isNull(value)) {
|
|
1508
|
-
this.append("null");
|
|
1509
|
-
} else if (isDate(value)) {
|
|
1510
|
-
this.appendImmediateValue(value.toISOString());
|
|
1511
|
-
} else if (isBigInt(value)) {
|
|
1512
|
-
this.appendImmediateValue(value.toString());
|
|
1513
|
-
} else {
|
|
1514
|
-
throw new Error(`invalid immediate value ${value}`);
|
|
1515
|
-
}
|
|
1516
|
-
}
|
|
1517
|
-
sortSelectModifiers(arr) {
|
|
1518
|
-
arr.sort((left, right) => left.modifier && right.modifier ? SELECT_MODIFIER_PRIORITY[left.modifier] - SELECT_MODIFIER_PRIORITY[right.modifier] : 1);
|
|
1519
|
-
return freeze(arr);
|
|
1520
|
-
}
|
|
1521
|
-
compileColumnAlterations(columnAlterations) {
|
|
1522
|
-
this.compileList(columnAlterations);
|
|
1523
|
-
}
|
|
1524
|
-
announcesNewColumnDataType() {
|
|
1525
|
-
return true;
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
var SELECT_MODIFIER_SQL = freeze({
|
|
1529
|
-
ForKeyShare: "for key share",
|
|
1530
|
-
ForNoKeyUpdate: "for no key update",
|
|
1531
|
-
ForUpdate: "for update",
|
|
1532
|
-
ForShare: "for share",
|
|
1533
|
-
NoWait: "nowait",
|
|
1534
|
-
SkipLocked: "skip locked",
|
|
1535
|
-
Distinct: "distinct"
|
|
1536
|
-
});
|
|
1537
|
-
var SELECT_MODIFIER_PRIORITY = freeze({
|
|
1538
|
-
ForKeyShare: 1,
|
|
1539
|
-
ForNoKeyUpdate: 1,
|
|
1540
|
-
ForUpdate: 1,
|
|
1541
|
-
ForShare: 1,
|
|
1542
|
-
NoWait: 2,
|
|
1543
|
-
SkipLocked: 2,
|
|
1544
|
-
Distinct: 0
|
|
1545
|
-
});
|
|
1546
|
-
var JOIN_TYPE_SQL = freeze({
|
|
1547
|
-
InnerJoin: "inner join",
|
|
1548
|
-
LeftJoin: "left join",
|
|
1549
|
-
RightJoin: "right join",
|
|
1550
|
-
FullJoin: "full join",
|
|
1551
|
-
LateralInnerJoin: "inner join lateral",
|
|
1552
|
-
LateralLeftJoin: "left join lateral",
|
|
1553
|
-
Using: "using"
|
|
1554
|
-
});
|
|
1555
|
-
|
|
1556
|
-
// node_modules/kysely/dist/esm/query-compiler/compiled-query.js
|
|
1557
|
-
var CompiledQuery = freeze({
|
|
1558
|
-
raw(sql2, parameters = []) {
|
|
1559
|
-
return freeze({
|
|
1560
|
-
sql: sql2,
|
|
1561
|
-
query: RawNode.createWithSql(sql2),
|
|
1562
|
-
parameters: freeze(parameters)
|
|
1563
|
-
});
|
|
1564
|
-
}
|
|
1565
|
-
});
|
|
1566
|
-
|
|
1567
|
-
// node_modules/kysely/dist/esm/dialect/dialect-adapter-base.js
|
|
1568
|
-
class DialectAdapterBase {
|
|
1569
|
-
get supportsCreateIfNotExists() {
|
|
1570
|
-
return true;
|
|
1571
|
-
}
|
|
1572
|
-
get supportsTransactionalDdl() {
|
|
1573
|
-
return false;
|
|
1574
|
-
}
|
|
1575
|
-
get supportsReturning() {
|
|
1576
|
-
return false;
|
|
1577
|
-
}
|
|
1578
|
-
get supportsOutput() {
|
|
1579
|
-
return false;
|
|
1580
|
-
}
|
|
1581
|
-
}
|
|
1582
|
-
|
|
1583
|
-
// node_modules/kysely/dist/esm/dialect/sqlite/sqlite-query-compiler.js
|
|
1584
|
-
var ID_WRAP_REGEX = /"/g;
|
|
1585
|
-
|
|
1586
|
-
class SqliteQueryCompiler extends DefaultQueryCompiler {
|
|
1587
|
-
getCurrentParameterPlaceholder() {
|
|
1588
|
-
return "?";
|
|
1589
|
-
}
|
|
1590
|
-
getLeftExplainOptionsWrapper() {
|
|
1591
|
-
return "";
|
|
1592
|
-
}
|
|
1593
|
-
getRightExplainOptionsWrapper() {
|
|
1594
|
-
return "";
|
|
1595
|
-
}
|
|
1596
|
-
getLeftIdentifierWrapper() {
|
|
1597
|
-
return '"';
|
|
1598
|
-
}
|
|
1599
|
-
getRightIdentifierWrapper() {
|
|
1600
|
-
return '"';
|
|
1601
|
-
}
|
|
1602
|
-
getAutoIncrement() {
|
|
1603
|
-
return "autoincrement";
|
|
1604
|
-
}
|
|
1605
|
-
sanitizeIdentifier(identifier) {
|
|
1606
|
-
return identifier.replace(ID_WRAP_REGEX, '""');
|
|
1607
|
-
}
|
|
1608
|
-
visitDefaultInsertValue(_) {
|
|
1609
|
-
this.append("null");
|
|
1610
|
-
}
|
|
1611
|
-
}
|
|
1612
|
-
|
|
1613
|
-
// node_modules/kysely/dist/esm/plugin/noop-plugin.js
|
|
1614
|
-
class NoopPlugin {
|
|
1615
|
-
transformQuery(args) {
|
|
1616
|
-
return args.node;
|
|
1617
|
-
}
|
|
1618
|
-
async transformResult(args) {
|
|
1619
|
-
return args.result;
|
|
1620
|
-
}
|
|
1621
|
-
}
|
|
1622
|
-
|
|
1623
|
-
// node_modules/kysely/dist/esm/migration/migrator.js
|
|
1624
|
-
var DEFAULT_MIGRATION_TABLE = "kysely_migration";
|
|
1625
|
-
var DEFAULT_MIGRATION_LOCK_TABLE = "kysely_migration_lock";
|
|
1626
|
-
var DEFAULT_ALLOW_UNORDERED_MIGRATIONS = false;
|
|
1627
|
-
var MIGRATION_LOCK_ID = "migration_lock";
|
|
1628
|
-
var NO_MIGRATIONS = freeze({ __noMigrations__: true });
|
|
1629
|
-
|
|
1630
|
-
class Migrator {
|
|
1631
|
-
#props;
|
|
1632
|
-
constructor(props) {
|
|
1633
|
-
this.#props = freeze(props);
|
|
1634
|
-
}
|
|
1635
|
-
async getMigrations() {
|
|
1636
|
-
const executedMigrations = await this.#doesTableExists(this.#migrationTable) ? await this.#props.db.withPlugin(this.#schemaPlugin).selectFrom(this.#migrationTable).select(["name", "timestamp"]).$narrowType().execute() : [];
|
|
1637
|
-
const migrations = await this.#resolveMigrations();
|
|
1638
|
-
return migrations.map(({ name, ...migration }) => {
|
|
1639
|
-
const executed = executedMigrations.find((it) => it.name === name);
|
|
1640
|
-
return {
|
|
1641
|
-
name,
|
|
1642
|
-
migration,
|
|
1643
|
-
executedAt: executed ? new Date(executed.timestamp) : undefined
|
|
1644
|
-
};
|
|
1645
|
-
});
|
|
1646
|
-
}
|
|
1647
|
-
async migrateToLatest() {
|
|
1648
|
-
return this.#migrate(() => ({ direction: "Up", step: Infinity }));
|
|
1649
|
-
}
|
|
1650
|
-
async migrateTo(targetMigrationName) {
|
|
1651
|
-
return this.#migrate(({ migrations, executedMigrations, pendingMigrations }) => {
|
|
1652
|
-
if (targetMigrationName === NO_MIGRATIONS) {
|
|
1653
|
-
return { direction: "Down", step: Infinity };
|
|
1654
|
-
}
|
|
1655
|
-
if (!migrations.find((m) => m.name === targetMigrationName)) {
|
|
1656
|
-
throw new Error(`migration "${targetMigrationName}" doesn't exist`);
|
|
1657
|
-
}
|
|
1658
|
-
const executedIndex = executedMigrations.indexOf(targetMigrationName);
|
|
1659
|
-
const pendingIndex = pendingMigrations.findIndex((m) => m.name === targetMigrationName);
|
|
1660
|
-
if (executedIndex !== -1) {
|
|
1661
|
-
return {
|
|
1662
|
-
direction: "Down",
|
|
1663
|
-
step: executedMigrations.length - executedIndex - 1
|
|
1664
|
-
};
|
|
1665
|
-
} else if (pendingIndex !== -1) {
|
|
1666
|
-
return { direction: "Up", step: pendingIndex + 1 };
|
|
1667
|
-
} else {
|
|
1668
|
-
throw new Error(`migration "${targetMigrationName}" isn't executed or pending`);
|
|
1669
|
-
}
|
|
1670
|
-
});
|
|
1671
|
-
}
|
|
1672
|
-
async migrateUp() {
|
|
1673
|
-
return this.#migrate(() => ({ direction: "Up", step: 1 }));
|
|
1674
|
-
}
|
|
1675
|
-
async migrateDown() {
|
|
1676
|
-
return this.#migrate(() => ({ direction: "Down", step: 1 }));
|
|
1677
|
-
}
|
|
1678
|
-
async#migrate(getMigrationDirectionAndStep) {
|
|
1679
|
-
try {
|
|
1680
|
-
await this.#ensureMigrationTablesExists();
|
|
1681
|
-
return await this.#runMigrations(getMigrationDirectionAndStep);
|
|
1682
|
-
} catch (error) {
|
|
1683
|
-
if (error instanceof MigrationResultSetError) {
|
|
1684
|
-
return error.resultSet;
|
|
1685
|
-
}
|
|
1686
|
-
return { error };
|
|
1687
|
-
}
|
|
1688
|
-
}
|
|
1689
|
-
get #migrationTableSchema() {
|
|
1690
|
-
return this.#props.migrationTableSchema;
|
|
1691
|
-
}
|
|
1692
|
-
get #migrationTable() {
|
|
1693
|
-
return this.#props.migrationTableName ?? DEFAULT_MIGRATION_TABLE;
|
|
1694
|
-
}
|
|
1695
|
-
get #migrationLockTable() {
|
|
1696
|
-
return this.#props.migrationLockTableName ?? DEFAULT_MIGRATION_LOCK_TABLE;
|
|
1697
|
-
}
|
|
1698
|
-
get #allowUnorderedMigrations() {
|
|
1699
|
-
return this.#props.allowUnorderedMigrations ?? DEFAULT_ALLOW_UNORDERED_MIGRATIONS;
|
|
1700
|
-
}
|
|
1701
|
-
get #schemaPlugin() {
|
|
1702
|
-
if (this.#migrationTableSchema) {
|
|
1703
|
-
return new WithSchemaPlugin(this.#migrationTableSchema);
|
|
1704
|
-
}
|
|
1705
|
-
return new NoopPlugin;
|
|
1706
|
-
}
|
|
1707
|
-
async#ensureMigrationTablesExists() {
|
|
1708
|
-
await this.#ensureMigrationTableSchemaExists();
|
|
1709
|
-
await this.#ensureMigrationTableExists();
|
|
1710
|
-
await this.#ensureMigrationLockTableExists();
|
|
1711
|
-
await this.#ensureLockRowExists();
|
|
1712
|
-
}
|
|
1713
|
-
async#ensureMigrationTableSchemaExists() {
|
|
1714
|
-
if (!this.#migrationTableSchema) {
|
|
1715
|
-
return;
|
|
1716
|
-
}
|
|
1717
|
-
if (!await this.#doesSchemaExists()) {
|
|
1718
|
-
try {
|
|
1719
|
-
await this.#createIfNotExists(this.#props.db.schema.createSchema(this.#migrationTableSchema));
|
|
1720
|
-
} catch (error) {
|
|
1721
|
-
if (!await this.#doesSchemaExists()) {
|
|
1722
|
-
throw error;
|
|
1723
|
-
}
|
|
1724
|
-
}
|
|
1725
|
-
}
|
|
1726
|
-
}
|
|
1727
|
-
async#ensureMigrationTableExists() {
|
|
1728
|
-
if (!await this.#doesTableExists(this.#migrationTable)) {
|
|
1729
|
-
try {
|
|
1730
|
-
if (this.#migrationTableSchema) {
|
|
1731
|
-
await this.#createIfNotExists(this.#props.db.schema.createSchema(this.#migrationTableSchema));
|
|
1732
|
-
}
|
|
1733
|
-
await this.#createIfNotExists(this.#props.db.schema.withPlugin(this.#schemaPlugin).createTable(this.#migrationTable).addColumn("name", "varchar(255)", (col) => col.notNull().primaryKey()).addColumn("timestamp", "varchar(255)", (col) => col.notNull()));
|
|
1734
|
-
} catch (error) {
|
|
1735
|
-
if (!await this.#doesTableExists(this.#migrationTable)) {
|
|
1736
|
-
throw error;
|
|
1737
|
-
}
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
|
-
}
|
|
1741
|
-
async#ensureMigrationLockTableExists() {
|
|
1742
|
-
if (!await this.#doesTableExists(this.#migrationLockTable)) {
|
|
1743
|
-
try {
|
|
1744
|
-
await this.#createIfNotExists(this.#props.db.schema.withPlugin(this.#schemaPlugin).createTable(this.#migrationLockTable).addColumn("id", "varchar(255)", (col) => col.notNull().primaryKey()).addColumn("is_locked", "integer", (col) => col.notNull().defaultTo(0)));
|
|
1745
|
-
} catch (error) {
|
|
1746
|
-
if (!await this.#doesTableExists(this.#migrationLockTable)) {
|
|
1747
|
-
throw error;
|
|
1748
|
-
}
|
|
1749
|
-
}
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
async#ensureLockRowExists() {
|
|
1753
|
-
if (!await this.#doesLockRowExists()) {
|
|
1754
|
-
try {
|
|
1755
|
-
await this.#props.db.withPlugin(this.#schemaPlugin).insertInto(this.#migrationLockTable).values({ id: MIGRATION_LOCK_ID, is_locked: 0 }).execute();
|
|
1756
|
-
} catch (error) {
|
|
1757
|
-
if (!await this.#doesLockRowExists()) {
|
|
1758
|
-
throw error;
|
|
1759
|
-
}
|
|
1760
|
-
}
|
|
1761
|
-
}
|
|
1762
|
-
}
|
|
1763
|
-
async#doesSchemaExists() {
|
|
1764
|
-
const schemas = await this.#props.db.introspection.getSchemas();
|
|
1765
|
-
return schemas.some((it) => it.name === this.#migrationTableSchema);
|
|
1766
|
-
}
|
|
1767
|
-
async#doesTableExists(tableName) {
|
|
1768
|
-
const schema = this.#migrationTableSchema;
|
|
1769
|
-
const tables = await this.#props.db.introspection.getTables({
|
|
1770
|
-
withInternalKyselyTables: true
|
|
1771
|
-
});
|
|
1772
|
-
return tables.some((it) => it.name === tableName && (!schema || it.schema === schema));
|
|
1773
|
-
}
|
|
1774
|
-
async#doesLockRowExists() {
|
|
1775
|
-
const lockRow = await this.#props.db.withPlugin(this.#schemaPlugin).selectFrom(this.#migrationLockTable).where("id", "=", MIGRATION_LOCK_ID).select("id").executeTakeFirst();
|
|
1776
|
-
return !!lockRow;
|
|
1777
|
-
}
|
|
1778
|
-
async#runMigrations(getMigrationDirectionAndStep) {
|
|
1779
|
-
const adapter = this.#props.db.getExecutor().adapter;
|
|
1780
|
-
const lockOptions = freeze({
|
|
1781
|
-
lockTable: this.#props.migrationLockTableName ?? DEFAULT_MIGRATION_LOCK_TABLE,
|
|
1782
|
-
lockRowId: MIGRATION_LOCK_ID,
|
|
1783
|
-
lockTableSchema: this.#props.migrationTableSchema
|
|
1784
|
-
});
|
|
1785
|
-
const run = async (db) => {
|
|
1786
|
-
try {
|
|
1787
|
-
await adapter.acquireMigrationLock(db, lockOptions);
|
|
1788
|
-
const state = await this.#getState(db);
|
|
1789
|
-
if (state.migrations.length === 0) {
|
|
1790
|
-
return { results: [] };
|
|
1791
|
-
}
|
|
1792
|
-
const { direction, step } = getMigrationDirectionAndStep(state);
|
|
1793
|
-
if (step <= 0) {
|
|
1794
|
-
return { results: [] };
|
|
1795
|
-
}
|
|
1796
|
-
if (direction === "Down") {
|
|
1797
|
-
return await this.#migrateDown(db, state, step);
|
|
1798
|
-
} else if (direction === "Up") {
|
|
1799
|
-
return await this.#migrateUp(db, state, step);
|
|
1800
|
-
}
|
|
1801
|
-
return { results: [] };
|
|
1802
|
-
} finally {
|
|
1803
|
-
await adapter.releaseMigrationLock(db, lockOptions);
|
|
1804
|
-
}
|
|
1805
|
-
};
|
|
1806
|
-
if (adapter.supportsTransactionalDdl) {
|
|
1807
|
-
return this.#props.db.transaction().execute(run);
|
|
1808
|
-
} else {
|
|
1809
|
-
return this.#props.db.connection().execute(run);
|
|
1810
|
-
}
|
|
1811
|
-
}
|
|
1812
|
-
async#getState(db) {
|
|
1813
|
-
const migrations = await this.#resolveMigrations();
|
|
1814
|
-
const executedMigrations = await this.#getExecutedMigrations(db);
|
|
1815
|
-
this.#ensureNoMissingMigrations(migrations, executedMigrations);
|
|
1816
|
-
if (!this.#allowUnorderedMigrations) {
|
|
1817
|
-
this.#ensureMigrationsInOrder(migrations, executedMigrations);
|
|
1818
|
-
}
|
|
1819
|
-
const pendingMigrations = this.#getPendingMigrations(migrations, executedMigrations);
|
|
1820
|
-
return freeze({
|
|
1821
|
-
migrations,
|
|
1822
|
-
executedMigrations,
|
|
1823
|
-
lastMigration: getLast(executedMigrations),
|
|
1824
|
-
pendingMigrations
|
|
1825
|
-
});
|
|
1826
|
-
}
|
|
1827
|
-
#getPendingMigrations(migrations, executedMigrations) {
|
|
1828
|
-
return migrations.filter((migration) => {
|
|
1829
|
-
return !executedMigrations.includes(migration.name);
|
|
1830
|
-
});
|
|
1831
|
-
}
|
|
1832
|
-
async#resolveMigrations() {
|
|
1833
|
-
const allMigrations = await this.#props.provider.getMigrations();
|
|
1834
|
-
return Object.keys(allMigrations).sort().map((name) => ({
|
|
1835
|
-
...allMigrations[name],
|
|
1836
|
-
name
|
|
1837
|
-
}));
|
|
1838
|
-
}
|
|
1839
|
-
async#getExecutedMigrations(db) {
|
|
1840
|
-
const executedMigrations = await db.withPlugin(this.#schemaPlugin).selectFrom(this.#migrationTable).select(["name", "timestamp"]).$narrowType().execute();
|
|
1841
|
-
const nameComparator = this.#props.nameComparator || ((a, b) => a.localeCompare(b));
|
|
1842
|
-
return executedMigrations.sort((a, b) => {
|
|
1843
|
-
if (a.timestamp === b.timestamp) {
|
|
1844
|
-
return nameComparator(a.name, b.name);
|
|
1845
|
-
}
|
|
1846
|
-
return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
|
|
1847
|
-
}).map((it) => it.name);
|
|
1848
|
-
}
|
|
1849
|
-
#ensureNoMissingMigrations(migrations, executedMigrations) {
|
|
1850
|
-
for (const executed of executedMigrations) {
|
|
1851
|
-
if (!migrations.some((it) => it.name === executed)) {
|
|
1852
|
-
throw new Error(`corrupted migrations: previously executed migration ${executed} is missing`);
|
|
1853
|
-
}
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1856
|
-
#ensureMigrationsInOrder(migrations, executedMigrations) {
|
|
1857
|
-
for (let i = 0;i < executedMigrations.length; ++i) {
|
|
1858
|
-
if (migrations[i].name !== executedMigrations[i]) {
|
|
1859
|
-
throw new Error(`corrupted migrations: expected previously executed migration ${executedMigrations[i]} to be at index ${i} but ${migrations[i].name} was found in its place. New migrations must always have a name that comes alphabetically after the last executed migration.`);
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
}
|
|
1863
|
-
async#migrateDown(db, state, step) {
|
|
1864
|
-
const migrationsToRollback = state.executedMigrations.slice().reverse().slice(0, step).map((name) => {
|
|
1865
|
-
return state.migrations.find((it) => it.name === name);
|
|
1866
|
-
});
|
|
1867
|
-
const results = migrationsToRollback.map((migration) => {
|
|
1868
|
-
return {
|
|
1869
|
-
migrationName: migration.name,
|
|
1870
|
-
direction: "Down",
|
|
1871
|
-
status: "NotExecuted"
|
|
1872
|
-
};
|
|
1873
|
-
});
|
|
1874
|
-
for (let i = 0;i < results.length; ++i) {
|
|
1875
|
-
const migration = migrationsToRollback[i];
|
|
1876
|
-
try {
|
|
1877
|
-
if (migration.down) {
|
|
1878
|
-
await migration.down(db);
|
|
1879
|
-
await db.withPlugin(this.#schemaPlugin).deleteFrom(this.#migrationTable).where("name", "=", migration.name).execute();
|
|
1880
|
-
results[i] = {
|
|
1881
|
-
migrationName: migration.name,
|
|
1882
|
-
direction: "Down",
|
|
1883
|
-
status: "Success"
|
|
1884
|
-
};
|
|
1885
|
-
}
|
|
1886
|
-
} catch (error) {
|
|
1887
|
-
results[i] = {
|
|
1888
|
-
migrationName: migration.name,
|
|
1889
|
-
direction: "Down",
|
|
1890
|
-
status: "Error"
|
|
1891
|
-
};
|
|
1892
|
-
throw new MigrationResultSetError({
|
|
1893
|
-
error,
|
|
1894
|
-
results
|
|
1895
|
-
});
|
|
1896
|
-
}
|
|
1897
|
-
}
|
|
1898
|
-
return { results };
|
|
1899
|
-
}
|
|
1900
|
-
async#migrateUp(db, state, step) {
|
|
1901
|
-
const migrationsToRun = state.pendingMigrations.slice(0, step);
|
|
1902
|
-
const results = migrationsToRun.map((migration) => {
|
|
1903
|
-
return {
|
|
1904
|
-
migrationName: migration.name,
|
|
1905
|
-
direction: "Up",
|
|
1906
|
-
status: "NotExecuted"
|
|
1907
|
-
};
|
|
1908
|
-
});
|
|
1909
|
-
for (let i = 0;i < results.length; i++) {
|
|
1910
|
-
const migration = state.pendingMigrations[i];
|
|
1911
|
-
try {
|
|
1912
|
-
await migration.up(db);
|
|
1913
|
-
await db.withPlugin(this.#schemaPlugin).insertInto(this.#migrationTable).values({
|
|
1914
|
-
name: migration.name,
|
|
1915
|
-
timestamp: new Date().toISOString()
|
|
1916
|
-
}).execute();
|
|
1917
|
-
results[i] = {
|
|
1918
|
-
migrationName: migration.name,
|
|
1919
|
-
direction: "Up",
|
|
1920
|
-
status: "Success"
|
|
1921
|
-
};
|
|
1922
|
-
} catch (error) {
|
|
1923
|
-
results[i] = {
|
|
1924
|
-
migrationName: migration.name,
|
|
1925
|
-
direction: "Up",
|
|
1926
|
-
status: "Error"
|
|
1927
|
-
};
|
|
1928
|
-
throw new MigrationResultSetError({
|
|
1929
|
-
error,
|
|
1930
|
-
results
|
|
1931
|
-
});
|
|
1932
|
-
}
|
|
1933
|
-
}
|
|
1934
|
-
return { results };
|
|
1935
|
-
}
|
|
1936
|
-
async#createIfNotExists(qb) {
|
|
1937
|
-
if (this.#props.db.getExecutor().adapter.supportsCreateIfNotExists) {
|
|
1938
|
-
qb = qb.ifNotExists();
|
|
1939
|
-
}
|
|
1940
|
-
await qb.execute();
|
|
1941
|
-
}
|
|
1942
|
-
}
|
|
1943
|
-
|
|
1944
|
-
class MigrationResultSetError extends Error {
|
|
1945
|
-
#resultSet;
|
|
1946
|
-
constructor(result) {
|
|
1947
|
-
super();
|
|
1948
|
-
this.#resultSet = result;
|
|
1949
|
-
}
|
|
1950
|
-
get resultSet() {
|
|
1951
|
-
return this.#resultSet;
|
|
1952
|
-
}
|
|
1953
|
-
}
|
|
1954
|
-
|
|
1955
|
-
// node_modules/kysely/dist/esm/dialect/sqlite/sqlite-introspector.js
|
|
1956
|
-
class SqliteIntrospector {
|
|
1957
|
-
#db;
|
|
1958
|
-
constructor(db) {
|
|
1959
|
-
this.#db = db;
|
|
1960
|
-
}
|
|
1961
|
-
async getSchemas() {
|
|
1962
|
-
return [];
|
|
1963
|
-
}
|
|
1964
|
-
async getTables(options = { withInternalKyselyTables: false }) {
|
|
1965
|
-
return await this.#getTableMetadata(options);
|
|
1966
|
-
}
|
|
1967
|
-
async getMetadata(options) {
|
|
1968
|
-
return {
|
|
1969
|
-
tables: await this.getTables(options)
|
|
1970
|
-
};
|
|
1971
|
-
}
|
|
1972
|
-
#tablesQuery(qb, options) {
|
|
1973
|
-
let tablesQuery = qb.selectFrom("sqlite_master").where("type", "in", ["table", "view"]).where("name", "not like", "sqlite_%").select(["name", "sql", "type"]).orderBy("name");
|
|
1974
|
-
if (!options.withInternalKyselyTables) {
|
|
1975
|
-
tablesQuery = tablesQuery.where("name", "!=", DEFAULT_MIGRATION_TABLE).where("name", "!=", DEFAULT_MIGRATION_LOCK_TABLE);
|
|
1976
|
-
}
|
|
1977
|
-
return tablesQuery;
|
|
1978
|
-
}
|
|
1979
|
-
async#getTableMetadata(options) {
|
|
1980
|
-
const tablesResult = await this.#tablesQuery(this.#db, options).execute();
|
|
1981
|
-
const tableMetadata = await this.#db.with("table_list", (qb) => this.#tablesQuery(qb, options)).selectFrom([
|
|
1982
|
-
"table_list as tl",
|
|
1983
|
-
sql`pragma_table_info(tl.name)`.as("p")
|
|
1984
|
-
]).select([
|
|
1985
|
-
"tl.name as table",
|
|
1986
|
-
"p.cid",
|
|
1987
|
-
"p.name",
|
|
1988
|
-
"p.type",
|
|
1989
|
-
"p.notnull",
|
|
1990
|
-
"p.dflt_value",
|
|
1991
|
-
"p.pk"
|
|
1992
|
-
]).orderBy(["tl.name", "p.cid"]).execute();
|
|
1993
|
-
const columnsByTable = {};
|
|
1994
|
-
for (const row of tableMetadata) {
|
|
1995
|
-
columnsByTable[row.table] ??= [];
|
|
1996
|
-
columnsByTable[row.table].push(row);
|
|
1997
|
-
}
|
|
1998
|
-
return tablesResult.map(({ name, sql: sql2, type }) => {
|
|
1999
|
-
let autoIncrementCol = sql2?.split(/[\(\),]/)?.find((it) => it.toLowerCase().includes("autoincrement"))?.trimStart()?.split(/\s+/)?.[0]?.replace(/["`]/g, "");
|
|
2000
|
-
const columns = columnsByTable[name] ?? [];
|
|
2001
|
-
if (!autoIncrementCol) {
|
|
2002
|
-
const pkCols = columns.filter((r) => r.pk > 0);
|
|
2003
|
-
if (pkCols.length === 1 && pkCols[0].type.toLowerCase() === "integer") {
|
|
2004
|
-
autoIncrementCol = pkCols[0].name;
|
|
2005
|
-
}
|
|
2006
|
-
}
|
|
2007
|
-
return {
|
|
2008
|
-
name,
|
|
2009
|
-
isView: type === "view",
|
|
2010
|
-
columns: columns.map((col) => ({
|
|
2011
|
-
name: col.name,
|
|
2012
|
-
dataType: col.type,
|
|
2013
|
-
isNullable: !col.notnull,
|
|
2014
|
-
isAutoIncrementing: col.name === autoIncrementCol,
|
|
2015
|
-
hasDefaultValue: col.dflt_value != null,
|
|
2016
|
-
comment: undefined
|
|
2017
|
-
}))
|
|
2018
|
-
};
|
|
2019
|
-
});
|
|
2020
|
-
}
|
|
2021
|
-
}
|
|
2022
|
-
|
|
2023
|
-
// node_modules/kysely/dist/esm/dialect/sqlite/sqlite-adapter.js
|
|
2024
|
-
class SqliteAdapter extends DialectAdapterBase {
|
|
2025
|
-
get supportsTransactionalDdl() {
|
|
2026
|
-
return false;
|
|
2027
|
-
}
|
|
2028
|
-
get supportsReturning() {
|
|
2029
|
-
return true;
|
|
2030
|
-
}
|
|
2031
|
-
async acquireMigrationLock(_db, _opt) {}
|
|
2032
|
-
async releaseMigrationLock(_db, _opt) {}
|
|
2033
|
-
}
|
|
2034
|
-
|
|
2035
|
-
// src/migrations/runner.ts
|
|
2036
|
-
class MigrationRunner {
|
|
2037
|
-
#db;
|
|
2038
|
-
#table;
|
|
2039
|
-
constructor(db, table = "_peta_migrations") {
|
|
2040
|
-
this.#db = db;
|
|
2041
|
-
this.#table = table;
|
|
2042
|
-
}
|
|
2043
|
-
async ensureTable() {
|
|
2044
|
-
await this.#db.schema.createTable(this.#table).ifNotExists().addColumn("name", "varchar", (c) => c.notNull().primaryKey()).addColumn("applied_at", "timestamp", (c) => c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`)).execute();
|
|
2045
|
-
}
|
|
2046
|
-
async getCompleted() {
|
|
2047
|
-
try {
|
|
2048
|
-
const rows = await this.#db.selectFrom(this.#table).select(["name", "applied_at"]).orderBy("name", "asc").execute();
|
|
2049
|
-
return rows.map((r) => ({ name: r.name, appliedAt: r.applied_at }));
|
|
2050
|
-
} catch {
|
|
2051
|
-
return [];
|
|
2052
|
-
}
|
|
2053
|
-
}
|
|
2054
|
-
async up(migrations) {
|
|
2055
|
-
await this.ensureTable();
|
|
2056
|
-
const completed = await this.getCompleted();
|
|
2057
|
-
const completedNames = new Set(completed.map((r) => r.name));
|
|
2058
|
-
const pending = migrations.filter((m) => !completedNames.has(m.name)).sort(byName);
|
|
2059
|
-
for (const m of pending) {
|
|
2060
|
-
await m.up(this.#db);
|
|
2061
|
-
await this.#db.insertInto(this.#table).values({ name: m.name, applied_at: new Date().toISOString() }).execute();
|
|
2062
|
-
}
|
|
2063
|
-
}
|
|
2064
|
-
async down(migrations) {
|
|
2065
|
-
const completed = await this.getCompleted();
|
|
2066
|
-
if (completed.length === 0 || migrations.length === 0)
|
|
2067
|
-
return;
|
|
2068
|
-
const applied = migrations.filter((m) => completed.some((r) => r.name === m.name)).sort(byName).reverse();
|
|
2069
|
-
const lastBatch = applied[0]?.name;
|
|
2070
|
-
if (!lastBatch)
|
|
2071
|
-
return;
|
|
2072
|
-
for (const m of applied) {
|
|
2073
|
-
await m.down(this.#db);
|
|
2074
|
-
await this.#db.deleteFrom(this.#table).where("name", "=", m.name).execute();
|
|
2075
|
-
}
|
|
2076
|
-
}
|
|
2077
|
-
async status(migrations) {
|
|
2078
|
-
const completed = await this.getCompleted();
|
|
2079
|
-
const completedNames = new Set(completed.map((r) => r.name));
|
|
2080
|
-
const pending = migrations.filter((m) => !completedNames.has(m.name)).sort(byName);
|
|
2081
|
-
return { completed, pending };
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
function byName(a, b) {
|
|
2085
|
-
return a.name.localeCompare(b.name);
|
|
2086
|
-
}
|
|
2087
|
-
|
|
2088
|
-
// src/migrations/generator.ts
|
|
2089
|
-
class MigrationGenerator {
|
|
2090
|
-
generateInitialMigration(models, options = {}) {
|
|
2091
|
-
const name = options.name ?? "Initial";
|
|
2092
|
-
const timestamp = new Date().toISOString().replace(/[-:T.Z]/g, "").slice(0, 14);
|
|
2093
|
-
const parts = [];
|
|
2094
|
-
const indexParts = [];
|
|
2095
|
-
const warnings = [];
|
|
2096
|
-
const registeredTables = new Set([...models.values()].map((m) => m.table).filter(Boolean));
|
|
2097
|
-
for (const [, modelClass] of models) {
|
|
2098
|
-
const table = modelClass.table;
|
|
2099
|
-
if (!table)
|
|
2100
|
-
continue;
|
|
2101
|
-
const cols = modelClass.columns;
|
|
2102
|
-
parts.push(this.#generateCreateTable(table, cols));
|
|
2103
|
-
for (const [colName, col] of Object.entries(cols)) {
|
|
2104
|
-
if (col.hasConstraint("index") && !col.isPrimaryKey && !col.isUnique) {
|
|
2105
|
-
indexParts.push(this.#generateCreateIndex(table, colName));
|
|
2106
|
-
}
|
|
2107
|
-
}
|
|
2108
|
-
for (const [relName, rel] of Object.entries(modelClass.relations ?? {})) {
|
|
2109
|
-
if (rel instanceof ManyToMany) {
|
|
2110
|
-
const through = rel.throughTable;
|
|
2111
|
-
if (!registeredTables.has(through)) {
|
|
2112
|
-
warnings.push(`// \u26A0 Detected ManyToMany "${modelClass.name}.${relName}" references table "${through}" ` + `but no model is registered for it.
|
|
2113
|
-
` + `// Add a model class with static override table = "${through}" to include the pivot ` + `table in the generated migration.`);
|
|
2114
|
-
}
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
}
|
|
2118
|
-
const upBody = [...parts, ...indexParts].join(`
|
|
2119
|
-
|
|
2120
|
-
`);
|
|
2121
|
-
const downTables = [...models.values()].filter((m) => m.table).map((m) => ` await db.schema.dropTable("${m.table}").ifExists().execute()`);
|
|
2122
|
-
const warningBlock = warnings.length > 0 ? ` // Warnings:
|
|
2123
|
-
${warnings.join(`
|
|
2124
|
-
`)}
|
|
2125
|
-
|
|
2126
|
-
` : "";
|
|
2127
|
-
return `import type { Kysely } from "kysely"
|
|
2128
|
-
import { sql } from "kysely"
|
|
2129
|
-
|
|
2130
|
-
export async function up(db: Kysely<any>): Promise<void> {
|
|
2131
|
-
${warningBlock}${upBody}
|
|
2132
|
-
}
|
|
2133
|
-
|
|
2134
|
-
export async function down(db: Kysely<any>): Promise<void> {
|
|
2135
|
-
${downTables.join(`
|
|
2136
|
-
`)}
|
|
2137
|
-
}
|
|
2138
|
-
`;
|
|
2139
|
-
}
|
|
2140
|
-
#generateCreateTable(table, columns) {
|
|
2141
|
-
const lines = [];
|
|
2142
|
-
lines.push(` await db.schema.createTable("${table}").ifNotExists()`);
|
|
2143
|
-
for (const [name, col] of Object.entries(columns)) {
|
|
2144
|
-
const typeSql = this.#mapType(col);
|
|
2145
|
-
const cb = this.#columnCallback(col);
|
|
2146
|
-
lines.push(` .addColumn("${name}", "${typeSql}"${cb})`);
|
|
2147
|
-
}
|
|
2148
|
-
lines.push(" .execute()");
|
|
2149
|
-
return lines.join(`
|
|
2150
|
-
`);
|
|
2151
|
-
}
|
|
2152
|
-
#generateCreateIndex(table, column) {
|
|
2153
|
-
return [
|
|
2154
|
-
` await db.schema.createIndex("${table}_${column}_index")`,
|
|
2155
|
-
` .on("${table}")`,
|
|
2156
|
-
` .column("${column}")`,
|
|
2157
|
-
" .execute()"
|
|
2158
|
-
].join(`
|
|
2159
|
-
`);
|
|
2160
|
-
}
|
|
2161
|
-
#columnCallback(col) {
|
|
2162
|
-
const calls = [];
|
|
2163
|
-
if (col.isPrimaryKey) {
|
|
2164
|
-
if (col.dataType === "integer") {
|
|
2165
|
-
calls.push("autoIncrement()");
|
|
2166
|
-
}
|
|
2167
|
-
calls.push("primaryKey()");
|
|
2168
|
-
}
|
|
2169
|
-
if (!col.isNullable && !col.isPrimaryKey) {
|
|
2170
|
-
calls.push("notNull()");
|
|
2171
|
-
}
|
|
2172
|
-
const dv = col.defaultValue;
|
|
2173
|
-
if (dv !== undefined && typeof dv !== "function") {
|
|
2174
|
-
calls.push(`defaultTo(${JSON.stringify(dv)})`);
|
|
2175
|
-
}
|
|
2176
|
-
if (col.isUnique && !col.isPrimaryKey) {
|
|
2177
|
-
calls.push("unique()");
|
|
2178
|
-
}
|
|
2179
|
-
const refConstraint = col.constraints.find((c) => c.type === "references");
|
|
2180
|
-
if (refConstraint?.args[0]) {
|
|
2181
|
-
const targetClass = typeof refConstraint.args[0] === "function" ? refConstraint.args[0]() : refConstraint.args[0];
|
|
2182
|
-
const targetTable = targetClass?.table;
|
|
2183
|
-
const targetColumns = refConstraint.args[1];
|
|
2184
|
-
if (typeof targetTable === "string" && targetTable && targetColumns?.length) {
|
|
2185
|
-
calls.push(`references("${targetTable}.${targetColumns[0]}")`);
|
|
2186
|
-
}
|
|
2187
|
-
}
|
|
2188
|
-
if (calls.length === 0)
|
|
2189
|
-
return "";
|
|
2190
|
-
const str = calls.join(".");
|
|
2191
|
-
return `, (c) => c.${str}`;
|
|
2192
|
-
}
|
|
2193
|
-
#mapType(col) {
|
|
2194
|
-
switch (col.dataType) {
|
|
2195
|
-
case "integer":
|
|
2196
|
-
case "smallint":
|
|
2197
|
-
case "bigint":
|
|
2198
|
-
case "text":
|
|
2199
|
-
case "boolean":
|
|
2200
|
-
case "timestamp":
|
|
2201
|
-
case "date":
|
|
2202
|
-
case "float":
|
|
2203
|
-
case "double":
|
|
2204
|
-
case "uuid":
|
|
2205
|
-
return col.dataType;
|
|
2206
|
-
case "string": {
|
|
2207
|
-
const max = col.args[0];
|
|
2208
|
-
return max != null ? `varchar(${max})` : "varchar";
|
|
2209
|
-
}
|
|
2210
|
-
case "json":
|
|
2211
|
-
case "jsonb":
|
|
2212
|
-
return "json";
|
|
2213
|
-
case "decimal": {
|
|
2214
|
-
const p = col.args[0];
|
|
2215
|
-
const s = col.args[1];
|
|
2216
|
-
return p != null ? `decimal(${p}, ${s ?? 0})` : "decimal";
|
|
2217
|
-
}
|
|
2218
|
-
case "enum": {
|
|
2219
|
-
return "text";
|
|
2220
|
-
}
|
|
2221
|
-
default:
|
|
2222
|
-
return col.dataType;
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
}
|
|
2226
|
-
|
|
2227
|
-
// src/migrations/config.ts
|
|
2228
|
-
import { Database } from "bun:sqlite";
|
|
2229
|
-
|
|
2230
|
-
// node_modules/kysely-bun-sqlite/dist/index.js
|
|
2231
|
-
var BunSqliteDriver = class {
|
|
2232
|
-
#config;
|
|
2233
|
-
#connectionMutex = new ConnectionMutex;
|
|
2234
|
-
#db;
|
|
2235
|
-
#connection;
|
|
2236
|
-
constructor(config) {
|
|
2237
|
-
this.#config = { ...config };
|
|
2238
|
-
}
|
|
2239
|
-
async init() {
|
|
2240
|
-
this.#db = this.#config.database;
|
|
2241
|
-
this.#connection = new BunSqliteConnection(this.#db);
|
|
2242
|
-
if (this.#config.onCreateConnection) {
|
|
2243
|
-
await this.#config.onCreateConnection(this.#connection);
|
|
2244
|
-
}
|
|
2245
|
-
}
|
|
2246
|
-
async acquireConnection() {
|
|
2247
|
-
await this.#connectionMutex.lock();
|
|
2248
|
-
return this.#connection;
|
|
2249
|
-
}
|
|
2250
|
-
async beginTransaction(connection) {
|
|
2251
|
-
await connection.executeQuery(CompiledQuery.raw("begin"));
|
|
2252
|
-
}
|
|
2253
|
-
async commitTransaction(connection) {
|
|
2254
|
-
await connection.executeQuery(CompiledQuery.raw("commit"));
|
|
2255
|
-
}
|
|
2256
|
-
async rollbackTransaction(connection) {
|
|
2257
|
-
await connection.executeQuery(CompiledQuery.raw("rollback"));
|
|
2258
|
-
}
|
|
2259
|
-
async releaseConnection() {
|
|
2260
|
-
this.#connectionMutex.unlock();
|
|
2261
|
-
}
|
|
2262
|
-
async destroy() {
|
|
2263
|
-
this.#db?.close();
|
|
2264
|
-
}
|
|
2265
|
-
};
|
|
2266
|
-
var BunSqliteConnection = class {
|
|
2267
|
-
#db;
|
|
2268
|
-
constructor(db) {
|
|
2269
|
-
this.#db = db;
|
|
2270
|
-
}
|
|
2271
|
-
executeQuery(compiledQuery) {
|
|
2272
|
-
const { sql: sql2, parameters } = compiledQuery;
|
|
2273
|
-
const stmt = this.#db.prepare(sql2);
|
|
2274
|
-
if (stmt.columnNames.length > 0) {
|
|
2275
|
-
return Promise.resolve({
|
|
2276
|
-
rows: stmt.all(parameters)
|
|
2277
|
-
});
|
|
2278
|
-
}
|
|
2279
|
-
const results = stmt.run(parameters);
|
|
2280
|
-
return Promise.resolve({
|
|
2281
|
-
insertId: BigInt(results.lastInsertRowid),
|
|
2282
|
-
numAffectedRows: BigInt(results.changes),
|
|
2283
|
-
rows: []
|
|
2284
|
-
});
|
|
2285
|
-
}
|
|
2286
|
-
async* streamQuery(compiledQuery) {
|
|
2287
|
-
const { sql: sql2, parameters } = compiledQuery;
|
|
2288
|
-
const stmt = this.#db.prepare(sql2);
|
|
2289
|
-
if (!("iterator" in stmt)) {
|
|
2290
|
-
throw new Error("bun:sqlite supports streaming in 1.1.31 or above. Please upgrade to use streaming.");
|
|
2291
|
-
}
|
|
2292
|
-
for await (const row of stmt.iterate(parameters)) {
|
|
2293
|
-
yield { rows: [row] };
|
|
2294
|
-
}
|
|
2295
|
-
}
|
|
2296
|
-
};
|
|
2297
|
-
var ConnectionMutex = class {
|
|
2298
|
-
#promise;
|
|
2299
|
-
#resolve;
|
|
2300
|
-
async lock() {
|
|
2301
|
-
while (this.#promise) {
|
|
2302
|
-
await this.#promise;
|
|
2303
|
-
}
|
|
2304
|
-
this.#promise = new Promise((resolve) => {
|
|
2305
|
-
this.#resolve = resolve;
|
|
2306
|
-
});
|
|
2307
|
-
}
|
|
2308
|
-
unlock() {
|
|
2309
|
-
const resolve = this.#resolve;
|
|
2310
|
-
this.#promise = undefined;
|
|
2311
|
-
this.#resolve = undefined;
|
|
2312
|
-
resolve?.();
|
|
2313
|
-
}
|
|
2314
|
-
};
|
|
2315
|
-
var BunSqliteDialect = class {
|
|
2316
|
-
#config;
|
|
2317
|
-
constructor(config) {
|
|
2318
|
-
this.#config = { ...config };
|
|
2319
|
-
}
|
|
2320
|
-
createDriver() {
|
|
2321
|
-
return new BunSqliteDriver(this.#config);
|
|
2322
|
-
}
|
|
2323
|
-
createQueryCompiler() {
|
|
2324
|
-
return new SqliteQueryCompiler;
|
|
2325
|
-
}
|
|
2326
|
-
createAdapter() {
|
|
2327
|
-
return new SqliteAdapter;
|
|
2328
|
-
}
|
|
2329
|
-
createIntrospector(db) {
|
|
2330
|
-
return new SqliteIntrospector(db);
|
|
2331
|
-
}
|
|
2332
|
-
};
|
|
2333
|
-
|
|
2334
|
-
// src/migrations/config.ts
|
|
2335
|
-
import { readdirSync } from "fs";
|
|
2336
|
-
import { resolve } from "path";
|
|
2337
|
-
function defineConfig(config) {
|
|
2338
|
-
return config;
|
|
2339
|
-
}
|
|
2340
|
-
async function resolveModels(models) {
|
|
2341
|
-
if (Array.isArray(models))
|
|
2342
|
-
return models;
|
|
2343
|
-
const resolved = [];
|
|
2344
|
-
const bunModule = await Promise.resolve(globalThis.Bun).catch(() => null);
|
|
2345
|
-
const Glob = bunModule?.Glob ?? null;
|
|
2346
|
-
if (!Glob)
|
|
2347
|
-
throw new Error("String models pattern requires Bun");
|
|
2348
|
-
let matched = false;
|
|
2349
|
-
for await (const file of new Glob(models).scan()) {
|
|
2350
|
-
matched = true;
|
|
2351
|
-
const mod = await import(resolve(file));
|
|
2352
|
-
for (const value of Object.values(mod)) {
|
|
2353
|
-
if (isModelClass(value))
|
|
2354
|
-
resolved.push(value);
|
|
2355
|
-
}
|
|
2356
|
-
}
|
|
2357
|
-
if (!matched) {
|
|
2358
|
-
try {
|
|
2359
|
-
const mod = await import(resolve(models));
|
|
2360
|
-
for (const value of Object.values(mod)) {
|
|
2361
|
-
if (isModelClass(value))
|
|
2362
|
-
resolved.push(value);
|
|
2363
|
-
}
|
|
2364
|
-
} catch {}
|
|
2365
|
-
}
|
|
2366
|
-
return resolved;
|
|
2367
|
-
}
|
|
2368
|
-
async function loadConfig() {
|
|
2369
|
-
const candidates = ["peta.config.ts", "peta.config.js", "peta.config.mjs"];
|
|
2370
|
-
let config;
|
|
2371
|
-
for (const file of candidates) {
|
|
2372
|
-
try {
|
|
2373
|
-
const mod = await import(resolve(file));
|
|
2374
|
-
if (mod.default) {
|
|
2375
|
-
config = mod.default;
|
|
2376
|
-
break;
|
|
2377
|
-
}
|
|
2378
|
-
} catch {
|
|
2379
|
-
continue;
|
|
2380
|
-
}
|
|
2381
|
-
}
|
|
2382
|
-
if (!config) {
|
|
2383
|
-
throw new Error(`No peta.config.ts found in current directory. Create one:
|
|
2384
|
-
|
|
2385
|
-
import { defineConfig } from "peta-orm/migrator"
|
|
2386
|
-
export default defineConfig({
|
|
2387
|
-
migrationsDir: "./migrations",
|
|
2388
|
-
models: [],
|
|
2389
|
-
})`);
|
|
2390
|
-
}
|
|
2391
|
-
const db = new Database(":memory:");
|
|
2392
|
-
const peta = new Peta({ dialect: new BunSqliteDialect({ database: db }) });
|
|
2393
|
-
const models = await resolveModels(config.models);
|
|
2394
|
-
peta.registerAll(...models);
|
|
2395
|
-
return {
|
|
2396
|
-
peta,
|
|
2397
|
-
migrationsDir: config.migrationsDir,
|
|
2398
|
-
getKysely: () => peta.kysely,
|
|
2399
|
-
getModels: () => peta.models
|
|
2400
|
-
};
|
|
2401
|
-
}
|
|
2402
|
-
async function loadMigrationFiles(dir) {
|
|
2403
|
-
let entries;
|
|
2404
|
-
try {
|
|
2405
|
-
entries = readdirSync(dir);
|
|
2406
|
-
} catch {
|
|
2407
|
-
return [];
|
|
2408
|
-
}
|
|
2409
|
-
const files = [];
|
|
2410
|
-
for (const entry of entries.sort()) {
|
|
2411
|
-
if (!entry.endsWith(".ts") && !entry.endsWith(".js"))
|
|
2412
|
-
continue;
|
|
2413
|
-
if (entry.startsWith("."))
|
|
2414
|
-
continue;
|
|
2415
|
-
const abs = resolve(dir, entry);
|
|
2416
|
-
const mod = await import(abs);
|
|
2417
|
-
if (typeof mod.up === "function" && typeof mod.down === "function") {
|
|
2418
|
-
files.push({ name: entry.replace(/\.(ts|js)$/, ""), up: mod.up, down: mod.down });
|
|
2419
|
-
}
|
|
2420
|
-
}
|
|
2421
|
-
return files;
|
|
2422
|
-
}
|
|
2423
|
-
|
|
2424
|
-
export { MigrationRunner, MigrationGenerator, defineConfig, loadConfig, loadMigrationFiles };
|