generated from avior/template-web-astro
Initial commit
This commit is contained in:
103
src/models/Dao.ts
Normal file
103
src/models/Dao.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* the Dao is the object that connect the Database or source to the application layer
|
||||
*
|
||||
* you MUST call it through the `DaoFactory` file
|
||||
*/
|
||||
export default abstract class Dao<Object extends { id: any } = { id: any }> {
|
||||
|
||||
/**
|
||||
* insert a new object into the source
|
||||
*
|
||||
* @param obj the object to create
|
||||
* @returns the object with it's id filled if create or null otherwise
|
||||
*/
|
||||
abstract create(obj: Omit<Object, 'id'>): Promise<Object | null>
|
||||
|
||||
/**
|
||||
* insert a new object into the source
|
||||
*
|
||||
* @param obj the object to create
|
||||
* @returns the object with it's id filled if create or null otherwise
|
||||
*/
|
||||
public insert: Dao<Object>['create'] = (obj: Parameters<Dao<Object>['create']>[0]) => this.create(obj)
|
||||
|
||||
/**
|
||||
* find the list of objects having elements from the query
|
||||
*
|
||||
* @param query a partial object which filter depending on the elements, if not set it will fetch everything
|
||||
* @returns an array containing the list of elements that match with the query
|
||||
*/
|
||||
abstract findAll(query?: Partial<Object>): Promise<Array<Object>>
|
||||
|
||||
/**
|
||||
* find the list of objects having elements from the query
|
||||
*
|
||||
* @param query a partial object which filter depending on the elements, if not set it will fetch everything
|
||||
* @returns an array containing the list of elements that match with the query
|
||||
*/
|
||||
public find: Dao<Object>['findAll'] = (query: Parameters<Dao<Object>['findAll']>[0]) => this.findAll(query)
|
||||
|
||||
/**
|
||||
* find an object by it's id
|
||||
*
|
||||
* (shortcut to findOne({id: id}))
|
||||
*
|
||||
* @param id the id of the object
|
||||
* @returns
|
||||
*/
|
||||
public findById(id: Object['id']): Promise<Object | null> {
|
||||
return this.findOne({id: id} as Partial<Object>)
|
||||
}
|
||||
|
||||
/**
|
||||
* find an object by it's id
|
||||
*
|
||||
* (shortcut to findOne({id: id}))
|
||||
*
|
||||
* @param id the id of the object
|
||||
* @returns
|
||||
*/
|
||||
public get(id: Object['id']) {
|
||||
return this.findById(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* find the first element that match `query`
|
||||
*
|
||||
* @param query a partial object which filter depending on the elements, if not set it will fetch everything
|
||||
* @returns the first element matching with the query or null otherwise
|
||||
*/
|
||||
public async findOne(query?: Partial<Object>): Promise<Object | null> {
|
||||
return (await this.findAll(query))[0] ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* update the remote reference of the object
|
||||
*
|
||||
* note: it will not try to insert an item (use `upsert` to handle this)
|
||||
*
|
||||
* @param obj the object to update
|
||||
* @returns an object if it was able to update or null otherwise
|
||||
*/
|
||||
abstract update(obj: Object): Promise<Object | null>
|
||||
|
||||
/**
|
||||
* update the remote reference of the object or create it if not found
|
||||
* @param obj the object to update/insert
|
||||
* @returns the object is updated/inserted or null otherwise
|
||||
*/
|
||||
public async upsert(object: Object | Omit<Object, 'id'>): Promise<Object | null> {
|
||||
if ('id' in object) {
|
||||
return this.update(object)
|
||||
}
|
||||
return this.insert(object)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the object
|
||||
* @param obj the object to delete
|
||||
*
|
||||
* @returns if the object was deleted or not (if object is not in db it will return true)
|
||||
*/
|
||||
abstract delete(obj: Object): Promise<boolean>
|
||||
}
|
54
src/models/DaoFactory.ts
Normal file
54
src/models/DaoFactory.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* TODO:
|
||||
* Add to `DaoItem` your model name
|
||||
* Add to the function `initDao` the Dao
|
||||
*/
|
||||
|
||||
/**
|
||||
* the different Daos that can be initialized
|
||||
*
|
||||
* Touch this interface to define which key is linked to which Dao
|
||||
*/
|
||||
interface DaoItem {
|
||||
}
|
||||
|
||||
/**
|
||||
* Class to get any DAO
|
||||
*/
|
||||
export default class DaoFactory {
|
||||
/**
|
||||
* reference of the different Daos for a correct singleton implementation
|
||||
*/
|
||||
private static daos: Partial<DaoItem> = {}
|
||||
|
||||
/**
|
||||
* Get a a dao by its key
|
||||
*
|
||||
* it will throw an error if no Dao exists linked to the item key
|
||||
*
|
||||
* @param key the dao key to get
|
||||
* @returns the Dao you want as a singleton
|
||||
*/
|
||||
public static get<Key extends keyof DaoItem>(key: Key): DaoItem[Key] {
|
||||
if (!(key in this.daos)) {
|
||||
const dao = this.initDao(key)
|
||||
if (!dao) {
|
||||
throw new Error(`${key} has no valid Dao`)
|
||||
}
|
||||
this.daos[key] = dao as DaoItem[Key]
|
||||
}
|
||||
return this.daos[key] as DaoItem[Key]
|
||||
}
|
||||
|
||||
/**
|
||||
* init a dao by its key, it does not care if it exists or not
|
||||
*
|
||||
* @param item the element to init
|
||||
* @returns a new initialized dao or undefined if no dao is linked
|
||||
*/
|
||||
private static initDao(item: keyof DaoItem): any | undefined {
|
||||
switch (item) {
|
||||
default: return undefined
|
||||
}
|
||||
}
|
||||
}
|
26
src/models/README.md
Normal file
26
src/models/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Models
|
||||
|
||||
this folder contains the Application data layer
|
||||
|
||||
## Workflow
|
||||
|
||||
1. Add a {model}/index.ts contianing your `interface`
|
||||
2. Add a {model}/{model}Dao.ts containing your DAO that `extends` from `Dao.ts`
|
||||
3. Add your Dao to the `DaoFactory.ts` file
|
||||
|
||||
|
||||
## **/index.ts
|
||||
|
||||
file containing the definition of the model
|
||||
|
||||
## **/\*Dao.ts
|
||||
|
||||
File containing the implementation of the Dao
|
||||
|
||||
## Dao.ts
|
||||
|
||||
the Dao.ts is the file each `*Dao.ts` extends from allowing to have a simple, quick and easy to comprehend connectivity
|
||||
|
||||
## DaoFactory.ts
|
||||
|
||||
The DaoFactory file is the file in which you will have the only direct reference to each `*Dao` files and will be sent from there to the rest of the applicaiton layer
|
Reference in New Issue
Block a user