@directus/api 13.1.0-beta.0 → 13.1.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.
Files changed (37) hide show
  1. package/dist/__utils__/snapshots.js +0 -9
  2. package/dist/app.js +0 -2
  3. package/dist/database/helpers/index.d.ts +2 -0
  4. package/dist/database/helpers/index.js +2 -0
  5. package/dist/database/helpers/sequence/dialects/default.d.ts +3 -0
  6. package/dist/database/helpers/sequence/dialects/default.js +3 -0
  7. package/dist/database/helpers/sequence/dialects/postgres.d.ts +9 -0
  8. package/dist/database/helpers/sequence/dialects/postgres.js +10 -0
  9. package/dist/database/helpers/sequence/index.d.ts +7 -0
  10. package/dist/database/helpers/sequence/index.js +7 -0
  11. package/dist/database/helpers/sequence/types.d.ts +5 -0
  12. package/dist/database/helpers/sequence/types.js +6 -0
  13. package/dist/database/index.js +8 -0
  14. package/dist/database/migrations/20230721A-require-shares-fields.js +45 -16
  15. package/dist/database/system-data/fields/collections.yaml +0 -19
  16. package/dist/env.d.ts +1 -1
  17. package/dist/env.js +16 -13
  18. package/dist/middleware/respond.js +0 -20
  19. package/dist/services/activity.js +4 -3
  20. package/dist/services/assets.js +17 -1
  21. package/dist/services/files.js +58 -1
  22. package/dist/services/import-export.js +16 -0
  23. package/dist/services/items.js +28 -3
  24. package/dist/types/collection.d.ts +0 -1
  25. package/dist/types/items.d.ts +1 -0
  26. package/dist/utils/sanitize-query.js +0 -3
  27. package/dist/utils/validate-query.js +0 -1
  28. package/dist/websocket/controllers/base.d.ts +1 -0
  29. package/dist/websocket/controllers/base.js +16 -0
  30. package/package.json +15 -15
  31. package/dist/controllers/branches.d.ts +0 -2
  32. package/dist/controllers/branches.js +0 -190
  33. package/dist/database/migrations/20230823A-add-content-versioning.d.ts +0 -3
  34. package/dist/database/migrations/20230823A-add-content-versioning.js +0 -26
  35. package/dist/database/system-data/fields/branches.yaml +0 -19
  36. package/dist/services/branches.d.ts +0 -25
  37. package/dist/services/branches.js +0 -205
@@ -7,7 +7,6 @@ export type CollectionMeta = {
7
7
  singleton: boolean;
8
8
  icon: string | null;
9
9
  translations: Record<string, string>;
10
- branches_enabled: boolean;
11
10
  item_duplication_fields: string[] | null;
12
11
  accountability: 'all' | 'accountability' | null;
13
12
  group: string | null;
@@ -47,6 +47,7 @@ export type MutationOptions = {
47
47
  */
48
48
  mutationTracker?: MutationTracker | undefined;
49
49
  preMutationError?: DirectusError | undefined;
50
+ bypassAutoIncrementSequenceReset?: boolean;
50
51
  };
51
52
  export type ActionEventParams = {
52
53
  event: string | string[];
@@ -46,9 +46,6 @@ export function sanitizeQuery(rawQuery, accountability) {
46
46
  if (rawQuery['search'] && typeof rawQuery['search'] === 'string') {
47
47
  query.search = rawQuery['search'];
48
48
  }
49
- if (rawQuery['branch']) {
50
- query.branch = rawQuery['branch'];
51
- }
52
49
  if (rawQuery['export']) {
53
50
  query.export = rawQuery['export'];
54
51
  }
@@ -17,7 +17,6 @@ const querySchema = Joi.object({
17
17
  meta: Joi.array().items(Joi.string().valid('total_count', 'filter_count')),
18
18
  search: Joi.string(),
19
19
  export: Joi.string().valid('csv', 'json', 'xml', 'yaml'),
20
- branch: Joi.string(),
21
20
  aggregate: Joi.object(),
22
21
  deep: Joi.object(),
23
22
  alias: Joi.object(),
@@ -30,6 +30,7 @@ export default abstract class SocketController {
30
30
  maxConnections: number;
31
31
  };
32
32
  protected getRateLimiter(): RateLimiterAbstract | null;
33
+ private catchInvalidMessages;
33
34
  protected handleUpgrade(request: IncomingMessage, socket: internal.Duplex, head: Buffer): Promise<void>;
34
35
  protected handleStrictUpgrade({ request, socket, head }: UpgradeContext, query: ParsedUrlQuery): Promise<void>;
35
36
  protected handleHandshakeUpgrade({ request, socket, head }: UpgradeContext): Promise<void>;
@@ -67,6 +67,19 @@ export default class SocketController {
67
67
  }
68
68
  return null;
69
69
  }
70
+ catchInvalidMessages(ws) {
71
+ /**
72
+ * This fix was done to prevent the API from crashing on receiving invalid WebSocket frames
73
+ * https://github.com/directus/directus/security/advisories/GHSA-hmgw-9jrg-hf2m
74
+ * https://github.com/websockets/ws/issues/2098
75
+ */
76
+ // @ts-ignore <- required because "_socket" is not typed on WS
77
+ ws._socket.prependListener('data', (data) => data.toString());
78
+ ws.on('error', (error) => {
79
+ if (error.message)
80
+ logger.debug(error.message);
81
+ });
82
+ }
70
83
  async handleUpgrade(request, socket, head) {
71
84
  const { pathname, query } = parse(request.url, true);
72
85
  if (pathname !== this.endpoint)
@@ -87,6 +100,7 @@ export default class SocketController {
87
100
  return;
88
101
  }
89
102
  this.server.handleUpgrade(request, socket, head, async (ws) => {
103
+ this.catchInvalidMessages(ws);
90
104
  const state = { accountability: null, expires_at: null };
91
105
  this.server.emit('connection', ws, state);
92
106
  });
@@ -109,12 +123,14 @@ export default class SocketController {
109
123
  return;
110
124
  }
111
125
  this.server.handleUpgrade(request, socket, head, async (ws) => {
126
+ this.catchInvalidMessages(ws);
112
127
  const state = { accountability, expires_at };
113
128
  this.server.emit('connection', ws, state);
114
129
  });
115
130
  }
116
131
  async handleHandshakeUpgrade({ request, socket, head }) {
117
132
  this.server.handleUpgrade(request, socket, head, async (ws) => {
133
+ this.catchInvalidMessages(ws);
118
134
  try {
119
135
  const payload = await waitForAnyMessage(ws, this.authentication.timeout);
120
136
  if (getMessageType(payload) !== 'auth')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@directus/api",
3
- "version": "13.1.0-beta.0",
3
+ "version": "13.1.0",
4
4
  "description": "Directus is a real-time API and App dashboard for managing SQL database content",
5
5
  "keywords": [
6
6
  "directus",
@@ -131,7 +131,7 @@
131
131
  "rollup": "3.22.0",
132
132
  "samlify": "2.8.10",
133
133
  "sanitize-html": "2.10.0",
134
- "sharp": "0.32.1",
134
+ "sharp": "0.32.5",
135
135
  "snappy": "7.2.2",
136
136
  "stream-json": "1.7.5",
137
137
  "strip-bom-stream": "5.0.0",
@@ -143,22 +143,22 @@
143
143
  "ws": "8.12.1",
144
144
  "zod": "3.21.4",
145
145
  "zod-validation-error": "1.0.1",
146
- "@directus/app": "10.8.0-beta.0",
147
- "@directus/constants": "10.2.4-beta.0",
146
+ "@directus/app": "10.8.0",
147
+ "@directus/constants": "10.2.3",
148
148
  "@directus/errors": "0.0.2",
149
- "@directus/extensions-sdk": "10.1.10-beta.0",
150
- "@directus/pressure": "1.0.9-beta.0",
149
+ "@directus/extensions-sdk": "10.1.10",
150
+ "@directus/pressure": "1.0.9",
151
151
  "@directus/schema": "10.0.2",
152
152
  "@directus/specs": "10.2.0",
153
153
  "@directus/storage": "10.0.5",
154
- "@directus/storage-driver-azure": "10.0.10-beta.0",
155
- "@directus/storage-driver-cloudinary": "10.0.10-beta.0",
156
- "@directus/storage-driver-gcs": "10.0.10-beta.0",
157
- "@directus/storage-driver-local": "10.0.10-beta.0",
158
- "@directus/storage-driver-s3": "10.0.10-beta.0",
159
- "@directus/storage-driver-supabase": "1.0.2-beta.0",
160
- "@directus/utils": "10.0.10-beta.0",
161
- "@directus/validation": "0.0.5-beta.0"
154
+ "@directus/storage-driver-azure": "10.0.10",
155
+ "@directus/storage-driver-cloudinary": "10.0.10",
156
+ "@directus/storage-driver-gcs": "10.0.10",
157
+ "@directus/storage-driver-local": "10.0.10",
158
+ "@directus/storage-driver-s3": "10.0.10",
159
+ "@directus/storage-driver-supabase": "1.0.2",
160
+ "@directus/utils": "10.0.10",
161
+ "@directus/validation": "0.0.5"
162
162
  },
163
163
  "devDependencies": {
164
164
  "@ngneat/falso": "6.4.0",
@@ -207,7 +207,7 @@
207
207
  "vitest": "0.31.1",
208
208
  "@directus/random": "0.2.2",
209
209
  "@directus/tsconfig": "1.0.0",
210
- "@directus/types": "10.1.6-beta.0"
210
+ "@directus/types": "10.1.6"
211
211
  },
212
212
  "optionalDependencies": {
213
213
  "@keyv/redis": "2.5.8",
@@ -1,2 +0,0 @@
1
- declare const router: import("express-serve-static-core").Router;
2
- export default router;
@@ -1,190 +0,0 @@
1
- import { isDirectusError } from '@directus/errors';
2
- import express from 'express';
3
- import { assign } from 'lodash-es';
4
- import { ErrorCode, InvalidPayloadError } from '../errors/index.js';
5
- import { respond } from '../middleware/respond.js';
6
- import useCollection from '../middleware/use-collection.js';
7
- import { validateBatch } from '../middleware/validate-batch.js';
8
- import { BranchesService } from '../services/branches.js';
9
- import { MetaService } from '../services/meta.js';
10
- import asyncHandler from '../utils/async-handler.js';
11
- import { sanitizeQuery } from '../utils/sanitize-query.js';
12
- const router = express.Router();
13
- router.use(useCollection('directus_branches'));
14
- router.post('/', asyncHandler(async (req, res, next) => {
15
- const service = new BranchesService({
16
- accountability: req.accountability,
17
- schema: req.schema,
18
- });
19
- const savedKeys = [];
20
- if (Array.isArray(req.body)) {
21
- const keys = await service.createMany(req.body);
22
- savedKeys.push(...keys);
23
- }
24
- else {
25
- const primaryKey = await service.createOne(req.body);
26
- savedKeys.push(primaryKey);
27
- }
28
- try {
29
- if (Array.isArray(req.body)) {
30
- const records = await service.readMany(savedKeys, req.sanitizedQuery);
31
- res.locals['payload'] = { data: records };
32
- }
33
- else {
34
- const record = await service.readOne(savedKeys[0], req.sanitizedQuery);
35
- res.locals['payload'] = { data: record };
36
- }
37
- }
38
- catch (error) {
39
- if (isDirectusError(error, ErrorCode.Forbidden)) {
40
- return next();
41
- }
42
- throw error;
43
- }
44
- return next();
45
- }), respond);
46
- const readHandler = asyncHandler(async (req, res, next) => {
47
- const service = new BranchesService({
48
- accountability: req.accountability,
49
- schema: req.schema,
50
- });
51
- const metaService = new MetaService({
52
- accountability: req.accountability,
53
- schema: req.schema,
54
- });
55
- let result;
56
- if (req.singleton) {
57
- result = await service.readSingleton(req.sanitizedQuery);
58
- }
59
- else if (req.body.keys) {
60
- result = await service.readMany(req.body.keys, req.sanitizedQuery);
61
- }
62
- else {
63
- result = await service.readByQuery(req.sanitizedQuery);
64
- }
65
- const meta = await metaService.getMetaForQuery(req.collection, req.sanitizedQuery);
66
- res.locals['payload'] = { data: result, meta };
67
- return next();
68
- });
69
- router.get('/', validateBatch('read'), readHandler, respond);
70
- router.search('/', validateBatch('read'), readHandler, respond);
71
- router.get('/:pk', asyncHandler(async (req, res, next) => {
72
- const service = new BranchesService({
73
- accountability: req.accountability,
74
- schema: req.schema,
75
- });
76
- const record = await service.readOne(req.params['pk'], req.sanitizedQuery);
77
- res.locals['payload'] = { data: record || null };
78
- return next();
79
- }), respond);
80
- router.patch('/', validateBatch('update'), asyncHandler(async (req, res, next) => {
81
- const service = new BranchesService({
82
- accountability: req.accountability,
83
- schema: req.schema,
84
- });
85
- let keys = [];
86
- if (Array.isArray(req.body)) {
87
- keys = await service.updateBatch(req.body);
88
- }
89
- else if (req.body.keys) {
90
- keys = await service.updateMany(req.body.keys, req.body.data);
91
- }
92
- else {
93
- const sanitizedQuery = sanitizeQuery(req.body.query, req.accountability);
94
- keys = await service.updateByQuery(sanitizedQuery, req.body.data);
95
- }
96
- try {
97
- const result = await service.readMany(keys, req.sanitizedQuery);
98
- res.locals['payload'] = { data: result || null };
99
- }
100
- catch (error) {
101
- if (isDirectusError(error, ErrorCode.Forbidden)) {
102
- return next();
103
- }
104
- throw error;
105
- }
106
- return next();
107
- }), respond);
108
- router.patch('/:pk', asyncHandler(async (req, res, next) => {
109
- const service = new BranchesService({
110
- accountability: req.accountability,
111
- schema: req.schema,
112
- });
113
- const primaryKey = await service.updateOne(req.params['pk'], req.body);
114
- try {
115
- const record = await service.readOne(primaryKey, req.sanitizedQuery);
116
- res.locals['payload'] = { data: record || null };
117
- }
118
- catch (error) {
119
- if (isDirectusError(error, ErrorCode.Forbidden)) {
120
- return next();
121
- }
122
- throw error;
123
- }
124
- return next();
125
- }), respond);
126
- router.delete('/', validateBatch('delete'), asyncHandler(async (req, _res, next) => {
127
- const service = new BranchesService({
128
- accountability: req.accountability,
129
- schema: req.schema,
130
- });
131
- if (Array.isArray(req.body)) {
132
- await service.deleteMany(req.body);
133
- }
134
- else if (req.body.keys) {
135
- await service.deleteMany(req.body.keys);
136
- }
137
- else {
138
- const sanitizedQuery = sanitizeQuery(req.body.query, req.accountability);
139
- await service.deleteByQuery(sanitizedQuery);
140
- }
141
- return next();
142
- }), respond);
143
- router.delete('/:pk', asyncHandler(async (req, _res, next) => {
144
- const service = new BranchesService({
145
- accountability: req.accountability,
146
- schema: req.schema,
147
- });
148
- await service.deleteOne(req.params['pk']);
149
- return next();
150
- }), respond);
151
- router.get('/:pk/compare', asyncHandler(async (req, res, next) => {
152
- const service = new BranchesService({
153
- accountability: req.accountability,
154
- schema: req.schema,
155
- });
156
- const branch = await service.readOne(req.params['pk']);
157
- const { outdated, mainHash } = await service.verifyHash(branch['collection'], branch['item'], branch['hash']);
158
- const commits = await service.getBranchCommits(branch['id']);
159
- const current = assign({}, ...commits);
160
- const fields = Object.keys(current);
161
- const mainBranchItem = await service.getMainBranchItem(branch['collection'], branch['item'], fields.length > 0 ? { fields } : undefined);
162
- res.locals['payload'] = { data: { outdated, mainHash, current, main: mainBranchItem } };
163
- return next();
164
- }), respond);
165
- router.post('/:pk/commit', asyncHandler(async (req, res, next) => {
166
- const service = new BranchesService({
167
- accountability: req.accountability,
168
- schema: req.schema,
169
- });
170
- const branch = await service.readOne(req.params['pk']);
171
- const mainBranchItem = await service.getMainBranchItem(branch['collection'], branch['item']);
172
- await service.commit(req.params['pk'], req.body);
173
- const commits = await service.getBranchCommits(req.params['pk']);
174
- const result = assign(mainBranchItem, ...commits);
175
- res.locals['payload'] = { data: result || null };
176
- return next();
177
- }), respond);
178
- router.post('/:pk/merge', asyncHandler(async (req, res, next) => {
179
- if (typeof req.body.mainHash !== 'string') {
180
- throw new InvalidPayloadError({ reason: `"mainHash" field is required` });
181
- }
182
- const service = new BranchesService({
183
- accountability: req.accountability,
184
- schema: req.schema,
185
- });
186
- const updatedItemKey = await service.merge(req.params['pk'], req.body.mainHash, req.body?.['fields']);
187
- res.locals['payload'] = { data: updatedItemKey || null };
188
- return next();
189
- }), respond);
190
- export default router;
@@ -1,3 +0,0 @@
1
- import type { Knex } from 'knex';
2
- export declare function up(knex: Knex): Promise<void>;
3
- export declare function down(knex: Knex): Promise<void>;
@@ -1,26 +0,0 @@
1
- export async function up(knex) {
2
- await knex.schema.createTable('directus_branches', (table) => {
3
- table.uuid('id').primary().notNullable();
4
- table.string('name').notNullable();
5
- table.string('collection', 64).references('collection').inTable('directus_collections').onDelete('CASCADE');
6
- table.string('item');
7
- table.string('hash').notNullable();
8
- table.timestamp('date_created').defaultTo(knex.fn.now());
9
- table.uuid('user_created').references('id').inTable('directus_users').onDelete('SET NULL');
10
- });
11
- await knex.schema.alterTable('directus_collections', (table) => {
12
- table.boolean('branches_enabled').notNullable().defaultTo(false);
13
- });
14
- await knex.schema.alterTable('directus_revisions', (table) => {
15
- table.uuid('branch').references('id').inTable('directus_branches').onDelete('CASCADE');
16
- });
17
- }
18
- export async function down(knex) {
19
- await knex.schema.dropTable('directus_branches');
20
- await knex.schema.alterTable('directus_collections', (table) => {
21
- table.dropColumn('branches_enabled');
22
- });
23
- await knex.schema.alterTable('directus_revisions', (table) => {
24
- table.dropColumn('branch');
25
- });
26
- }
@@ -1,19 +0,0 @@
1
- table: directus_branches
2
-
3
- fields:
4
- - field: id
5
- special:
6
- - uuid
7
- readonly: true
8
- hidden: true
9
- - field: name
10
- - field: collection
11
- - field: item
12
- - field: hash
13
- - field: date_created
14
- special:
15
- - date-created
16
- - cast-timestamp
17
- - field: user_created
18
- special:
19
- - user-created
@@ -1,25 +0,0 @@
1
- import type { Item, PrimaryKey, Query } from '@directus/types';
2
- import type { AbstractServiceOptions, MutationOptions } from '../types/index.js';
3
- import { AuthorizationService } from './authorization.js';
4
- import { CollectionsService } from './collections.js';
5
- import { ItemsService } from './items.js';
6
- export declare class BranchesService extends ItemsService {
7
- authorizationService: AuthorizationService;
8
- collectionsService: CollectionsService;
9
- constructor(options: AbstractServiceOptions);
10
- private validateCreateData;
11
- private validateUpdateData;
12
- getMainBranchItem(collection: string, item: PrimaryKey, query?: Query): Promise<Item>;
13
- verifyHash(collection: string, item: PrimaryKey, hash: string): Promise<{
14
- outdated: boolean;
15
- mainHash: string;
16
- }>;
17
- getBranchCommits(key: PrimaryKey): Promise<Partial<Item>[]>;
18
- createOne(data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey>;
19
- createMany(data: Partial<Item>[], opts?: MutationOptions): Promise<PrimaryKey[]>;
20
- updateBatch(data: Partial<Item>[], opts?: MutationOptions): Promise<PrimaryKey[]>;
21
- updateMany(keys: PrimaryKey[], data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey[]>;
22
- updateByQuery(query: Query, data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey[]>;
23
- commit(key: PrimaryKey, data: Partial<Item>): Promise<Partial<Item>>;
24
- merge(branch: PrimaryKey, mainHash: string, fields?: string[]): Promise<import("../types/items.js").PrimaryKey>;
25
- }
@@ -1,205 +0,0 @@
1
- import Joi from 'joi';
2
- import { assign, pick } from 'lodash-es';
3
- import objectHash from 'object-hash';
4
- import getDatabase from '../database/index.js';
5
- import emitter from '../emitter.js';
6
- import { InvalidPayloadError, UnprocessableContentError } from '../errors/index.js';
7
- import { ActivityService } from './activity.js';
8
- import { AuthorizationService } from './authorization.js';
9
- import { CollectionsService } from './collections.js';
10
- import { ItemsService } from './items.js';
11
- import { PayloadService } from './payload.js';
12
- import { RevisionsService } from './revisions.js';
13
- const branchUpdateSchema = Joi.object({
14
- name: Joi.string(),
15
- });
16
- export class BranchesService extends ItemsService {
17
- authorizationService;
18
- collectionsService;
19
- constructor(options) {
20
- super('directus_branches', options);
21
- this.authorizationService = new AuthorizationService({
22
- accountability: this.accountability,
23
- knex: this.knex,
24
- schema: this.schema,
25
- });
26
- this.collectionsService = new CollectionsService({
27
- accountability: this.accountability,
28
- knex: this.knex,
29
- schema: this.schema,
30
- });
31
- }
32
- async validateCreateData(data) {
33
- if (!data['name'])
34
- throw new InvalidPayloadError({ reason: `"name" is required` });
35
- // Reserves the "main" branch name for the branch query parameter
36
- if (data['name'] === 'main')
37
- throw new InvalidPayloadError({ reason: `"main" is a reserved branch name` });
38
- if (!data['collection']) {
39
- throw new InvalidPayloadError({ reason: `"collection" is required` });
40
- }
41
- if (!data['item'])
42
- throw new InvalidPayloadError({ reason: `"item" is required` });
43
- // will throw an error if the collection does not exist or the accountability does not have permission to read it
44
- const existingCollection = await this.collectionsService.readOne(data['collection']);
45
- if (!existingCollection.meta?.branches_enabled) {
46
- throw new UnprocessableContentError({
47
- reason: `Branch feature is not enabled for collection "${data['collection']}"`,
48
- });
49
- }
50
- const existingBranches = await super.readByQuery({
51
- fields: ['name', 'collection', 'item'],
52
- filter: { name: { _eq: data['name'] }, collection: { _eq: data['collection'] }, item: { _eq: data['item'] } },
53
- });
54
- if (existingBranches.length > 0) {
55
- throw new UnprocessableContentError({
56
- reason: `Branch "${data['name']}" already exists for item "${data['item']}" in collection "${data['collection']}"`,
57
- });
58
- }
59
- // will throw an error if the accountability does not have permission to read the item
60
- await this.authorizationService.checkAccess('read', data['collection'], data['item']);
61
- }
62
- async validateUpdateData(data) {
63
- // Only allow updates on "name" field
64
- const { error } = branchUpdateSchema.validate(data);
65
- if (error)
66
- throw new InvalidPayloadError({ reason: error.message });
67
- // Reserves the "main" branch name for the branch query parameter
68
- if ('name' in data && data['name'] === 'main') {
69
- throw new InvalidPayloadError({ reason: `"main" is a reserved branch name` });
70
- }
71
- }
72
- async getMainBranchItem(collection, item, query) {
73
- // will throw an error if the accountability does not have permission to read the item
74
- await this.authorizationService.checkAccess('read', collection, item);
75
- const itemsService = new ItemsService(collection, {
76
- knex: this.knex,
77
- accountability: this.accountability,
78
- schema: this.schema,
79
- });
80
- return await itemsService.readOne(item, query);
81
- }
82
- async verifyHash(collection, item, hash) {
83
- const mainBranchItem = await this.getMainBranchItem(collection, item);
84
- const mainHash = objectHash(mainBranchItem);
85
- return { outdated: hash !== mainHash, mainHash };
86
- }
87
- async getBranchCommits(key) {
88
- const revisionsService = new RevisionsService({
89
- knex: this.knex,
90
- schema: this.schema,
91
- });
92
- const result = await revisionsService.readByQuery({
93
- filter: { branch: { _eq: key } },
94
- });
95
- return result.map((revision) => revision['delta']);
96
- }
97
- async createOne(data, opts) {
98
- await this.validateCreateData(data);
99
- const mainBranchItem = await this.getMainBranchItem(data['collection'], data['item']);
100
- data['hash'] = objectHash(mainBranchItem);
101
- return super.createOne(data, opts);
102
- }
103
- async createMany(data, opts) {
104
- if (!Array.isArray(data)) {
105
- throw new InvalidPayloadError({ reason: 'Input should be an array of items' });
106
- }
107
- for (const item of data) {
108
- await this.validateCreateData(item);
109
- const mainBranchItem = await this.getMainBranchItem(item['collection'], item['item']);
110
- item['hash'] = objectHash(mainBranchItem);
111
- }
112
- return super.createMany(data, opts);
113
- }
114
- async updateBatch(data, opts) {
115
- if (!Array.isArray(data)) {
116
- throw new InvalidPayloadError({ reason: 'Input should be an array of items' });
117
- }
118
- for (const item of data) {
119
- await this.validateUpdateData(item);
120
- }
121
- return super.updateBatch(data, opts);
122
- }
123
- async updateMany(keys, data, opts) {
124
- await this.validateUpdateData(data);
125
- return super.updateMany(keys, data, opts);
126
- }
127
- async updateByQuery(query, data, opts) {
128
- await this.validateUpdateData(data);
129
- return super.updateByQuery(query, data, opts);
130
- }
131
- async commit(key, data) {
132
- const branch = await super.readOne(key);
133
- const payloadService = new PayloadService(this.collection, {
134
- accountability: this.accountability,
135
- knex: this.knex,
136
- schema: this.schema,
137
- });
138
- const activityService = new ActivityService({
139
- knex: this.knex,
140
- schema: this.schema,
141
- });
142
- const revisionsService = new RevisionsService({
143
- knex: this.knex,
144
- schema: this.schema,
145
- });
146
- const activity = await activityService.createOne({
147
- action: 'commit',
148
- user: this.accountability?.user ?? null,
149
- collection: branch['collection'],
150
- ip: this.accountability?.ip ?? null,
151
- user_agent: this.accountability?.userAgent ?? null,
152
- origin: this.accountability?.origin ?? null,
153
- item: branch['item'],
154
- });
155
- const revisionDelta = await payloadService.prepareDelta(data);
156
- await revisionsService.createOne({
157
- activity,
158
- branch: key,
159
- collection: branch['collection'],
160
- item: branch['item'],
161
- data: revisionDelta,
162
- delta: revisionDelta,
163
- });
164
- return data;
165
- }
166
- async merge(branch, mainHash, fields) {
167
- const { id, collection, item } = (await this.readOne(branch));
168
- // will throw an error if the accountability does not have permission to update the item
169
- await this.authorizationService.checkAccess('update', collection, item);
170
- const { outdated } = await this.verifyHash(collection, item, mainHash);
171
- if (outdated) {
172
- throw new UnprocessableContentError({
173
- reason: `Main branch has changed since this branch was last updated`,
174
- });
175
- }
176
- const commits = await this.getBranchCommits(id);
177
- const branchResult = assign({}, ...commits);
178
- const payloadToUpdate = fields ? pick(branchResult, fields) : branchResult;
179
- const itemsService = new ItemsService(collection, {
180
- accountability: this.accountability,
181
- schema: this.schema,
182
- });
183
- const payloadAfterHooks = await emitter.emitFilter(['items.merge', `${collection}.items.merge`], payloadToUpdate, {
184
- collection,
185
- item,
186
- branch,
187
- }, {
188
- database: getDatabase(),
189
- schema: this.schema,
190
- accountability: this.accountability,
191
- });
192
- const updatedItemKey = await itemsService.updateOne(item, payloadAfterHooks);
193
- emitter.emitAction(['items.merge', `${collection}.items.merge`], {
194
- payload: payloadAfterHooks,
195
- collection,
196
- item: updatedItemKey,
197
- branch,
198
- }, {
199
- database: getDatabase(),
200
- schema: this.schema,
201
- accountability: this.accountability,
202
- });
203
- return updatedItemKey;
204
- }
205
- }