@zenstackhq/runtime 3.0.0-alpha.16 → 3.0.0-alpha.18

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.
@@ -561,6 +561,10 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
561
561
  * Whether the dialect supports DISTINCT ON.
562
562
  */
563
563
  abstract get supportsDistinctOn(): boolean;
564
+ /**
565
+ * Whether the dialect support inserting with `DEFAULT` as field value.
566
+ */
567
+ abstract get supportInsertWithDefault(): boolean;
564
568
  }
565
569
 
566
570
  type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
@@ -796,7 +800,7 @@ type ClientContract<Schema extends SchemaDef> = {
796
800
  * Executes a prepared raw query and returns the number of affected rows.
797
801
  * @example
798
802
  * ```
799
- * const result = await client.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
803
+ * const result = await db.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
800
804
  * ```
801
805
  */
802
806
  $executeRaw(query: TemplateStringsArray, ...values: any[]): ZenStackPromise<Schema, number>;
@@ -805,7 +809,7 @@ type ClientContract<Schema extends SchemaDef> = {
805
809
  * This method is susceptible to SQL injections.
806
810
  * @example
807
811
  * ```
808
- * const result = await client.$executeRawUnsafe('UPDATE User SET cool = $1 WHERE email = $2 ;', true, 'user@email.com')
812
+ * const result = await db.$executeRawUnsafe('UPDATE User SET cool = $1 WHERE email = $2 ;', true, 'user@email.com')
809
813
  * ```
810
814
  */
811
815
  $executeRawUnsafe(query: string, ...values: any[]): ZenStackPromise<Schema, number>;
@@ -813,7 +817,7 @@ type ClientContract<Schema extends SchemaDef> = {
813
817
  * Performs a prepared raw query and returns the `SELECT` data.
814
818
  * @example
815
819
  * ```
816
- * const result = await client.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'user@email.com'};`
820
+ * const result = await db.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'user@email.com'};`
817
821
  * ```
818
822
  */
819
823
  $queryRaw<T = unknown>(query: TemplateStringsArray, ...values: any[]): ZenStackPromise<Schema, T>;
@@ -822,7 +826,7 @@ type ClientContract<Schema extends SchemaDef> = {
822
826
  * This method is susceptible to SQL injections.
823
827
  * @example
824
828
  * ```
825
- * const result = await client.$queryRawUnsafe('SELECT * FROM User WHERE id = $1 OR email = $2;', 1, 'user@email.com')
829
+ * const result = await db.$queryRawUnsafe('SELECT * FROM User WHERE id = $1 OR email = $2;', 1, 'user@email.com')
826
830
  * ```
827
831
  */
828
832
  $queryRawUnsafe<T = unknown>(query: string, ...values: any[]): ZenStackPromise<Schema, T>;
@@ -920,17 +924,17 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
920
924
  * @example
921
925
  * ```ts
922
926
  * // find all users and return all scalar fields
923
- * await client.user.findMany();
927
+ * await db.user.findMany();
924
928
  *
925
929
  * // find all users with name 'Alex'
926
- * await client.user.findMany({
930
+ * await db.user.findMany({
927
931
  * where: {
928
932
  * name: 'Alex'
929
933
  * }
930
934
  * });
931
935
  *
932
936
  * // select fields
933
- * await client.user.findMany({
937
+ * await db.user.findMany({
934
938
  * select: {
935
939
  * name: true,
936
940
  * email: true,
@@ -938,21 +942,21 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
938
942
  * }); // result: `Array<{ name: string, email: string }>`
939
943
  *
940
944
  * // omit fields
941
- * await client.user.findMany({
945
+ * await db.user.findMany({
942
946
  * omit: {
943
947
  * name: true,
944
948
  * }
945
949
  * }); // result: `Array<{ id: number; email: string; ... }>`
946
950
  *
947
951
  * // include relations (and all scalar fields)
948
- * await client.user.findMany({
952
+ * await db.user.findMany({
949
953
  * include: {
950
954
  * posts: true,
951
955
  * }
952
956
  * }); // result: `Array<{ ...; posts: Post[] }>`
953
957
  *
954
958
  * // include relations with filter
955
- * await client.user.findMany({
959
+ * await db.user.findMany({
956
960
  * include: {
957
961
  * posts: {
958
962
  * where: {
@@ -963,14 +967,14 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
963
967
  * });
964
968
  *
965
969
  * // pagination and sorting
966
- * await client.user.findMany({
970
+ * await db.user.findMany({
967
971
  * skip: 10,
968
972
  * take: 10,
969
973
  * orderBy: [{ name: 'asc' }, { email: 'desc' }],
970
974
  * });
971
975
  *
972
976
  * // pagination with cursor (https://www.prisma.io/docs/orm/prisma-client/queries/pagination#cursor-based-pagination)
973
- * await client.user.findMany({
977
+ * await db.user.findMany({
974
978
  * cursor: { id: 10 },
975
979
  * skip: 1,
976
980
  * take: 10,
@@ -978,17 +982,17 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
978
982
  * });
979
983
  *
980
984
  * // distinct
981
- * await client.user.findMany({
985
+ * await db.user.findMany({
982
986
  * distinct: ['name']
983
987
  * });
984
988
  *
985
989
  * // count all relations
986
- * await client.user.findMany({
990
+ * await db.user.findMany({
987
991
  * _count: true,
988
992
  * }); // result: `{ _count: { posts: number; ... } }`
989
993
  *
990
994
  * // count selected relations
991
- * await client.user.findMany({
995
+ * await db.user.findMany({
992
996
  * _count: { select: { posts: true } },
993
997
  * }); // result: `{ _count: { posts: number } }`
994
998
  * ```
@@ -1000,14 +1004,14 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1000
1004
  * @returns a single entity or null if not found
1001
1005
  * @see {@link findMany}
1002
1006
  */
1003
- findUnique<T extends FindUniqueArgs<Schema, Model>>(args?: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;
1007
+ findUnique<T extends FindUniqueArgs<Schema, Model>>(args: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;
1004
1008
  /**
1005
1009
  * Returns a uniquely identified entity or throws `NotFoundError` if not found.
1006
1010
  * @param args - query args
1007
1011
  * @returns a single entity
1008
1012
  * @see {@link findMany}
1009
1013
  */
1010
- findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model>>(args?: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
1014
+ findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model>>(args: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
1011
1015
  /**
1012
1016
  * Returns the first entity.
1013
1017
  * @param args - query args
@@ -1030,12 +1034,12 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1030
1034
  * @example
1031
1035
  * ```ts
1032
1036
  * // simple create
1033
- * await client.user.create({
1037
+ * await db.user.create({
1034
1038
  * data: { name: 'Alex', email: 'alex@zenstack.dev' }
1035
1039
  * });
1036
1040
  *
1037
1041
  * // nested create with relation
1038
- * await client.user.create({
1042
+ * await db.user.create({
1039
1043
  * data: {
1040
1044
  * email: 'alex@zenstack.dev',
1041
1045
  * posts: { create: { title: 'Hello World' } }
@@ -1044,7 +1048,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1044
1048
  *
1045
1049
  * // you can use `select`, `omit`, and `include` to control
1046
1050
  * // the fields returned by the query, as with `findMany`
1047
- * await client.user.create({
1051
+ * await db.user.create({
1048
1052
  * data: {
1049
1053
  * email: 'alex@zenstack.dev',
1050
1054
  * posts: { create: { title: 'Hello World' } }
@@ -1053,7 +1057,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1053
1057
  * }); // result: `{ id: number; posts: Post[] }`
1054
1058
  *
1055
1059
  * // connect relations
1056
- * await client.user.create({
1060
+ * await db.user.create({
1057
1061
  * data: {
1058
1062
  * email: 'alex@zenstack.dev',
1059
1063
  * posts: { connect: { id: 1 } }
@@ -1061,7 +1065,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1061
1065
  * });
1062
1066
  *
1063
1067
  * // connect relations, and create if not found
1064
- * await client.user.create({
1068
+ * await db.user.create({
1065
1069
  * data: {
1066
1070
  * email: 'alex@zenstack.dev',
1067
1071
  * posts: {
@@ -1083,7 +1087,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1083
1087
  * @example
1084
1088
  * ```ts
1085
1089
  * // create multiple entities
1086
- * await client.user.createMany({
1090
+ * await db.user.createMany({
1087
1091
  * data: [
1088
1092
  * { name: 'Alex', email: 'alex@zenstack.dev' },
1089
1093
  * { name: 'John', email: 'john@zenstack.dev' }
@@ -1091,7 +1095,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1091
1095
  * });
1092
1096
  *
1093
1097
  * // skip items that cause unique constraint violation
1094
- * await client.user.createMany({
1098
+ * await db.user.createMany({
1095
1099
  * data: [
1096
1100
  * { name: 'Alex', email: 'alex@zenstack.dev' },
1097
1101
  * { name: 'John', email: 'john@zenstack.dev' }
@@ -1110,7 +1114,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1110
1114
  * @example
1111
1115
  * ```ts
1112
1116
  * // create multiple entities and return selected fields
1113
- * await client.user.createManyAndReturn({
1117
+ * await db.user.createManyAndReturn({
1114
1118
  * data: [
1115
1119
  * { name: 'Alex', email: 'alex@zenstack.dev' },
1116
1120
  * { name: 'John', email: 'john@zenstack.dev' }
@@ -1129,19 +1133,19 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1129
1133
  * @example
1130
1134
  * ```ts
1131
1135
  * // update fields
1132
- * await client.user.update({
1136
+ * await db.user.update({
1133
1137
  * where: { id: 1 },
1134
1138
  * data: { name: 'Alex' }
1135
1139
  * });
1136
1140
  *
1137
1141
  * // connect a relation
1138
- * await client.user.update({
1142
+ * await db.user.update({
1139
1143
  * where: { id: 1 },
1140
1144
  * data: { posts: { connect: { id: 1 } } }
1141
1145
  * });
1142
1146
  *
1143
1147
  * // connect relation, and create if not found
1144
- * await client.user.update({
1148
+ * await db.user.update({
1145
1149
  * where: { id: 1 },
1146
1150
  * data: {
1147
1151
  * posts: {
@@ -1154,7 +1158,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1154
1158
  * });
1155
1159
  *
1156
1160
  * // create many related entities (only available for one-to-many relations)
1157
- * await client.user.update({
1161
+ * await db.user.update({
1158
1162
  * where: { id: 1 },
1159
1163
  * data: {
1160
1164
  * posts: {
@@ -1166,19 +1170,19 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1166
1170
  * });
1167
1171
  *
1168
1172
  * // disconnect a one-to-many relation
1169
- * await client.user.update({
1173
+ * await db.user.update({
1170
1174
  * where: { id: 1 },
1171
1175
  * data: { posts: { disconnect: { id: 1 } } }
1172
1176
  * });
1173
1177
  *
1174
1178
  * // disconnect a one-to-one relation
1175
- * await client.user.update({
1179
+ * await db.user.update({
1176
1180
  * where: { id: 1 },
1177
1181
  * data: { profile: { disconnect: true } }
1178
1182
  * });
1179
1183
  *
1180
1184
  * // replace a relation (only available for one-to-many relations)
1181
- * await client.user.update({
1185
+ * await db.user.update({
1182
1186
  * where: { id: 1 },
1183
1187
  * data: {
1184
1188
  * posts: {
@@ -1188,7 +1192,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1188
1192
  * });
1189
1193
  *
1190
1194
  * // update a relation
1191
- * await client.user.update({
1195
+ * await db.user.update({
1192
1196
  * where: { id: 1 },
1193
1197
  * data: {
1194
1198
  * posts: {
@@ -1198,7 +1202,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1198
1202
  * });
1199
1203
  *
1200
1204
  * // upsert a relation
1201
- * await client.user.update({
1205
+ * await db.user.update({
1202
1206
  * where: { id: 1 },
1203
1207
  * data: {
1204
1208
  * posts: {
@@ -1212,7 +1216,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1212
1216
  * });
1213
1217
  *
1214
1218
  * // update many related entities (only available for one-to-many relations)
1215
- * await client.user.update({
1219
+ * await db.user.update({
1216
1220
  * where: { id: 1 },
1217
1221
  * data: {
1218
1222
  * posts: {
@@ -1225,13 +1229,13 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1225
1229
  * });
1226
1230
  *
1227
1231
  * // delete a one-to-many relation
1228
- * await client.user.update({
1232
+ * await db.user.update({
1229
1233
  * where: { id: 1 },
1230
1234
  * data: { posts: { delete: { id: 1 } } }
1231
1235
  * });
1232
1236
  *
1233
1237
  * // delete a one-to-one relation
1234
- * await client.user.update({
1238
+ * await db.user.update({
1235
1239
  * where: { id: 1 },
1236
1240
  * data: { profile: { delete: true } }
1237
1241
  * });
@@ -1246,13 +1250,13 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1246
1250
  * @example
1247
1251
  * ```ts
1248
1252
  * // update many entities
1249
- * await client.user.updateMany({
1253
+ * await db.user.updateMany({
1250
1254
  * where: { email: { endsWith: '@zenstack.dev' } },
1251
1255
  * data: { role: 'ADMIN' }
1252
1256
  * });
1253
1257
  *
1254
1258
  * // limit the number of updated entities
1255
- * await client.user.updateMany({
1259
+ * await db.user.updateMany({
1256
1260
  * where: { email: { endsWith: '@zenstack.dev' } },
1257
1261
  * data: { role: 'ADMIN' },
1258
1262
  * limit: 10
@@ -1267,14 +1271,14 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1267
1271
  * @example
1268
1272
  * ```ts
1269
1273
  * // update many entities and return selected fields
1270
- * await client.user.updateManyAndReturn({
1274
+ * await db.user.updateManyAndReturn({
1271
1275
  * where: { email: { endsWith: '@zenstack.dev' } },
1272
1276
  * data: { role: 'ADMIN' },
1273
1277
  * select: { id: true, email: true }
1274
1278
  * }); // result: `Array<{ id: string; email: string }>`
1275
1279
  *
1276
1280
  * // limit the number of updated entities
1277
- * await client.user.updateManyAndReturn({
1281
+ * await db.user.updateManyAndReturn({
1278
1282
  * where: { email: { endsWith: '@zenstack.dev' } },
1279
1283
  * data: { role: 'ADMIN' },
1280
1284
  * limit: 10
@@ -1290,7 +1294,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1290
1294
  * @example
1291
1295
  * ```ts
1292
1296
  * // upsert an entity
1293
- * await client.user.upsert({
1297
+ * await db.user.upsert({
1294
1298
  * // `where` clause is used to find the entity
1295
1299
  * where: { id: 1 },
1296
1300
  * // `create` clause is used if the entity is not found
@@ -1311,12 +1315,12 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1311
1315
  * @example
1312
1316
  * ```ts
1313
1317
  * // delete an entity
1314
- * await client.user.delete({
1318
+ * await db.user.delete({
1315
1319
  * where: { id: 1 }
1316
1320
  * });
1317
1321
  *
1318
1322
  * // delete an entity and return selected fields
1319
- * await client.user.delete({
1323
+ * await db.user.delete({
1320
1324
  * where: { id: 1 },
1321
1325
  * select: { id: true, email: true }
1322
1326
  * }); // result: `{ id: string; email: string }`
@@ -1331,12 +1335,12 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1331
1335
  * @example
1332
1336
  * ```ts
1333
1337
  * // delete many entities
1334
- * await client.user.deleteMany({
1338
+ * await db.user.deleteMany({
1335
1339
  * where: { email: { endsWith: '@zenstack.dev' } }
1336
1340
  * });
1337
1341
  *
1338
1342
  * // limit the number of deleted entities
1339
- * await client.user.deleteMany({
1343
+ * await db.user.deleteMany({
1340
1344
  * where: { email: { endsWith: '@zenstack.dev' } },
1341
1345
  * limit: 10
1342
1346
  * });
@@ -1351,13 +1355,13 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1351
1355
  * @example
1352
1356
  * ```ts
1353
1357
  * // count all
1354
- * await client.user.count();
1358
+ * await db.user.count();
1355
1359
  *
1356
1360
  * // count with a filter
1357
- * await client.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
1361
+ * await db.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
1358
1362
  *
1359
1363
  * // count rows and field values
1360
- * await client.user.count({
1364
+ * await db.user.count({
1361
1365
  * select: { _all: true, email: true }
1362
1366
  * }); // result: `{ _all: number, email: number }`
1363
1367
  */
@@ -1370,7 +1374,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1370
1374
  * @example
1371
1375
  * ```ts
1372
1376
  * // aggregate rows
1373
- * await client.profile.aggregate({
1377
+ * await db.profile.aggregate({
1374
1378
  * where: { email: { endsWith: '@zenstack.dev' } },
1375
1379
  * _count: true,
1376
1380
  * _avg: { age: true },
@@ -1388,26 +1392,26 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1388
1392
  * @example
1389
1393
  * ```ts
1390
1394
  * // group by a field
1391
- * await client.profile.groupBy({
1395
+ * await db.profile.groupBy({
1392
1396
  * by: 'country',
1393
1397
  * _count: true
1394
1398
  * }); // result: `Array<{ country: string, _count: number }>`
1395
1399
  *
1396
1400
  * // group by multiple fields
1397
- * await client.profile.groupBy({
1401
+ * await db.profile.groupBy({
1398
1402
  * by: ['country', 'city'],
1399
1403
  * _count: true
1400
1404
  * }); // result: `Array<{ country: string, city: string, _count: number }>`
1401
1405
  *
1402
1406
  * // group by with sorting, the `orderBy` fields must be in the `by` list
1403
- * await client.profile.groupBy({
1407
+ * await db.profile.groupBy({
1404
1408
  * by: 'country',
1405
1409
  * orderBy: { country: 'desc' }
1406
1410
  * });
1407
1411
  *
1408
1412
  * // group by with having (post-aggregation filter), the `having` fields must
1409
1413
  * // be in the `by` list
1410
- * await client.profile.groupBy({
1414
+ * await db.profile.groupBy({
1411
1415
  * by: 'country',
1412
1416
  * having: { country: 'US' }
1413
1417
  * });