-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add orchid driver * docs: add Orchid driver docs * style: fix lint * chore: add orchid-orm to peerDependencies * fix: OrchidAdapter support create table
- Loading branch information
1 parent
1822312
commit 695cd71
Showing
8 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/bentocache/src/drivers/database/adapters/orchid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { DbResult, DefaultColumnTypes, DefaultSchemaConfig } from 'orchid-orm' | ||
|
||
import { DatabaseDriver } from '../database.js' | ||
import type { CreateDriverResult, DatabaseAdapter, OrchidConfig } from '../../../types/main.js' | ||
|
||
/** | ||
* Create a orchid driver | ||
*/ | ||
export function orchidDriver(options: OrchidConfig): CreateDriverResult<DatabaseDriver> { | ||
return { | ||
options, | ||
factory: (config: OrchidConfig) => { | ||
const adapter = new OrchidAdapter(config) | ||
return new DatabaseDriver(adapter, config) | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* Orchid adapter for the DatabaseDriver | ||
*/ | ||
export class OrchidAdapter implements DatabaseAdapter { | ||
#connection: DbResult<DefaultColumnTypes<DefaultSchemaConfig>> | ||
#tableName!: string | ||
|
||
constructor(config: OrchidConfig) { | ||
this.#connection = config.connection | ||
} | ||
|
||
private getTable() { | ||
return this.#connection(this.#tableName, (t) => ({ | ||
key: t.varchar().primaryKey(), | ||
value: t.varchar(), | ||
expires_at: t | ||
.timestampNoTZ() | ||
.encode((value: Date) => value) | ||
.parse((v: any): number => (v ? new Date(v).valueOf() : v)) | ||
.nullable(), | ||
})) | ||
} | ||
|
||
setTableName(tableName: string): void { | ||
this.#tableName = tableName | ||
} | ||
|
||
async get(key: string): Promise<{ value: string; expiresAt: number | null } | undefined> { | ||
const result = await this.getTable().findByOptional({ key }).select('value', 'expires_at') | ||
|
||
if (!result) return | ||
|
||
return { value: result.value, expiresAt: result.expires_at } | ||
} | ||
|
||
async delete(key: string): Promise<boolean> { | ||
const count = await this.getTable().where({ key }).delete() | ||
return count > 0 | ||
} | ||
|
||
async deleteMany(keys: string[]): Promise<number> { | ||
return await this.getTable().whereIn('key', keys).delete() | ||
} | ||
|
||
async disconnect(): Promise<void> { | ||
await this.#connection.close() | ||
} | ||
|
||
async createTableIfNotExists(): Promise<void> { | ||
await this.#connection.adapter.pool.query(` | ||
CREATE TABLE IF NOT EXISTS "public"."${this.#tableName}" ( | ||
"key" varchar NOT NULL, | ||
"value" text NOT NULL, | ||
"expires_at" timestamp, | ||
PRIMARY KEY ("key") | ||
); | ||
`) | ||
} | ||
|
||
async pruneExpiredEntries(): Promise<void> { | ||
await this.getTable() | ||
.where({ expires_at: { lt: new Date() } }) | ||
.delete() | ||
} | ||
|
||
async clear(prefix: string): Promise<void> { | ||
await this.getTable() | ||
.where({ key: { startsWith: prefix } }) | ||
.delete() | ||
} | ||
|
||
async set(row: { key: string; value: any; expiresAt: Date | null }): Promise<void> { | ||
await this.getTable() | ||
.findBy({ key: row.key }) | ||
.upsert({ | ||
create: { | ||
key: row.key, | ||
value: row.value, | ||
expires_at: row.expiresAt, | ||
}, | ||
update: { | ||
value: row.value, | ||
expires_at: row.expiresAt, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { OrchidConfig } from '../../../src/types/main.js' | ||
import { DatabaseDriver } from '../../../src/drivers/database/database.js' | ||
import { OrchidAdapter } from '../../../src/drivers/database/adapters/orchid.js' | ||
|
||
export function createOrchidStore(options: OrchidConfig) { | ||
const adapter = new OrchidAdapter(options) | ||
return new DatabaseDriver(adapter, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { test } from '@japa/runner' | ||
import { createDb } from 'orchid-orm' | ||
|
||
import { createOrchidStore } from './helpers.js' | ||
import { registerCacheDriverTestSuite } from '../../../src/test_suite.js' | ||
|
||
test.group('Orchid | Postgres driver', (group) => { | ||
registerCacheDriverTestSuite({ | ||
test, | ||
group, | ||
supportsMilliseconds: false, | ||
createDriver: (options) => { | ||
const db = createDb({ user: 'postgres', password: 'postgres' }) | ||
|
||
return createOrchidStore({ connection: db, prefix: 'japa', ...options }) | ||
}, | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.