@magentrix-corp/magentrix-sdk 1.1.7 → 1.1.8

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 (2) hide show
  1. package/README.md +191 -32
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -77,7 +77,7 @@ try {
77
77
 
78
78
  // Retrieve by ID
79
79
  try {
80
- const result = await dataService.retrieve('account-id-123')
80
+ const result = await dataService.retrieve('00I00000000003x0001')
81
81
  console.log(result.record) // The account object
82
82
  console.log('Name:', result.record.Name)
83
83
  } catch (error) {
@@ -108,7 +108,7 @@ const error = ref<string | null>(null)
108
108
  onMounted(async () => {
109
109
  try {
110
110
  // Session is automatically created when needed
111
- account.value = (await dataService.retrieve('006000000LJIxF70000')).record
111
+ account.value = (await dataService.retrieve('00I00000000003x0001')).record
112
112
  console.log(account.value)
113
113
  } catch (err) {
114
114
  if (err instanceof MagentrixError) {
@@ -188,6 +188,33 @@ const config: MagentrixConfig = {
188
188
  const dataService = new MagentrixClient(config)
189
189
  ```
190
190
 
191
+ ## Magentrix ID Format
192
+
193
+ Magentrix uses a unique 19-character base-62 identifier format that is case-sensitive.
194
+
195
+ ### ID Structure
196
+
197
+ A Magentrix ID consists of three parts:
198
+
199
+ 1. **First 3 characters**: Key Prefix - Identifies the entity type (e.g., "001" for Account, "003" for Contact)
200
+ 2. **Middle 12 characters**: Sequential base-62 number - Unique record identifier
201
+ 3. **Last 4 characters**: Organization ID - Unique identifier for the specific client
202
+
203
+ ### Example IDs
204
+
205
+ ```
206
+ 00I00000000003x0001 // Account record
207
+ 00300000000001A0001 // Contact record
208
+ 00500000000002B0001 // Opportunity record
209
+ ```
210
+
211
+ **Important**:
212
+ - IDs are case-sensitive
213
+ - Always use the full 19-character ID when referencing records
214
+ - The key prefix determines the entity type
215
+
216
+ ---
217
+
191
218
  ## API Reference
192
219
 
193
220
  ### Session Management
@@ -302,7 +329,7 @@ import { MagentrixError } from '@magentrix-corp/magentrix-sdk'
302
329
 
303
330
  try {
304
331
  // Retrieve returns an object with a 'record' property
305
- const result = await dataService.retrieve('account-id-123')
332
+ const result = await dataService.retrieve('00I00000000003x0001')
306
333
  const account = result.record
307
334
 
308
335
  console.log('Account Name:', account.Name)
@@ -310,7 +337,7 @@ try {
310
337
  console.log('Account Status:', account.Status)
311
338
 
312
339
  // Or destructure directly
313
- const { record } = await dataService.retrieve('contact-id-456')
340
+ const { record } = await dataService.retrieve('00300000000001A0001')
314
341
  console.log('Contact:', record.Name)
315
342
  } catch (error) {
316
343
  if (error instanceof MagentrixError) {
@@ -331,13 +358,14 @@ try {
331
358
 
332
359
  ### CRUD Operations
333
360
 
334
- #### `create(entityName: string, data: any): Promise<any>`
361
+ #### `create(entityName: string, data: any | any[]): Promise<any>`
335
362
 
336
- Create a new record.
363
+ Create a new record or multiple records in a single API call.
337
364
 
338
365
  ```typescript
339
366
  import { MagentrixError, DatabaseError } from '@magentrix-corp/magentrix-sdk'
340
367
 
368
+ // Create a single record
341
369
  try {
342
370
  const newAccount = await dataService.create('Account', {
343
371
  Name: 'Acme Corporation',
@@ -355,28 +383,52 @@ try {
355
383
  console.error('Create failed:', error.message)
356
384
  }
357
385
  }
386
+
387
+ // Create multiple records in a single API call (RECOMMENDED for batch operations)
388
+ try {
389
+ const newAccounts = await dataService.create('Account', [
390
+ { Name: 'Account 1', Email: 'account1@example.com', Status: 'Active' },
391
+ { Name: 'Account 2', Email: 'account2@example.com', Status: 'Active' },
392
+ { Name: 'Account 3', Email: 'account3@example.com', Status: 'Active' }
393
+ ])
394
+ console.log('Created IDs:', newAccounts.map(a => a.Id))
395
+ } catch (error) {
396
+ if (error instanceof DatabaseError) {
397
+ console.error('Database error:', error.message)
398
+ error.getErrors().forEach(err => {
399
+ console.error(`Field: ${err.fieldName}, Message: ${err.message}`)
400
+ })
401
+ } else if (error instanceof MagentrixError) {
402
+ console.error('Batch create failed:', error.message)
403
+ }
404
+ }
358
405
  ```
359
406
 
360
407
  **Parameters**:
361
408
  - `entityName`: Name of the entity (e.g., 'Account', 'Contact')
362
- - `data`: Object containing the record data
409
+ - `data`: Single object or array of objects containing the record data
363
410
 
364
- **Returns**: Created record with ID
411
+ **Returns**:
412
+ - Single object: Created record with ID
413
+ - Array: Array of created records with IDs
365
414
 
366
415
  **Throws**: `MagentrixError` if entityName or data is missing, `DatabaseError` for validation errors
367
416
 
417
+ **Note**: When creating multiple records, pass an array to make a single optimized API call instead of multiple calls.
418
+
368
419
  ---
369
420
 
370
- #### `edit(entityName: string, data: any): Promise<any>`
421
+ #### `edit(entityName: string, data: any | any[]): Promise<any>`
371
422
 
372
- Update an existing record (requires `Id` in data).
423
+ Update an existing record or multiple records in a single API call (requires `Id` in data).
373
424
 
374
425
  ```typescript
375
426
  import { MagentrixError, DatabaseError } from '@magentrix-corp/magentrix-sdk'
376
427
 
428
+ // Update a single record
377
429
  try {
378
430
  const updated = await dataService.edit('Account', {
379
- Id: 'account-id-123',
431
+ Id: '00I00000000003x0001',
380
432
  Name: 'Updated Name',
381
433
  Status: 'Inactive'
382
434
  })
@@ -391,28 +443,52 @@ try {
391
443
  console.error('Edit failed:', error.message)
392
444
  }
393
445
  }
446
+
447
+ // Update multiple records in a single API call (RECOMMENDED for batch operations)
448
+ try {
449
+ const updated = await dataService.edit('Account', [
450
+ { Id: '00I00000000003x0001', Name: 'Updated Account 1', Status: 'Active' },
451
+ { Id: '00I00000000003y0001', Name: 'Updated Account 2', Status: 'Inactive' },
452
+ { Id: '00I00000000003z0001', Name: 'Updated Account 3', Status: 'Active' }
453
+ ])
454
+ console.log('Updated successfully:', updated.length, 'records')
455
+ } catch (error) {
456
+ if (error instanceof DatabaseError) {
457
+ console.error('Database error:', error.message)
458
+ error.getErrors().forEach(err => {
459
+ console.error(`Field: ${err.fieldName}, Message: ${err.message}`)
460
+ })
461
+ } else if (error instanceof MagentrixError) {
462
+ console.error('Batch edit failed:', error.message)
463
+ }
464
+ }
394
465
  ```
395
466
 
396
467
  **Parameters**:
397
468
  - `entityName`: Name of the entity
398
- - `data`: Object containing the record data with `Id`
469
+ - `data`: Single object or array of objects containing the record data with `Id`
399
470
 
400
- **Returns**: Updated record
471
+ **Returns**:
472
+ - Single object: Updated record
473
+ - Array: Array of updated records
401
474
 
402
475
  **Throws**: `MagentrixError` if entityName or data is missing, `DatabaseError` for validation errors
403
476
 
477
+ **Note**: When updating multiple records, pass an array to make a single optimized API call instead of multiple calls.
478
+
404
479
  ---
405
480
 
406
- #### `upsert(entityName: string, data: any): Promise<any>`
481
+ #### `upsert(entityName: string, data: any | any[]): Promise<any>`
407
482
 
408
- Create or update a record. If `Id` is provided and exists, updates the record; otherwise creates a new one.
483
+ Create or update a record or multiple records in a single API call. If `Id` is provided and exists, updates the record; otherwise creates a new one.
409
484
 
410
485
  ```typescript
411
486
  import { MagentrixError, DatabaseError } from '@magentrix-corp/magentrix-sdk'
412
487
 
488
+ // Upsert a single record
413
489
  try {
414
490
  const record = await dataService.upsert('Contact', {
415
- Id: 'contact-id-456', // Optional
491
+ Id: '00300000000001A0001', // Optional
416
492
  Name: 'John Doe',
417
493
  Email: 'john@example.com'
418
494
  })
@@ -427,16 +503,39 @@ try {
427
503
  console.error('Upsert failed:', error.message)
428
504
  }
429
505
  }
506
+
507
+ // Upsert multiple records in a single API call (RECOMMENDED for batch operations)
508
+ try {
509
+ const records = await dataService.upsert('Contact', [
510
+ { Id: '00300000000001A0001', Name: 'John Doe', Email: 'john@example.com' }, // Will update
511
+ { Name: 'Jane Smith', Email: 'jane@example.com' }, // Will create (no Id)
512
+ { Id: '00300000000001B0001', Name: 'Bob Johnson', Email: 'bob@example.com' } // Will update
513
+ ])
514
+ console.log('Upsert successful:', records.length, 'records')
515
+ } catch (error) {
516
+ if (error instanceof DatabaseError) {
517
+ console.error('Database error:', error.message)
518
+ error.getErrors().forEach(err => {
519
+ console.error(`Field: ${err.fieldName}, Message: ${err.message}`)
520
+ })
521
+ } else if (error instanceof MagentrixError) {
522
+ console.error('Batch upsert failed:', error.message)
523
+ }
524
+ }
430
525
  ```
431
526
 
432
527
  **Parameters**:
433
528
  - `entityName`: Name of the entity
434
- - `data`: Object containing the record data
529
+ - `data`: Single object or array of objects containing the record data (Id is optional)
435
530
 
436
- **Returns**: Created or updated record
531
+ **Returns**:
532
+ - Single object: Created or updated record
533
+ - Array: Array of created or updated records
437
534
 
438
535
  **Throws**: `MagentrixError` if entityName or data is missing, `DatabaseError` for validation errors
439
536
 
537
+ **Note**: When upserting multiple records, pass an array to make a single optimized API call instead of multiple calls.
538
+
440
539
  ---
441
540
 
442
541
  #### `delete(entityName: string, id: string, permanent?: boolean): Promise<any>`
@@ -448,7 +547,7 @@ import { MagentrixError } from '@magentrix-corp/magentrix-sdk'
448
547
 
449
548
  try {
450
549
  // Soft delete (default)
451
- await dataService.delete('Account', 'account-id-123')
550
+ await dataService.delete('Account', '00I00000000003x0001')
452
551
  console.log('Account deleted successfully')
453
552
  } catch (error) {
454
553
  if (error instanceof MagentrixError) {
@@ -458,7 +557,7 @@ try {
458
557
 
459
558
  try {
460
559
  // Permanent delete
461
- await dataService.delete('Account', 'account-id-123', true)
560
+ await dataService.delete('Account', '00I00000000003x0001', true)
462
561
  console.log('Account permanently deleted')
463
562
  } catch (error) {
464
563
  if (error instanceof MagentrixError) {
@@ -486,7 +585,11 @@ Delete multiple records by IDs.
486
585
  import { MagentrixError } from '@magentrix-corp/magentrix-sdk'
487
586
 
488
587
  try {
489
- await dataService.deleteMany('Contact', ['id-1', 'id-2', 'id-3'])
588
+ await dataService.deleteMany('Contact', [
589
+ '00300000000001A0001',
590
+ '00300000000001B0001',
591
+ '00300000000001C0001'
592
+ ])
490
593
  console.log('Contacts deleted successfully')
491
594
  } catch (error) {
492
595
  if (error instanceof MagentrixError) {
@@ -905,32 +1008,69 @@ console.log(`Welcome ${userInfo.name}!`)
905
1008
 
906
1009
  ### Batch Operations
907
1010
 
908
- Perform multiple operations efficiently:
1011
+ The `create()`, `edit()`, and `upsert()` methods accept arrays for efficient batch operations in a single API call.
1012
+
1013
+ **✅ RECOMMENDED: Single API call with array**
909
1014
 
910
1015
  ```typescript
911
- import { MagentrixError } from '@magentrix-corp/magentrix-sdk'
1016
+ import { MagentrixError, DatabaseError } from '@magentrix-corp/magentrix-sdk'
912
1017
 
913
- // Create multiple records
1018
+ // Create multiple records in a single API call (OPTIMIZED)
914
1019
  const accounts = [
915
- { Name: 'Account 1', Status: 'Active' },
916
- { Name: 'Account 2', Status: 'Active' },
917
- { Name: 'Account 3', Status: 'Active' }
1020
+ { Name: 'Account 1', Email: 'account1@example.com', Status: 'Active' },
1021
+ { Name: 'Account 2', Email: 'account2@example.com', Status: 'Active' },
1022
+ { Name: 'Account 3', Email: 'account3@example.com', Status: 'Active' }
918
1023
  ]
919
1024
 
920
1025
  try {
921
- const created = await Promise.all(
922
- accounts.map(account => dataService.create('Account', account))
923
- )
1026
+ const created = await dataService.create('Account', accounts)
924
1027
  console.log('Created IDs:', created.map(a => a.Id))
925
1028
  } catch (error) {
926
- if (error instanceof MagentrixError) {
1029
+ if (error instanceof DatabaseError) {
927
1030
  console.error('Batch create failed:', error.message)
1031
+ error.getErrors().forEach(err => {
1032
+ console.error(`Field: ${err.fieldName}, Message: ${err.message}`)
1033
+ })
1034
+ }
1035
+ }
1036
+
1037
+ // Update multiple records in a single API call (OPTIMIZED)
1038
+ try {
1039
+ const updates = [
1040
+ { Id: '00I00000000003x0001', Name: 'Updated Account 1', Status: 'Active' },
1041
+ { Id: '00I00000000003y0001', Name: 'Updated Account 2', Status: 'Inactive' },
1042
+ { Id: '00I00000000003z0001', Name: 'Updated Account 3', Status: 'Active' }
1043
+ ]
1044
+ const updated = await dataService.edit('Account', updates)
1045
+ console.log('Updated:', updated.length, 'records')
1046
+ } catch (error) {
1047
+ if (error instanceof DatabaseError) {
1048
+ console.error('Batch update failed:', error.message)
1049
+ }
1050
+ }
1051
+
1052
+ // Upsert multiple records in a single API call (OPTIMIZED)
1053
+ try {
1054
+ const records = [
1055
+ { Id: '00300000000001A0001', Name: 'John Doe', Email: 'john@example.com' }, // Updates
1056
+ { Name: 'Jane Smith', Email: 'jane@example.com' }, // Creates (no Id)
1057
+ { Id: '00300000000001B0001', Name: 'Bob Johnson', Email: 'bob@example.com' } // Updates
1058
+ ]
1059
+ const results = await dataService.upsert('Contact', records)
1060
+ console.log('Upserted:', results.length, 'records')
1061
+ } catch (error) {
1062
+ if (error instanceof DatabaseError) {
1063
+ console.error('Batch upsert failed:', error.message)
928
1064
  }
929
1065
  }
930
1066
 
931
1067
  // Delete multiple records
932
1068
  try {
933
- const idsToDelete = ['id-1', 'id-2', 'id-3']
1069
+ const idsToDelete = [
1070
+ '00I00000000003x0001',
1071
+ '00I00000000003y0001',
1072
+ '00I00000000003z0001'
1073
+ ]
934
1074
  await dataService.deleteMany('Account', idsToDelete)
935
1075
  console.log('Batch delete successful')
936
1076
  } catch (error) {
@@ -940,6 +1080,25 @@ try {
940
1080
  }
941
1081
  ```
942
1082
 
1083
+ **❌ NOT RECOMMENDED: Multiple API calls**
1084
+
1085
+ ```typescript
1086
+ // This makes multiple API calls - NOT optimized!
1087
+ const accounts = [
1088
+ { Name: 'Account 1', Status: 'Active' },
1089
+ { Name: 'Account 2', Status: 'Active' },
1090
+ { Name: 'Account 3', Status: 'Active' }
1091
+ ]
1092
+
1093
+ // DON'T DO THIS - makes 3 separate API calls
1094
+ const created = await Promise.all(
1095
+ accounts.map(account => dataService.create('Account', account))
1096
+ )
1097
+
1098
+ // INSTEAD DO THIS - makes 1 API call
1099
+ const created = await dataService.create('Account', accounts)
1100
+ ```
1101
+
943
1102
  ### Custom Query Examples
944
1103
 
945
1104
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magentrix-corp/magentrix-sdk",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "TypeScript SDK for Magentrix APIs with dual authentication modes, automatic session management, CRUD operations, and Vue 3 integration",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.js",