diff --git a/deno.json b/deno.json index a77d45d..0730069 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "tasks": { - "test": "deno test --unstable --check --allow-read --allow-write --allow-ffi --allow-env=HOME --allow-net --import-map=test/import_map.json test", - "test:watch": "deno test --unstable --check --allow-read --allow-write --allow-ffi --allow-env=HOME --allow-net --import-map=test/import_map.json --watch test" + "test": "deno test --unstable --check --allow-read --allow-write --allow-ffi --allow-env=HOME,DENO_DIR,XDG_CACHE_HOME --allow-net --import-map=test/import_map.json test", + "test:watch": "deno test --unstable --check --allow-read --allow-write --allow-ffi --allow-env=HOME,DENO_DIR,XDG_CACHE_HOME --allow-net --import-map=test/import_map.json --watch test" }, "lint": { "rules": { diff --git a/deno.lock b/deno.lock index bc54f45..9ae973c 100644 --- a/deno.lock +++ b/deno.lock @@ -1,5 +1,16 @@ { - "version": "2", + "version": "3", + "packages": { + "specifiers": { + "npm:ts-expect": "npm:ts-expect@1.3.0" + }, + "npm": { + "ts-expect@1.3.0": { + "integrity": "sha512-e4g0EJtAjk64xgnFPD6kTBUtpnMVzDrMb12N1YZV0VvSlhnVT3SGxiYTLdGy8Q5cYHOIC/FAHmZ10eGrAguicQ==", + "dependencies": {} + } + } + }, "remote": { "https://deno.land/std@0.152.0/_deno_unstable.ts": "be3276fd42cffb49f51b705c4b0aa8656aaf2a34be22d769455c8e50ea38e51a", "https://deno.land/std@0.152.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", @@ -31,6 +42,57 @@ "https://deno.land/std@0.155.0/testing/_format.ts": "cd11136e1797791045e639e9f0f4640d5b4166148796cad37e6ef75f7d7f3832", "https://deno.land/std@0.155.0/testing/asserts.ts": "ac295f7fd22a7af107580e2475402a8c386cb1bf18bf837ae266ac0665786026", "https://deno.land/std@0.161.0/semver/mod.ts": "25f6d286ab714096bc278babbf8d7c7d51ce790c83805fd7e1e2bd6a18b5f2e2", + "https://deno.land/std@0.176.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", + "https://deno.land/std@0.176.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", + "https://deno.land/std@0.176.0/encoding/hex.ts": "50f8c95b52eae24395d3dfcb5ec1ced37c5fe7610ef6fffdcc8b0fdc38e3b32f", + "https://deno.land/std@0.176.0/fmt/colors.ts": "938c5d44d889fb82eff6c358bea8baa7e85950a16c9f6dae3ec3a7a729164471", + "https://deno.land/std@0.176.0/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32", + "https://deno.land/std@0.176.0/fs/copy.ts": "14214efd94fc3aa6db1e4af2b4b9578e50f7362b7f3725d5a14ad259a5df26c8", + "https://deno.land/std@0.176.0/fs/empty_dir.ts": "c3d2da4c7352fab1cf144a1ecfef58090769e8af633678e0f3fabaef98594688", + "https://deno.land/std@0.176.0/fs/ensure_dir.ts": "724209875497a6b4628dfb256116e5651c4f7816741368d6c44aab2531a1e603", + "https://deno.land/std@0.176.0/fs/ensure_file.ts": "c38602670bfaf259d86ca824a94e6cb9e5eb73757fefa4ebf43a90dd017d53d9", + "https://deno.land/std@0.176.0/fs/ensure_link.ts": "c0f5b2f0ec094ed52b9128eccb1ee23362a617457aa0f699b145d4883f5b2fb4", + "https://deno.land/std@0.176.0/fs/ensure_symlink.ts": "2955cc8332aeca9bdfefd05d8d3976b94e282b0f353392a71684808ed2ffdd41", + "https://deno.land/std@0.176.0/fs/eol.ts": "f1f2eb348a750c34500741987b21d65607f352cf7205f48f4319d417fff42842", + "https://deno.land/std@0.176.0/fs/exists.ts": "b8c8a457b71e9d7f29b9d2f87aad8dba2739cbe637e8926d6ba6e92567875f8e", + "https://deno.land/std@0.176.0/fs/expand_glob.ts": "45d17e89796a24bd6002e4354eda67b4301bb8ba67d2cac8453cdabccf1d9ab0", + "https://deno.land/std@0.176.0/fs/mod.ts": "bc3d0acd488cc7b42627044caf47d72019846d459279544e1934418955ba4898", + "https://deno.land/std@0.176.0/fs/move.ts": "4cb47f880e3f0582c55e71c9f8b1e5e8cfaacb5e84f7390781dd563b7298ec19", + "https://deno.land/std@0.176.0/fs/walk.ts": "ea95ffa6500c1eda6b365be488c056edc7c883a1db41ef46ec3bf057b1c0fe32", + "https://deno.land/std@0.176.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", + "https://deno.land/std@0.176.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", + "https://deno.land/std@0.176.0/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0", + "https://deno.land/std@0.176.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", + "https://deno.land/std@0.176.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1", + "https://deno.land/std@0.176.0/path/mod.ts": "4b83694ac500d7d31b0cdafc927080a53dc0c3027eb2895790fb155082b0d232", + "https://deno.land/std@0.176.0/path/posix.ts": "8b7c67ac338714b30c816079303d0285dd24af6b284f7ad63da5b27372a2c94d", + "https://deno.land/std@0.176.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1", + "https://deno.land/std@0.176.0/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba", + "https://deno.land/std@0.179.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", + "https://deno.land/std@0.179.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", + "https://deno.land/std@0.179.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", + "https://deno.land/std@0.179.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", + "https://deno.land/std@0.179.0/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0", + "https://deno.land/std@0.179.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", + "https://deno.land/std@0.179.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1", + "https://deno.land/std@0.179.0/path/mod.ts": "4b83694ac500d7d31b0cdafc927080a53dc0c3027eb2895790fb155082b0d232", + "https://deno.land/std@0.179.0/path/posix.ts": "8b7c67ac338714b30c816079303d0285dd24af6b284f7ad63da5b27372a2c94d", + "https://deno.land/std@0.179.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1", + "https://deno.land/std@0.179.0/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba", + "https://deno.land/x/plug@1.0.1/deps.ts": "35ea2acd5e3e11846817a429b7ef4bec47b80f2d988f5d63797147134cbd35c2", + "https://deno.land/x/plug@1.0.1/download.ts": "8d6a023ade0806a0653b48cd5f6f8b15fcfaa1dbf2aa1f4bc90fc5732d27b144", + "https://deno.land/x/plug@1.0.1/mod.ts": "5dec80ee7a3a325be45c03439558531bce7707ac118f4376cebbd6740ff24bfb", + "https://deno.land/x/plug@1.0.1/types.ts": "d8eb738fc6ed883e6abf77093442c2f0b71af9090f15c7613621d4039e410ee1", + "https://deno.land/x/plug@1.0.1/util.ts": "5ba8127b9adc36e070b9e22971fb8106869eea1741f452a87b4861e574f13481", + "https://deno.land/x/sqlite3@0.9.1/deno.json": "50895b0bb0c13ae38b93413d7f9f62652f6e7076cd99b9876f6b3b7f6c488dca", + "https://deno.land/x/sqlite3@0.9.1/deps.ts": "f6035f0884a730c0d55b0cdce68846f13bbfc14e8afbf0b3cd4f12a52b4107b7", + "https://deno.land/x/sqlite3@0.9.1/mod.ts": "d41b8b30e1b20b537ef4d78cae98d90f6bd65c727b64aa1a18bffbb28f7d6ec3", + "https://deno.land/x/sqlite3@0.9.1/src/blob.ts": "3681353b3c97bc43f9b02f8d1c3269c0dc4eb9cb5d3af16c7ce4d1e1ec7507c4", + "https://deno.land/x/sqlite3@0.9.1/src/constants.ts": "85fd27aa6e199093f25f5f437052e16fd0e0870b96ca9b24a98e04ddc8b7d006", + "https://deno.land/x/sqlite3@0.9.1/src/database.ts": "c326446463955f276dcbe18547ede4b19ea3085bef0980548c0a58d830b3b5d9", + "https://deno.land/x/sqlite3@0.9.1/src/ffi.ts": "b83f6d16179be7a97a298d6e8172941dbf532058e7c2b3df3a708beefe285c90", + "https://deno.land/x/sqlite3@0.9.1/src/statement.ts": "4773bc8699a9084b93e65126cd5f9219c248de1fce447270bdae2c3630637150", + "https://deno.land/x/sqlite3@0.9.1/src/util.ts": "3892904eb057271d4072215c3e7ffe57a9e59e4df78ac575046eb278ca6239cd", "https://deno.land/x/sqlite_native@0.1.0/src/binary_manager.ts": "1b33c4fd58ccf338d90dc212d5083a81f82a5b3cb246579747a5efb36f22fe17", "https://deno.land/x/sqlite_native@0.1.0/src/constants.ts": "85fd27aa6e199093f25f5f437052e16fd0e0870b96ca9b24a98e04ddc8b7d006", "https://deno.land/x/sqlite_native@0.1.0/src/database.ts": "eab9f1c95accd2391e845d5351e4aec8fc02eb6d67723c648a3372565de3ff37", @@ -77,16 +139,5 @@ "https://deno.land/x/zod@v3.18.0/index.ts": "035a7422d9f2be54daa0fe464254b69225b443000673e4794095d672471e8792", "https://deno.land/x/zod@v3.18.0/mod.ts": "64e55237cb4410e17d968cd08975566059f27638ebb0b86048031b987ba251c4", "https://deno.land/x/zod@v3.18.0/types.ts": "f15cf4d03176c0987a6b5d3589ccee35e73b7947f33f5ab93f1be27b3a51517e" - }, - "npm": { - "specifiers": { - "ts-expect": "ts-expect@1.3.0" - }, - "packages": { - "ts-expect@1.3.0": { - "integrity": "sha512-e4g0EJtAjk64xgnFPD6kTBUtpnMVzDrMb12N1YZV0VvSlhnVT3SGxiYTLdGy8Q5cYHOIC/FAHmZ10eGrAguicQ==", - "dependencies": {} - } - } } -} \ No newline at end of file +} diff --git a/drivers/sqlite.ts b/drivers/sqlite.ts index d68a7cf..eb74927 100644 --- a/drivers/sqlite.ts +++ b/drivers/sqlite.ts @@ -1,8 +1,8 @@ -import * as sqlite_native from 'https://deno.land/x/sqlite_native@1.0.5/mod.ts' +import * as sqlite3 from 'https://deno.land/x/sqlite3@0.9.1/mod.ts' import type { OptionalOnEmpty } from '../src/util.ts' import { Vars, type SchemaGeneric } from '../src/schema.ts' import { ModelBase, WithStaticSchema } from '../src/model.ts' -import { StatementBase } from '../src/statement.ts' +import { StatementBase, type RawRowData } from '../src/statement.ts' import { TormBase, type SchemasModel, type InitOptions } from '../src/torm.ts' import { MigrationBase, type MigrationClass } from '../src/migration.ts' import { field } from '../src/mod.ts' @@ -11,10 +11,10 @@ import { field } from '../src/mod.ts' class Statement< Params extends SchemaGeneric, Result extends SchemaGeneric - > extends StatementBase { + > extends StatementBase { public one = (...[params]: OptionalOnEmpty): Result | undefined => { - const row = this.stmt.one(this.encode_params(params)) + const row = this.stmt.get(this.encode_params(params)) if (row) return this.decode_result(row) // throw new Error('undefined one() is unimplemented') } @@ -22,7 +22,13 @@ class Statement< public all = (...[params]: OptionalOnEmpty) => this.stmt.all(this.encode_params(params)).map(this.decode_result) - public exec = (...[params]: OptionalOnEmpty) => this.stmt.exec(this.encode_params(params)) + public exec = (...[params]: OptionalOnEmpty) => { + const changes = this.stmt.run(this.encode_params(params)) + return { + changes, + last_insert_row_id: this.driver.lastInsertRowId + } + } protected prepare = (sql: string) => this.driver.prepare(sql) @@ -41,11 +47,11 @@ const Model = WithStaticSchema(TempModelNonAbstract) abstract class Migration extends MigrationBase { protected create_stmt = Statement.create - static create(version: string, arg: string | ((driver: sqlite_native.Database) => void)): MigrationClass { + static create(version: string, arg: string | ((driver: sqlite3.Database) => void)): MigrationClass { return class InlineMigration extends Migration { version = version call = () => { - if (typeof arg === 'string') this.driver.exec(arg) + if (typeof arg === 'string') this.driver.run(arg) else arg(this.driver) } } @@ -58,9 +64,9 @@ class SqliteMasterModel extends Model('sqlite_master', { }) {} class InitializeTormMetadata extends Migration { version = '0.1.0' - call(driver?: sqlite_native.Database) { + call(driver?: sqlite3.Database) { if (!driver) throw new Error('Cannot initialize torm metadata without passing driver') - driver.exec(` + driver.run(` CREATE TABLE IF NOT EXISTS __torm_metadata__ ( singleton INTEGER NOT NULL UNIQUE DEFAULT 1 CHECK (singleton = 1), -- ensure only a single row can exist torm_version TEXT NOT NULL, @@ -68,7 +74,7 @@ class InitializeTormMetadata extends Migration { updated_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')), created_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')) )`) - driver.prepare(`INSERT INTO __torm_metadata__ (torm_version) VALUES (:torm_version)`).exec({ + driver.prepare(`INSERT INTO __torm_metadata__ (torm_version) VALUES (:torm_version)`).run({ torm_version: this.version }) } @@ -159,14 +165,14 @@ ${columns.join('\n ')} } } -class Torm extends TormBase { - public constructor(private db_path: string, private options: sqlite_native.DatabaseOptions = {}) { +class Torm extends TormBase { + public constructor(private db_path: string, private options: sqlite3.DatabaseOpenOptions = {}) { super() } public async init(options?: InitOptions) { - const driver = new sqlite_native.Database(this.db_path, this.options) - await driver.connect() + const driver = new sqlite3.Database(this.db_path, this.options) + // await driver.connect() this._init(driver, options) this.schemas.version() } @@ -184,7 +190,7 @@ export { Migration, } -type Database = sqlite_native.Database +type Database = sqlite3.Database export type { Database as Driver } export { field } export { Vars } diff --git a/src/statement.ts b/src/statement.ts index 75c23c5..4f30105 100644 --- a/src/statement.ts +++ b/src/statement.ts @@ -37,7 +37,7 @@ export type StatementResult = type ColumnValue = string | number | bigint | Uint8Array | null; -interface RawRowData { +export interface RawRowData { [field_name: string]: ColumnValue } diff --git a/src/util.ts b/src/util.ts index a32a37d..e86b939 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,4 @@ -import type { Driver as SQLiteNativeDriver } from '../drivers/sqlite.ts' +import type { Driver as SQLite3Driver } from '../drivers/sqlite.ts' type AllKeys = T extends any ? keyof T : never; type PickType> = T extends { [k in K]?: any } @@ -15,7 +15,8 @@ export type ValueOf = T[keyof T] export type Constructor = new (...args: any[]) => T; export type Driver = - | SQLiteNativeDriver + // | SQLiteNativeDriver + | SQLite3Driver type TNullProperties = { diff --git a/test/alias.test.ts b/test/alias.test.ts index fc827ad..6bd3c08 100644 --- a/test/alias.test.ts +++ b/test/alias.test.ts @@ -27,7 +27,7 @@ class Tag extends Model('tag', { id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL, tag_group_id INTEGER NOT NULL, - FOREIGN KEY(tag_group_id) REFERENCES tag(tag_group_id) + FOREIGN KEY(tag_group_id) REFERENCES tag_group(id) )`) }