@vestify/type-orm v1.1.1
Vestify TypeORM
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 Type | Modifier Key | Description | Possible Values |
---|---|---|---|
destroy and destroyMany | hard_delete | Instructs destroy methods to commit a true DELETE transaction. By default, destroy and destroyMany will simply set the destroyed_at column value to the current datetime | true , false |
create and createMany | prevent_updates | Instructs the create method to avoid updating an already existing record | true , 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago