Skip to content

Commit

Permalink
Merge pull request #3085 from opral/mesdk-199-add-default-values
Browse files Browse the repository at this point in the history
use default values
  • Loading branch information
samuelstroschein authored Aug 30, 2024
2 parents 30601e2 + 881b9af commit bb0792c
Show file tree
Hide file tree
Showing 40 changed files with 92 additions and 784 deletions.
45 changes: 11 additions & 34 deletions inlang/source-code/sdk2/src/database/initDbAndSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { createInMemoryDatabase } from "sqlite-wasm-kysely";
import { test, expect } from "vitest";
import { initDb } from "./initDb.js";
import { isBundleId } from "../bundle-id/bundle-id.js";
import { validate } from "uuid";
import { validate as isUuid } from "uuid";
import { createSchema } from "./schema.js";

test("bundle ids should have a default value", async () => {
test("bundle default values", async () => {
const sqlite = await createInMemoryDatabase({
readOnly: false,
});
Expand All @@ -14,36 +14,15 @@ test("bundle ids should have a default value", async () => {

const bundle = await db
.insertInto("bundle")
.values({
alias: {
mock: "mock",
},
})
.defaultValues()
.returningAll()
.executeTakeFirstOrThrow();

expect(isBundleId(bundle.id)).toBe(true);
});

test("bundle aliases should default to an empty object to ease bundle creation", async () => {
const sqlite = await createInMemoryDatabase({
readOnly: false,
});
const db = initDb({ sqlite });
await createSchema({ sqlite });

const bundle = await db
.insertInto("bundle")
.values({
id: "mock-id",
})
.returningAll()
.executeTakeFirstOrThrow();

expect(bundle.alias).toStrictEqual({});
});

test("message ids should default to uuid", async () => {
test("message default values", async () => {
const sqlite = await createInMemoryDatabase({
readOnly: false,
});
Expand All @@ -61,16 +40,16 @@ test("message ids should default to uuid", async () => {
.values({
bundleId: bundle.id,
locale: "en",
selectors: [],
declarations: [],
})
.returningAll()
.executeTakeFirstOrThrow();

expect(validate(message.id)).toBe(true);
expect(isUuid(message.id)).toBe(true);
expect(message.declarations).toStrictEqual([]);
expect(message.selectors).toStrictEqual([]);
});

test("variant ids should default to uuid", async () => {
test("variant default values", async () => {
const sqlite = await createInMemoryDatabase({
readOnly: false,
});
Expand All @@ -88,8 +67,6 @@ test("variant ids should default to uuid", async () => {
.values({
bundleId: bundle.id,
locale: "en",
selectors: [],
declarations: [],
})
.returningAll()
.executeTakeFirstOrThrow();
Expand All @@ -98,13 +75,13 @@ test("variant ids should default to uuid", async () => {
.insertInto("variant")
.values({
messageId: message.id,
match: {},
pattern: [],
})
.returningAll()
.executeTakeFirstOrThrow();

expect(validate(variant.id)).toBe(true);
expect(isUuid(variant.id)).toBe(true);
expect(variant.match).toStrictEqual({});
expect(variant.pattern).toStrictEqual([]);
});

test("it should handle json serialization", async () => {
Expand Down
8 changes: 6 additions & 2 deletions inlang/source-code/sdk2/src/database/schema.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { Selectable } from "kysely";
import type { Bundle, Message, Variant } from "../schema/schemaV2.js";
import type { InlangDatabaseSchema } from "./schema.js";
import type {
Bundle,
InlangDatabaseSchema,
Message,
Variant,
} from "./schema.js";

let _;

Expand Down
24 changes: 17 additions & 7 deletions inlang/source-code/sdk2/src/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Generated, Insertable, Selectable, Updateable } from "kysely";
import type { Bundle, Message, Variant } from "../schema/schemaV2.js";
import type { SqliteDatabase } from "sqlite-wasm-kysely";
import { Declaration, Expression, Pattern } from "../json-schema/pattern.js";

export async function createSchema(args: { sqlite: SqliteDatabase }) {
args.sqlite.exec(`
Expand All @@ -15,16 +15,16 @@ CREATE TABLE message (
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
bundle_id TEXT NOT NULL,
locale TEXT NOT NULL,
declarations TEXT NOT NULL,
selectors TEXT NOT NULL,
declarations TEXT NOT NULL DEFAULT '[]',
selectors TEXT NOT NULL DEFAULT '[]',
FOREIGN KEY (bundle_id) REFERENCES bundle(id) ON DELETE CASCADE
) strict;
CREATE TABLE variant (
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
message_id TEXT NOT NULL,
match TEXT NOT NULL,
pattern TEXT NOT NULL,
match TEXT NOT NULL DEFAULT '{}',
pattern TEXT NOT NULL DEFAULT '[]',
FOREIGN KEY (message_id) REFERENCES message(id) ON DELETE CASCADE
) strict;
Expand All @@ -44,20 +44,30 @@ type BundleTable = {
alias: Generated<Record<string, string>>;
};

type MessageTable = Omit<Message, "id"> & {
type MessageTable = {
id: Generated<string>;
bundleId: string;
locale: string;
declarations: Generated<Array<Declaration>>;
selectors: Generated<Array<Expression>>;
};

type VariantTable = Omit<Variant, "id"> & {
type VariantTable = {
id: Generated<string>;
messageId: string;
match: Generated<Record<string, string>>;
pattern: Generated<Pattern>;
};

export type Bundle = Selectable<BundleTable>;
export type NewBundle = Insertable<BundleTable>;
export type BundleUpdate = Updateable<BundleTable>;

export type Message = Selectable<MessageTable>;
export type NewMessage = Insertable<MessageTable>;
export type MessageUpdate = Updateable<MessageTable>;

export type Variant = Selectable<VariantTable>;
export type NewVariant = Selectable<VariantTable>;
export type VariantUpdate = Updateable<VariantTable>;

Expand Down
22 changes: 13 additions & 9 deletions inlang/source-code/sdk2/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// @ts-ignore
import { v4 as uuid } from "uuid";
import type { Bundle, Expression, Text, Variant } from "./schema/schemaV2.js";
import type { ProjectSettings } from "./schema/settings.js";
import type { ProjectSettings } from "./json-schema/settings.js";
import { generateBundleId } from "./bundle-id/bundle-id.js";
import type { BundleNested, MessageNested } from "./database/schema.js";
import type {
Bundle,
MessageNested,
NewBundleNested,
NewMessageNested,
NewVariant,
Variant,
} from "./database/schema.js";
import type { Expression, Text } from "./json-schema/pattern.js";

/**
* create v2 Bundle with a random human ID
Expand All @@ -18,7 +24,7 @@ export function createBundle(args: {
id?: string;
messages: MessageNested[];
alias?: Bundle["alias"];
}): BundleNested {
}): NewBundleNested {
return {
id: args.id ?? generateBundleId(),
alias: args.alias ?? {},
Expand All @@ -35,14 +41,12 @@ export function createMessage(args: {
locale: ProjectSettings["locales"][number];
text: string;
match?: Record<Expression["arg"]["name"], string>;
}): MessageNested {
}): NewMessageNested {
const messageId = uuid();
return {
bundleId: args.bundleId,
id: messageId,
locale: args.locale,
declarations: [],
selectors: [],
variants: [
createVariant({
messageId: messageId,
Expand All @@ -63,7 +67,7 @@ export function createVariant(args: {
text?: string;
match?: Record<Expression["arg"]["name"], string>;
pattern?: Variant["pattern"];
}): Variant {
}): NewVariant {
return {
messageId: args.messageId,
id: args.id ? args.id : uuid(),
Expand Down
2 changes: 1 addition & 1 deletion inlang/source-code/sdk2/src/import-export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
PluginDoesNotImplementFunctionError,
PluginMissingError,
} from "../plugin/errors.js";
import type { ProjectSettings } from "../schema/settings.js";
import type { ProjectSettings } from "../json-schema/settings.js";
import type { InlangDatabaseSchema } from "../database/schema.js";
import { selectBundleNested } from "../query-utilities/selectBundleNested.js";
import type { InlangPlugin } from "../plugin/schema.js";
Expand Down
6 changes: 3 additions & 3 deletions inlang/source-code/sdk2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export { newProject } from "./project/newProject.js";
export { loadProjectInMemory } from "./project/loadProjectInMemory.js";
export { loadProjectFromDirectoryInMemory } from "./project/loadProjectFromDirectory.js";
export type { InlangProject } from "./project/api.js";
export * from "./schema/schemaV2.js";
export * from "./schema/settings.js";
export * from "./json-schema/settings.js";
export * from "./json-schema/pattern.js";
export * from "./mock/index.js";
export * from "./helper.js";
export * from "./query-utilities/index.js";
Expand All @@ -18,4 +18,4 @@ export type { InlangDatabaseSchema } from "./database/schema.js";
export type { ResourceFile } from "./project/api.js";
export type { InlangPlugin } from "./plugin/schema.js";
export type { IdeExtensionConfig } from "./plugin/meta/ideExtension.js";
export * from "./database/schema.js";
export * from "./database/schema.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the message data structure that has been used in the v1 version of the SDK.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from "vitest";
import { fromMessageV1 } from "./fromMessageV1.js";
import { Value } from "@sinclair/typebox/value";
import { MessageV1 } from "../schemaV1.js";
import { MessageV1 } from "./schemaV1.js";

const messageV1: MessageV1 = {
id: "hello_world",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { generateStableBundleId } from "../../bundle-id/bundle-id.js";
import type { BundleNested, MessageNested } from "../../database/schema.js";
import type {
BundleNested,
MessageNested,
Variant,
} from "../../database/schema.js";
import type { InlangPlugin } from "../../plugin/schema.js";
import type { MessageV1, PatternV1 } from "../schemaV1.js";
import type { Declaration, Expression, Pattern, Variant } from "../schemaV2.js";
import type { Declaration, Expression, Pattern } from "../pattern.js";
import type { MessageV1, PatternV1 } from "./schemaV1.js";

/**
* Converts a MessageV1 into a BundleNested
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from "vitest";
import { toMessageV1 } from "./toMessageV1.js";
import { Value } from "@sinclair/typebox/value";
import { MessageV1 } from "../schemaV1.js";
import { MessageV1 } from "./schemaV1.js";
import type { BundleNested } from "../../database/schema.js";

test("toMessageV1", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { BundleNested } from "../../database/schema.js";
import type { InlangPlugin } from "../../plugin/schema.js";
import type { Expression, Pattern } from "../pattern.js";
import type {
ExpressionV1,
MessageV1,
PatternV1,
VariantV1,
} from "../schemaV1.js";
import type { Expression, Pattern } from "../schemaV2.js";
} from "./schemaV1.js";

/**
* Converts a BundleNested into a legacy format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,3 @@ export const Declaration = Type.Object({

export type Pattern = Static<typeof Pattern>;
export const Pattern = Type.Array(Type.Union([Text, Expression]));

export type Bundle = Static<typeof Bundle>;
export const Bundle = Type.Object({
id: Type.String(),
alias: Type.Record(Type.String(), Type.String()),
});

export type Message = Static<typeof Message>;
export const Message = Type.Object({
id: Type.String(),
bundleId: Type.String(),
locale: Type.String(),
declarations: Type.Array(Declaration),
selectors: Type.Array(Expression),
});

export type Variant = Static<typeof Variant>;
export const Variant = Type.Object({
id: Type.String(),
messageId: Type.String(),
match: Type.Record(Type.String(), Type.String()),
pattern: Pattern,
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { test, expect } from "vitest";
import { loadProjectInMemory } from "../project/loadProjectInMemory.js";
import { newProject } from "../project/newProject.js";
import type { Change, NewChange } from "@lix-js/sdk";
import type { Bundle } from "../schema/schemaV2.js";
import { applyChanges } from "./applyChanges.js";
import { loadDatabaseInMemory } from "sqlite-wasm-kysely";
import { initDb } from "../database/initDb.js";
import type { Bundle } from "../database/schema.js";

test("it should be able to delete", async () => {
const project = await loadProjectInMemory({
Expand Down
Loading

0 comments on commit bb0792c

Please sign in to comment.