Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to migrate sql js #2175

Merged
merged 1 commit into from
Dec 2, 2024
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
199 changes: 199 additions & 0 deletions apps/backend/migrations/deployment.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions apps/frontend/src/routes/(authed)/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ export const load: LayoutLoad = async (event) => {

// const search = new URLSearchParams({ redirect: redirectURL })

// const db = await createSqljsDatabase()
// const drizzleDB = await createDrizzleDatabase(db)
// migrate(drizzleDB)

// const qb = await createSqljsQueryBuilder(db)
// await qb
// .insertInto("undb_user")
// .values({
// id: "1",
// email: "test@test.com",
// password: "password",
// username: "test",
// })
// .execute()
// const users = await qb.selectFrom("undb_user").selectAll().execute()
// console.log({ users })

const me = await event.fetch("/api/me")
if (me.redirected) {
throw redirect(301, me.url)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"format": "prettier --write \"**/*.{ts,tsx,md,svelte}\"",
"studio": "drizzle-kit studio --config drizzle.config.ts",
"studio:turso": "drizzle-kit studio --config drizzle.turso.config.ts",
"generate": "bun generate:db",
"generate": "run-s generate:db migrate:deploy",
"generate:db": "drizzle-kit generate --config drizzle.config.ts",
"migrate:db": "drizzle-kit push --config drizzle.config.ts",
"move-assets": "bun run ./scripts/move-assets.ts",
"migrate:deploy": "bun run ./scripts/migrate.ts",
"prepare": "husky || echo 1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { singleton } from "@undb/di"
import { None, Some, type Option } from "@undb/domain"
import { type ApiTokenSpecification, type IApiTokenDTO, type IApiTokenQueryRepository } from "@undb/openapi"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { ApiTokenFilterVisitor } from "./api-token.filter-visitor"

@singleton()
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/api-token/api-token.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { singleton } from "@undb/di"
import type { ApiTokenDo, IApiTokenRepository } from "@undb/openapi"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"

@singleton()
export class ApiTokenRepository implements IApiTokenRepository {
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/audit/audit.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { singleton } from "@undb/di"
import type { Mapper } from "@undb/domain"
import { pick } from "radash"
import type { Audit } from "../db"
import { json } from "../qb"
import { json } from "../qb.server"

@singleton()
export class AuditMapper implements Mapper<AuditDo, Audit, IAuditDTO> {
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/audit/audit.query-repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AuditSpecification, IAuditDTO, IAuditQueryRepository } from "@undb/audit"
import { injectContext, type IContext } from "@undb/context"
import { inject, singleton } from "@undb/di"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { AuditFilterVisitor } from "./audit.filter-visitor"
import { AuditMapper } from "./audit.mapper"

Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/audit/audit.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { Audit, AuditSpecification, IAuditRepository } from "@undb/audit"
import { injectContext, type IContext } from "@undb/context"
import { inject, singleton } from "@undb/di"
import type { Option } from "@undb/domain"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { AuditMapper } from "./audit.mapper"

@singleton()
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/base/base.query-repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WithBaseId, type IBaseDTO, type IBaseQueryRepository, type IBaseSpecification } from "@undb/base"
import { inject, singleton } from "@undb/di"
import { None, Some, type Option } from "@undb/domain"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { BaseFilterVisitor } from "./base.filter-visitor"
import { BaseMapper } from "./base.mapper"

Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/base/base.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { None, Some, type Option } from "@undb/domain"
import { injectTableRepository, TableBaseIdSpecification, type ITableRepository } from "@undb/table"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { UnderlyingTableService } from "../underlying/underlying-table.service"
import { BaseFilterVisitor } from "./base.filter-visitor"
import { BaseMapper } from "./base.mapper"
Expand Down
1 change: 1 addition & 0 deletions packages/persistence/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { isIdbSupported, isOpfsSupported } from "kysely-wasqlite-worker"
export * from "./migrate.client"
export * from "./qb.client"
2 changes: 1 addition & 1 deletion packages/persistence/src/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { inject, singleton } from "@undb/di"
import { AsyncLocalStorage } from "node:async_hooks"
import type { ITxContext } from "./ctx.interface"
import type { IQueryBuilder } from "./qb"
import { injectQueryBuilder } from "./qb.provider"
import type { IQueryBuilder } from "./qb.server"
import type { AnonymousTx, Tx } from "./qb.type"

export interface TxContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { DuplicatedDashboardSpecification } from "@undb/dashboard/src/speci
import type { ExpressionBuilder } from "kysely"
import { AbstractQBVisitor } from "../abstract-qb.visitor"
import type { Database } from "../db"
import type { IQueryBuilder } from "../qb"
import type { IQueryBuilder } from "../qb.server"

export class DashboardFilterVisitor extends AbstractQBVisitor<Dashboard> implements IDashboardSpecVisitor {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/dashboard/dashboard.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DashboardFactory, type Dashboard as DashboardDo, type IDashboardDTO } f
import { singleton } from "@undb/di"
import type { Mapper } from "@undb/domain"
import type { Dashboard } from "../db"
import { json } from "../qb"
import { json } from "../qb.server"

@singleton()
export class DashboardMapper implements Mapper<DashboardDo, Dashboard, IDashboardDTO> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
WithDashboardWidgets,
} from "@undb/dashboard"
import { AbstractQBMutationVisitor } from "../abstract-qb.visitor"
import { json, type IQueryBuilder } from "../qb"
import { json, type IQueryBuilder } from "../qb.server"

export class DashboardMutateVisitor extends AbstractQBMutationVisitor implements IDashboardSpecVisitor {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { inject, singleton } from "@undb/di"
import { None, Some, type Option } from "@undb/domain"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { DashboardFilterVisitor } from "./dashboard.filter-visitor"
import { DashboardMapper } from "./dashboard.mapper"
import { DashboardReferenceVisitor } from "./dashboard.reference-visitor"
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/dashboard/dashboard.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { inject, singleton } from "@undb/di"
import { None, Some, type Option } from "@undb/domain"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { DashboardFilterVisitor } from "./dashboard.filter-visitor"
import { DashboardMapper } from "./dashboard.mapper"
import { DashboardMutateVisitor } from "./dashboard.mutate-visitor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { singleton } from "@undb/di"
import { None, Some, type Option } from "@undb/domain"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { InvitationFilterVisitor } from "./invitation.filter-visitor"

@singleton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { WithSpaceMemberUserId } from "@undb/authz/src/space-member/specifi
import type { ExpressionBuilder } from "kysely"
import { AbstractQBVisitor } from "../abstract-qb.visitor"
import type { Database } from "../db"
import type { IQueryBuilder } from "../qb"
import type { IQueryBuilder } from "../qb.server"

export class SpaceMemberFilterVisitor extends AbstractQBVisitor<SpaceMember> implements ISpaceMemberVisitor {
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SpaceMemberComositeSpecification, type ISpaceMemberDTO, type ISpaceMemberQueryRepository } from "@undb/authz"
import { inject, singleton } from "@undb/di"
import { None, Option, Some } from "@undb/domain"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { MemberMapper } from "./member.mapper"
import { SpaceMemberFilterVisitor } from "./space-member.filter-visitor"

Expand Down
15 changes: 9 additions & 6 deletions packages/persistence/src/member/space-member.repository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SpaceMember,SpaceMemberComositeSpecification,type ISpaceMemberRepository } from "@undb/authz"
import { SpaceMember, SpaceMemberComositeSpecification, type ISpaceMemberRepository } from "@undb/authz"
import { singleton } from "@undb/di"
import { None,Some,type Option } from "@undb/domain"
import { None, Some, type Option } from "@undb/domain"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IQueryBuilder } from "../qb.server"
import { SpaceMemberFilterVisitor } from "./space-member.filter-visitor"

@singleton()
Expand All @@ -17,7 +17,8 @@ export class SpaceMemberRepository implements ISpaceMemberRepository {
) {}

async exists(spec: SpaceMemberComositeSpecification): Promise<boolean> {
const user = await this.txContext.getCurrentTransaction()
const user = await this.txContext
.getCurrentTransaction()
.selectFrom("undb_space_member")
.selectAll()
.where((eb) => {
Expand All @@ -31,7 +32,8 @@ export class SpaceMemberRepository implements ISpaceMemberRepository {
}

async findOne(spec: SpaceMemberComositeSpecification): Promise<Option<SpaceMember>> {
const member = await this.txContext.getCurrentTransaction()
const member = await this.txContext
.getCurrentTransaction()
.selectFrom("undb_space_member")
.selectAll()
.where((eb) => {
Expand Down Expand Up @@ -60,7 +62,8 @@ export class SpaceMemberRepository implements ISpaceMemberRepository {
}
async insert(member: SpaceMember): Promise<void> {
const json = member.toJSON()
await this.txContext.getCurrentTransaction()
await this.txContext
.getCurrentTransaction()
.insertInto("undb_space_member")
.values({
id: json.id,
Expand Down
62 changes: 62 additions & 0 deletions packages/persistence/src/migrate.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { sql } from "drizzle-orm"
import { type SQLJsDatabase } from "drizzle-orm/sql-js"

import config from "../../../apps/backend/migrations/deployment.json"

export function migrate<TSchema extends Record<string, unknown>>(db: SQLJsDatabase<TSchema>) {
const TABLE_NAME = sql.identifier("__drizzle_migrations")

db.run(
sql`
CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
hash TEXT NOT NULL,
tag TEXT NOT NULL,
created_at INTEGER NOT NULL
);
`,
)

const deployments = db.values<[number, string, string]>(
sql`
SELECT id,
hash,
created_at
FROM ${TABLE_NAME}
ORDER BY created_at DESC
LIMIT 1;
`,
)

const deployment = deployments.at(0)

const migrations = config.filter((migration) => {
const timestamp = deployment?.at(2)
return !deployment || Number(timestamp) < migration.when
})

if (migrations.length === 0) {
return console.log("There, currently, are no migrations to deploy")
}

db.transaction((tx) => {
migrations.forEach((migration, i) => {
console.info("%d. Deploying migration:", i + 1)
console.info(" TAG => %s", migration.tag)
console.info(" HASH => %s", migration.hash)
migration.sql.forEach((stmt) => tx.run(stmt))

tx.run(
sql`
INSERT INTO ${TABLE_NAME} ("hash", "created_at", "tag") VALUES (
${sql.raw(`'${migration.hash}'`)},
${sql.raw(`${migration.when}`)},
${sql.raw(`'${migration.tag}'`)}
);
`,
)
})
})

console.info("Database up to date!")
}
2 changes: 1 addition & 1 deletion packages/persistence/src/outbox.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IContext } from "@undb/context"
import type { BaseEvent } from "@undb/domain"
import type { InsertOutbox } from "./db"
import { json } from "./qb"
import { json } from "./qb.server"

export class OutboxMapper {
static fromEvent(event: BaseEvent, context: IContext): InsertOutbox {
Expand Down
12 changes: 10 additions & 2 deletions packages/persistence/src/qb.client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { drizzle } from "drizzle-orm/sql-js"
import { SqlJsDialect } from "kysely-wasm"
import InitSqlJs from "sql.js"
import { createQueryBuilderWithDialect } from "./qb.util"

export const createSqljsQueryBuilder = async () => {
export const createSqljsDatabase = async () => {
const SQL = await InitSqlJs({
locateFile: (file) => `/${file}`,
})
const db = new SQL.Database()
return new SQL.Database()
}

export const createDrizzleDatabase = async (db: InitSqlJs.Database) => {
return drizzle(db)
}

export const createSqljsQueryBuilder = async (db: InitSqlJs.Database) => {
const dialect = new SqlJsDialect({
async database() {
return db
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import type { FormulaField } from "@undb/table/src/modules/schema/fields/variants/formula-field"
import { getTableName } from "drizzle-orm"
import { sql, type QueryCreator, type SelectExpression } from "kysely"
import type { IRecordQueryBuilder } from "../qb"
import type { IRecordQueryBuilder } from "../qb.server"
import { users } from "../tables"
import { JoinTable } from "../underlying/reference/join-table"
import { UnderlyingTable } from "../underlying/underlying-table"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
type UserEqual,
} from "@undb/table"
import { type QueryCreator } from "kysely"
import type { IRecordQueryBuilder } from "../qb"
import type { IRecordQueryBuilder } from "../qb.server"

export class RecordQuerySpecCreatorVisitor implements IRecordVisitor {
private getFieldId(spec: RecordComositeSpecification) {
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/record/record-query.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { FieldIdVo, type Field, type IViewSort, type RecordComositeSpecification
import { sql, type ExpressionBuilder, type SelectQueryBuilder } from "kysely"
import type { ITxContext } from "../ctx.interface"
import { injectTxCTX } from "../ctx.provider"
import type { IRecordQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IRecordQueryBuilder } from "../qb.server"
import { UnderlyingTable } from "../underlying/underlying-table"
import { RecordQueryCreatorVisitor } from "./record-query-creator-visitor"
import { RecordQuerySpecCreatorVisitor } from "./record-query-spec-creator-visitor"
Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/record/record.mutate-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import { startOfDay, startOfToday, startOfTomorrow, startOfYesterday } from "dat
import { sql, type ExpressionBuilder } from "kysely"
import { unique } from "radash"
import { AbstractQBMutationVisitor } from "../abstract-qb.visitor"
import type { IQueryBuilder, IRecordQueryBuilder } from "../qb"
import type { IQueryBuilder, IRecordQueryBuilder } from "../qb.server"
import { JoinTable } from "../underlying/reference/join-table"
import { getDateRangeFieldName } from "../underlying/underlying-table.util"

Expand Down
2 changes: 1 addition & 1 deletion packages/persistence/src/record/record.query-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
} from "@undb/table"
import { getTableName } from "drizzle-orm"
import { sql, type AliasedExpression, type Expression, type ExpressionBuilder } from "kysely"
import type { IRecordQueryBuilder } from "../qb"
import { injectQueryBuilder } from "../qb.provider"
import type { IRecordQueryBuilder } from "../qb.server"
import { users } from "../tables"
import { UnderlyingTable } from "../underlying/underlying-table"
import { RecordQueryHelper } from "./record-query.helper"
Expand Down
Loading