@coursebuilder/adapter-drizzle 1.0.1 → 1.0.3

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 (27) hide show
  1. package/dist/chunk-253RMSB3.js +99 -0
  2. package/dist/{chunk-MOV5TUON.js → chunk-5QEGBX2O.js} +52 -21
  3. package/dist/chunk-ORRM7KVW.js +25 -0
  4. package/dist/index.js +3 -1
  5. package/dist/lib/mysql/index.cjs +301 -175
  6. package/dist/lib/mysql/index.cjs.map +1 -1
  7. package/dist/lib/mysql/index.d.cts +518 -0
  8. package/dist/lib/mysql/index.d.ts +518 -0
  9. package/dist/lib/mysql/index.js +3 -1
  10. package/dist/lib/mysql/schemas/entitlements/entitlement-type.cjs +46 -0
  11. package/dist/lib/mysql/schemas/entitlements/entitlement-type.cjs.map +1 -0
  12. package/dist/lib/mysql/schemas/entitlements/entitlement-type.d.cts +60 -0
  13. package/dist/lib/mysql/schemas/entitlements/entitlement-type.d.ts +60 -0
  14. package/dist/lib/mysql/schemas/entitlements/entitlement-type.js +8 -0
  15. package/dist/lib/mysql/schemas/entitlements/entitlement-type.js.map +1 -0
  16. package/dist/lib/mysql/schemas/entitlements/entitlement.cjs +351 -0
  17. package/dist/lib/mysql/schemas/entitlements/entitlement.cjs.map +1 -0
  18. package/dist/lib/mysql/schemas/entitlements/entitlement.d.cts +210 -0
  19. package/dist/lib/mysql/schemas/entitlements/entitlement.d.ts +210 -0
  20. package/dist/lib/mysql/schemas/entitlements/entitlement.js +21 -0
  21. package/dist/lib/mysql/schemas/entitlements/entitlement.js.map +1 -0
  22. package/dist/lib/utils.d.cts +1 -1
  23. package/dist/lib/utils.d.ts +1 -1
  24. package/package.json +2 -2
  25. package/src/lib/mysql/index.ts +43 -14
  26. package/src/lib/mysql/schemas/entitlements/entitlement-type.ts +21 -0
  27. package/src/lib/mysql/schemas/entitlements/entitlement.ts +66 -0
@@ -0,0 +1,21 @@
1
+ import {
2
+ getEntitlementRelationsSchema,
3
+ getEntitlementsSchema
4
+ } from "../../../../chunk-253RMSB3.js";
5
+ import "../../../../chunk-LPJ2P2KJ.js";
6
+ import "../../../../chunk-MP4CUWYC.js";
7
+ import "../../../../chunk-WJWJ6E5Y.js";
8
+ import "../../../../chunk-EHRARBRS.js";
9
+ import "../../../../chunk-BLDW3QUS.js";
10
+ import "../../../../chunk-DKJO4Y3K.js";
11
+ import "../../../../chunk-WPJZAYX7.js";
12
+ import "../../../../chunk-6MRFUIH5.js";
13
+ import "../../../../chunk-MGZMGDQ5.js";
14
+ import "../../../../chunk-KPBVZ44W.js";
15
+ import "../../../../chunk-LZHZDFNZ.js";
16
+ import "../../../../chunk-H736K5TN.js";
17
+ export {
18
+ getEntitlementRelationsSchema,
19
+ getEntitlementsSchema
20
+ };
21
+ //# sourceMappingURL=entitlement.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,4 +1,4 @@
1
- import { MySqlDatabase, AnyMySqlTable, MySqlTableFn } from 'drizzle-orm/mysql-core';
1
+ import { MySqlDatabase, MySqlTableFn, AnyMySqlTable } from 'drizzle-orm/mysql-core';
2
2
  import { PgDatabase, PgTableFn } from 'drizzle-orm/pg-core';
3
3
  import { BaseSQLiteDatabase, SQLiteTableFn, AnySQLiteTable } from 'drizzle-orm/sqlite-core';
4
4
  import { DefaultSchema } from './mysql/index.cjs';
@@ -1,4 +1,4 @@
1
- import { MySqlDatabase, AnyMySqlTable, MySqlTableFn } from 'drizzle-orm/mysql-core';
1
+ import { MySqlDatabase, MySqlTableFn, AnyMySqlTable } from 'drizzle-orm/mysql-core';
2
2
  import { PgDatabase, PgTableFn } from 'drizzle-orm/pg-core';
3
3
  import { BaseSQLiteDatabase, SQLiteTableFn, AnySQLiteTable } from 'drizzle-orm/sqlite-core';
4
4
  import { DefaultSchema } from './mysql/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coursebuilder/adapter-drizzle",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Drizzle adapter for Course Builder.",
5
5
  "keywords": [
6
6
  "coursebuilder",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@auth/core": "0.37.2",
46
- "@coursebuilder/core": "1.0.1",
46
+ "@coursebuilder/core": "1.0.3",
47
47
  "@libsql/client": "0.5.6",
48
48
  "@types/better-sqlite3": "7.6.9",
49
49
  "@types/uuid": "9.0.8",
@@ -208,6 +208,11 @@ import {
208
208
  getTagTagSchema,
209
209
  } from './schemas/content/tag-tag.js'
210
210
  import { getTagRelationsSchema, getTagSchema } from './schemas/content/tag.js'
211
+ import { getEntitlementTypesSchema } from './schemas/entitlements/entitlement-type.js'
212
+ import {
213
+ getEntitlementRelationsSchema,
214
+ getEntitlementsSchema,
215
+ } from './schemas/entitlements/entitlement.js'
211
216
  import {
212
217
  getOrganizationMembershipRolesRelationsSchema,
213
218
  getOrganizationMembershipRolesSchema,
@@ -319,6 +324,9 @@ export function getCourseBuilderSchema(mysqlTable: MySqlTableFn) {
319
324
  subscriptionRelations: getSubscriptionRelationsSchema(mysqlTable),
320
325
  profiles: getProfilesSchema(mysqlTable),
321
326
  profilesRelations: getProfilesRelationsSchema(mysqlTable),
327
+ entitlementTypes: getEntitlementTypesSchema(mysqlTable),
328
+ entitlements: getEntitlementsSchema(mysqlTable),
329
+ entitlementsRelations: getEntitlementRelationsSchema(mysqlTable),
322
330
  } as const
323
331
  }
324
332
 
@@ -710,6 +718,7 @@ export function mySqlDrizzleAdapter(
710
718
  upgradedFromPurchaseId,
711
719
  country,
712
720
  usedCouponId,
721
+ organizationId,
713
722
  } = options
714
723
 
715
724
  const existingMerchantCharge = merchantChargeSchema.nullable().parse(
@@ -798,6 +807,7 @@ export function mySqlDrizzleAdapter(
798
807
  .update(coupon)
799
808
  .set({
800
809
  maxUses: (existingBulkCoupon?.maxUses || 0) + quantity,
810
+ ...(organizationId ? { organizationId } : {}),
801
811
  })
802
812
  .where(eq(coupon.id, bulkCouponId))
803
813
  } else {
@@ -815,6 +825,7 @@ export function mySqlDrizzleAdapter(
815
825
  restrictedToProductId: productId,
816
826
  maxUses: quantity,
817
827
  status: 1,
828
+ ...(organizationId ? { organizationId } : {}),
818
829
  ...(merchantCouponToUse
819
830
  ? {
820
831
  merchantCouponId: merchantCouponToUse.id,
@@ -865,6 +876,7 @@ export function mySqlDrizzleAdapter(
865
876
  country,
866
877
  upgradedFromId: upgradedFromPurchaseId || null,
867
878
  couponId: usedCouponId || null,
879
+ ...(organizationId ? { organizationId } : {}),
868
880
  })
869
881
 
870
882
  const oneWeekInMilliseconds = 1000 * 60 * 60 * 24 * 7
@@ -876,6 +888,7 @@ export function mySqlDrizzleAdapter(
876
888
  ? new Date()
877
889
  : new Date(Date.now() + oneWeekInMilliseconds),
878
890
  sourceUserId: userId,
891
+ ...(organizationId ? { organizationId } : {}),
879
892
  })
880
893
 
881
894
  // const result = await Promise.all([
@@ -1504,6 +1517,7 @@ export function mySqlDrizzleAdapter(
1504
1517
  ...fieldsNoImage,
1505
1518
  ...(image?.url && { image }),
1506
1519
  },
1520
+ type: input.type,
1507
1521
  })
1508
1522
  .where(eq(products.id, currentProduct.id))
1509
1523
 
@@ -2713,21 +2727,36 @@ export function mySqlDrizzleAdapter(
2713
2727
  userId: string
2714
2728
  invitedById: string
2715
2729
  }) => {
2716
- const id = crypto.randomUUID()
2717
- await client.insert(organizationMembershipTable).values({
2718
- ...options,
2719
- id,
2720
- })
2721
-
2722
- return OrganizationMemberSchema.parse(
2730
+ const currentMembership =
2723
2731
  await client.query.organizationMemberships.findFirst({
2724
- where: eq(organizationMembershipTable.id, id),
2725
- with: {
2726
- organization: true,
2727
- user: true,
2728
- },
2729
- }),
2730
- )
2732
+ where: and(
2733
+ eq(
2734
+ organizationMembershipTable.organizationId,
2735
+ options.organizationId,
2736
+ ),
2737
+ eq(organizationMembershipTable.userId, options.userId),
2738
+ ),
2739
+ })
2740
+
2741
+ if (currentMembership) {
2742
+ return OrganizationMemberSchema.parse(currentMembership)
2743
+ } else {
2744
+ const id = crypto.randomUUID()
2745
+ await client.insert(organizationMembershipTable).values({
2746
+ ...options,
2747
+ id,
2748
+ })
2749
+
2750
+ return OrganizationMemberSchema.parse(
2751
+ await client.query.organizationMemberships.findFirst({
2752
+ where: eq(organizationMembershipTable.id, id),
2753
+ with: {
2754
+ organization: true,
2755
+ user: true,
2756
+ },
2757
+ }),
2758
+ )
2759
+ }
2731
2760
  },
2732
2761
  removeMemberFromOrganization: async (options: {
2733
2762
  organizationId: string
@@ -0,0 +1,21 @@
1
+ import {
2
+ MySqlTableFn,
3
+ text,
4
+ uniqueIndex,
5
+ varchar,
6
+ } from 'drizzle-orm/mysql-core'
7
+
8
+ export function getEntitlementTypesSchema(mysqlTable: MySqlTableFn) {
9
+ return mysqlTable(
10
+ 'EntitlementType',
11
+ {
12
+ id: varchar('id', { length: 191 }).notNull().primaryKey(),
13
+ name: varchar('name', { length: 255 }).notNull(),
14
+ description: text('description'),
15
+ // Add any type-specific config fields here
16
+ },
17
+ (table) => ({
18
+ uniqueName: uniqueIndex('unique_name_idx').on(table.name),
19
+ }),
20
+ )
21
+ }
@@ -0,0 +1,66 @@
1
+ import { relations, sql } from 'drizzle-orm'
2
+ import {
3
+ index,
4
+ json,
5
+ MySqlTableFn,
6
+ timestamp,
7
+ varchar,
8
+ } from 'drizzle-orm/mysql-core'
9
+
10
+ import { getUsersSchema } from '../auth/users.js'
11
+ import { getOrganizationMembershipsSchema } from '../org/organization-memberships.js'
12
+ import { getOrganizationsSchema } from '../org/organizations.js'
13
+
14
+ export function getEntitlementsSchema(mysqlTable: MySqlTableFn) {
15
+ return mysqlTable(
16
+ 'Entitlement',
17
+ {
18
+ id: varchar('id', { length: 191 }).notNull().primaryKey(),
19
+ entitlementType: varchar('entitlementType', { length: 255 }).notNull(),
20
+ userId: varchar('userId', { length: 191 }),
21
+ organizationId: varchar('organizationId', { length: 191 }),
22
+ organizationMembershipId: varchar('organizationMembershipId', {
23
+ length: 191,
24
+ }),
25
+ sourceType: varchar('sourceType', { length: 255 }).notNull(), // 'PURCHASE' | 'SUBSCRIPTION' | 'MANUAL' etc
26
+ sourceId: varchar('sourceId', { length: 191 }).notNull(),
27
+ metadata: json('metadata').$type<Record<string, any>>().default({}),
28
+ expiresAt: timestamp('expiresAt', { mode: 'date', fsp: 3 }),
29
+ createdAt: timestamp('createdAt', { mode: 'date', fsp: 3 })
30
+ .default(sql`CURRENT_TIMESTAMP(3)`)
31
+ .notNull(),
32
+ updatedAt: timestamp('updatedAt', { mode: 'date', fsp: 3 })
33
+ .default(sql`CURRENT_TIMESTAMP(3)`)
34
+ .notNull(),
35
+ deletedAt: timestamp('deletedAt', { mode: 'date', fsp: 3 }),
36
+ },
37
+ (table) => ({
38
+ userIdIdx: index('userId_idx').on(table.userId),
39
+ orgIdIdx: index('orgId_idx').on(table.organizationId),
40
+ sourceIdx: index('source_idx').on(table.sourceType, table.sourceId),
41
+ typeIdx: index('type_idx').on(table.entitlementType),
42
+ }),
43
+ )
44
+ }
45
+
46
+ export function getEntitlementRelationsSchema(mysqlTable: MySqlTableFn) {
47
+ const entitlements = getEntitlementsSchema(mysqlTable)
48
+ const users = getUsersSchema(mysqlTable)
49
+ const orgs = getOrganizationsSchema(mysqlTable)
50
+ const memberships = getOrganizationMembershipsSchema(mysqlTable)
51
+
52
+ return relations(entitlements, ({ one }) => ({
53
+ user: one(users, {
54
+ fields: [entitlements.userId],
55
+ references: [users.id],
56
+ }),
57
+ organization: one(orgs, {
58
+ fields: [entitlements.organizationId],
59
+ references: [orgs.id],
60
+ }),
61
+ membership: one(memberships, {
62
+ fields: [entitlements.organizationMembershipId],
63
+ references: [memberships.id],
64
+ }),
65
+ }))
66
+ }