You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.4 KiB
JavaScript
98 lines
3.4 KiB
JavaScript
2 years ago
|
"use strict";
|
||
|
|
||
|
function createSimpleNode(type) {
|
||
|
return { type: type };
|
||
|
}
|
||
|
|
||
|
function createSetAttributeNode(attribute, value) {
|
||
|
return {
|
||
|
type: "setAttribute",
|
||
|
attribute: attribute,
|
||
|
value: value
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function createSetTypeNode(type) {
|
||
|
return {
|
||
|
type: "setFieldType",
|
||
|
fieldType: type
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
// Field types
|
||
|
boolean: createSetTypeNode("boolean"),
|
||
|
integer: createSetTypeNode("integer"),
|
||
|
decimal: createSetTypeNode("decimal"),
|
||
|
string: createSetTypeNode("string"),
|
||
|
bytes: createSetTypeNode("bytes"),
|
||
|
date: createSetTypeNode("date"),
|
||
|
duration: createSetTypeNode("duration"),
|
||
|
// Optionality
|
||
|
required: createSetAttributeNode("required", true),
|
||
|
optional: createSetAttributeNode("required", false),
|
||
|
// Field attributes (numeric)
|
||
|
signed: createSetAttributeNode("signed", true),
|
||
|
unsigned: createSetAttributeNode("signed", false),
|
||
|
precision: function (digits) {
|
||
|
// For decimals only
|
||
|
return { type: "setAttribute", attribute: "precision", value: digits };
|
||
|
},
|
||
|
// Field attributes (dates)
|
||
|
withTimezone: createSetAttributeNode("withTimezone", true),
|
||
|
withoutTimezone: createSetAttributeNode("withTimezone", false),
|
||
|
// Defaults
|
||
|
defaultTo: function (valueOrFunction) {
|
||
|
return { type: "defaultTo", valueOrFunction: valueOrFunction };
|
||
|
},
|
||
|
defaultExistingTo: function (valueOrFunction) {
|
||
|
// This is only used for migrating existing records, eg. when a new field is added to the collection
|
||
|
return { type: "defaultExistingTo", valueOrFunction: valueOrFunction };
|
||
|
},
|
||
|
// Migration value transforms
|
||
|
transformTo: function (transformerFunction) {
|
||
|
// Mandatory when a field's schema changes in one or more ways that cannot be losslessly applied
|
||
|
return { type: "transformTo", transformer: transformerFunction };
|
||
|
},
|
||
|
rollbackTo: function (rollbackTransformerFunction) {
|
||
|
// Mandatory when a field's schema changes in one or more ways that make a lossless *rollback* impossible - alternatively, unsafeForbidRollback may also be used at the cost of making a rollback impossible (even in hot reloading mode!)
|
||
|
return { type: "rollbackTo", transformer: rollbackTransformerFunction };
|
||
|
},
|
||
|
unsafeForbidRollback: createSimpleNode("forbidRollback"),
|
||
|
// Schema operations (collections)
|
||
|
addCollection: function (name, operations) {
|
||
|
return { type: "addCollection", name: name, operations: operations };
|
||
|
},
|
||
|
modifyCollection: function (name, operations) {
|
||
|
return { type: "modifyCollection", name: name, operations: operations };
|
||
|
},
|
||
|
deleteCollection: function (name) {
|
||
|
return { type: "deleteCollection", name: name };
|
||
|
},
|
||
|
renameCollection: function (oldName, newName) {
|
||
|
// TODO: Figure out how to approach this. Maybe use a unique ID for every collection instead, and have a separate mapping from collection names to collection IDs?
|
||
|
return { type: "renameCollection", oldName: oldName, newName: newName };
|
||
|
},
|
||
|
// Schema operations (fields)
|
||
|
addFields: function (fields) {
|
||
|
return { type: "addFields", fields: fields };
|
||
|
},
|
||
|
modifyFields: function (fields) {
|
||
|
return { type: "modifyFields", fields: fields };
|
||
|
},
|
||
|
deleteField: function (name) {
|
||
|
return { type: "deleteField", name: name };
|
||
|
},
|
||
|
renameField: function (oldName, newName) {
|
||
|
return { type: "renameField", oldName: oldName, newName: newName };
|
||
|
},
|
||
|
// Schema operations (indexes)
|
||
|
addIndex: function (fieldName) {
|
||
|
// TODO: Determine an API for composite indexes
|
||
|
return { type: "addIndex", fieldName: fieldName };
|
||
|
},
|
||
|
removeIndex: function (fieldName) {
|
||
|
return { type: "removeIndex", fieldName: fieldName };
|
||
|
},
|
||
|
};
|