RestOC.Record_Base module

Record Base Module

The base concept for all records stored in any sort of DB

class RestOC.Record_Base.Record(record={}, custom={})

Bases: abc.ABC

The base class for all child record classes

__contains__(field)

Python magic method for checking a key exists in a dict like object

Parameters

field (str) – The field to check for

Returns

bool

__delitem__(field)

Python magic method for deleting a key from a dict like object

Parameters

field (str) – The field to delete

Raises

KeyError

Returns

None

__getitem__(field)

Python magic method for getting a key from a dict like object

Parameters

field (str) – The field to return

Raises

KeyError

Returns

mixed

__init__(record={}, custom={})

Constructor

Initialises the instance and returns it

Parameters
  • record (dict) – The record data

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

Record

__setitem__(field, val)

Python magic method for setting a key in a dict like object

Parameters
  • field (str) – The field to set

  • val (mixed) – The value of the field

Raises
  • KeyError

  • ValueError

Returns

None

__str__()

Python magic method to return a string for the instance

Returns

str

abstract classmethod addChanges(_id, changes, customer={})

Add Changes

Adds a record to the tables associated _changes table. Useful for Record types that can’t handle multiple levels and have children tables that shouldn’t be updated for every change in a single record

Parameters
  • _id (mixed) – The ID of the record the change is associated with

  • changes (dict) – The dictionary of changes to add

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

abstract classmethod append(_id, array, item, custom={})

Append

Adds an item to a given array/list for a specific record

Parameters
  • _id (mixed) – The ID of the record to append to

  • array (str) – The name of the field with the array

  • item (mixed) – The value to append to the array

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

changed(field)

Changed

Returns whether a specific field has been changed, might give a false positive if the entire record is marked as to be replaced

Parameters

field (str) – The field to check

Returns

bool

changes()

Changes

Returns the list of changed fields

Returns

list

abstract classmethod config()

Config

Returns the configuration data associated with the record type

Returns

dict

abstract classmethod contains(_id, array, item, custom={})

Contains

Checks if a specific item exist inside a given array/list

Parameters
  • _id (mixed) – The ID of the record to check

  • array (str) – The name of the field with the array

  • item (mixed) – The value to check for in the array

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

abstract classmethod count(_id, index=None, filter=None, custom={})

Count

Returns the number of records associated with index or filter

Parameters
  • _ids (mixed) – The ID to check

  • index (str) – Used as the index instead of the primary key

  • filter (dict) – Additional filter

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

abstract create(conflict='error', changes=None)

Create

Adds the record to the DB and returns the primary key

Parameters
  • conflict (str) – Must be one of ‘error’, ‘replace’, or ‘update’

  • changes (dict) – Data needed to store a change record, is dependant on the ‘changes’ config value

Returns

mixed|None

abstract classmethod createMany(records, conflict='error', custom={})

Create Many

Inserts multiple records at once

Parameters
  • records (Record_Base.Record[]) – A list of Record instances to insert

  • conflict (str) – What to do on a conflict, Record type specific

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

mixed|None

abstract delete(changes=None)

Delete

Deletes the record represented by the instance

Parameters

changes (dict) – Data needed to store a change record, is dependant on the ‘changes’ config value

Returns

bool

abstract classmethod deleteGet(_id, index=None, custom={})

Delete Get

Deletes one or many records by ID or index and returns how many were found/deleted

Parameters
  • _id (mixed|mixed[]) – The ID(s) to delete or None for all records

  • index (str) – Used as the index instead of the primary key

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

int

abstract classmethod exists(_id, index=None, custom={})

Exists

Returns true if the specified ID or unique index value exists

Parameters
  • _id (mixed) – The ID to check

  • index (str) – Used as the index instead of the primary key

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

fieldDelete(field)

Field Delete

Deletes a specific field from a record (used by __delitem__)

Parameters

field (str) – The field to delete

Raises

KeyError

Returns

self

fieldGet(field, default=None)

Field Get

Returns a specific field, if it’s not found, returns the default

Parameters
  • field (str) – The field to get

  • default (mixed) – Returned if the field doesn’t exist

Returns

mixed

fieldSet(field, val)

Field Set

Sets a specific field in a record (used by __setitem__)

Parameters
  • field (str) – The name of the field to set

  • val (mixed) – The value to set the field to

Raises
  • KeyError – field doesn’t exist in the structure of the record

  • ValueError – value is not valid for the field

Returns

self for chaining

abstract classmethod filter(fields, raw=None, distinct=False, orderby=None, limit=None, custom={})

Filter

Finds records based on the specific fields and values passed

Parameters
  • fields (dict) – A dictionary of field names to the values they should match

  • raw (bool|list) – Return raw data (dict) for all or a set list of fields

  • distinct (bool) – Only return distinct data

  • orderby (str|str[]) – A field or fields to order the results by

  • limit (int|tuple) – The limit and possible starting point

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

Record[]|dict[]

classmethod generateChanges(old, new)

Generate Changes

Generates the list of changes between two records

Parameters
  • old (dict) – Old record

  • new (dict) – New record

Returns

dict|None

classmethod generateConfig(tree, special='db', db=None)

Generate Config

Generates record specific config based on the Format-OC tree passed

Parameters
  • tree (FormatOC.Tree) – the tree associated with the record type

  • special (str) – The special section used to identify the child info

Returns

dict

abstract classmethod get(_id=None, index=None, filter=None, match=None, raw=None, distinct=False, orderby=None, limit=None, custom={})

Get

Returns records by ID or index, can also be given an extra filter

Parameters
  • _id (mixed|mixed[]) – The ID(s) to fetch from the table

  • index (str) – Index to use instead of primary key

  • filter (dict) – Additional filter

  • match (tuple) – Name/Match filter

  • raw (bool|list) – Return raw data (dict) for all or a set list of fields

  • distinct (bool) – Only return distinct data

  • orderby (str|str[]) – A field or fields to order the results by

  • limit (int|tuple) – The limit and possible starting point

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

Record|Record[]|dict|dict[]

abstract classmethod getChanges(_id, orderby=None, custom={})

Get Changes

Returns the changes record associated with the primary record and table. Used by Record types that have the ‘changes’ flag set

Parameters
  • _id (mixed) – The of the primary record to fetch changes for

  • orderby (str|str[]) – A field or fields to order the results by

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

dict

record(fields=False)

Record

Returns the record data as a dict

Parameters

fields (list) – Optional list of fields to return, if not set, returns all fields

Returns

dict

abstract classmethod remove(_id, array, index, custom={})

Remove

Removes an item from a given array/list for a specific record

Parameters
  • _id (mixed) – The ID of the record to remove from

  • array (str) – The name of the field with the array

  • index (uint) – The index of the array to remove

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

abstract save(replace=False, changes=None)

Save

Updates the record in the DB and returns true if anything has changed, or a new revision number of the record is revisionable

Parameters
  • replace (bool) – If true, replace all fields instead of updating

  • changes (dict) – Data needed to store a change record, is dependant on the ‘changes’ config value

Returns

bool|str

classmethod struct(custom={})

Struct

Returns structure info for the record based on the DB and Tree

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

dict

abstract classmethod tableCreate(custom={})

Table Create

Creates the record’s table/collection/etc in the DB

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

abstract classmethod tableDrop(custom={})

Table Drop

Deletes the record’s table/collection/etc in the DB

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

classmethod tableName()

Table Name

Returns the name for the given record child class

Returns

str

abstract classmethod updateField(field, value, _id=None, index=None, filter=None)

Updated Field

Updates a specific field to the value for an ID, many IDs, or the entire table. Returns the number of records altered

Parameters
  • field (str) – The name of the field to update

  • value (mixed) – The value to set the field to

  • _id (mixed) – Optional ID(s) to filter by

  • index (str) – Optional name of the index to use instead of primary

  • filter (dict) – Optional filter list to decide what records get updated

Returns

uint

abstract classmethod uuid()

UUID

Returns a universal unique ID

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

str

exception RestOC.Record_Base.RevisionException

Bases: Exception

Revision Exception

Raised if a record can not be updated do to Revision failure

RestOC.Record_Base.dbPrepend(pre=None)

DB Prepend

Gets or sets the global prefix for all DBs, useful for testing/development

Parameters

pre (str) – The prefix to store

Returns

str|None

RestOC.Record_Base.getType(type_)

Get Type

Returns the module used to work with the specific type

Parameters

type_ (str) – The name of the type of DB

Returns

module

RestOC.Record_Base.registerType(type_, module_)

Register Type

Sets the class instance used for a specific DB type

Parameters
  • type_ (str) – The name used for the type of DB

  • module_ (module) – The module being registered

Throws:

ValueError

Returns

None