mirror of
https://github.com/zhigang1992/pocketbase-typegen.git
synced 2026-04-29 12:45:19 +08:00
add sort-keys linting (#34)
This commit is contained in:
@@ -5,8 +5,8 @@ import path from "path"
|
||||
it("creates a type file from json schema", async () => {
|
||||
const out = path.resolve(__dirname, "pocketbase-types-example.ts")
|
||||
const result = await main({
|
||||
out,
|
||||
json: path.resolve(__dirname, "pb_schema.json"),
|
||||
out,
|
||||
})
|
||||
|
||||
const fileOutput = await fs.readFile(out, { encoding: "utf-8" })
|
||||
|
||||
@@ -9,39 +9,39 @@ import { FieldSchema } from "../src/types"
|
||||
|
||||
const textField: FieldSchema = {
|
||||
id: "1",
|
||||
system: false,
|
||||
unique: false,
|
||||
options: {},
|
||||
name: "field1",
|
||||
options: {},
|
||||
required: true,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
}
|
||||
const jsonField1: FieldSchema = {
|
||||
id: "2",
|
||||
system: false,
|
||||
unique: false,
|
||||
options: {},
|
||||
name: "data1",
|
||||
options: {},
|
||||
required: true,
|
||||
system: false,
|
||||
type: "json",
|
||||
unique: false,
|
||||
}
|
||||
const jsonField2: FieldSchema = {
|
||||
id: "3",
|
||||
system: false,
|
||||
unique: false,
|
||||
options: {},
|
||||
name: "data2",
|
||||
options: {},
|
||||
required: true,
|
||||
system: false,
|
||||
type: "json",
|
||||
unique: false,
|
||||
}
|
||||
const expandField: FieldSchema = {
|
||||
id: "4",
|
||||
system: false,
|
||||
unique: false,
|
||||
options: {},
|
||||
name: "post_relation_field",
|
||||
options: {},
|
||||
required: true,
|
||||
system: false,
|
||||
type: "relation",
|
||||
unique: false,
|
||||
}
|
||||
|
||||
describe("getGenericArgList", () => {
|
||||
|
||||
@@ -11,38 +11,38 @@ import {
|
||||
|
||||
const defaultFieldSchema: FieldSchema = {
|
||||
id: "abc",
|
||||
system: false,
|
||||
unique: false,
|
||||
options: {},
|
||||
name: "defaultName",
|
||||
options: {},
|
||||
required: true,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
}
|
||||
|
||||
describe("generate", () => {
|
||||
it("generates correct output given db input", () => {
|
||||
const collections: Array<CollectionRecord> = [
|
||||
{
|
||||
name: "books",
|
||||
id: "123",
|
||||
type: "base",
|
||||
system: false,
|
||||
listRule: null,
|
||||
viewRule: null,
|
||||
createRule: null,
|
||||
updateRule: null,
|
||||
deleteRule: null,
|
||||
id: "123",
|
||||
listRule: null,
|
||||
name: "books",
|
||||
schema: [
|
||||
{
|
||||
name: "title",
|
||||
type: "text",
|
||||
required: false,
|
||||
id: "xyz",
|
||||
system: false,
|
||||
unique: false,
|
||||
name: "title",
|
||||
options: {},
|
||||
required: false,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
},
|
||||
],
|
||||
system: false,
|
||||
type: "base",
|
||||
updateRule: null,
|
||||
viewRule: null,
|
||||
},
|
||||
]
|
||||
const result = generate(collections)
|
||||
@@ -69,13 +69,13 @@ describe("createRecordType", () => {
|
||||
const name = "books"
|
||||
const schema: FieldSchema[] = [
|
||||
{
|
||||
system: false,
|
||||
id: "hhnwjkke",
|
||||
name: "title",
|
||||
type: "text",
|
||||
options: { max: null, min: null, pattern: "" },
|
||||
required: false,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
options: { min: null, max: null, pattern: "" },
|
||||
},
|
||||
]
|
||||
const result = createRecordType(name, schema)
|
||||
@@ -86,13 +86,13 @@ describe("createRecordType", () => {
|
||||
const name = "books"
|
||||
const schema: FieldSchema[] = [
|
||||
{
|
||||
system: false,
|
||||
id: "hhnwjkke",
|
||||
name: "avatars",
|
||||
type: "file",
|
||||
required: false,
|
||||
unique: false,
|
||||
options: { maxSelect: 2 },
|
||||
required: false,
|
||||
system: false,
|
||||
type: "file",
|
||||
unique: false,
|
||||
},
|
||||
]
|
||||
const result = createRecordType(name, schema)
|
||||
@@ -103,26 +103,26 @@ describe("createRecordType", () => {
|
||||
describe("createResponseType", () => {
|
||||
it("creates type definition for a response", () => {
|
||||
const row: CollectionRecord = {
|
||||
type: "base",
|
||||
id: "123",
|
||||
system: false,
|
||||
listRule: null,
|
||||
viewRule: null,
|
||||
createRule: null,
|
||||
updateRule: null,
|
||||
deleteRule: null,
|
||||
id: "123",
|
||||
listRule: null,
|
||||
name: "books",
|
||||
schema: [
|
||||
{
|
||||
system: false,
|
||||
id: "hhnwjkke",
|
||||
name: "title",
|
||||
type: "text",
|
||||
options: { max: null, min: null, pattern: "" },
|
||||
required: false,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
options: { min: null, max: null, pattern: "" },
|
||||
},
|
||||
],
|
||||
system: false,
|
||||
type: "base",
|
||||
updateRule: null,
|
||||
viewRule: null,
|
||||
}
|
||||
|
||||
const result = createResponseType(row)
|
||||
@@ -133,13 +133,13 @@ describe("createResponseType", () => {
|
||||
const name = "books"
|
||||
const schema: FieldSchema[] = [
|
||||
{
|
||||
system: false,
|
||||
id: "hhnwjkke",
|
||||
name: "avatars",
|
||||
type: "file",
|
||||
required: false,
|
||||
unique: false,
|
||||
options: { maxSelect: 2 },
|
||||
required: false,
|
||||
system: false,
|
||||
type: "file",
|
||||
unique: false,
|
||||
},
|
||||
]
|
||||
const result = createRecordType(name, schema)
|
||||
@@ -242,10 +242,10 @@ describe("createTypeField", () => {
|
||||
createTypeField("test_collection", {
|
||||
...defaultFieldSchema,
|
||||
name: "selectFieldWithOpts",
|
||||
type: "select",
|
||||
options: {
|
||||
values: ["one", "two", "three"],
|
||||
},
|
||||
type: "select",
|
||||
})
|
||||
).toEqual(`\tselectFieldWithOpts: TestCollectionSelectFieldWithOptsOptions`)
|
||||
})
|
||||
@@ -255,10 +255,10 @@ describe("createTypeField", () => {
|
||||
createTypeField("test_collection", {
|
||||
...defaultFieldSchema,
|
||||
name: "selectField",
|
||||
type: "select",
|
||||
options: {
|
||||
maxSelect: 2,
|
||||
},
|
||||
type: "select",
|
||||
})
|
||||
).toEqual("\tselectField: string[]")
|
||||
})
|
||||
@@ -268,11 +268,11 @@ describe("createTypeField", () => {
|
||||
createTypeField("test_collection", {
|
||||
...defaultFieldSchema,
|
||||
name: "selectFieldWithOpts",
|
||||
type: "select",
|
||||
options: {
|
||||
values: ["one", "two", "three"],
|
||||
maxSelect: 2,
|
||||
values: ["one", "two", "three"],
|
||||
},
|
||||
type: "select",
|
||||
})
|
||||
).toEqual(
|
||||
`\tselectFieldWithOpts: TestCollectionSelectFieldWithOptsOptions[]`
|
||||
@@ -304,10 +304,10 @@ describe("createTypeField", () => {
|
||||
createTypeField("test_collection", {
|
||||
...defaultFieldSchema,
|
||||
name: "fileField",
|
||||
type: "file",
|
||||
options: {
|
||||
maxSelect: 3,
|
||||
},
|
||||
type: "file",
|
||||
})
|
||||
).toEqual("\tfileField: string[]")
|
||||
})
|
||||
@@ -327,10 +327,10 @@ describe("createTypeField", () => {
|
||||
createTypeField("test_collection", {
|
||||
...defaultFieldSchema,
|
||||
name: "relationFieldMany",
|
||||
type: "relation",
|
||||
options: {
|
||||
maxSelect: 3,
|
||||
},
|
||||
type: "relation",
|
||||
})
|
||||
).toEqual("\trelationFieldMany: RecordIdString[]")
|
||||
})
|
||||
@@ -340,10 +340,10 @@ describe("createTypeField", () => {
|
||||
createTypeField("test_collection", {
|
||||
...defaultFieldSchema,
|
||||
name: "relationFieldMany",
|
||||
type: "relation",
|
||||
options: {
|
||||
maxSelect: null,
|
||||
},
|
||||
type: "relation",
|
||||
})
|
||||
).toEqual("\trelationFieldMany: RecordIdString[]")
|
||||
})
|
||||
@@ -375,13 +375,13 @@ describe("createSelectOptions", () => {
|
||||
const name = "choose"
|
||||
const schema: FieldSchema[] = [
|
||||
{
|
||||
system: false,
|
||||
id: "hhnwjkke",
|
||||
name: "title",
|
||||
type: "select",
|
||||
required: false,
|
||||
unique: false,
|
||||
options: { values: ["one", "one", "two", "space space", "$@#*(&#%"] },
|
||||
required: false,
|
||||
system: false,
|
||||
type: "select",
|
||||
unique: false,
|
||||
},
|
||||
]
|
||||
const result = createSelectOptions(name, schema)
|
||||
|
||||
@@ -23,16 +23,16 @@ const thing = getOne(Collections.Everything, "a")
|
||||
|
||||
// Works when passing in JSON generic
|
||||
const everythingRecordWithGeneric: EverythingRecord<{ a: "some string" }> = {
|
||||
bool_field: true,
|
||||
json_field: { a: "some string" },
|
||||
number_field: 1,
|
||||
bool_field: true,
|
||||
}
|
||||
|
||||
// Works without passing in JSON generic
|
||||
const everythingRecordWithoutGeneric: EverythingRecord = {
|
||||
bool_field: true,
|
||||
json_field: { a: "some string" },
|
||||
number_field: 1,
|
||||
bool_field: true,
|
||||
}
|
||||
|
||||
// Test select option enums
|
||||
|
||||
@@ -52,11 +52,11 @@ describe("getOptionValues", () => {
|
||||
const fieldWithoutValues: FieldSchema = {
|
||||
id: "1",
|
||||
name: "myfield",
|
||||
type: "text",
|
||||
system: false,
|
||||
required: false,
|
||||
unique: false,
|
||||
options: {},
|
||||
required: false,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
}
|
||||
expect(getOptionValues(fieldWithoutValues)).toEqual([])
|
||||
})
|
||||
@@ -65,13 +65,13 @@ describe("getOptionValues", () => {
|
||||
const fieldWithValues: FieldSchema = {
|
||||
id: "1",
|
||||
name: "myfield",
|
||||
type: "text",
|
||||
system: false,
|
||||
required: false,
|
||||
unique: false,
|
||||
options: {
|
||||
values: ["one", "one", "one", "two"],
|
||||
},
|
||||
required: false,
|
||||
system: false,
|
||||
type: "text",
|
||||
unique: false,
|
||||
}
|
||||
expect(getOptionValues(fieldWithValues)).toEqual(["one", "two"])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user