@monlite/core 1.4.0 → 2.0.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/dist/index.d.cts CHANGED
@@ -75,14 +75,22 @@ declare class Collection<T = Doc> {
75
75
  findManyCore(args?: FindManyArgs<T>): WithId<T>[];
76
76
  /** @internal Synchronous core of exists (used by reactivity). */
77
77
  existsCore(where: WhereInput<T> | undefined): boolean;
78
- findMany(args?: FindManyArgs<T>): Promise<WithId<T>[]>;
78
+ findMany<S extends Select<T> | undefined = undefined>(args?: Omit<FindManyArgs<T>, "select"> & {
79
+ select?: S;
80
+ }): Promise<Projected<T, S>[]>;
79
81
  /** Resolve one `$lookup` spec against already-fetched rows (2 queries, no N+1). */
80
82
  private applyLookup;
81
- findFirst(args?: FindFirstArgs<T>): Promise<WithId<T> | null>;
83
+ findFirst<S extends Select<T> | undefined = undefined>(args?: Omit<FindFirstArgs<T>, "select"> & {
84
+ select?: S;
85
+ }): Promise<Projected<T, S> | null>;
82
86
  /** Alias of {@link findFirst} for Prisma familiarity. */
83
- findUnique(args?: FindFirstArgs<T>): Promise<WithId<T> | null>;
87
+ findUnique<S extends Select<T> | undefined = undefined>(args?: Omit<FindFirstArgs<T>, "select"> & {
88
+ select?: S;
89
+ }): Promise<Projected<T, S> | null>;
84
90
  /** Like {@link findFirst} but throws if no document matches. */
85
- findFirstOrThrow(args?: FindFirstArgs<T>): Promise<WithId<T>>;
91
+ findFirstOrThrow<S extends Select<T> | undefined = undefined>(args?: Omit<FindFirstArgs<T>, "select"> & {
92
+ select?: S;
93
+ }): Promise<Projected<T, S>>;
86
94
  /** True if at least one document matches. */
87
95
  exists(where?: WhereInput<T>): Promise<boolean>;
88
96
  /**
@@ -301,9 +309,12 @@ interface FieldFilter<V = any> {
301
309
  }
302
310
  /** A value used directly as a filter is shorthand for `{ equals: value }`. */
303
311
  type FilterInput<V> = V | FieldFilter<V>;
312
+ /** A dot-notation nested path, e.g. `"address.city"`. */
313
+ type DotPath = `${string}.${string}`;
304
314
  /**
305
- * Where input. Known fields are typed from `T`; nested paths can also be
306
- * addressed with dot notation (e.g. `"address.city"`).
315
+ * Where input. For a **typed** collection, keys are checked against `T` (an
316
+ * unknown field is a type error); dot-notation nested paths are still allowed.
317
+ * For an **untyped** collection (`Doc`), any field is accepted (schema-free).
307
318
  */
308
319
  type WhereInput<T = Doc> = {
309
320
  [K in keyof T]?: FilterInput<T[K]>;
@@ -315,7 +326,7 @@ type WhereInput<T = Doc> = {
315
326
  OR?: WhereInput<T> | WhereInput<T>[];
316
327
  NOT?: WhereInput<T> | WhereInput<T>[];
317
328
  } & {
318
- [path: string]: any;
329
+ [path: DotPath]: FilterInput<any>;
319
330
  };
320
331
  /** Mongo-inspired update operators. */
321
332
  interface UpdateOperators {
@@ -332,17 +343,31 @@ interface UpdateOperators {
332
343
  type UpdateData<T = Doc> = (Partial<T> & Record<string, any>) | UpdateOperators;
333
344
  type SortOrder = "asc" | "desc";
334
345
  type OrderBy<T = Doc> = ({
335
- [K in keyof T]?: SortOrder;
346
+ [K in keyof WithId<T>]?: SortOrder;
336
347
  } & {
337
- [path: string]: SortOrder;
348
+ [path: DotPath]: SortOrder;
338
349
  }) | Array<{
339
- [path: string]: SortOrder;
350
+ [K in keyof WithId<T>]?: SortOrder;
351
+ } & {
352
+ [path: DotPath]: SortOrder;
340
353
  }>;
341
354
  type Select<T = Doc> = {
342
- [K in keyof T]?: boolean;
355
+ [K in keyof WithId<T>]?: boolean;
343
356
  } & {
344
- [path: string]: boolean;
357
+ [path: DotPath]: boolean;
358
+ };
359
+ /** True for the schema-free `Doc` (and `any`), so typed collections opt in only. */
360
+ type IsLoose<T> = string extends keyof T ? true : false;
361
+ /** Keys of a select set to a truthy value (a widened `boolean` counts as truthy). */
362
+ type SelectedKeys<S> = keyof {
363
+ [K in keyof S as S[K] extends false ? never : K]: K;
345
364
  };
365
+ /**
366
+ * The shape `findMany`/`findFirst` return for a given `select`. Untyped (`Doc`)
367
+ * collections and absent/empty selects return the full document; a typed select
368
+ * narrows to exactly the chosen fields.
369
+ */
370
+ type Projected<T, S> = IsLoose<T> extends true ? WithId<T> : [S] extends [undefined] ? WithId<T> : keyof S extends never ? WithId<T> : Pick<WithId<T>, Extract<SelectedKeys<S>, keyof WithId<T>>>;
346
371
  /**
347
372
  * A `$lookup`-style left join: for each result, fetch matching documents from
348
373
  * another collection and attach them. With `unwind`, emit one row per match
@@ -810,4 +835,4 @@ declare function objectId(): string;
810
835
  /** True when a value looks like a monlite/ObjectId id (24 hex chars). */
811
836
  declare function isObjectId(value: unknown): value is string;
812
837
 
813
- export { type AggregateArgs, type AggregateResult, type ApplyResult, Collection, type CollectionMode, type CollectionOptions, type CollectionSchema, type ColumnDef, type ColumnInfo, type ColumnType, type ConflictResolver, type ConflictRow, type CountArgs, type CreateArgs, type CreateManyArgs, Monlite as Db, type DeleteArgs, type Doc, type Driver, type DriverName, type DriverOpenOptions, type EncryptionOptions, type ExplainResult, type FieldFilter, type FieldSelection, type FilterInput, type FindFirstArgs, type FindManyArgs, type GroupByArgs, type GroupByResult, type HavingComparison, type HavingInput, type LiveEvent, type LocalChange, type LookupSpec, type MigrateOptions, Monlite, MonliteConstraintError, MonliteEncryptionError, MonliteError, MonliteForeignKeyError, MonliteNotNullError, type MonliteOptions, type MonlitePlugin, MonliteQueryError, MonliteUniqueConstraintError, type OrderBy, type PluginChange, type PreparedStatement, type RemoteChange, type RunResult, type Select, type SortOrder, type SyncOp, type SyncStateRow, SyncStore, type SystemFields, type UpdateArgs, type UpdateData, type UpdateOperators, type UpsertArgs, type Version, type WatchHandle, type WhereInput as WhereClause, type WhereInput, type WithId, compareVersions, createDb, isObjectId, makeVersion, normalizeDriverError, objectId, versionTs };
838
+ export { type AggregateArgs, type AggregateResult, type ApplyResult, Collection, type CollectionMode, type CollectionOptions, type CollectionSchema, type ColumnDef, type ColumnInfo, type ColumnType, type ConflictResolver, type ConflictRow, type CountArgs, type CreateArgs, type CreateManyArgs, Monlite as Db, type DeleteArgs, type Doc, type DotPath, type Driver, type DriverName, type DriverOpenOptions, type EncryptionOptions, type ExplainResult, type FieldFilter, type FieldSelection, type FilterInput, type FindFirstArgs, type FindManyArgs, type GroupByArgs, type GroupByResult, type HavingComparison, type HavingInput, type LiveEvent, type LocalChange, type LookupSpec, type MigrateOptions, Monlite, MonliteConstraintError, MonliteEncryptionError, MonliteError, MonliteForeignKeyError, MonliteNotNullError, type MonliteOptions, type MonlitePlugin, MonliteQueryError, MonliteUniqueConstraintError, type OrderBy, type PluginChange, type PreparedStatement, type Projected, type RemoteChange, type RunResult, type Select, type SortOrder, type SyncOp, type SyncStateRow, SyncStore, type SystemFields, type UpdateArgs, type UpdateData, type UpdateOperators, type UpsertArgs, type Version, type WatchHandle, type WhereInput as WhereClause, type WhereInput, type WithId, compareVersions, createDb, isObjectId, makeVersion, normalizeDriverError, objectId, versionTs };
package/dist/index.d.ts CHANGED
@@ -75,14 +75,22 @@ declare class Collection<T = Doc> {
75
75
  findManyCore(args?: FindManyArgs<T>): WithId<T>[];
76
76
  /** @internal Synchronous core of exists (used by reactivity). */
77
77
  existsCore(where: WhereInput<T> | undefined): boolean;
78
- findMany(args?: FindManyArgs<T>): Promise<WithId<T>[]>;
78
+ findMany<S extends Select<T> | undefined = undefined>(args?: Omit<FindManyArgs<T>, "select"> & {
79
+ select?: S;
80
+ }): Promise<Projected<T, S>[]>;
79
81
  /** Resolve one `$lookup` spec against already-fetched rows (2 queries, no N+1). */
80
82
  private applyLookup;
81
- findFirst(args?: FindFirstArgs<T>): Promise<WithId<T> | null>;
83
+ findFirst<S extends Select<T> | undefined = undefined>(args?: Omit<FindFirstArgs<T>, "select"> & {
84
+ select?: S;
85
+ }): Promise<Projected<T, S> | null>;
82
86
  /** Alias of {@link findFirst} for Prisma familiarity. */
83
- findUnique(args?: FindFirstArgs<T>): Promise<WithId<T> | null>;
87
+ findUnique<S extends Select<T> | undefined = undefined>(args?: Omit<FindFirstArgs<T>, "select"> & {
88
+ select?: S;
89
+ }): Promise<Projected<T, S> | null>;
84
90
  /** Like {@link findFirst} but throws if no document matches. */
85
- findFirstOrThrow(args?: FindFirstArgs<T>): Promise<WithId<T>>;
91
+ findFirstOrThrow<S extends Select<T> | undefined = undefined>(args?: Omit<FindFirstArgs<T>, "select"> & {
92
+ select?: S;
93
+ }): Promise<Projected<T, S>>;
86
94
  /** True if at least one document matches. */
87
95
  exists(where?: WhereInput<T>): Promise<boolean>;
88
96
  /**
@@ -301,9 +309,12 @@ interface FieldFilter<V = any> {
301
309
  }
302
310
  /** A value used directly as a filter is shorthand for `{ equals: value }`. */
303
311
  type FilterInput<V> = V | FieldFilter<V>;
312
+ /** A dot-notation nested path, e.g. `"address.city"`. */
313
+ type DotPath = `${string}.${string}`;
304
314
  /**
305
- * Where input. Known fields are typed from `T`; nested paths can also be
306
- * addressed with dot notation (e.g. `"address.city"`).
315
+ * Where input. For a **typed** collection, keys are checked against `T` (an
316
+ * unknown field is a type error); dot-notation nested paths are still allowed.
317
+ * For an **untyped** collection (`Doc`), any field is accepted (schema-free).
307
318
  */
308
319
  type WhereInput<T = Doc> = {
309
320
  [K in keyof T]?: FilterInput<T[K]>;
@@ -315,7 +326,7 @@ type WhereInput<T = Doc> = {
315
326
  OR?: WhereInput<T> | WhereInput<T>[];
316
327
  NOT?: WhereInput<T> | WhereInput<T>[];
317
328
  } & {
318
- [path: string]: any;
329
+ [path: DotPath]: FilterInput<any>;
319
330
  };
320
331
  /** Mongo-inspired update operators. */
321
332
  interface UpdateOperators {
@@ -332,17 +343,31 @@ interface UpdateOperators {
332
343
  type UpdateData<T = Doc> = (Partial<T> & Record<string, any>) | UpdateOperators;
333
344
  type SortOrder = "asc" | "desc";
334
345
  type OrderBy<T = Doc> = ({
335
- [K in keyof T]?: SortOrder;
346
+ [K in keyof WithId<T>]?: SortOrder;
336
347
  } & {
337
- [path: string]: SortOrder;
348
+ [path: DotPath]: SortOrder;
338
349
  }) | Array<{
339
- [path: string]: SortOrder;
350
+ [K in keyof WithId<T>]?: SortOrder;
351
+ } & {
352
+ [path: DotPath]: SortOrder;
340
353
  }>;
341
354
  type Select<T = Doc> = {
342
- [K in keyof T]?: boolean;
355
+ [K in keyof WithId<T>]?: boolean;
343
356
  } & {
344
- [path: string]: boolean;
357
+ [path: DotPath]: boolean;
358
+ };
359
+ /** True for the schema-free `Doc` (and `any`), so typed collections opt in only. */
360
+ type IsLoose<T> = string extends keyof T ? true : false;
361
+ /** Keys of a select set to a truthy value (a widened `boolean` counts as truthy). */
362
+ type SelectedKeys<S> = keyof {
363
+ [K in keyof S as S[K] extends false ? never : K]: K;
345
364
  };
365
+ /**
366
+ * The shape `findMany`/`findFirst` return for a given `select`. Untyped (`Doc`)
367
+ * collections and absent/empty selects return the full document; a typed select
368
+ * narrows to exactly the chosen fields.
369
+ */
370
+ type Projected<T, S> = IsLoose<T> extends true ? WithId<T> : [S] extends [undefined] ? WithId<T> : keyof S extends never ? WithId<T> : Pick<WithId<T>, Extract<SelectedKeys<S>, keyof WithId<T>>>;
346
371
  /**
347
372
  * A `$lookup`-style left join: for each result, fetch matching documents from
348
373
  * another collection and attach them. With `unwind`, emit one row per match
@@ -810,4 +835,4 @@ declare function objectId(): string;
810
835
  /** True when a value looks like a monlite/ObjectId id (24 hex chars). */
811
836
  declare function isObjectId(value: unknown): value is string;
812
837
 
813
- export { type AggregateArgs, type AggregateResult, type ApplyResult, Collection, type CollectionMode, type CollectionOptions, type CollectionSchema, type ColumnDef, type ColumnInfo, type ColumnType, type ConflictResolver, type ConflictRow, type CountArgs, type CreateArgs, type CreateManyArgs, Monlite as Db, type DeleteArgs, type Doc, type Driver, type DriverName, type DriverOpenOptions, type EncryptionOptions, type ExplainResult, type FieldFilter, type FieldSelection, type FilterInput, type FindFirstArgs, type FindManyArgs, type GroupByArgs, type GroupByResult, type HavingComparison, type HavingInput, type LiveEvent, type LocalChange, type LookupSpec, type MigrateOptions, Monlite, MonliteConstraintError, MonliteEncryptionError, MonliteError, MonliteForeignKeyError, MonliteNotNullError, type MonliteOptions, type MonlitePlugin, MonliteQueryError, MonliteUniqueConstraintError, type OrderBy, type PluginChange, type PreparedStatement, type RemoteChange, type RunResult, type Select, type SortOrder, type SyncOp, type SyncStateRow, SyncStore, type SystemFields, type UpdateArgs, type UpdateData, type UpdateOperators, type UpsertArgs, type Version, type WatchHandle, type WhereInput as WhereClause, type WhereInput, type WithId, compareVersions, createDb, isObjectId, makeVersion, normalizeDriverError, objectId, versionTs };
838
+ export { type AggregateArgs, type AggregateResult, type ApplyResult, Collection, type CollectionMode, type CollectionOptions, type CollectionSchema, type ColumnDef, type ColumnInfo, type ColumnType, type ConflictResolver, type ConflictRow, type CountArgs, type CreateArgs, type CreateManyArgs, Monlite as Db, type DeleteArgs, type Doc, type DotPath, type Driver, type DriverName, type DriverOpenOptions, type EncryptionOptions, type ExplainResult, type FieldFilter, type FieldSelection, type FilterInput, type FindFirstArgs, type FindManyArgs, type GroupByArgs, type GroupByResult, type HavingComparison, type HavingInput, type LiveEvent, type LocalChange, type LookupSpec, type MigrateOptions, Monlite, MonliteConstraintError, MonliteEncryptionError, MonliteError, MonliteForeignKeyError, MonliteNotNullError, type MonliteOptions, type MonlitePlugin, MonliteQueryError, MonliteUniqueConstraintError, type OrderBy, type PluginChange, type PreparedStatement, type Projected, type RemoteChange, type RunResult, type Select, type SortOrder, type SyncOp, type SyncStateRow, SyncStore, type SystemFields, type UpdateArgs, type UpdateData, type UpdateOperators, type UpsertArgs, type Version, type WatchHandle, type WhereInput as WhereClause, type WhereInput, type WithId, compareVersions, createDb, isObjectId, makeVersion, normalizeDriverError, objectId, versionTs };
package/dist/index.js CHANGED
@@ -1150,17 +1150,20 @@ var Collection = class {
1150
1150
  return this.db.prepare(`SELECT 1 FROM "${this.name}" WHERE ${clause} LIMIT 1`).get(...params) != null;
1151
1151
  }
1152
1152
  async findMany(args = {}) {
1153
- if (!args.lookup) return this.findManyCore(args);
1154
- const specs = Array.isArray(args.lookup) ? args.lookup : [args.lookup];
1153
+ const a = args;
1154
+ if (!a.lookup) {
1155
+ return this.findManyCore(a);
1156
+ }
1157
+ const specs = Array.isArray(a.lookup) ? a.lookup : [a.lookup];
1155
1158
  let rows = this.findManyCore({
1156
- ...args,
1159
+ ...a,
1157
1160
  select: void 0,
1158
1161
  lookup: void 0
1159
1162
  });
1160
1163
  for (const spec of specs) rows = await this.applyLookup(rows, spec);
1161
- if (args.select) {
1164
+ if (a.select) {
1162
1165
  rows = rows.map((r) => {
1163
- const projected = project(r, args.select);
1166
+ const projected = project(r, a.select);
1164
1167
  for (const spec of specs) projected[spec.as] = r[spec.as];
1165
1168
  return projected;
1166
1169
  });