@aphexcms/sqlite-adapter 0.1.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.
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/asset-adapter.d.ts +66 -0
- package/dist/asset-adapter.d.ts.map +1 -0
- package/dist/asset-adapter.js +303 -0
- package/dist/asset-adapter.js.map +1 -0
- package/dist/document-adapter.d.ts +94 -0
- package/dist/document-adapter.d.ts.map +1 -0
- package/dist/document-adapter.js +486 -0
- package/dist/document-adapter.js.map +1 -0
- package/dist/filter-parser.d.ts +36 -0
- package/dist/filter-parser.d.ts.map +1 -0
- package/dist/filter-parser.js +308 -0
- package/dist/filter-parser.js.map +1 -0
- package/dist/index.d.ts +273 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +611 -0
- package/dist/index.js.map +1 -0
- package/dist/organization-adapter.d.ts +38 -0
- package/dist/organization-adapter.d.ts.map +1 -0
- package/dist/organization-adapter.js +241 -0
- package/dist/organization-adapter.js.map +1 -0
- package/dist/reference-adapter.d.ts +56 -0
- package/dist/reference-adapter.d.ts.map +1 -0
- package/dist/reference-adapter.js +109 -0
- package/dist/reference-adapter.js.map +1 -0
- package/dist/roles-adapter.d.ts +19 -0
- package/dist/roles-adapter.d.ts.map +1 -0
- package/dist/roles-adapter.js +87 -0
- package/dist/roles-adapter.js.map +1 -0
- package/dist/schema-adapter.d.ts +23 -0
- package/dist/schema-adapter.d.ts.map +1 -0
- package/dist/schema-adapter.js +99 -0
- package/dist/schema-adapter.js.map +1 -0
- package/dist/schema.d.ts +3803 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +240 -0
- package/dist/schema.js.map +1 -0
- package/dist/user-adapter.d.ts +31 -0
- package/dist/user-adapter.d.ts.map +1 -0
- package/dist/user-adapter.js +69 -0
- package/dist/user-adapter.js.map +1 -0
- package/dist/utils/reference-resolver.d.ts +15 -0
- package/dist/utils/reference-resolver.d.ts.map +1 -0
- package/dist/utils/reference-resolver.js +80 -0
- package/dist/utils/reference-resolver.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Benjamin Sinidol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @aphexcms/sqlite-adapter
|
|
2
|
+
|
|
3
|
+
SQLite database adapter for [AphexCMS](https://github.com/IcelandicIcecream/aphex), built on [libsql](https://github.com/tursodatabase/libsql).
|
|
4
|
+
|
|
5
|
+
Implements the full `DatabaseAdapter` contract from `@aphexcms/cms-core`, so it is a drop-in alternative to `@aphexcms/postgresql-adapter`. Works against a local `file:` database (no Docker, no server) or a Turso-hosted `libsql://` URL.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @aphexcms/sqlite-adapter @libsql/client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
Let the adapter create and own the client:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createSQLiteProvider } from '@aphexcms/sqlite-adapter';
|
|
19
|
+
|
|
20
|
+
// Local file
|
|
21
|
+
const db = createSQLiteProvider({ url: 'file:.aphex/site.db' }).createAdapter();
|
|
22
|
+
|
|
23
|
+
// Turso
|
|
24
|
+
const db = createSQLiteProvider({
|
|
25
|
+
url: 'libsql://mydb-me.turso.io',
|
|
26
|
+
authToken: process.env.DATABASE_AUTH_TOKEN
|
|
27
|
+
}).createAdapter();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then hand it to `createCMSConfig()`:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createCMSConfig } from '@aphexcms/cms-core/server';
|
|
34
|
+
|
|
35
|
+
export default createCMSConfig({
|
|
36
|
+
schemaTypes,
|
|
37
|
+
database: db
|
|
38
|
+
// …
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Bringing your own client
|
|
43
|
+
|
|
44
|
+
Apps usually share one libsql client between the adapter, Drizzle, and the auth provider. Pass it in — **the adapter never modifies a client it didn't create**, so apply pragmas yourself:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { createClient } from '@libsql/client';
|
|
48
|
+
import { createSQLiteProvider, applyRecommendedPragmas } from '@aphexcms/sqlite-adapter';
|
|
49
|
+
|
|
50
|
+
const client = createClient({ url: 'file:.aphex/site.db' });
|
|
51
|
+
await applyRecommendedPragmas(client, 'file:.aphex/site.db');
|
|
52
|
+
|
|
53
|
+
const db = createSQLiteProvider({ client }).createAdapter();
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Pragmas
|
|
57
|
+
|
|
58
|
+
When the adapter creates the client from `url`, it applies a recommended set to local `file:` databases:
|
|
59
|
+
|
|
60
|
+
| Pragma | Value | Why |
|
|
61
|
+
| -------------- | -------- | --------------------------------------------------------- |
|
|
62
|
+
| `journal_mode` | `WAL` | Reads proceed while a write is in flight |
|
|
63
|
+
| `synchronous` | `NORMAL` | The safe pairing for WAL |
|
|
64
|
+
| `busy_timeout` | `5000` | Waits instead of throwing `SQLITE_BUSY` under concurrency |
|
|
65
|
+
|
|
66
|
+
In-memory and Turso URLs are skipped — Turso manages its own journaling.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
// Default: recommended set
|
|
70
|
+
createSQLiteProvider({ url });
|
|
71
|
+
|
|
72
|
+
// Opt out entirely
|
|
73
|
+
createSQLiteProvider({ url, pragmas: false });
|
|
74
|
+
|
|
75
|
+
// Recommended set plus tuning (see SQLitePragmaOptions)
|
|
76
|
+
createSQLiteProvider({
|
|
77
|
+
url,
|
|
78
|
+
pragmas: { cacheSize: -65536 } // 64 MiB page cache (negative = KiB)
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Raw statements, run verbatim
|
|
82
|
+
createSQLiteProvider({ url, pragmas: 'PRAGMA busy_timeout=10000;' });
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Pragmas apply to the client's main connection. libsql opens a fresh connection per interactive transaction, which gets SQLite defaults.
|
|
86
|
+
|
|
87
|
+
## Multi-tenancy
|
|
88
|
+
|
|
89
|
+
SQLite has **no Row-Level Security**. Organization isolation comes from the explicit `organizationId` WHERE clause every query applies — the same mechanism that actually isolates tenants on pooled Postgres, where the connection is the table owner and bypasses RLS anyway.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
createSQLiteProvider({ url, multiTenancy: { enableHierarchy: true } });
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`enableHierarchy` works as on Postgres. There is no `enableRLS` option.
|
|
96
|
+
|
|
97
|
+
## Migrations
|
|
98
|
+
|
|
99
|
+
Point `drizzle.config.ts` at `dialect: 'sqlite'` and re-export the CMS tables from `@aphexcms/sqlite-adapter/schema` instead of the Postgres adapter.
|
|
100
|
+
|
|
101
|
+
The `aphex migrate` CLI detects SQLite from `APHEX_DATABASE=sqlite`, or from a `DATABASE_URL` starting with `file:` / `libsql:`. Remote databases use `DATABASE_AUTH_TOKEN`.
|
|
102
|
+
|
|
103
|
+
## Behavioral parity
|
|
104
|
+
|
|
105
|
+
Both adapters run the same cross-dialect conformance suite (`tests/conformance.spec.ts`) against an in-memory libsql and an in-memory PGlite, so filters, sorting, versioning, references, and org isolation behave identically.
|
|
106
|
+
|
|
107
|
+
Two documented differences:
|
|
108
|
+
|
|
109
|
+
- **`contains` uses SQLite's `LIKE`**, which is case-insensitive for ASCII only. Postgres `ILIKE` handles full Unicode.
|
|
110
|
+
- **Document JSON filters and sorts compile to `json_extract`** (Postgres uses `->>`). Both are per-row extraction expressions.
|
|
111
|
+
|
|
112
|
+
### Query performance
|
|
113
|
+
|
|
114
|
+
Content stored in `draftData` / `publishedData` is JSON. Filtering or sorting by a field _inside_ that JSON — anything that isn't a real column like `type` or `status` — compiles to `json_extract(...)` with no index behind it, so it scans within the `(organization_id, type)` partition. This is true of the PostgreSQL adapter too. If you filter a field often, promote it to a real column or add an expression index.
|
|
115
|
+
|
|
116
|
+
## Exports
|
|
117
|
+
|
|
118
|
+
| Path | Contents |
|
|
119
|
+
| --------------------------------- | -------------------------------------------------------------------------------------------------- |
|
|
120
|
+
| `@aphexcms/sqlite-adapter` | `createSQLiteProvider`, `applyRecommendedPragmas`, `SQLiteAdapter`, types |
|
|
121
|
+
| `@aphexcms/sqlite-adapter/schema` | Drizzle table definitions (`documents`, `assets`, `organizations`, …) and the combined `cmsSchema` |
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { drizzle } from 'drizzle-orm/libsql';
|
|
2
|
+
import type { AssetAdapter, AssetFilters, CreateAssetData, UpdateAssetData, Asset, FindOptions, FindResult, Where } from '@aphexcms/cms-core/server';
|
|
3
|
+
import type { CMSSchema } from './schema.js';
|
|
4
|
+
/**
|
|
5
|
+
* SQLite asset adapter implementation
|
|
6
|
+
* Handles all asset-related database operations
|
|
7
|
+
*/
|
|
8
|
+
export declare class SQLiteAssetAdapter implements AssetAdapter {
|
|
9
|
+
private db;
|
|
10
|
+
private tables;
|
|
11
|
+
constructor(db: ReturnType<typeof drizzle>, tables: CMSSchema);
|
|
12
|
+
/**
|
|
13
|
+
* Create new asset
|
|
14
|
+
*/
|
|
15
|
+
createAsset(data: CreateAssetData): Promise<Asset>;
|
|
16
|
+
/**
|
|
17
|
+
* Find asset by ID
|
|
18
|
+
*/
|
|
19
|
+
findAssetById(organizationId: string, id: string): Promise<Asset | null>;
|
|
20
|
+
/**
|
|
21
|
+
* Find asset by ID across multiple organizations (single query)
|
|
22
|
+
*/
|
|
23
|
+
findAssetByIdInOrgs(organizationIds: string[], id: string): Promise<Asset | null>;
|
|
24
|
+
/**
|
|
25
|
+
* Find multiple assets with filtering
|
|
26
|
+
*/
|
|
27
|
+
findAssets(organizationId: string, filters?: AssetFilters): Promise<Asset[]>;
|
|
28
|
+
/**
|
|
29
|
+
* Find asset by ID without an organization filter (public asset serving).
|
|
30
|
+
* Plain query — SQLite has no RLS to bypass, the global scope is the point.
|
|
31
|
+
*/
|
|
32
|
+
findAssetByIdGlobal(id: string): Promise<Asset | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Update asset metadata
|
|
35
|
+
*/
|
|
36
|
+
updateAsset(organizationId: string, id: string, data: UpdateAssetData): Promise<Asset | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Delete asset by ID
|
|
39
|
+
*/
|
|
40
|
+
deleteAsset(organizationId: string, id: string): Promise<boolean>;
|
|
41
|
+
/**
|
|
42
|
+
* Count total assets
|
|
43
|
+
*/
|
|
44
|
+
countAssets(organizationId: string, filters?: AssetFilters): Promise<number>;
|
|
45
|
+
/**
|
|
46
|
+
* Count assets by type
|
|
47
|
+
*/
|
|
48
|
+
countAssetsByType(organizationId: string): Promise<Record<string, number>>;
|
|
49
|
+
/**
|
|
50
|
+
* Get total size of all assets
|
|
51
|
+
*/
|
|
52
|
+
getTotalAssetsSize(organizationId: string): Promise<number>;
|
|
53
|
+
/**
|
|
54
|
+
* Advanced filtering - find many assets with where clause and pagination
|
|
55
|
+
*/
|
|
56
|
+
findManyAssetsAdvanced(organizationId: string, options?: FindOptions): Promise<FindResult<Asset>>;
|
|
57
|
+
/**
|
|
58
|
+
* Advanced filtering - find asset by ID
|
|
59
|
+
*/
|
|
60
|
+
findAssetByIdAdvanced(organizationId: string, id: string): Promise<Asset | null>;
|
|
61
|
+
/**
|
|
62
|
+
* Count assets matching where clause
|
|
63
|
+
*/
|
|
64
|
+
countAssetsAdvanced(organizationId: string, where?: Where): Promise<number>;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=asset-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asset-adapter.d.ts","sourceRoot":"","sources":["../src/asset-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,KAAK,EACX,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,eAAe,EACf,KAAK,EACL,WAAW,EACX,UAAU,EACV,KAAK,EACL,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAO1C;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACtD,OAAO,CAAC,EAAE,CAA6B;IACvC,OAAO,CAAC,MAAM,CAAY;gBAEd,EAAE,EAAE,UAAU,CAAC,OAAO,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS;IAK7D;;O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,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;IAYtF;;OAEG;IACG,mBAAmB,CAAC,cAAc,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;CAmBjF"}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { eq, desc, and, like, sql, inArray } from 'drizzle-orm';
|
|
2
|
+
import { parseWhere, parseSort } from './filter-parser.js';
|
|
3
|
+
// Default values
|
|
4
|
+
const DEFAULT_LIMIT = 20;
|
|
5
|
+
const DEFAULT_OFFSET = 0;
|
|
6
|
+
/**
|
|
7
|
+
* SQLite asset adapter implementation
|
|
8
|
+
* Handles all asset-related database operations
|
|
9
|
+
*/
|
|
10
|
+
export class SQLiteAssetAdapter {
|
|
11
|
+
db;
|
|
12
|
+
tables;
|
|
13
|
+
constructor(db, tables) {
|
|
14
|
+
this.db = db;
|
|
15
|
+
this.tables = tables;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create new asset
|
|
19
|
+
*/
|
|
20
|
+
async createAsset(data) {
|
|
21
|
+
const result = await this.db
|
|
22
|
+
.insert(this.tables.assets)
|
|
23
|
+
.values({
|
|
24
|
+
organizationId: data.organizationId,
|
|
25
|
+
assetType: data.assetType,
|
|
26
|
+
filename: data.filename,
|
|
27
|
+
originalFilename: data.originalFilename,
|
|
28
|
+
mimeType: data.mimeType,
|
|
29
|
+
size: data.size,
|
|
30
|
+
url: data.url,
|
|
31
|
+
path: data.path,
|
|
32
|
+
storageAdapter: data.storageAdapter,
|
|
33
|
+
width: data.width,
|
|
34
|
+
height: data.height,
|
|
35
|
+
metadata: data.metadata,
|
|
36
|
+
title: data.title,
|
|
37
|
+
description: data.description,
|
|
38
|
+
alt: data.alt,
|
|
39
|
+
creditLine: data.creditLine,
|
|
40
|
+
createdBy: data.createdBy
|
|
41
|
+
})
|
|
42
|
+
.returning();
|
|
43
|
+
return result[0];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Find asset by ID
|
|
47
|
+
*/
|
|
48
|
+
async findAssetById(organizationId, id) {
|
|
49
|
+
try {
|
|
50
|
+
const result = await this.db
|
|
51
|
+
.select()
|
|
52
|
+
.from(this.tables.assets)
|
|
53
|
+
.where(and(eq(this.tables.assets.id, id), eq(this.tables.assets.organizationId, organizationId)))
|
|
54
|
+
.limit(1);
|
|
55
|
+
return result[0] || null;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Find asset by ID across multiple organizations (single query)
|
|
63
|
+
*/
|
|
64
|
+
async findAssetByIdInOrgs(organizationIds, id) {
|
|
65
|
+
try {
|
|
66
|
+
if (organizationIds.length === 0) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const result = await this.db
|
|
70
|
+
.select()
|
|
71
|
+
.from(this.tables.assets)
|
|
72
|
+
.where(and(eq(this.tables.assets.id, id), inArray(this.tables.assets.organizationId, organizationIds)))
|
|
73
|
+
.limit(1);
|
|
74
|
+
return result[0] || null;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Find multiple assets with filtering
|
|
82
|
+
*/
|
|
83
|
+
async findAssets(organizationId, filters = {}) {
|
|
84
|
+
try {
|
|
85
|
+
const { assetType, mimeType, search, limit = DEFAULT_LIMIT, offset = DEFAULT_OFFSET } = filters;
|
|
86
|
+
const conditions = [eq(this.tables.assets.organizationId, organizationId)];
|
|
87
|
+
if (assetType) {
|
|
88
|
+
conditions.push(eq(this.tables.assets.assetType, assetType));
|
|
89
|
+
}
|
|
90
|
+
if (mimeType) {
|
|
91
|
+
conditions.push(eq(this.tables.assets.mimeType, mimeType));
|
|
92
|
+
}
|
|
93
|
+
if (search) {
|
|
94
|
+
conditions.push(like(this.tables.assets.originalFilename, `%${search}%`));
|
|
95
|
+
}
|
|
96
|
+
// Build and execute query
|
|
97
|
+
const result = await this.db
|
|
98
|
+
.select()
|
|
99
|
+
.from(this.tables.assets)
|
|
100
|
+
.where(and(...conditions))
|
|
101
|
+
.orderBy(desc(this.tables.assets.createdAt))
|
|
102
|
+
.limit(limit)
|
|
103
|
+
.offset(offset);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Find asset by ID without an organization filter (public asset serving).
|
|
112
|
+
* Plain query — SQLite has no RLS to bypass, the global scope is the point.
|
|
113
|
+
*/
|
|
114
|
+
async findAssetByIdGlobal(id) {
|
|
115
|
+
try {
|
|
116
|
+
const result = await this.db
|
|
117
|
+
.select()
|
|
118
|
+
.from(this.tables.assets)
|
|
119
|
+
.where(eq(this.tables.assets.id, id))
|
|
120
|
+
.limit(1);
|
|
121
|
+
return result[0] || null;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Update asset metadata
|
|
129
|
+
*/
|
|
130
|
+
async updateAsset(organizationId, id, data) {
|
|
131
|
+
try {
|
|
132
|
+
const result = await this.db
|
|
133
|
+
.update(this.tables.assets)
|
|
134
|
+
.set({
|
|
135
|
+
...data,
|
|
136
|
+
updatedAt: new Date()
|
|
137
|
+
})
|
|
138
|
+
.where(and(eq(this.tables.assets.id, id), eq(this.tables.assets.organizationId, organizationId)))
|
|
139
|
+
.returning();
|
|
140
|
+
return result[0] || null;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Delete asset by ID
|
|
148
|
+
*/
|
|
149
|
+
async deleteAsset(organizationId, id) {
|
|
150
|
+
try {
|
|
151
|
+
const result = await this.db
|
|
152
|
+
.delete(this.tables.assets)
|
|
153
|
+
.where(and(eq(this.tables.assets.id, id), eq(this.tables.assets.organizationId, organizationId)))
|
|
154
|
+
.returning({ id: this.tables.assets.id });
|
|
155
|
+
return result.length > 0;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Count total assets
|
|
163
|
+
*/
|
|
164
|
+
async countAssets(organizationId, filters) {
|
|
165
|
+
try {
|
|
166
|
+
const conditions = [eq(this.tables.assets.organizationId, organizationId)];
|
|
167
|
+
if (filters?.assetType) {
|
|
168
|
+
conditions.push(eq(this.tables.assets.assetType, filters.assetType));
|
|
169
|
+
}
|
|
170
|
+
if (filters?.mimeType) {
|
|
171
|
+
conditions.push(eq(this.tables.assets.mimeType, filters.mimeType));
|
|
172
|
+
}
|
|
173
|
+
if (filters?.search) {
|
|
174
|
+
conditions.push(like(this.tables.assets.originalFilename, `%${filters.search}%`));
|
|
175
|
+
}
|
|
176
|
+
const result = await this.db
|
|
177
|
+
.select({ count: sql `count(*)` })
|
|
178
|
+
.from(this.tables.assets)
|
|
179
|
+
.where(and(...conditions));
|
|
180
|
+
return result[0]?.count || 0;
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Count assets by type
|
|
188
|
+
*/
|
|
189
|
+
async countAssetsByType(organizationId) {
|
|
190
|
+
try {
|
|
191
|
+
const result = await this.db
|
|
192
|
+
.select({
|
|
193
|
+
assetType: this.tables.assets.assetType,
|
|
194
|
+
count: sql `count(*)`
|
|
195
|
+
})
|
|
196
|
+
.from(this.tables.assets)
|
|
197
|
+
.where(eq(this.tables.assets.organizationId, organizationId))
|
|
198
|
+
.groupBy(this.tables.assets.assetType);
|
|
199
|
+
const counts = {};
|
|
200
|
+
result.forEach((row) => {
|
|
201
|
+
counts[row.assetType] = row.count;
|
|
202
|
+
});
|
|
203
|
+
return counts;
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return {};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Get total size of all assets
|
|
211
|
+
*/
|
|
212
|
+
async getTotalAssetsSize(organizationId) {
|
|
213
|
+
try {
|
|
214
|
+
const result = await this.db
|
|
215
|
+
.select({ totalSize: sql `sum(size)` })
|
|
216
|
+
.from(this.tables.assets)
|
|
217
|
+
.where(eq(this.tables.assets.organizationId, organizationId));
|
|
218
|
+
return result[0]?.totalSize || 0;
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Advanced filtering - find many assets with where clause and pagination
|
|
226
|
+
*/
|
|
227
|
+
async findManyAssetsAdvanced(organizationId, options = {}) {
|
|
228
|
+
const { where, limit = DEFAULT_LIMIT, offset = DEFAULT_OFFSET, sort } = options;
|
|
229
|
+
// Build base conditions
|
|
230
|
+
const baseConditions = [eq(this.tables.assets.organizationId, organizationId)];
|
|
231
|
+
// Parse where clause (assets don't have JSON data like documents)
|
|
232
|
+
const whereCondition = parseWhere(where, this.tables.assets, 'draft');
|
|
233
|
+
// Combine conditions
|
|
234
|
+
const allConditions = whereCondition
|
|
235
|
+
? and(...baseConditions, whereCondition)
|
|
236
|
+
: and(...baseConditions);
|
|
237
|
+
// Get total count
|
|
238
|
+
const countResult = await this.db
|
|
239
|
+
.select({ count: sql `count(*)` })
|
|
240
|
+
.from(this.tables.assets)
|
|
241
|
+
.where(allConditions);
|
|
242
|
+
const totalDocs = countResult[0]?.count || 0;
|
|
243
|
+
// Build query
|
|
244
|
+
let query = this.db.select().from(this.tables.assets);
|
|
245
|
+
if (allConditions) {
|
|
246
|
+
query = query.where(allConditions);
|
|
247
|
+
}
|
|
248
|
+
// Add sorting
|
|
249
|
+
const orderBy = parseSort(sort, this.tables.assets, 'draft');
|
|
250
|
+
if (orderBy.length > 0) {
|
|
251
|
+
query = query.orderBy(...orderBy);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
// Default sort by createdAt desc
|
|
255
|
+
query = query.orderBy(desc(this.tables.assets.createdAt));
|
|
256
|
+
}
|
|
257
|
+
// Apply pagination
|
|
258
|
+
const docs = await query.limit(limit).offset(offset);
|
|
259
|
+
// Calculate pagination metadata
|
|
260
|
+
const totalPages = Math.ceil(totalDocs / limit);
|
|
261
|
+
const currentPage = Math.floor(offset / limit) + 1;
|
|
262
|
+
return {
|
|
263
|
+
docs: docs,
|
|
264
|
+
totalDocs,
|
|
265
|
+
limit,
|
|
266
|
+
offset,
|
|
267
|
+
page: currentPage,
|
|
268
|
+
totalPages,
|
|
269
|
+
hasNextPage: currentPage < totalPages,
|
|
270
|
+
hasPrevPage: currentPage > 1
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Advanced filtering - find asset by ID
|
|
275
|
+
*/
|
|
276
|
+
async findAssetByIdAdvanced(organizationId, id) {
|
|
277
|
+
const result = await this.db
|
|
278
|
+
.select()
|
|
279
|
+
.from(this.tables.assets)
|
|
280
|
+
.where(and(eq(this.tables.assets.id, id), eq(this.tables.assets.organizationId, organizationId)))
|
|
281
|
+
.limit(1);
|
|
282
|
+
return result[0] || null;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Count assets matching where clause
|
|
286
|
+
*/
|
|
287
|
+
async countAssetsAdvanced(organizationId, where) {
|
|
288
|
+
// Build base conditions
|
|
289
|
+
const baseConditions = [eq(this.tables.assets.organizationId, organizationId)];
|
|
290
|
+
// Parse where clause
|
|
291
|
+
const whereCondition = parseWhere(where, this.tables.assets, 'draft');
|
|
292
|
+
// Combine conditions
|
|
293
|
+
const allConditions = whereCondition
|
|
294
|
+
? and(...baseConditions, whereCondition)
|
|
295
|
+
: and(...baseConditions);
|
|
296
|
+
const result = await this.db
|
|
297
|
+
.select({ count: sql `count(*)` })
|
|
298
|
+
.from(this.tables.assets)
|
|
299
|
+
.where(allConditions);
|
|
300
|
+
return result[0]?.count || 0;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
//# sourceMappingURL=asset-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asset-adapter.js","sourceRoot":"","sources":["../src/asset-adapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAYhE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAExD,iBAAiB;AACjB,MAAM,aAAa,GAAG,EAAE,CAAC;AACzB,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IACtB,EAAE,CAA6B;IAC/B,MAAM,CAAY;IAE1B,YAAY,EAA8B,EAAE,MAAiB;QAC5D,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,WAAW,CAAC,IAAqB;QACtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE;aAC1B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;aAC1B,MAAM,CAAC;YACP,cAAc,EAAE,IAAI,CAAC,cAAc;YACnC,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,gBAAgB,EAAE,IAAI,CAAC,gBAAgB;YACvC,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,IAAI,EAAE,IAAI,CAAC,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G,cAAc,EAAE,cAAc,CAAC;YACxC,CAAC,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,CAAC;QAE1B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE;aAC1B,MAAM,CAAC,EAAE,KAAK,EAAE,GAAG,CAAQ,UAAU,EAAE,CAAC;aACxC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;aACxB,KAAK,CAAC,aAAc,CAAC,CAAC;QAExB,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC;IAC9B,CAAC;CACD"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { drizzle } from 'drizzle-orm/libsql';
|
|
2
|
+
import type { DocumentAdapter, CreateDocumentData, Document, DocumentVersion, DocumentVersionList, FindOptions, FindResult, Where } from '@aphexcms/cms-core/server';
|
|
3
|
+
import type { CMSSchema } from './schema.js';
|
|
4
|
+
export declare const DOCUMENT_STATUS: {
|
|
5
|
+
DRAFT: "draft";
|
|
6
|
+
PUBLISHED: "published";
|
|
7
|
+
UNPUBLISHED: "unpublished";
|
|
8
|
+
};
|
|
9
|
+
export type DocumentStatus = 'draft' | 'published' | 'unpublished';
|
|
10
|
+
/**
|
|
11
|
+
* SQLite document adapter implementation
|
|
12
|
+
* Handles all document-related database operations
|
|
13
|
+
*/
|
|
14
|
+
export declare class SQLiteDocumentAdapter implements DocumentAdapter {
|
|
15
|
+
private db;
|
|
16
|
+
private tables;
|
|
17
|
+
constructor(db: ReturnType<typeof drizzle>, tables: CMSSchema);
|
|
18
|
+
/**
|
|
19
|
+
* Create new document (always starts as draft)
|
|
20
|
+
*/
|
|
21
|
+
createDocument(data: CreateDocumentData): Promise<Document>;
|
|
22
|
+
/**
|
|
23
|
+
* Update draft data (auto-save)
|
|
24
|
+
*/
|
|
25
|
+
updateDocDraft(organizationId: string, id: string, data: any, updatedBy?: string): Promise<Document | null>;
|
|
26
|
+
/**
|
|
27
|
+
* Publish document (copy draft -> published)
|
|
28
|
+
*/
|
|
29
|
+
publishDoc(organizationId: string, id: string): Promise<Document | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Unpublish document (soft — keeps publishedData intact, just marks as unpublished)
|
|
32
|
+
*/
|
|
33
|
+
unpublishDoc(organizationId: string, id: string): Promise<Document | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Delete document permanently
|
|
36
|
+
*/
|
|
37
|
+
deleteDocById(organizationId: string, id: string): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* Count documents by type
|
|
40
|
+
*/
|
|
41
|
+
countDocsByType(organizationId: string, type: string): Promise<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Get counts for all document types
|
|
44
|
+
*/
|
|
45
|
+
getDocCountsByType(organizationId: string): Promise<Record<string, number>>;
|
|
46
|
+
countDocsByTypeMultiOrg(organizationIds: string[], type: string): Promise<number>;
|
|
47
|
+
getDocCountsByTypeMultiOrg(organizationIds: string[]): Promise<Record<string, number>>;
|
|
48
|
+
findDocByIdInOrgs(organizationIds: string[], id: string): Promise<Document | null>;
|
|
49
|
+
/**
|
|
50
|
+
* Advanced filtering - find many documents with where clause and pagination
|
|
51
|
+
*/
|
|
52
|
+
findManyDocAdvanced(organizationId: string, collectionName: string, options?: FindOptions): Promise<FindResult<Document>>;
|
|
53
|
+
/**
|
|
54
|
+
* Advanced filtering - find document by ID with options
|
|
55
|
+
*/
|
|
56
|
+
findByDocIdAdvanced(organizationId: string, id: string, options?: Partial<FindOptions>): Promise<Document | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Count documents matching where clause
|
|
59
|
+
*/
|
|
60
|
+
countDocuments(organizationId: string, collectionName: string, where?: Where): Promise<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Find documents that reference a specific asset ID in their JSON data
|
|
63
|
+
*/
|
|
64
|
+
findDocumentsReferencingAsset(organizationId: string, assetId: string, knownTypes?: string[]): Promise<Array<{
|
|
65
|
+
documentId: string;
|
|
66
|
+
type: string;
|
|
67
|
+
title: string;
|
|
68
|
+
status: string | null;
|
|
69
|
+
}>>;
|
|
70
|
+
/**
|
|
71
|
+
* Count document references for multiple asset IDs in batch
|
|
72
|
+
*/
|
|
73
|
+
countDocumentReferencesForAssets(organizationId: string, assetIds: string[], knownTypes?: string[]): Promise<Record<string, number>>;
|
|
74
|
+
/**
|
|
75
|
+
* Clear all references to an asset from publishedData of non-published documents.
|
|
76
|
+
* Called after asset deletion so stale publishedData doesn't hold dangling refs.
|
|
77
|
+
* Only touches draft/unpublished docs — published docs block deletion upstream.
|
|
78
|
+
*/
|
|
79
|
+
clearAssetFromPublishedData(organizationId: string, assetId: string): Promise<number>;
|
|
80
|
+
listDocumentVersions(organizationId: string, documentId: string, options?: {
|
|
81
|
+
limit?: number;
|
|
82
|
+
offset?: number;
|
|
83
|
+
}): Promise<DocumentVersionList>;
|
|
84
|
+
getDocumentVersion(organizationId: string, documentId: string, versionNumber: number): Promise<DocumentVersion | null>;
|
|
85
|
+
createDocumentVersion(data: {
|
|
86
|
+
documentId: string;
|
|
87
|
+
organizationId: string;
|
|
88
|
+
eventType: 'draft' | 'publish';
|
|
89
|
+
data: any;
|
|
90
|
+
createdBy?: string | null;
|
|
91
|
+
}): Promise<DocumentVersion | null>;
|
|
92
|
+
deleteDocumentVersions(documentId: string, versionIds: string[]): Promise<void>;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=document-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-adapter.d.ts","sourceRoot":"","sources":["../src/document-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,KAAK,EACX,eAAe,EACf,kBAAkB,EAClB,QAAQ,EACR,eAAe,EACf,mBAAmB,EACnB,WAAW,EACX,UAAU,EACV,KAAK,EACL,MAAM,2BAA2B,CAAC;AAEnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,C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