@coursebuilder/adapter-drizzle 0.1.2 → 0.1.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 (100) hide show
  1. package/dist/{chunk-ISURSRBK.js → chunk-3LWFRGZT.js} +519 -313
  2. package/dist/{chunk-RVYJDDZD.js → chunk-NXFHRDLS.js} +2 -2
  3. package/dist/{chunk-44VDBYFU.js → chunk-QK6CINAJ.js} +3 -3
  4. package/dist/{chunk-MYRB6GLJ.js → chunk-ULABK6XG.js} +2 -2
  5. package/dist/{chunk-DHYYRN35.js → chunk-XSXWLOIN.js} +4624 -448
  6. package/dist/{chunk-QZOBGJSV.js → chunk-XXZQBN6S.js} +2 -2
  7. package/dist/index.d.ts +0 -2
  8. package/dist/index.js +11 -27
  9. package/dist/lib/mysql/index.cjs +880 -721
  10. package/dist/lib/mysql/index.cjs.map +1 -1
  11. package/dist/lib/mysql/index.d.ts +212 -10
  12. package/dist/lib/mysql/index.js +11 -14
  13. package/dist/lib/mysql/schemas/auth/accounts.cjs +73 -57
  14. package/dist/lib/mysql/schemas/auth/accounts.cjs.map +1 -1
  15. package/dist/lib/mysql/schemas/auth/accounts.js +5 -4
  16. package/dist/lib/mysql/schemas/auth/role-permissions.cjs +64 -48
  17. package/dist/lib/mysql/schemas/auth/role-permissions.cjs.map +1 -1
  18. package/dist/lib/mysql/schemas/auth/role-permissions.js +6 -5
  19. package/dist/lib/mysql/schemas/auth/roles.cjs +64 -48
  20. package/dist/lib/mysql/schemas/auth/roles.cjs.map +1 -1
  21. package/dist/lib/mysql/schemas/auth/roles.js +5 -4
  22. package/dist/lib/mysql/schemas/auth/sessions.cjs +64 -48
  23. package/dist/lib/mysql/schemas/auth/sessions.cjs.map +1 -1
  24. package/dist/lib/mysql/schemas/auth/sessions.js +6 -5
  25. package/dist/lib/mysql/schemas/auth/user-permissions.cjs +65 -49
  26. package/dist/lib/mysql/schemas/auth/user-permissions.cjs.map +1 -1
  27. package/dist/lib/mysql/schemas/auth/user-permissions.js +5 -4
  28. package/dist/lib/mysql/schemas/auth/user-roles.cjs +67 -51
  29. package/dist/lib/mysql/schemas/auth/user-roles.cjs.map +1 -1
  30. package/dist/lib/mysql/schemas/auth/user-roles.js +5 -4
  31. package/dist/lib/mysql/schemas/auth/users.cjs +170 -154
  32. package/dist/lib/mysql/schemas/auth/users.cjs.map +1 -1
  33. package/dist/lib/mysql/schemas/auth/users.js +5 -4
  34. package/dist/lib/mysql/schemas/commerce/coupon.cjs +1 -1
  35. package/dist/lib/mysql/schemas/commerce/coupon.cjs.map +1 -1
  36. package/dist/lib/mysql/schemas/commerce/coupon.d.ts +4 -4
  37. package/dist/lib/mysql/schemas/commerce/coupon.js +1 -1
  38. package/dist/lib/mysql/schemas/commerce/price.cjs +189 -11
  39. package/dist/lib/mysql/schemas/commerce/price.cjs.map +1 -1
  40. package/dist/lib/mysql/schemas/commerce/price.d.ts +6 -1
  41. package/dist/lib/mysql/schemas/commerce/price.js +14 -3
  42. package/dist/lib/mysql/schemas/commerce/product.cjs +227 -9
  43. package/dist/lib/mysql/schemas/commerce/product.cjs.map +1 -1
  44. package/dist/lib/mysql/schemas/commerce/product.d.ts +8 -2
  45. package/dist/lib/mysql/schemas/commerce/product.js +12 -1
  46. package/dist/lib/mysql/schemas/commerce/purchase.cjs +140 -122
  47. package/dist/lib/mysql/schemas/commerce/purchase.cjs.map +1 -1
  48. package/dist/lib/mysql/schemas/commerce/purchase.js +5 -4
  49. package/dist/lib/mysql/schemas/commerce/upgradable-products.cjs +107 -24
  50. package/dist/lib/mysql/schemas/commerce/upgradable-products.cjs.map +1 -1
  51. package/dist/lib/mysql/schemas/commerce/upgradable-products.js +11 -2
  52. package/dist/lib/mysql/schemas/communication/communication-preferences.cjs +84 -68
  53. package/dist/lib/mysql/schemas/communication/communication-preferences.cjs.map +1 -1
  54. package/dist/lib/mysql/schemas/communication/communication-preferences.js +5 -4
  55. package/dist/lib/mysql/schemas/content/content-contributions.cjs +92 -76
  56. package/dist/lib/mysql/schemas/content/content-contributions.cjs.map +1 -1
  57. package/dist/lib/mysql/schemas/content/content-contributions.js +5 -4
  58. package/dist/lib/mysql/schemas/content/content-resource-product.cjs +243 -0
  59. package/dist/lib/mysql/schemas/content/content-resource-product.cjs.map +1 -0
  60. package/dist/lib/mysql/schemas/content/content-resource-product.d.ts +101 -0
  61. package/dist/lib/mysql/schemas/content/content-resource-product.js +19 -0
  62. package/dist/lib/mysql/schemas/content/content-resource-resource.cjs +67 -51
  63. package/dist/lib/mysql/schemas/content/content-resource-resource.cjs.map +1 -1
  64. package/dist/lib/mysql/schemas/content/content-resource-resource.js +5 -4
  65. package/dist/lib/mysql/schemas/content/content-resource.cjs +116 -62
  66. package/dist/lib/mysql/schemas/content/content-resource.cjs.map +1 -1
  67. package/dist/lib/mysql/schemas/content/content-resource.d.ts +1 -0
  68. package/dist/lib/mysql/schemas/content/content-resource.js +5 -4
  69. package/dist/lib/utils.cjs.map +1 -1
  70. package/dist/lib/utils.d.ts +3 -7
  71. package/dist/lib/utils.js +12 -3
  72. package/dist/lib/utils.js.map +1 -1
  73. package/package.json +2 -2
  74. package/src/index.ts +0 -8
  75. package/src/lib/mysql/index.ts +154 -23
  76. package/src/lib/mysql/schemas/auth/user-roles.ts +2 -2
  77. package/src/lib/mysql/schemas/commerce/coupon.ts +2 -2
  78. package/src/lib/mysql/schemas/commerce/price.ts +22 -1
  79. package/src/lib/mysql/schemas/commerce/product.ts +26 -2
  80. package/src/lib/mysql/schemas/content/content-resource-product.ts +62 -0
  81. package/src/lib/mysql/schemas/content/content-resource.ts +5 -0
  82. package/src/lib/utils.ts +1 -9
  83. package/dist/chunk-7LAAAEVE.js +0 -379
  84. package/dist/chunk-AFQGYW7Z.js +0 -45
  85. package/dist/chunk-CPE4P26C.js +0 -42
  86. package/dist/chunk-GFUUJRX7.js +0 -18
  87. package/dist/chunk-O2KKII62.js +0 -381
  88. package/dist/chunk-Y34AIPU7.js +0 -4137
  89. package/dist/lib/pg.cjs +0 -4527
  90. package/dist/lib/pg.cjs.map +0 -1
  91. package/dist/lib/pg.d.ts +0 -496
  92. package/dist/lib/pg.js +0 -11
  93. package/dist/lib/sqlite.cjs +0 -4536
  94. package/dist/lib/sqlite.cjs.map +0 -1
  95. package/dist/lib/sqlite.d.ts +0 -496
  96. package/dist/lib/sqlite.js +0 -12
  97. package/dist/lib/sqlite.js.map +0 -1
  98. package/src/lib/pg.ts +0 -579
  99. package/src/lib/sqlite.ts +0 -562
  100. /package/dist/lib/{pg.js.map → mysql/schemas/content/content-resource-product.js.map} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coursebuilder/adapter-drizzle",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Drizzle adapter for Course Builder.",
5
5
  "keywords": [
6
6
  "coursebuilder",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@auth/core": "^0.28.1",
43
- "@coursebuilder/core": "0.1.3",
43
+ "@coursebuilder/core": "0.1.4",
44
44
  "@libsql/client": "0.5.6",
45
45
  "@types/better-sqlite3": "7.6.9",
46
46
  "@types/uuid": "9.0.8",
package/src/index.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  import { is } from 'drizzle-orm'
2
2
  import { MySqlDatabase, type MySqlTableFn } from 'drizzle-orm/mysql-core'
3
- import { PgDatabase, type PgTableFn } from 'drizzle-orm/pg-core'
4
- import { BaseSQLiteDatabase, type SQLiteTableFn } from 'drizzle-orm/sqlite-core'
5
3
 
6
4
  import { type CourseBuilderAdapter } from '@coursebuilder/core/adapters'
7
5
 
@@ -9,8 +7,6 @@ import {
9
7
  createTables as createMySqlTables,
10
8
  mySqlDrizzleAdapter,
11
9
  } from './lib/mysql/index.js'
12
- import { pgDrizzleAdapter } from './lib/pg.js'
13
- import { SQLiteDrizzleAdapter } from './lib/sqlite.js'
14
10
  import { type SqlFlavorOptions, type TableFn } from './lib/utils.js'
15
11
 
16
12
  export function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(
@@ -19,10 +15,6 @@ export function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(
19
15
  ): CourseBuilderAdapter {
20
16
  if (is(db, MySqlDatabase)) {
21
17
  return mySqlDrizzleAdapter(db, table as MySqlTableFn)
22
- } else if (is(db, PgDatabase)) {
23
- return pgDrizzleAdapter(db, table as PgTableFn)
24
- } else if (is(db, BaseSQLiteDatabase)) {
25
- return SQLiteDrizzleAdapter(db, table as SQLiteTableFn)
26
18
  }
27
19
 
28
20
  throw new Error(
@@ -19,8 +19,11 @@ import {
19
19
  merchantCouponSchema,
20
20
  MerchantCustomer,
21
21
  MerchantProduct,
22
+ merchantProductSchema,
22
23
  Price,
24
+ priceSchema,
23
25
  Product,
26
+ productSchema,
24
27
  Purchase,
25
28
  purchaseSchema,
26
29
  PurchaseUserTransfer,
@@ -79,7 +82,10 @@ import { getMerchantPriceSchema } from './schemas/commerce/merchant-price.js'
79
82
  import { getMerchantProductSchema } from './schemas/commerce/merchant-product.js'
80
83
  import { getMerchantSessionSchema } from './schemas/commerce/merchant-session.js'
81
84
  import { getPriceSchema } from './schemas/commerce/price.js'
82
- import { getProductSchema } from './schemas/commerce/product.js'
85
+ import {
86
+ getProductRelationsSchema,
87
+ getProductSchema,
88
+ } from './schemas/commerce/product.js'
83
89
  import { getPurchaseUserTransferSchema } from './schemas/commerce/purchase-user-transfer.js'
84
90
  import {
85
91
  getPurchaseRelationsSchema,
@@ -99,6 +105,10 @@ import {
99
105
  getContentContributionRelationsSchema,
100
106
  getContentContributionsSchema,
101
107
  } from './schemas/content/content-contributions.js'
108
+ import {
109
+ getContentResourceProductRelationsSchema,
110
+ getContentResourceProductSchema,
111
+ } from './schemas/content/content-resource-product.js'
102
112
  import {
103
113
  getContentResourceResourceRelationsSchema,
104
114
  getContentResourceResourceSchema,
@@ -165,6 +175,10 @@ export function getCourseBuilderSchema(mysqlTable: MySqlTableFn) {
165
175
  upgradableProducts: getUpgradableProductsSchema(mysqlTable),
166
176
  upgradableProductsRelations:
167
177
  getUpgradableProductsRelationsSchema(mysqlTable),
178
+ contentResourceProduct: getContentResourceProductSchema(mysqlTable),
179
+ contentResourceProductRelations:
180
+ getContentResourceProductRelationsSchema(mysqlTable),
181
+ productRelations: getProductRelationsSchema(mysqlTable),
168
182
  } as const
169
183
  }
170
184
 
@@ -205,13 +219,20 @@ export function mySqlDrizzleAdapter(
205
219
  availableUpgradesForProduct(
206
220
  purchases: any,
207
221
  productId: string,
208
- ): Promise<
209
- {
210
- upgradableTo: { id: string; name: string }
211
- upgradableFrom: { id: string; name: string }
212
- }[]
213
- > {
214
- throw new Error('Method not implemented.')
222
+ ): Promise<any[]> {
223
+ const previousPurchaseProductIds = purchases.map(
224
+ ({ productId }: Purchase) => productId,
225
+ )
226
+
227
+ return client.query.upgradableProducts.findMany({
228
+ where: and(
229
+ eq(upgradableProducts.upgradableToId, productId),
230
+ inArray(
231
+ upgradableProducts.upgradableFromId,
232
+ previousPurchaseProductIds,
233
+ ),
234
+ ),
235
+ })
215
236
  },
216
237
  clearLessonProgressForUser(options: {
217
238
  userId: string
@@ -448,8 +469,16 @@ export function mySqlDrizzleAdapter(
448
469
  }> {
449
470
  throw new Error('Method not implemented.')
450
471
  },
451
- getCoupon(couponIdOrCode: string): Promise<Coupon | null> {
452
- throw new Error('Method not implemented.')
472
+ async getCoupon(couponIdOrCode: string): Promise<Coupon | null> {
473
+ return couponSchema.nullable().parse(
474
+ await client
475
+ .select()
476
+ .from(coupon)
477
+ .where(
478
+ or(eq(coupon.id, couponIdOrCode), eq(coupon.code, couponIdOrCode)),
479
+ )
480
+ .then((res) => res[0] ?? null),
481
+ )
453
482
  },
454
483
  getCouponWithBulkPurchases(couponId: string): Promise<
455
484
  | (Coupon & {
@@ -484,21 +513,86 @@ export function mySqlDrizzleAdapter(
484
513
  ): Promise<MerchantCharge | null> {
485
514
  throw new Error('Method not implemented.')
486
515
  },
487
- getMerchantCoupon(
516
+ async getMerchantCouponsForTypeAndPercent(params: {
517
+ type: string
518
+ percentageDiscount: number
519
+ }): Promise<MerchantCoupon[]> {
520
+ return z.array(merchantCouponSchema).parse(
521
+ await client.query.merchantCoupon.findMany({
522
+ where: and(
523
+ eq(merchantCoupon.type, params.type),
524
+ eq(
525
+ merchantCoupon.percentageDiscount,
526
+ params.percentageDiscount.toString(),
527
+ ),
528
+ ),
529
+ }),
530
+ )
531
+ },
532
+ async getMerchantCouponForTypeAndPercent(params: {
533
+ type: string
534
+ percentageDiscount: number
535
+ }): Promise<MerchantCoupon | null> {
536
+ const foundMerchantCoupon = await client.query.merchantCoupon.findFirst({
537
+ where: and(
538
+ eq(merchantCoupon.type, params.type),
539
+ eq(
540
+ merchantCoupon.percentageDiscount,
541
+ params.percentageDiscount.toString(),
542
+ ),
543
+ ),
544
+ })
545
+
546
+ const parsed = merchantCouponSchema
547
+ .nullable()
548
+ .safeParse(foundMerchantCoupon)
549
+ if (parsed.success) {
550
+ return parsed.data
551
+ }
552
+
553
+ return null
554
+ },
555
+ async getMerchantCoupon(
488
556
  merchantCouponId: string,
489
557
  ): Promise<MerchantCoupon | null> {
490
- throw new Error('Method not implemented.')
558
+ const foundMerchantCoupon = await client.query.merchantCoupon.findFirst({
559
+ where: eq(merchantCoupon.id, merchantCouponId),
560
+ })
561
+
562
+ const parsed = merchantCouponSchema
563
+ .nullable()
564
+ .safeParse(foundMerchantCoupon)
565
+ if (parsed.success) {
566
+ return parsed.data
567
+ }
568
+
569
+ return null
491
570
  },
492
- getMerchantProduct(
571
+ async getMerchantProduct(
493
572
  stripeProductId: string,
494
573
  ): Promise<MerchantProduct | null> {
495
- throw new Error('Method not implemented.')
574
+ return merchantProductSchema.nullable().parse(
575
+ await client.query.merchantProduct.findFirst({
576
+ where: eq(merchantProduct.identifier, stripeProductId),
577
+ }),
578
+ )
496
579
  },
497
580
  getPrice(productId: string): Promise<Price | null> {
498
581
  throw new Error('Method not implemented.')
499
582
  },
500
- getProduct(productId: string): Promise<Product | null> {
501
- throw new Error('Method not implemented.')
583
+ async getPriceForProduct(productId: string): Promise<Price | null> {
584
+ return priceSchema.nullable().parse(
585
+ await client.query.prices.findFirst({
586
+ where: eq(prices.productId, productId),
587
+ }),
588
+ )
589
+ },
590
+ async getProduct(productId: string): Promise<Product | null> {
591
+ return productSchema.nullable().parse(
592
+ await client.query.products.findFirst({
593
+ where: eq(products.id, productId),
594
+ }),
595
+ )
502
596
  },
503
597
  async getPurchase(purchaseId: string): Promise<Purchase | null> {
504
598
  const purchase = await client.query.purchases.findFirst({
@@ -510,7 +604,6 @@ export function mySqlDrizzleAdapter(
510
604
  async createPurchase(options): Promise<Purchase> {
511
605
  const newPurchaseId = options.id || `purchase-${v4()}`
512
606
  await client.insert(purchaseTable).values({
513
- state: 'Valid',
514
607
  ...options,
515
608
  id: newPurchaseId,
516
609
  })
@@ -524,7 +617,7 @@ export function mySqlDrizzleAdapter(
524
617
  userId: string,
525
618
  ): Promise<{
526
619
  purchase?: Purchase
527
- existingPurchase?: Purchase
620
+ existingPurchase?: Purchase & { product?: Product | null }
528
621
  availableUpgrades: UpgradableProduct[]
529
622
  }> {
530
623
  const allPurchases = await this.getPurchasesForUser(userId)
@@ -641,7 +734,7 @@ export function mySqlDrizzleAdapter(
641
734
  const userPurchases = await client.query.purchases.findMany({
642
735
  where: and(
643
736
  eq(purchaseTable.userId, userId),
644
- inArray(purchaseTable.state, visiblePurchaseStates),
737
+ inArray(purchaseTable.status, visiblePurchaseStates),
645
738
  ),
646
739
  with: {
647
740
  user: true,
@@ -667,11 +760,50 @@ export function mySqlDrizzleAdapter(
667
760
  }),
668
761
  )
669
762
  },
670
- pricesOfPurchasesTowardOneBundle(options: {
763
+ async pricesOfPurchasesTowardOneBundle({
764
+ userId,
765
+ bundleId,
766
+ }: {
671
767
  userId: string | undefined
672
768
  bundleId: string
673
769
  }): Promise<Price[]> {
674
- throw new Error('Method not implemented.')
770
+ if (userId === undefined) return []
771
+
772
+ const canUpgradeProducts = await client.query.upgradableProducts.findMany(
773
+ {
774
+ where: eq(upgradableProducts.upgradableToId, bundleId),
775
+ },
776
+ )
777
+
778
+ const upgradableFrom = z.array(z.string()).parse(
779
+ canUpgradeProducts.map((product) => {
780
+ return product.upgradableFromId
781
+ }),
782
+ )
783
+
784
+ const purchases = await client.query.purchases.findMany({
785
+ where: and(
786
+ eq(purchaseTable.userId, userId),
787
+ inArray(purchaseTable.productId, upgradableFrom),
788
+ ),
789
+ })
790
+
791
+ const purchasesMade = z.array(z.string()).parse(
792
+ purchases.map((purchase) => {
793
+ return purchase.productId
794
+ }),
795
+ )
796
+
797
+ if (purchasesMade.length === 0) return []
798
+
799
+ const foundPrices = await client.query.prices.findMany({
800
+ where: and(
801
+ eq(prices.productId, bundleId),
802
+ inArray(prices.productId, purchasesMade),
803
+ ),
804
+ })
805
+
806
+ return z.array(priceSchema).parse(foundPrices)
675
807
  },
676
808
  toggleLessonProgressForUser(options: {
677
809
  userId: string
@@ -806,7 +938,7 @@ export function mySqlDrizzleAdapter(
806
938
  WHERE
807
939
  type = 'videoResource'
808
940
  AND (id = ${id});
809
-
941
+
810
942
  `
811
943
  return client
812
944
  .execute(query)
@@ -892,7 +1024,6 @@ export function mySqlDrizzleAdapter(
892
1024
  return parsedResource.data
893
1025
  },
894
1026
  async createUser(data) {
895
- console.log(data)
896
1027
  try {
897
1028
  const id = crypto.randomUUID()
898
1029
 
@@ -47,12 +47,12 @@ export function getUserRolesRelationsSchema(mysqlTable: MySqlTableFn) {
47
47
  user: one(users, {
48
48
  fields: [userRoles.userId],
49
49
  references: [users.id],
50
- relationName: 'role',
50
+ relationName: 'user',
51
51
  }),
52
52
  role: one(roles, {
53
53
  fields: [userRoles.roleId],
54
54
  references: [roles.id],
55
- relationName: 'user',
55
+ relationName: 'role',
56
56
  }),
57
57
  }))
58
58
  }
@@ -1,12 +1,12 @@
1
1
  import { sql } from 'drizzle-orm'
2
2
  import {
3
+ boolean,
3
4
  decimal,
4
5
  int,
5
6
  json,
6
7
  MySqlTableFn,
7
8
  primaryKey,
8
9
  timestamp,
9
- tinyint,
10
10
  unique,
11
11
  varchar,
12
12
  } from 'drizzle-orm/mysql-core'
@@ -23,7 +23,7 @@ export function getCouponSchema(mysqlTable: MySqlTableFn) {
23
23
  expires: timestamp('expires', { mode: 'date', fsp: 3 }),
24
24
  metadata: json('fields').$type<Record<string, any>>().default({}),
25
25
  maxUses: int('maxUses').default(-1).notNull(),
26
- default: tinyint('default').default(0).notNull(),
26
+ default: boolean('default').default(false).notNull(),
27
27
  merchantCouponId: varchar('merchantCouponId', { length: 191 }),
28
28
  status: int('status').default(0).notNull(),
29
29
  usedCount: int('usedCount').default(0).notNull(),
@@ -1,4 +1,4 @@
1
- import { sql } from 'drizzle-orm'
1
+ import { relations, sql } from 'drizzle-orm'
2
2
  import {
3
3
  decimal,
4
4
  int,
@@ -9,6 +9,9 @@ import {
9
9
  varchar,
10
10
  } from 'drizzle-orm/mysql-core'
11
11
 
12
+ import { getMerchantPriceSchema } from './merchant-price.js'
13
+ import { getProductSchema } from './product.js'
14
+
12
15
  export function getPriceSchema(mysqlTable: MySqlTableFn) {
13
16
  return mysqlTable(
14
17
  'prices',
@@ -30,3 +33,21 @@ export function getPriceSchema(mysqlTable: MySqlTableFn) {
30
33
  },
31
34
  )
32
35
  }
36
+
37
+ export function getProductRelationsSchema(mysqlTable: MySqlTableFn) {
38
+ const product = getProductSchema(mysqlTable)
39
+ const price = getPriceSchema(mysqlTable)
40
+ const merchantPrice = getMerchantPriceSchema(mysqlTable)
41
+ return relations(price, ({ one, many }) => ({
42
+ product: one(product, {
43
+ fields: [price.productId],
44
+ references: [product.id],
45
+ relationName: 'user',
46
+ }),
47
+ merchantPrice: one(merchantPrice, {
48
+ fields: [price.id],
49
+ references: [merchantPrice.priceId],
50
+ relationName: 'merchantPrice',
51
+ }),
52
+ }))
53
+ }
@@ -1,4 +1,4 @@
1
- import { sql } from 'drizzle-orm'
1
+ import { relations, sql } from 'drizzle-orm'
2
2
  import {
3
3
  int,
4
4
  json,
@@ -8,6 +8,10 @@ import {
8
8
  varchar,
9
9
  } from 'drizzle-orm/mysql-core'
10
10
 
11
+ import { getContentResourceProductSchema } from '../content/content-resource-product.js'
12
+ import { getMerchantProductSchema } from './merchant-product.js'
13
+ import { getPriceSchema } from './price.js'
14
+
11
15
  export function getProductSchema(mysqlTable: MySqlTableFn) {
12
16
  return mysqlTable(
13
17
  'products',
@@ -15,7 +19,7 @@ export function getProductSchema(mysqlTable: MySqlTableFn) {
15
19
  id: varchar('id', { length: 191 }).notNull(),
16
20
  name: varchar('name', { length: 191 }).notNull(),
17
21
  key: varchar('key', { length: 191 }),
18
- metadata: json('fields').$type<Record<string, any>>().default({}),
22
+ fields: json('fields').$type<Record<string, any>>().default({}),
19
23
  createdAt: timestamp('createdAt', { mode: 'date', fsp: 3 })
20
24
  .default(sql`CURRENT_TIMESTAMP(3)`)
21
25
  .notNull(),
@@ -29,3 +33,23 @@ export function getProductSchema(mysqlTable: MySqlTableFn) {
29
33
  },
30
34
  )
31
35
  }
36
+
37
+ export function getProductRelationsSchema(mysqlTable: MySqlTableFn) {
38
+ const product = getProductSchema(mysqlTable)
39
+ const price = getPriceSchema(mysqlTable)
40
+ const merchantProduct = getMerchantProductSchema(mysqlTable)
41
+ const contentResourceProduct = getContentResourceProductSchema(mysqlTable)
42
+ return relations(product, ({ one, many }) => ({
43
+ price: one(price, {
44
+ fields: [product.id],
45
+ references: [price.productId],
46
+ relationName: 'price',
47
+ }),
48
+ resources: many(contentResourceProduct, { relationName: 'product' }),
49
+ merchantProduct: one(merchantProduct, {
50
+ fields: [product.id],
51
+ references: [merchantProduct.productId],
52
+ relationName: 'merchantProduct',
53
+ }),
54
+ }))
55
+ }
@@ -0,0 +1,62 @@
1
+ import { relations, sql } from 'drizzle-orm'
2
+ import {
3
+ double,
4
+ index,
5
+ json,
6
+ MySqlTableFn,
7
+ primaryKey,
8
+ timestamp,
9
+ varchar,
10
+ } from 'drizzle-orm/mysql-core'
11
+
12
+ import { getProductSchema } from '../commerce/product.js'
13
+ import { getContentResourceSchema } from './content-resource.js'
14
+
15
+ export function getContentResourceProductSchema(mysqlTable: MySqlTableFn) {
16
+ return mysqlTable(
17
+ 'contentResourceProduct',
18
+ {
19
+ productId: varchar('productId', { length: 255 }).notNull(),
20
+ resourceId: varchar('resourceId', { length: 255 }).notNull(),
21
+ position: double('position').notNull().default(0),
22
+ metadata: json('metadata').$type<Record<string, any>>().default({}),
23
+ createdAt: timestamp('createdAt', {
24
+ mode: 'date',
25
+ fsp: 3,
26
+ }).default(sql`CURRENT_TIMESTAMP(3)`),
27
+ updatedAt: timestamp('updatedAt', {
28
+ mode: 'date',
29
+ fsp: 3,
30
+ }).default(sql`CURRENT_TIMESTAMP(3)`),
31
+ deletedAt: timestamp('deletedAt', {
32
+ mode: 'date',
33
+ fsp: 3,
34
+ }),
35
+ },
36
+ (crr) => ({
37
+ pk: primaryKey({ columns: [crr.productId, crr.resourceId] }),
38
+ contentResourceIdIdx: index('contentResourceId_idx').on(crr.productId),
39
+ resourceIdIdx: index('resourceId_idx').on(crr.resourceId),
40
+ }),
41
+ )
42
+ }
43
+
44
+ export function getContentResourceProductRelationsSchema(
45
+ mysqlTable: MySqlTableFn,
46
+ ) {
47
+ const contentResource = getContentResourceSchema(mysqlTable)
48
+ const contentResourceProduct = getContentResourceProductSchema(mysqlTable)
49
+ const product = getProductSchema(mysqlTable)
50
+ return relations(contentResourceProduct, ({ one }) => ({
51
+ product: one(product, {
52
+ fields: [contentResourceProduct.productId],
53
+ references: [product.id],
54
+ relationName: 'product',
55
+ }),
56
+ resource: one(contentResource, {
57
+ fields: [contentResourceProduct.resourceId],
58
+ references: [contentResource.id],
59
+ relationName: 'resource',
60
+ }),
61
+ }))
62
+ }
@@ -9,6 +9,7 @@ import {
9
9
 
10
10
  import { getUsersSchema } from '../auth/users.js'
11
11
  import { getContentContributionsSchema } from './content-contributions.js'
12
+ import { getContentResourceProductSchema } from './content-resource-product.js'
12
13
  import { getContentResourceResourceSchema } from './content-resource-resource.js'
13
14
 
14
15
  export function getContentResourceSchema(mysqlTable: MySqlTableFn) {
@@ -44,6 +45,7 @@ export function getContentResourceRelationsSchema(mysqlTable: MySqlTableFn) {
44
45
  const contentResource = getContentResourceSchema(mysqlTable)
45
46
  const users = getUsersSchema(mysqlTable)
46
47
  const contentResourceResource = getContentResourceResourceSchema(mysqlTable)
48
+ const contentResourceProduct = getContentResourceProductSchema(mysqlTable)
47
49
  return relations(contentResource, ({ one, many }) => ({
48
50
  createdBy: one(users, {
49
51
  fields: [contentResource.createdById],
@@ -52,5 +54,8 @@ export function getContentResourceRelationsSchema(mysqlTable: MySqlTableFn) {
52
54
  }),
53
55
  resources: many(contentResourceResource, { relationName: 'resourceOf' }),
54
56
  resourceOf: many(contentResourceResource, { relationName: 'resource' }),
57
+ resourceProducts: many(contentResourceProduct, {
58
+ relationName: 'resource',
59
+ }),
55
60
  }))
56
61
  }
package/src/lib/utils.ts CHANGED
@@ -6,8 +6,6 @@ import { type BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core'
6
6
  import type { AnySQLiteTable, SQLiteTableFn } from 'drizzle-orm/sqlite-core'
7
7
 
8
8
  import type { DefaultSchema as MySqlSchema } from './mysql/index.js'
9
- import type { DefaultSchema as PgSchema } from './pg.js'
10
- import type { DefaultSchema as SQLiteSchema } from './sqlite.js'
11
9
 
12
10
  export type AnyMySqlDatabase = MySqlDatabase<any, any>
13
11
  export type AnyPgDatabase = PgDatabase<any, any, any>
@@ -15,8 +13,6 @@ export type AnySQLiteDatabase = BaseSQLiteDatabase<any, any, any, any>
15
13
 
16
14
  export interface MinimumSchema {
17
15
  mysql: MySqlSchema & Record<string, AnyMySqlTable>
18
- pg: PgSchema & Record<string, AnyPgTable>
19
- sqlite: SQLiteSchema & Record<string, AnySQLiteTable>
20
16
  }
21
17
 
22
18
  export type SqlFlavorOptions =
@@ -26,11 +22,7 @@ export type SqlFlavorOptions =
26
22
 
27
23
  export type ClientFlavors<Flavor> = Flavor extends AnyMySqlDatabase
28
24
  ? MinimumSchema['mysql']
29
- : Flavor extends AnyPgDatabase
30
- ? MinimumSchema['pg']
31
- : Flavor extends AnySQLiteDatabase
32
- ? MinimumSchema['sqlite']
33
- : never
25
+ : never
34
26
 
35
27
  export type TableFn<Flavor> = Flavor extends AnyMySqlDatabase
36
28
  ? MySqlTableFn