corsair 0.1.3 → 0.1.4

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 (108) hide show
  1. package/dist/adapters/drizzle/postgres.d.ts.map +1 -1
  2. package/dist/adapters/drizzle/postgres.js +1 -0
  3. package/dist/core/auth/key-manager.d.ts +5 -5
  4. package/dist/core/auth/key-manager.d.ts.map +1 -1
  5. package/dist/core/auth/key-manager.js +66 -64
  6. package/dist/core/client/index.d.ts +3 -3
  7. package/dist/core/client/index.d.ts.map +1 -1
  8. package/dist/core/client/index.js +29 -218
  9. package/dist/core/endpoints/index.d.ts +2 -2
  10. package/dist/core/endpoints/index.d.ts.map +1 -1
  11. package/dist/core/index.d.ts +2 -2
  12. package/dist/core/index.d.ts.map +1 -1
  13. package/dist/core/index.js +10 -6
  14. package/dist/core/plugins/index.d.ts +3 -3
  15. package/dist/core/plugins/index.d.ts.map +1 -1
  16. package/dist/db/index.d.ts +70 -8
  17. package/dist/db/index.d.ts.map +1 -1
  18. package/dist/db/index.js +20 -4
  19. package/dist/db/kysely/database.d.ts +15 -0
  20. package/dist/db/kysely/database.d.ts.map +1 -0
  21. package/dist/db/kysely/database.js +19 -0
  22. package/dist/db/kysely/orm.d.ts +6 -0
  23. package/dist/db/kysely/orm.d.ts.map +1 -0
  24. package/dist/db/kysely/orm.js +334 -0
  25. package/dist/db/kysely/postgres.d.ts +5 -0
  26. package/dist/db/kysely/postgres.d.ts.map +1 -0
  27. package/dist/db/kysely/postgres.js +20 -0
  28. package/dist/db/orm.d.ts +375 -32
  29. package/dist/db/orm.d.ts.map +1 -1
  30. package/dist/db/orm.js +176 -332
  31. package/dist/index.d.ts +3 -2
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +5 -1
  34. package/dist/plugins/github/endpoints/index.d.ts +130 -130
  35. package/dist/plugins/github/endpoints/types.d.ts +574 -574
  36. package/dist/plugins/github/endpoints/types.js +29 -29
  37. package/dist/plugins/github/integration.test.js +50 -76
  38. package/dist/plugins/github/schema/database.d.ts +121 -121
  39. package/dist/plugins/github/schema/database.js +17 -17
  40. package/dist/plugins/github/schema/index.d.ts +121 -121
  41. package/dist/plugins/gmail/endpoints/types.d.ts +128 -128
  42. package/dist/plugins/gmail/endpoints/types.js +1 -1
  43. package/dist/plugins/gmail/integration.test.js +15 -17
  44. package/dist/plugins/gmail/schema/database.d.ts +14 -14
  45. package/dist/plugins/gmail/schema/database.js +4 -4
  46. package/dist/plugins/gmail/schema/index.d.ts +14 -14
  47. package/dist/plugins/hubspot/endpoints/index.d.ts +100 -100
  48. package/dist/plugins/hubspot/endpoints/types.d.ts +300 -300
  49. package/dist/plugins/hubspot/endpoints/types.js +8 -8
  50. package/dist/plugins/hubspot/integration.test.js +56 -96
  51. package/dist/plugins/hubspot/schema/database.d.ts +24 -24
  52. package/dist/plugins/hubspot/schema/database.js +8 -8
  53. package/dist/plugins/hubspot/schema/index.d.ts +24 -24
  54. package/dist/plugins/linear/endpoints/comments.js +4 -4
  55. package/dist/plugins/linear/endpoints/index.d.ts +44 -44
  56. package/dist/plugins/linear/endpoints/projects.js +4 -4
  57. package/dist/plugins/linear/endpoints/teams.js +4 -4
  58. package/dist/plugins/linear/endpoints/types.d.ts +756 -756
  59. package/dist/plugins/linear/endpoints/types.js +76 -76
  60. package/dist/plugins/linear/integration.test.js +39 -50
  61. package/dist/plugins/linear/schema/database.d.ts +75 -75
  62. package/dist/plugins/linear/schema/database.js +25 -25
  63. package/dist/plugins/linear/schema/index.d.ts +75 -75
  64. package/dist/plugins/linear/webhooks/comments.d.ts.map +1 -1
  65. package/dist/plugins/linear/webhooks/comments.js +6 -4
  66. package/dist/plugins/linear/webhooks/issues.d.ts.map +1 -1
  67. package/dist/plugins/linear/webhooks/issues.js +26 -4
  68. package/dist/plugins/linear/webhooks/projects.d.ts.map +1 -1
  69. package/dist/plugins/linear/webhooks/projects.js +12 -4
  70. package/dist/plugins/posthog/integration.test.js +15 -17
  71. package/dist/plugins/posthog/schema/database.d.ts +3 -3
  72. package/dist/plugins/posthog/schema/database.js +1 -1
  73. package/dist/plugins/posthog/schema/index.d.ts +3 -3
  74. package/dist/plugins/resend/endpoints/index.d.ts +6 -6
  75. package/dist/plugins/resend/endpoints/types.d.ts +28 -28
  76. package/dist/plugins/resend/endpoints/types.js +3 -3
  77. package/dist/plugins/resend/integration.test.js +21 -26
  78. package/dist/plugins/resend/schema/database.d.ts +6 -6
  79. package/dist/plugins/resend/schema/database.js +2 -2
  80. package/dist/plugins/resend/schema/index.d.ts +6 -6
  81. package/dist/plugins/resend/webhooks/domains.d.ts.map +1 -1
  82. package/dist/plugins/resend/webhooks/domains.js +2 -0
  83. package/dist/plugins/resend/webhooks/emails.d.ts.map +1 -1
  84. package/dist/plugins/resend/webhooks/emails.js +2 -0
  85. package/dist/plugins/slack/integration.test.js +18 -20
  86. package/dist/plugins/slack/schema/database.d.ts +8 -8
  87. package/dist/plugins/slack/schema/database.js +2 -2
  88. package/dist/plugins/slack/schema/index.d.ts +8 -8
  89. package/dist/plugins/slack/webhooks/reactions.js +1 -1
  90. package/dist/plugins/utils/events.d.ts +3 -3
  91. package/dist/plugins/utils/events.d.ts.map +1 -1
  92. package/dist/plugins/utils/events.js +15 -12
  93. package/dist/tests/hooks.test.js +8 -8
  94. package/dist/tests/plugins-test-utils.d.ts +3 -1
  95. package/dist/tests/plugins-test-utils.d.ts.map +1 -1
  96. package/dist/tests/plugins-test-utils.js +21 -22
  97. package/dist/tests/setup-db.d.ts +4 -3
  98. package/dist/tests/setup-db.d.ts.map +1 -1
  99. package/dist/tests/setup-db.js +7 -118
  100. package/dist/tsup.config.d.ts.map +1 -1
  101. package/dist/tsup.config.js +0 -4
  102. package/package.json +2 -37
  103. package/dist/tests/drizzle-adapter.test.d.ts +0 -2
  104. package/dist/tests/drizzle-adapter.test.d.ts.map +0 -1
  105. package/dist/tests/drizzle-adapter.test.js +0 -534
  106. package/dist/tests/multi-tenancy.test.d.ts +0 -2
  107. package/dist/tests/multi-tenancy.test.d.ts.map +0 -1
  108. package/dist/tests/multi-tenancy.test.js +0 -277
@@ -1 +1 @@
1
- {"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../adapters/drizzle/postgres.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,gBAAgB,EAMhB,MAAM,UAAU,CAAC;AAyClB,MAAM,MAAM,oBAAoB,GAAG;IAClC;;OAEG;IACH,QAAQ,EAAE,IAAI,CAAC;IACf;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAC7C;;OAEG;IACH,UAAU,CAAC,EACR;QACA,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAClC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC9B,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC9B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAC3B,GACD,SAAS,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B,CAAC;AA4KF;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC7B,EAAE,EAAE,OAAO,EACX,MAAM,EAAE,oBAAoB,GAC1B,gBAAgB,CAiIlB"}
1
+ {"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../adapters/drizzle/postgres.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACX,gBAAgB,EAMhB,MAAM,UAAU,CAAC;AAyClB,MAAM,MAAM,oBAAoB,GAAG;IAClC;;OAEG;IACH,QAAQ,EAAE,IAAI,CAAC;IACf;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAC7C;;OAEG;IACH,UAAU,CAAC,EACR;QACA,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAClC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC9B,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC9B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAC3B,GACD,SAAS,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B,CAAC;AA4KF;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC7B,EAAE,EAAE,OAAO,EACX,MAAM,EAAE,oBAAoB,GAC1B,gBAAgB,CAiIlB"}
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck we are currently removing all db adapters. leaving this for some legacy purposes but adding no-check since we are not using it
1
2
  import { and, asc, count, desc, eq, ilike, inArray, sql } from 'drizzle-orm';
2
3
  function normalizeOperator(op) {
3
4
  return op ?? '=';
@@ -1,11 +1,11 @@
1
- import type { CorsairDbAdapter } from '../../adapters/types';
1
+ import type { CorsairDatabase } from '../../db/kysely/database';
2
2
  import type { AuthTypes } from '../constants';
3
3
  import type { AccountKeyManagerFor, IntegrationKeyManagerFor } from './types';
4
4
  export type IntegrationKeyManagerOptions<T extends AuthTypes> = {
5
5
  authType: T;
6
6
  integrationName: string;
7
7
  kek: string;
8
- database: CorsairDbAdapter;
8
+ database: CorsairDatabase;
9
9
  };
10
10
  /**
11
11
  * Creates an integration-level key manager for the given auth type.
@@ -20,7 +20,7 @@ export type AccountKeyManagerOptions<T extends AuthTypes> = {
20
20
  integrationName: string;
21
21
  tenantId: string;
22
22
  kek: string;
23
- database: CorsairDbAdapter;
23
+ database: CorsairDatabase;
24
24
  };
25
25
  /**
26
26
  * Creates an account-level key manager for the given auth type.
@@ -34,10 +34,10 @@ export declare function createAccountKeyManager<T extends AuthTypes>(options: Ac
34
34
  * Initializes an integration with a new DEK.
35
35
  * Call this when creating a new integration or when setting up encryption for the first time.
36
36
  */
37
- export declare function initializeIntegrationDEK(database: CorsairDbAdapter, integrationName: string, kek: string): Promise<string>;
37
+ export declare function initializeIntegrationDEK(database: CorsairDatabase, integrationName: string, kek: string): Promise<string>;
38
38
  /**
39
39
  * Initializes an account with a new DEK.
40
40
  * Call this when creating a new account or when setting up encryption for the first time.
41
41
  */
42
- export declare function initializeAccountDEK(database: CorsairDbAdapter, integrationName: string, tenantId: string, kek: string): Promise<string>;
42
+ export declare function initializeAccountDEK(database: CorsairDatabase, integrationName: string, tenantId: string, kek: string): Promise<string>;
43
43
  //# sourceMappingURL=key-manager.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"key-manager.d.ts","sourceRoot":"","sources":["../../../core/auth/key-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAU9C,OAAO,KAAK,EAEX,oBAAoB,EAEpB,wBAAwB,EACxB,MAAM,SAAS,CAAC;AAMjB,MAAM,MAAM,4BAA4B,CAAC,CAAC,SAAS,SAAS,IAAI;IAC/D,QAAQ,EAAE,CAAC,CAAC;IACZ,eAAe,EAAE,MAAM,CAAC;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,SAAS,EAC9D,OAAO,EAAE,4BAA4B,CAAC,CAAC,CAAC,GACtC,wBAAwB,CAAC,CAAC,CAAC,CAuE7B;AAMD,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,SAAS,IAAI;IAC3D,QAAQ,EAAE,CAAC,CAAC;IACZ,eAAe,EAAE,MAAM,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,SAAS,EAC1D,OAAO,EAAE,wBAAwB,C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AMG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,SAAS,EAC1D,OAAO,EAAE,wBAAwB,CAAC,CAAC,CAAC,GAClC,oBAAoB,CAAC,CAAC,CAAC,CAuGzB;AAMD;;;GAGG;AACH,wBAAsB,wBAAwB,CAC7C,QAAQ,EAAE,eAAe,EACzB,eAAe,EAAE,MAAM,EACvB,GAAG,EAAE,MAAM,GACT,OAAO,CAAC,MAAM,CAAC,CAwBjB;AAED;;;GAGG;AACH,wBAAsB,oBAAoB,CACzC,QAAQ,EAAE,eAAe,EACzB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,GACT,OAAO,CAAC,MAAM,CAAC,CAqCjB"}
@@ -17,10 +17,11 @@ export function createIntegrationKeyManager(options) {
17
17
  getIntegration: async () => {
18
18
  if (cachedIntegration)
19
19
  return cachedIntegration;
20
- const integration = await database.findOne({
21
- table: 'corsair_integrations',
22
- where: [{ field: 'name', value: integrationName }],
23
- });
20
+ const integration = await database.db
21
+ .selectFrom('corsair_integrations')
22
+ .selectAll()
23
+ .where('name', '=', integrationName)
24
+ .executeTakeFirst();
24
25
  if (!integration) {
25
26
  throw new Error(`Integration "${integrationName}" not found. Make sure to create the integration first.`);
26
27
  }
@@ -33,15 +34,15 @@ export function createIntegrationKeyManager(options) {
33
34
  },
34
35
  updateIntegration: async (data) => {
35
36
  const integration = await ctx.getIntegration();
36
- await database.update({
37
- table: 'corsair_integrations',
38
- where: [{ field: 'id', value: integration.id }],
39
- data: {
40
- ...(data.config !== undefined ? { config: data.config } : {}),
41
- ...(data.dek !== undefined ? { dek: data.dek } : {}),
42
- updated_at: new Date(),
43
- },
44
- });
37
+ await database.db
38
+ .updateTable('corsair_integrations')
39
+ .set({
40
+ ...(data.config !== undefined ? { config: data.config } : {}),
41
+ ...(data.dek !== undefined ? { dek: data.dek } : {}),
42
+ updated_at: new Date(),
43
+ })
44
+ .where('id', '=', integration.id)
45
+ .execute();
45
46
  // Invalidate cache
46
47
  cachedIntegration = null;
47
48
  },
@@ -74,10 +75,11 @@ export function createAccountKeyManager(options) {
74
75
  const getIntegration = async () => {
75
76
  if (cachedIntegration)
76
77
  return cachedIntegration;
77
- const integration = await database.findOne({
78
- table: 'corsair_integrations',
79
- where: [{ field: 'name', value: integrationName }],
80
- });
78
+ const integration = await database.db
79
+ .selectFrom('corsair_integrations')
80
+ .selectAll()
81
+ .where('name', '=', integrationName)
82
+ .executeTakeFirst();
81
83
  if (!integration) {
82
84
  throw new Error(`Integration "${integrationName}" not found. Make sure to create the integration first.`);
83
85
  }
@@ -97,13 +99,12 @@ export function createAccountKeyManager(options) {
97
99
  if (cachedAccount)
98
100
  return cachedAccount;
99
101
  const integration = await getIntegration();
100
- const account = await database.findOne({
101
- table: 'corsair_accounts',
102
- where: [
103
- { field: 'tenant_id', value: tenantId },
104
- { field: 'integration_id', value: integration.id },
105
- ],
106
- });
102
+ const account = await database.db
103
+ .selectFrom('corsair_accounts')
104
+ .selectAll()
105
+ .where('tenant_id', '=', tenantId)
106
+ .where('integration_id', '=', integration.id)
107
+ .executeTakeFirst();
107
108
  if (!account) {
108
109
  throw new Error(`Account not found for tenant "${tenantId}" and integration "${integrationName}". Make sure to create the account first.`);
109
110
  }
@@ -116,15 +117,15 @@ export function createAccountKeyManager(options) {
116
117
  },
117
118
  updateAccount: async (data) => {
118
119
  const account = await ctx.getAccount();
119
- await database.update({
120
- table: 'corsair_accounts',
121
- where: [{ field: 'id', value: account.id }],
122
- data: {
123
- ...(data.config !== undefined ? { config: data.config } : {}),
124
- ...(data.dek !== undefined ? { dek: data.dek } : {}),
125
- updated_at: new Date(),
126
- },
127
- });
120
+ await database.db
121
+ .updateTable('corsair_accounts')
122
+ .set({
123
+ ...(data.config !== undefined ? { config: data.config } : {}),
124
+ ...(data.dek !== undefined ? { dek: data.dek } : {}),
125
+ updated_at: new Date(),
126
+ })
127
+ .where('id', '=', account.id)
128
+ .execute();
128
129
  // Invalidate cache
129
130
  cachedAccount = null;
130
131
  },
@@ -149,23 +150,24 @@ export function createAccountKeyManager(options) {
149
150
  * Call this when creating a new integration or when setting up encryption for the first time.
150
151
  */
151
152
  export async function initializeIntegrationDEK(database, integrationName, kek) {
152
- const integration = await database.findOne({
153
- table: 'corsair_integrations',
154
- where: [{ field: 'name', value: integrationName }],
155
- });
153
+ const integration = await database.db
154
+ .selectFrom('corsair_integrations')
155
+ .selectAll()
156
+ .where('name', '=', integrationName)
157
+ .executeTakeFirst();
156
158
  if (!integration) {
157
159
  throw new Error(`Integration "${integrationName}" not found.`);
158
160
  }
159
161
  const dek = generateDEK();
160
162
  const encryptedDek = await encryptDEK(dek, kek);
161
- await database.update({
162
- table: 'corsair_integrations',
163
- where: [{ field: 'id', value: integration.id }],
164
- data: {
165
- dek: encryptedDek,
166
- updated_at: new Date(),
167
- },
168
- });
163
+ await database.db
164
+ .updateTable('corsair_integrations')
165
+ .set({
166
+ dek: encryptedDek,
167
+ updated_at: new Date(),
168
+ })
169
+ .where('id', '=', integration.id)
170
+ .execute();
169
171
  return dek;
170
172
  }
171
173
  /**
@@ -173,32 +175,32 @@ export async function initializeIntegrationDEK(database, integrationName, kek) {
173
175
  * Call this when creating a new account or when setting up encryption for the first time.
174
176
  */
175
177
  export async function initializeAccountDEK(database, integrationName, tenantId, kek) {
176
- const integration = await database.findOne({
177
- table: 'corsair_integrations',
178
- where: [{ field: 'name', value: integrationName }],
179
- });
178
+ const integration = await database.db
179
+ .selectFrom('corsair_integrations')
180
+ .selectAll()
181
+ .where('name', '=', integrationName)
182
+ .executeTakeFirst();
180
183
  if (!integration) {
181
184
  throw new Error(`Integration "${integrationName}" not found.`);
182
185
  }
183
- const account = await database.findOne({
184
- table: 'corsair_accounts',
185
- where: [
186
- { field: 'tenant_id', value: tenantId },
187
- { field: 'integration_id', value: integration.id },
188
- ],
189
- });
186
+ const account = await database.db
187
+ .selectFrom('corsair_accounts')
188
+ .selectAll()
189
+ .where('tenant_id', '=', tenantId)
190
+ .where('integration_id', '=', integration.id)
191
+ .executeTakeFirst();
190
192
  if (!account) {
191
193
  throw new Error(`Account not found for tenant "${tenantId}" and integration "${integrationName}".`);
192
194
  }
193
195
  const dek = generateDEK();
194
196
  const encryptedDek = await encryptDEK(dek, kek);
195
- await database.update({
196
- table: 'corsair_accounts',
197
- where: [{ field: 'id', value: account.id }],
198
- data: {
199
- dek: encryptedDek,
200
- updated_at: new Date(),
201
- },
202
- });
197
+ await database.db
198
+ .updateTable('corsair_accounts')
199
+ .set({
200
+ dek: encryptedDek,
201
+ updated_at: new Date(),
202
+ })
203
+ .where('id', '=', account.id)
204
+ .execute();
203
205
  return dek;
204
206
  }
@@ -1,5 +1,5 @@
1
1
  import type { ZodTypeAny } from 'zod';
2
- import type { CorsairDbAdapter } from '../../adapters/types';
2
+ import type { CorsairDatabase } from '../../db/kysely/database';
3
3
  import type { CorsairPluginSchema, PluginEntityClients } from '../../db/orm';
4
4
  import type { AccountKeyManagerFor, IntegrationKeyManagerFor } from '../auth/types';
5
5
  import type { AuthTypes } from '../constants';
@@ -105,7 +105,7 @@ export type CorsairSingleTenantClient<Plugins extends readonly CorsairPlugin[]>
105
105
  keys: InferAllIntegrationKeys<Plugins>;
106
106
  };
107
107
  export type BuildCorsairClientOptions = {
108
- database: CorsairDbAdapter | undefined;
108
+ database: CorsairDatabase | undefined;
109
109
  tenantId: string | undefined;
110
110
  kek: string | undefined;
111
111
  rootErrorHandlers?: CorsairErrorHandler;
@@ -125,6 +125,6 @@ export declare function buildCorsairClient<const Plugins extends readonly Corsai
125
125
  * @param kek - Key Encryption Key for envelope encryption
126
126
  * @returns An object with key managers for each plugin
127
127
  */
128
- export declare function buildIntegrationKeys<const Plugins extends readonly CorsairPlugin[]>(plugins: Plugins, database: CorsairDbAdapter, kek: string): InferAllIntegrationKeys<Plugins>;
128
+ export declare function buildIntegrationKeys<const Plugins extends readonly CorsairPlugin[]>(plugins: Plugins, database: CorsairDatabase, kek: string): InferAllIntegrationKeys<Plugins>;
129
129
  export {};
130
130
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../core/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEtC,OAAO,KAAK,EAAE,gBAAgB,EAAgB,MAAM,sBAAsB,CAAC;AAC3E,OAAO,KAAK,EACX,mBAAmB,EAEnB,mBAAmB,EACnB,MAAM,cAAc,CAAC;AAKtB,OAAO,KAAK,EACX,oBAAoB,EACpB,wBAAwB,EACxB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEhE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACrD,OAAO,KAAK,EAAyB,aAAa,EAAE,MAAM,YAAY,CAAC;AAEvE,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAOhF;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,CAC9B,MAAM,SAAS,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,GAAG,SAAS,IACvE,MAAM,SAAS,mBAAmB,CAAC,MAAM,QAAQ,CAAC,GACnD;IAAE,EAAE,EAAE,mBAAmB,CAAC,QAAQ,CAAC,CAAA;CAAE,GACrC,EAAE,CAAC;AAMN;;GAEG;AACH,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,GAAG,KAAK,CAAC,SAAS,CAC9E,CAAC,EAAE,MAAM,CAAC,KACN,IAAI,GACN,CAAC,GACD,KAAK,CAAC;AAET;;;;;;;;;;GAUG;AACH,MAAM,MAAM,eAAe,CAC1B,O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,CAAC;CACxC,CAAC;AAMF;;;;;GAKG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,CAAC,OAAO,SAAS,SAAS,aAAa,EAAE,EAE9C,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,yBAAyB,GAChC,aAAa,CAAC,OAAO,CAAC,CA2IxB;AAMD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CACnC,KAAK,CAAC,OAAO,SAAS,SAAS,aAAa,EAAE,EAE9C,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,eAAe,EACzB,GAAG,EAAE,MAAM,GACT,uBAAuB,CAAC,OAAO,CAAC,CAmBlC"}
@@ -1,7 +1,6 @@
1
- import { withTenantAdapter } from '../../adapters/tenant';
1
+ import { createKyselyEntityClient } from '../../db/kysely/orm';
2
2
  import { createAccountKeyManager, createIntegrationKeyManager, } from '../auth/key-manager';
3
3
  import { bindEndpointsRecursively } from '../endpoints/bind';
4
- import { generateUUID } from '../utils';
5
4
  import { bindWebhooksRecursively } from '../webhooks/bind';
6
5
  // ─────────────────────────────────────────────────────────────────────────────
7
6
  // Account ID Resolver
@@ -19,21 +18,21 @@ function createAccountIdResolver(database, integrationName, tenantId) {
19
18
  throw new Error('Database not configured');
20
19
  }
21
20
  // Find the integration by name
22
- const integration = await database.findOne({
23
- table: 'corsair_integrations',
24
- where: [{ field: 'name', value: integrationName }],
25
- });
21
+ const integration = await database.db
22
+ .selectFrom('corsair_integrations')
23
+ .selectAll()
24
+ .where('name', '=', integrationName)
25
+ .executeTakeFirst();
26
26
  if (!integration) {
27
27
  throw new Error(`Integration "${integrationName}" not found. Make sure to create the integration first.`);
28
28
  }
29
29
  // Find the account for this tenant and integration
30
- const account = await database.findOne({
31
- table: 'corsair_accounts',
32
- where: [
33
- { field: 'tenant_id', value: tenantId },
34
- { field: 'integration_id', value: integration.id },
35
- ],
36
- });
30
+ const account = await database.db
31
+ .selectFrom('corsair_accounts')
32
+ .selectAll()
33
+ .where('tenant_id', '=', tenantId)
34
+ .where('integration_id', '=', integration.id)
35
+ .executeTakeFirst();
37
36
  if (!account) {
38
37
  throw new Error(`Account not found for tenant "${tenantId}" and integration "${integrationName}". Make sure to create the account first.`);
39
38
  }
@@ -41,22 +40,6 @@ function createAccountIdResolver(database, integrationName, tenantId) {
41
40
  return cachedAccountId;
42
41
  };
43
42
  }
44
- /**
45
- * Attempts to parse a value as JSON if it's a string, otherwise returns the value unchanged.
46
- * @param value - The value to parse
47
- * @returns The parsed value or the original value if parsing fails
48
- */
49
- function parseJsonLike(value) {
50
- if (typeof value === 'string') {
51
- try {
52
- return JSON.parse(value);
53
- }
54
- catch {
55
- return value;
56
- }
57
- }
58
- return value;
59
- }
60
43
  /**
61
44
  * Creates an entity client for a specific plugin and entity type with database operations.
62
45
  * The client lazily resolves the account ID when operations are performed.
@@ -68,194 +51,21 @@ function parseJsonLike(value) {
68
51
  * @returns An entity client with CRUD operations
69
52
  */
70
53
  function createEntityClient(database, getAccountId, entityTypeName, version, dataSchema) {
71
- const tableName = 'corsair_entities';
72
- function baseWhere(accountId) {
73
- return [
74
- { field: 'account_id', value: accountId },
75
- { field: 'entity_type', value: entityTypeName },
76
- ];
77
- }
78
- function parseRow(row) {
79
- const data = parseJsonLike(row.data);
80
- return { ...row, data: dataSchema.parse(data) };
54
+ if (database) {
55
+ return createKyselyEntityClient(database.db, getAccountId, entityTypeName, version, dataSchema);
81
56
  }
82
57
  return {
83
- findByEntityId: async (entityId) => {
84
- if (!database)
85
- return null;
86
- const accountId = await getAccountId();
87
- const row = await database.findOne({
88
- table: tableName,
89
- where: [
90
- ...baseWhere(accountId),
91
- { field: 'entity_id', value: entityId },
92
- ],
93
- });
94
- return row ? parseRow(row) : null;
95
- },
96
- findById: async (id) => {
97
- if (!database)
98
- return null;
99
- const accountId = await getAccountId();
100
- const row = await database.findOne({
101
- table: tableName,
102
- where: [...baseWhere(accountId), { field: 'id', value: id }],
103
- });
104
- return row ? parseRow(row) : null;
105
- },
106
- findManyByEntityIds: async (entityIds) => {
107
- if (!database || entityIds.length === 0)
108
- return [];
109
- const accountId = await getAccountId();
110
- const rows = await database.findMany({
111
- table: tableName,
112
- where: [
113
- ...baseWhere(accountId),
114
- { field: 'entity_id', operator: 'in', value: entityIds },
115
- ],
116
- });
117
- return rows.map(parseRow);
118
- },
119
- list: async (options) => {
120
- if (!database)
121
- return [];
122
- const accountId = await getAccountId();
123
- const rows = await database.findMany({
124
- table: tableName,
125
- where: baseWhere(accountId),
126
- limit: options?.limit ?? 100,
127
- offset: options?.offset ?? 0,
128
- });
129
- return rows.map(parseRow);
130
- },
131
- search: async (options) => {
132
- if (!database)
133
- return [];
134
- const accountId = await getAccountId();
135
- // Build where clauses from search options
136
- const whereConditions = [...baseWhere(accountId)];
137
- // Helper to parse filter value into operator and value
138
- function parseFilterValue(filterValue) {
139
- if (typeof filterValue === 'object' &&
140
- filterValue !== null &&
141
- !Array.isArray(filterValue)) {
142
- const obj = filterValue;
143
- if ('contains' in obj && typeof obj.contains === 'string') {
144
- return { operator: 'like', value: `%${obj.contains}%` };
145
- }
146
- if ('equals' in obj) {
147
- return { operator: '=', value: obj.equals };
148
- }
149
- }
150
- // Exact match
151
- return { operator: '=', value: filterValue };
152
- }
153
- // Reserved keys that aren't entity column filters
154
- const reservedKeys = new Set(['data', 'limit', 'offset']);
155
- // Handle entity column filters (derived from CorsairEntity)
156
- for (const [key, filterValue] of Object.entries(options)) {
157
- if (reservedKeys.has(key) || filterValue === undefined)
158
- continue;
159
- const { operator, value } = parseFilterValue(filterValue);
160
- whereConditions.push({ field: key, operator, value });
161
- }
162
- // Handle data (JSONB) filters
163
- if (options.data && typeof options.data === 'object') {
164
- for (const [key, filterValue] of Object.entries(options.data)) {
165
- if (filterValue === undefined)
166
- continue;
167
- const { operator, value } = parseFilterValue(filterValue);
168
- // Use JSONB path query syntax: data->>'key'
169
- whereConditions.push({
170
- field: `data->>'${key}'`,
171
- operator,
172
- value,
173
- });
174
- }
175
- }
176
- const rows = await database.findMany({
177
- table: tableName,
178
- where: whereConditions,
179
- limit: options.limit ?? 100,
180
- offset: options.offset ?? 0,
181
- });
182
- return rows.map(parseRow);
183
- },
184
- upsertByEntityId: async (entityId, data) => {
185
- if (!database)
186
- throw new Error('Database not configured');
187
- const accountId = await getAccountId();
188
- const parsed = dataSchema.parse(data);
189
- const now = new Date();
190
- const existing = await database.findOne({
191
- table: tableName,
192
- select: ['id'],
193
- where: [
194
- ...baseWhere(accountId),
195
- { field: 'entity_id', value: entityId },
196
- ],
197
- });
198
- if (existing?.id) {
199
- await database.update({
200
- table: tableName,
201
- where: [{ field: 'id', value: existing.id }],
202
- data: { version, data: parsed, updated_at: now },
203
- });
204
- const updated = await database.findOne({
205
- table: tableName,
206
- where: [{ field: 'id', value: existing.id }],
207
- });
208
- return parseRow(updated);
209
- }
210
- const id = generateUUID();
211
- await database.insert({
212
- table: tableName,
213
- data: {
214
- id,
215
- created_at: now,
216
- updated_at: now,
217
- account_id: accountId,
218
- entity_id: entityId,
219
- entity_type: entityTypeName,
220
- version,
221
- data: parsed,
222
- },
223
- });
224
- const inserted = await database.findOne({
225
- table: tableName,
226
- where: [{ field: 'id', value: id }],
227
- });
228
- return parseRow(inserted);
229
- },
230
- deleteById: async (id) => {
231
- if (!database)
232
- return false;
233
- const accountId = await getAccountId();
234
- const deleted = await database.deleteMany({
235
- table: tableName,
236
- where: [...baseWhere(accountId), { field: 'id', value: id }],
237
- });
238
- return deleted > 0;
239
- },
240
- deleteByEntityId: async (entityId) => {
241
- if (!database)
242
- return false;
243
- const accountId = await getAccountId();
244
- const deleted = await database.deleteMany({
245
- table: tableName,
246
- where: [
247
- ...baseWhere(accountId),
248
- { field: 'entity_id', value: entityId },
249
- ],
250
- });
251
- return deleted > 0;
252
- },
253
- count: async () => {
254
- if (!database)
255
- return 0;
256
- const accountId = await getAccountId();
257
- return database.count({ table: tableName, where: baseWhere(accountId) });
58
+ findByEntityId: async () => null,
59
+ findById: async () => null,
60
+ findManyByEntityIds: async () => [],
61
+ list: async () => [],
62
+ search: async () => [],
63
+ upsertByEntityId: async () => {
64
+ throw new Error('Database not configured');
258
65
  },
66
+ deleteById: async () => false,
67
+ deleteByEntityId: async () => false,
68
+ count: async () => 0,
259
69
  };
260
70
  }
261
71
  // ─────────────────────────────────────────────────────────────────────────────
@@ -269,7 +79,6 @@ function createEntityClient(database, getAccountId, entityTypeName, version, dat
269
79
  */
270
80
  export function buildCorsairClient(plugins, options) {
271
81
  const { database, tenantId, kek, rootErrorHandlers } = options;
272
- const scopedDatabase = database && tenantId ? withTenantAdapter(database, tenantId) : database;
273
82
  const apiUnsafe = {};
274
83
  const pluginEntitiesUnsafe = {};
275
84
  for (const plugin of plugins) {
@@ -280,12 +89,14 @@ export function buildCorsairClient(plugins, options) {
280
89
  const schema = plugin.schema;
281
90
  const effectiveTenantId = tenantId ?? 'default';
282
91
  // Create a shared account ID resolver for this plugin
283
- const getAccountId = createAccountIdResolver(scopedDatabase, plugin.id, effectiveTenantId);
92
+ const getAccountId = createAccountIdResolver(database, plugin.id, effectiveTenantId);
284
93
  // Create typed entity clients from plugin schema, nested under `db`
285
94
  if (schema?.entities) {
286
95
  const dbClients = {};
287
96
  for (const [entityTypeName, dataSchema] of Object.entries(schema.entities)) {
288
- const entityClient = createEntityClient(scopedDatabase, getAccountId, entityTypeName, schema.version, dataSchema);
97
+ const entityClient = database
98
+ ? createKyselyEntityClient(database.db, getAccountId, entityTypeName, schema.version, dataSchema)
99
+ : createEntityClient(undefined, getAccountId, entityTypeName, schema.version, dataSchema);
289
100
  dbClients[entityTypeName] = entityClient;
290
101
  }
291
102
  pluginEntitiesUnsafe[plugin.id].db = dbClients;
@@ -306,7 +117,7 @@ export function buildCorsairClient(plugins, options) {
306
117
  }
307
118
  // Build plugin context with entity clients under `db`, account ID resolver, and keys manager
308
119
  const ctxForPlugin = {
309
- database: scopedDatabase,
120
+ database,
310
121
  db: pluginEntitiesUnsafe[plugin.id]?.db ?? {},
311
122
  $getAccountId: getAccountId,
312
123
  ...(plugin.options ? { options: plugin.options } : {}),
@@ -1,4 +1,4 @@
1
- import type { CorsairDbAdapter } from '../../adapters/types';
1
+ import type { CorsairDatabase } from '../../db/kysely/database';
2
2
  /**
3
3
  * Bivariance hack for function types to ensure proper type inference.
4
4
  * @template Args - The function arguments
@@ -26,7 +26,7 @@ export type BoundEndpointTree = {
26
26
  */
27
27
  export type CorsairContext<Endpoints extends BoundEndpointTree = BoundEndpointTree> = {
28
28
  /** The configured Corsair DB adapter (if provided to `createCorsair`) */
29
- database: CorsairDbAdapter;
29
+ database?: CorsairDatabase;
30
30
  /** All bound endpoints for this plugin, allowing endpoints to call each other */
31
31
  endpoints: Endpoints;
32
32
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../core/endpoints/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAM7D;;;;GAIG;AACH,KAAK,SAAS,CAAC,IAAI,SAAS,OAAO,EAAE,EAAE,CAAC,IAAI;IAC3C,cAAc,CAAC,GAAG,IAAI,EAAE,IAAI,GAAG,CAAC,CAAC;CACjC,CAAC,gBAAgB,CAAC,CAAC;AAMpB;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,IAAI,GAAG,OAAO,EAAE,GAAG,GAAG,OAAO,IAAI,CAC5D,IAAI,EAAE,IAAI,KACN,OAAO,CAAC,GAAG,CAAC,CAAC;AAElB;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,GAAG,iBAAiB,CAAC;CACnD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,cAAc,CACzB,SAAS,SAAS,iBAAiB,GAAG,iBAAiB,IACpD;IACH,yEAAyE;IACzE,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,iFAAiF;IACjF,SAAS,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF;;;;;GAKG;A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AAC;AAElB;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,GAAG,iBAAiB,CAAC;CACnD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,cAAc,CACzB,SAAS,SAAS,iBAAiB,GAAG,iBAAiB,IACpD;IACH,yEAAyE;IACzE,QAAQ,CAAC,EAAE,eAAe,CAAC;IAC3B,iFAAiF;IACjF,SAAS,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,eAAe,CAC1B,GAAG,SAAS,cAAc,GAAG,cAAc,EAC3C,IAAI,GAAG,OAAO,EACd,GAAG,GAAG,OAAO,IACV,SAAS,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;AAMpD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,YAAY,GAAG;IAC1B,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,YAAY,CAAC;CAC7D,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,YAAY,IAAI;KAClD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,GAChE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GACvB,CAAC,CAAC,CAAC,CAAC,SAAS,YAAY,GACxB,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACnB,KAAK;CACT,CAAC"}
@@ -1,10 +1,10 @@
1
- import type { CorsairDbAdapter } from '../adapters/types';
1
+ import type { CorsairDatabase } from '../db/kysely/database';
2
2
  import type { CorsairSingleTenantClient, CorsairTenantWrapper } from './client';
3
3
  import type { CorsairIntegration, CorsairPlugin } from './plugins';
4
4
  export declare const CORSAIR_INTERNAL: unique symbol;
5
5
  export type CorsairInternalConfig = {
6
6
  plugins: readonly CorsairPlugin[];
7
- database: CorsairDbAdapter | undefined;
7
+ database: CorsairDatabase | undefined;
8
8
  kek: string;
9
9
  multiTenancy: boolean;
10
10
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../core/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1D,OAAO,KAAK,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAEhF,OAAO,KAAK,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAMnE,eAAO,MAAM,gBAAgB,eAAiC,CAAC;AAE/D,MAAM,MAAM,qBAAqB,GAAG;IACnC,OAAO,EAAE,SAAS,aAAa,EAAE,CAAC;IAClC,QAAQ,EAAE,gBAAgB,GAAG,SAAS,CAAC;IACvC,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,OAAO,CAAC;CACtB,CAAC;AAMF;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,CAAC,OAAO,SAAS,SAAS,aAAa,EAAE,EAC3E,MAAM,EAAE,kBAAkB,CAAC,OAAO,CAAC,GAAG;IAAE,YAAY,EAAE,IAAI,CAAA;CAAE,GAC1D,oBAAoB,CAAC,OAAO,CAAC,CAAC;AAEjC;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,CAAC,OAAO,SAAS,SAAS,aAAa,EAAE,EAC3E,MAAM,EAAE,kBAAkB,CAAC,OAAO,CAAC,GAAG;IAAE,YAAY,CAAC,EAAE,KAAK,GAAG,SAAS,CAAA;CAAE,GACxE,yBAAyB,CAAC,OAAO,CAAC,CAAC;AAqEtC,YAAY,EACX,gBAAgB,EAChB,gBAAgB,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