1.1.1 • Published 5 years ago

@vestify/type-orm v1.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Vestify TypeORM

Build Status Coverage Status Depfu Maintainability

What Is It?

@vestify/type-orm is a module for interacting with a database using TypeORM within the @vestify framework. It provides CRUD mappers for Resource which are tailored to a database that is managed with TypeORM and can be used in a Vestify instance. This allows you to implement a Vestify instance without having to write your own CRUD mapping functions, though it does provide flexibility so that if you need to customize your queries for some of your Resources the option is available.

Usage

Installation

Install @vestify/type-orm and peer dependencies:

npm i @vestify/core @vestify/type-orm typeorm

Defining and Using Resource Objects

Use tools from @vestify/type-orm to define your resources:

Set Up Your Schemas, DB and Tables

interface MyResourceShape extends TypeOrmResourceShape {
  name: string
}

const myResourceTableName = 'my_resources'
const myResourceSchema: EntitySchema<MyResourceShape> = new EntitySchema({
  columns: {
    ...standardVestifyColumns,
    name: { type: 'text' },
  },
  name: myResourceTableName,
})

const table = new Table({
  columns: [...standardVestifyTableColumns, { name: 'name', type: 'text' }],
  name: myResourceTableName,
})

Establish a Connection to Your DB and Use it to Build Your Tables

const connection: Connection = createConnection({
  // TypeORM connection settings
})
const queryRunner = connection.createQueryRunner()
await queryRunner.createTable(table)
const myResource = defineResourceUsingSchema(connection, myResourceSchema)

Then you can use those resources in an instance of Vestify from @vestify/core which is included as a dependency:

const userPermissionsRetriever: UserPermissionsRetriever = (req) => {
  // Uses req to retrieve Permissions for the user
}
const vestify = initVestify([myResource], userPermissionsRetriever)

A demo application for how to use @vestify/type-orm specifically is available here.

A more basic example of how to use the Vestify instance for an Express app can be found in the @vestify/core README.

You can also use the methods implemented for the resource directly if you do not need to perform authorizations:

const david = await myResource.create({ name: 'David' })
const walter = await myResource.update({
  payload: { name: 'Walter' },
  resourceIdentifier: { fromQuery: { id: david.id } },
})
// ...etc

Modifiers

The following modifiers are available for you to provide in HTTP requests in the modifiers object as per @vestify/core request handling strategy.

Action Request TypeModifier KeyDescriptionPossible Values
destroy and destroyManyhard_deleteInstructs destroy methods to commit a true DELETE transaction. By default, destroy and destroyMany will simply set the destroyed_at column value to the current datetimetrue, false
create and createManyprevent_updatesInstructs the create method to avoid updating an already existing recordtrue, false

Development

Test Database

You'll need PostgreSQL running on your machine to run the tests. This is the initial database type that the module is being built around, but if others are needed they will have tests written against them as well later on.

MacOS

Make sure you have Homebrew installed

brew doctor
brew update
brew install postgresql
brew services start postgresql
initdb /usr/local/var/postgres
1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.4-alpha

5 years ago

0.0.6-alpha

5 years ago

0.0.5-alpha

5 years ago

0.0.7-alpha

5 years ago

0.0.3-alpha

5 years ago

0.0.2-alpha

5 years ago

0.0.1-alpha

5 years ago