Skip to content
On this page

metronom / Modules / Model / default

Class: default

Model.default

Model Class

Indexable

▪ [index: string]: any

Table of contents

Properties

Constructors

Methods

Properties

keyPrefix

keyPrefix: String

First part of redis key. It's identifier for model class

Defined in

Model.ts:31


keyUnique

keyUnique: undefined | String

Second part of redis key. It's identifier for record

Defined in

Model.ts:34


schema

schema: Schema

Object struct model

Defined in

Model.ts:37


redisClient

redisClient: IRedisAdaptor

Defined in

Model.ts:39


flexSchema

flexSchema: undefined | Boolean

you can't define any key except the fields in schema, but if this value is true, you can only add a value to the schema by giving it keyUnique

Defined in

Model.ts:44

Constructors

constructor

new default(schema, keyPrefix?, modelOption?)

Represents a Metronom ORM Model

Parameters

NameTypeDefault valueDescription
schemaSchemaundefinedRecord's key-value schema
keyPrefixstring'object'Record unique key's prefix. "users:1234" --> "keyPrefix:keyUnique"
modelOption?ModelOptionsundefinedOptional model settings. It's include 3 key. + keyUnique: it's unique part of model key + flexSchema: Normally, you can't define any key except the fields in schema, but if this value is true, you can only add a value to the schema by giving it keyUnique + redisClientOptions: node-redis client options.

Methods

create

create(valueObject): Promise<default>

Creates ModelInstance by parameter then saves it to Redis and returns it

Parameters

NameTypeDescription
valueObjectObjectdata to be saved according to the Model.schema

Returns

Promise<default>

new ModelInstance


findById

findById(id): Promise<null | default>

Fetches record by keyUnique

Parameters

NameTypeDescription
idanykeyUnique

Returns

Promise<null | default>

ModelInstance or null


getAll

getAll(options?): Promise<[] | default[]>

Fetches all records with the same keyPrefix value

Parameters

NameType
options?FilterOptions

Returns

Promise<[] | default[]>

List of ModelInstance


filter

filter(filterFunction): Promise<[] | default[]>

Filters in the same way as Array.filter, pulling all records with the same keyPrefix value

Parameters

NameTypeDescription
filterFunctionFilterFunctionIt takes the values (value, index, array) and returns true then the record is filtered. It can be asynchronous function

Returns

Promise<[] | default[]>

Filtred ModelInstances or empty array


deleteById

deleteById(id): Promise<number>

delete record by keyUnique

Parameters

NameTypeDescription
idanykeyUnique

Returns

Promise<number>

deleted records count it always '1' if it succesfull


deleteAll

deleteAll(options?): Promise<number>

Delete all records with the same keyPrefix value

Parameters

NameType
options?any

Returns

Promise<number>

deleted records count or 0


runCommand

runCommand(commands): Promise<any>

Redis command executer

Example

runCommand(['hget', 'user:1234', 'name'])

Parameters

NameTypeDescription
commandsanyRedis command list.

Returns

Promise<any>

Released under the MIT License.