@magnet-cms/adapter-db-drizzle 1.0.2

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.
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # @magnet-cms/adapter-db-drizzle
2
+
3
+ Drizzle ORM database adapter for Magnet CMS. Supports PostgreSQL, MySQL, and SQLite through [Drizzle ORM](https://orm.drizzle.team/).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # Install the adapter
9
+ bun add @magnet-cms/adapter-db-drizzle drizzle-orm
10
+
11
+ # For PostgreSQL
12
+ bun add pg
13
+ # or for Neon serverless
14
+ bun add @neondatabase/serverless
15
+
16
+ # For MySQL
17
+ bun add mysql2
18
+
19
+ # For SQLite
20
+ bun add better-sqlite3
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### Basic Setup
26
+
27
+ ```typescript
28
+ import { MagnetModule } from '@magnet-cms/core'
29
+ import { Module } from '@nestjs/common'
30
+
31
+ @Module({
32
+ imports: [
33
+ MagnetModule.forRoot({
34
+ db: {
35
+ connectionString: process.env.DATABASE_URL,
36
+ dialect: 'postgresql',
37
+ },
38
+ jwt: {
39
+ secret: process.env.JWT_SECRET,
40
+ },
41
+ }),
42
+ ],
43
+ })
44
+ export class AppModule {}
45
+ ```
46
+
47
+ ### With Neon (Serverless PostgreSQL)
48
+
49
+ ```typescript
50
+ MagnetModule.forRoot({
51
+ db: {
52
+ connectionString: process.env.NEON_DATABASE_URL,
53
+ dialect: 'postgresql',
54
+ driver: 'neon', // Use Neon serverless driver
55
+ },
56
+ jwt: {
57
+ secret: process.env.JWT_SECRET,
58
+ },
59
+ })
60
+ ```
61
+
62
+ ### Defining Schemas
63
+
64
+ Use the same `@Schema()` and `@Prop()` decorators from `@magnet-cms/common`:
65
+
66
+ ```typescript
67
+ import { Schema, Prop } from '@magnet-cms/common'
68
+
69
+ @Schema()
70
+ export class Article {
71
+ @Prop({ required: true, intl: true })
72
+ title!: string
73
+
74
+ @Prop({ intl: true })
75
+ content?: string
76
+
77
+ @Prop({ required: true, unique: true })
78
+ slug!: string
79
+
80
+ @Prop({ type: Boolean, default: false })
81
+ featured?: boolean
82
+ }
83
+ ```
84
+
85
+ The adapter automatically generates Drizzle table schemas from these decorators.
86
+
87
+ ## Configuration Options
88
+
89
+ | Option | Type | Description |
90
+ |--------|------|-------------|
91
+ | `connectionString` | `string` | Database connection URL |
92
+ | `dialect` | `'postgresql' \| 'mysql' \| 'sqlite'` | SQL dialect to use |
93
+ | `driver` | `'pg' \| 'neon' \| 'mysql2' \| 'better-sqlite3'` | Database driver (auto-detected) |
94
+ | `debug` | `boolean` | Enable query logging |
95
+
96
+ ## i18n and Versioning
97
+
98
+ Like the Mongoose adapter, this adapter supports document-based i18n and versioning. Each document can have multiple locale variants stored in the same table:
99
+
100
+ ```sql
101
+ -- Auto-generated table structure
102
+ CREATE TABLE articles (
103
+ id UUID PRIMARY KEY,
104
+ document_id UUID NOT NULL, -- Groups locale variants
105
+ locale VARCHAR(10) DEFAULT 'en',
106
+ status VARCHAR(20) DEFAULT 'draft',
107
+ published_at TIMESTAMP,
108
+ title TEXT NOT NULL,
109
+ content TEXT,
110
+ slug VARCHAR(255),
111
+ featured BOOLEAN DEFAULT false,
112
+ created_at TIMESTAMP DEFAULT NOW(),
113
+ updated_at TIMESTAMP DEFAULT NOW(),
114
+ UNIQUE(document_id, locale, status)
115
+ );
116
+ ```
117
+
118
+ ## Query Builder
119
+
120
+ The adapter provides a fluent query builder with MongoDB-style operators:
121
+
122
+ ```typescript
123
+ const articles = await articleModel.query()
124
+ .where({ status: 'active', views: { $gte: 100 } })
125
+ .sort({ createdAt: -1 })
126
+ .limit(10)
127
+ .exec()
128
+ ```
129
+
130
+ ### Supported Operators
131
+
132
+ | Operator | SQL Equivalent |
133
+ |----------|---------------|
134
+ | `$eq` | `=` |
135
+ | `$ne` | `<>` |
136
+ | `$gt` | `>` |
137
+ | `$gte` | `>=` |
138
+ | `$lt` | `<` |
139
+ | `$lte` | `<=` |
140
+ | `$in` | `IN (...)` |
141
+ | `$nin` | `NOT IN (...)` |
142
+ | `$regex` | `ILIKE '%...%'` |
143
+ | `$like` | `LIKE` |
144
+ | `$ilike` | `ILIKE` |
145
+
146
+ ## Native Access
147
+
148
+ For advanced queries, you can access the native Drizzle instance:
149
+
150
+ ```typescript
151
+ const { db, table } = articleModel.native()
152
+
153
+ // Use Drizzle directly
154
+ const results = await db
155
+ .select()
156
+ .from(table)
157
+ .where(sql`${table.views} > 1000`)
158
+ ```
159
+
160
+ ## License
161
+
162
+ MIT
@@ -0,0 +1,38 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
9
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
10
+ }) : x)(function(x) {
11
+ if (typeof require !== "undefined") return require.apply(this, arguments);
12
+ throw Error('Dynamic require of "' + x + '" is not supported');
13
+ });
14
+ var __commonJS = (cb, mod) => function __require2() {
15
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
16
+ };
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+
38
+ export { __commonJS, __export, __name, __require, __toESM };