@@ -4,6 +4,7 @@ import { Collection } from "./collection/collection";
44import { applyIndexes } from "./schema/indexes" ;
55import type { AnySchema , Schemas } from "./schema/schema" ;
66import { Schema } from "./schema/schema" ;
7+ import { applySearchIndexes } from "./schema/search-indexes" ;
78import { getValidator , type SchemaValidation , type Validator } from "./schema/validation" ;
89import type { DbCollections } from "./type-helpers" ;
910import { createAsyncLimiter , createAsyncResolver , type AsyncResolver } from "./utils/misc" ;
@@ -92,16 +93,22 @@ export class Database<TSchemas extends Schemas<any, any>> {
9293 /**
9394 * Creates collections with indexes and document validation if provided.
9495 *
95- * @param options - Init options
96+ * @param options - Init options. Pass `true` to run all steps, or an object to selectively enable steps.
97+ * @param collections - Select collections. Omit to initialize all collections, or an object to selectively enable collections.
9698 */
97- public async initialize ( options ?: InitOptions < keyof TSchemas [ "schemas" ] & string > ) : Promise < void > {
99+ public async initialize (
100+ options ?: InitOptions | true ,
101+ collections ?: InitCollections < keyof TSchemas [ "schemas" ] & string > ,
102+ ) : Promise < void > {
98103 const promises : Promise < void > [ ] = [ ] ;
99- const collections = Object . values ( this . collections ) . map ( ( c : Collection < any , any > ) : CollectionInit => {
100- const resolver = createAsyncResolver ( ) ;
101- promises . push ( resolver . promise ) ;
102- return { schema : c . schema , defaultValidation : this . options ?. validation , resolver } ;
103- } ) ;
104- initializeCollections ( this . db , collections , options ) ;
104+ const collectionInits = ( Object . values ( this . collections ) as Collection < any , any > [ ] )
105+ . filter ( ( c ) => ! collections || collections [ c . schema . name ] === true )
106+ . map ( ( c ) : CollectionInit => {
107+ const resolver = createAsyncResolver ( ) ;
108+ promises . push ( resolver . promise ) ;
109+ return { schema : c . schema , defaultValidation : this . options ?. validation , resolver } ;
110+ } ) ;
111+ initializeCollections ( this . db , collectionInits , options ) ;
105112 return Promise . all ( promises ) . then < void > ( ( ) => undefined ) ;
106113 }
107114
@@ -145,33 +152,40 @@ export function createDatabase<T extends Schemas<any, any>>(
145152 return new Database ( db , schemas , options ) ;
146153}
147154
148- type InitOptions < T extends string > = {
149- indexes ?: boolean ;
150- validation ?: boolean ;
151- collections ?: Partial < Record < T , true > > ;
155+ /**
156+ * Initialization options. When provided, only fields explicitly set to `true` will run.
157+ * When omitted, all initialization steps run.
158+ */
159+ export type InitOptions = {
160+ /** Create or update schema indexes. */
161+ indexes ?: true ;
162+ /** Create or update search indexes. */
163+ searchIndexes ?: true ;
164+ /** Apply document validation rules. */
165+ validation ?: true ;
152166} ;
153167
168+ /**
169+ * Limit initialization to specific collections.
170+ * When provided, only collections with a `true` value will be initialized.
171+ */
172+ type InitCollections < T extends string > = { [ K in T ] ?: true } ;
173+
154174type CollectionInit = {
155175 schema : AnySchema ;
156176 defaultValidation ?: SchemaValidation ;
157177 resolver : AsyncResolver ;
158178} ;
159179
160- function initializeCollections ( db : Db , collections : CollectionInit [ ] , options ?: InitOptions < any > ) {
180+ function initializeCollections ( db : Db , collections : CollectionInit [ ] , options ?: InitOptions | true ) {
181+ const opts = options === true ? undefined : options ;
161182 const run = createAsyncLimiter ( 10 ) ;
162183 const existingPromise = db
163184 . listCollections ( { } , { nameOnly : true } )
164185 . toArray ( )
165186 . then ( ( colls ) => new Set ( colls . map ( ( c ) => c . name ) ) ) ;
166187
167188 for ( const c of collections ) {
168- // Skip disabled collections
169- const enabled = options ?. collections ? options . collections [ c . schema . name ] === true : true ;
170- if ( ! enabled ) {
171- c . resolver . resolve ( ) ;
172- continue ;
173- }
174-
175189 run ( async ( ) => {
176190 const existing = await existingPromise ;
177191 const exists = existing . has ( c . schema . name ) ;
@@ -180,7 +194,7 @@ function initializeCollections(db: Db, collections: CollectionInit[], options?:
180194 // Get schema validation
181195 let validation : ( SchemaValidation & { validator : Validator } ) | undefined ;
182196 const validationOptions = schemaOptions . validation ?? c . defaultValidation ;
183- if ( ( options ?. validation ?? true ) && validationOptions ) {
197+ if ( ( opts === undefined || opts . validation ) && validationOptions ) {
184198 validation = { ...validationOptions , validator : getValidator ( c . schema ) } ;
185199 }
186200
@@ -194,9 +208,14 @@ function initializeCollections(db: Db, collections: CollectionInit[], options?:
194208 }
195209
196210 // Create schema indexes
197- if ( ( options ?. indexes ?? true ) && schemaOptions . indexes ) {
211+ if ( ( opts === undefined || opts . indexes ) && schemaOptions . indexes ) {
198212 await applyIndexes ( coll , schemaOptions . indexes ) ;
199213 }
214+
215+ // Create schema search indexes
216+ if ( ( opts === undefined || opts . searchIndexes ) && schemaOptions . searchIndexes ) {
217+ await applySearchIndexes ( coll , schemaOptions . searchIndexes ) ;
218+ }
200219 } )
201220 . then ( c . resolver . resolve )
202221 . catch ( c . resolver . reject ) ;
0 commit comments