File size: 9,007 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint class-methods-use-this: 0 no-unused-vars: 0 */
/* eslint no-useless-constructor: 0 */
import { SupportedDatabasesType } from './supported-databases.type.js'
import { BaseProperty, BaseRecord, ParamsType } from '../index.js'
import { NotImplementedError, Filter } from '../../utils/index.js'
import { ResourceOptions, ResourceDecorator } from '../../decorators/index.js'
import AdminJS from '../../../adminjs.js'
import { ActionContext } from '../../actions/index.js'
/**
* Representation of a ORM Resource in AdminJS. Visually resource is a list item in the sidebar.
* Each resource has many records and many properties.
*
* Analogy is REST resource.
*
* It is an __abstract class__ and all database adapters should implement extend it implement
* following methods:
*
* - (static) {@link BaseResource.isAdapterFor isAdapterFor()}
* - {@link BaseResource#databaseName databaseName()}
* - {@link BaseResource#name name()}
* - {@link BaseResource#id id()}
* - {@link BaseResource#properties properties()}
* - {@link BaseResource#property property()}
* - {@link BaseResource#count count()}
* - {@link BaseResource#find find()}
* - {@link BaseResource#findOne findOne()}
* - {@link BaseResource#findMany findMany()}
* - {@link BaseResource#create create()}
* - {@link BaseResource#update update()}
* - {@link BaseResource#delete delete()}
* @category Base
* @abstract
* @hideconstructor
*/
class BaseResource {
public _decorated: ResourceDecorator | null
/**
* Checks if given adapter supports resource provided by the user.
* This function has to be implemented only if you want to create your custom
* database adapter.
*
* For one time Admin Resource creation - it is not needed.
*
* @param {any} rawResource resource provided in AdminJSOptions#resources array
* @return {Boolean} if given adapter supports this resource - returns true
* @abstract
*/
static isAdapterFor(rawResource): boolean {
throw new NotImplementedError('BaseResource.isAdapterFor')
}
/**
* Creates given resource based on the raw resource object
*
* @param {Object} [resource]
*/
constructor(resource?: any) {
this._decorated = null
}
/**
* The name of the database to which resource belongs. When resource is
* a mongoose model it should be database name of the mongo database.
*
* Visually, by default, all resources are nested in sidebar under their database names.
* @return {String} database name
* @abstract
*/
databaseName(): string {
throw new NotImplementedError('BaseResource#databaseName')
}
/**
* Returns type of the database. It is used to compute sidebar icon for
* given resource. Default: 'database'
* @return {String}
*/
databaseType(): SupportedDatabasesType | string {
return 'other'
}
/**
* Each resource has to have uniq id which will be put to an URL of AdminJS routes.
* For instance in {@link Router} path for the `new` form is `/resources/{resourceId}/new`
* @return {String} uniq resource id
* @abstract
*/
id(): string {
throw new NotImplementedError('BaseResource#id')
}
/**
* returns array of all properties which belongs to resource
* @return {BaseProperty[]}
* @abstract
*/
properties(): Array<BaseProperty> {
throw new NotImplementedError('BaseResource#properties')
}
/**
* returns property object for given field
* @param {String} path path/name of the property. Take a look at
* {@link BaseProperty} to learn more about
* property paths.
* @return {BaseProperty | null}
* @abstract
*/
property(path: string): BaseProperty | null {
throw new NotImplementedError('BaseResource#property')
}
/**
* Returns number of elements for given resource by including filters
* @param {Filter} filter represents what data should be included
* @param {ActionContext} [context]
* @return {Promise<Number>}
* @abstract
*/
async count(filter: Filter, context?: ActionContext): Promise<number> {
throw new NotImplementedError('BaseResource#count')
}
/**
* Returns actual records for given resource
*
* @param {Filter} filter what data should be included
* @param {Object} options
* @param {Number} [options.limit] how many records should be taken
* @param {Number} [options.offset] offset
* @param {Object} [options.sort] sort
* @param {Number} [options.sort.sortBy] sortable field
* @param {Number} [options.sort.direction] either asc or desc
* @param {ActionContext} [context]
* @return {Promise<BaseRecord[]>} list of records
* @abstract
* @example
* // filters example
* {
* name: 'Tom',
* createdAt: { from: '2019-01-01', to: '2019-01-18' }
* }
*/
async find(filter: Filter, options: {
limit?: number;
offset?: number;
sort?: {
sortBy?: string;
direction?: 'asc' | 'desc';
};
}, context?: ActionContext): Promise<Array<BaseRecord>> {
throw new NotImplementedError('BaseResource#find')
}
/**
* Finds one Record in the Resource by its id
*
* @param {String} id uniq id of the Resource Record
* @param {ActionContext} [context]
* @return {Promise<BaseRecord> | null} record
* @abstract
*/
async findOne(id: string, context?: ActionContext): Promise<BaseRecord | null> {
throw new NotImplementedError('BaseResource#findOne')
}
/**
* Finds many records based on the resource ids
*
* @param {Array<string>} ids list of ids to find
* @param {ActionContext} [context]
*
* @return {Promise<Array<BaseRecord>>} records
*/
async findMany(ids: Array<string | number>, context?: ActionContext):
Promise<Array<BaseRecord>> {
throw new NotImplementedError('BaseResource#findMany')
}
/**
* Builds new Record of given Resource.
*
* Each Record is an representation of the resource item. Before it can be saved,
* it has to be instantiated.
*
* This function has to be implemented if you want to create new records.
*
* @param {Record<string, any>} params
* @return {BaseRecord}
*/
build(params: Record<string, any>): BaseRecord {
return new BaseRecord(params, this)
}
/**
* Creates new record
*
* @param {Record<string, any>} params
* @param {ActionContext} [context]
* @return {Promise<Object>} created record converted to raw Object which
* can be used to initiate new {@link BaseRecord} instance
* @throws {ValidationError} If there are validation errors it should be thrown
* @abstract
*/
async create(params: Record<string, any>, context?: ActionContext): Promise<ParamsType> {
throw new NotImplementedError('BaseResource#create')
}
/**
* Updates the record.
*
* @param {String} id uniq id of the Resource Record
* @param {Record<string, any>} params
* @param {ActionContext} [context]
* @return {Promise<Object>} created record converted to raw Object which
* can be used to initiate new {@link BaseRecord} instance
* @throws {ValidationError} If there are validation errors it should be thrown
* @abstract
*/
async update(id: string, params: Record<string, any>, context?: ActionContext)
: Promise<ParamsType> {
throw new NotImplementedError('BaseResource#update')
}
/**
* Delete given record by id
*
* @param {String | Number} id id of the Record
* @param {ActionContext} [context]
* @throws {ValidationError} If there are validation errors it should be thrown
* @abstract
*/
async delete(id: string, context?: ActionContext): Promise<void> {
throw new NotImplementedError('BaseResource#delete')
}
/**
* Assigns given decorator to the Resource. Than it will be available under
* resource.decorate() method
*
* @param {BaseDecorator} Decorator
* @param {AdminJS} admin current instance of AdminJS
* @param {ResourceOptions} [options]
* @private
*/
assignDecorator(admin: AdminJS, options: ResourceOptions = {}): void {
this._decorated = new ResourceDecorator({ resource: this, admin, options })
}
/**
* Gets decorator object for given resource
* @return {BaseDecorator | null}
*/
decorate(): ResourceDecorator {
if (!this._decorated) {
throw new Error('resource does not have any assigned decorator yet')
}
return this._decorated
}
}
export default BaseResource
|