@minatojs/driver-sqlite
Advanced tools
Comparing version 4.4.1 to 4.5.0
@@ -21,10 +21,2 @@ import { Builder } from '@minatojs/sql-utils'; | ||
} | ||
export interface SQLiteFieldInfo { | ||
cid: number; | ||
name: string; | ||
type: string; | ||
notnull: number; | ||
dflt_value: string; | ||
pk: boolean; | ||
} | ||
export class SQLiteDriver extends Driver<SQLiteDriver.Config> { | ||
@@ -69,2 +61,6 @@ static name: string; | ||
withTransaction(callback: () => Promise<void>): Promise<void>; | ||
getIndexes(table: string): Promise<Driver.Index<string>[]>; | ||
createIndex(table: string, index: Driver.Index): Promise<void>; | ||
dropIndex(table: string, name: string): Promise<void>; | ||
_parseIndexDef(def: string): {}; | ||
} | ||
@@ -71,0 +67,0 @@ export namespace SQLiteDriver { |
{ | ||
"name": "@minatojs/driver-sqlite", | ||
"version": "4.4.1", | ||
"version": "4.5.0", | ||
"description": "SQLite Driver for Minato", | ||
@@ -26,9 +26,9 @@ "type": "module", | ||
"type": "git", | ||
"url": "git+https://github.com/shigma/minato.git", | ||
"url": "git+https://github.com/cordiverse/minato.git", | ||
"directory": "packages/sqlite" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/shigma/minato/issues" | ||
"url": "https://github.com/cordiverse/minato/issues" | ||
}, | ||
"homepage": "https://github.com/shigma/minato/packages/sqlite#readme", | ||
"homepage": "https://github.com/cordiverse/minato/packages/sqlite#readme", | ||
"keywords": [ | ||
@@ -41,6 +41,8 @@ "orm", | ||
"peerDependencies": { | ||
"minato": "^3.4.1" | ||
"minato": "^3.5.0" | ||
}, | ||
"devDependencies": { | ||
"@minatojs/tests": "^2.4.1" | ||
"@minatojs/tests": "^2.5.0", | ||
"cordis": "^3.18.0", | ||
"minato": "^3.5.0" | ||
}, | ||
@@ -47,0 +49,0 @@ "dependencies": { |
@@ -5,4 +5,4 @@ # @minatojs/driver-sqlite | ||
[![npm](https://img.shields.io/npm/v/@minatojs/driver-sqlite?style=flat-square)](https://www.npmjs.com/package/@minatojs/driver-sqlite) | ||
[![GitHub](https://img.shields.io/github/license/shigma/minato?style=flat-square)](https://github.com/shigma/minato/blob/master/LICENSE) | ||
[![GitHub](https://img.shields.io/github/license/cordiverse/minato?style=flat-square)](https://github.com/cordiverse/minato/blob/master/LICENSE) | ||
SQLite Driver for Minato. |
@@ -5,3 +5,3 @@ import { Binary, deepEqual, Dict, difference, isNullable, makeArray, mapValues } from 'cosmokit' | ||
import { resolve } from 'node:path' | ||
import { readFile, writeFile } from 'node:fs/promises' | ||
import { access, readFile, writeFile } from 'node:fs/promises' | ||
import { createRequire } from 'node:module' | ||
@@ -37,3 +37,3 @@ import init from '@minatojs/sql.js' | ||
export interface SQLiteFieldInfo { | ||
interface SQLiteFieldInfo { | ||
cid: number | ||
@@ -47,2 +47,9 @@ name: string | ||
interface SQLiteMasterInfo { | ||
type: string | ||
name: string | ||
tbl_name: string | ||
sql: string | ||
} | ||
export class SQLiteDriver extends Driver<SQLiteDriver.Config> { | ||
@@ -172,2 +179,11 @@ static name = 'sqlite' | ||
}) | ||
if (this.path !== ':memory:') { | ||
const dir = resolve(this.path, '..') | ||
try { | ||
await access(dir) | ||
} catch { | ||
throw new Error(`The database directory '${resolve(this.path, '..')}' is not accessible. You may have to create it first.`) | ||
} | ||
} | ||
if (!isBrowser || this.path === ':memory:') { | ||
@@ -221,3 +237,3 @@ this.db = new sqlite.Database(this.path) | ||
this.define<number, number | bigint>({ | ||
types: Field.number as any, | ||
types: ['primary', ...Field.number as any], | ||
dump: value => value, | ||
@@ -434,2 +450,39 @@ load: value => isNullable(value) ? value : Number(value), | ||
} | ||
async getIndexes(table: string) { | ||
const indexes = this._all(`SELECT type,name,tbl_name,sql FROM sqlite_master WHERE type = 'index' AND tbl_name = ?`, [table]) as SQLiteMasterInfo[] | ||
const result: Driver.Index[] = [] | ||
for (const { name, sql } of indexes) { | ||
result.push({ | ||
name, | ||
unique: !sql || sql.toUpperCase().startsWith('CREATE UNIQUE'), | ||
keys: this._parseIndexDef(sql), | ||
}) | ||
} | ||
return result | ||
} | ||
async createIndex(table: string, index: Driver.Index) { | ||
const name = index.name ?? Object.entries(index.keys).map(([key, direction]) => `${key}_${direction ?? 'asc'}`).join('+') | ||
const keyFields = Object.entries(index.keys).map(([key, direction]) => `${escapeId(key)} ${direction ?? 'asc'}`).join(', ') | ||
await this._run(`create ${index.unique ? 'UNIQUE' : ''} index ${escapeId(name)} ON ${escapeId(table)} (${keyFields})`) | ||
} | ||
async dropIndex(table: string, name: string) { | ||
await this._run(`DROP INDEX ${escapeId(name)}`) | ||
} | ||
_parseIndexDef(def: string) { | ||
if (!def) return {} | ||
try { | ||
const keys = {}, matches = def.match(/\((.*)\)/)! | ||
matches[1].split(',').forEach((key) => { | ||
const [name, direction] = key.trim().split(' ') | ||
keys[name.startsWith('`') ? name.slice(1, -1) : name] = direction?.toLowerCase() === 'desc' ? 'desc' : 'asc' | ||
}) | ||
return keys | ||
} catch { | ||
return {} | ||
} | ||
} | ||
} | ||
@@ -436,0 +489,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
145517
2208
3