@edium/halifax 1.0.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.
Files changed (135) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/LICENSE +21 -0
  3. package/README.md +148 -0
  4. package/README_AUTH.md +172 -0
  5. package/README_AUTOCRUD.md +253 -0
  6. package/README_CACHE.md +164 -0
  7. package/README_HTTP_ADAPTERS.md +309 -0
  8. package/README_MULTITENANCY.md +162 -0
  9. package/README_QUERYBUILDER.md +219 -0
  10. package/README_REPO_ADAPTERS.md +266 -0
  11. package/dist/adapters/http/ExpressAdapter.d.ts +40 -0
  12. package/dist/adapters/http/ExpressAdapter.d.ts.map +1 -0
  13. package/dist/adapters/http/ExpressAdapter.js +109 -0
  14. package/dist/adapters/http/ExpressAdapter.js.map +1 -0
  15. package/dist/adapters/orm/prisma/PrismaAdapter.d.ts +143 -0
  16. package/dist/adapters/orm/prisma/PrismaAdapter.d.ts.map +1 -0
  17. package/dist/adapters/orm/prisma/PrismaAdapter.js +277 -0
  18. package/dist/adapters/orm/prisma/PrismaAdapter.js.map +1 -0
  19. package/dist/adapters/orm/prisma/createPrismaResources.d.ts +15 -0
  20. package/dist/adapters/orm/prisma/createPrismaResources.d.ts.map +1 -0
  21. package/dist/adapters/orm/prisma/createPrismaResources.js +51 -0
  22. package/dist/adapters/orm/prisma/createPrismaResources.js.map +1 -0
  23. package/dist/adapters/orm/prisma/helpers.d.ts +27 -0
  24. package/dist/adapters/orm/prisma/helpers.d.ts.map +1 -0
  25. package/dist/adapters/orm/prisma/helpers.js +45 -0
  26. package/dist/adapters/orm/prisma/helpers.js.map +1 -0
  27. package/dist/adapters/orm/prisma/index.d.ts +4 -0
  28. package/dist/adapters/orm/prisma/index.d.ts.map +1 -0
  29. package/dist/adapters/orm/prisma/index.js +3 -0
  30. package/dist/adapters/orm/prisma/index.js.map +1 -0
  31. package/dist/adapters/orm/prisma/types.d.ts +49 -0
  32. package/dist/adapters/orm/prisma/types.d.ts.map +1 -0
  33. package/dist/adapters/orm/prisma/types.js +2 -0
  34. package/dist/adapters/orm/prisma/types.js.map +1 -0
  35. package/dist/auth/AuthStrategy.d.ts +198 -0
  36. package/dist/auth/AuthStrategy.d.ts.map +1 -0
  37. package/dist/auth/AuthStrategy.js +227 -0
  38. package/dist/auth/AuthStrategy.js.map +1 -0
  39. package/dist/classes/QueryBuilder.d.ts +33 -0
  40. package/dist/classes/QueryBuilder.d.ts.map +1 -0
  41. package/dist/classes/QueryBuilder.js +262 -0
  42. package/dist/classes/QueryBuilder.js.map +1 -0
  43. package/dist/core/crudRouter.d.ts +36 -0
  44. package/dist/core/crudRouter.d.ts.map +1 -0
  45. package/dist/core/crudRouter.js +391 -0
  46. package/dist/core/crudRouter.js.map +1 -0
  47. package/dist/core/queryString.d.ts +13 -0
  48. package/dist/core/queryString.d.ts.map +1 -0
  49. package/dist/core/queryString.js +89 -0
  50. package/dist/core/queryString.js.map +1 -0
  51. package/dist/core/types.d.ts +293 -0
  52. package/dist/core/types.d.ts.map +1 -0
  53. package/dist/core/types.js +13 -0
  54. package/dist/core/types.js.map +1 -0
  55. package/dist/core/validation.d.ts +75 -0
  56. package/dist/core/validation.d.ts.map +1 -0
  57. package/dist/core/validation.js +206 -0
  58. package/dist/core/validation.js.map +1 -0
  59. package/dist/enums/SqlComparison.d.ts +18 -0
  60. package/dist/enums/SqlComparison.d.ts.map +1 -0
  61. package/dist/enums/SqlComparison.js +19 -0
  62. package/dist/enums/SqlComparison.js.map +1 -0
  63. package/dist/enums/SqlOperator.d.ts +6 -0
  64. package/dist/enums/SqlOperator.d.ts.map +1 -0
  65. package/dist/enums/SqlOperator.js +7 -0
  66. package/dist/enums/SqlOperator.js.map +1 -0
  67. package/dist/enums/SqlOrder.d.ts +6 -0
  68. package/dist/enums/SqlOrder.d.ts.map +1 -0
  69. package/dist/enums/SqlOrder.js +7 -0
  70. package/dist/enums/SqlOrder.js.map +1 -0
  71. package/dist/errors/AuthenticationError.d.ts +10 -0
  72. package/dist/errors/AuthenticationError.d.ts.map +1 -0
  73. package/dist/errors/AuthenticationError.js +13 -0
  74. package/dist/errors/AuthenticationError.js.map +1 -0
  75. package/dist/errors/AuthorizationError.d.ts +10 -0
  76. package/dist/errors/AuthorizationError.d.ts.map +1 -0
  77. package/dist/errors/AuthorizationError.js +13 -0
  78. package/dist/errors/AuthorizationError.js.map +1 -0
  79. package/dist/errors/BadRequestError.d.ts +10 -0
  80. package/dist/errors/BadRequestError.d.ts.map +1 -0
  81. package/dist/errors/BadRequestError.js +13 -0
  82. package/dist/errors/BadRequestError.js.map +1 -0
  83. package/dist/errors/HttpError.d.ts +12 -0
  84. package/dist/errors/HttpError.d.ts.map +1 -0
  85. package/dist/errors/HttpError.js +17 -0
  86. package/dist/errors/HttpError.js.map +1 -0
  87. package/dist/errors/MethodNotAllowedError.d.ts +10 -0
  88. package/dist/errors/MethodNotAllowedError.d.ts.map +1 -0
  89. package/dist/errors/MethodNotAllowedError.js +13 -0
  90. package/dist/errors/MethodNotAllowedError.js.map +1 -0
  91. package/dist/errors/NotAcceptableError.d.ts +10 -0
  92. package/dist/errors/NotAcceptableError.d.ts.map +1 -0
  93. package/dist/errors/NotAcceptableError.js +13 -0
  94. package/dist/errors/NotAcceptableError.js.map +1 -0
  95. package/dist/errors/NotFoundError.d.ts +10 -0
  96. package/dist/errors/NotFoundError.d.ts.map +1 -0
  97. package/dist/errors/NotFoundError.js +13 -0
  98. package/dist/errors/NotFoundError.js.map +1 -0
  99. package/dist/errors/NotImplementedError.d.ts +10 -0
  100. package/dist/errors/NotImplementedError.d.ts.map +1 -0
  101. package/dist/errors/NotImplementedError.js +13 -0
  102. package/dist/errors/NotImplementedError.js.map +1 -0
  103. package/dist/errors/ServerError.d.ts +10 -0
  104. package/dist/errors/ServerError.d.ts.map +1 -0
  105. package/dist/errors/ServerError.js +13 -0
  106. package/dist/errors/ServerError.js.map +1 -0
  107. package/dist/errors/UnprocessableEntityError.d.ts +10 -0
  108. package/dist/errors/UnprocessableEntityError.d.ts.map +1 -0
  109. package/dist/errors/UnprocessableEntityError.js +13 -0
  110. package/dist/errors/UnprocessableEntityError.js.map +1 -0
  111. package/dist/errors/UnsupportedMediaTypeError.d.ts +10 -0
  112. package/dist/errors/UnsupportedMediaTypeError.d.ts.map +1 -0
  113. package/dist/errors/UnsupportedMediaTypeError.js +13 -0
  114. package/dist/errors/UnsupportedMediaTypeError.js.map +1 -0
  115. package/dist/index.d.ts +27 -0
  116. package/dist/index.d.ts.map +1 -0
  117. package/dist/index.js +27 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/interfaces/IParamQuery.d.ts +8 -0
  120. package/dist/interfaces/IParamQuery.d.ts.map +1 -0
  121. package/dist/interfaces/IParamQuery.js +2 -0
  122. package/dist/interfaces/IParamQuery.js.map +1 -0
  123. package/dist/interfaces/IQueryFilter.d.ts +18 -0
  124. package/dist/interfaces/IQueryFilter.d.ts.map +1 -0
  125. package/dist/interfaces/IQueryFilter.js +2 -0
  126. package/dist/interfaces/IQueryFilter.js.map +1 -0
  127. package/dist/interfaces/IQueryOptions.d.ts +20 -0
  128. package/dist/interfaces/IQueryOptions.d.ts.map +1 -0
  129. package/dist/interfaces/IQueryOptions.js +2 -0
  130. package/dist/interfaces/IQueryOptions.js.map +1 -0
  131. package/dist/interfaces/ISort.d.ts +9 -0
  132. package/dist/interfaces/ISort.d.ts.map +1 -0
  133. package/dist/interfaces/ISort.js +2 -0
  134. package/dist/interfaces/ISort.js.map +1 -0
  135. package/package.json +169 -0
@@ -0,0 +1,143 @@
1
+ import type { IQueryOptions } from '../../../interfaces/IQueryOptions.js';
2
+ import type { Repository, RepositoryCapabilities, DeleteManyResult, ListOptions, ListResult, NativeQueryResult, UpdateManyResult } from '../../../core/types.js';
3
+ import type { FieldDefinition, RelationDefinition, ModelSchema } from '../../../core/types.js';
4
+ import type { PrismaAdapterOptions } from './types.js';
5
+ /**
6
+ * PrismaAdapter is a generic repository implementation that uses Prisma delegates to perform database operations.
7
+ * It supports basic CRUD operations and can be extended to support more complex queries.
8
+ * The adapter can optionally use a Prisma client for executing raw SQL queries when needed.
9
+ * It also extracts field and relation definitions from a provided Prisma model schema to enhance query capabilities.
10
+ */
11
+ export declare class PrismaAdapter<TRecord = unknown, TCreate = Partial<TRecord>, TUpdate = Partial<TRecord>> implements Repository<TRecord, TCreate, TUpdate> {
12
+ /** Private properties to hold the Prisma delegate, client, and configuration options. */
13
+ private readonly delegate;
14
+ /** Optional Prisma client for executing raw SQL queries, required for certain operations like updateMany and deleteMany. */
15
+ private readonly client?;
16
+ /** The field name used for the primary key in the model. */
17
+ private readonly idField;
18
+ /** The name of the database table associated with the model. */
19
+ private readonly tableName?;
20
+ /** A flag indicating whether to return created records. */
21
+ private readonly returnCreated;
22
+ /** A set of capabilities that the repository supports. */
23
+ readonly capabilities: RepositoryCapabilities;
24
+ /** An array of field definitions for the model. */
25
+ readonly fields: FieldDefinition[] | undefined;
26
+ /** An array of relation definitions for the model. */
27
+ readonly relations: RelationDefinition[] | undefined;
28
+ /**
29
+ * Constructs a new instance of PrismaAdapter with the provided options.
30
+ * @param options - An object containing the Prisma delegate, optional client, and configuration settings for the adapter.
31
+ */
32
+ constructor(options: PrismaAdapterOptions);
33
+ /**
34
+ * Extracts field definitions from a Prisma model schema.
35
+ * @param model - The Prisma model schema.
36
+ * @returns An array of field definitions.
37
+ */
38
+ static fieldsFromModel(model: ModelSchema): FieldDefinition[];
39
+ /**
40
+ * Extracts relation definitions from a Prisma model schema.
41
+ * @param model - The Prisma model schema.
42
+ * @returns
43
+ */
44
+ static relationsFromModel(model: ModelSchema): RelationDefinition[];
45
+ /**
46
+ * Retrieves a single record by its ID, with optional field selection and relation inclusion.
47
+ * @param id - The ID of the record to retrieve.
48
+ * @param options - Optional parameters for field selection and relation inclusion.
49
+ * @returns A promise that resolves to the retrieved record or null if not found.
50
+ * @throws ServerError if the Prisma delegate does not support the required methods.
51
+ */
52
+ getOne(id: string | number, options?: Pick<ListOptions, 'fields' | 'include'>): Promise<TRecord | null>;
53
+ /**
54
+ * Retrieves multiple records based on the provided query options, including filtering, sorting, pagination, and field selection.
55
+ * @param options - An object containing query options such as filtering conditions, sorting, pagination, and field selection.
56
+ * @returns A promise that resolves to an object containing the total count of matching records and an array of the retrieved records.
57
+ * @throws ServerError if the Prisma delegate does not support the required methods.
58
+ */
59
+ getMany(options?: ListOptions): Promise<ListResult<TRecord>>;
60
+ /**
61
+ * Creates a new record in the database using the provided data.
62
+ * @param data - An object containing the data for the new record to be created.
63
+ * @returns A promise that resolves to the created record.
64
+ * @throws ServerError if the Prisma delegate does not support the create method.
65
+ */
66
+ createOne(data: TCreate): Promise<TRecord>;
67
+ /**
68
+ * Creates multiple records in the database using the provided array of data objects.
69
+ * If the Prisma delegate does not support createMany or if returnCreated is true, it falls back to creating records one by one and returns the created records. Otherwise, it uses createMany for better performance but does not return the created records.
70
+ * @param data - An array of objects, each containing the data for a new record to be created.
71
+ * @returns A promise that resolves to an array of the created records.
72
+ * @throws ServerError if the Prisma delegate does not support the createMany method.
73
+ */
74
+ createMany(data: TCreate[]): Promise<TRecord[]>;
75
+ /**
76
+ * Updates a single record identified by its ID with the provided data. If the record does not exist, it returns null.
77
+ * @param id - The ID of the record to be updated.
78
+ * @param data - An object containing the data to update the record with.
79
+ * @returns A promise that resolves to the updated record or null if the record does not exist.
80
+ * @throws ServerError if the Prisma delegate does not support the update method.
81
+ */
82
+ updateOne(id: string | number, data: TUpdate): Promise<TRecord | null>;
83
+ /**
84
+ * Updates multiple records that match the provided query options with the given data.
85
+ * This method requires a Prisma client for executing raw SQL queries, as Prisma does not
86
+ * natively support bulk updates with return values. It first selects the IDs of the records
87
+ * to be updated based on the query options, then executes an update query, and finally returns
88
+ * the IDs of the updated records.
89
+ *
90
+ * **Note:** The SELECT and UPDATE are issued as two separate queries without a transaction.
91
+ * Under concurrent writes, rows inserted or deleted between the two queries may cause the
92
+ * returned `updated` IDs to differ from the rows actually modified.
93
+ *
94
+ * @param query - An object containing query options to filter the records that should be updated, such as where conditions, sorting, and pagination.
95
+ * @param data - An object containing the data to update the matching records with.
96
+ * @returns A promise that resolves to an object containing an array of the IDs of the updated records.
97
+ * @throws NotImplementedError if the Prisma client or tableName is not provided, as they are required for executing raw SQL queries.
98
+ * @throws ServerError if the Prisma client does not support the required methods for executing raw SQL queries.
99
+ */
100
+ updateMany(query: IQueryOptions, data: TUpdate): Promise<UpdateManyResult<TRecord>>;
101
+ /**
102
+ * Upserts a single record identified by its ID with the provided data. If the record does not exist, it creates a new one.
103
+ * If it exists, it updates the existing record. This method requires the Prisma delegate to support the upsert operation.
104
+ * @param id - The ID of the record to be upserted.
105
+ * @param data - An object containing the data to upsert the record with. This data will be used for both creating a new record if it does not exist and updating the existing record if it does exist.
106
+ * @returns A promise that resolves to the upserted record, whether it was created or updated.
107
+ * @throws NotImplementedError if the Prisma delegate does not support the upsert method.
108
+ * @throws ServerError if the Prisma delegate does not support the required methods for upserting records.
109
+ */
110
+ upsertOne(id: string | number, data: TCreate & TUpdate): Promise<TRecord>;
111
+ /**
112
+ * Deletes a single record identified by its ID. If the record does not exist, it returns false.
113
+ * If the deletion is successful, it returns true. This method requires the Prisma delegate to support the delete operation.
114
+ * @param id - The ID of the record to be deleted.
115
+ * @returns A promise that resolves to true if the record was successfully deleted, or false if the record did not exist.
116
+ * @throws NotImplementedError if the Prisma delegate does not support the delete method.
117
+ * @throws ServerError if the Prisma delegate does not support the required methods for deleting records.
118
+ */
119
+ deleteOne(id: string | number): Promise<boolean>;
120
+ /**
121
+ * Deletes multiple records that match the provided query options. This method requires a Prisma
122
+ * client for executing raw SQL queries.
123
+ *
124
+ * **Note:** The SELECT and DELETE are issued as two separate queries without a transaction.
125
+ * Under concurrent writes, rows inserted or deleted between the two queries may cause the
126
+ * returned `deleted` IDs to differ from the rows actually removed.
127
+ *
128
+ * @param query - An object containing query options to filter the records that should be deleted, such as where conditions, sorting, and pagination.
129
+ * @returns A promise that resolves to an object containing an array of the IDs of the deleted records.
130
+ * @throws NotImplementedError if the Prisma client or tableName is not provided, as they are required for executing raw SQL queries.
131
+ * @throws ServerError if the Prisma client does not support the required methods for executing raw SQL queries.
132
+ */
133
+ deleteMany(query: IQueryOptions): Promise<DeleteManyResult>;
134
+ /**
135
+ * Executes a query built using the QueryBuilder, which allows for complex filtering, sorting, pagination, and field selection. This method requires a Prisma client for executing raw SQL queries, as it relies on the QueryBuilder to generate SQL statements. It first builds a count query to get the total number of matching records and then builds a select query to retrieve the actual records based on the provided query options.
136
+ * @param query - An object containing query options such as filtering conditions, sorting, pagination, and field selection, which will be used to build the SQL queries.
137
+ * @returns A promise that resolves to an object containing the total count of matching records and an array of the retrieved records.
138
+ * @throws NotImplementedError if the Prisma client or tableName is not provided, as they are required for executing raw SQL queries.
139
+ * @throws ServerError if the Prisma client does not support the required methods for executing raw SQL queries.
140
+ */
141
+ executeQueryBuilder(query: IQueryOptions): Promise<NativeQueryResult<TRecord>>;
142
+ }
143
+ //# sourceMappingURL=PrismaAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PrismaAdapter.d.ts","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/PrismaAdapter.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAClE,OAAO,KAAK,EACV,UAAU,EACV,sBAAsB,EACtB,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AACvF,OAAO,KAAK,EAAsC,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAG1F;;;;;GAKG;AACH,qBAAa,aAAa,CACxB,OAAO,GAAG,OAAO,EACjB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,EAC1B,OAAO,GAAG,OAAO,CAAC,OAAO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;IAS5D;;;;;;OAMG;IACU,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IASnF;;;;;;;;;;;;;;;;OAgBG;IACU,UAAU,CAAC,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAmBhG;;;;;;;;OAQG;IACU,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAYtF;;;;;;;OAOG;IACU,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU7D;;;;;;;;;;;;OAYG;IACU,UAAU,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAgBxE;;;;;;OAMG;IACU,mBAAmB,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;CAyB5F"}
@@ -0,0 +1,277 @@
1
+ import { QueryBuilder } from '../../../classes/QueryBuilder.js';
2
+ import { NotImplementedError } from '../../../errors/NotImplementedError.js';
3
+ import { ServerError } from '../../../errors/ServerError.js';
4
+ /** Returns true for Prisma's P2025 "record not found" error. */
5
+ function isNotFoundError(error) {
6
+ return (typeof error === 'object' &&
7
+ error !== null &&
8
+ 'code' in error &&
9
+ error.code === 'P2025');
10
+ }
11
+ import { toSelect, toInclude, toOrderBy } from './helpers.js';
12
+ /**
13
+ * PrismaAdapter is a generic repository implementation that uses Prisma delegates to perform database operations.
14
+ * It supports basic CRUD operations and can be extended to support more complex queries.
15
+ * The adapter can optionally use a Prisma client for executing raw SQL queries when needed.
16
+ * It also extracts field and relation definitions from a provided Prisma model schema to enhance query capabilities.
17
+ */
18
+ export class PrismaAdapter {
19
+ /** Private properties to hold the Prisma delegate, client, and configuration options. */
20
+ delegate;
21
+ /** Optional Prisma client for executing raw SQL queries, required for certain operations like updateMany and deleteMany. */
22
+ client;
23
+ /** The field name used for the primary key in the model. */
24
+ idField;
25
+ /** The name of the database table associated with the model. */
26
+ tableName;
27
+ /** A flag indicating whether to return created records. */
28
+ returnCreated;
29
+ /** A set of capabilities that the repository supports. */
30
+ capabilities;
31
+ /** An array of field definitions for the model. */
32
+ fields;
33
+ /** An array of relation definitions for the model. */
34
+ relations;
35
+ /**
36
+ * Constructs a new instance of PrismaAdapter with the provided options.
37
+ * @param options - An object containing the Prisma delegate, optional client, and configuration settings for the adapter.
38
+ */
39
+ constructor(options) {
40
+ this.delegate = options.delegate;
41
+ this.client = options.client;
42
+ this.idField = options.idField ?? 'id';
43
+ this.tableName = options.tableName;
44
+ this.returnCreated = options.returnCreated ?? false;
45
+ this.capabilities = {
46
+ supportsNativeSql: !!options.client,
47
+ supportsIncludes: true,
48
+ supportsTransactions: false,
49
+ supportsCreateManyReturn: this.returnCreated,
50
+ supportsNoSqlQueryAst: false
51
+ };
52
+ if (options.model) {
53
+ this.fields = PrismaAdapter.fieldsFromModel(options.model);
54
+ this.relations = PrismaAdapter.relationsFromModel(options.model);
55
+ }
56
+ }
57
+ /**
58
+ * Extracts field definitions from a Prisma model schema.
59
+ * @param model - The Prisma model schema.
60
+ * @returns An array of field definitions.
61
+ */
62
+ static fieldsFromModel(model) {
63
+ return model.fields
64
+ .filter((f) => f.kind !== 'object')
65
+ .map((f) => ({
66
+ name: f.name,
67
+ filterable: true,
68
+ sortable: true,
69
+ writable: !f.isId && !f.isReadOnly
70
+ }));
71
+ }
72
+ /**
73
+ * Extracts relation definitions from a Prisma model schema.
74
+ * @param model - The Prisma model schema.
75
+ * @returns
76
+ */
77
+ static relationsFromModel(model) {
78
+ return model.fields
79
+ .filter((f) => f.kind === 'object')
80
+ .map((f) => ({ name: f.name, includable: true }));
81
+ }
82
+ /**
83
+ * Retrieves a single record by its ID, with optional field selection and relation inclusion.
84
+ * @param id - The ID of the record to retrieve.
85
+ * @param options - Optional parameters for field selection and relation inclusion.
86
+ * @returns A promise that resolves to the retrieved record or null if not found.
87
+ * @throws ServerError if the Prisma delegate does not support the required methods.
88
+ */
89
+ async getOne(id, options) {
90
+ const select = toSelect(options?.fields);
91
+ const include = toInclude(options?.include);
92
+ const args = { where: { [this.idField]: id } };
93
+ if (select)
94
+ args.select = select;
95
+ else if (include)
96
+ args.include = include;
97
+ if (this.delegate.findUnique) {
98
+ return (await this.delegate.findUnique(args));
99
+ }
100
+ if (this.delegate.findFirst) {
101
+ return (await this.delegate.findFirst(args));
102
+ }
103
+ throw new ServerError('Prisma delegate does not support findUnique or findFirst.');
104
+ }
105
+ /**
106
+ * Retrieves multiple records based on the provided query options, including filtering, sorting, pagination, and field selection.
107
+ * @param options - An object containing query options such as filtering conditions, sorting, pagination, and field selection.
108
+ * @returns A promise that resolves to an object containing the total count of matching records and an array of the retrieved records.
109
+ * @throws ServerError if the Prisma delegate does not support the required methods.
110
+ */
111
+ async getMany(options = {}) {
112
+ const select = toSelect(options.fields);
113
+ const include = toInclude(options.include);
114
+ const args = {
115
+ where: options.where,
116
+ orderBy: toOrderBy(options.orderBy),
117
+ skip: options.offset,
118
+ take: options.limit
119
+ };
120
+ if (select)
121
+ args.select = select;
122
+ else if (include)
123
+ args.include = include;
124
+ const [count, results] = await Promise.all([
125
+ this.delegate.count({ where: options.where }),
126
+ this.delegate.findMany(args)
127
+ ]);
128
+ return { count, results: results };
129
+ }
130
+ /**
131
+ * Creates a new record in the database using the provided data.
132
+ * @param data - An object containing the data for the new record to be created.
133
+ * @returns A promise that resolves to the created record.
134
+ * @throws ServerError if the Prisma delegate does not support the create method.
135
+ */
136
+ async createOne(data) {
137
+ return (await this.delegate.create({ data }));
138
+ }
139
+ /**
140
+ * Creates multiple records in the database using the provided array of data objects.
141
+ * If the Prisma delegate does not support createMany or if returnCreated is true, it falls back to creating records one by one and returns the created records. Otherwise, it uses createMany for better performance but does not return the created records.
142
+ * @param data - An array of objects, each containing the data for a new record to be created.
143
+ * @returns A promise that resolves to an array of the created records.
144
+ * @throws ServerError if the Prisma delegate does not support the createMany method.
145
+ */
146
+ async createMany(data) {
147
+ if (!this.delegate.createMany || this.returnCreated) {
148
+ return await Promise.all(data.map((item) => this.createOne(item)));
149
+ }
150
+ await this.delegate.createMany({ data });
151
+ return [];
152
+ }
153
+ /**
154
+ * Updates a single record identified by its ID with the provided data. If the record does not exist, it returns null.
155
+ * @param id - The ID of the record to be updated.
156
+ * @param data - An object containing the data to update the record with.
157
+ * @returns A promise that resolves to the updated record or null if the record does not exist.
158
+ * @throws ServerError if the Prisma delegate does not support the update method.
159
+ */
160
+ async updateOne(id, data) {
161
+ try {
162
+ return (await this.delegate.update({ where: { [this.idField]: id }, data }));
163
+ }
164
+ catch (error) {
165
+ if (isNotFoundError(error))
166
+ return null;
167
+ throw error;
168
+ }
169
+ }
170
+ /**
171
+ * Updates multiple records that match the provided query options with the given data.
172
+ * This method requires a Prisma client for executing raw SQL queries, as Prisma does not
173
+ * natively support bulk updates with return values. It first selects the IDs of the records
174
+ * to be updated based on the query options, then executes an update query, and finally returns
175
+ * the IDs of the updated records.
176
+ *
177
+ * **Note:** The SELECT and UPDATE are issued as two separate queries without a transaction.
178
+ * Under concurrent writes, rows inserted or deleted between the two queries may cause the
179
+ * returned `updated` IDs to differ from the rows actually modified.
180
+ *
181
+ * @param query - An object containing query options to filter the records that should be updated, such as where conditions, sorting, and pagination.
182
+ * @param data - An object containing the data to update the matching records with.
183
+ * @returns A promise that resolves to an object containing an array of the IDs of the updated records.
184
+ * @throws NotImplementedError if the Prisma client or tableName is not provided, as they are required for executing raw SQL queries.
185
+ * @throws ServerError if the Prisma client does not support the required methods for executing raw SQL queries.
186
+ */
187
+ async updateMany(query, data) {
188
+ if (!this.client?.$queryRawUnsafe || !this.tableName) {
189
+ throw new NotImplementedError('Native SQL updateMany requires a Prisma client and tableName.');
190
+ }
191
+ const updateQuery = QueryBuilder.buildUpdateQuery({ ...query, tableName: query.tableName || this.tableName }, data, [this.idField], this.idField);
192
+ const updated = await this.client.$queryRawUnsafe(updateQuery.statement, ...updateQuery.parameters);
193
+ return { updated: updated.map((item) => item[this.idField]) };
194
+ }
195
+ /**
196
+ * Upserts a single record identified by its ID with the provided data. If the record does not exist, it creates a new one.
197
+ * If it exists, it updates the existing record. This method requires the Prisma delegate to support the upsert operation.
198
+ * @param id - The ID of the record to be upserted.
199
+ * @param data - An object containing the data to upsert the record with. This data will be used for both creating a new record if it does not exist and updating the existing record if it does exist.
200
+ * @returns A promise that resolves to the upserted record, whether it was created or updated.
201
+ * @throws NotImplementedError if the Prisma delegate does not support the upsert method.
202
+ * @throws ServerError if the Prisma delegate does not support the required methods for upserting records.
203
+ */
204
+ async upsertOne(id, data) {
205
+ if (!this.delegate.upsert) {
206
+ throw new NotImplementedError('Prisma delegate does not support upsert.');
207
+ }
208
+ return (await this.delegate.upsert({
209
+ where: { [this.idField]: id },
210
+ create: data,
211
+ update: data
212
+ }));
213
+ }
214
+ /**
215
+ * Deletes a single record identified by its ID. If the record does not exist, it returns false.
216
+ * If the deletion is successful, it returns true. This method requires the Prisma delegate to support the delete operation.
217
+ * @param id - The ID of the record to be deleted.
218
+ * @returns A promise that resolves to true if the record was successfully deleted, or false if the record did not exist.
219
+ * @throws NotImplementedError if the Prisma delegate does not support the delete method.
220
+ * @throws ServerError if the Prisma delegate does not support the required methods for deleting records.
221
+ */
222
+ async deleteOne(id) {
223
+ try {
224
+ await this.delegate.delete({ where: { [this.idField]: id } });
225
+ return true;
226
+ }
227
+ catch (error) {
228
+ if (isNotFoundError(error))
229
+ return false;
230
+ throw error;
231
+ }
232
+ }
233
+ /**
234
+ * Deletes multiple records that match the provided query options. This method requires a Prisma
235
+ * client for executing raw SQL queries.
236
+ *
237
+ * **Note:** The SELECT and DELETE are issued as two separate queries without a transaction.
238
+ * Under concurrent writes, rows inserted or deleted between the two queries may cause the
239
+ * returned `deleted` IDs to differ from the rows actually removed.
240
+ *
241
+ * @param query - An object containing query options to filter the records that should be deleted, such as where conditions, sorting, and pagination.
242
+ * @returns A promise that resolves to an object containing an array of the IDs of the deleted records.
243
+ * @throws NotImplementedError if the Prisma client or tableName is not provided, as they are required for executing raw SQL queries.
244
+ * @throws ServerError if the Prisma client does not support the required methods for executing raw SQL queries.
245
+ */
246
+ async deleteMany(query) {
247
+ if (!this.client?.$queryRawUnsafe || !this.tableName) {
248
+ throw new NotImplementedError('Native SQL deleteMany requires a Prisma client and tableName.');
249
+ }
250
+ const resolvedQuery = { ...query, tableName: query.tableName || this.tableName };
251
+ const deleteQuery = QueryBuilder.buildDeleteQuery(resolvedQuery, [this.idField]);
252
+ const deleted = await this.client.$queryRawUnsafe(deleteQuery.statement, ...deleteQuery.parameters);
253
+ return { deleted: deleted.map((item) => item[this.idField]) };
254
+ }
255
+ /**
256
+ * Executes a query built using the QueryBuilder, which allows for complex filtering, sorting, pagination, and field selection. This method requires a Prisma client for executing raw SQL queries, as it relies on the QueryBuilder to generate SQL statements. It first builds a count query to get the total number of matching records and then builds a select query to retrieve the actual records based on the provided query options.
257
+ * @param query - An object containing query options such as filtering conditions, sorting, pagination, and field selection, which will be used to build the SQL queries.
258
+ * @returns A promise that resolves to an object containing the total count of matching records and an array of the retrieved records.
259
+ * @throws NotImplementedError if the Prisma client or tableName is not provided, as they are required for executing raw SQL queries.
260
+ * @throws ServerError if the Prisma client does not support the required methods for executing raw SQL queries.
261
+ */
262
+ async executeQueryBuilder(query) {
263
+ if (!this.client?.$queryRawUnsafe || !this.tableName) {
264
+ throw new NotImplementedError('Native SQL query-builder requires a Prisma client and tableName.');
265
+ }
266
+ const resolvedQuery = { ...query, tableName: query.tableName || this.tableName };
267
+ const countQuery = QueryBuilder.buildCountQuery(resolvedQuery);
268
+ const selectQuery = QueryBuilder.buildSelectQuery(resolvedQuery);
269
+ const countRows = await this.client.$queryRawUnsafe(countQuery.statement, ...countQuery.parameters);
270
+ const results = await this.client.$queryRawUnsafe(selectQuery.statement, ...selectQuery.parameters);
271
+ return {
272
+ count: Number(countRows[0]?.count ?? 0),
273
+ results
274
+ };
275
+ }
276
+ }
277
+ //# sourceMappingURL=PrismaAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PrismaAdapter.js","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/PrismaAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAA;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAErD,gEAAgE;AAChE,SAAS,eAAe,CAAC,KAAc;IACrC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACd,KAAiC,CAAC,IAAI,KAAK,OAAO,CACpD,CAAA;AACH,CAAC;AAaD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAE7D;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IAKxB,yFAAyF;IACxE,QAAQ,CAAgB;IACzC,6HAA6H;IAC5G,MAAM,CAAiC;IACxD,4DAA4D;IAC3C,OAAO,CAAQ;IAChC,gEAAgE;IAC/C,SAAS,CAAqB;IAC/C,2DAA2D;IAC1C,aAAa,CAAS;IAEvC,0DAA0D;IAC1C,YAAY,CAAwB;IACpD,mDAAmD;IACnC,MAAM,CAA+B;IACrD,sDAAsD;IACtC,SAAS,CAAkC;IAE3D;;;OAGG;IACH,YAAmB,OAA6B;QAC9C,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAA;QAChC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,IAAI,CAAA;QACtC,IA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OAMG;IACI,KAAK,CAAC,mBAAmB,CAAC,KAAoB;QACnD,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,eAAe,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACrD,MAAM,IAAI,mBAAmB,CAC3B,kEAAkE,CACnE,CAAA;QACH,CAAC;QAED,MAAM,aAAa,GAAG,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,EAAE,CAAA;QAChF,MAAM,UAAU,GAAG,YAAY,CAAC,eAAe,CAAC,aAAa,CAAC,CAAA;QAC9D,MAAM,WAAW,GAAG,YAAY,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAA;QAEhE,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,eAAe,CACjD,UAAU,CAAC,SAAS,EACpB,GAAG,UAAU,CAAC,UAAU,CACzB,CAAA;QACD,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,eAAe,CAC/C,WAAW,CAAC,SAAS,EACrB,GAAG,WAAW,CAAC,UAAU,CAC1B,CAAA;QAED,OAAO;YACL,KAAK,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC;YACvC,OAAO;SACR,CAAA;IACH,CAAC;CACF"}
@@ -0,0 +1,15 @@
1
+ import type { ModelField, ResourceDefinition } from '../../../core/types.js';
2
+ import type { CreatePrismaResourcesOptions } from './types.js';
3
+ /**
4
+ * Creates resource definitions for Prisma models based on the provided schema and options.
5
+ * @param prismaClient An instance of the Prisma client to be used for database operations.
6
+ * @param schema An array of model definitions, where each model includes its name, optional database name, and fields.
7
+ * @param options An optional configuration object that allows customization of the generated resources, including model-specific options, default limits, and permissions.
8
+ * @returns An array of resource definitions that can be used to set up API endpoints or other data access layers based on the Prisma models.
9
+ */
10
+ export declare function createPrismaResources(prismaClient: object, schema: ReadonlyArray<{
11
+ name: string;
12
+ dbName?: string | null;
13
+ fields: ModelField[];
14
+ }>, options?: CreatePrismaResourcesOptions): ResourceDefinition[];
15
+ //# sourceMappingURL=createPrismaResources.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createPrismaResources.d.ts","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/createPrismaResources.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACrE,OAAO,KAAK,EAAE,4BAA4B,EAAsC,MAAM,YAAY,CAAA;AAIlG;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,aAAa,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,EAAE,UAAU,EAAE,CAAA;CAAE,CAAC,EACrF,OAAO,GAAE,4BAAiC,GACzC,kBAAkB,EAAE,CAuCtB"}
@@ -0,0 +1,51 @@
1
+ import { PrismaAdapter } from './PrismaAdapter.js';
2
+ import { toRoutePrefix } from './helpers.js';
3
+ /**
4
+ * Creates resource definitions for Prisma models based on the provided schema and options.
5
+ * @param prismaClient An instance of the Prisma client to be used for database operations.
6
+ * @param schema An array of model definitions, where each model includes its name, optional database name, and fields.
7
+ * @param options An optional configuration object that allows customization of the generated resources, including model-specific options, default limits, and permissions.
8
+ * @returns An array of resource definitions that can be used to set up API endpoints or other data access layers based on the Prisma models.
9
+ */
10
+ export function createPrismaResources(prismaClient, schema, options = {}) {
11
+ const client = prismaClient;
12
+ return schema
13
+ .filter((model) => !options.models?.[model.name]?.exclude)
14
+ .map((model) => {
15
+ const modelOpts = options.models?.[model.name] ?? {};
16
+ const tableName = modelOpts.tableName ?? model.dbName ?? model.name;
17
+ const routePrefix = modelOpts.routePrefix ?? toRoutePrefix(model.name);
18
+ const delegateKey = model.name.charAt(0).toLowerCase() + model.name.slice(1);
19
+ const adapter = new PrismaAdapter({
20
+ delegate: client[delegateKey],
21
+ client: client,
22
+ tableName,
23
+ ...(options.idField !== undefined && { idField: options.idField }),
24
+ ...(options.returnCreated !== undefined && { returnCreated: options.returnCreated }),
25
+ model
26
+ });
27
+ const resource = {
28
+ name: model.name,
29
+ routePrefix,
30
+ tableName,
31
+ fields: adapter.fields,
32
+ repository: adapter,
33
+ permissions: { ...options.permissions, ...modelOpts.permissions }
34
+ };
35
+ if (adapter.relations?.length)
36
+ resource.relations = adapter.relations;
37
+ if (modelOpts.requiredPermissions)
38
+ resource.requiredPermissions = modelOpts.requiredPermissions;
39
+ const defaultLimit = modelOpts.defaultLimit ?? options.defaultLimit;
40
+ const maxLimit = modelOpts.maxLimit ?? options.maxLimit;
41
+ const maxFilterDepth = modelOpts.maxFilterDepth;
42
+ if (defaultLimit !== undefined)
43
+ resource.defaultLimit = defaultLimit;
44
+ if (maxLimit !== undefined)
45
+ resource.maxLimit = maxLimit;
46
+ if (maxFilterDepth !== undefined)
47
+ resource.maxFilterDepth = maxFilterDepth;
48
+ return resource;
49
+ });
50
+ }
51
+ //# sourceMappingURL=createPrismaResources.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createPrismaResources.js","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/createPrismaResources.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAE5C;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CACnC,YAAoB,EACpB,MAAqF,EACrF,UAAwC,EAAE;IAE1C,MAAM,MAAM,GAAG,YAAmB,CAAA;IAElC,OAAO,MAAM;SACV,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;SACzD,GAAG,CAAC,CAAC,KAAK,EAAE,EA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MAAM,YAAY,GAAG,SAAS,CAAC,YAAY,IAAI,OAAO,CAAC,YAAY,CAAA;QACnE,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAA;QACvD,MAAM,cAAc,GAAG,SAAS,CAAC,cAAc,CAAA;QAC/C,IAAI,YAAY,KAAK,SAAS;YAAE,QAAQ,CAAC,YAAY,GAAG,YAAY,CAAA;QACpE,IAAI,QAAQ,KAAK,SAAS;YAAE,QAAQ,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxD,IAAI,cAAc,KAAK,SAAS;YAAE,QAAQ,CAAC,cAAc,GAAG,cAAc,CAAA;QAC1E,OAAO,QAAQ,CAAA;IACjB,CAAC,CAAC,CAAA;AACN,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { ListOptions } from '../../../core/types.js';
2
+ /**
3
+ * Converts an array of field names into a Prisma select object.
4
+ * For example, ['id', 'name'] becomes { id: true, name: true }.
5
+ * @param fields An array of field names to select. If undefined or empty, returns undefined to select all fields.
6
+ * @returns A Prisma select object or undefined if no fields are specified.
7
+ */
8
+ export declare function toSelect(fields?: string[]): Record<string, boolean> | undefined;
9
+ /**
10
+ * Converts an array of relation names into a Prisma include object.
11
+ * @param include An array of relation names to include. If undefined or empty, returns undefined to include no relations.
12
+ * @returns A Prisma include object or undefined if no relations are specified.
13
+ */
14
+ export declare function toInclude(include?: string[]): Record<string, boolean> | undefined;
15
+ /**
16
+ * Converts an array of sort options into a Prisma orderBy object.
17
+ * @param orderBy An array of sort options. If undefined or empty, returns undefined.
18
+ * @returns A Prisma orderBy object or undefined if no sort options are specified.
19
+ */
20
+ export declare function toOrderBy(orderBy?: ListOptions['orderBy']): Array<Record<string, 'asc' | 'desc'>> | undefined;
21
+ /**
22
+ * Converts a model name into a route prefix by converting it to kebab-case and pluralizing it.
23
+ * @param modelName The name of the model to convert, e.g., 'UserProfile'.
24
+ * @returns The route prefix, e.g., 'user-profiles'.
25
+ */
26
+ export declare function toRoutePrefix(modelName: string): string;
27
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAElD;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAG/E;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAGjF;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,OAAO,CAAC,EAAE,WAAW,CAAC,SAAS,CAAC,GAC/B,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,MAAM,CAAC,CAAC,GAAG,SAAS,CAGnD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAKvD"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Converts an array of field names into a Prisma select object.
3
+ * For example, ['id', 'name'] becomes { id: true, name: true }.
4
+ * @param fields An array of field names to select. If undefined or empty, returns undefined to select all fields.
5
+ * @returns A Prisma select object or undefined if no fields are specified.
6
+ */
7
+ export function toSelect(fields) {
8
+ if (!fields?.length)
9
+ return undefined;
10
+ return Object.fromEntries(fields.map((f) => [f, true]));
11
+ }
12
+ /**
13
+ * Converts an array of relation names into a Prisma include object.
14
+ * @param include An array of relation names to include. If undefined or empty, returns undefined to include no relations.
15
+ * @returns A Prisma include object or undefined if no relations are specified.
16
+ */
17
+ export function toInclude(include) {
18
+ if (!include?.length)
19
+ return undefined;
20
+ return Object.fromEntries(include.map((r) => [r, true]));
21
+ }
22
+ /**
23
+ * Converts an array of sort options into a Prisma orderBy object.
24
+ * @param orderBy An array of sort options. If undefined or empty, returns undefined.
25
+ * @returns A Prisma orderBy object or undefined if no sort options are specified.
26
+ */
27
+ export function toOrderBy(orderBy) {
28
+ if (!orderBy?.length)
29
+ return undefined;
30
+ return orderBy.map((sort) => ({ [sort.field]: sort.direction }));
31
+ }
32
+ /**
33
+ * Converts a model name into a route prefix by converting it to kebab-case and pluralizing it.
34
+ * @param modelName The name of the model to convert, e.g., 'UserProfile'.
35
+ * @returns The route prefix, e.g., 'user-profiles'.
36
+ */
37
+ export function toRoutePrefix(modelName) {
38
+ const kebab = modelName.replace(/([A-Z])/g, (m, l, i) => (i > 0 ? '-' : '') + l.toLowerCase());
39
+ if (kebab.endsWith('y') && !/[aeiou]y$/.test(kebab))
40
+ return kebab.slice(0, -1) + 'ies';
41
+ if (/(?:s|x|z|ch|sh)$/.test(kebab))
42
+ return kebab + 'es';
43
+ return kebab + 's';
44
+ }
45
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/helpers.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAiB;IACxC,IAAI,CAAC,MAAM,EAAE,MAAM;QAAE,OAAO,SAAS,CAAA;IACrC,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAA;AACzD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,OAAkB;IAC1C,IAAI,CAAC,OAAO,EAAE,MAAM;QAAE,OAAO,SAAS,CAAA;IACtC,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAA;AAC1D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACvB,OAAgC;IAEhC,IAAI,CAAC,OAAO,EAAE,MAAM;QAAE,OAAO,SAAS,CAAA;IACtC,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAA;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,SAAiB;IAC7C,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC,CAAA;IAC9F,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,CAAA;IACtF,IAAI,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,GAAG,IAAI,CAAA;IACvD,OAAO,KAAK,GAAG,GAAG,CAAA;AACpB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export type { PrismaDelegate, PrismaNativeClient, PrismaAdapterOptions, CreatePrismaResourcesOptions } from './types.js';
2
+ export { PrismaAdapter } from './PrismaAdapter.js';
3
+ export { createPrismaResources } from './createPrismaResources.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,cAAc,EACd,kBAAkB,EAClB,oBAAoB,EACpB,4BAA4B,EAC7B,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { PrismaAdapter } from './PrismaAdapter.js';
2
+ export { createPrismaResources } from './createPrismaResources.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/adapters/orm/prisma/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}