lazypock 0.1.2 → 0.1.5
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/README.md +44 -42
- package/dist/{chunk-3MZJ5HCD.js → chunk-Y6PMBL2S.js} +7 -5
- package/dist/{chunk-3MZJ5HCD.js.map → chunk-Y6PMBL2S.js.map} +1 -1
- package/dist/cli.cjs +9 -7
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.global.js +9 -7
- package/dist/cli.global.js.map +1 -1
- package/dist/cli.js +4 -4
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +189 -133
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +98 -78
- package/dist/index.d.ts +98 -78
- package/dist/index.global.js +188 -133
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +183 -130
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/cli.ts +7 -4
- package/src/codegen.ts +6 -4
- package/src/collection.ts +64 -10
- package/src/collections.ts +200 -0
- package/src/index.ts +2 -0
- package/src/lazypock.ts +5 -170
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
fieldTypeScriptType,
|
|
5
5
|
generateTypes,
|
|
6
6
|
schemaFieldType
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-Y6PMBL2S.js";
|
|
8
8
|
|
|
9
9
|
// src/types.ts
|
|
10
10
|
var ApiError = class extends Error {
|
|
@@ -321,17 +321,62 @@ var CollectionService = class {
|
|
|
321
321
|
return encodeURIComponent(id);
|
|
322
322
|
}
|
|
323
323
|
/**
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* @param
|
|
324
|
+
* Fetch a paginated list of records (PocketBase `getList`).
|
|
325
|
+
*
|
|
326
|
+
* @param page Page number (default 1).
|
|
327
|
+
* @param perPage Records per page (default 30).
|
|
328
|
+
* @param options Query params (`filter`, `sort`, `expand`, `fields`) + request options.
|
|
327
329
|
*/
|
|
328
|
-
|
|
329
|
-
const
|
|
330
|
+
getList(page = 1, perPage = 30, options) {
|
|
331
|
+
const { ...rest } = options ?? {};
|
|
332
|
+
const qs = new URLSearchParams(
|
|
333
|
+
Object.fromEntries(
|
|
334
|
+
Object.entries({ page: String(page), perPage: String(perPage), ...rest }).map(
|
|
335
|
+
([k, v]) => [k, String(v)]
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
).toString();
|
|
330
339
|
return this.http.get(
|
|
331
|
-
"/" + this.encodeId(this.collectionName) + qs,
|
|
340
|
+
"/" + this.encodeId(this.collectionName) + "?" + qs,
|
|
332
341
|
options
|
|
333
342
|
);
|
|
334
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Fetch all records at once (auto-paginates). Mirrors PocketBase's
|
|
346
|
+
* `pb.collection(name).getFullList()`.
|
|
347
|
+
*
|
|
348
|
+
* @param options Query params (`sort`, `filter`, `batch`, etc.) + request options.
|
|
349
|
+
*/
|
|
350
|
+
async getFullList(options) {
|
|
351
|
+
const { batch = 1e3, ...rest } = options ?? {};
|
|
352
|
+
const items = [];
|
|
353
|
+
let page = 1;
|
|
354
|
+
for (; ; ) {
|
|
355
|
+
const res = await this.getList(
|
|
356
|
+
page,
|
|
357
|
+
batch,
|
|
358
|
+
rest
|
|
359
|
+
);
|
|
360
|
+
if (!res || !res.items || res.items.length === 0) break;
|
|
361
|
+
items.push(...res.items);
|
|
362
|
+
if (page >= (res.totalPages ?? page)) break;
|
|
363
|
+
page += 1;
|
|
364
|
+
}
|
|
365
|
+
return items;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Fetch the first record matching a filter (PocketBase `getFirstListItem`).
|
|
369
|
+
*
|
|
370
|
+
* @param filter Filter expression (e.g. `title = 'x'`).
|
|
371
|
+
* @param options Optional request options.
|
|
372
|
+
*/
|
|
373
|
+
async getFirstListItem(filter, options) {
|
|
374
|
+
const res = await this.getList(1, 1, {
|
|
375
|
+
...options,
|
|
376
|
+
filter
|
|
377
|
+
});
|
|
378
|
+
return res?.items?.[0] ?? null;
|
|
379
|
+
}
|
|
335
380
|
/**
|
|
336
381
|
* Get a single record by ID.
|
|
337
382
|
* @param id Record ID.
|
|
@@ -776,6 +821,135 @@ var FilesService = class {
|
|
|
776
821
|
}
|
|
777
822
|
};
|
|
778
823
|
|
|
824
|
+
// src/collections.ts
|
|
825
|
+
var REGISTRY_TOPIC = "collections";
|
|
826
|
+
var CollectionsService = class {
|
|
827
|
+
/** @internal */
|
|
828
|
+
constructor(http, realtime) {
|
|
829
|
+
this.http = http;
|
|
830
|
+
this.realtime = realtime;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Fetch a paginated list of collections (admin).
|
|
834
|
+
*
|
|
835
|
+
* @param params Optional query params (`page`, `perPage`, `filter`, `sort`).
|
|
836
|
+
* @param options Optional request options.
|
|
837
|
+
*/
|
|
838
|
+
async getList(params, options) {
|
|
839
|
+
const qs = params ? "?" + new URLSearchParams(
|
|
840
|
+
Object.fromEntries(
|
|
841
|
+
Object.entries(params).map(([k, v]) => [k, String(v)])
|
|
842
|
+
)
|
|
843
|
+
).toString() : "";
|
|
844
|
+
return this.http?.get("/collections" + qs, options) ?? null;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Fetch all collections at once (auto-paginates). Mirrors PocketBase's
|
|
848
|
+
* `pb.collections.getFullList()` — defaults to listing everything.
|
|
849
|
+
*
|
|
850
|
+
* @param options Query params (`sort`, `batch`, etc.) or request options.
|
|
851
|
+
*/
|
|
852
|
+
async getFullList(options) {
|
|
853
|
+
if (!this.http) return [];
|
|
854
|
+
const { batch = 1e3, ...rest } = options ?? {};
|
|
855
|
+
const items = [];
|
|
856
|
+
let page = 1;
|
|
857
|
+
for (; ; ) {
|
|
858
|
+
const res = await this.getList({
|
|
859
|
+
...rest,
|
|
860
|
+
page,
|
|
861
|
+
perPage: batch
|
|
862
|
+
});
|
|
863
|
+
if (!res || !res.items || res.items.length === 0) break;
|
|
864
|
+
items.push(...res.items);
|
|
865
|
+
if (page >= (res.totalPages ?? page)) break;
|
|
866
|
+
page += 1;
|
|
867
|
+
}
|
|
868
|
+
return items;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Get a single collection by ID or name (admin).
|
|
872
|
+
*
|
|
873
|
+
* @param id Collection ID or name.
|
|
874
|
+
* @param options Optional request options.
|
|
875
|
+
*/
|
|
876
|
+
async getOne(id, options) {
|
|
877
|
+
return this.http?.get("/collections/" + encodeURIComponent(id), options) ?? null;
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* Create a new collection (admin).
|
|
881
|
+
*
|
|
882
|
+
* @param data Collection definition (name, type, fields, options, rules, etc.).
|
|
883
|
+
* @param options Optional request options.
|
|
884
|
+
*/
|
|
885
|
+
async create(data, options) {
|
|
886
|
+
return this.http?.post("/collections", data, options) ?? null;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Update an existing collection (admin).
|
|
890
|
+
*
|
|
891
|
+
* @param id Collection ID or name.
|
|
892
|
+
* @param data Updated collection fields.
|
|
893
|
+
* @param options Optional request options.
|
|
894
|
+
*/
|
|
895
|
+
async update(id, data, options) {
|
|
896
|
+
return this.http?.patch(
|
|
897
|
+
"/collections/" + encodeURIComponent(id),
|
|
898
|
+
data,
|
|
899
|
+
options
|
|
900
|
+
) ?? null;
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Delete a collection (admin).
|
|
904
|
+
*
|
|
905
|
+
* @param id Collection ID or name.
|
|
906
|
+
* @param options Optional request options.
|
|
907
|
+
*/
|
|
908
|
+
async delete(id, options) {
|
|
909
|
+
const res = this.http?.delete(
|
|
910
|
+
"/collections/" + encodeURIComponent(id),
|
|
911
|
+
options
|
|
912
|
+
);
|
|
913
|
+
return res == null ? false : true;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Subscribe to collection registry changes.
|
|
917
|
+
*
|
|
918
|
+
* @param callback Received on every collection create/update/delete.
|
|
919
|
+
* @returns A function that unsubscribes this callback.
|
|
920
|
+
*/
|
|
921
|
+
subscribe(callback) {
|
|
922
|
+
if (!this.realtime) {
|
|
923
|
+
console.warn("[lazypock] No realtime service configured.");
|
|
924
|
+
return () => {
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
const handler = (raw) => {
|
|
928
|
+
callback({
|
|
929
|
+
action: normalizeAction2(raw.event),
|
|
930
|
+
collection: raw.payload ?? {},
|
|
931
|
+
topic: raw.topic
|
|
932
|
+
});
|
|
933
|
+
};
|
|
934
|
+
this.realtime.ensureConnected();
|
|
935
|
+
this.realtime.subscribe(REGISTRY_TOPIC, handler);
|
|
936
|
+
return () => this.realtime?.unsubscribe(REGISTRY_TOPIC, handler);
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Unsubscribe all callbacks from the registry channel.
|
|
940
|
+
*/
|
|
941
|
+
unsubscribe() {
|
|
942
|
+
this.realtime?.unsubscribe(REGISTRY_TOPIC);
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
function normalizeAction2(event) {
|
|
946
|
+
const e = event.toLowerCase();
|
|
947
|
+
if (e === "create") return "create";
|
|
948
|
+
if (e === "update") return "update";
|
|
949
|
+
if (e === "delete") return "delete";
|
|
950
|
+
return "update";
|
|
951
|
+
}
|
|
952
|
+
|
|
779
953
|
// src/lazypock.ts
|
|
780
954
|
var LazypockClient = class {
|
|
781
955
|
/**
|
|
@@ -792,6 +966,7 @@ var LazypockClient = class {
|
|
|
792
966
|
this.realtime.setUrl(wsUrlFromBaseUrl(baseUrl));
|
|
793
967
|
}
|
|
794
968
|
this.files = new FilesService(this.http);
|
|
969
|
+
this.collections = new CollectionsService(this.http, this.realtime);
|
|
795
970
|
if (options.types?.schemas) {
|
|
796
971
|
this.schemaByName = new Map(
|
|
797
972
|
options.types.schemas.map((s) => [s.name, s])
|
|
@@ -952,129 +1127,6 @@ var LazypockClient = class {
|
|
|
952
1127
|
health(options) {
|
|
953
1128
|
return this.http.get("/health", options);
|
|
954
1129
|
}
|
|
955
|
-
// ── Collection Management (admin) ──
|
|
956
|
-
/**
|
|
957
|
-
* List all collections (admin).
|
|
958
|
-
* @param q URL query string (e.g. `page=1&perPage=200`).
|
|
959
|
-
* @param options Optional request options.
|
|
960
|
-
*/
|
|
961
|
-
listCollections(q, options) {
|
|
962
|
-
return this.http.get(
|
|
963
|
-
"/collections" + (q ? "?" + q : ""),
|
|
964
|
-
options
|
|
965
|
-
);
|
|
966
|
-
}
|
|
967
|
-
/**
|
|
968
|
-
* Get a single collection by ID or name.
|
|
969
|
-
* @param id Collection ID or name.
|
|
970
|
-
* @param options Optional request options.
|
|
971
|
-
*/
|
|
972
|
-
getCollection(id, options) {
|
|
973
|
-
return this.http.get(
|
|
974
|
-
"/collections/" + encodeURIComponent(id),
|
|
975
|
-
options
|
|
976
|
-
);
|
|
977
|
-
}
|
|
978
|
-
/**
|
|
979
|
-
* Create a new collection (admin).
|
|
980
|
-
* @param data Collection definition (name, type, fields, options, rules, etc.).
|
|
981
|
-
* @param options Optional request options.
|
|
982
|
-
*/
|
|
983
|
-
createCollection(data, options) {
|
|
984
|
-
return this.http.post("/collections", data, options);
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
* Update an existing collection (admin).
|
|
988
|
-
* @param id Collection ID or name.
|
|
989
|
-
* @param data Updated collection fields.
|
|
990
|
-
* @param options Optional request options.
|
|
991
|
-
*/
|
|
992
|
-
updateCollection(id, data, options) {
|
|
993
|
-
return this.http.patch(
|
|
994
|
-
"/collections/" + encodeURIComponent(id),
|
|
995
|
-
data,
|
|
996
|
-
options
|
|
997
|
-
);
|
|
998
|
-
}
|
|
999
|
-
/**
|
|
1000
|
-
* Delete a collection (admin).
|
|
1001
|
-
* @param id Collection ID or name.
|
|
1002
|
-
* @param options Optional request options.
|
|
1003
|
-
*/
|
|
1004
|
-
deleteCollection(id, options) {
|
|
1005
|
-
return this.http.delete("/collections/" + encodeURIComponent(id), options);
|
|
1006
|
-
}
|
|
1007
|
-
// ── Records (dynamic collection) ──
|
|
1008
|
-
/**
|
|
1009
|
-
* List records from a dynamic collection with optional filter/sort/pagination.
|
|
1010
|
-
*
|
|
1011
|
-
* @param coll Collection name.
|
|
1012
|
-
* @param params Query parameters including:
|
|
1013
|
-
* - `filter` — PocketBase filter syntax (e.g. `title~'hello' && published=true`)
|
|
1014
|
-
* - `sort` — Comma-separated, `-` prefix for DESC (e.g. `-created,title`)
|
|
1015
|
-
* - `page` — Page number (default: 1)
|
|
1016
|
-
* - `perPage` — Items per page (default: 30, max: 200)
|
|
1017
|
-
* - `expand` — Comma-separated relation fields (e.g. `author,category`)
|
|
1018
|
-
* @param options Optional request options.
|
|
1019
|
-
*/
|
|
1020
|
-
listRecords(coll, params, options) {
|
|
1021
|
-
const qs = params ? "?" + new URLSearchParams(params).toString() : "";
|
|
1022
|
-
return this.http.get(
|
|
1023
|
-
"/" + encodeURIComponent(coll) + qs,
|
|
1024
|
-
options
|
|
1025
|
-
);
|
|
1026
|
-
}
|
|
1027
|
-
/**
|
|
1028
|
-
* Get a single record by ID.
|
|
1029
|
-
* @param coll Collection name.
|
|
1030
|
-
* @param id Record ID.
|
|
1031
|
-
* @param options Optional request options.
|
|
1032
|
-
*/
|
|
1033
|
-
getRecord(coll, id, options) {
|
|
1034
|
-
return this.http.get(
|
|
1035
|
-
"/" + encodeURIComponent(coll) + "/" + encodeURIComponent(id),
|
|
1036
|
-
options
|
|
1037
|
-
);
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Create a record in a dynamic collection.
|
|
1041
|
-
* @param coll Collection name.
|
|
1042
|
-
* @param data Record fields.
|
|
1043
|
-
* @param options Optional request options.
|
|
1044
|
-
*/
|
|
1045
|
-
createRecord(coll, data, options) {
|
|
1046
|
-
return this.http.post(
|
|
1047
|
-
"/" + encodeURIComponent(coll),
|
|
1048
|
-
data,
|
|
1049
|
-
options
|
|
1050
|
-
);
|
|
1051
|
-
}
|
|
1052
|
-
/**
|
|
1053
|
-
* Update a record in a dynamic collection.
|
|
1054
|
-
* @param coll Collection name.
|
|
1055
|
-
* @param id Record ID.
|
|
1056
|
-
* @param data Updated record fields.
|
|
1057
|
-
* @param options Optional request options.
|
|
1058
|
-
*/
|
|
1059
|
-
updateRecord(coll, id, data, options) {
|
|
1060
|
-
return this.http.patch(
|
|
1061
|
-
"/" + encodeURIComponent(coll) + "/" + encodeURIComponent(id),
|
|
1062
|
-
data,
|
|
1063
|
-
options
|
|
1064
|
-
);
|
|
1065
|
-
}
|
|
1066
|
-
/**
|
|
1067
|
-
* Delete a record from a dynamic collection.
|
|
1068
|
-
* @param coll Collection name.
|
|
1069
|
-
* @param id Record ID.
|
|
1070
|
-
* @param options Optional request options.
|
|
1071
|
-
*/
|
|
1072
|
-
deleteRecord(coll, id, options) {
|
|
1073
|
-
return this.http.delete(
|
|
1074
|
-
"/" + encodeURIComponent(coll) + "/" + encodeURIComponent(id),
|
|
1075
|
-
options
|
|
1076
|
-
);
|
|
1077
|
-
}
|
|
1078
1130
|
};
|
|
1079
1131
|
|
|
1080
1132
|
// src/client.ts
|
|
@@ -1090,6 +1142,7 @@ export {
|
|
|
1090
1142
|
ApiError,
|
|
1091
1143
|
AuthStore,
|
|
1092
1144
|
CollectionService,
|
|
1145
|
+
CollectionsService,
|
|
1093
1146
|
FilesService,
|
|
1094
1147
|
LazypockClient,
|
|
1095
1148
|
RealtimeService,
|