@easy-cms/drizzle 1.8.0 → 1.9.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 (2) hide show
  1. package/dist/index.js +61 -0
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -1031,6 +1031,49 @@ var WhereBuilder = class {
1031
1031
  order.push(desc(col2(model, "id")));
1032
1032
  return order;
1033
1033
  }
1034
+ /**
1035
+ * The columns a cursor goes by, in `orderBy`'s order: the sort's, then `id`. Only plain
1036
+ * columns: a sort on a value inside blocks has nothing to compare a cursor with.
1037
+ */
1038
+ keyset(model, sort) {
1039
+ const keys = sort.map((entry) => {
1040
+ const descending = entry.startsWith("-");
1041
+ const path = descending ? entry.slice(1) : entry;
1042
+ const target = resolvePath(model, path.split("."), this.drafts, this.localization);
1043
+ if (target.kind !== "column")
1044
+ throw new QueryError(
1045
+ `A cursor can't follow a sort by "${path}"; sort by a field of the document, or use page`
1046
+ );
1047
+ return { column: target.column, descending };
1048
+ });
1049
+ keys.push({ column: col2(model, "id"), descending: true });
1050
+ return keys;
1051
+ }
1052
+ /**
1053
+ * Rows after `values` (the keys of the last row of the page before) in the keyset's order.
1054
+ * Where nulls go follows the database's default order, as `orderBy` leaves it: last in
1055
+ * ascending order on Postgres, first on SQLite.
1056
+ */
1057
+ after(keys, values) {
1058
+ const nullsHigh = this.dialect.name === "postgres";
1059
+ const equal = (key, value) => value === null ? isNull(key.column) : eq2(key.column, value);
1060
+ const later = (key, value) => {
1061
+ const nullsLater = nullsHigh !== key.descending;
1062
+ if (value === null) return nullsLater ? sql`1 = 0` : isNotNull(key.column);
1063
+ const beyond = key.descending ? lt(key.column, value) : gt(key.column, value);
1064
+ return nullsLater ? or(beyond, isNull(key.column)) : beyond;
1065
+ };
1066
+ const branches = keys.map(
1067
+ (key, i) => and2(...keys.slice(0, i).map((k, j) => equal(k, values[j])), later(key, values[i]))
1068
+ );
1069
+ const rest = or(...branches);
1070
+ const [first] = keys;
1071
+ const start = values[0];
1072
+ if (!first || start === null || start === void 0) return rest;
1073
+ const from = first.descending ? lte(first.column, start) : gte(first.column, start);
1074
+ const bound = nullsHigh !== first.descending ? or(from, isNull(first.column)) : from;
1075
+ return and2(bound, rest);
1076
+ }
1034
1077
  };
1035
1078
 
1036
1079
  // src/database.ts
@@ -1132,6 +1175,24 @@ var DrizzleDatabase = class {
1132
1175
  hasPrevPage: args.page > 1
1133
1176
  };
1134
1177
  }
1178
+ async findAfter(args) {
1179
+ const model = this.model(args.collection);
1180
+ const builder = this.builder(args.collection);
1181
+ const keys = builder.keyset(model.root, args.sort);
1182
+ const condition = this.where(args.collection, args.where);
1183
+ const after = args.after ? builder.after(keys, args.after) : void 0;
1184
+ const columns = Object.entries(getTableColumns(model.root.table));
1185
+ const names = keys.map(({ column }) => columns.find(([, c]) => c === column)?.[0]);
1186
+ const rows = await this.db.select().from(model.root.table).where(and3(condition, after)).orderBy(...builder.orderBy(model.root, args.sort)).limit(args.limit + 1);
1187
+ const more = rows.length > args.limit;
1188
+ const page = more ? rows.slice(0, args.limit) : rows;
1189
+ const last = page.at(-1);
1190
+ return {
1191
+ docs: await this.load(model, page),
1192
+ hasNextPage: more,
1193
+ next: last ? names.map((name) => last[name] ?? null) : null
1194
+ };
1195
+ }
1135
1196
  async findRows(args, take) {
1136
1197
  const model = this.model(args.collection);
1137
1198
  const condition = this.where(args.collection, args.where);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/drizzle",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Shared Drizzle ORM layer for Easy CMS database adapters",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,10 +23,10 @@
23
23
  "drizzle-orm": "0.45.4"
24
24
  },
25
25
  "peerDependencies": {
26
- "@easy-cms/core": "^1.8.0"
26
+ "@easy-cms/core": "^1.9.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@easy-cms/core": "^1.8.0"
29
+ "@easy-cms/core": "^1.9.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"