Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/local/src/server/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { homedir } from "node:os";
import { basename, join } from "node:path";

import { Scope, ScopeId, collectTables, createExecutor, type AnyPlugin } from "@executor-js/sdk";
import { withQueryContext } from "fumadb/query";
import { loadPluginsFromJsonc } from "@executor-js/config";

import executorConfig from "../../executor.config";
Expand Down Expand Up @@ -189,7 +190,9 @@ const importLegacySqliteIfNeeded = async (options: {
const result = await importSqliteDataToFuma({
sqlitePath: storage.sqlitePath,
markerPath: storage.importMarkerPath,
db: target.db,
target: withQueryContext(target.db, {
allowedScopeIds: new Set([scopeId]),
}),
tables,
scopeId,
});
Expand Down
8 changes: 5 additions & 3 deletions apps/local/src/server/sqlite-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";

import { collectTables } from "@executor-js/sdk";
import { withQueryContext } from "fumadb/query";

import { importSqliteDataToFuma } from "./sqlite-import";
import { createSqliteFumaDb, type SqliteFumaDb } from "./sqlite-fumadb";
Expand Down Expand Up @@ -81,10 +82,11 @@ describe("importSqliteDataToFuma", () => {
path: join(workDir, "target.db"),
});

const scopedDb = withQueryContext(sqlite.db, { allowedScopeIds: new Set(["scope_a"]) });
const result = await importSqliteDataToFuma({
sqlitePath,
markerPath,
db: sqlite.db,
target: scopedDb,
tables,
scopeId: "scope_a",
});
Expand All @@ -96,7 +98,7 @@ describe("importSqliteDataToFuma", () => {
expect(existsSync(sqlitePath)).toBe(false);
expect(result.backupPath && existsSync(result.backupPath)).toBe(true);

const source = (await sqlite.db.findFirst("source", {
const source = (await scopedDb.findFirst("source", {
where: (b) => b("id", "=", "src_1"),
})) as Record<string, unknown>;
expect(source.scope_id).toBe("scope_a");
Expand All @@ -105,7 +107,7 @@ describe("importSqliteDataToFuma", () => {
expect(source.can_edit).toBe(true);
expect(source.created_at).toBeInstanceOf(Date);

const blob = (await sqlite.db.findFirst("blob", {
const blob = (await scopedDb.findFirst("blob", {
where: (b) => b("id", "=", JSON.stringify(["scope_a/plugin", "spec"])),
})) as Record<string, unknown>;
expect(blob.value).toBe("{}");
Expand Down
6 changes: 3 additions & 3 deletions apps/local/src/server/sqlite-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dirname } from "node:path";

/* oxlint-disable executor/no-json-parse, executor/no-switch-statement, executor/no-try-catch-or-throw -- boundary: one-shot legacy SQLite importer normalizes unknown rows and wraps native sqlite failures */

import type { AnyColumn, AnyTable, FumaDb, FumaTables } from "@executor-js/sdk";
import { type AnyColumn, type AnyTable, type FumaTables } from "@executor-js/sdk";

type SqliteRow = Record<string, unknown>;

Expand All @@ -25,7 +25,7 @@ export class LocalSqliteImportError extends Data.TaggedError("LocalSqliteImportE
export interface LocalSqliteImportOptions {
readonly sqlitePath: string;
readonly markerPath: string;
readonly db: FumaDb;
readonly target: ImportFumaDb;
readonly tables: FumaTables;
readonly scopeId: string;
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export const importSqliteDataToFuma = async (
const importedTables: string[] = [];
let importedRows = 0;

await (options.db as ImportFumaDb).transaction(async (db) => {
await options.target.transaction(async (db) => {
for (const [tableKey, table] of Object.entries(options.tables)) {
const tableName = table.names.sql;
if (!tableExists(sqlite!, tableName)) continue;
Expand Down
2 changes: 2 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/core/fumadb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@
},
"devDependencies": {
"@effect/vitest": "catalog:",
"@types/better-sqlite3": "^7.6.13",
"@types/node": "catalog:",
"@types/pg": "^8.20.0",
"@types/semver": "^7.7.1",
"better-sqlite3": "^12.9.0",
"tsup": "catalog:",
"typescript": "catalog:",
"vitest": "catalog:"
Expand Down
44 changes: 16 additions & 28 deletions packages/core/fumadb/src/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
import type {
AnySchema,
AnyTable,
IdColumn,
Relation,
TableColumnValues,
TableInsertValues,
TableUpdateValues,
} from "../schema/create";
import type { Condition, ConditionBuilder } from "./condition-builder";
import type { ORMAdapter } from "./orm";

export type { Condition, ConditionBuilder } from "./condition-builder";
export { withQueryContext } from "./orm";
export type {
CompiledJoin,
ORMAdapter,
SimplifiedCountOptions,
SimplifyFindOptions,
} from "./orm";

export type AnySelectClause = SelectClause<AnyTable>;

export type SelectClause<T extends AnyTable> = true | (keyof T["columns"])[];

type TableToColumnValues<T extends AnyTable> = {
[K in keyof T["columns"]]: T["columns"][K]["$out"];
};
export type TableToColumnValues<T extends AnyTable> = TableColumnValues<T>;

type PickNullable<T> = {
[P in keyof T as null extends T[P] ? P : never]: T[P];
};
export type TableToInsertValues<T extends AnyTable> = TableInsertValues<T>;

type PickNotNullable<T> = {
[P in keyof T as null extends T[P] ? never : P]: T[P];
};

type TableToInsertValues<T extends AnyTable> = Partial<
PickNullable<{
[K in keyof T["columns"]]: T["columns"][K]["$in"];
}>
> &
PickNotNullable<{
[K in keyof T["columns"]]: T["columns"][K]["$in"];
}>;

type TableToUpdateValues<T extends AnyTable> = {
[K in keyof T["columns"]]?: T["columns"][K] extends IdColumn
? never
: T["columns"][K]["$in"];
};
export type TableToUpdateValues<T extends AnyTable> = TableUpdateValues<T>;

type MainSelectResult<
export type MainSelectResult<
S extends SelectClause<T>,
T extends AnyTable,
> = S extends true
Expand Down Expand Up @@ -70,7 +58,7 @@ export type JoinBuilder<T extends AnyTable, Out = {}> = {
: never;
};

type SelectResult<
export type SelectResult<
T extends AnyTable,
JoinOut,
Select extends SelectClause<T>,
Expand Down Expand Up @@ -113,7 +101,7 @@ export type FindManyOptions<
: {});

export interface AbstractQuery<S extends AnySchema> {
internal: ORMAdapter;
internal: ORMAdapter<S>;

/**
* The code in the transaction will receive a transaction query instance.
Expand Down
Loading
Loading