@rvoh/dream 0.44.6 → 0.44.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/bin/index.js +7 -87
- package/dist/cjs/src/dream/LeftJoinLoadBuilder.js +1 -1
- package/dist/cjs/src/dream/LoadBuilder.js +1 -1
- package/dist/cjs/src/dream/Query.js +46 -1417
- package/dist/cjs/src/dream/QueryDriver/Base.js +311 -0
- package/dist/cjs/src/dream/QueryDriver/Kysely.js +1771 -0
- package/dist/cjs/src/dream/QueryDriver/Postgres.js +6 -0
- package/dist/cjs/src/dream/internal/saveDream.js +4 -17
- package/dist/esm/src/bin/index.js +7 -87
- package/dist/esm/src/dream/LeftJoinLoadBuilder.js +1 -1
- package/dist/esm/src/dream/LoadBuilder.js +1 -1
- package/dist/esm/src/dream/Query.js +46 -1417
- package/dist/esm/src/dream/QueryDriver/Base.js +308 -0
- package/dist/esm/src/dream/QueryDriver/Kysely.js +1768 -0
- package/dist/esm/src/dream/QueryDriver/Postgres.js +3 -0
- package/dist/esm/src/dream/internal/saveDream.js +4 -17
- package/dist/types/src/bin/index.d.ts +0 -2
- package/dist/types/src/dream/Query.d.ts +27 -155
- package/dist/types/src/dream/QueryDriver/Base.d.ts +242 -0
- package/dist/types/src/dream/QueryDriver/Kysely.d.ts +354 -0
- package/dist/types/src/dream/QueryDriver/Postgres.d.ts +4 -0
- package/dist/types/src/dream/internal/executeDatabaseQuery.d.ts +2 -1
- package/dist/types/src/serializer/SerializerRenderer.d.ts +1 -1
- package/dist/types/src/serializer/builders/DreamSerializerBuilder.d.ts +6 -6
- package/dist/types/src/serializer/builders/ObjectSerializerBuilder.d.ts +5 -5
- package/dist/types/src/types/serializer.d.ts +1 -1
- package/dist/types/src/types/serializer.ts +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Benchmark.html +2 -2
- package/docs/classes/CalendarDate.html +2 -2
- package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
- package/docs/classes/Decorators.html +19 -19
- package/docs/classes/Dream.html +131 -131
- package/docs/classes/DreamApp.html +4 -4
- package/docs/classes/DreamBin.html +3 -4
- package/docs/classes/DreamCLI.html +4 -4
- package/docs/classes/DreamImporter.html +2 -2
- package/docs/classes/DreamLogos.html +2 -2
- package/docs/classes/DreamMigrationHelpers.html +7 -7
- package/docs/classes/DreamSerializerBuilder.html +2 -2
- package/docs/classes/DreamTransaction.html +2 -2
- package/docs/classes/Encrypt.html +2 -2
- package/docs/classes/Env.html +2 -2
- package/docs/classes/GlobalNameNotSet.html +3 -3
- package/docs/classes/NonLoadedAssociation.html +3 -3
- package/docs/classes/ObjectSerializerBuilder.html +2 -2
- package/docs/classes/Query.html +86 -57
- package/docs/classes/Range.html +2 -2
- package/docs/classes/RecordNotFound.html +3 -3
- package/docs/classes/ValidationError.html +3 -3
- package/docs/functions/DreamSerializer.html +1 -1
- package/docs/functions/ObjectSerializer.html +1 -1
- package/docs/functions/ReplicaSafe.html +1 -1
- package/docs/functions/STI.html +1 -1
- package/docs/functions/SoftDelete.html +1 -1
- package/docs/functions/camelize.html +1 -1
- package/docs/functions/capitalize.html +1 -1
- package/docs/functions/cloneDeepSafe.html +1 -1
- package/docs/functions/closeAllDbConnections.html +1 -1
- package/docs/functions/compact.html +1 -1
- package/docs/functions/dreamDbConnections.html +1 -1
- package/docs/functions/dreamPath.html +1 -1
- package/docs/functions/expandStiClasses.html +1 -1
- package/docs/functions/generateDream.html +1 -1
- package/docs/functions/globalClassNameFromFullyQualifiedModelName.html +1 -1
- package/docs/functions/groupBy.html +1 -1
- package/docs/functions/hyphenize.html +1 -1
- package/docs/functions/inferSerializerFromDreamOrViewModel.html +1 -1
- package/docs/functions/inferSerializersFromDreamClassOrViewModelClass.html +1 -1
- package/docs/functions/intersection.html +1 -1
- package/docs/functions/isDreamSerializer.html +1 -1
- package/docs/functions/isEmpty.html +1 -1
- package/docs/functions/loadRepl.html +1 -1
- package/docs/functions/lookupClassByGlobalName.html +1 -1
- package/docs/functions/normalizeUnicode.html +1 -1
- package/docs/functions/pascalize.html +1 -1
- package/docs/functions/pgErrorType.html +1 -1
- package/docs/functions/range-1.html +1 -1
- package/docs/functions/relativeDreamPath.html +1 -1
- package/docs/functions/round.html +1 -1
- package/docs/functions/serializerNameFromFullyQualifiedModelName.html +1 -1
- package/docs/functions/sharedPathPrefix.html +1 -1
- package/docs/functions/snakeify.html +1 -1
- package/docs/functions/sort.html +1 -1
- package/docs/functions/sortBy.html +1 -1
- package/docs/functions/sortObjectByKey.html +1 -1
- package/docs/functions/sortObjectByValue.html +1 -1
- package/docs/functions/standardizeFullyQualifiedModelName.html +1 -1
- package/docs/functions/uncapitalize.html +1 -1
- package/docs/functions/uniq.html +1 -1
- package/docs/functions/untypedDb.html +1 -1
- package/docs/functions/validateColumn.html +1 -1
- package/docs/functions/validateTable.html +1 -1
- package/docs/interfaces/BelongsToStatement.html +2 -2
- package/docs/interfaces/DecoratorContext.html +2 -2
- package/docs/interfaces/DreamAppInitOptions.html +2 -2
- package/docs/interfaces/DreamAppOpts.html +2 -2
- package/docs/interfaces/EncryptOptions.html +2 -2
- package/docs/interfaces/InternalAnyTypedSerializerRendersMany.html +2 -2
- package/docs/interfaces/InternalAnyTypedSerializerRendersOne.html +2 -2
- package/docs/interfaces/OpenapiDescription.html +2 -2
- package/docs/interfaces/OpenapiSchemaProperties.html +1 -1
- package/docs/interfaces/OpenapiSchemaPropertiesShorthand.html +1 -1
- package/docs/interfaces/OpenapiTypeFieldObject.html +1 -1
- package/docs/interfaces/SerializerRendererOpts.html +2 -2
- package/docs/types/Camelized.html +1 -1
- package/docs/types/CommonOpenapiSchemaObjectFields.html +1 -1
- package/docs/types/DateTime.html +1 -1
- package/docs/types/DbConnectionType.html +1 -1
- package/docs/types/DbTypes.html +1 -1
- package/docs/types/DreamAssociationMetadata.html +1 -1
- package/docs/types/DreamAttributes.html +1 -1
- package/docs/types/DreamClassColumn.html +1 -1
- package/docs/types/DreamColumn.html +1 -1
- package/docs/types/DreamColumnNames.html +1 -1
- package/docs/types/DreamLogLevel.html +1 -1
- package/docs/types/DreamLogger.html +1 -1
- package/docs/types/DreamModelSerializerType.html +1 -1
- package/docs/types/DreamOrViewModelClassSerializerKey.html +1 -1
- package/docs/types/DreamOrViewModelSerializerKey.html +1 -1
- package/docs/types/DreamParamSafeAttributes.html +1 -1
- package/docs/types/DreamParamSafeColumnNames.html +1 -1
- package/docs/types/DreamSerializable.html +1 -1
- package/docs/types/DreamSerializableArray.html +1 -1
- package/docs/types/DreamSerializerKey.html +1 -1
- package/docs/types/DreamSerializers.html +1 -1
- package/docs/types/DreamTableSchema.html +1 -1
- package/docs/types/DreamVirtualColumns.html +1 -1
- package/docs/types/EncryptAlgorithm.html +1 -1
- package/docs/types/HasManyStatement.html +1 -1
- package/docs/types/HasOneStatement.html +1 -1
- package/docs/types/Hyphenized.html +1 -1
- package/docs/types/IdType.html +1 -1
- package/docs/types/OpenapiAllTypes.html +1 -1
- package/docs/types/OpenapiFormats.html +1 -1
- package/docs/types/OpenapiNumberFormats.html +1 -1
- package/docs/types/OpenapiPrimitiveBaseTypes.html +1 -1
- package/docs/types/OpenapiPrimitiveTypes.html +1 -1
- package/docs/types/OpenapiSchemaArray.html +1 -1
- package/docs/types/OpenapiSchemaArrayShorthand.html +1 -1
- package/docs/types/OpenapiSchemaBase.html +1 -1
- package/docs/types/OpenapiSchemaBody.html +1 -1
- package/docs/types/OpenapiSchemaBodyShorthand.html +1 -1
- package/docs/types/OpenapiSchemaCommonFields.html +1 -1
- package/docs/types/OpenapiSchemaExpressionAllOf.html +1 -1
- package/docs/types/OpenapiSchemaExpressionAnyOf.html +1 -1
- package/docs/types/OpenapiSchemaExpressionOneOf.html +1 -1
- package/docs/types/OpenapiSchemaExpressionRef.html +1 -1
- package/docs/types/OpenapiSchemaExpressionRefSchemaShorthand.html +1 -1
- package/docs/types/OpenapiSchemaInteger.html +1 -1
- package/docs/types/OpenapiSchemaNull.html +1 -1
- package/docs/types/OpenapiSchemaNumber.html +1 -1
- package/docs/types/OpenapiSchemaObject.html +1 -1
- package/docs/types/OpenapiSchemaObjectAllOf.html +1 -1
- package/docs/types/OpenapiSchemaObjectAllOfShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectAnyOf.html +1 -1
- package/docs/types/OpenapiSchemaObjectAnyOfShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectBase.html +1 -1
- package/docs/types/OpenapiSchemaObjectBaseShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectOneOf.html +1 -1
- package/docs/types/OpenapiSchemaObjectOneOfShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectShorthand.html +1 -1
- package/docs/types/OpenapiSchemaPrimitiveGeneric.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionAllOf.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionOneOf.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionSerializableRef.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionSerializerRef.html +1 -1
- package/docs/types/OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
- package/docs/types/OpenapiSchemaString.html +1 -1
- package/docs/types/OpenapiShorthandAllTypes.html +1 -1
- package/docs/types/OpenapiShorthandPrimitiveBaseTypes.html +1 -1
- package/docs/types/OpenapiShorthandPrimitiveTypes.html +1 -1
- package/docs/types/OpenapiTypeField.html +1 -1
- package/docs/types/Pascalized.html +1 -1
- package/docs/types/PrimaryKeyType.html +1 -1
- package/docs/types/RoundingPrecision.html +1 -1
- package/docs/types/SerializerCasing.html +1 -1
- package/docs/types/SimpleObjectSerializerType.html +1 -1
- package/docs/types/Snakeified.html +1 -1
- package/docs/types/Timestamp.html +1 -1
- package/docs/types/UpdateableAssociationProperties.html +1 -1
- package/docs/types/UpdateableProperties.html +1 -1
- package/docs/types/ValidationType.html +1 -1
- package/docs/types/ViewModel.html +1 -1
- package/docs/types/ViewModelClass.html +1 -1
- package/docs/types/WhereStatementForDream.html +1 -1
- package/docs/types/WhereStatementForDreamClass.html +1 -1
- package/docs/variables/DateTime-1.html +1 -1
- package/docs/variables/DreamConst.html +1 -1
- package/docs/variables/TRIGRAM_OPERATORS.html +1 -1
- package/docs/variables/openapiPrimitiveTypes-1.html +1 -1
- package/docs/variables/openapiShorthandPrimitiveTypes-1.html +1 -1
- package/docs/variables/ops.html +1 -1
- package/docs/variables/primaryKeyTypes.html +1 -1
- package/package.json +2 -2
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import _db from '../../db/index.js';
|
|
2
1
|
import ValidationError from '../../errors/ValidationError.js';
|
|
3
2
|
import { DateTime } from '../../helpers/DateTime.js';
|
|
4
|
-
import namespaceColumn from '../../helpers/namespaceColumn.js';
|
|
5
3
|
import sqlAttributes from '../../helpers/sqlAttributes.js';
|
|
6
|
-
import
|
|
4
|
+
import Query from '../Query.js';
|
|
7
5
|
import runHooksFor from './runHooksFor.js';
|
|
8
6
|
export default async function saveDream(dream, txn = null, { skipHooks = false } = {}) {
|
|
9
|
-
const db = txn?.kyselyTransaction ?? _db('primary');
|
|
10
7
|
const alreadyPersisted = dream.isPersisted;
|
|
11
8
|
if (!skipHooks) {
|
|
12
9
|
if (alreadyPersisted)
|
|
@@ -22,26 +19,16 @@ export default async function saveDream(dream, txn = null, { skipHooks = false }
|
|
|
22
19
|
throw new ValidationError(dream['sanitizedConstructorName'], dream.errors);
|
|
23
20
|
if (alreadyPersisted && !dream.isDirty)
|
|
24
21
|
return dream;
|
|
25
|
-
let query;
|
|
26
22
|
const now = DateTime.now();
|
|
27
23
|
if (!alreadyPersisted && !dream.createdAt && dream.columns().has('createdAt'))
|
|
28
24
|
dream.createdAt = now;
|
|
29
25
|
if (!dream.dirtyAttributes().updatedAt && dream.columns().has('updatedAt'))
|
|
30
26
|
dream.updatedAt = now;
|
|
31
|
-
const
|
|
32
|
-
if (alreadyPersisted) {
|
|
33
|
-
query = db
|
|
34
|
-
.updateTable(dream.table)
|
|
35
|
-
.set(sqlifiedAttributes)
|
|
36
|
-
.where(namespaceColumn(dream.primaryKey, dream.table), '=', dream.primaryKeyValue);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
query = db.insertInto(dream.table).values(sqlifiedAttributes);
|
|
40
|
-
}
|
|
27
|
+
const hasUnsavedData = !!Object.keys(sqlAttributes(dream)).length;
|
|
41
28
|
// BeforeSave/Update actions may clear all the data that we intended to save, leaving us with
|
|
42
29
|
// an invalid update command. The Sortable decorator is an example of this.
|
|
43
|
-
if (!alreadyPersisted ||
|
|
44
|
-
const data = await
|
|
30
|
+
if (!alreadyPersisted || hasUnsavedData) {
|
|
31
|
+
const data = await Query.dbDriverClass().saveDream(dream, txn);
|
|
45
32
|
dream['isPersisted'] = true;
|
|
46
33
|
dream.setAttributes(data);
|
|
47
34
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export default class DreamBin {
|
|
2
2
|
static sync(onSync: () => Promise<void> | void): Promise<void>;
|
|
3
|
-
static buildDreamSchema(): Promise<void>;
|
|
4
3
|
static dbCreate(): Promise<void>;
|
|
5
4
|
static dbDrop(): Promise<void>;
|
|
6
5
|
static dbMigrate(): Promise<void>;
|
|
@@ -16,5 +15,4 @@ export default class DreamBin {
|
|
|
16
15
|
}): Promise<void>;
|
|
17
16
|
static generateMigration(migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
18
17
|
private static buildDocs;
|
|
19
|
-
private static duplicateDatabase;
|
|
20
18
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { DeleteQueryBuilder, SelectQueryBuilder, UpdateQueryBuilder } from 'kysely';
|
|
2
|
-
import ConnectedToDB from '../db/ConnectedToDB.js';
|
|
3
2
|
import Dream from '../Dream.js';
|
|
4
3
|
import { ColumnNamesAccountingForJoinedAssociations, LimitStatement, OffsetStatement, OrderQueryStatement, PassthroughOnClause, WhereStatement, WhereStatementForJoinedAssociation } from '../types/associations/shared.js';
|
|
5
4
|
import { DbConnectionType } from '../types/db.js';
|
|
6
|
-
import { AllDefaultScopeNames, DefaultScopeName, DreamColumnNames, DreamTableSchema, OrderDir, PassthroughColumnNames, PluckEachArgs, PrimaryKeyForFind, RelaxedJoinAndStatement, RelaxedJoinStatement, RelaxedPreloadOnStatement, RelaxedPreloadStatement, TableColumnNames, TableOrAssociationName } from '../types/dream.js';
|
|
5
|
+
import { AllDefaultScopeNames, DefaultScopeName, DreamColumnNames, DreamConstructorType, DreamTableSchema, OrderDir, PassthroughColumnNames, PluckEachArgs, PrimaryKeyForFind, RelaxedJoinAndStatement, RelaxedJoinStatement, RelaxedPreloadOnStatement, RelaxedPreloadStatement, TableColumnNames, TableOrAssociationName } from '../types/dream.js';
|
|
7
6
|
import { DefaultQueryTypeOptions, ExtendQueryType, NamespacedOrBaseModelColumnTypes, PaginatedDreamQueryOptions, PaginatedDreamQueryResult, QueryToKyselyDBType, QueryToKyselyTableNamesType } from '../types/query.js';
|
|
8
7
|
import { JoinedAssociation, JoinedAssociationsTypeFromAssociations, QueryTypeOptions, VariadicJoinsArgs, VariadicLeftJoinLoadArgs, VariadicLoadArgs } from '../types/variadic.js';
|
|
9
8
|
import DreamTransaction from './DreamTransaction.js';
|
|
10
|
-
|
|
9
|
+
import PostgresQueryDriver from './QueryDriver/Postgres.js';
|
|
10
|
+
export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Readonly<QueryTypeOptions> = DefaultQueryTypeOptions<DreamInstance>> {
|
|
11
11
|
/**
|
|
12
12
|
* @internal
|
|
13
13
|
*
|
|
@@ -182,6 +182,16 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
182
182
|
* baseSelectQuery.
|
|
183
183
|
*/
|
|
184
184
|
private baseSelectQuery;
|
|
185
|
+
private originalOpts;
|
|
186
|
+
protected readonly dreamClass: DreamConstructorType<DreamInstance>;
|
|
187
|
+
readonly dreamInstance: DreamInstance;
|
|
188
|
+
protected connectionOverride: DbConnectionType | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* @internal
|
|
191
|
+
*
|
|
192
|
+
* stores the Dream models joined in this Query instance
|
|
193
|
+
*/
|
|
194
|
+
protected readonly innerJoinDreamClasses: readonly (typeof Dream)[];
|
|
185
195
|
constructor(dreamInstance: DreamInstance, opts?: QueryOpts<DreamInstance, DreamColumnNames<DreamInstance>>);
|
|
186
196
|
/**
|
|
187
197
|
* Returns true. Useful for distinguishing Query instances
|
|
@@ -190,14 +200,6 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
190
200
|
* @returns true
|
|
191
201
|
*/
|
|
192
202
|
get isDreamQuery(): boolean;
|
|
193
|
-
/**
|
|
194
|
-
* @internal
|
|
195
|
-
*
|
|
196
|
-
* Used for applying preload and load statements
|
|
197
|
-
*
|
|
198
|
-
* @returns An associated Query
|
|
199
|
-
*/
|
|
200
|
-
private dreamClassQueryWithScopeBypasses;
|
|
201
203
|
/**
|
|
202
204
|
* @internal
|
|
203
205
|
*
|
|
@@ -373,38 +375,6 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
373
375
|
*
|
|
374
376
|
*/
|
|
375
377
|
private fleshOutJoinStatements;
|
|
376
|
-
/**
|
|
377
|
-
* @internal
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*/
|
|
381
|
-
private associationNamesToDreamClassesMap;
|
|
382
|
-
/**
|
|
383
|
-
* @internal
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*/
|
|
387
|
-
private associationNamesToAssociationsMap;
|
|
388
|
-
/**
|
|
389
|
-
* @internal
|
|
390
|
-
*/
|
|
391
|
-
private associationNamesToAssociationDataAndDreamClassesMap;
|
|
392
|
-
/**
|
|
393
|
-
* @internal
|
|
394
|
-
*/
|
|
395
|
-
private throughAssociationDetails;
|
|
396
|
-
/**
|
|
397
|
-
* @internal
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
*/
|
|
401
|
-
private joinStatementsToDreamClassesMap;
|
|
402
|
-
/**
|
|
403
|
-
* @internal
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
*/
|
|
407
|
-
private joinStatementsToAssociationsMap;
|
|
408
378
|
/**
|
|
409
379
|
* @internal
|
|
410
380
|
*
|
|
@@ -718,21 +688,6 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
718
688
|
* @returns the min value of the specified column for this Query
|
|
719
689
|
*/
|
|
720
690
|
min<Q extends Query<DreamInstance, QueryTypeOpts>, DB extends DreamInstance['DB'], ColumnName extends ColumnNamesAccountingForJoinedAssociations<Q['queryTypeOpts']['joinedAssociations'], DB, QueryTypeOpts['rootTableName'], QueryTypeOpts['rootTableAlias']>, ReturnType extends NamespacedOrBaseModelColumnTypes<[ColumnName], Q, DreamInstance>[0]>(columnName: ColumnName): Promise<ReturnType>;
|
|
721
|
-
/**
|
|
722
|
-
* @internal
|
|
723
|
-
*
|
|
724
|
-
* Runs the query and extracts plucked values
|
|
725
|
-
*
|
|
726
|
-
* @returns An array of plucked values
|
|
727
|
-
*/
|
|
728
|
-
private executePluck;
|
|
729
|
-
/**
|
|
730
|
-
* @internal
|
|
731
|
-
*
|
|
732
|
-
*/
|
|
733
|
-
private executeJoinLoad;
|
|
734
|
-
private fleshOutJoinLoadExecutionResults;
|
|
735
|
-
private hydratePreloadedThroughColumns;
|
|
736
691
|
/**
|
|
737
692
|
* Plucks the provided fields from the given dream class table
|
|
738
693
|
*
|
|
@@ -787,6 +742,20 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
787
742
|
all(options?: {
|
|
788
743
|
columns?: DreamColumnNames<DreamInstance>[];
|
|
789
744
|
}): Promise<DreamInstance[]>;
|
|
745
|
+
static dbDriverClass<T extends Dream>(): {
|
|
746
|
+
new (query: Query<T, any>): PostgresQueryDriver<T>;
|
|
747
|
+
migrate(): Promise<void>;
|
|
748
|
+
rollback(opts: {
|
|
749
|
+
steps: number;
|
|
750
|
+
}): Promise<void>;
|
|
751
|
+
generateMigration(migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
752
|
+
sync(onSync: () => Promise<void> | void): Promise<void>;
|
|
753
|
+
dbCreate(): Promise<void>;
|
|
754
|
+
dbDrop(): Promise<void>;
|
|
755
|
+
saveDream(dream: Dream, txn?: DreamTransaction<Dream> | null): Promise<any>;
|
|
756
|
+
duplicateDatabase(): Promise<void>;
|
|
757
|
+
};
|
|
758
|
+
dbDriverInstance(query?: Query<DreamInstance, any>): PostgresQueryDriver<DreamInstance>;
|
|
790
759
|
/**
|
|
791
760
|
* Paginates the results of your query, accepting a pageSize and page argument,
|
|
792
761
|
* which it uses to segment your query into pages, leveraging limit and offset
|
|
@@ -1016,103 +985,6 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
1016
985
|
skipHooks?: boolean;
|
|
1017
986
|
}): Promise<number>;
|
|
1018
987
|
private updateWithoutCallingModelHooks;
|
|
1019
|
-
/**
|
|
1020
|
-
* @internal
|
|
1021
|
-
*
|
|
1022
|
-
* Used for applying first and last queries
|
|
1023
|
-
*
|
|
1024
|
-
* @returns A dream instance or null
|
|
1025
|
-
*/
|
|
1026
|
-
private takeOne;
|
|
1027
|
-
/**
|
|
1028
|
-
* @internal
|
|
1029
|
-
*
|
|
1030
|
-
* Used to hydrate dreams with the provided associations
|
|
1031
|
-
*/
|
|
1032
|
-
private hydrateAssociation;
|
|
1033
|
-
/**
|
|
1034
|
-
* @internal
|
|
1035
|
-
*
|
|
1036
|
-
* Used to bridge through associations
|
|
1037
|
-
*/
|
|
1038
|
-
private followThroughAssociation;
|
|
1039
|
-
/**
|
|
1040
|
-
* @internal
|
|
1041
|
-
*
|
|
1042
|
-
* Polymorphic BelongsTo. Since polymorphic associations may point to multiple tables,
|
|
1043
|
-
* preload by loading each target class separately.
|
|
1044
|
-
*
|
|
1045
|
-
* Used to preload polymorphic belongs to associations
|
|
1046
|
-
*/
|
|
1047
|
-
private preloadPolymorphicBelongsTo;
|
|
1048
|
-
private preloadPolymorphicAssociationModel;
|
|
1049
|
-
/**
|
|
1050
|
-
* @internal
|
|
1051
|
-
*
|
|
1052
|
-
* Applies a preload statement
|
|
1053
|
-
*/
|
|
1054
|
-
private applyOnePreload;
|
|
1055
|
-
/**
|
|
1056
|
-
* @internal
|
|
1057
|
-
*
|
|
1058
|
-
* Used by loadBuider
|
|
1059
|
-
*/
|
|
1060
|
-
private hydratePreload;
|
|
1061
|
-
/**
|
|
1062
|
-
* @internal
|
|
1063
|
-
*
|
|
1064
|
-
* Applies a preload statement
|
|
1065
|
-
*/
|
|
1066
|
-
private applyPreload;
|
|
1067
|
-
/**
|
|
1068
|
-
* @internal
|
|
1069
|
-
*
|
|
1070
|
-
* retrieves on statements that can be applied to a preload
|
|
1071
|
-
*/
|
|
1072
|
-
private applyablePreloadOnStatements;
|
|
1073
|
-
private conditionallyApplyDefaultScopes;
|
|
1074
|
-
/**
|
|
1075
|
-
* Each association in the chain is pushed onto `throughAssociations`
|
|
1076
|
-
* and `applyOneJoin` is recursively called. The trick is that the
|
|
1077
|
-
* through associations don't get written into the SQL; they
|
|
1078
|
-
* locate the next association we need to build into the SQL,
|
|
1079
|
-
* which is only run by the association that started the `through`
|
|
1080
|
-
* chain. The final association at the end of the `through` chain _is_
|
|
1081
|
-
* written into the SQL as a full association, but the modifications from
|
|
1082
|
-
* the `through` association are only added when the recursion returns
|
|
1083
|
-
* back to the association that kicked off the through associations.
|
|
1084
|
-
*/
|
|
1085
|
-
private joinsBridgeThroughAssociations;
|
|
1086
|
-
private applyOneJoin;
|
|
1087
|
-
private applyAssociationAndStatementsToJoinStatement;
|
|
1088
|
-
private conditionallyApplyDefaultScopesDependentOnAssociation;
|
|
1089
|
-
private distinctColumnNameForAssociation;
|
|
1090
|
-
private recursivelyJoin;
|
|
1091
|
-
private throwUnlessAllRequiredWhereClausesProvided;
|
|
1092
|
-
private applyOrderStatementForAssociation;
|
|
1093
|
-
private inArrayWithNull_or_notInArrayWithoutNull_ExpressionBuilder;
|
|
1094
|
-
private inArrayWithoutNullExpressionBuilder;
|
|
1095
|
-
private notInArrayWithNullExpressionBuilder;
|
|
1096
|
-
private whereStatementToExpressionWrapper;
|
|
1097
|
-
private dreamWhereStatementToExpressionBuilderParts;
|
|
1098
|
-
private applyJoinAndStatement;
|
|
1099
|
-
private _applyJoinAndStatements;
|
|
1100
|
-
private _applyJoinAndAnyStatements;
|
|
1101
|
-
private joinAndStatementToExpressionWrapper;
|
|
1102
|
-
private buildCommon;
|
|
1103
|
-
private checkForQueryViolations;
|
|
1104
|
-
private aliasWhereStatements;
|
|
1105
|
-
private aliasWhereStatement;
|
|
1106
|
-
private rawifiedSelfOnClause;
|
|
1107
|
-
private buildDelete;
|
|
1108
|
-
private buildSelect;
|
|
1109
|
-
private columnsWithRequiredLoadColumns;
|
|
1110
|
-
private buildUpdate;
|
|
1111
|
-
private attachLimitAndOrderStatementsToNonSelectQuery;
|
|
1112
|
-
private get hasSimilarityClauses();
|
|
1113
|
-
private similarityStatementBuilder;
|
|
1114
|
-
private conditionallyAttachSimilarityColumnsToSelect;
|
|
1115
|
-
private conditionallyAttachSimilarityColumnsToUpdate;
|
|
1116
988
|
private invertOrder;
|
|
1117
989
|
}
|
|
1118
990
|
export interface QueryOpts<DreamInstance extends Dream, ColumnType extends DreamColumnNames<DreamInstance> = DreamColumnNames<DreamInstance>, Schema extends DreamInstance['schema'] = DreamInstance['schema'], DB extends DreamInstance['DB'] = DreamInstance['DB'], PassthroughColumns extends PassthroughColumnNames<DreamInstance> = PassthroughColumnNames<DreamInstance>> {
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { DeleteQueryBuilder, SelectQueryBuilder, UpdateQueryBuilder } from 'kysely';
|
|
2
|
+
import Dream from '../../Dream.js';
|
|
3
|
+
import { AssociationStatement } from '../../types/associations/shared.js';
|
|
4
|
+
import { DreamColumnNames, DreamConstructorType, DreamTableSchema } from '../../types/dream.js';
|
|
5
|
+
import { PreloadedDreamsAndWhatTheyPointTo, QueryToKyselyDBType, QueryToKyselyTableNamesType } from '../../types/query.js';
|
|
6
|
+
import DreamTransaction from '../DreamTransaction.js';
|
|
7
|
+
import Query from '../Query.js';
|
|
8
|
+
import PostgresQueryDriver from './Postgres.js';
|
|
9
|
+
import { DbConnectionType } from '../../types/db.js';
|
|
10
|
+
export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
11
|
+
query: Query<DreamInstance, any>;
|
|
12
|
+
protected readonly dreamClass: DreamConstructorType<DreamInstance>;
|
|
13
|
+
protected readonly dreamInstance: DreamInstance;
|
|
14
|
+
protected dreamTransaction: DreamTransaction<Dream> | null;
|
|
15
|
+
protected connectionOverride: DbConnectionType | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*
|
|
19
|
+
* stores the Dream models joined in this Query instance
|
|
20
|
+
*/
|
|
21
|
+
protected readonly innerJoinDreamClasses: readonly (typeof Dream)[];
|
|
22
|
+
constructor(query: Query<DreamInstance, any>);
|
|
23
|
+
/**
|
|
24
|
+
* migrate the database. Must respond to the NODE_ENV value.
|
|
25
|
+
*/
|
|
26
|
+
static migrate(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* rollback the database. Must respond to the NODE_ENV value.
|
|
29
|
+
*/
|
|
30
|
+
static rollback(_: {
|
|
31
|
+
steps: number;
|
|
32
|
+
}): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* create the database. Must respond to the NODE_ENV value.
|
|
35
|
+
*/
|
|
36
|
+
static dbCreate(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* delete the database. Must respond to the NODE_ENV value.
|
|
39
|
+
*/
|
|
40
|
+
static dbDrop(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* This should build a new migration file in the migrations folder
|
|
43
|
+
* of your application. This will then need to be read and run
|
|
44
|
+
* whenever the `migrate` method is called. The filename should
|
|
45
|
+
* contain a timestamp at the front of the filename, so that it
|
|
46
|
+
* is sorted by date in the file tree, and, more importantly, so
|
|
47
|
+
* they can be run in order by your migration runner.
|
|
48
|
+
*/
|
|
49
|
+
static generateMigration(migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* defines the syncing behavior for dream and psychic,
|
|
52
|
+
* which is run whenever the `sync` command is called.
|
|
53
|
+
* This is an important step, and will be incredibly
|
|
54
|
+
* comlpex to override. You will need to do the following
|
|
55
|
+
* when overriding this method:
|
|
56
|
+
*
|
|
57
|
+
* 1. introspect the db and use it to generate a db.ts file in the
|
|
58
|
+
* same shape as the existing one. Currently, the process for generating
|
|
59
|
+
* this file is extremely complex and messy, and will be difficult
|
|
60
|
+
* to achieve.
|
|
61
|
+
* 2. generate a types/dream.ts file in the same shape as the existing
|
|
62
|
+
* one. This is normally done using `await new SchemaBuilder().build()`,
|
|
63
|
+
* but this will likely need to be overridden to tailor to your custom
|
|
64
|
+
* database engine.
|
|
65
|
+
*/
|
|
66
|
+
static sync(_: () => Promise<void> | void): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Converts the given dream class into a Kysely query, enabling
|
|
69
|
+
* you to build custom queries using the Kysely API
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* await User.query().toKysely('select').where('email', '=', 'how@yadoin').execute()
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param type - the type of Kysely query builder instance you would like to obtain
|
|
76
|
+
* @returns A Kysely query. Depending on the type passed, it will return either a SelectQueryBuilder, DeleteQueryBuilder, or an UpdateQueryBuilder
|
|
77
|
+
*/
|
|
78
|
+
toKysely<QueryType extends 'select' | 'delete' | 'update', DbType = QueryToKyselyDBType<Query<DreamInstance>>, TableNames = QueryToKyselyTableNamesType<Query<DreamInstance>>, ToKyselyReturnType = QueryType extends 'select' ? SelectQueryBuilder<DbType, TableNames & keyof DbType, unknown> : QueryType extends 'delete' ? DeleteQueryBuilder<DbType, TableNames & keyof DbType, unknown> : QueryType extends 'update' ? UpdateQueryBuilder<DbType, TableNames & keyof DbType, TableNames & keyof DbType, unknown> : never>(type: QueryType): ToKyselyReturnType;
|
|
79
|
+
/**
|
|
80
|
+
* @internal
|
|
81
|
+
*
|
|
82
|
+
* This method is used internally by a Query driver to
|
|
83
|
+
* take the result of a single row in a database, and
|
|
84
|
+
* turn that row into the provided dream instance.
|
|
85
|
+
*
|
|
86
|
+
* If needed, the return type can be overriden to
|
|
87
|
+
* explicitly define the resulting dream instance,
|
|
88
|
+
* in cases where a proper type for the dream class
|
|
89
|
+
* cannot be inferred, i.e.
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* this.dbResultToDreamInstance<typeof Dream, DreamInstance>(result, this.dreamClass)
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
dbResultToDreamInstance<DreamClass extends typeof Dream, RetType = InstanceType<DreamClass>>(result: any, dreamClass: typeof Dream): RetType;
|
|
96
|
+
/**
|
|
97
|
+
* @internal
|
|
98
|
+
*
|
|
99
|
+
* Used for applying first and last queries
|
|
100
|
+
*
|
|
101
|
+
* @returns A dream instance or null
|
|
102
|
+
*/
|
|
103
|
+
takeOne(this: PostgresQueryDriver<DreamInstance>): Promise<DreamInstance | null>;
|
|
104
|
+
/**
|
|
105
|
+
* Retrieves an array containing all records matching the Query.
|
|
106
|
+
* Be careful using this, since it will attempt to pull every
|
|
107
|
+
* record into memory at once. When querying might return a large
|
|
108
|
+
* number of records, consider using `.findEach`, which will pull
|
|
109
|
+
* the records in batches.
|
|
110
|
+
*
|
|
111
|
+
* ```ts
|
|
112
|
+
* await User.query().all()
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @returns an array of dreams
|
|
116
|
+
*/
|
|
117
|
+
takeAll(options?: {
|
|
118
|
+
columns?: DreamColumnNames<DreamInstance>[];
|
|
119
|
+
}): Promise<DreamInstance[]>;
|
|
120
|
+
/**
|
|
121
|
+
* Retrieves the max value of the specified column
|
|
122
|
+
* for this Query
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* await User.query().max('id')
|
|
126
|
+
* // 99
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @param columnName - a column name on the model
|
|
130
|
+
* @returns the max value of the specified column for this Query
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
max(columnName: string): Promise<any>;
|
|
134
|
+
/**
|
|
135
|
+
* Retrieves the min value of the specified column
|
|
136
|
+
* for this Query
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* await User.query().min('id')
|
|
140
|
+
* // 1
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @param columnName - a column name on the model
|
|
144
|
+
* @returns the min value of the specified column for this Query
|
|
145
|
+
*/
|
|
146
|
+
min(columnName: string): Promise<any>;
|
|
147
|
+
/**
|
|
148
|
+
* Retrieves the number of records in the database
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* await User.query().count()
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @returns The number of records in the database
|
|
155
|
+
*/
|
|
156
|
+
count(): Promise<number>;
|
|
157
|
+
/**
|
|
158
|
+
* @internal
|
|
159
|
+
*
|
|
160
|
+
* Runs the query and extracts plucked values
|
|
161
|
+
*
|
|
162
|
+
* @returns An array of plucked values
|
|
163
|
+
*/
|
|
164
|
+
pluck(...fields: DreamColumnNames<DreamInstance>[]): Promise<any[]>;
|
|
165
|
+
/**
|
|
166
|
+
* Returns a new Kysely SelectQueryBuilder instance to be used
|
|
167
|
+
* in a sub Query
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* const records = await User.where({
|
|
171
|
+
* id: Post.query().nestedSelect('userId'),
|
|
172
|
+
* }).all()
|
|
173
|
+
* // [User{id: 1}, ...]
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* @param selection - the column to use for your nested Query
|
|
177
|
+
* @returns A Kysely SelectQueryBuilder instance
|
|
178
|
+
*/
|
|
179
|
+
nestedSelect<SimpleFieldType extends keyof DreamColumnNames<DreamInstance>, PluckThroughFieldType>(this: PostgresQueryDriver<DreamInstance>, selection: SimpleFieldType | PluckThroughFieldType): void;
|
|
180
|
+
/**
|
|
181
|
+
* executes provided query instance as a deletion query.
|
|
182
|
+
* @returns the number of deleted rows
|
|
183
|
+
*/
|
|
184
|
+
delete(): Promise<number>;
|
|
185
|
+
/**
|
|
186
|
+
* executes provided query instance as an update query.
|
|
187
|
+
* @returns the number of updated rows
|
|
188
|
+
*/
|
|
189
|
+
update(attributes: DreamTableSchema<DreamInstance>): Promise<number>;
|
|
190
|
+
/**
|
|
191
|
+
* persists any unsaved changes to the database. If a transaction
|
|
192
|
+
* is provided as a second argument, it will use that transaction
|
|
193
|
+
* to encapsulate the persisting of the dream, as well as any
|
|
194
|
+
* subsequent model hooks that are fired.
|
|
195
|
+
*/
|
|
196
|
+
static saveDream(dream: Dream, txn?: DreamTransaction<Dream> | null): Promise<void>;
|
|
197
|
+
/**
|
|
198
|
+
* Returns the sql that would be executed by this Query
|
|
199
|
+
*
|
|
200
|
+
* ```ts
|
|
201
|
+
* User.where({ email: 'how@yadoin' }).sql()
|
|
202
|
+
* // {
|
|
203
|
+
* // query: {
|
|
204
|
+
* // kind: 'SelectQueryNode',
|
|
205
|
+
* // from: { kind: 'FromNode', froms: [Array] },
|
|
206
|
+
* // selections: [ [Object] ],
|
|
207
|
+
* // distinctOn: undefined,
|
|
208
|
+
* // joins: undefined,
|
|
209
|
+
* // groupBy: undefined,
|
|
210
|
+
* // orderBy: undefined,
|
|
211
|
+
* // where: { kind: 'WhereNode', where: [Object] },
|
|
212
|
+
* // frontModifiers: undefined,
|
|
213
|
+
* // endModifiers: undefined,
|
|
214
|
+
* // limit: undefined,
|
|
215
|
+
* // offset: undefined,
|
|
216
|
+
* // with: undefined,
|
|
217
|
+
* // having: undefined,
|
|
218
|
+
* // explain: undefined,
|
|
219
|
+
* // setOperations: undefined
|
|
220
|
+
* // },
|
|
221
|
+
* // sql: 'select "users".* from "users" where ("users"."email" = $1 and "users"."deleted_at" is null)',
|
|
222
|
+
* // parameters: [ 'how@yadoin' ]
|
|
223
|
+
* //}
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @returns An object representing the underlying sql statement
|
|
227
|
+
*
|
|
228
|
+
*/
|
|
229
|
+
sql(): void;
|
|
230
|
+
/**
|
|
231
|
+
* @internal
|
|
232
|
+
*
|
|
233
|
+
* Used to hydrate dreams with the provided associations
|
|
234
|
+
*/
|
|
235
|
+
hydrateAssociation(dreams: Dream[], association: AssociationStatement, preloadedDreamsAndWhatTheyPointTo: PreloadedDreamsAndWhatTheyPointTo[]): void;
|
|
236
|
+
/**
|
|
237
|
+
* @internal
|
|
238
|
+
*
|
|
239
|
+
* Used by loadBuider
|
|
240
|
+
*/
|
|
241
|
+
hydratePreload(this: QueryDriverBase<DreamInstance>, dream: Dream): Promise<void>;
|
|
242
|
+
}
|