0.6.1 • Published 3 days ago

kysely-plugin-serialize v0.6.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

kysely-plugin-serialize

Auto serialize / deserialize plugin for kysely

Purpose

Kysely does not process the values passed in, so in some cases you may need to manually handle the values to ensure they can be processed correctly by the dialect. Therefore, the main goal of this plugin is DX, so the values serialized by the default serializer may not conform to the usual standards. If you need to ensure the actual values in the database, you can write your own serializer.

Install

pnpm add kysely kysely-plugin-serialize

Usage

The following example will return an error when using sqlite dialects, unless using this plugin:

interface TestTable {
  id: Generated<number>
  person: { name: string, age: number, time: Date } | null
  gender: boolean
  blob: Uint8Array | null
  date: Date
}

interface Database {
  test: TestTable
}

const db = new Kysely<Database>({
  dialect: new SqliteDialect({
    database: new Database(':memory:'),
  }),
  plugins: [
    new SerializePlugin(),
  ],
})

await db.insertInto('test').values({
  gender: true,
  person: { name: 'test', age: 2, time: new Date() },
  blob: Uint8Array.from([1, 2, 3]),
  date: new Date(),
}).execute()

You can also provide a custom serializer function:

const db = new Kysely<Database>({
  dialect: new SqliteDialect({
    database: new Database(':memory:'),
  }),
  plugins: [
    new SerializePlugin({
      serializer: (value) => {
        if (value instanceof Date) {
          return formatDatetime(value)
        }

        if (value !== null && typeof value === 'object') {
          return JSON.stringify(value)
        }

        return value
      }
    }),
  ],
})

Notice

THIS PLUGIN SHOULD BE PLACED AT THE END OF PLUGINS ARRAY

default serializer / deserializer is built for SQLite

rules:

  1. number / bigint / ArrayBuffer / Buffer will skip serialization
  2. boolean will be serialized to 'true' / 'false'
  3. Date will be serialized to ISO string
  4. others will be serialized by JSON.stringify / JSON.parse

Credit

kysely #138

0.6.1

3 days ago

0.6.0

2 months ago

0.5.7

3 months ago

0.5.6

4 months ago

0.5.4

5 months ago

0.5.3

6 months ago

0.5.2

6 months ago

0.5.1

6 months ago

0.5.0

6 months ago

0.4.5

7 months ago

0.4.4

7 months ago

0.4.7

6 months ago

0.4.6

7 months ago

0.4.0

8 months ago

0.4.3

8 months ago

0.4.2

8 months ago

0.3.0

11 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.3.6

10 months ago

0.3.5

10 months ago

0.3.8

10 months ago

0.3.7

10 months ago

0.3.2

11 months ago

0.3.1

11 months ago

0.2.2

12 months ago

0.3.4

11 months ago

0.1.6

12 months ago

0.3.3

11 months ago

0.1.5

1 year ago

0.1.4-patch1

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.2-beta.0

1 year ago

0.1.0

1 year ago