@quatrain/core v1.0.0-beta9
Quatrain Core
Why
Quatrain Core is an abstraction layer designed to accelerate and simplify the development of applications using many models and various data stores, being database or APIs.
With Quatrain Core and peer packages, one may develop robust applications based on Oriented Object Programming with a clear separation of concerns between logic, data and storage.
How to install
yarn install @quatrain/core
Quatrain Core comes with a built-in Mock backend adapter. Other adapters are available in different packages. For example, a Google Firestore adapter is available:
yarn install @quatrain/backend-firestore
How to use
Setup
import { Core } from '@quatrain/core'
Core.addBackend(myAdapter, 'myDB', true)
Create a model
import { BaseObjectCore } from '@quatrain/core'
export type Cat = {
name: string
color: `#${string}`
}
export class CatCore extends BaseObjectCore {
static COLLECTION = 'cats'
static PROPERTIES = [
{
name: 'name',
type: Property.STRING,
minLength: 1,
maxLength: 32,
},
{
name: 'color',
type: Property.STRING,
minLength: 4,
maxLength: 7,
},
]
}
Instantiate a model
const catData: Cat = {
name: 'Garfield',
color: '#ffa502',
}
const garfield = CatCore.fromObject(catData)
console.log(garfield.name)
// > "Garfield"
console.log(garfield.color)
// > "#ffa502"
console.log(garfield.core)
// > CatCore { ... }
Interact with backend
// Let's save Garfield in our database
garfield.core.save()
// Now, let's retrieve Garfield in the database
const persistedGarfield = await CatCore.fromBackend('garfield')
Using repositories
You can use repositories as an alternative way to persist and retrieve models in your backend.
This module provides a BaseRepository class that you can extend and override to apply your business logic when doing backend operations.
Let's create a repository for our Cat model, that prevents us from deleting a cat.
import { Core, BackendInterface, BaseRepository } from '@quatrain/core'
export default class CatRepository extends BaseRepository<Cat> {
constructor(backendAdapter: BackendInterface = Core.getBackend()) {
super(CatCore, backendAdapter)
}
async delete(uid: string) {
throw Error("Don't delete the cats!")
}
}
Now, let's use our new CatRepository.
const repository = new CatRepository()
repository.create(garfield)
repository.read('garfield')
repository.update(persistedGarfield)
repository.delete('garfield') // Will throw "Don't delete the cats!"
23 days ago
1 month ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
11 months ago
12 months ago
12 months ago
11 months ago
1 year ago
2 years ago