shelving 1.40.0 → 1.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db/Database.d.ts +14 -16
- package/db/Database.js +37 -49
- package/db/Operation.d.ts +82 -0
- package/db/Operation.js +132 -0
- package/db/index.d.ts +1 -1
- package/db/index.js +1 -1
- package/package.json +6 -6
- package/react/useDocument.js +2 -2
- package/react/useQuery.js +3 -3
- package/update/ArrayUpdate.d.ts +4 -4
- package/update/ArrayUpdate.js +4 -4
- package/update/DataUpdate.d.ts +1 -1
- package/update/DataUpdate.js +1 -1
- package/update/ObjectUpdate.d.ts +4 -4
- package/update/ObjectUpdate.js +4 -4
- package/util/async.d.ts +28 -4
- package/util/async.js +36 -3
- package/util/date.d.ts +49 -18
- package/util/date.js +92 -85
- package/util/number.d.ts +56 -9
- package/util/number.js +76 -13
- package/util/string.d.ts +6 -0
- package/util/string.js +6 -0
- package/util/units.d.ts +37 -9
- package/util/units.js +60 -89
- package/db/Write.d.ts +0 -50
- package/db/Write.js +0 -73
package/util/number.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { AssertionError } from "../error/index.js";
|
|
2
|
+
import { NBSP, NNBSP } from "./string.js";
|
|
3
|
+
// Constants.
|
|
4
|
+
export const TRILLION = 1000000000000;
|
|
5
|
+
export const BILLION = 1000000000;
|
|
6
|
+
export const MILLION = 1000000;
|
|
2
7
|
/** Is a value a number? */
|
|
3
8
|
export const isNumber = (v) => typeof v === "number";
|
|
4
9
|
/**
|
|
@@ -36,29 +41,87 @@ export function getNumber(value) {
|
|
|
36
41
|
*
|
|
37
42
|
* @param num The number to round.
|
|
38
43
|
* @param step The rounding to round to, e.g. `2` or `0.1` (defaults to `1`, i.e. round numbers).
|
|
39
|
-
*
|
|
44
|
+
*
|
|
45
|
+
* @returns The number rounded to the specified step.
|
|
40
46
|
*/
|
|
41
|
-
export
|
|
42
|
-
if (step < 0.00001)
|
|
43
|
-
throw new AssertionError("roundToStep() does not work accurately with steps smaller than 0.00001", step);
|
|
44
|
-
return Math.round(num / step) * step;
|
|
45
|
-
}
|
|
47
|
+
export const roundStep = (num, step = 1) => Math.round(num / step) * step;
|
|
46
48
|
/**
|
|
47
49
|
* Round a number to a specified set of decimal places.
|
|
48
|
-
* -
|
|
50
|
+
* - Better than `Math.round()` because it allows a `precision` argument.
|
|
51
|
+
* - Better than `num.toFixed()` because it trims excess `0` zeroes.
|
|
49
52
|
*
|
|
50
|
-
* @param num The number to
|
|
51
|
-
* @param precision Maximum of digits shown after the decimal point (defaults to 10)
|
|
52
|
-
*
|
|
53
|
+
* @param num The number to round.
|
|
54
|
+
* @param precision Maximum number of digits shown after the decimal point (defaults to 10).
|
|
55
|
+
*
|
|
56
|
+
* @returns The number rounded to the specified precision.
|
|
53
57
|
*/
|
|
54
|
-
export const roundNumber = (num, precision =
|
|
58
|
+
export const roundNumber = (num, precision = 0) => Math.round(num * 10 ** precision) / 10 ** precision;
|
|
59
|
+
/**
|
|
60
|
+
* Truncate a number to a specified set of decimal places.
|
|
61
|
+
* - Better than `Math.trunc()` because it allows a `precision` argument.
|
|
62
|
+
*
|
|
63
|
+
* @param num The number to truncate.
|
|
64
|
+
* @param precision Maximum number of digits shown after the decimal point (defaults to 10).
|
|
65
|
+
*
|
|
66
|
+
* @returns The number truncated to the specified precision.
|
|
67
|
+
*/
|
|
68
|
+
export const truncateNumber = (num, precision = 0) => Math.trunc(num * 10 ** precision) / 10 ** precision;
|
|
55
69
|
/**
|
|
56
70
|
* Format a number (based on the user's browser settings).
|
|
71
|
+
*
|
|
57
72
|
* @param num The number to format.
|
|
58
|
-
* @param
|
|
73
|
+
* @param maxPrecision Maximum number of digits shown after the decimal point (defaults to 2).
|
|
74
|
+
* @param minPrecision Minimum number of digits shown after the decimal point (defaults to 0).
|
|
75
|
+
*
|
|
59
76
|
* @returns The number formatted as a string in the browser's current locale.
|
|
60
77
|
*/
|
|
61
|
-
export const formatNumber = (num,
|
|
78
|
+
export const formatNumber = (num, maxPrecision = 4, minPrecision = 0) => new Intl.NumberFormat(undefined, { maximumFractionDigits: maxPrecision, minimumFractionDigits: minPrecision }).format(num);
|
|
79
|
+
/** Format a number with a short suffix (number and suffix are separated by a non-breaking narrow space). */
|
|
80
|
+
export const formatQuantity = (num, suffix, maxPrecision, minPrecision) => `${formatNumber(num, maxPrecision, minPrecision)}${NNBSP}${suffix}`;
|
|
81
|
+
/** Format a number with a longer full-word suffix (number and suffix are separated by a non-breaking space). */
|
|
82
|
+
export function formatFullQuantity(num, singular, plural, maxPrecision, minPrecision) {
|
|
83
|
+
const qty = formatNumber(num, maxPrecision, minPrecision);
|
|
84
|
+
return `${qty}${NBSP}${qty === "1" ? singular : plural}`;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Cram a large whole numbers into a space efficient format, e.g. `14.7M`
|
|
88
|
+
* - Improves glanceability.
|
|
89
|
+
* - Keeps number of characters under five if possible.
|
|
90
|
+
*
|
|
91
|
+
* - Numbers over 100 trillion: `157T`
|
|
92
|
+
* - Numbers over 10 trillion: `15.7T` (includes zero e.g. `40.0T` for consistency).
|
|
93
|
+
* - Numbers over 1 trillion: `1.57T` (includes zeros e.g. `4.00T` for consistency).
|
|
94
|
+
* - Numbers over 100 billion: `157B`
|
|
95
|
+
* - Numbers over 10 billion: `15.7B` (includes zero e.g. `40.0B` for consistency).
|
|
96
|
+
* - Numbers over 1 billion: `1.57B` (includes zeros e.g. `4.00B` for consistency).
|
|
97
|
+
* - Numbers over 100 million: `157M`
|
|
98
|
+
* - Numbers over 10 million: `15.7M` (includes zero e.g. `40.0M` for consistency).
|
|
99
|
+
* - Numbers over 1 million: `1.57M` (includes zeros e.g. `4.00M` for consistency).
|
|
100
|
+
* - Numbers over 100,000: `157K`
|
|
101
|
+
* - Numbers over 10,000: `15.7K` (includes zero e.g. `14.0K` for consistency).
|
|
102
|
+
* - Smaller numbers: `1570` and `157` and `15.7` and `1.6`
|
|
103
|
+
*
|
|
104
|
+
* @param num The number to format.
|
|
105
|
+
* @param precision Maximum number of digits shown after the decimal point (defaults to 10, only used for numbers under 10,000).
|
|
106
|
+
*
|
|
107
|
+
* @returns The number formatted as a crammed string.
|
|
108
|
+
*/
|
|
109
|
+
export function cramNumber(num) {
|
|
110
|
+
const abs = Math.abs(num);
|
|
111
|
+
if (abs >= TRILLION)
|
|
112
|
+
return `${_significance(num / TRILLION)}T`;
|
|
113
|
+
if (abs >= BILLION)
|
|
114
|
+
return `${_significance(num / BILLION)}B`;
|
|
115
|
+
if (abs >= MILLION)
|
|
116
|
+
return `${_significance(num / MILLION)}M`;
|
|
117
|
+
if (abs >= 10000)
|
|
118
|
+
return `${_significance(num / 1000)}K`;
|
|
119
|
+
return truncateNumber(num, 2).toString();
|
|
120
|
+
}
|
|
121
|
+
function _significance(num) {
|
|
122
|
+
const digits = num >= 100 ? 0 : num >= 10 ? 1 : 2;
|
|
123
|
+
return truncateNumber(num, digits).toFixed(digits);
|
|
124
|
+
}
|
|
62
125
|
/**
|
|
63
126
|
* Is a number within a specified range?
|
|
64
127
|
*
|
package/util/string.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { ImmutableArray } from "./array.js";
|
|
2
|
+
/** Non-breaking space. */
|
|
3
|
+
export declare const NBSP = "\u00A0";
|
|
4
|
+
/** Thin space. */
|
|
5
|
+
export declare const THINSP = "\u2009";
|
|
6
|
+
/** Non-breaking narrow space. */
|
|
7
|
+
export declare const NNBSP = "\u202F";
|
|
2
8
|
/** Is a value a string? */
|
|
3
9
|
export declare const isString: (v: unknown) => v is string;
|
|
4
10
|
/**
|
package/util/string.js
CHANGED
|
@@ -3,6 +3,12 @@ import { formatDate } from "./date.js";
|
|
|
3
3
|
import { isData } from "./data.js";
|
|
4
4
|
import { isArray } from "./array.js";
|
|
5
5
|
import { formatNumber, isBetween } from "./number.js";
|
|
6
|
+
/** Non-breaking space. */
|
|
7
|
+
export const NBSP = "\xA0";
|
|
8
|
+
/** Thin space. */
|
|
9
|
+
export const THINSP = "\u2009";
|
|
10
|
+
/** Non-breaking narrow space. */
|
|
11
|
+
export const NNBSP = "\u202F";
|
|
6
12
|
/** Is a value a string? */
|
|
7
13
|
export const isString = (v) => typeof v === "string";
|
|
8
14
|
/**
|
package/util/units.d.ts
CHANGED
|
@@ -1,11 +1,39 @@
|
|
|
1
|
+
/** Valid information about a unit of measure. */
|
|
2
|
+
export declare type UnitData = {
|
|
3
|
+
/** Plural name for a unit, e.g. `feet` */
|
|
4
|
+
readonly plural?: string;
|
|
5
|
+
/** Type of a unit. */
|
|
6
|
+
readonly type: UnitType;
|
|
7
|
+
/** Short suffix for this unit, e.g. `km` */
|
|
8
|
+
readonly suffix: string;
|
|
9
|
+
/** All units must specify their 'base' unit, e.g. `meter` for for distance units and `liter` for volume units. */
|
|
10
|
+
readonly base: number;
|
|
11
|
+
} & {
|
|
12
|
+
[K in UnitReference]?: number;
|
|
13
|
+
};
|
|
14
|
+
/** Valid system of measurement reference. */
|
|
15
|
+
export declare type UnitType = "percentage" | "angle" | "temperature" | "length" | "speed" | "pace" | "mass" | "time" | "volume";
|
|
16
|
+
/** Valid unit of measurement reference (correspond to units allowed in `Intl.NumberFormat`, but not all). */
|
|
17
|
+
export declare type UnitReference = "percent" | "degree" | "millimeter" | "centimeter" | "meter" | "kilometer" | "mile" | "yard" | "foot" | "inch" | "liter" | "milliliter" | "gallon" | "fluid-ounce" | "gram" | "kilogram" | "pound" | "stone" | "ounce" | "millisecond" | "second" | "minute" | "day" | "hour" | "week" | "month" | "year";
|
|
18
|
+
/** List of units. */
|
|
19
|
+
export declare const UNITS: {
|
|
20
|
+
[K in UnitReference]: UnitData;
|
|
21
|
+
};
|
|
22
|
+
/** Convert between two units of the same type. */
|
|
23
|
+
export declare function convertUnits(num: number, from: UnitReference, to: UnitReference): number;
|
|
1
24
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
25
|
+
* Format a number with a given unit of measure, e.g. `12 kg` or `29.5 l`
|
|
26
|
+
*
|
|
27
|
+
* @param num The number to format.
|
|
28
|
+
* @param unit String reference for a unit of measure e.g. `kilometer`
|
|
29
|
+
* @param maxPrecision Number of decimal places to round the number to e.g. `2`
|
|
4
30
|
*/
|
|
5
|
-
export declare
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
31
|
+
export declare const formatUnits: (num: number, unit: UnitReference, maxPrecision?: number | undefined, minPrecision?: number | undefined) => string;
|
|
32
|
+
/**
|
|
33
|
+
* Format a number with a given unit of measure, e.g. `12 kilograms` or `29.5 liters` or `1 degree`
|
|
34
|
+
*
|
|
35
|
+
* @param num The number to format.
|
|
36
|
+
* @param unit String reference for a unit of measure e.g. `kilometer`
|
|
37
|
+
* @param maxPrecision Number of decimal places to round the number to e.g. `2`
|
|
38
|
+
*/
|
|
39
|
+
export declare const formatFullUnits: (num: number, unit: UnitReference, maxPrecision?: number | undefined, minPrecision?: number | undefined) => string;
|
package/util/units.js
CHANGED
|
@@ -1,92 +1,63 @@
|
|
|
1
|
-
import {
|
|
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
|
-
inch: 63360,
|
|
35
|
-
regex: /(\d|\b)(miles?|mi|m)\b/i,
|
|
36
|
-
short: "mi",
|
|
37
|
-
precision: 2,
|
|
38
|
-
m: "mile", // e.g. `10m` means miles.
|
|
39
|
-
},
|
|
40
|
-
yard: {
|
|
41
|
-
meter: 0.9144,
|
|
42
|
-
inch: 36,
|
|
43
|
-
foot: 3,
|
|
44
|
-
regex: /(\d|\b)(yards?|yds?|y)\b/i,
|
|
45
|
-
short: "yd",
|
|
46
|
-
},
|
|
47
|
-
foot: {
|
|
48
|
-
meter: 0.3048,
|
|
49
|
-
inch: 12,
|
|
50
|
-
regex: /(\d|\b)(feets?|foots?|fts?|f)\b/i,
|
|
51
|
-
short: "ft",
|
|
52
|
-
},
|
|
53
|
-
inch: {
|
|
54
|
-
meter: 0.0254,
|
|
55
|
-
regex: /(\d|\b)(inches|inch|in)\b/i,
|
|
56
|
-
short: "in",
|
|
57
|
-
},
|
|
1
|
+
import { AssertionError } from "../error/index.js";
|
|
2
|
+
import { DAY, HOUR, MINUTE, MONTH, SECOND, WEEK, YEAR } from "./date.js";
|
|
3
|
+
import { formatFullQuantity, formatQuantity } from "./number.js";
|
|
4
|
+
import { NNBSP } from "./string.js";
|
|
5
|
+
/** List of units. */
|
|
6
|
+
export const UNITS = {
|
|
7
|
+
"percent": { type: "percentage", base: 1, suffix: "%" },
|
|
8
|
+
"degree": { type: "angle", base: 1, suffix: "deg" },
|
|
9
|
+
"millimeter": { type: "length", base: 0.001, suffix: "mm" },
|
|
10
|
+
"centimeter": { type: "length", base: 0.01, suffix: "cm" },
|
|
11
|
+
"meter": { type: "length", base: 1, centimeter: 100, millimeter: 1000, suffix: "m" },
|
|
12
|
+
"kilometer": { type: "length", base: 1000, centimeter: 100000, millimeter: 1000000, suffix: "km" },
|
|
13
|
+
"inch": { type: "length", base: 0.0254, suffix: "in" },
|
|
14
|
+
"foot": { type: "length", base: 0.3048, inch: 12, suffix: "ft", plural: "feet" },
|
|
15
|
+
"yard": { type: "length", base: 0.9144, inch: 36, foot: 3, suffix: "yd" },
|
|
16
|
+
"mile": { type: "length", base: 1609.344, yard: 1760, foot: 5280, inch: 63360, suffix: "mi" },
|
|
17
|
+
"milliliter": { type: "volume", base: 1, suffix: "ml" },
|
|
18
|
+
"liter": { type: "volume", base: 1000, suffix: "l" },
|
|
19
|
+
"fluid-ounce": { type: "volume", base: 29.5735295625, gallon: 128, suffix: `fl${NNBSP}oz` },
|
|
20
|
+
"gallon": { type: "volume", base: 3785.411784, suffix: "gal" },
|
|
21
|
+
"gram": { type: "mass", base: 1, suffix: "g" },
|
|
22
|
+
"kilogram": { type: "mass", base: 1000, suffix: "kg" },
|
|
23
|
+
"ounce": { type: "mass", base: 28.349523125, pound: 0.0625, suffix: "oz" },
|
|
24
|
+
"pound": { type: "mass", base: 453.59237, ounce: 16, suffix: "lb" },
|
|
25
|
+
"stone": { type: "mass", base: 6350.29318, pound: 14, ounce: 224, suffix: "st", plural: "stone" },
|
|
26
|
+
"millisecond": { type: "time", base: 1, suffix: "ms" },
|
|
27
|
+
"second": { type: "time", base: SECOND, suffix: "s" },
|
|
28
|
+
"minute": { type: "time", base: MINUTE, suffix: "m" },
|
|
29
|
+
"hour": { type: "time", base: HOUR, suffix: "h" },
|
|
30
|
+
"day": { type: "time", base: DAY, suffix: "d" },
|
|
31
|
+
"week": { type: "time", base: WEEK, suffix: "w" },
|
|
32
|
+
"month": { type: "time", base: MONTH, suffix: "m" },
|
|
33
|
+
"year": { type: "time", base: YEAR, suffix: "y" },
|
|
58
34
|
};
|
|
59
|
-
/** Convert between two
|
|
60
|
-
export
|
|
35
|
+
/** Convert between two units of the same type. */
|
|
36
|
+
export function convertUnits(num, from, to) {
|
|
61
37
|
if (from === to)
|
|
62
38
|
return num;
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
if (
|
|
66
|
-
return num *
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return "inch";
|
|
89
|
-
return false; // Cannot figure out unit.
|
|
90
|
-
};
|
|
91
|
-
/** Format a distance. */
|
|
92
|
-
export const formatUnit = (num, unit = "meter", precision = unitsData[unit].precision || 0) => `${formatNumber(num, precision)} ${unitsData[unit].short}`;
|
|
39
|
+
const fromData = UNITS[from];
|
|
40
|
+
const exact = fromData[to]; // Get the exact conversion if possible (e.g. 5280 feet in a mile).
|
|
41
|
+
if (typeof exact === "number")
|
|
42
|
+
return num * exact;
|
|
43
|
+
const toData = UNITS[to];
|
|
44
|
+
if (fromData.type !== toData.type)
|
|
45
|
+
throw new AssertionError(`Target unit must be ${fromData.type}`, toData.type);
|
|
46
|
+
return (num * fromData.base) / toData.base;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Format a number with a given unit of measure, e.g. `12 kg` or `29.5 l`
|
|
50
|
+
*
|
|
51
|
+
* @param num The number to format.
|
|
52
|
+
* @param unit String reference for a unit of measure e.g. `kilometer`
|
|
53
|
+
* @param maxPrecision Number of decimal places to round the number to e.g. `2`
|
|
54
|
+
*/
|
|
55
|
+
export const formatUnits = (num, unit, maxPrecision, minPrecision) => formatQuantity(num, UNITS[unit].suffix, maxPrecision, minPrecision);
|
|
56
|
+
/**
|
|
57
|
+
* Format a number with a given unit of measure, e.g. `12 kilograms` or `29.5 liters` or `1 degree`
|
|
58
|
+
*
|
|
59
|
+
* @param num The number to format.
|
|
60
|
+
* @param unit String reference for a unit of measure e.g. `kilometer`
|
|
61
|
+
* @param maxPrecision Number of decimal places to round the number to e.g. `2`
|
|
62
|
+
*/
|
|
63
|
+
export const formatFullUnits = (num, unit, maxPrecision, minPrecision) => formatFullQuantity(num, unit, UNITS[unit].plural || `${unit}s`, maxPrecision, minPrecision);
|
package/db/Write.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { PropUpdates, Update } from "../update/index.js";
|
|
2
|
-
import { ImmutableArray, Data, Transformable, Key, Nullish } from "../util/index.js";
|
|
3
|
-
import type { Database, DatabaseDocument } from "./Database.js";
|
|
4
|
-
/** Represent a write made to a database. */
|
|
5
|
-
export declare abstract class Write implements Transformable<Database, void | PromiseLike<void>> {
|
|
6
|
-
abstract transform(db: Database): void | PromiseLike<void>;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Represent a list of writes made to a database.
|
|
10
|
-
* - Sets of writes are predictable and repeatable, so unpredictable operations like `create()` and query operations are not supported.
|
|
11
|
-
* - Every write must be applied to a specific database document in a specific collection and are applied in the specified order.
|
|
12
|
-
*/
|
|
13
|
-
export declare class Writes extends Write {
|
|
14
|
-
readonly writes: ImmutableArray<Write>;
|
|
15
|
-
constructor(...writes: Write[]);
|
|
16
|
-
transform(db: Database): Promise<void>;
|
|
17
|
-
}
|
|
18
|
-
/** Represent a write made to a single document in a database. */
|
|
19
|
-
export declare abstract class DocumentWrite<T extends Data> extends Write {
|
|
20
|
-
readonly collection: string;
|
|
21
|
-
readonly id: string;
|
|
22
|
-
constructor({ collection, id }: DatabaseDocument<T>);
|
|
23
|
-
}
|
|
24
|
-
/** Represent a set operation made to a single document in a database. */
|
|
25
|
-
export declare class DocumentSet<T extends Data> extends DocumentWrite<T> {
|
|
26
|
-
readonly data: T;
|
|
27
|
-
constructor(ref: DatabaseDocument<T>, data: T);
|
|
28
|
-
transform(db: Database): Promise<void>;
|
|
29
|
-
/** Set one of the props on this set operation to a different value. */
|
|
30
|
-
set<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
31
|
-
}
|
|
32
|
-
/** Represent an update operation made to a single document in a database. */
|
|
33
|
-
export declare class DocumentUpdate<T extends Data> extends DocumentWrite<T> {
|
|
34
|
-
readonly updates: PropUpdates<T>;
|
|
35
|
-
constructor(ref: DatabaseDocument<T>, updates: PropUpdates<T>);
|
|
36
|
-
transform(db: Database): Promise<void>;
|
|
37
|
-
/** update one of the props on this set operation to a different value. */
|
|
38
|
-
update<K extends Key<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
39
|
-
}
|
|
40
|
-
/** Represent a delete operation made to a single document in a database. */
|
|
41
|
-
export declare class DocumentDelete<T extends Data> extends DocumentWrite<T> {
|
|
42
|
-
transform(db: Database): Promise<void>;
|
|
43
|
-
}
|
|
44
|
-
/** Set of hydrations for all change classes. */
|
|
45
|
-
export declare const WRITE_HYDRATIONS: {
|
|
46
|
-
Writes: typeof Writes;
|
|
47
|
-
DocumentSet: typeof DocumentSet;
|
|
48
|
-
DocumentUpdate: typeof DocumentUpdate;
|
|
49
|
-
DocumentDelete: typeof DocumentDelete;
|
|
50
|
-
};
|
package/db/Write.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { transform, isNullish } from "../util/index.js";
|
|
2
|
-
/** Represent a write made to a database. */
|
|
3
|
-
export class Write {
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Represent a list of writes made to a database.
|
|
7
|
-
* - Sets of writes are predictable and repeatable, so unpredictable operations like `create()` and query operations are not supported.
|
|
8
|
-
* - Every write must be applied to a specific database document in a specific collection and are applied in the specified order.
|
|
9
|
-
*/
|
|
10
|
-
export class Writes extends Write {
|
|
11
|
-
constructor(...writes) {
|
|
12
|
-
super();
|
|
13
|
-
this.writes = writes;
|
|
14
|
-
}
|
|
15
|
-
async transform(db) {
|
|
16
|
-
for (const writes of this.writes)
|
|
17
|
-
await transform(db, writes);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
/** Represent a write made to a single document in a database. */
|
|
21
|
-
export class DocumentWrite extends Write {
|
|
22
|
-
constructor({ collection, id }) {
|
|
23
|
-
super();
|
|
24
|
-
this.collection = collection;
|
|
25
|
-
this.id = id;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/** Represent a set operation made to a single document in a database. */
|
|
29
|
-
export class DocumentSet extends DocumentWrite {
|
|
30
|
-
constructor(ref, data) {
|
|
31
|
-
super(ref);
|
|
32
|
-
this.data = data;
|
|
33
|
-
}
|
|
34
|
-
async transform(db) {
|
|
35
|
-
await db.doc(this.collection, this.id).set(this.data);
|
|
36
|
-
}
|
|
37
|
-
/** Set one of the props on this set operation to a different value. */
|
|
38
|
-
set(key, value) {
|
|
39
|
-
if (isNullish(key))
|
|
40
|
-
return this;
|
|
41
|
-
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
/** Represent an update operation made to a single document in a database. */
|
|
45
|
-
export class DocumentUpdate extends DocumentWrite {
|
|
46
|
-
constructor(ref, updates) {
|
|
47
|
-
super(ref);
|
|
48
|
-
this.updates = updates;
|
|
49
|
-
}
|
|
50
|
-
async transform(db) {
|
|
51
|
-
await db.doc(this.collection, this.id).update(this.updates);
|
|
52
|
-
}
|
|
53
|
-
/** update one of the props on this set operation to a different value. */
|
|
54
|
-
update(key, value) {
|
|
55
|
-
if (isNullish(key))
|
|
56
|
-
return this;
|
|
57
|
-
return { __proto__: Object.getPrototypeOf(this), ...this, updates: { ...this.updates, [key]: value } };
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/** Represent a delete operation made to a single document in a database. */
|
|
61
|
-
export class DocumentDelete extends DocumentWrite {
|
|
62
|
-
async transform(db) {
|
|
63
|
-
await db.doc(this.collection, this.id).delete();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/** Set of hydrations for all change classes. */
|
|
67
|
-
export const WRITE_HYDRATIONS = {
|
|
68
|
-
Writes,
|
|
69
|
-
DocumentSet,
|
|
70
|
-
DocumentUpdate,
|
|
71
|
-
DocumentDelete,
|
|
72
|
-
};
|
|
73
|
-
WRITE_HYDRATIONS;
|