An opinionated tool that uses YAML to create and populate tables using knexjs
npm install knex-yaml-schema --saveyarn add knex-yaml-schema- uses
yamlschema to declare tables in a simple, readable way - uses
camelCaseto reference tables and fields, and converts automatically tosnake_case - inserts data in a simple way, allowing you to keep your references consistent
- resolves dependencies using a simple syntax, so you can insert rows that depend on other row's IDs
The module itself is a function, which takes a knex instance and Promise as parameters.
const knex = require("knex")({ client: "pg" });
const schema = require("knex-yaml-schema")(knex, Promise);Creates the tables specified in the yaml schema. The Yaml syntax is explained in usage below.
Table and field names will be converted from camelCase to snake_case.
Drops the tables specified. Names will be converted from camelCase to snake_case.
Deletes all data in the tables specified and fills them with the given yaml fixtures.
When inserting multiple tables in the same command, you can create refereces -- using the inserted PK of a row to populate another row from another table.
The syntax for doing this is simple:
Artist:
data:
-
name: Miles Davis
-
name: Jimmy Hendrix
Album:
data:
-
name: Kind of Blue
artistId: <Artist[1]>
-
name: Blue Haze
artistId: <Artist[1]>
-
name: Are You Experienced
artistId: <Artist[2]>
-
name: Axis: Bold As Love
artistId: <Artist[2]>
-
name: Band of Gypsies
artistId: <Artist[2]>Table and field names will be converted from camelCase to snake_case.
const pg = require("knex")({ client: "pg" });
const schema = require("knex-yaml-schema")(knex, Promise);
schema.create`
TableWithAVeryComplicatedNameForAChange:
properties:
id: number
floatField:
type: number
format: float
dateField:
type: date
dateTimeField:
type: datetime
yesOrNoField:
type: boolean
jsonDumpOfEverything:
type: json
enumType:
type: enum ['beatles', 'rolling stones']
aFieldWithAComplicatedNameAlso:
type: string
timestamps:
type: timestamps
`; notes: primary key -- an auto-generated uuid
details:
- unique: true
- indexed: true
- not null
notes: foreign key -- a uuid linked to the PK of a table in the database
details:
- indexed: true
- not null
notes: a simple uuid field
notes: reference -- a number representing the ID of an external back-end entity
details:
- indexed: true
- not null
notes: string field with a limited list of possible values
notes: string field with default size of 255 characters.
details:
- size: <vale> -- indicates the size of the string field
notes: long text field
notes: string field containing a json dump
notes: simple logic field
notes: date field. should ignore time
notes: datetime field.
notes: a numeric value with several possible formats. defaults to 'integer'
details:
- format:integer -- standard integer number
- format:float -- low precision floating-point number
notes: shortcut to automatically create two fields for controlling table timestamps. Given field name will be ignored and replaced by the two standard field names.
details:
- name: created_at: timestampz -- defaults to now()
- name: updated_at: timestampz -- defaults to now()
- not null
notes: shortcut to automatically create two fields for storing audit timestamps. Given field name will be ignored and replaced by the two standard field names.
details:
- name: original_created_at: timestampz -- defaults to now()
- name: original_updated_at: timestampz -- defaults to now()
- not null
MIT © Jason Santos