Creates a Rekord object given a set of options. A Rekord object is also the constructor for creating instances of the Rekord object defined.
Classes
- Collection
- Database
- Eventful
- Eventful$
- FilteredCollection
- FilteredModelCollection
- Map
- Model
- ModelCollection
- Page
- RelationCollection
- Search
Members
-
staticRekord.fileProcessors
-
files: { field: { type: 'text', // base64, dataURL, resource processor: 'processor_name', capacity: 1024 * 1024, // maximum bytes types: ['image/png', 'image/jpg', 'image/gif'], // acceptable MIME types autoSave: true, store: true, save: true } }
Methods
-
staticRekord.addEventful(target, secret)
-
Adds functions to the given object (or prototype) so you can listen for any number of events on the given object, optionally once. Listeners can be removed later.
The following methods will be added to the given target:
target.on( events, callback, [context] ) target.once( events, callback, [context] ) target.after( events, callback, [context] ) target.off( events, callback ) target.trigger( events, [a, b, c...] )
Where...
events
is a string of space delimited events.callback
is a function to invoke when the event is triggered.context
is an object that should be thethis
when the callback is invoked. If no context is given the default value is the object which has the trigger function that was invoked.
Name Type Default Description target
Object optional The object to add
on
,once
,off
, andtrigger
functions to.secret
Boolean false optional If true - the functions will be prefixed with
$
. -
staticRekord.bind(context, The){function}
-
Returns the given function with the given context (
this
). This also has the benefits of returning a "copy" of the function which makes it ideal for use in listening on/once events and off events.var context = {}; var func = Rekord.bind( context, function(x) { this.y = x * 2; }); func( 4 ); context.y; // 8
Name Type Description context
Object The value of
this
for the given function.The
function function to invoke with the given context.
Returns:
Type Description function - A new function which is a copy of the given function with a new context. -
staticRekord.collect(a){Rekord.Collection}
-
Returns an instance of
Rekord.Collection
with the initial values passed as arguments to this function.Rekord.collect(1, 2, 3, 4); Rekord.collect([1, 2, 3, 4]); // same as above Rekord.collect(); Rekord.collect([]); // same as above
Name Type Description a
Array.<Any> | Any The initial values in the collection. You can pass an array of values or any number of arguments.
Returns:
Type Description Rekord.Collection - A newly created instance containing the given values. -
staticRekord.createComparator(comparator, nullsFirst){comparisonCallback}
-
Creates a function which compares two values.
Name Type Default Description comparator
comparatorInput The input which creates a comparison function.
nullsFirst
Boolean false optional True if null values should be sorted first.
Returns:
Type Description comparisonCallback -
staticRekord.createPropertyResolver(properties, delim){propertyResolverCallback}
-
Creates a function which resolves a value from another value given an expression. This is often used to get a property value of an object.
// x = {age: 6, name: 'tom', user: {first: 'jack'}} createPropertyResolver()( x ) // x createPropertyResolver( 'age' )( x ) // 6 createPropertyResolver( 'user.first' )( x ) // 'jack' createPropertyResolver( '{name} & {user.first}')( x ) // 'tom & jack' createPropertyResolver( ['name', 'age'] )( x ) // 'tom,6' createPropertyResolver( ['name', 'age'], ' is ' )( x ) // 'tom is 6' createPropertyResolver( {age:null, user:'first'})( x ) // '6,jack'
Name Type Default Description properties
propertyResolverInput optional The expression which converts one value into another.
delim
String ',' optional A delimiter to use to join multiple properties into a string.
Returns:
Type Description propertyResolverCallback - A function to take values and resolve new ones. -
staticRekord.createWhere(properties, value, equals){whereCallback}
-
Creates a function which returns a true or false value given a test value. This is also known as a filter function.
Rekord.createWhere('field', true); // when an object has property where field=true Rekord.createWhere('field'); // when an object has the property named field Rekord.createWhere(function(){}); // a function can be given which is immediately returned Rekord.createWhere(['field', function(){}, ['field', true]]); // when an object meets all of the above criteria Rekord.createWhere({foo: 1, bar: 2}); // when an object has foo=1 and bar=2 Rekord.createWhere('field', true, myEquals); // A custom comparison function can be given. Rekord.createWhere(); // always returns true
Name Type Default Description properties
whereInput optional The first expression used to generate a filter function.
value
Any optional When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.
equals
equalityCallback Rekord.equalsStrict optional An alternative function can be used to compare to values.
- See:
Returns:
Type Description whereCallback - A function which takes a value (typically an object) and returns a true or false value. -
staticRekord.indexOf(arr, x, comparator){Number|Boolean}
-
Finds the index of a variable in an array optionally using a custom comparison function. If the variable is not found in the array then
false
is returned.Rekord.indexOf([1, 2, 3], 1); // 0 Rekord.indexOf([1, 2, 3], 4); // false Rekord.indexOf([1, 2, 2], 2); // 1
Name Type Description arr
Array The array to search through.
x
Any The variable to search for.
comparator
function optional The function to use which compares two values and returns a truthy value if they are considered equivalent. If a comparator is not given then strict comparison is used to determine equivalence.
Returns:
Type Description Number | Boolean - The index in the array the variable exists at, otherwise false if the variable wasn't found in the array. -
staticRekord.isArray(x){Boolean}
-
Determines whether the given variable is an instance of Array.
Rekord.isArray(); // false Rekord.isArray('x'): // false Rekord.isArray(1); // false Rekord.isArray([]); // true Rekord.isArray(Rekord.collect(1, 2, 3)); // true
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is an instance of Array, otherwise false. -
staticRekord.isBoolean(x){Boolean}
-
Determines whether the given variable is a boolean value.
Rekord.isBoolean(); // false Rekord.isBoolean('x'): // false Rekord.isBoolean(1); // false Rekord.isBoolean(true); // true
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is a boolean value, otherwise false. -
staticRekord.isDate(x){Boolean}
-
Determines whether the given variable is an instance of Date.
Rekord.isDate(); // false Rekord.isDate('x'): // false Rekord.isDate(1); // false Rekord.isDate(true); // false Rekord.isDate(new Date()); // true
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is an instance of Date, otherwise false. -
staticRekord.isDefined(x){Boolean}
-
Determines whether the given variable is defined.
Rekord.isDefined(); // false Rekord.isDefined(0); // true Rekord.isDefined(true); // true Rekord.isDefined(void 0); // false Rekord.isDefined(undefined); // false
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is defined, otherwise false. -
staticRekord.isFunction(x){Boolean}
-
Determines whether the given variable is a function.
Rekord.isFunction(); // false Rekord.isFunction(parseInt); // true Rekord.isFunction(2); // false
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is a function, otherwise false. -
staticRekord.isNumber(x){Boolean}
-
Determines whether the given variable is a valid number. NaN and Infinity are not valid numbers.
Rekord.isNumber(); // false Rekord.isNumber('x'): // false Rekord.isNumber(1); // true Rekord.isNumber(NaN); // false Rekord.isNumber(Infinity); // true
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is a valid number, otherwise false. -
staticRekord.isObject(x){Boolean}
-
Determines whether the given variable is a non-null object. As a note, Arrays are considered objects.
Rekord.isObject(); // false Rekord.isObject('x'): // false Rekord.isObject(1); // false Rekord.isObject([]); // true Rekord.isObject({}); // true Rekord.isObject(null); // false
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is a non-null object, otherwise false. -
staticRekord.isRegExp(x){Boolean}
-
Determines whether the given variable is an instance of RegExp.
Rekord.isRegExp(); // false Rekord.isRegExp('x'): // false Rekord.isRegExp(1); // false Rekord.isRegExp(true); // false Rekord.isRegExp(/[xyz]/); // true
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is an instance of RegExp, otherwise false. -
staticRekord.isRekord(x){Boolean}
-
Determines whether the given variable is a Rekord object. A Rekord object is a constructor for a model and also has a Database variable. A Rekord object is strictly created by the Rekord function.
var Task = Rekord({ name: 'task', fields: ['name', 'done', 'finished_at', 'created_at', 'assigned_to'] }); Rekord.isRekord( Task ); // true
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is a Rekord object, otherwise false. -
staticRekord.isString(x){Boolean}
-
Determines whether the given variable is a string.
Rekord.isString(); // false Rekord.isString('x'): // true Rekord.isString(1); // false
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is a string, otherwise false. -
staticRekord.isValue(x){Boolean}
-
Determines whether the given variable is not null and is not undefined.
Rekord.isValue(); // false Rekord.isValue('x'): // true Rekord.isValue(1); // true Rekord.isValue([]); // true Rekord.isValue({}); // true Rekord.isValue(null); // false Rekord.isValue(void 0); // false Rekord.isValue(undefined); // false
Name Type Description x
Any The variable to test.
Returns:
Type Description Boolean - True if the variable is non-null and not undefined. -
The factory responsible for creating a service which publishes operations and receives operations that have occurred. The first argument is a reference to the Database and the second argument is a function to invoke when a live operation occurs. This function must return a function that can be passed an operation to be delegated to other clients.
Name Type Description database
Database The database this live function is for.
Returns:
Type Description function - The function which sends operations. -
staticRekord.noop()
-
A function that doesn't perform any operations.
-
staticRekord.propsMatch(test, testFields, expected, expectedFields, equals){Boolean}
-
Determines whether the properties on one object equals the properties on another object.
Name Type Description test
Object The object to test for matching.
testFields
String | Array.<String> The property name or array of properties to test for equality on
test
.expected
Object The object with the expected values.
expectedFields
String | Array.<String> The property name or array of properties to test for equality on
expected
.equals
equalityCallback optional The equality function which compares two values and returns whether they are considered equivalent.
Returns:
Type Description Boolean - True if the testFields
properties ontest
are equivalent to theexpectedFields
onexpected
according to theequals
function. -
staticRekord.saveWhere(name, properties, value, equals)
-
Saves a function created with
Rekord.createWhere
to a cache of filter functions which can be created more quickly in subsequent calls. It's advised to make use of saved where's even in simpler scenarios for several reasons:- You can name a comparison which is self documenting
- When refactoring, you only need to modify a single place in the code
- It's slightly more efficient (time & memory) to cache filter functions
Rekord.saveWhere('whereName', 'field', true); Rekord.createWhere('whereName'); // returns the same function except quicker
Name Type Default Description name
String The name of the filter function to save for later use.
properties
String | Object | Array | whereCallback optional value
Any optional equals
equalityCallback Rekord.equalsStrict optional - See:
-
Sets the debug implementation provided the factory function. This function can only be called once - all subsequent calls will be ignored unless
overwrite
is given as a truthy value.Name Type Default Description factory
function The factory which provides debug implementations.
overwrite
Boolean false optional True if existing implementations are to be ignored and the given factory should be the implementation.
-
Sets the live implementation provided the factory function. This function can only be called once - all subsequent calls will be ignored unless
overwrite
is given as a truthy value.Name Type Default Description factory
function The factory which provides live implementations.
overwrite
Boolean false optional True if existing implementations are to be ignored and the given factory should be the implementation.
-
Sets the rest implementation provided the factory function. This function can only be called once - all subsequent calls will be ignored unless
overwrite
is given as a truthy value.Name Type Default Description factory
function The factory which provides rest implementations.
overwrite
Boolean false optional True if existing implementations are to be ignored and the given factory should be the implementation.
-
Sets the store implementation provided the factory function. This function can only be called once - all subsequent calls will be ignored unless
overwrite
is given as a truthy value.Name Type Default Description factory
function The factory which provides store implementations.
overwrite
Boolean false optional True if existing implementations are to be ignored and the given factory should be the implementation.
-
A factory function for returning an object capable of storing objects for retrieval later by the application.
Name Type Description database
Database The database this store is for.
Returns:
Type Description Object - An object with put, remove, and all functions. -
staticRekord.toArray(x, delimiter){Array.<String>}
-
Converts the given variable to an array of strings. If the variable is a string it is split based on the delimiter given. If the variable is an array then it is returned. If the variable is any other type it may result in an error.
Rekord.toArray([1, 2, 3]); // [1, 2, 3] Rekord.toArray('1,2,3', ','); // ['1', '2', '3'] Rekord.toArray(1); // [1] Rekord.toArray(null); // []
Name Type Description x
String | Array.<String> The variable to convert to an Array.
delimiter
String optional The delimiter to split if the given variable is a string.
Returns:
Type Description Array.<String> - The array of strings created. -
staticRekord.uuid(){String}
-
Generates a UUID using the random number method.
Returns:
Type Description String - The generated UUID.